os/kernelhwsrv/kerneltest/e32test/device/t_sirco2.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_sirco2.cpp
    15 // 
    16 //
    17 
    18 #include "t_slowir.h"
    19 #include <e32svr.h> 
    20 #include <e32std.h>
    21 #include <e32std_private.h> 
    22 #include <e32hal.h>
    23 #include <hal.h>
    24 
    25 #if defined(__VC32__) && _MSC_VER==1100
    26 // Disable MSVC++ 5.0 aggressive warnings about non-expansion of inline functions. 
    27 #pragma warning(disable : 4710)	// function '...' not expanded
    28 #endif
    29 
    30 #if defined (__WINS__)
    31 #define PDD_NAME _L("ECDRV.PDD")
    32 #define LDD_NAME _L("ECOMM.LDD")
    33 #else
    34 #define PDD_NAME _L("EUART")
    35 #define LDD_NAME _L("ECOMM")
    36 #endif
    37 
    38 #define PDD2_NAME _L("EUART")
    39 
    40 #ifdef __WINS__
    41 	#include <es_sock.h>
    42 	RSocketServ ss;
    43 #endif
    44 
    45 #define FIND_NAME _L("Comm.*")
    46 
    47 const TInt KUnit0=0;
    48 const TInt KUnit1=1;
    49 const TInt KUnit2=2;
    50 
    51 //LOCAL_D RTest test(_L("T_SlowIR"));
    52 
    53 TBuf8<2060> WriteBuf;
    54 TBuf8<2060> ReadBuf;
    55 TInt iTimeDelay=1000000;
    56 TInt iBufSz=2000;
    57 TBool iRWToWrite=EFalse;
    58 
    59 void ResetReadBuffer()
    60 	{
    61 	TInt i=0;
    62 	ReadBuf.SetLength(2060);
    63 	for(i=0;i<2050;i++)
    64 		ReadBuf[i] ='R';
    65 	}
    66 
    67 void ResetWriteBuffer()
    68 //
    69 // Mark a buffer with repeating byte pattern
    70 //
    71 	{
    72 	TUint startChar='A';
    73 	TUint endChar='z';
    74 	WriteBuf.SetLength(2060);
    75 
    76 	TUint character=startChar;
    77 	for (TInt i=0;i<WriteBuf.Length();i++)
    78 		{
    79 		WriteBuf[i]=(TText8)character;
    80 		if(++character>endChar)
    81 			character=startChar;
    82 		}
    83 	}
    84 
    85 TInt CActiveRW::ErrorStats()
    86 	{ // Computes error %ge
    87 		if(iRxCount != 0)
    88 			return (iRxErrCount*200+iRxCount)/(2*iRxCount);
    89 		else
    90 			return 0;
    91 	}
    92 
    93 TBool CActiveRW::CompareBuffers(TInt aLen)
    94 	{
    95 	TInt i=0;
    96 	if(aLen !=ReadBuf.Length() || aLen !=WriteBuf.Length())
    97 		return EFalse;
    98 
    99 	while(i<aLen)
   100 		{
   101 		if(ReadBuf[i]!=WriteBuf[i]){
   102 			return EFalse;}
   103 		i++;
   104 		}
   105 	return ETrue;
   106 	}
   107 
   108 void SetUpBuffers()
   109 	{
   110 	ResetReadBuffer();
   111 	ResetWriteBuffer();
   112 	}
   113 
   114 CActiveConsole::CActiveConsole(CConsoleBase* aConsole) 
   115 	: CActive(EPriorityNormal)
   116 	{
   117 	iConsole=aConsole;
   118 	iInit1  =EFalse;
   119 	iInit2  =EFalse;
   120 	}
   121 
   122 CActiveConsole* CActiveConsole::NewLC(CConsoleBase* aConsole)
   123 	{
   124 	CActiveConsole* self = new (ELeave) CActiveConsole(aConsole);
   125 	self->ConstructL();
   126 	return self;
   127 	}
   128 
   129 void CActiveConsole::ConstructL ()
   130 	{ 
   131 	TCommCaps aCapsBuf;
   132 	TCommCapsV01& aCaps=aCapsBuf();
   133 	TCommConfig aConfigBuf;
   134 	TCommConfigV01& aConfig=aConfigBuf();
   135 	iConsole->Printf(_L("\r\n"));
   136 	CActiveScheduler::Add(this);			// Add to active scheduler
   137 
   138     TBuf<10> pddName=PDD_NAME;
   139 #if defined (__MARM__)
   140 	const TInt KMaxPdds=10;
   141 	iConsole->Printf(_L("Load MARM PDDs\n\r"));
   142 	RDebug::Print(_L("Load MARM PDDs\n\r"));	
   143 #else
   144 	const TInt KMaxPdds=0;
   145 	iConsole->Printf(_L("Load WINS PDD\n\r"));
   146 	iConsole->Printf(PDD_NAME);
   147 #endif
   148 //		iConsole->Read(iStatus); 
   149 
   150 	TInt i;
   151 	TInt r;
   152 	for (i=-1; i<KMaxPdds; ++i)
   153 		{
   154 		if (i==0)
   155 			pddName.Append(TChar('0'));
   156 		else if (i>0)
   157 			pddName[pddName.Length()-1] = (TText)('0'+i);
   158 		r=User::LoadPhysicalDevice(pddName);
   159 		if (r==KErrNone || r==KErrAlreadyExists)
   160 			{
   161 			iConsole->Printf(_L("PDD %S loaded\n"),&pddName);
   162 			RDebug::Print(_L("PDD %S loaded\n"),&pddName);	
   163 			}
   164 		}
   165 
   166 	RDebug::Print(_L("Load MARM LDD\n\r"));	
   167 	RDebug::Print(LDD_NAME);	
   168 	iConsole->Printf(_L("Load LDD\n\r"));
   169 	iConsole->Printf(LDD_NAME);
   170 	r=User::LoadLogicalDevice(LDD_NAME);
   171 	RDebug::Print(_L("\n\rReturn %d\n\r"),r);	
   172 	iConsole->Printf(_L("\n\rReturn %d\n\r"),r);
   173 //	test(r==KErrNone || r==KErrAlreadyExists);
   174 
   175 	RDebug::Print(_L("\n\rFind PDDs\n\r"));	
   176 	iConsole->Printf(_L("Find PDDs\n"));
   177 	TFindPhysicalDevice findPDD(FIND_NAME);
   178 	TFullName findResult;
   179 	TInt res=findPDD.Next(findResult);
   180 	while (res==KErrNone)
   181 		{
   182 		RDebug::Print(_L("Found Driver: %S\n\r"),&findResult);	
   183 		iConsole->Printf(_L("Found Driver: %S\n\r"),&findResult);
   184 		res=findPDD.Next(findResult);
   185 		}
   186 
   187 	iConsole->Printf(_L("Found drivers\n\r"));
   188 
   189 	iPort=new RCommDev;
   190 //	test(iPort!=NULL);
   191 
   192 	SetUpBuffers();
   193 
   194 	TInt muid=0;
   195 	TInt ret=HAL::Get(HAL::EMachineUid, muid);
   196 	if (ret!=KErrNone)
   197 		iConsole->Printf(_L("Error %d reading MachineUid\r\n"),ret);
   198 	if(muid==HAL::EMachineUid_Integrator)
   199 		iConsole->Printf(_L("Platform is Integrator\r\n"));
   200 
   201 	if(muid==HAL::EMachineUid_Integrator) ret=iPort->Open(KUnit2); // unit =2 for Integrator
   202 	else ret=iPort->Open(KUnit1); // unit == 1 for brutus
   203 	if (ret!=KErrNone)
   204 		iConsole->Printf(_L("Error %d on opening Slow ir port\r\n"),ret);
   205 	else
   206 		iConsole->Printf(_L("Successfully opened Slow ir port\r\n"));
   207 
   208 	iPort->Caps(aCapsBuf);
   209 
   210 	if (ret!=KErrNone)
   211 		iConsole->Printf(_L("Error %d on getting caps\r\n"),ret);
   212 	else
   213 		iConsole->Printf(_L("Sir Caps: %d\r\n"),aCaps.iRate);
   214 
   215 	iPort->Config(aConfigBuf);
   216 	if (ret!=KErrNone)
   217 		iConsole->Printf(_L("Error %d getting config\r\n"),ret);
   218 	else
   219 		iConsole->Printf(_L("IR port config read\r\n"));
   220 
   221     iConsole->Printf(_L("****Choose Rate*****\r\n"));
   222     iConsole->Printf(_L("press '1' for   9600\r\n"));
   223     iConsole->Printf(_L("press '2' for  19200\r\n"));
   224     iConsole->Printf(_L("press '3' for  38400\r\n"));
   225     iConsole->Printf(_L("press '4' for  57600\r\n"));
   226     iConsole->Printf(_L("press '5' for 115200\r\n"));
   227 
   228     TRequestStatus key;
   229     iConsole->Read(key);
   230     User::WaitForRequest(key);
   231     TChar pressedkey = TChar(iConsole->KeyCode());
   232 
   233     switch(pressedkey)
   234         {
   235     case '1':
   236         aConfig.iRate=EBps9600;
   237         break;
   238     case '2':
   239         aConfig.iRate=EBps19200;
   240         break;
   241     case '3':
   242         aConfig.iRate=EBps38400;
   243         break;
   244     case '4':
   245         aConfig.iRate=EBps57600;
   246         break;
   247     case '5':
   248         aConfig.iRate=EBps115200;
   249         break;
   250     default:
   251         aConfig.iRate=EBps115200;
   252         break;
   253         }
   254 
   255 	aConfig.iHandshake=0;
   256 	aConfig.iSIREnable=ESIREnable;
   257 	aConfig.iParity=EParityNone;
   258 	aConfig.iDataBits=EData8;
   259 	aConfig.iStopBits=EStop1;
   260 
   261 	ret=iPort->SetConfig(aConfigBuf);
   262 
   263 	if (ret!=KErrNone)
   264 		iConsole->Printf(_L("Error %d setting config\r\n"),ret);
   265 	else
   266 		iConsole->Printf(_L("Have configured port\n"));
   267 
   268 
   269 	iRW=CActiveRW::NewL(iConsole,iPort);
   270 
   271 
   272 	if(iRW)
   273 		iConsole->Printf(_L("Have created writer\r\n"));
   274 	else
   275 		iConsole->Printf(_L("Failed to create writer\r\n"));
   276 	}
   277 
   278 CActiveConsole::~CActiveConsole()
   279 	{
   280 	// Make sure we're cancelled
   281 	Cancel();
   282 
   283 	if(iRW)
   284 		delete iRW;
   285 
   286 	iPort->Close();
   287 	}
   288 
   289 void  CActiveConsole::DoCancel()
   290 	{
   291 	iConsole->ReadCancel();
   292 	}
   293 
   294 void  CActiveConsole::RunL()
   295 	{
   296 	ProcessKeyPressL(TChar(iConsole->KeyCode()));
   297 //	iConsole->Printf(_L("CActiveConsole - Completed with code %d\r\n\r\n"), iStatus.Int ());
   298 	}
   299 
   300 void CActiveConsole::RequestCharacter()
   301 	{
   302 	if(!iInit1)
   303 		{
   304 		Options1();
   305 		return;
   306 		}
   307 	if(!iInit2)
   308 		{
   309 		Options2();
   310 		return;
   311 		}
   312 	  // A request is issued to the CConsoleBase to accept a
   313 	  // character from the keyboard.
   314 	iConsole->Printf(_L("*********************************\r\n"));
   315 	iConsole->Printf(_L("press Escape to quit\r\n"));
   316 	iConsole->Printf(_L("press 'r' to start as reader\r\n"));
   317 	iConsole->Printf(_L("press 'w' to start as writer\r\n"));
   318 	iConsole->Printf(_L("press 's' stop \r\n"));
   319 
   320 	iConsole->Read(iStatus); 
   321 	SetActive();
   322 	}
   323 
   324 void CActiveConsole::Options1()
   325 	{
   326 	iConsole->Printf(_L("*****Choose Delay*****\r\n"));
   327 	iConsole->Printf(_L("press '1' 1.00 sec delay\r\n"));
   328 	iConsole->Printf(_L("press '2' 0.10 sec delay\r\n"));
   329 	iConsole->Printf(_L("press '3' 0.01 sec delay\r\n"));
   330 	iConsole->Printf(_L("press '4' 0.00 sec delay\r\n"));
   331 	iConsole->Read(iStatus); 
   332 	SetActive();	
   333 	}
   334 
   335 void CActiveConsole::Options2()
   336 	{
   337 	iConsole->Printf(_L("****Choose Buf Sz*****\r\n"));
   338 	iConsole->Printf(_L("press '1' 1    byte \r\n"));
   339 	iConsole->Printf(_L("press '2' 4    bytes\r\n"));
   340 	iConsole->Printf(_L("press '3' 2000 bytes\r\n"));
   341 	iConsole->Printf(_L("press '4' 2051 bytes\r\n"));
   342 	iConsole->Read(iStatus); 
   343 	SetActive();	
   344 	}
   345 
   346 void CActiveConsole::ProcessKeyPressL(TChar aChar)
   347 	{
   348 	if (aChar == EKeyEscape)
   349 		{
   350 		CActiveScheduler::Stop();
   351 		return;
   352 		}
   353 
   354 	if(!iInit1)
   355 		{
   356 		switch(aChar)
   357 			{
   358 		case '1'://1 sec
   359 			iTimeDelay=1000000;
   360 			break;
   361 		case '2'://0.1 sec
   362 			iTimeDelay=100000;
   363 			break;
   364 		case '3'://0.01 sec
   365 			iTimeDelay=10000;
   366 			break;
   367 		case '4'://0 sec
   368 			iTimeDelay=0;
   369 			break;
   370 		default:
   371 			iTimeDelay=1000000;
   372 			break;
   373 			}
   374 		iConsole->Printf(_L("Time Delay: %d\r\n"),iTimeDelay);
   375 		iInit1=ETrue;
   376 		RequestCharacter();
   377 		return;
   378 		}
   379 	if(!iInit2)
   380 		{
   381 		switch(aChar)
   382 			{
   383 		case '1':
   384 			iBufSz=1;
   385 			break;
   386 		case '2':
   387 			iBufSz=4;
   388 			break;
   389 		case '3':
   390 			iBufSz=2000;
   391 			break;
   392 		case '4':
   393 			iBufSz=2051;
   394 			break;
   395 		default:
   396 			iBufSz=2000;
   397 			break;
   398 			}
   399 		// check that we have enough space
   400 		if(iBufSz>iPort->ReceiveBufferLength())
   401 			iPort->SetReceiveBufferLength(iBufSz);
   402 		// if it won't do it, then settle for what it is by default
   403 		if(iBufSz>iPort->ReceiveBufferLength())
   404 			iBufSz=iPort->ReceiveBufferLength();
   405 
   406 		iConsole->Printf(_L("Buffer size: %d\r\n"),iBufSz);
   407 		iInit2=ETrue;
   408 		RequestCharacter();
   409 		return;
   410 		}
   411 
   412 	switch (aChar)
   413 		{
   414 	case 'r'://start reader
   415 	case 'R'://start reader
   416 		iRW->Start(EFalse);
   417 		break;
   418 	case 'w'://start writer
   419 	case 'W'://start writer
   420 		iRW->Start(ETrue);
   421 		break;
   422 	case 's'://stop reader
   423 	case 'S'://stop reader
   424 		iRW->Stop();
   425 		break;
   426 	default:
   427 		iConsole->Printf(_L("\r\nUnknown Command\r\n\r\n"));
   428 		break;
   429 		}
   430 	RequestCharacter ();
   431 	return;
   432 	}
   433 
   434 
   435 
   436 //
   437 // class CActiveRW
   438 //
   439 
   440 CActiveRW::CActiveRW(CConsoleBase* aConsole,RCommDev* aPort)
   441 	: CActive (EPriorityNormal)
   442 	{
   443 	iConsole=aConsole;
   444 	iPort=aPort;
   445 	iLength=0;
   446 	iUnrecovered=0;
   447 	iRxCount=0;
   448 	iRxErrCount=0;
   449 	}
   450 
   451 CActiveRW* CActiveRW::NewL(CConsoleBase* aConsole,RCommDev* aPort)
   452 	{
   453 	CActiveRW* self = new (ELeave) CActiveRW(aConsole,aPort);
   454 
   455 	CleanupStack::PushL (self);
   456 	self->ConstructL();
   457 	CActiveScheduler::Add (self);
   458 	CleanupStack::Pop ();
   459 	return (self);
   460 	}
   461 
   462 void CActiveRW::ConstructL()
   463 	{
   464 	}
   465 
   466 CActiveRW::~CActiveRW()
   467 	{
   468 	Cancel();
   469 	}
   470 
   471 void CActiveRW::RunL ()
   472 	{
   473 
   474 	TInt i=0;
   475 	if(iNextXfer==EWriteXfer)
   476 		iRxCount++;
   477 
   478 	if (iStatus != KErrNone)
   479 	{
   480 		if(iNextXfer==EWriteXfer)
   481 			{
   482 			iRxErrCount++;
   483 			iConsole->Printf(_L("Error %d on reading, error = %d%%\r\n"),iStatus.Int(),ErrorStats());
   484 			iPort->ResetBuffers();
   485 			iNextXfer=EDiscardXfer;
   486 
   487 			}
   488 		else
   489 			{
   490 			iConsole->Printf(_L("Error %d on writing\r\n"),iStatus.Int());
   491 			iConsole->Printf(_L("Received %d characters\r\n"),iPort->QueryReceiveBuffer());
   492 			}
   493 	}
   494 	//return;
   495 
   496 	if(iTimeDelay)
   497 		User::After(iTimeDelay);
   498 
   499 	if(iNextXfer==EReadXfer)
   500 		{
   501 //		iPort->ResetBuffers();
   502 		ResetReadBuffer();
   503 		ReadBuf.SetLength(iBufSz);
   504 		iConsole->Printf(_L("Reading(%d)"),ReadBuf.Length());
   505 		iPort->Read(iStatus, ReadBuf, ReadBuf.Length());
   506 		iNextXfer=EWriteXfer;
   507 		}
   508 	else // EWriteXfer || EDiagXfer || EDiscardXfer
   509 		{
   510 		if(iNextXfer==EWriteXfer)
   511 			{
   512 			if(!CompareBuffers(iBufSz))
   513 				{
   514 				iConsole->Printf(_L("\n***** Buffers don't match *****\n"));
   515 				for (i=0;i<ReadBuf.Length();i++)
   516 					{
   517 					iConsole->Printf(_L("WriteBuf[%d] = %02x, ReadBuf[%d] = %02x\n"),i,WriteBuf[i],i,ReadBuf[i]);
   518 					}
   519 
   520 				iUnrecovered=iPort->QueryReceiveBuffer();
   521 				iConsole->Printf(_L("Unrecovered %d characters\r\n"),iUnrecovered);
   522 				if(iUnrecovered)
   523 					{
   524 					ReadBuf.SetLength(Max(iBufSz,iUnrecovered));
   525 					iNextXfer=EDiagXfer;
   526 					iConsole->Printf(_L("\n***** Doing Diagnostic read *****\n"));
   527 					iPort->Read(iStatus, ReadBuf, iUnrecovered);
   528 					SetActive();
   529 					return;
   530 					}
   531 				}
   532 			else
   533 				{
   534 				iConsole->Printf(_L("ok\n"));
   535 				}
   536 			}
   537 		else if(iNextXfer==EDiagXfer)//EDiagXfer
   538 			{
   539 			iConsole->Printf(_L("\n***** Diagnostic dump %d *****\n"),iUnrecovered);
   540 
   541 			for (i=0;i<iUnrecovered;i++)
   542 				{
   543 				iConsole->Printf(_L("ReadBuf[%d] = %02x\n"),i,ReadBuf[i]);
   544 				}
   545 			}
   546 		WriteBuf.SetLength(iBufSz);
   547 		iConsole->Printf(_L("\nWriting(%d), "),WriteBuf.Length());
   548 		iPort->Write(iStatus, WriteBuf, WriteBuf.Length());
   549 		iNextXfer=EReadXfer;
   550 		
   551 		}
   552 	iUnrecovered=0;
   553 	SetActive();
   554 	}
   555 
   556 void CActiveRW::Start(TBool StartWriting)
   557 	{
   558 	iPort->ResetBuffers();
   559 	if(IsActive())
   560 		return;
   561 	if(StartWriting)
   562 		{
   563 //		iConsole->Printf(_L("Starting with write.%d....\r\n"),iBufSz);
   564 		WriteBuf.SetLength(iBufSz);
   565 		iPort->Write(iStatus, WriteBuf, WriteBuf.Length());
   566 		iNextXfer=EReadXfer;
   567 		}
   568 	else
   569 		{
   570 		ReadBuf.SetLength(iBufSz);
   571 		iPort->Read(iStatus, ReadBuf, ReadBuf.Length());
   572 //		iConsole->Printf(_L("Starting with read.%d....\r\n"),ReadBuf.Length());
   573 		iNextXfer=EWriteXfer;
   574 		}
   575 	SetActive();
   576 	}
   577 
   578 void CActiveRW::Stop()
   579 	{
   580 	iConsole->Printf(_L("Stopping.....\r\n"));
   581 	iConsole->Printf(_L("Unrecovered %d characters\r\n"),iPort->QueryReceiveBuffer());
   582 	Cancel();
   583 	}
   584 
   585 void CActiveRW::DoCancel()
   586 	{
   587 	iPort->WriteCancel();
   588 	iPort->ReadCancel();
   589 	}
   590 
   591 #pragma warning (default:4710)