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