Update contrib.
1 // Copyright (c) 1998-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_dceutl.cpp
19 #include <e32base_private.h>
28 const TInt KDisplayTitleY=0;
29 const TInt KDisplayMainY=2;
30 const TInt KDisplayTextY=17;
33 const TInt KTestPatternSize=250;
34 enum TPanic {ECreatingConsole,ELoadingPDD,ELoadingLDD,ECreatingRComm,EOpeningPort,ESettingPort,ECircBuf,EStraySignal};
36 #if defined (__WINS__)
37 #define PDD_NAME _L("ECDRV.PDD")
38 #define LDD_NAME _L("ECOMM.LDD")
40 #define PDD_NAME _L("EUARTn")
41 #define LDD_NAME _L("ECOMM")
44 class RComm : public RBusDevComm
47 TInt WriteS(const TDesC8& aDes);
50 CConsoleBase *TheConsole;
52 TCommConfig TheConfigBuf;
53 TCommConfigV01& TheConfig=TheConfigBuf();
56 TBuf8<KTestPatternSize> TheTestBuf;
58 LOCAL_C void Panic(TPanic aPanic)
65 TheSerialPort->Close();
68 User::Panic(_L("T_DCEUTL"),aPanic);
71 LOCAL_C TPtrC BaudrateText(TBps aBaudrate)
75 case EBps50: return(_L("50"));
76 case EBps75: return(_L("75"));
77 case EBps110: return(_L("110"));
78 case EBps134: return(_L("134"));
79 case EBps150: return(_L("150"));
80 case EBps300: return(_L("300"));
81 case EBps600: return(_L("600"));
82 case EBps1200: return(_L("1200"));
83 case EBps1800: return(_L("1800"));
84 case EBps2000: return(_L("2000"));
85 case EBps2400: return(_L("2400"));
86 case EBps3600: return(_L("3600"));
87 case EBps4800: return(_L("4800"));
88 case EBps7200: return(_L("7200"));
89 case EBps9600: return(_L("9600"));
90 case EBps19200: return(_L("19200"));
91 case EBps38400: return(_L("38400"));
92 case EBps57600: return(_L("57600"));
93 case EBps115200: return(_L("115200"));
94 case EBps230400: return(_L("230400"));
95 case EBps460800: return(_L("460800"));
96 case EBps576000: return(_L("576000"));
97 case EBps1152000: return(_L("1152000"));
98 case EBps4000000: return(_L("4000000"));
99 case EBpsSpecial: return(_L("Special"));
100 default: return(_L("Unknown"));
104 LOCAL_C TPtrC DtrText(TBool aDtrState)
107 return(_L("ASSERTED"));
109 return(_L("NEGATED"));
112 LOCAL_C TPtrC RtsText(TBool aRtsState)
115 return(_L("ASSERTED"));
117 return(_L("NEGATED"));
120 TInt RComm::WriteS(const TDesC8& aDes)
127 Write(s,aDes,aDes.Length());
128 User::WaitForRequest(s);
132 LOCAL_C void CenteredPrintf(TInt aLine,TRefByValue<const TDesC> aFmt,...)
134 // Print centrally on specified line
140 aBuf.AppendFormatList(aFmt,list);
141 TInt xPos = ((TheConsole->ScreenSize().iWidth)-aBuf.Length())/2;
144 TheConsole->SetPos(0,aLine);
145 TheConsole->ClearToEndOfLine();
146 TheConsole->SetPos(xPos,aLine);
147 TheConsole->Write(aBuf);
150 LOCAL_C void Heading(TRefByValue<const TDesC> aFmt,...)
158 aBuf.AppendFormatList(aFmt,list);
159 CenteredPrintf(KDisplayTitleY,aBuf);
162 LOCAL_C void Instructions(TBool topLine,TRefByValue<const TDesC> aFmt,...)
164 // Print instructions (dont use top line with hex display).
170 aBuf.AppendFormatList(aFmt,list);
171 CenteredPrintf((topLine)?KDisplayTextY-1:KDisplayTextY,aBuf);
174 LOCAL_C void StripeMem(TDes8& aBuf,TUint aStartChar,TUint anEndChar)
176 // Mark a buffer with repeating byte pattern
180 if (aStartChar==anEndChar)
182 aBuf.Fill(aStartChar);
186 TUint character=aStartChar;
187 for (TInt i=0;i<aBuf.Length();i++)
189 aBuf[i]=(TText8)character;
190 if(++character>anEndChar)
191 character=aStartChar;
195 #define COLUMN_HEADER _L(" RxBuf | Expected ")
196 LOCAL_C void DumpDescriptors(TDes8 &aLeft,TDes8 &aRight)
203 CenteredPrintf(KDisplayMainY+2,_L("Compare failed:"));
204 TInt minLen=Min(aLeft.Length(),aRight.Length());
205 CenteredPrintf(KDisplayMainY+3,COLUMN_HEADER);
208 TInt pos=KDisplayMainY+4;
211 b.Format(_L("%02x: "),i);
217 b.AppendFormat(_L("%02x "),v);
228 b.AppendFormat(_L("%02x "),v);
233 CenteredPrintf(pos++,b);
243 LOCAL_C TInt ChangeBaudrate()
249 CenteredPrintf(KDisplayMainY,_L("Select Baudrate:-"));
250 CenteredPrintf(KDisplayMainY+1,_L("A - 4800 "));
251 CenteredPrintf(KDisplayMainY+2,_L("B - 9600 "));
252 CenteredPrintf(KDisplayMainY+3,_L("C - 19200 "));
253 CenteredPrintf(KDisplayMainY+4,_L("D - 38400 "));
254 CenteredPrintf(KDisplayMainY+5,_L("E - 57600 "));
255 CenteredPrintf(KDisplayMainY+6,_L("F - 115200"));
259 c=(TUint)TheConsole->Getch();
262 while(c<'A' && c>'F');
266 case 'A': TheConfig.iRate=EBps4800; break;
267 case 'B': TheConfig.iRate=EBps9600; break;
268 case 'C': TheConfig.iRate=EBps19200; break;
269 case 'D': TheConfig.iRate=EBps38400; break;
270 case 'E': TheConfig.iRate=EBps57600; break;
271 case 'F': TheConfig.iRate=EBps115200; break;
272 case 0x1b: return(KErrNone);
274 TInt r=TheSerialPort->SetConfig(TheConfigBuf);
277 CenteredPrintf(KDisplayMainY+9,_L("Error (%d) changing baudrate"),r);
284 LOCAL_C TInt SendHayesCommand()
286 // Send short hayes command
290 TInt r=TheSerialPort->WriteS(_L8("AT&f\r"));
293 CenteredPrintf(KDisplayMainY+1,_L("Error (%d) sending data"),r);
299 LOCAL_C TInt SendLongHayesCommand()
301 // Send Long hayes command
305 TInt r=TheSerialPort->WriteS(_L8("AT&f&f&f&f&f&f&f\r"));
308 CenteredPrintf(KDisplayMainY+1,_L("Error (%d) sending data"),r);
314 const TInt KBufSize=0x100;
315 LOCAL_C TInt Loopback()
317 // Loopback data from Rx to Tx
321 CenteredPrintf(KDisplayMainY,_L("Loopback mode"));
322 CenteredPrintf(KDisplayMainY+5,_L("Hit a key abort"));
323 TheSerialPort->ResetBuffers();
325 CCirBuffer* cbufPtr=new CCirBuffer;
326 __ASSERT_ALWAYS(cbufPtr!=NULL,Panic(ECircBuf));
327 TRAPD(r,cbufPtr->SetLengthL(KBufSize));
328 __ASSERT_ALWAYS(r==KErrNone,Panic(ECircBuf));
329 TRequestStatus kStat,rStat,tStat = 0;
331 TBool TxActive=EFalse;
334 TPtr8 txPtr(&txChar,1);
337 TPtr8 rxPtr(&rxChar,1);
338 TheSerialPort->Read(rStat,rxPtr,1);
340 TheConsole->Read(kStat);
343 User::WaitForAnyRequest();
344 if (rStat!=KRequestPending)
346 if (rStat.Int()!=KErrNone)
348 CenteredPrintf(KDisplayMainY+5,_L("Rx error(%d)"),rStat.Int());
349 TheConsole->ReadCancel();
350 User::WaitForRequest(kStat);
353 cbufPtr->Put((TInt)rxChar);
354 TheSerialPort->Read(rStat,rxPtr,1);
357 txChar=(TUint8)cbufPtr->Get();
358 TheSerialPort->Write(tStat,txPtr,1);
362 else if (TxActive && tStat!=KRequestPending)
364 if (tStat.Int()!=KErrNone)
366 CenteredPrintf(KDisplayMainY+5,_L("Tx error(%d)"),tStat.Int());
367 TheSerialPort->ReadCancel();
368 User::WaitForRequest(rStat);
369 TheConsole->ReadCancel();
370 User::WaitForRequest(kStat);
375 TInt t=cbufPtr->Get();
381 TheSerialPort->Write(tStat,txPtr,1);
384 else if (kStat!=KRequestPending)
386 CenteredPrintf(KDisplayMainY+5,_L("Tx count (%d) - Hit another key"),TxCount);
387 TheSerialPort->ReadCancel();
388 User::WaitForRequest(rStat);
392 TheSerialPort->WriteCancel();
393 User::WaitForRequest(tStat);
405 LOCAL_C TInt ToggleDtr()
407 // Toggle state of DTR signal
413 TheSerialPort->SetSignals(0,KSignalDTR); // Negate DTR
418 TheSerialPort->SetSignals(KSignalDTR,0); // Assert DTR
424 LOCAL_C TInt ToggleRts()
426 // Toggle state of RTS signal
432 TheSerialPort->SetSignals(0,KSignalRTS); // Negate RTS
437 TheSerialPort->SetSignals(KSignalRTS,0); // Assert RTS
443 LOCAL_C TInt SendXoff()
449 TInt r=TheSerialPort->WriteS(_L8("\x13"));
452 CenteredPrintf(KDisplayMainY+1,_L("Error (%d) sending XOFF"),r);
458 LOCAL_C TInt ReceiveBlock()
464 CenteredPrintf(KDisplayMainY,_L("Waiting to recieve a block. Hit a key to abort"));
465 TheSerialPort->ResetBuffers();
466 TRequestStatus kStat,rStat;
467 TBuf8<KTestPatternSize> rdBuf(KTestPatternSize);
468 TheSerialPort->Read(rStat,rdBuf);
469 TheConsole->Read(kStat);
470 User::WaitForRequest(kStat,rStat);
471 if (rStat!=KRequestPending)
473 TheConsole->ReadCancel();
474 User::WaitForRequest(kStat);
475 if (rStat.Int()!=KErrNone)
477 CenteredPrintf(KDisplayMainY+5,_L("Rx error(%d)"),rStat.Int());
480 else if (rdBuf.Compare(TheTestBuf)!=0)
481 DumpDescriptors(rdBuf,TheTestBuf);
484 CenteredPrintf(KDisplayMainY+5,_L("Success"));
490 TheSerialPort->ReadCancel();
491 User::WaitForRequest(rStat);
496 LOCAL_C TInt TransmitBlock()
503 CenteredPrintf(KDisplayMainY,_L("Hit a key to transmit a block"));
504 while ((TUint)TheConsole->Getch()!=0x1b)
506 r=TheSerialPort->WriteS(TheTestBuf);
509 CenteredPrintf(KDisplayMainY+1,_L("Error (%d) transmitting block"),r);
516 LOCAL_C TInt SendXon()
522 TInt r=TheSerialPort->WriteS(_L8("\x11"));
525 CenteredPrintf(KDisplayMainY+1,_L("Error (%d) sending XON"),r);
531 LOCAL_C void DceUtil()
533 // DCE Serial Driver test utilities
536 TBuf<20> b(_L("BDHLOQRSTXY\x1b"));
540 TheConsole->ClearScreen();
541 TPtrC br=BaudrateText(TheConfig.iRate);
542 TPtrC dt=DtrText(TheDtrState);
543 TPtrC rt=RtsText(TheRtsState);
544 Heading(_L("T_DCEUTL 1.01 (Baudrate: %S DTR:%S RTS:%S)"),&br,&dt,&rt);
545 Instructions(ETrue,_L("Change(B)aud Toggle(D)TR Send(H)ayes (L)oopBack Send X(O)FF"));
546 Instructions(EFalse,_L("(Q)uit (R)xBlock Toggle RT(S) (T)xBlock Send(X)ON LongHayes(Y)?"));
550 c=(TUint)TheConsole->Getch();
553 while(b.Locate(c)==KErrNotFound);
557 case 'B': // Change baudrate
560 case 'D': // Toggle state of DTR signal
563 case 'H': // Send short hayes command
566 case 'L': // Loopback data from Rx to Tx
569 case 'O': // Send XOFF
572 case 'Q': case 0x1b: // Quit
574 case 'R': // Receive a block
577 case 'S': // Toggle state of RTS signal
580 case 'T': // Transmit a block
583 case 'X': // Send XON
586 case 'Y': // Send long hayes command
587 SendLongHayesCommand();
593 GLDEF_C TInt E32Main()
597 TRAPD(r,TheConsole=Console::NewL(_L("T_DCEUTL"),TSize(KConsFullScreen,KConsFullScreen)))
598 __ASSERT_ALWAYS(r==KErrNone,Panic(ECreatingConsole));
599 TheTestBuf.SetLength(KTestPatternSize);
600 StripeMem(TheTestBuf,'A','Z');
603 User::CommandLine(cmd);
605 if ((cmd.Length()>0) && (cmd[0]>='1' && cmd[0]<='4'))
606 port=(TInt)(cmd[0]-'0');
608 // Load Device Drivers
609 TheConsole->Printf(_L("Load PDD\n\r"));
610 TBuf<9> pddName=PDD_NAME;
611 #if !defined (__WINS__)
612 pddName[5]=(TText)('1'+port);
614 if (HAL::Get(HAL::EMachineUid, muid)==KErrNone)
616 // Brutus uses EUART4 for both COM3 and COM4
617 if (muid==HAL::EMachineUid_Brutus && port==4)
618 pddName[5]=(TText)'4';
621 r=User::LoadPhysicalDevice(pddName);
622 __ASSERT_ALWAYS(r==KErrNone||r==KErrAlreadyExists,Panic(ELoadingPDD));
623 TheConsole->Printf(_L("Load LDD\n\r"));
624 r=User::LoadLogicalDevice(LDD_NAME);
625 __ASSERT_ALWAYS(r==KErrNone||r==KErrAlreadyExists,Panic(ELoadingLDD));
627 // Create RComm object
628 TheConsole->Printf(_L("Create RComm object\n\r"));
629 TheSerialPort=new RComm;
630 __ASSERT_ALWAYS(TheSerialPort!=NULL,Panic(ECreatingRComm));
633 TheConsole->Printf(_L("Open Serial Port (%d)\n\r"),port);
634 r=TheSerialPort->Open(port);
635 __ASSERT_ALWAYS(r==KErrNone,Panic(EOpeningPort));
638 TheConsole->Printf(_L("Setup serial port\n\r"));
639 TheSerialPort->Config(TheConfigBuf);
640 TheConfig.iRate=EBps9600;
641 TheConfig.iDataBits=EData8;
642 TheConfig.iStopBits=EStop1;
643 TheConfig.iParity=EParityNone;
644 TheConfig.iHandshake=(KConfigFreeRTS|KConfigFreeDTR); // So we can control them ourselves
645 r=TheSerialPort->SetConfig(TheConfigBuf);
646 __ASSERT_ALWAYS((r==KErrNone||r==KErrNotSupported),Panic(ESettingPort));
647 if (r==KErrNotSupported)
649 // Port may not support the handshake settings
650 TheConfig.iHandshake=0;
651 r=TheSerialPort->SetConfig(TheConfigBuf);
652 __ASSERT_ALWAYS(r==KErrNone,Panic(ESettingPort));
656 TheSerialPort->SetSignals(KSignalDTR,0); // Assert DTR
659 TheSerialPort->SetSignals(KSignalRTS,0); // Assert RTS
664 TheSerialPort->Close();
665 delete TheSerialPort;