messagingfw/msgtest/targetautomation/Trgtest/trgtest.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32utils\trgtest\trgtesth.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32err.h>
       
    19 #include <windows.h>
       
    20 #include <stdio.h>
       
    21 
       
    22 const TUint8 SOH=0x01;
       
    23 const TUint8 STX=0x02;
       
    24 const TUint8 EOT=0x04;
       
    25 const TUint8 ACK=0x06;
       
    26 const TUint8 NAK=0x15;
       
    27 const TUint8 CAN=0x18;
       
    28 const TUint8 SUB=0x1A;
       
    29 const TUint8 BIGC=0x43;
       
    30 const TUint8 BIGG=0x47;
       
    31 
       
    32 #define READ_BUF_SIZE	32768
       
    33 #define WRITE_BUF_SIZE	32768
       
    34 #define MAX_LINE		32768
       
    35 #define PACKET_SIZE		1024
       
    36 
       
    37 #define MIN(a,b)		((a)<(b)?(a):(b))
       
    38 #define OFFSET(p,off)	((void*)((char*)p+off))
       
    39 
       
    40 #define RESET_COMM()	PurgeComm(Comm, PURGE_RXCLEAR|PURGE_TXCLEAR)
       
    41 
       
    42 
       
    43 FILE* LogFile;
       
    44 HANDLE Comm;
       
    45 
       
    46 char ImgFileName[256];
       
    47 TInt ImgFileSize;
       
    48 char LineBuf[MAX_LINE+1];
       
    49 void* ImgFileChunkBase;
       
    50 
       
    51 #define TT_ASSERT(x)	__ASSERT_ALWAYS(x, __Panic(__LINE__))
       
    52 
       
    53 void __Panic(TInt aLine)
       
    54 	{
       
    55 	fprintf(stderr, "Assertion failed at line %d\n", aLine);
       
    56 	exit(999);
       
    57 	}
       
    58 
       
    59 void WriteLog(const void* aBuf, TInt aLength)
       
    60 	{
       
    61 	fwrite(aBuf, 1, aLength, LogFile);
       
    62 	}
       
    63 
       
    64 void WriteLogS(const char* aString)
       
    65 	{
       
    66 	WriteLog( aString, strlen(aString) );
       
    67 	}
       
    68 
       
    69 /*
       
    70 void TraceLog(const char* aFmt, ...)
       
    71 	{
       
    72 	char buf[512];
       
    73 	va_list list;
       
    74 	va_start(list, aFmt);
       
    75 	sprintf(buf, "TRGTEST: ");
       
    76 // timestamp
       
    77 	vsprintf(buf, aFmt, list);
       
    78 	strcat(buf, "\n");
       
    79 	WriteLogS(buf);
       
    80 	}
       
    81 */
       
    82 
       
    83 /*
       
    84 YModem packet structure:
       
    85 Byte 0 = STX
       
    86 Byte 1 = sequence number (first user data packet is 1)
       
    87 Byte 2 = complement of sequence number
       
    88 Bytes 3-1026 = data (1K per packet)
       
    89 Bytes 1027, 1028 = 16-bit CRC (big-endian)
       
    90 
       
    91 A herald packet is sent first, with sequence number 0
       
    92 The data field consists of the null-terminated file name
       
    93 followed by the null-terminated file size in ASCII decimal
       
    94 digits.
       
    95 */
       
    96 struct SPacket
       
    97 	{
       
    98 	TUint8 iPTI;
       
    99 	TUint8 iSeq;
       
   100 	TUint8 iSeqBar;
       
   101 	TUint8 iData[PACKET_SIZE];
       
   102 	TUint8 iCRC1;
       
   103 	TUint8 iCRC0;
       
   104 	};
       
   105 
       
   106 static const TUint16 crcTab[256] =
       
   107     {
       
   108 	0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,0x8108,0x9129,0xa14a,
       
   109 	0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,
       
   110 	0x72f7,0x62d6,0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,0x2462,
       
   111 	0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,0xa56a,0xb54b,0x8528,0x9509,
       
   112 	0xe5ee,0xf5cf,0xc5ac,0xd58d,0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,
       
   113 	0x46b4,0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,0x48c4,0x58e5,
       
   114 	0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,
       
   115 	0x9969,0xa90a,0xb92b,0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
       
   116 	0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,0x6ca6,0x7c87,0x4ce4,
       
   117 	0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,
       
   118 	0x8d68,0x9d49,0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,0xff9f,
       
   119 	0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,0x9188,0x81a9,0xb1ca,0xa1eb,
       
   120 	0xd10c,0xc12d,0xf14e,0xe16f,0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,
       
   121 	0x6067,0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,0x02b1,0x1290,
       
   122 	0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,
       
   123 	0xe54f,0xd52c,0xc50d,0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
       
   124 	0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,0x26d3,0x36f2,0x0691,
       
   125 	0x16b0,0x6657,0x7676,0x4615,0x5634,0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,
       
   126 	0xb98a,0xa9ab,0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,0xcb7d,
       
   127 	0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,0x4a75,0x5a54,0x6a37,0x7a16,
       
   128 	0x0af1,0x1ad0,0x2ab3,0x3a92,0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,
       
   129 	0x8dc9,0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,0xef1f,0xff3e,
       
   130 	0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,
       
   131 	0x3eb2,0x0ed1,0x1ef0
       
   132     };
       
   133 
       
   134 void UpdateCrc(const void* aPtr, TInt aLength, TUint16& aCrc)
       
   135 //
       
   136 // Perform a CCITT CRC checksum.
       
   137 //
       
   138 	{
       
   139 
       
   140 	register const TUint8* pB = (const TUint8*)aPtr;
       
   141 	register TUint16 crc=aCrc;
       
   142     while (aLength--)
       
   143 		crc=TUint16((crc<<8)^crcTab[(crc>>8)^*pB++]);
       
   144 	aCrc=crc;
       
   145 	}
       
   146 
       
   147 void ClearCommError()
       
   148 	{
       
   149 	DWORD err;
       
   150 	COMSTAT s;
       
   151 	TT_ASSERT(ClearCommError(Comm,&err,&s));
       
   152 	}
       
   153 
       
   154 void SetupCommPort(TInt aPort)
       
   155 	{
       
   156 	char buf[10];
       
   157 	DCB dcb;
       
   158 
       
   159 	sprintf(buf, "COM%d", aPort);
       
   160 	Comm = CreateFileA(buf, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
       
   161 	if (Comm == INVALID_HANDLE_VALUE)
       
   162 		{
       
   163 		fprintf(stderr, "Can't open COM%d : Error %d\n", aPort, GetLastError());
       
   164 		exit(999);
       
   165 		}
       
   166 	dcb.DCBlength = sizeof(dcb);
       
   167 	TT_ASSERT(GetCommState(Comm, &dcb));
       
   168 	TT_ASSERT(SetCommMask(Comm, EV_ERR|EV_RXCHAR));
       
   169 	TT_ASSERT(SetupComm(Comm, READ_BUF_SIZE, WRITE_BUF_SIZE));
       
   170 	ClearCommError();
       
   171 
       
   172 	dcb.fAbortOnError = TRUE;
       
   173 	dcb.BaudRate=115200;
       
   174 	dcb.Parity=NOPARITY;
       
   175 	dcb.fParity = FALSE;
       
   176 	dcb.fErrorChar = FALSE;
       
   177 	dcb.ByteSize=8;
       
   178 	dcb.StopBits=ONESTOPBIT;
       
   179 	dcb.fInX  = FALSE;
       
   180 	dcb.fOutX = FALSE;
       
   181 	dcb.XonChar = 0;
       
   182 	dcb.XoffChar = 0;
       
   183 	dcb.ErrorChar = 0;
       
   184 	dcb.fOutxDsrFlow = FALSE;
       
   185 	dcb.fOutxCtsFlow = FALSE;
       
   186 	dcb.fDsrSensitivity =  FALSE;
       
   187 	dcb.fRtsControl = RTS_CONTROL_DISABLE;
       
   188 	dcb.fDtrControl = DTR_CONTROL_DISABLE;
       
   189 	TT_ASSERT(SetCommState(Comm, &dcb));
       
   190 	EscapeCommFunction(Comm, SETDTR);
       
   191 	EscapeCommFunction(Comm, SETRTS);
       
   192 	}
       
   193 
       
   194 void SetupTimeout(TInt aInt, TInt aTot)
       
   195 	{
       
   196 	COMMTIMEOUTS ct;
       
   197 	GetCommTimeouts(Comm, &ct);
       
   198 	ct.ReadIntervalTimeout = aInt;	//ms
       
   199 	ct.ReadTotalTimeoutMultiplier = 0;
       
   200 	ct.ReadTotalTimeoutConstant = aTot;	// ms
       
   201 	ct.WriteTotalTimeoutConstant = 0;
       
   202 	ct.WriteTotalTimeoutMultiplier = 0;
       
   203 	SetCommTimeouts(Comm, &ct);
       
   204 	}
       
   205 
       
   206 TInt CommRead1(TInt aTimeout)
       
   207 	{
       
   208 	unsigned char c;
       
   209 	DWORD n=0;
       
   210 	BOOL ok;
       
   211 
       
   212 	SetupTimeout(0,aTimeout);
       
   213 	ok = ReadFile(Comm, &c, 1, &n, NULL);
       
   214 	if (!ok)
       
   215 		ClearCommError();
       
   216 	if (ok && n)
       
   217 		return c;
       
   218 	return KErrTimedOut;
       
   219 	}
       
   220 
       
   221 TInt CommRead(void* aBuf, TInt aMax, TInt aTimeout)
       
   222 	{
       
   223 	DWORD n=0;
       
   224 	BOOL ok;
       
   225 
       
   226 	SetupTimeout(100, aTimeout);
       
   227 	ok = ReadFile(Comm, aBuf, aMax, &n, NULL);
       
   228 	if (!ok)
       
   229 		ClearCommError();
       
   230 	if (n==0)
       
   231 		return KErrTimedOut;
       
   232 	return n;
       
   233 	}
       
   234 
       
   235 void CommWrite(const void* aBuf, TInt aLen)
       
   236 	{
       
   237 	DWORD n = 0;
       
   238 	BOOL ok = WriteFile(Comm, aBuf, aLen, &n, NULL);
       
   239 	if (!ok)
       
   240 		ClearCommError();
       
   241 	}
       
   242 
       
   243 void CommWriteC(TUint aChar)
       
   244 	{
       
   245 	unsigned char c = (unsigned char)aChar;
       
   246 	CommWrite(&c, 1);
       
   247 	}
       
   248 
       
   249 void CommWriteS(const char* aString)
       
   250 	{
       
   251 	CommWrite( aString, strlen(aString) );
       
   252 	}
       
   253 
       
   254 TInt PreparePacket(SPacket& pkt, TInt aSeq)
       
   255 	{
       
   256 	TInt r = KErrNone;
       
   257 	TUint16 crc = 0;
       
   258 
       
   259 	pkt.iPTI = STX;
       
   260 	pkt.iSeq = (TUint8)(aSeq>=0 ? aSeq : 0);
       
   261 	pkt.iSeqBar = (TUint8)~pkt.iSeq;
       
   262 	if (aSeq>0)
       
   263 		{
       
   264 		TInt l;
       
   265 		TInt fpos = (aSeq-1)*PACKET_SIZE;	// file position of packet
       
   266 		if ( fpos >= ImgFileSize )
       
   267 			return KErrEof;
       
   268 		l = MIN(PACKET_SIZE, ImgFileSize-fpos);
       
   269 		memcpy(pkt.iData, OFFSET(ImgFileChunkBase,fpos), l);
       
   270 		if (l<PACKET_SIZE)
       
   271 			memset(pkt.iData+l, 0, PACKET_SIZE-l);
       
   272 		}
       
   273 	else
       
   274 		{
       
   275 		memset(pkt.iData, 0, PACKET_SIZE);
       
   276 		if (aSeq==0)
       
   277 			{
       
   278 			const char* p = ImgFileName;
       
   279 			const char* q = p + strlen(p);
       
   280 			while (--q>=p && *q!='\\') {}
       
   281 			sprintf( (char*)pkt.iData, "%s%c%d", q+1, 0, ImgFileSize);
       
   282 			}
       
   283 		}
       
   284 	UpdateCrc(pkt.iData, PACKET_SIZE, crc);
       
   285 	pkt.iCRC1 = (TUint8)(crc>>8);
       
   286 	pkt.iCRC0 = (TUint8)crc;
       
   287 	return r;
       
   288 	}
       
   289 
       
   290 TInt SendPacket(TInt& aSeq, TBool aStream)
       
   291 	{
       
   292 	TInt c;
       
   293 	SPacket pkt;
       
   294 	TInt retries = 10;
       
   295 	TInt tmout = (aSeq>=0) ? 2000 : 500;
       
   296 	TInt r = PreparePacket(pkt, aSeq);
       
   297 
       
   298 //	if ( (aSeq & 7) == 0 )
       
   299 //		printf("SendPacket %d\n", aSeq);
       
   300 	if ( (aSeq & 0x0F) == 0 )
       
   301 		printf(".");
       
   302 
       
   303 	if (r!=KErrNone)
       
   304 		return r;
       
   305 	for(;;)
       
   306 		{
       
   307 		RESET_COMM();
       
   308 		CommWrite(&pkt, sizeof(pkt));
       
   309 		if (aStream)
       
   310 			break;
       
   311 		c = CommRead1(tmout);
       
   312 		if (c==KErrTimedOut && aSeq<0)
       
   313 			return KErrNone;
       
   314 		if (c>=0)
       
   315 			{
       
   316 			if (c==ACK)
       
   317 				break;
       
   318 			}
       
   319 		if (--retries==0)
       
   320 			return KErrTimedOut;
       
   321 		}
       
   322 	if (aSeq==0)
       
   323 		{
       
   324 		c = CommRead1(100);
       
   325 		if (c==KErrTimedOut)
       
   326 			{
       
   327 			++aSeq;
       
   328 			return KErrNone;
       
   329 			}
       
   330 		if (aStream && c!=BIGG)
       
   331 			return KErrTimedOut;
       
   332 		else if (!aStream && c!=BIGC)
       
   333 			return KErrTimedOut;
       
   334 		}
       
   335 	++aSeq;
       
   336 	return r;
       
   337 	}
       
   338 
       
   339 TInt SendImageFile()
       
   340 	{
       
   341 	TInt r = 0;
       
   342 	TInt b1;
       
   343 	TUint8 b2[256];
       
   344 	TBool stream;
       
   345 	TInt seq = 0;
       
   346 
       
   347 	printf("Sending image file: %s\n",ImgFileName);
       
   348 //	TraceLog("Sending image file: %s",ImgFileName);
       
   349 
       
   350 	RESET_COMM();
       
   351 	r = CommRead(b2, 256, 20000);
       
   352 	if (r<0)
       
   353 		return r;
       
   354 //	TraceLog(b2);
       
   355 	if (b2[r-1]!=BIGG && b2[r-1]!=BIGC)
       
   356 		return KErrTimedOut;
       
   357 	stream = (b2[r-1]==BIGG);
       
   358 	seq = 0;
       
   359 	r = KErrNone;
       
   360 	while (r==KErrNone)
       
   361 		{
       
   362 		r = SendPacket(seq, stream);
       
   363 		if (r!=KErrNone && r!=KErrEof)
       
   364 			printf("<SendPacket %d -> %d\n", seq, r);
       
   365 //		TraceLog(_L8("<SendPacket seq=%d r=%d"), seq, r);
       
   366 		}
       
   367 	if (r!=KErrEof)
       
   368 		return r;
       
   369 	r=KErrNone;
       
   370 	RESET_COMM();
       
   371 	CommWriteC(EOT);
       
   372 	b1 = CommRead1(500);
       
   373 	if (b1==KErrTimedOut)
       
   374 		return KErrNone;
       
   375 	if (b1!=ACK)
       
   376 		return KErrNone;
       
   377 	b1 = CommRead1(1000);
       
   378 	if (b1==KErrTimedOut)
       
   379 		return KErrNone;
       
   380 	if (stream && b1!=BIGG)
       
   381 		return KErrNone;
       
   382 	else if (!stream && b1!=BIGC)
       
   383 		return KErrNone;
       
   384 	seq = -1;
       
   385 	r = SendPacket(seq, stream);
       
   386 	return r;
       
   387 	}
       
   388 
       
   389 void GetResponse()
       
   390 	{
       
   391 	TInt r;
       
   392 	do	{
       
   393 		r = CommRead(LineBuf, MAX_LINE, 1000);
       
   394 		if (r>=0)
       
   395 			LineBuf[r]=0, WriteLog(LineBuf, r);
       
   396 		} while (r>=0);
       
   397 	}
       
   398 
       
   399 void IssueCommandAndGetResponse(const char* aCmd)
       
   400 	{
       
   401 	CommWriteS(aCmd);
       
   402 	GetResponse();
       
   403 	}
       
   404 
       
   405 int token_length(const char* s)
       
   406 	{
       
   407 	const char* p = s;
       
   408 	for (; *p && !isspace(*p); ++p) {}
       
   409 	return p - s;
       
   410 	}
       
   411 
       
   412 const char* skip_space(const char* s)
       
   413 	{
       
   414 	for (; *s && isspace(*s); ++s) {}
       
   415 	return s;
       
   416 	}
       
   417 
       
   418 void GetMonitorInfo()
       
   419 	{
       
   420 	BOOL ok = FALSE;
       
   421 	int l;
       
   422 	int type;
       
   423 	RESET_COMM();
       
   424 	IssueCommandAndGetResponse("replacement\xd");
       
   425 	IssueCommandAndGetResponse("f\xd");
       
   426 	const char* p = LineBuf;
       
   427 	const char* q = LineBuf + strlen(LineBuf);
       
   428 	while (p<q)
       
   429 		{
       
   430 		p = skip_space(p);
       
   431 		if (p+16>q || memcmp(p, "Fault Category: ", 16))
       
   432 			{
       
   433 			p += token_length(p);
       
   434 			continue;
       
   435 			}
       
   436 		p = skip_space(p+16);
       
   437 		l = token_length(p);
       
   438 		if (l!=4 || memcmp(p, "KERN", 4))
       
   439 			{
       
   440 			p += l;
       
   441 			continue;
       
   442 			}
       
   443 		p = skip_space(p+4);
       
   444 		if (p+14>q || memcmp(p, "Fault Reason: ", 14))
       
   445 			{
       
   446 			p += token_length(p);
       
   447 			continue;
       
   448 			}
       
   449 		p = skip_space(p+14);
       
   450 		if (p+8<=q && memcmp(p, "00000050", 8)==0)
       
   451 			{
       
   452 			ok = TRUE;
       
   453 			break;
       
   454 			}
       
   455 		p+=8;
       
   456 		continue;
       
   457 		}
       
   458 	if (ok)
       
   459 		return;
       
   460 
       
   461 	IssueCommandAndGetResponse("i\xd");
       
   462 	for (type=0; type<14; ++type)
       
   463 		{
       
   464 		char b[8];
       
   465 		sprintf(b, "c%x\xd", type);
       
   466 		IssueCommandAndGetResponse(b);
       
   467 		}
       
   468 	}
       
   469 
       
   470 TInt ReceiveTestLog()
       
   471 	{
       
   472 	const TInt KMaxNoActivityTimeout = 60 * 60 * 1000;	// 1 hour
       
   473 	const TInt KSerialPollInterval = 10;
       
   474 	TInt bytes = 0;
       
   475 	TInt error = KErrNone;
       
   476 	TInt noActivityTimeMilliSeconds = 0;
       
   477 
       
   478 	printf("\nLogging to file started.\n");
       
   479 	
       
   480 	for (;;)
       
   481 		{
       
   482 		bytes =	CommRead(LineBuf, 100, KSerialPollInterval);
       
   483 		if (bytes > 0)
       
   484 			{
       
   485 			if (bytes >= 23 && memcmp(LineBuf+bytes-23, "!TERMINATE_MSG_LOGGING!", 23)==0)
       
   486 				{
       
   487 				printf("SUCCESS: Terminated from remote.\n");
       
   488 				error = KErrNone;
       
   489 				break;
       
   490 				}
       
   491 			LineBuf[bytes]=0;
       
   492 			WriteLog(LineBuf, bytes);
       
   493 //			printf(LineBuf);
       
   494 			noActivityTimeMilliSeconds=0;
       
   495 			}
       
   496 		else if (bytes == KErrTimedOut)
       
   497 			{
       
   498 			noActivityTimeMilliSeconds += KSerialPollInterval;
       
   499 			if (noActivityTimeMilliSeconds >= KMaxNoActivityTimeout)
       
   500 				{
       
   501 				error = KErrTimedOut;
       
   502 				printf("FAILED: Timed out.\n");
       
   503 				break;
       
   504 				}
       
   505 			}
       
   506 		else
       
   507 			{
       
   508 			error = bytes;
       
   509 			printf("FAILED: Error detected (%d).\n",error);
       
   510 			break;
       
   511 			}
       
   512 		}
       
   513 
       
   514 	CommRead(LineBuf, 100, 100);
       
   515 	return error;
       
   516 	}
       
   517 
       
   518 /*
       
   519 void ReceiveTestLog()
       
   520 	{
       
   521 	TInt r=0;
       
   522 	TInt r2;
       
   523 	TInt tmc=0;
       
   524 
       
   525 	for (;;)
       
   526 		{
       
   527 		r =	CommRead(LineBuf, 100, 100);
       
   528 		if (r>0)
       
   529 			{
       
   530 			LineBuf[r]=0;
       
   531 			WriteLog(LineBuf, r);
       
   532 			printf(LineBuf);
       
   533 			}
       
   534 //		printf("r=%d\n",r);
       
   535 		if (r>=23 && memcmp(LineBuf+r-23, "!TERMINATE_MSG_LOGGING!", 23)==0)
       
   536 			break;
       
   537 		check = TRUE;
       
   538 		}
       
   539 
       
   540 	for(;;)
       
   541 		{
       
   542 		TBool check = FALSE;
       
   543 		r = CommRead(LineBuf, MAX_LINE, 2000);
       
   544 		if (r==KErrTimedOut)
       
   545 			{
       
   546 			++tmc;
       
   547 			if (tmc<500)
       
   548 				continue;
       
   549 			check = TRUE;
       
   550 			}
       
   551 		else if (r>=0)
       
   552 			tmc=0;
       
   553 		if (r>=10 && memcmp(LineBuf+r-10, "Password: ", 10)==0)
       
   554 			check = TRUE;
       
   555 		if (r>0)
       
   556 			WriteLog(LineBuf, r);
       
   557 		if (check || r==KErrTimedOut)
       
   558 			{
       
   559 			// Send CR to see if it elicits monitor prompt
       
   560 			CommWriteC(0x0d);
       
   561 			r2 = CommRead(LineBuf, 12, 1000);
       
   562 			if (r2>0)
       
   563 				WriteLog(LineBuf, r2);
       
   564 			if (r2==12 && memcmp(LineBuf, "\xd\xaPassword: ", 12)==0)
       
   565 				break;
       
   566 			}
       
   567 		if (r<0)
       
   568 			break;
       
   569 		}
       
   570 
       
   571 	TT_ASSERT(r>0 || r==KErrTimedOut);
       
   572 	if (r>0)
       
   573 		{
       
   574 		// yes, extract info
       
   575 		GetMonitorInfo();
       
   576 		CommWriteS("X\xd");		// this should make the target do a cold reboot
       
   577 		}
       
   578 	else
       
   579 		{
       
   580 		// locked up somehow
       
   581 		TraceLog("Target hung");
       
   582 		}
       
   583 	}
       
   584 */
       
   585 
       
   586 int main(int argc, char** argv)
       
   587 	{
       
   588 	TInt r;
       
   589 	TInt port;
       
   590 	const char* logfilename;
       
   591 	FILE* img;
       
   592 	int n;
       
   593 
       
   594 	if (argc != 4)
       
   595 		{
       
   596 		fprintf(stderr, "trgtest <port#> <imagefilename> <logfilename>\n");
       
   597 		exit(999);
       
   598 		}
       
   599 	port = atoi(argv[1]);
       
   600 	strcpy(ImgFileName, argv[2]);
       
   601 	logfilename = argv[3];
       
   602 	LogFile = fopen(logfilename, "wb");
       
   603 	if (!LogFile)
       
   604 		{
       
   605 		fprintf(stderr, "Can't open %s for write\n", logfilename);
       
   606 		exit(999);
       
   607 		}
       
   608 
       
   609 //	TraceLog("Port  = %d\n", port);
       
   610 //	TraceLog("Image = %s\n", ImgFileName);
       
   611 //	TraceLog("Log   = %s\n", logfilename);
       
   612 
       
   613 	img = fopen(ImgFileName, "rb");
       
   614 	if (!img)
       
   615 		{
       
   616 		fprintf(stderr, "Can't open %s for read\n", ImgFileName);
       
   617 		exit(999);
       
   618 		}
       
   619 	fseek(img,0,SEEK_END);
       
   620 	ImgFileSize=ftell(img);
       
   621 	fseek(img,0,SEEK_SET);
       
   622 //	TraceLog("ImgFileSize = %d\n", ImgFileSize);
       
   623 	ImgFileChunkBase = malloc(ImgFileSize);
       
   624 	TT_ASSERT(ImgFileChunkBase != NULL);
       
   625 	n = fread(ImgFileChunkBase, 1, ImgFileSize, img);
       
   626 	TT_ASSERT(n == ImgFileSize);
       
   627 	fclose(img);
       
   628 
       
   629 	SetupCommPort(port);
       
   630 
       
   631 	r = SendImageFile();
       
   632 	free(ImgFileChunkBase);
       
   633 	if (r==KErrNone)
       
   634 		{
       
   635 //		TraceLog("Send image file OK");
       
   636 		ReceiveTestLog();
       
   637 		}
       
   638 //	else
       
   639 //		TraceLog("SendImageFile -> %d",r);
       
   640 
       
   641 	WriteLogS("\n\n");
       
   642 //	TraceLog("DONE");
       
   643 
       
   644 	EscapeCommFunction(Comm, CLRRTS);
       
   645 	EscapeCommFunction(Comm, CLRDTR);
       
   646 	CloseHandle(Comm);
       
   647 	fclose(LogFile);
       
   648 	return 0;
       
   649 	}