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_modem1.cpp
15 // Test program for PC Card Serial Port Driver - Requires Dacom GoldCard Modem.
20 #include <e32std_private.h>
29 class RComm : public RBusDevComm
32 TInt WriteS(const TDesC8& aDes);
35 const TInt KBlockSize=256;
36 // Block size Line rate
37 const TInt KBlocksShort=4; // 4*256=1K - <1200
38 const TInt KBlocksMedium=12; // 12*256=3K - <4800
39 const TInt KBlocksLong=64; // 64*256=16K - <28800
40 const TInt KBlocksVeryLong=128; // 128*256=32K - 28800
42 enum TLineRate {EV21_300,EBell103_300,EV22_1200,EBell212_1200,EV22bis_2400,
43 EV32_4800,EV32_9600,EV32bis_14400,EV34_28800,ELineRateEnd};
44 const TInt KMaxLineRates=ELineRateEnd;
45 const TInt KStandardRxBufferSize=0x400;
47 #if !defined (__WINS__)
48 #define PDD_NAME _L("EUARTn")
49 #define LDD_NAME _L("ECOMM")
51 #define PDD_NAME _L("ECDRV.PDD")
52 #define LDD_NAME _L("ECOMM.LDD")
55 LOCAL_D RTest test(_L("T_MODEM1"));
56 LOCAL_D TInt LineModeData[KMaxLineRates]={0,64,1,69,2,9,9,10,11};
57 LOCAL_D TInt LineRateData[KMaxLineRates]={300,300,1200,1200,2400,4800,9600,14400,28800};
58 LOCAL_D TInt LineConnectData[KMaxLineRates]={1200,1200,1200,1200,2400,4800,9600,14400,28800};
59 LOCAL_D RComm *theSerialPort;
62 TInt RComm::WriteS(const TDesC8& aDes)
69 Write(s,aDes,aDes.Length());
70 User::WaitForRequest(s);
74 LOCAL_C TPtrC BaudRateInText(TBps aRate)
76 // Convert into Baudrate text
82 case EBps50: return(_L("50")); break;
83 case EBps75: return(_L("75")); break;
84 case EBps110: return(_L("110")); break;
85 case EBps134: return(_L("134")); break;
86 case EBps150: return(_L("150")); break;
87 case EBps300: return(_L("300")); break;
88 case EBps600: return(_L("600")); break;
89 case EBps1200: return(_L("1200")); break;
90 case EBps1800: return(_L("1800")); break;
91 case EBps2000: return(_L("2000")); break;
92 case EBps2400: return(_L("2400")); break;
93 case EBps3600: return(_L("3600")); break;
94 case EBps4800: return(_L("4800")); break;
95 case EBps7200: return(_L("7200")); break;
96 case EBps9600: return(_L("9600")); break;
97 case EBps19200: return(_L("19200")); break;
98 case EBps38400: return(_L("38400")); break;
99 case EBps57600: return(_L("57600")); break;
100 case EBps115200: return(_L("115000")); break;
101 default: return(_L("Unknown")); break;
105 LOCAL_C TInt TranslateCrLf(TDes8 &aDes)
107 // Search for CR/LF characters in a string and replace them with
108 // '\r' '\n' format. Also replaces unprintable characters with "?"
112 TText8 buf[KBlockSize];
113 TText8 *pS=(TText8*)aDes.Ptr();
114 TText8 *pSE=pS+aDes.Size();
116 TText8 *pTMax=pT+(KBlockSize-1);
117 for (;pS<pSE;pS++,pT++)
121 if (*pS=='\xD'||*pS=='\xA')
124 *pT=(*pS=='\xD')?'r':'n';
126 else if (((TChar)*pS).IsPrint())
132 if ((pT-&buf[0])>aDes.MaxLength())
138 LOCAL_C void PrintBuf(TDes8 &aBuf)
140 // Print the contents of a buffer
144 TInt len=aBuf.Length();
145 for (TInt i=0;i<=len/8;i++)
147 test.Printf(_L("%4d: "),i*8);
149 for (TInt j=0;j<8;j++)
153 TInt v=aBuf[(i*8)+j];
154 test.Printf(_L("%02x "),v);
156 test.Printf(_L("\n\r"));
160 LOCAL_C void testLoopBack(TLineRate aLineRate,TBps aBaudRate)
162 // Perform an analogue loopback test at the specified linerate
168 TPtrC bd=BaudRateInText(aBaudRate);
169 b.Format(_L("Loopback test(%S)"),&bd);
172 TBuf8<KBlockSize> txBuf;
173 theSerialPort->ResetBuffers();
174 txBuf.Format(_L8("AT&F+MS=%d,0,%d,%d\\N0&K3&D2M0\r"),LineModeData[aLineRate],LineRateData[aLineRate],LineRateData[aLineRate]);
175 test(theSerialPort->WriteS(txBuf)==KErrNone);
177 TBuf8<KBlockSize> rxBuf;
178 User::After(2000000); // 2Secs
179 err=theSerialPort->QueryReceiveBuffer();
181 rxBuf.SetLength(err);
182 TRequestStatus rxStat;
183 theSerialPort->ReadOneOrMore(rxStat,rxBuf);
184 User::WaitForRequest(rxStat);
185 // test.Printf(_L(" Rx(%d):"),rxStat); // ???
186 test(rxStat==KErrNone);
187 txBuf.Append(_L("\r\nOK\r\n"));
188 err=rxBuf.Compare(txBuf);
189 // test(TranslateCrLf(rxBuf)==KErrNone); // ???
190 // test.Printf(_L(" %S\r\n"),&rxBuf); // ???
193 test.Next(_L("Get loopback"));
194 txBuf.Format(_L8("AT&T1\r"));
195 test(theSerialPort->WriteS(txBuf)==KErrNone);
196 User::After(5000000); // 5Secs
197 err=theSerialPort->QueryReceiveBuffer();
199 rxBuf.SetLength(err);
200 theSerialPort->ReadOneOrMore(rxStat,rxBuf);
201 User::WaitForRequest(rxStat);
202 test.Printf(_L(" Rx(%d):"),rxStat);
203 test(rxStat==KErrNone);
204 txBuf.AppendFormat(_L8("\r\nCONNECT %d\r\n"),LineConnectData[aLineRate]);
205 err=rxBuf.Compare(txBuf);
206 test(TranslateCrLf(rxBuf)==KErrNone);
207 test.Printf(_L(" %S\r\n"),&rxBuf); // Print what we got back (without CR/LF etc).
208 // Sometimes get extra character as modem goes on-line so just look for command echo + connect
210 User::After(2000000); // 2Secs
212 TInt totalBlocksToTransfer;
213 if (aBaudRate<EBps1200||aLineRate<EV22_1200)
214 totalBlocksToTransfer=KBlocksShort;
215 else if (aBaudRate<EBps4800||aLineRate<EV32_4800)
216 totalBlocksToTransfer=KBlocksMedium;
217 else if (aLineRate<EV34_28800)
218 totalBlocksToTransfer=KBlocksLong;
220 totalBlocksToTransfer=KBlocksVeryLong;
221 b.Format(_L("Transfering data(%dK)"),(totalBlocksToTransfer*KBlockSize)/1024);
223 TInt loopBackFail=KErrGeneral;
224 TRequestStatus txStat;
225 txBuf.SetLength(KBlockSize);
227 for (i=0;i<KBlockSize;i++)
229 theSerialPort->Write(txStat,txBuf,KBlockSize);
230 TInt txBlks=(totalBlocksToTransfer-1);
231 rxBuf.Fill(0,KBlockSize);
232 theSerialPort->Read(rxStat,rxBuf,KBlockSize);
234 TRequestStatus tStat;
236 test(tim.CreateLocal()==KErrNone);
237 tim.After(tStat,40000000); // 40Secs
238 test.Printf(_L(">"));
241 User::WaitForAnyRequest();
242 if (tStat!=KRequestPending)
244 // test.Printf(_L("t")); // Timed out
245 theSerialPort->ReadCancel(); // Cancel serial read
246 User::WaitForRequest(rxStat);
249 theSerialPort->WriteCancel(); // Cancel serial write
250 User::WaitForRequest(txStat);
252 loopBackFail=KErrTimedOut; // Test failed
255 else if (rxStat!=KRequestPending)
257 // test.Printf(_L("r")); // Serial rx request complete
260 loopBackFail=rxStat.Int(); // Test failed
263 for (i=0;i<KBlockSize;i++)
267 loopBackFail=KErrCorrupt; // Test failed
268 rxBuf[KBlockSize-1]=0;
269 // PrintBuf(rxBuf); // ???
270 // goto endSerial; // !!!Ignore compare fails for now!!!
273 test.Printf(_L("<"));
274 if (++rxBlks<totalBlocksToTransfer)
276 rxBuf.Fill(0,KBlockSize);
277 theSerialPort->Read(rxStat,rxBuf,KBlockSize);
281 loopBackFail=KErrNone;
283 tim.Cancel(); // Cancel timer request.
284 User::WaitForRequest(tStat);
287 theSerialPort->WriteCancel(); // Cancel serial write
288 User::WaitForRequest(txStat);
293 else if (txStat!=KRequestPending)
295 // test.Printf(_L("s")); // Serial tx request complete
298 theSerialPort->Write(txStat,txBuf,KBlockSize);
299 test.Printf(_L(">"));
305 // test.Printf(_L("?")); // Stray signal - cancel everything
306 theSerialPort->ReadCancel(); // Cancel serial read
307 User::WaitForRequest(rxStat);
308 tim.Cancel(); // Cancel timer request.
309 User::WaitForRequest(tStat);
312 theSerialPort->WriteCancel(); // Cancel serial write
313 User::WaitForRequest(txStat);
315 loopBackFail=KErrDied;
319 test.Printf(_L(" (%d)\r\n"),loopBackFail);
320 // !!! At this point RTS may or may not be asserted following the write cancel. The
321 // following seems necessary to make sure RTS is asserted so any remaining Rx data
322 // can be received.and thrown away
323 User::After(2000000);
324 theSerialPort->ResetBuffers();
325 User::After(1000000); // Wait 1Secs for any remaining Rx data
328 test.Next(_L("Disconnect"));
329 theSerialPort->ResetBuffers(); // Through away any remaining Rx data.
330 txBuf.Format(_L8("+++"));
331 test(theSerialPort->WriteS(txBuf)==KErrNone);
332 User::After(2000000); // 2Secs
333 err=theSerialPort->QueryReceiveBuffer();
335 rxBuf.SetLength(err);
336 theSerialPort->ReadOneOrMore(rxStat,rxBuf);
337 User::WaitForRequest(rxStat);
338 test(rxStat==KErrNone);
339 txBuf.Append(_L("\r\nOK\r\n"));
340 err=rxBuf.Compare(txBuf);
341 // test(TranslateCrLf(rxBuf)==KErrNone); // ???
342 // test.Printf(_L(" %S\r\n"),&rxBuf); // ???
345 txBuf.Format(_L8("ATH0\r"));
346 test(theSerialPort->WriteS(txBuf)==KErrNone);
347 User::After(4000000); // 4Secs
348 err=theSerialPort->QueryReceiveBuffer();
350 rxBuf.SetLength(err);
351 theSerialPort->ReadOneOrMore(rxStat,rxBuf);
352 User::WaitForRequest(rxStat);
353 test(rxStat==KErrNone);
354 txBuf.Append(_L("\r\nOK\r\n"));
355 err=rxBuf.Compare(txBuf);
356 // test(TranslateCrLf(rxBuf)==KErrNone); // ???
357 // test.Printf(_L(" %S\r\n"),&rxBuf); // ???
360 test.Next(_L("Check result"));
361 test(loopBackFail==KErrNone || loopBackFail==KErrCorrupt); // !!!Ignore compare fails for now!!!
362 // test(loopBackFail==KErrNone);
367 LOCAL_C void testAllLineRates(TBps aRate)
369 // Perform loopback test at the specified baudrate in as many line modes that
370 // are supported at this baudrate
374 test.Start(_L("Setting baudrate"));
376 TCommConfigV01 &c=cBuf();
377 theSerialPort->Config(cBuf);
381 c.iParity=EParityNone;
382 c.iHandshake=KConfigObeyCTS;
384 test(theSerialPort->SetConfig(cBuf)==KErrNone);
386 if (aRate>=EBps38400)
388 test.Next(_L("Testing at V.34-28800"));
389 testLoopBack(EV34_28800,aRate);
392 if (aRate>=EBps19200)
394 test.Next(_L("Testing at V.32bis-14400"));
395 testLoopBack(EV32bis_14400,aRate);
400 test.Next(_L("Testing at V.32-9600"));
401 testLoopBack(EV32_9600,aRate);
404 // if (aRate>=EBps4800)
406 // test.Next(_L("Testing at V.32-4800"));
407 // testLoopBack(EV32_4800,aRate);
412 test.Next(_L("Testing at V.22bis-2400"));
413 testLoopBack(EV22bis_2400,aRate);
416 // if (aRate>=EBps1200)
418 // test.Next(_L("Testing at Bell212-1200"));
419 // testLoopBack(EBell212_1200,aRate);
422 // if (aRate>=EBps1200)
424 // test.Next(_L("Testing at V.22-1200"));
425 // testLoopBack(EV22_1200,aRate);
428 // test.Next(_L("Testing at Bell103-300"));
429 // testLoopBack(EBell103_300,aRate);
431 test.Next(_L("Testing at V.21-300"));
432 testLoopBack(EV21_300,aRate);
437 GLDEF_C TInt E32Main()
439 // test.SetLogged(EFalse); // Turn off serial port debugging!
444 test.Start(_L("PC Card Modem Test Program"));
448 proc.CommandLine(cmd);
450 // First parameter (if present) sets the serial port number
452 if ((cmd.Length()>0) && (cmd[0]>='1' && cmd[0]<='4'))
453 port=(TInt)(cmd[0]-'0');
455 // 2nd parameter (if present) sets the start speed
456 // (4=115K,3=57600,2=38400,1=19200,0=9600)
458 if ((cmd.Length()>3) && (cmd[2]>='0' && cmd[2]<='4'))
459 startSpeed=(TInt)(cmd[2]-'0');
461 test.Next(_L("Load Device Drivers"));
462 TBuf<10> pddName=PDD_NAME;
463 #if !defined (__WINS__)
464 pddName[5]=(TText)('1'+port);
466 if (HAL::Get(HAL::EMachineUid, muid)==KErrNone)
468 // Brutus uses EUART4 for both COM3 and COM4
469 if (muid==HAL::EMachineUid_Brutus && port==4)
470 pddName[5]=(TText)'4';
473 r=User::LoadPhysicalDevice(pddName);
474 test(r==KErrNone||r==KErrAlreadyExists);
475 r=User::LoadLogicalDevice(LDD_NAME);
476 test(r==KErrNone||r==KErrAlreadyExists);
478 test.Next(_L("Open serial port"));
479 theSerialPort=new RComm;
480 test(theSerialPort!=NULL);
481 r=theSerialPort->Open(port);
483 // TCommCaps capsBuf;
484 // TCommCapsV01& caps=capsBuf();
485 // theSerialPort->Caps(capsBuf);
487 // Check that the driver powering sequence has completed successfully by
488 // issueing a few simple driver control functions.
489 test.Next(_L("Modem power tests"));
490 test(theSerialPort->SetReceiveBufferLength(KStandardRxBufferSize)==KErrNone);
491 r=theSerialPort->ReceiveBufferLength();
492 // test.Printf(_L("(%d)"),r); // ???
493 test(r==KStandardRxBufferSize);
494 r=(TInt)theSerialPort->Signals();
495 // test.Printf(_L("(%d)"),r); // ???
500 test(timer.CreateLocal()==KErrNone);
503 tim+=TTimeIntervalSeconds(8);
505 UserHal::SwitchOff();
506 User::WaitForRequest(rs);
507 test(rs.Int()==KErrNone);
509 r=theSerialPort->ReceiveBufferLength();
510 // test.Printf(_L("(%d)"),r); // ???
511 test(r==KStandardRxBufferSize);
512 r=(TInt)theSerialPort->Signals();
513 // test.Printf(_L("(%d)"),r); // ???
515 User::After(2000000); // 2Secs !!!
519 test.Next(_L("Testing at 115K"));
520 testAllLineRates(EBps115200);
525 test.Next(_L("Testing at 57600"));
526 testAllLineRates(EBps57600);
531 test.Next(_L("Testing at 38400"));
532 testAllLineRates(EBps38400);
537 test.Next(_L("Testing at 19200"));
538 testAllLineRates(EBps19200);
541 test.Next(_L("Testing at 9600"));
542 testAllLineRates(EBps9600);
544 test.Next(_L("Close serial port"));
545 theSerialPort->Close();
546 delete theSerialPort;