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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\device\t_commsk.cpp
19 #include <e32base_private.h>
28 #if defined (__WINS__)
29 #define PDD_NAME _L("ECDRV.PDD")
30 #define LDD_NAME _L("ECOMM.LDD")
32 #define PDD_NAME _L("EUARTn")
33 #define LDD_NAME _L("ECOMM")
36 // Our own comms object with synchronous writes
37 class RComm : public RBusDevComm
40 TInt WriteS(const TDesC8& aDes);
41 TInt WriteS(const TDesC8& aDes,TInt aLength);
42 TInt WriteSE(const TDes8& Entered, TInt aLength);
45 LOCAL_D RTest test(_L("T_COMMSK"));
47 TCommCaps theCaps1Buf;
48 TCommCapsV01& theCaps1=theCaps1Buf();
49 TBool MediaChangeTestingEnabled;
51 const TInt KWriteSize=250;
52 const TInt KXonNumReads=0x10;
53 const TInt KXonReadSize=0x400; // Must be bigger than comm buffer high water==0x300
58 enum TResTest {ETestPower,ESimpleWriting};
60 void Display(CConsoleBase *aConsole,TInt aCycles);
61 void Add(TResTest aTst,TInt anErr);
62 inline TInt SetFreeMem(TInt aVal)
63 {return(iFreeMem=aVal);}
78 const TSpeedAndName KSpeeds[]=
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")},
97 const TFrameAndName KFrameTypes[]=
99 {EData8,EStop1,EParityNone,_S("8,N,1")},
100 {EData8,EStop1,EParityEven,_S("8,E,1")},
101 {EData8,EStop1,EParityOdd,_S("8,O,1")},
103 {EData8,EStop2,EParityNone,_S("8,N,2")},
104 {EData8,EStop2,EParityEven,_S("8,E,2")},
105 {EData8,EStop2,EParityOdd,_S("8,O,2")},
107 {EData7,EStop2,EParityNone,_S("7,N,2")},
108 {EData7,EStop2,EParityEven,_S("7,E,2")},
109 {EData7,EStop2,EParityOdd,_S("7,O,2")},
111 {EData7,EStop1,EParityNone,_S("7,N,1")},
112 {EData7,EStop1,EParityEven,_S("7,E,1")},
113 {EData7,EStop1,EParityOdd,_S("7,O,1")},
116 class THandShakeAndName
123 THandShakeAndName KHandshakes[]=
125 {KConfigObeyDSR,_S("DSR/DTR")},
126 {KConfigObeyCTS,_S("CTS/RTS")},
129 enum TSerialTestFault {EBadArg,EUnknownSignal};
131 TInt RComm::WriteS(const TDesC8& aDes)
136 return(WriteS(aDes,aDes.Length()));
139 TInt RComm::WriteS(const TDesC8& aDes,TInt aLength)
146 Write(s,aDes,aLength);
147 User::WaitForRequest(s);
159 void TResult::Display(CConsoleBase *aConsole,TInt aCycles)
161 // Display test results:
165 test.Console()->ClearScreen();
169 aConsole->SetPos(xStartPos,yStartPos);
170 test.Printf(_L("Total cycles : %d"),aCycles);
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);
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);
181 aConsole->SetPos(xStartPos,yStartPos+5);
182 test.Printf(_L("Free mem (in bytes) : %d"),iFreeMem);
185 void TResult::Add(TResTest aTst,TInt anErr)
187 // Increment the corresponding variable if test fails
210 #ifdef _DEBUG_DEVCOMM
211 LOCAL_C void CommDebug(RBusDevComm& aComm)
213 TCommDebugInfoPckg infopckg;
214 TCommDebugInfo& info = infopckg();
215 aComm.DebugInfo(infopckg);
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);
231 void CommDebug(RBusDevComm& /*aComm*/)
233 test.Printf(_L("Debug Dump not available\r\n"));
237 LOCAL_C void Panic(TSerialTestFault const& aFault)
239 // Panic the test code
242 User::Panic(_L("Comm Test"),aFault);
245 LOCAL_C void StripeMem(TDes8& aBuf,TUint aStartChar,TUint anEndChar)
247 // Mark a buffer with repeating byte pattern
251 __ASSERT_ALWAYS(aStartChar<=anEndChar,Panic(EBadArg));
252 if (aStartChar==anEndChar)
254 aBuf.Fill(aStartChar);
258 TUint character=aStartChar;
259 for (TInt i=0;i<aBuf.Length();i++)
261 aBuf[i]=(TText8)character;
262 if(++character>anEndChar)
263 character=aStartChar;
267 LOCAL_C TInt TestSimpleWriting()
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');
279 TRequestStatus readStatus;
280 TRequestStatus timeStatus;
282 theSerialPort->WriteS(outDes,KBufSize);
283 theSerialPort->Read(readStatus,inDes);
286 test(tim.CreateLocal()==KErrNone);
287 tim.After(timeStatus,2000000); // Async. timer request - 2Secs
290 User::WaitForRequest(timeStatus,readStatus);
292 if (readStatus!=KRequestPending)
294 // Serial request is complete - cancel timer
296 User::WaitForRequest(timeStatus);
297 if (readStatus==KErrNone)
299 test(inDes.Length()==inDes.MaxLength());
300 test(inDes.Length()==KBufSize);
301 outDes.SetLength(inDes.Length());
302 return((inDes.Compare(outDes)==0)?KErrNone:KErrGeneral);
308 else if (timeStatus!=KRequestPending)
310 // Timed out before Serial test completed
311 theSerialPort->ReadCancel(); // Cancel serial read
312 User::WaitForRequest(readStatus);
316 Panic(EUnknownSignal);
317 return(KErrNone); // Never gets here
322 // Set up the serial port
326 theSerialPort->QueryReceiveBuffer();
329 TCommConfigV01& c=cBuf();
330 theSerialPort->Config(cBuf);
335 c.iParity=EParityNone;
339 TInt r=theSerialPort->SetConfig(cBuf);
343 LOCAL_C TInt TestPower()
346 User::After(1000000); // Allow 1 second before power down
352 wakeup+=TTimeIntervalSeconds(10);
353 timer.At(done,wakeup);
354 UserHal::SwitchOff();
355 User::WaitForRequest(done);
359 GLDEF_C TInt E32Main()
362 // test.SetLogged(EFalse); // Turn off serial port debugging!
365 test.Start(_L("Comms Soak Test"));
368 User::CommandLine(cmd);
370 if ((cmd.Length()>0) && (cmd[0]>='1' && cmd[0]<='4'))
371 port=(TInt)(cmd[0]-'0');
373 test.Next(_L("Load Ldd/Pdd"));
375 TBuf<10> pddName=PDD_NAME;
376 #if !defined (__WINS__)
377 pddName[5]=(TText)('1'+port);
379 if (HAL::Get(HAL::EMachineUid, muid)==KErrNone)
381 // Brutus uses EUART4 for both COM3 and COM4
382 if (muid==HAL::EMachineUid_Brutus && port==4)
383 pddName[5]=(TText)'4';
386 r=User::LoadPhysicalDevice(pddName);
387 test.Printf(_L("Load %S returned %d\n\r"),&pddName,r);
388 test(r==KErrNone || r==KErrAlreadyExists);
390 r=User::LoadLogicalDevice(LDD_NAME);
391 test.Printf(_L("Load LDD returned %d\n\r"),r);
392 test(r==KErrNone || r==KErrAlreadyExists);
394 theSerialPort=new RComm;
395 test(theSerialPort!=NULL);
397 // Set up the serial port
398 theSerialPort->Open(port);
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();
409 MediaChangeTestingEnabled=ETrue;
416 test.Console()->Read(stat);
418 while (stat==KRequestPending)
420 // Calculate the amount of free memory
421 TMemoryInfoV1Buf membuf;
422 UserHal::MemoryInfo(membuf);
423 TMemoryInfoV1 &memoryInfo=membuf();
424 results.SetFreeMem(memoryInfo.iFreeRamInBytes);
426 if (MediaChangeTestingEnabled)
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
434 // Power Down then test we can still read/write
436 results.Add(TResult::ETestPower,r);
438 r=TestSimpleWriting();
439 results.Add(TResult::ESimpleWriting,r);
441 // Display the results of the test cycle
443 results.Display(test.Console(),cycles);
446 User::WaitForRequest(stat);
447 theSerialPort->Close();