usbmgmt/usbmgrtest/t_termusb/src/t_termusb.cpp
changeset 0 c9bc50fca66e
child 25 4ddb65515edd
equal deleted inserted replaced
-1:000000000000 0:c9bc50fca66e
       
     1 /*
       
     2 * Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * T_TERM.CPP
       
    16 * Dumb terminal
       
    17 *
       
    18 */
       
    19 
       
    20 #define VERBOSE
       
    21 #define _PC_CARD_SERIAL
       
    22 
       
    23 #include <e32test.h>
       
    24 #include <e32twin.h>
       
    25 #include <c32comm.h>
       
    26 #include <d32comm.h>
       
    27 #include <f32file.h>
       
    28 #include <hal.h>
       
    29 #include <usbman.h>
       
    30 
       
    31 #include <flogger.h>
       
    32 
       
    33 void StartLoader();
       
    34 
       
    35 const TPtrC KCaptureFileName=_L("C:\\CAPTURE.TXT");
       
    36 const TPtrC KUploadFileName=_L("C:\\UPLOAD.TXT");
       
    37 RFs TheFs;
       
    38 RFile TheCaptureFile;
       
    39 RFile TheUploadFile;
       
    40 
       
    41 TBuf8<1024> ch;
       
    42 TBuf8<1024> chw;
       
    43 TBuf<1024>	buf;
       
    44 TCommConfig TheConfigBuf;
       
    45 TCommConfigV01 &TheConfig=TheConfigBuf();
       
    46 TInt TheLastError=KErrNone;
       
    47 
       
    48 const TInt KMaxDumpLength=0x100;
       
    49 
       
    50 enum TTermPanic
       
    51 	{
       
    52 	EStraySignal,
       
    53 	ELoadPhysicalDeviceErr,
       
    54 	ELoadLogicalDeviceErr,
       
    55 	EOpenErr,
       
    56 	EConnectFsErr,
       
    57 	ECaptureFileOpen,
       
    58 	EOpenUploadFile,
       
    59 	};
       
    60 
       
    61 enum TRxMode
       
    62 	{
       
    63 	ENormal=0,
       
    64 	ELoopBack=1,
       
    65 	ECountChars=2,
       
    66 	ERxOff=3,
       
    67 	ECapture=128,
       
    68 	};
       
    69 
       
    70 struct SSettings
       
    71 	{
       
    72 	TBool iNotFinished;
       
    73 	TBool iLocalEcho;
       
    74 	TInt iAddLF;
       
    75 	TBool iDump;
       
    76 	TInt iDumpRepeat;
       
    77 	TBuf8<KMaxDumpLength> iDumpData;
       
    78 	TRxMode iRxMode;
       
    79 	TInt iCharCount;
       
    80 	TInt iMaxInOne;
       
    81 	TInt iInfraRed;
       
    82 	TBool iWaitAfterWrite;
       
    83 	// Fifo
       
    84 	// Brk
       
    85 	};
       
    86 
       
    87 LOCAL_D SSettings TheSettings;
       
    88 LOCAL_D RUsb TheUsbServer;
       
    89 LOCAL_D RCommServ TheCommServer;
       
    90 LOCAL_D RComm TheCommPort;
       
    91 LOCAL_D RConsole TheWindow;
       
    92 
       
    93 LOCAL_C TInt CommWriteSync(RComm &aComm, const TDesC8 &aData)
       
    94 	{
       
    95 	TRequestStatus stat;
       
    96 	aComm.Write(stat, aData);
       
    97 	User::WaitForRequest(stat);
       
    98 	return stat.Int();
       
    99 	}
       
   100 
       
   101 LOCAL_C TInt WaitAfterWrite(RComm& aComm)
       
   102 	{
       
   103 	TRequestStatus s;
       
   104 	TBuf8<1> b;
       
   105 	aComm.Write(s,b);
       
   106 	User::WaitForRequest(s);
       
   107 	return s.Int();
       
   108 	}
       
   109 
       
   110 LOCAL_C TInt RateToInt(TBps aRate)
       
   111 //
       
   112 //
       
   113 //
       
   114 	{
       
   115 
       
   116 	switch (aRate)
       
   117 		{
       
   118 	case EBps115200:	return 115200;
       
   119     case EBps57600:	return 57600;
       
   120     case EBps38400:	return 38400;
       
   121     case EBps19200:	return 19200;
       
   122     case EBps9600:	return 9600;
       
   123 	case EBps7200:	return 7200;
       
   124     case EBps4800:	return 4800;
       
   125 	case EBps3600:	return 3600;
       
   126     case EBps2400:	return 2400;
       
   127 	case EBps2000:	return 2000;
       
   128 	case EBps1800:	return 1800;
       
   129     case EBps1200:	return 1200;
       
   130     case EBps600:	return 600;
       
   131     case EBps300:	return 300;
       
   132     case EBps150:	return 150;
       
   133 	case EBps134:	return 134;
       
   134     case EBps110:	return 110;
       
   135 	case EBps75:	return 75;
       
   136 	case EBps50:	return 50;
       
   137 	default:	return -1;
       
   138 		}
       
   139 	}
       
   140 
       
   141 LOCAL_C TBps IntToRate(TInt aVal)
       
   142 //
       
   143 //
       
   144 //
       
   145 	{
       
   146 
       
   147 	if (aVal>=115200) return EBps115200;
       
   148 	if (aVal>=57600) return EBps57600;
       
   149 	if (aVal>=38400) return EBps38400;
       
   150 	if (aVal>=19200) return EBps19200;
       
   151 	if (aVal>=9600) return EBps9600;
       
   152 	if (aVal>=7200) return EBps7200;
       
   153 	if (aVal>=4800) return EBps4800;
       
   154 	if (aVal>=3600) return EBps3600;
       
   155 	if (aVal>=2400) return EBps2400;
       
   156 	if (aVal>=2000) return EBps2000;
       
   157 	if (aVal>=1800) return EBps1800;
       
   158 	if (aVal>=1200) return EBps1200;
       
   159 	if (aVal>=600) return EBps600;
       
   160 	if (aVal>=300) return EBps300;
       
   161 	if (aVal>=150) return EBps150;
       
   162 	if (aVal>=134) return EBps134;
       
   163 	if (aVal>=110) return EBps110;
       
   164 	if (aVal>=75) return EBps75;
       
   165 	if (aVal>=50) return EBps50;
       
   166 	return EBps50;
       
   167 	}
       
   168 
       
   169 LOCAL_C void ConfigString(TDes &aBuf, const TCommConfigV01 &aConfig, const SSettings &aSettings)
       
   170 //
       
   171 //	Construct a Configuaration string
       
   172 //
       
   173 	{
       
   174 
       
   175 	// Config
       
   176 	aBuf.Format(_L(" %d "), RateToInt(aConfig.iRate));
       
   177 	switch (aConfig.iParity)
       
   178 		{
       
   179 	case EParityEven: aBuf.Append(_L("E")); break;
       
   180 	case EParityOdd: aBuf.Append(_L("O")); break;
       
   181 	case EParityNone: aBuf.Append(_L("N")); break;
       
   182     default: break;
       
   183 		}
       
   184 	switch (aConfig.iDataBits)
       
   185 		{
       
   186 	case EData5: aBuf.Append(_L("5")); break;
       
   187 	case EData6: aBuf.Append(_L("6")); break;
       
   188 	case EData7: aBuf.Append(_L("7")); break;
       
   189 	case EData8: aBuf.Append(_L("8")); break;
       
   190     default: break;
       
   191 		}
       
   192 	if (aConfig.iStopBits==EStop1)
       
   193 		aBuf.Append(_L("1 "));
       
   194 	else
       
   195 		aBuf.Append(_L("2 "));
       
   196 
       
   197 	aBuf.Append(_L("Use:"));
       
   198 	if (aConfig.iHandshake==0)
       
   199 		aBuf.Append(_L("NoControl "));
       
   200 	if (aConfig.iHandshake&(KConfigObeyXoff|KConfigSendXoff))
       
   201 		aBuf.Append(_L("XonXoff "));
       
   202 	if (aConfig.iHandshake&KConfigObeyCTS)
       
   203 		aBuf.Append(_L("CTS/RTS "));
       
   204 	if (aConfig.iHandshake&KConfigObeyDSR)
       
   205 		aBuf.Append(_L("DSR/DTR "));
       
   206 	if (aConfig.iHandshake&KConfigWriteBufferedComplete)
       
   207 		aBuf.Append(_L("Early "));
       
   208 	//|KConfigObeyDCD|KConfigFailDCD|))
       
   209 
       
   210 
       
   211 //	if (aConfig.iBreak==TEiger::EBreakOn)
       
   212 //		aBuf.Append(_L("Brk "));
       
   213 	if (aConfig.iFifo==EFifoEnable)
       
   214 		aBuf.Append(_L("Fifo "));
       
   215 	
       
   216 	// Settings
       
   217 	if (aSettings.iLocalEcho)
       
   218 		aBuf.Append(_L("LocalEcho "));
       
   219 	if (aSettings.iAddLF)
       
   220 		aBuf.Append(_L("AddLF "));
       
   221 	// rx mode
       
   222 	if ((aSettings.iRxMode&~ECapture)==ELoopBack)
       
   223 		aBuf.Append(_L("LpBk"));
       
   224 	else if ((aSettings.iRxMode&~ECapture)==ECountChars)
       
   225 		aBuf.Append(_L("CtCh"));
       
   226 	//else if (aSettings.iRxMode==ERxOff)
       
   227 		//{
       
   228 		//aBuf.Append(_L("NoRx"));
       
   229 		//}
       
   230 	aBuf.Append(_L(" "));
       
   231 	aBuf.AppendNum((TInt)(RThread().Priority()));
       
   232 	if (aSettings.iInfraRed==1)
       
   233 		aBuf.Append(_L("IR1"));
       
   234 	else if (aSettings.iInfraRed==2)
       
   235 		aBuf.Append(_L("IR2"));
       
   236 	if (aSettings.iWaitAfterWrite)
       
   237 		aBuf.Append(_L("Wait"));
       
   238 
       
   239 	aBuf.Append(_L("Last Err: "));
       
   240 	if (TheLastError==KErrNone)
       
   241 		aBuf.Append(_L("None "));
       
   242 	else if (TheLastError==KErrCommsLineFail)
       
   243 		aBuf.Append(_L("LineFail "));
       
   244 	else if (TheLastError==KErrCommsFrame)
       
   245 		aBuf.Append(_L("Frame "));
       
   246 	else if (TheLastError==KErrCommsOverrun)
       
   247 		aBuf.Append(_L("Overrun "));
       
   248 	else if (TheLastError==KErrCommsParity)
       
   249 		aBuf.Append(_L("Parity "));
       
   250 	else if (TheLastError==KErrAbort)
       
   251 		aBuf.Append(_L("Abort "));
       
   252 	else if (TheLastError==KErrBadPower)
       
   253 		aBuf.Append(_L("BadPower "));
       
   254 	else if (TheLastError==KErrNotReady)
       
   255 		aBuf.Append(_L("NotReady "));
       
   256 	else
       
   257 		aBuf.AppendNum(TheLastError);
       
   258 	}
       
   259 
       
   260 LOCAL_C void GetRate(TBps &aRate, const TDesC &aDes)
       
   261 //
       
   262 //	Set Baud rate
       
   263 //
       
   264 	{
       
   265 
       
   266 	TInt32 i;
       
   267 	if (TLex(aDes).Val(i)==KErrNone)
       
   268 		aRate=IntToRate(i);
       
   269 	}
       
   270 
       
   271 LOCAL_C void GetParity(TParity &aParity, const TDesC &aDes)
       
   272 //
       
   273 //
       
   274 //
       
   275 	{
       
   276 
       
   277 	if (aDes.FindF(_L("O"))>=0)
       
   278 		aParity=EParityOdd;
       
   279   	if (aDes.FindF(_L("E"))>=0)
       
   280 		aParity=EParityEven;
       
   281 	if (aDes.FindF(_L("N"))>=0)
       
   282 		aParity=EParityNone;
       
   283 	}
       
   284 
       
   285 LOCAL_C void GetHandshake(TUint &aHandshake, const TDesC &aDes)
       
   286 //
       
   287 //
       
   288 //
       
   289 	{
       
   290 
       
   291 	if (aDes.FindF(_L("N"))>=0)
       
   292 		aHandshake=0;
       
   293 	if (aDes.FindF(_L("X"))>=0)
       
   294 		aHandshake=KConfigObeyXoff|KConfigSendXoff;
       
   295 	if (aDes.FindF(_L("C"))>=0)
       
   296 		aHandshake=KConfigObeyCTS;
       
   297 	if (aDes.FindF(_L("D"))>=0)
       
   298 		aHandshake=KConfigObeyDSR|KConfigFreeRTS;
       
   299 	if (aDes.FindF(_L("E"))>=0)
       
   300 		aHandshake|=KConfigWriteBufferedComplete;
       
   301 	}
       
   302 
       
   303 LOCAL_C void GetStopBit(TStopBits &aStop, const TDesC &aDes)
       
   304 	{
       
   305 
       
   306 	TInt32 in;
       
   307 	if (TLex(aDes).Val(in)==KErrNone)
       
   308 		{
       
   309 		if (in==1)
       
   310 			aStop=EStop1;
       
   311 		if (in==2)
       
   312 			aStop=EStop2;
       
   313 		}
       
   314 	else
       
   315 		{
       
   316 		if (aStop==EStop1)
       
   317 			aStop=EStop2;
       
   318 		else
       
   319 			aStop=EStop1;
       
   320 		}
       
   321 	}
       
   322 
       
   323 LOCAL_C void GetLength(TDataBits &aData, const TDesC &aDes)
       
   324 	{
       
   325 
       
   326 	TInt32 in;
       
   327 	if (TLex(aDes).Val(in)==KErrNone)
       
   328 		{
       
   329 		switch (in)
       
   330 			{
       
   331 		case 5: aData=EData5; break;
       
   332 		case 6: aData=EData6; break;
       
   333 		case 7: aData=EData7; break;
       
   334 		case 8: aData=EData8; break;
       
   335 		default: break;
       
   336 			}
       
   337 		}
       
   338 	}
       
   339 
       
   340 LOCAL_C void GetInfraRedMode(TInt &aInfraRed, const TDesC &aDes)
       
   341 	{
       
   342 
       
   343 	if (aDes.FindF(_L("0"))>=0)
       
   344 		aInfraRed=0;
       
   345 	else if (aDes.FindF(_L("1"))>=0)
       
   346 		aInfraRed=1;
       
   347 	else if (aDes.FindF(_L("2"))>=0)
       
   348 		aInfraRed=2;
       
   349 	}
       
   350 
       
   351 LOCAL_C void GetWaitMode(TBool &aWait, const TDesC &aDes)
       
   352 	{
       
   353 
       
   354 	if (aDes.FindF(_L("0"))>=0)
       
   355 		aWait=EFalse;
       
   356 	else if (aDes.FindF(_L("1"))>=0)
       
   357 		aWait=ETrue;
       
   358 	}
       
   359 
       
   360 /*LOCAL_C void GetBreak(const TDesC &aDes)
       
   361 	{
       
   362 
       
   363 	if (aDes==_L(""))
       
   364 		{
       
   365 		if (data.iBreak==TEiger::EBreakOn)
       
   366 			data.iBreak=TEiger::EBreakOff;
       
   367 		else
       
   368 			data.iBreak=TEiger::EBreakOn;
       
   369 		}
       
   370 	if (aDes.FindF(_L("N"))>=0)
       
   371 		data.iBreak=TEiger::EBreakOn;
       
   372 	if (aDes.FindF(_L("F"))>=0)
       
   373 		data.iBreak=TEiger::EBreakOff;
       
   374 	SetConfig();
       
   375 	}
       
   376 */
       
   377 LOCAL_C void GetFifo(TUint& aFifo, const TDesC &aDes)
       
   378 	{
       
   379 
       
   380 	if (aDes==_L(""))
       
   381 		{
       
   382 		if (aFifo==EFifoEnable)
       
   383 			aFifo=EFifoDisable;
       
   384 		else
       
   385 			aFifo=EFifoEnable;
       
   386 		}
       
   387 	if (aDes.FindF(_L("N"))>=0)
       
   388 		aFifo=EFifoEnable;
       
   389 	if (aDes.FindF(_L("F"))>=0)
       
   390 		aFifo=EFifoDisable;
       
   391 	}
       
   392 
       
   393 LOCAL_C void GetEcho(TBool &aEcho, const TDesC &aDes)
       
   394 	{
       
   395 
       
   396 	if (aDes==_L(""))
       
   397 		{
       
   398 		if (aEcho)
       
   399 			aEcho=EFalse;
       
   400 		else
       
   401 			aEcho=ETrue;
       
   402 		}
       
   403 	if (aDes.FindF(_L("N"))>=0)
       
   404 		aEcho=ETrue;
       
   405 	if (aDes.FindF(_L("F"))>=0)
       
   406 		aEcho=EFalse;
       
   407 	}
       
   408 
       
   409 LOCAL_C void GetRxMode(TRxMode &aMode, const TDesC &aDes)
       
   410 	{
       
   411 
       
   412 	if (aDes.FindF(_L("O"))>=0)
       
   413 		aMode=ERxOff;
       
   414 	if (aDes.FindF(_L("N"))>=0)
       
   415 		aMode=ENormal;
       
   416 	if (aDes.FindF(_L("L"))>=0)
       
   417 		aMode=ELoopBack;
       
   418 	if (aDes.FindF(_L("C"))>=0)
       
   419 		aMode=ECountChars;
       
   420 	if (aDes.FindF(_L("S"))>=0)
       
   421 		{
       
   422 		aMode=TRxMode(TInt(aMode)|ECapture);
       
   423 		TInt r=TheCaptureFile.Create(TheFs,KCaptureFileName,EFileWrite);
       
   424 		if (r!=KErrNone)
       
   425 			User::Panic(_L("T_TERM CAP"),r);
       
   426 		}
       
   427 	if (aDes.FindF(_L("Z"))>=0)
       
   428 		{
       
   429 		aMode=TRxMode(TInt(aMode)&~ECapture);
       
   430 		TheCaptureFile.Close();
       
   431 		}
       
   432 	if (aDes.FindF(_L("0"))>=0)
       
   433 		RThread().SetPriority(EPriorityNormal);
       
   434 	if (aDes.FindF(_L("1"))>=0)
       
   435 		RThread().SetPriority(EPriorityAbsoluteHigh);
       
   436 	}
       
   437 
       
   438 LOCAL_C void GetDump(SSettings &aSettings, const TDesC &aDes)
       
   439 	{
       
   440 	
       
   441 	TInt32 in;
       
   442 	if (TLex(aDes).Val(in)==KErrNone)
       
   443 		{
       
   444 		aSettings.iDump=ETrue;
       
   445 		aSettings.iDumpRepeat=in;
       
   446 		return;
       
   447 		}
       
   448 	if (aDes.Length()!=0)
       
   449 		{
       
   450 		TBuf8<16> b=_L8("0123456789ABCDEF");
       
   451 		aSettings.iDumpData.Zero();
       
   452 		TInt i;
       
   453 		for (i=0; i<16; i++)
       
   454 			aSettings.iDumpData+=b;
       
   455 		return;
       
   456 		}
       
   457 	RConsole dialog;
       
   458 	TInt r=dialog.Init(_L("Type data to dump to comm.  Escape to finish"),TSize(KConsFullScreen,KConsFullScreen));
       
   459 	r=dialog.Control(_L("+Maximize +NewLine"));
       
   460 	aSettings.iDumpData=_L8("");
       
   461 	TConsoleKey k;
       
   462 	do 
       
   463 		{
       
   464 		dialog.Read(k);
       
   465 		if (k.Code()==EKeyEscape)
       
   466 			break;
       
   467 		TText a=(TText)k.Code();
       
   468 		TPtrC s(&a,1);
       
   469 		dialog.Write(s);
       
   470 		aSettings.iDumpData.Append(k.Code());
       
   471 		//if (a=='\r')
       
   472 		//	dialog.Write(_L("\n"));
       
   473 		} while (aSettings.iDumpData.Length()<KMaxDumpLength);
       
   474 
       
   475 	dialog.Destroy();
       
   476 	dialog.Close();
       
   477 	}
       
   478 
       
   479 
       
   480 
       
   481 LOCAL_C void CommandWindow(TCommConfigV01 &aConfig, SSettings &aSettings)
       
   482 //
       
   483 //	Display some words of wisdom and get a command from the user
       
   484 //
       
   485 	{
       
   486 
       
   487 	TBuf<32> b;
       
   488 	b.Num(aSettings.iCharCount);
       
   489 	b+=_L(" ");
       
   490 	b.AppendNum(aSettings.iMaxInOne);
       
   491 	b+=_L("\n");
       
   492 	RConsole dialog;
       
   493 	TInt r=dialog.Init(_L("."),TSize(KConsFullScreen,KConsFullScreen));
       
   494 	r=dialog.Control(_L("+Maximize +NewLine"));
       
   495 	dialog.Write(_L("B<n> Set Bps to n               P[Odd|Even|None] Set Parity\n"));
       
   496 	dialog.Write(_L("S[1|2] Set/Toggle stop bits     L<n> Set Data Length (5<=n<=8)\n"));
       
   497 	dialog.Write(_L("K[On|Off] Set/Toggle BRK        F[On|Off] Set/Toggle Fifo\n"));
       
   498 	dialog.Write(_L("H[None|X|CtsRts|DsrDtr] Handshaking\n"));
       
   499 	dialog.Write(_L("D[<n>] Set data or Dump data n times\n"));
       
   500  	dialog.Write(_L("J Toggle Add Line Feed          E Toggle local Echo\n"));
       
   501 	dialog.Write(_L("U [NLCO] Set Rx Mode to Normal, Loopback, Count, Off \n"));
       
   502 	dialog.Write(b);
       
   503 	dialog.Write(_L("Q Quit\n"));
       
   504 	dialog.Write(_L("\n:"));
       
   505 
       
   506 	//	Get a command
       
   507 	TBuf<0x80> des=_L("");
       
   508 	TConsoleKey k;
       
   509 	dialog.Read(k);
       
   510 	while ((k.Code()!='\r') && (k.Code()!=EKeyEscape))
       
   511 		{
       
   512 		TText a=(TText)k.Code();
       
   513 		TPtrC s(&a,1);
       
   514 		dialog.Write(s);
       
   515 		des.Append(k.Code());
       
   516 		dialog.Read(k);
       
   517 		}
       
   518 
       
   519 	if (k.Code()!=EKeyEscape && des.Length()>0)
       
   520 		{
       
   521 		des.UpperCase();
       
   522 		TBuf<0x80> right(des.Right(des.Length()-1));
       
   523 		if (des[0]=='B')
       
   524 			GetRate(aConfig.iRate, right);
       
   525 		if (des[0]=='P')
       
   526 			GetParity(aConfig.iParity, right);
       
   527 		if (des[0]=='S')
       
   528 			GetStopBit(aConfig.iStopBits, right);
       
   529 		if (des[0]=='L')
       
   530 			GetLength(aConfig.iDataBits, right);
       
   531 //		if (des[0]=='K')
       
   532 //			GetBreak(aSettings.iBreak, right);
       
   533 		if (des[0]=='F')
       
   534 			GetFifo(aConfig.iFifo, right);
       
   535 		if (des[0]=='I')
       
   536 			GetInfraRedMode(aSettings.iInfraRed, right);
       
   537 		if (aSettings.iInfraRed==1)
       
   538 			{
       
   539 			aConfig.iSIREnable=ESIREnable;
       
   540 			aConfig.iSIRSettings=KConfigSIRPulseWidthMinimum;
       
   541 			}
       
   542 		else if (aSettings.iInfraRed==2)
       
   543 			{
       
   544 			aConfig.iSIREnable=ESIREnable;
       
   545 			aConfig.iSIRSettings=KConfigSIRPulseWidthMaximum;
       
   546 			}
       
   547 		else
       
   548 			{
       
   549 			aConfig.iSIREnable=ESIRDisable;
       
   550 			aConfig.iSIRSettings=0;
       
   551 			}
       
   552 		if (des[0]=='H')
       
   553 			GetHandshake(aConfig.iHandshake, right);
       
   554 		if (des[0]=='E')
       
   555 			GetEcho(aSettings.iLocalEcho, right);
       
   556 		if (des[0]=='D')
       
   557 			GetDump(aSettings, right);
       
   558 		if (des[0]=='J')
       
   559 			aSettings.iAddLF=!aSettings.iAddLF;
       
   560 		if (des[0]=='U')
       
   561 			{
       
   562 			GetRxMode(aSettings.iRxMode, right);
       
   563 			aSettings.iCharCount=0;
       
   564 			aSettings.iMaxInOne=0;
       
   565 			}
       
   566 		if (des[0]=='Q')
       
   567 			aSettings.iNotFinished=EFalse;
       
   568 		if (des[0]=='W')
       
   569 			GetWaitMode(aSettings.iWaitAfterWrite, right);
       
   570 		}
       
   571 
       
   572 	dialog.Destroy();
       
   573 	dialog.Close();
       
   574 	}
       
   575 
       
   576 // The following decl is a hack for the Eiger build.
       
   577 // Without it T_TERM.EXE has no .data or .bss section.  This means the data offset
       
   578 // field in the file header is zero.  When the kernel comes to copy the data sections
       
   579 // from rom into ram it reads the data size field (which is the size of all data
       
   580 // sections) from the header and tries to copy data from 0x00000000 (the data offset),
       
   581 // causing a data abort.
       
   582 TInt dummy=10;	 
       
   583 #if defined (__WINS__)
       
   584 #define PDD_NAME _L("ECDRV")
       
   585 #define LDD_NAME _L("ECOMM")
       
   586 #else
       
   587 #define PDD_NAME _L("EUARTn")
       
   588 #define LDD_NAME _L("EUSBC")
       
   589 #endif
       
   590 
       
   591 #define CSY_NAME _L("ECACM")
       
   592 #define PORT_NAME _L("ACM::0")
       
   593 
       
   594 LOCAL_C void ProcessError(TInt anError)
       
   595 	{
       
   596 	TBuf<80> buf;
       
   597 	if (anError!=KErrNone)
       
   598 		{
       
   599 		TheLastError=anError;
       
   600 		ConfigString(buf, TheConfig, TheSettings);
       
   601 		TheWindow.SetTitle(buf);
       
   602 		}
       
   603 	}
       
   604 
       
   605 LOCAL_C void HandleRx(TRequestStatus& aStatus, TBool aFinish)
       
   606 	{
       
   607 	chw.Copy(ch);
       
   608 	switch(TheSettings.iRxMode & ~ECapture)
       
   609 		{
       
   610 		case ENormal:
       
   611 			{
       
   612 			buf.Copy(chw);
       
   613 			TheWindow.Write(buf);
       
   614 			break;
       
   615 			}
       
   616 		case ELoopBack:
       
   617 			{
       
   618 			ProcessError(CommWriteSync(TheCommPort,chw));
       
   619 			if (TheSettings.iWaitAfterWrite)
       
   620 				ProcessError(WaitAfterWrite(TheCommPort));
       
   621 			break;
       
   622 			}
       
   623 		case ECountChars:
       
   624 			{
       
   625 			TInt l=chw.Length();
       
   626 			TheSettings.iCharCount+=l;
       
   627 			if (l>TheSettings.iMaxInOne)
       
   628 				TheSettings.iMaxInOne=l;
       
   629 			break;
       
   630 			}
       
   631 		}
       
   632 	if (TheSettings.iRxMode & ECapture)
       
   633 		TheCaptureFile.Write(chw);
       
   634 	if (TheSettings.iRxMode!=ERxOff)
       
   635 		{
       
   636 		if ((TheSettings.iRxMode & ~ECapture)==ELoopBack && !aFinish)
       
   637 			TheCommPort.Read(aStatus, ch);
       
   638 		else
       
   639 			TheCommPort.ReadOneOrMore(aStatus, ch);
       
   640 		}
       
   641 	}
       
   642 
       
   643 GLDEF_C TInt E32Main()
       
   644 //
       
   645 // Term
       
   646 //
       
   647     {
       
   648 	// Open the window asap
       
   649 	TheWindow.Init(_L("TERM"),TSize(KConsFullScreen,KConsFullScreen));
       
   650 	RDebug::Print(_L("E32Main: Initialised Window!"));
       
   651 
       
   652     // Initialisation
       
   653 	TBuf <0x100> cmd;
       
   654 	User::CommandLine(cmd);
       
   655 
       
   656 	// Load Device Drivers
       
   657 	TInt r;
       
   658 
       
   659 	TheWindow.Write(_L("Starting\r\n"));
       
   660 	r=User::LoadLogicalDevice(LDD_NAME);
       
   661 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   662 		{
       
   663     	TConsoleKey keystroke;
       
   664 		TBuf<32> outBuf;
       
   665 		outBuf.AppendFormat(_L("Failed 0x%X\n\r"),r);
       
   666 		TheWindow.Write(outBuf);
       
   667 		TheWindow.Read(keystroke);
       
   668 		User::Panic(_L("T_TERM"), ELoadLogicalDeviceErr);
       
   669 		}
       
   670 
       
   671 	TheWindow.Write(_L("Loaded LDD\r\n"));
       
   672 
       
   673 	r=TheFs.Connect();
       
   674 	if (r!=KErrNone)
       
   675 		User::Panic(_L("T_TERM"), EConnectFsErr);
       
   676 
       
   677 	TheWindow.Write(_L("Connected to file server\r\n"));
       
   678 
       
   679 	TheSettings.iNotFinished=ETrue;
       
   680 	TheSettings.iLocalEcho=ETrue;
       
   681 	TheSettings.iAddLF=FALSE;
       
   682 	TheSettings.iDump=EFalse;
       
   683 	TheSettings.iDumpRepeat=1;
       
   684 	TheSettings.iDumpData=_L8("Some Text\r");
       
   685 	TheSettings.iRxMode=ENormal;
       
   686 	TheSettings.iCharCount=0;
       
   687 	TheSettings.iMaxInOne=0;
       
   688 	TheSettings.iInfraRed=0;
       
   689 	TheSettings.iWaitAfterWrite=EFalse;
       
   690 	
       
   691 	r = StartC32();
       
   692 	if (r!=KErrNone && r !=KErrAlreadyExists)
       
   693 		{
       
   694 		RDebug::Print(_L("E32Main: Failed to start C32. Error = %d"), r);
       
   695 		User::Panic(_L("T_TERM"), EOpenErr);
       
   696 		}
       
   697 
       
   698 	TheWindow.Write(_L("Started c32\r\n"));
       
   699 
       
   700 	// Comms Config
       
   701 	r = TheUsbServer.Connect();
       
   702 	if (r!=KErrNone)
       
   703 		{
       
   704 		RDebug::Print(_L("E32Main: Failed to connect to UsbMan Server. Error = %d"), r);
       
   705 		User::Panic(_L("T_TERM"), EOpenErr);
       
   706 		}
       
   707 
       
   708 	TheWindow.Write(_L("Connected to UsbMan Server\r\n"));
       
   709 
       
   710 	TRequestStatus status;
       
   711 	TheUsbServer.Start(status);
       
   712 	User::WaitForRequest(status);
       
   713 
       
   714 	if (status.Int() != KErrNone)
       
   715 		{
       
   716 		RDebug::Print(_L("E32Main: Unable to start USB services. Error %d"), status.Int());
       
   717 		User::Panic(_L("T_TERM"), EOpenErr);
       
   718 		}
       
   719 
       
   720 	TheWindow.Write(_L("Started USB services\r\n"));
       
   721 
       
   722 	r = TheCommServer.Connect();
       
   723 	if (r!=KErrNone)
       
   724 		{
       
   725 		RDebug::Print(_L("E32Main: Failed to Connect to C32. Error = %d"), r);
       
   726 		User::Panic(_L("T_TERM"), EOpenErr);
       
   727 		}
       
   728 
       
   729 	TheWindow.Write(_L("Connected to C32 Server\r\n"));
       
   730 
       
   731 	r = TheCommServer.LoadCommModule(CSY_NAME);
       
   732 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   733 		{
       
   734 		RDebug::Print(_L("E32Main: Failed to load USB CSY. Error = %d"), r);
       
   735 		User::Panic(_L("T_TERM"), EOpenErr);
       
   736 		}
       
   737 
       
   738 	TheWindow.Write(_L("Loaded USB CSY\r\n"));
       
   739 
       
   740 	r = TheCommPort.Open(TheCommServer, PORT_NAME,ECommExclusive); // Comm port
       
   741 	if (r!=KErrNone)
       
   742 		{
       
   743 		RDebug::Print(_L("E32Main: Failed to Open USB Comm Port. Error = %d"), r);
       
   744 		User::Panic(_L("T_TERM"), EOpenErr);
       
   745 		}
       
   746 
       
   747 	TheWindow.Write(_L("Opened USB Comm Port\r\n"));
       
   748 
       
   749 	TheCommPort.Config(TheConfigBuf);	// get config
       
   750 	TheConfig.iHandshake=0; //KConfigObeyXoff|KConfigSendXoff;
       
   751 	TheCommPort.SetConfig(TheConfigBuf);
       
   752 	TheCommPort.SetReceiveBufferLength(8192);
       
   753 
       
   754 	//	Set up a console window
       
   755 	TheWindow.Control(_L("+Maximize +Newline"));
       
   756 	TheWindow.Write(_L("Startup complete\r\n\nType '*' for commands\r\n"));
       
   757 	TBuf<0x80> buf;
       
   758 	ConfigString(buf, TheConfig, TheSettings);
       
   759 	TheWindow.SetTitle(buf);
       
   760 
       
   761 	TConsoleKey k;
       
   762 	TRequestStatus readStat, keyStat;
       
   763 
       
   764 	// main loop
       
   765 	TheWindow.Read(k, keyStat);
       
   766 	TheCommPort.ReadOneOrMore(readStat, ch);
       
   767 	do
       
   768 		{
       
   769 		User::WaitForRequest(readStat, keyStat);
       
   770 		if (keyStat!=KRequestPending)
       
   771 			{
       
   772 			TUint32 c=k.Code();
       
   773 			switch(c)
       
   774     			{
       
   775     			case '\x3':
       
   776     				{
       
   777     				TheCommPort.ReadCancel();
       
   778     				HandleRx(readStat,ETrue);
       
   779     				}
       
   780     				break;
       
   781                 case '*':
       
   782 		    		{
       
   783 		    		CommandWindow(TheConfig, TheSettings);
       
   784 		    		TheCommPort.ReadCancel();
       
   785 		    		TheCommPort.SetConfig(TheConfigBuf);
       
   786 		    		if (TheSettings.iRxMode!=ERxOff)
       
   787 		    			TheCommPort.ReadOneOrMore(readStat, ch);
       
   788 		    		ConfigString(buf, TheConfig, TheSettings);
       
   789 		    		TheWindow.SetTitle(buf);
       
   790 		    		}
       
   791 		    		break;
       
   792 		    	case '\x15':
       
   793 		    		{
       
   794 		    		TInt r=TheUploadFile.Open(TheFs,KUploadFileName,EFileRead);
       
   795 		    		if (r!=KErrNone)
       
   796 		    			User::Panic(_L("T_TERM"),EOpenUploadFile);
       
   797 		    		TBuf8<0x100> buf;
       
   798 		    		do	{
       
   799 		    			TheUploadFile.Read(buf);
       
   800 		    			ProcessError(CommWriteSync(TheCommPort,buf));
       
   801 		    			if (TheSettings.iWaitAfterWrite)
       
   802 		    				ProcessError(WaitAfterWrite(TheCommPort));
       
   803 		    			} while(buf.Length()!=0);
       
   804 		    		TheUploadFile.Close();
       
   805 		    		}
       
   806 		    		break;
       
   807 		    	default:
       
   808 		    		if (c<256)
       
   809 		    			{
       
   810 		    			TText8 a8=(TText8)c;
       
   811 		    			TText a=(TText)c;
       
   812 		    			TPtrC8 s8(&a8,1);
       
   813 		    			TPtrC s(&a,1);
       
   814 		    			ProcessError(CommWriteSync(TheCommPort, s8));
       
   815 		    			if (TheSettings.iWaitAfterWrite)
       
   816 		    				ProcessError(WaitAfterWrite(TheCommPort));
       
   817 		    			if (TheSettings.iLocalEcho)
       
   818 		    				{
       
   819 		    				TheWindow.Write(s);
       
   820 		    				if (c=='\r' && TheSettings.iAddLF) TheWindow.Write(_L("\n"));
       
   821 		    				}
       
   822 		    			}
       
   823 		    		break;
       
   824 		    	}
       
   825 			TheWindow.Read(k, keyStat);
       
   826 			}
       
   827 		else if (readStat!=KRequestPending)
       
   828 			{
       
   829 			ProcessError(readStat.Int());
       
   830 			if (readStat!=KErrAbort && readStat!=KErrBadPower && readStat!=KErrNotReady)
       
   831 				HandleRx(readStat,EFalse);
       
   832 			else
       
   833 				{
       
   834 				if (TheSettings.iRxMode!=ERxOff)
       
   835 					TheCommPort.ReadOneOrMore(readStat, ch);
       
   836 				}
       
   837 			}
       
   838 		else
       
   839 			{
       
   840 			User::Panic(_L("T_TERM"), EStraySignal);
       
   841 			}
       
   842 
       
   843 		if (TheSettings.iDump)
       
   844 			{
       
   845 			TheSettings.iDump=EFalse;
       
   846 			TInt i;
       
   847 			for (i=0; i<TheSettings.iDumpRepeat; i++)
       
   848 				{
       
   849 				ProcessError(CommWriteSync(TheCommPort, TheSettings.iDumpData));
       
   850 				if (TheSettings.iWaitAfterWrite)
       
   851 					ProcessError(WaitAfterWrite(TheCommPort));
       
   852 				}
       
   853 			}
       
   854 		} while(TheSettings.iNotFinished);
       
   855 
       
   856 	TheWindow.Destroy();
       
   857 	TheWindow.Close();
       
   858 	TheCommPort.Close();
       
   859 	TheCommServer.Close();
       
   860 	TheUsbServer.Stop();
       
   861 	TheUsbServer.Close();
       
   862 
       
   863 	return(KErrNone);
       
   864     }
       
   865