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