os/kernelhwsrv/kerneltest/e32test/device/t_commsk.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1997-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 the License "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 // e32test\device\t_commsk.cpp
    15 // 
    16 //
    17 
    18 #include <e32base.h>
    19 #include <e32base_private.h>
    20 #include <e32test.h>
    21 #include <e32cons.h>
    22 #include <e32svr.h>
    23 #include <e32hal.h>
    24 #include <d32comm.h>
    25 #include <e32uid.h>
    26 #include <hal.h>
    27 
    28 #if defined (__WINS__)
    29 #define PDD_NAME _L("ECDRV.PDD")
    30 #define LDD_NAME _L("ECOMM.LDD")
    31 #else
    32 #define PDD_NAME _L("EUARTn")
    33 #define LDD_NAME _L("ECOMM")
    34 #endif
    35 
    36 //	Our own comms object with synchronous writes
    37 class RComm : public RBusDevComm
    38 	{
    39 public:
    40 	TInt WriteS(const TDesC8& aDes);
    41 	TInt WriteS(const TDesC8& aDes,TInt aLength);
    42 	TInt WriteSE(const TDes8& Entered, TInt aLength);
    43 	};
    44 
    45 LOCAL_D RTest test(_L("T_COMMSK"));
    46 RComm* theSerialPort;
    47 TCommCaps theCaps1Buf;
    48 TCommCapsV01& theCaps1=theCaps1Buf();
    49 TBool MediaChangeTestingEnabled;
    50 
    51 const TInt KWriteSize=250;
    52 const TInt KXonNumReads=0x10;
    53 const TInt KXonReadSize=0x400;	// Must be bigger than comm buffer high water==0x300
    54 
    55 class TResult
    56 	{
    57 public:
    58 	enum TResTest {ETestPower,ESimpleWriting};
    59 	TResult();
    60 	void Display(CConsoleBase *aConsole,TInt aCycles);
    61 	void Add(TResTest aTst,TInt anErr);
    62 	inline TInt SetFreeMem(TInt aVal)
    63 		{return(iFreeMem=aVal);}
    64 private:
    65 	TInt iFreeMem;	
    66 	TInt iTestPowerFails;
    67 	TInt iWriteFails;
    68 	};
    69 
    70 class TSpeedAndName
    71 	{
    72 public:
    73 	TUint iMask; 
    74 	TBps iSpeed;
    75 	const TText* iName;
    76 	};
    77 
    78 const TSpeedAndName KSpeeds[]=
    79 	{
    80 	{KCapsBps50,EBps50,_S("50")}, 
    81 	{KCapsBps2400,EBps2400,_S("2400")},
    82 	{KCapsBps9600,EBps9600,_S("9600")},
    83 	{KCapsBps19200,EBps19200,_S("19200")},
    84 	{KCapsBps57600,EBps57600,_S("57600")},
    85 	{KCapsBps115200,EBps115200,_S("115200")},
    86 	};
    87 
    88 class TFrameAndName
    89 	{
    90 public:
    91 	TDataBits iData;
    92 	TStopBits iStop;
    93 	TParity iParity;
    94 	const TText* iName;
    95 	};
    96 
    97 const TFrameAndName KFrameTypes[]=
    98 	{
    99 	{EData8,EStop1,EParityNone,_S("8,N,1")},
   100 	{EData8,EStop1,EParityEven,_S("8,E,1")},
   101 	{EData8,EStop1,EParityOdd,_S("8,O,1")},
   102 
   103 	{EData8,EStop2,EParityNone,_S("8,N,2")},
   104 	{EData8,EStop2,EParityEven,_S("8,E,2")},
   105 	{EData8,EStop2,EParityOdd,_S("8,O,2")},
   106 
   107 	{EData7,EStop2,EParityNone,_S("7,N,2")},
   108 	{EData7,EStop2,EParityEven,_S("7,E,2")},
   109 	{EData7,EStop2,EParityOdd,_S("7,O,2")},
   110 
   111 	{EData7,EStop1,EParityNone,_S("7,N,1")},
   112 	{EData7,EStop1,EParityEven,_S("7,E,1")},
   113 	{EData7,EStop1,EParityOdd,_S("7,O,1")},
   114 	};
   115 
   116 class THandShakeAndName
   117 	{
   118 public:
   119 	TUint iHandshake;
   120 	const TText* iName;
   121 	};
   122 
   123 THandShakeAndName KHandshakes[]=
   124 	{
   125 	{KConfigObeyDSR,_S("DSR/DTR")},
   126 	{KConfigObeyCTS,_S("CTS/RTS")},
   127 	};
   128 
   129 enum TSerialTestFault {EBadArg,EUnknownSignal};
   130 
   131 TInt RComm::WriteS(const TDesC8& aDes)
   132 
   133 //	Syncronous write
   134 
   135 	{
   136 	return(WriteS(aDes,aDes.Length()));
   137 	}
   138 
   139 TInt RComm::WriteS(const TDesC8& aDes,TInt aLength)
   140 //
   141 //	Syncronous write
   142 //
   143 	{
   144 
   145 	TRequestStatus s;
   146 	Write(s,aDes,aLength);
   147 	User::WaitForRequest(s);
   148 	return(s.Int());
   149 	}
   150 
   151 TResult::TResult()
   152 	{
   153 
   154 	iTestPowerFails=0;
   155 	iFreeMem=0;
   156 	iWriteFails=0;
   157 	}
   158 
   159 void TResult::Display(CConsoleBase *aConsole,TInt aCycles)
   160 //
   161 //	Display test results:
   162 //
   163 	{
   164 
   165 	test.Console()->ClearScreen();
   166 	TInt xStartPos=0;
   167 	TInt yStartPos=2;
   168 
   169 	aConsole->SetPos(xStartPos,yStartPos);
   170 	test.Printf(_L("Total cycles         : %d"),aCycles);
   171 
   172 	//	Display results of Power Down/Up followed by a simple write/read to the Serial Port
   173 	aConsole->SetPos(xStartPos,yStartPos+2);
   174 	if (MediaChangeTestingEnabled)
   175 		test.Printf(_L("Media Change failures: %d\n"),iTestPowerFails);
   176 	else
   177 		test.Printf(_L("Power test failures  : %d\n"),iTestPowerFails);
   178 	aConsole->SetPos(xStartPos,yStartPos+3);
   179 	test.Printf(_L("Serial test failures : %d\n"),iWriteFails);
   180 
   181 	aConsole->SetPos(xStartPos,yStartPos+5);
   182 	test.Printf(_L("Free mem (in bytes)  : %d"),iFreeMem);
   183 	}
   184 
   185 void TResult::Add(TResTest aTst,TInt anErr)
   186 //	
   187 //	Increment the corresponding variable if test fails
   188 //	
   189 	{
   190 
   191 	if (anErr!=KErrNone)
   192 		{
   193 		switch (aTst)
   194 			{
   195 			case ETestPower:
   196 				if (anErr!=KErrNone)
   197 					++iTestPowerFails;
   198 				break;
   199 			case ESimpleWriting:
   200 				if (anErr!=KErrNone)
   201 					++iWriteFails;
   202 				break;
   203 			default:
   204 				iTestPowerFails=0;
   205 				break;
   206 			}
   207 		}
   208 	}
   209 
   210 #ifdef _DEBUG_DEVCOMM
   211 LOCAL_C void CommDebug(RBusDevComm& aComm)
   212 	{
   213 	TCommDebugInfoPckg infopckg;
   214 	TCommDebugInfo& info = infopckg();
   215 	aComm.DebugInfo(infopckg);
   216 
   217 	test.Printf(_L("  LDD State        :    TX         RX    \r\n"));
   218 	test.Printf(_L("  Busy             : %10d %10d\r\n"), info.iTxBusy, info.iRxBusy);
   219 	test.Printf(_L("  Held             : %10d %10d\r\n"), info.iTxHeld, info.iRxHeld);
   220 	test.Printf(_L("  Length           : %10d %10d\r\n"), info.iTxLength, info.iRxLength);
   221 	test.Printf(_L("  Offset           : %10d %10d\r\n"), info.iTxOffset, info.iRxOffset);
   222 	test.Printf(_L("  Int Count        : %10d %10d\r\n"), info.iTxIntCount, info.iRxIntCount);
   223 	test.Printf(_L("  Err Count        : %10d %10d\r\n"), info.iTxErrCount, info.iRxErrCount);
   224 	test.Printf(_L("  Buf Count        : %10d %10d\r\n"), info.iTxBufCount, info.iRxBufCount);
   225 	test.Printf(_L("  Fill/Drain       : %10d %10d\r\n"), info.iFillingTxBuf, info.iFillingTxBuf);
   226 	test.Printf(_L("  XON              : %10d %10d\r\n"), info.iTxXon, info.iRxXon);
   227 	test.Printf(_L("  XOFF             : %10d %10d\r\n"), info.iTxXoff, info.iRxXoff);
   228 	test.Printf(_L("  Chars            : %10d %10d\r\n"), info.iTxChars, info.iRxChars);
   229 	}
   230 #else
   231 void CommDebug(RBusDevComm& /*aComm*/)
   232 	{
   233 	test.Printf(_L("Debug Dump not available\r\n"));
   234 	}
   235 #endif
   236 
   237 LOCAL_C void Panic(TSerialTestFault const& aFault)
   238 //
   239 // Panic the test code
   240 //
   241 	{
   242 	User::Panic(_L("Comm Test"),aFault);
   243 	}
   244 
   245 LOCAL_C void StripeMem(TDes8& aBuf,TUint aStartChar,TUint anEndChar)
   246 //
   247 // Mark a buffer with repeating byte pattern
   248 //
   249 	{
   250 
   251 	__ASSERT_ALWAYS(aStartChar<=anEndChar,Panic(EBadArg));
   252 	if (aStartChar==anEndChar)
   253 		{
   254 		aBuf.Fill(aStartChar);
   255 		return;
   256 		}
   257 
   258 	TUint character=aStartChar;
   259 	for (TInt i=0;i<aBuf.Length();i++)
   260 		{
   261 		aBuf[i]=(TText8)character;
   262 		if(++character>anEndChar)
   263 			character=aStartChar;
   264 		}
   265 	}
   266 
   267 LOCAL_C TInt TestSimpleWriting()
   268 	{	
   269 	
   270 	const TInt KBufSize=100;
   271 	TUint8* inBuf=new TUint8[KBufSize]; 
   272 	TUint8* outBuf=new TUint8[KBufSize];
   273 	TPtr8 outDes(outBuf,KBufSize,KBufSize);
   274 	TPtr8 inDes(inBuf,KBufSize,KBufSize);
   275 	StripeMem(outDes,'A','Z');
   276 	inDes.FillZ();
   277 	inDes.SetLength(1);
   278 	
   279 	TRequestStatus readStatus;
   280 	TRequestStatus timeStatus;
   281 			
   282 	theSerialPort->WriteS(outDes,KBufSize);
   283 	theSerialPort->Read(readStatus,inDes);
   284 	
   285 	RTimer tim;
   286 	test(tim.CreateLocal()==KErrNone);
   287 	tim.After(timeStatus,2000000);  // Async. timer request - 2Secs
   288 
   289 			
   290 	User::WaitForRequest(timeStatus,readStatus);
   291 	
   292 	if (readStatus!=KRequestPending)
   293         {
   294 		//	Serial request is complete - cancel timer
   295 		tim.Cancel();
   296 		User::WaitForRequest(timeStatus);
   297 		if (readStatus==KErrNone)
   298 			{
   299 			test(inDes.Length()==inDes.MaxLength());
   300 			test(inDes.Length()==KBufSize);
   301 			outDes.SetLength(inDes.Length());
   302 			return((inDes.Compare(outDes)==0)?KErrNone:KErrGeneral);
   303 			}
   304 		else 
   305 			return KErrGeneral;	
   306 		}
   307 	
   308 	else if (timeStatus!=KRequestPending)
   309        	{
   310 		//	Timed out before Serial test completed
   311 		theSerialPort->ReadCancel(); // Cancel serial read
   312 		User::WaitForRequest(readStatus);
   313 		return KErrTimedOut;
   314 		}
   315 	else
   316 		Panic(EUnknownSignal);
   317 	return(KErrNone); // Never gets here
   318 	}
   319 
   320 LOCAL_C TInt SetUp()
   321 //
   322 //	Set up the serial port
   323 //
   324 	{
   325 	
   326 	theSerialPort->QueryReceiveBuffer();
   327 
   328 	TCommConfig cBuf;
   329 	TCommConfigV01& c=cBuf();
   330 	theSerialPort->Config(cBuf);
   331 	
   332 	c.iFifo=EFifoEnable;
   333 	c.iDataBits=EData8;
   334 	c.iStopBits=EStop1;
   335 	c.iParity=EParityNone;
   336 	c.iRate=EBps115200;
   337 	c.iHandshake=0;
   338 
   339 	TInt r=theSerialPort->SetConfig(cBuf);
   340 	return (r);
   341 	}
   342 
   343 LOCAL_C TInt TestPower()	
   344 	{	
   345 	
   346 	User::After(1000000);	// Allow 1 second before power down
   347 	RTimer timer;
   348 	TRequestStatus done;
   349 	timer.CreateLocal();
   350 	TTime wakeup;
   351 	wakeup.HomeTime();
   352 	wakeup+=TTimeIntervalSeconds(10);
   353 	timer.At(done,wakeup);
   354 	UserHal::SwitchOff();
   355 	User::WaitForRequest(done);
   356 	return (done.Int());
   357 	}
   358 	
   359 GLDEF_C TInt E32Main()
   360     {
   361 
   362 //	test.SetLogged(EFalse); 	// Turn off serial port debugging!
   363 
   364 	test.Title();
   365 	test.Start(_L("Comms Soak Test"));
   366 
   367 	TBuf <0x100> cmd;
   368 	User::CommandLine(cmd);
   369 	TInt port=0;
   370 	if ((cmd.Length()>0) && (cmd[0]>='1' && cmd[0]<='4'))
   371 		port=(TInt)(cmd[0]-'0');
   372 
   373 	test.Next(_L("Load Ldd/Pdd"));
   374 	TInt r;
   375     TBuf<10> pddName=PDD_NAME;
   376 #if !defined (__WINS__)
   377 	pddName[5]=(TText)('1'+port);
   378 	TInt muid=0;
   379 	if (HAL::Get(HAL::EMachineUid, muid)==KErrNone)
   380 		{
   381 		// Brutus uses EUART4 for both COM3 and COM4
   382 		if (muid==HAL::EMachineUid_Brutus && port==4)
   383 			pddName[5]=(TText)'4';
   384 		}
   385 #endif
   386 	r=User::LoadPhysicalDevice(pddName);
   387 	test.Printf(_L("Load %S returned %d\n\r"),&pddName,r);
   388 	test(r==KErrNone || r==KErrAlreadyExists);
   389 
   390 	r=User::LoadLogicalDevice(LDD_NAME);
   391 	test.Printf(_L("Load LDD returned %d\n\r"),r);
   392 	test(r==KErrNone || r==KErrAlreadyExists);
   393 
   394 	theSerialPort=new RComm;
   395 	test(theSerialPort!=NULL);
   396 
   397 	//	Set up the serial port
   398 	theSerialPort->Open(port);
   399 	r=SetUp();
   400 	test(r==KErrNone);
   401 	
   402 	MediaChangeTestingEnabled=EFalse;
   403 	test.Printf(_L("\r\nThis test requires a loopback conector on the Serial Port\r\n"));
   404 	test.Printf(_L("<<Hit M for media change testing>>\r\n"));
   405 	test.Printf(_L("<<Any other key for power testing>>\r\n"));
   406 	TChar c=(TUint)test.Getch();
   407 	c.UpperCase();
   408 	if (c=='M')
   409 		MediaChangeTestingEnabled=ETrue;
   410 
   411 	//	Continuous Test
   412 	TInt cycles=0;
   413 	TResult results;
   414 	
   415 	TRequestStatus stat;
   416 	test.Console()->Read(stat);
   417 	
   418 	while (stat==KRequestPending)
   419 		{
   420 		// Calculate the amount of free memory
   421 		TMemoryInfoV1Buf membuf;
   422     	UserHal::MemoryInfo(membuf);
   423     	TMemoryInfoV1 &memoryInfo=membuf();
   424 		results.SetFreeMem(memoryInfo.iFreeRamInBytes);
   425 	
   426 		if (MediaChangeTestingEnabled)
   427 			{
   428 			// Media Change Test (if enabled) then SetUp Serial Port and test we can still read/write
   429 //			UserSvr::ForceRemountMedia(ERemovableMedia0); // Generate media change
   430 //			User::After(1000000);	// Allow 1 second after power down
   431 			}
   432 		else
   433 			{
   434 			// Power Down then test we can still read/write
   435 			r=TestPower();			
   436 			results.Add(TResult::ETestPower,r);
   437 			}
   438 		r=TestSimpleWriting();
   439 		results.Add(TResult::ESimpleWriting,r);
   440 
   441 		//	Display the results of the test cycle
   442 		cycles++;
   443 		results.Display(test.Console(),cycles);
   444 		}  
   445 
   446 	User::WaitForRequest(stat);
   447 	theSerialPort->Close();
   448 	test.End();
   449 
   450 	return(KErrNone);
   451 	}