usbmgmt/usbmgrtest/t_usbmodem/src/t_usbmodem.cpp
changeset 0 c9bc50fca66e
child 15 f92a4f87e424
equal deleted inserted replaced
-1:000000000000 0:c9bc50fca66e
       
     1 /*
       
     2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * This test is used to pipe data to and from the USB ACM comm port to
       
    16 * a real comm port connected to a modem.
       
    17 *
       
    18 */
       
    19 
       
    20 #include "t_usbmodem.h"
       
    21 #include <e32debug.h>
       
    22 
       
    23 //
       
    24 //--- class CSimplexRead ----------------------------------------------------
       
    25 //
       
    26 CSimplexRead* CSimplexRead::NewL(CDuplex& aDuplex)
       
    27 	{
       
    28 	CSimplexRead* self = new (ELeave) CSimplexRead(aDuplex);
       
    29 	self->ConstructL();
       
    30 	CActiveScheduler::Add(self);
       
    31 	return (self);	
       
    32 	}
       
    33 
       
    34 void CSimplexRead::ConstructL()
       
    35 	{
       
    36 	// Not much to do yet
       
    37 	}
       
    38 
       
    39 void CSimplexRead::StartL()
       
    40 	{
       
    41 #ifdef _DEBUG
       
    42 	RDebug::Print(_L(": CSimplexRead::StartL\n"));
       
    43 #endif
       
    44 	if(IsActive())
       
    45 		{
       
    46 		RDebug::Print(_L(": CSimplexRead::StartL Warning - Already active\n"));
       
    47 		return;
       
    48 		}
       
    49 	//iBuffer.SetMax();	
       
    50 	iCommPort->ReadOneOrMore(iStatus, iBuffer);
       
    51 	SetActive();
       
    52 	}
       
    53 
       
    54 void CSimplexRead::RunL()
       
    55 	{
       
    56 #ifdef _DEBUG
       
    57 	RDebug::Print(_L(": CSimplexRead::RunL - iStatus:%d iBuffer.Length:%d TotalAmount Left:%d\n"),iStatus.Int(),iBuffer.Length(),iTotalAmount);
       
    58 #endif
       
    59 	//RDebug::Print(_L(">%S<\n"),&iBuffer);
       
    60 	//Notify Duplex object
       
    61 	iDuplex.NotifyRead(iStatus.Int(),iBuffer.Length(), iBuffer);
       
    62 	}
       
    63 
       
    64 void CSimplexRead::DoCancel(void)
       
    65 	{
       
    66 #ifdef _DEBUG
       
    67 	RDebug::Print(_L(": CSimplexRead::DoCancel\n"));
       
    68 #endif
       
    69 	if(iCommPort)
       
    70 		iCommPort->ReadCancel();
       
    71 #ifdef _DEBUG
       
    72 	RDebug::Print(_L(": CSimplexRead::DoCancel done\n"));
       
    73 #endif
       
    74 	}
       
    75 
       
    76 //
       
    77 //--- class CSimplexWrite ----------------------------------------------------
       
    78 //
       
    79 CSimplexWrite* CSimplexWrite::NewL(CDuplex& aDuplex)
       
    80 	{
       
    81 	CSimplexWrite* self = new (ELeave) CSimplexWrite(aDuplex);
       
    82 	self->ConstructL();
       
    83 	CActiveScheduler::Add(self);
       
    84 	return (self);	
       
    85 	}
       
    86 
       
    87 void CSimplexWrite::ConstructL()
       
    88 	{
       
    89 	// Not much to do yet
       
    90 	}
       
    91 
       
    92 void CSimplexWrite::StartL(TDes8& aBuf)
       
    93 	{
       
    94 #ifdef _DEBUG
       
    95 	RDebug::Print(_L(": CSimplexWrite::StartL - About to write l=%d\n"),aBuf.Length());
       
    96 #endif
       
    97 	if(IsActive())
       
    98 		{
       
    99 		RDebug::Print(_L(": CSimplexWrite::StartL Warning - Already active\n"));
       
   100 		return;
       
   101 		}
       
   102 
       
   103 	iCommPort->Write(iStatus, aBuf);
       
   104 	SetActive();
       
   105 	}
       
   106 
       
   107 void CSimplexWrite::RunL()
       
   108 	{
       
   109 #ifdef _DEBUG
       
   110 	RDebug::Print(_L(": CSimplexWrite::RunL - iStatus:%d"),iStatus.Int());
       
   111 #endif
       
   112 	//Notify Duplex object
       
   113 	iDuplex.NotifyWrite(iStatus.Int());
       
   114 	}
       
   115 
       
   116 void CSimplexWrite::DoCancel(void)
       
   117 	{
       
   118 	if(iCommPort)
       
   119 		iCommPort->WriteCancel();
       
   120 #ifdef _DEBUG
       
   121 	RDebug::Print(_L(": CSimplexWrite:: Comm port write Canceled\n"));
       
   122 #endif
       
   123 	}
       
   124 
       
   125 //
       
   126 // --- class CDuplex ---------------------------------------------------------
       
   127 //
       
   128 CDuplex* CDuplex::NewL()
       
   129 	{
       
   130 	CDuplex* self = new (ELeave) CDuplex();
       
   131 	self->ConstructL();
       
   132 	return (self);	
       
   133 	}
       
   134 
       
   135 void CDuplex::ConstructL()
       
   136 	{
       
   137 #ifdef _DEBUG
       
   138 	RDebug::Print(_L(": CDuplex::ConstructL\n"));
       
   139 #endif
       
   140 	//Create reader and writer
       
   141 	iSimplexRead = CSimplexRead::NewL(*this);
       
   142 	iSimplexWrite = CSimplexWrite::NewL(*this);
       
   143 	}
       
   144 
       
   145 void CDuplex::StartL()
       
   146 	{
       
   147 #ifdef _DEBUG
       
   148 	RDebug::Print(_L(": CDuplex::StartL\n"));
       
   149 #endif
       
   150 	iSimplexRead->StartL();
       
   151 	}
       
   152 
       
   153 void CDuplex::Cancel()
       
   154 	{
       
   155 #ifdef _DEBUG
       
   156 	RDebug::Print(_L(": CDuplex::Cancel\n"));
       
   157 #endif
       
   158 	if(iSimplexRead)
       
   159 		iSimplexRead->Cancel(); 
       
   160 	if(iSimplexWrite)
       
   161 		iSimplexWrite->Cancel();
       
   162 	}
       
   163 
       
   164 #ifdef _DEBUG
       
   165 	void CDuplex::NotifyRead(TInt aErr,TInt aSize, TDes8& aBuf)
       
   166 #endif
       
   167 
       
   168 #ifndef _DEBUG
       
   169 	void CDuplex::NotifyRead(TInt aErr,TInt /*aSize*/, TDes8& aBuf)
       
   170 #endif
       
   171 	{
       
   172 	TInt ignoreErr;
       
   173 
       
   174 	// Check if error
       
   175 	switch(aErr)
       
   176 		{
       
   177 		case KErrNone:
       
   178 			// Start the write
       
   179 #ifdef _DEBUG
       
   180 			RDebug::Print(_L(": CDuplex::NotifyRead err:%d recvd:%d\n"), aErr, aSize);
       
   181 #endif
       
   182 			TRAP(ignoreErr, iSimplexWrite->StartL(aBuf));
       
   183 			break;
       
   184 		default:
       
   185 #ifdef _DEBUG
       
   186 			RDebug::Print(_L(": CDuplex::NotifyRead error:%d\n"), aErr);
       
   187 #endif
       
   188 			Cancel();
       
   189 			break;
       
   190 		}
       
   191 #ifdef _DEBUG
       
   192 	RDebug::Print(_L(": CDuplex::NotifyRead Done \n"), aErr, aSize);
       
   193 #endif
       
   194 	return;
       
   195 	}
       
   196 
       
   197 void CDuplex::NotifyWrite(TInt aErr)
       
   198 	{
       
   199 	TInt ignoreErr;
       
   200 
       
   201 	// Check if error
       
   202 	switch(aErr)
       
   203 		{
       
   204 		case KErrNone:
       
   205 			//Not much right now, just trigger another read
       
   206 #ifdef _DEBUG
       
   207 			RDebug::Print(_L(": CDuplex::NotifyWrite err:%d \n"), aErr);
       
   208 #endif
       
   209 			TRAP(ignoreErr, iSimplexRead->StartL());
       
   210 			break;
       
   211 		default:
       
   212 #ifdef _DEBUG
       
   213 			RDebug::Print(_L(": CDuplex::NotifyWrite error:%d\n"), aErr);
       
   214 #endif
       
   215 			Cancel();
       
   216 			break;
       
   217 		}
       
   218 #ifdef _DEBUG
       
   219 	RDebug::Print(_L(": CDuplex::NotifyWrite Done \n"), aErr);
       
   220 #endif
       
   221 
       
   222 	return;
       
   223 	}
       
   224 
       
   225 CDuplex::~CDuplex()
       
   226 	{
       
   227 #ifdef _DEBUG
       
   228 	RDebug::Print(_L(": Deleting CDuplex\n"));
       
   229 #endif
       
   230 	Cancel();
       
   231 	delete iSimplexRead;
       
   232 	delete iSimplexWrite;
       
   233 	}
       
   234 
       
   235 //
       
   236 //----- class CConfigChangeNotifier -----------------------------------------
       
   237 //
       
   238 CConfigChangeNotifier::CConfigChangeNotifier()
       
   239 	:CActive(CActive::EPriorityStandard)
       
   240 	{
       
   241 	}
       
   242 
       
   243 CConfigChangeNotifier* CConfigChangeNotifier::NewL()
       
   244 	{
       
   245 	CConfigChangeNotifier* self = new (ELeave) CConfigChangeNotifier();
       
   246 	CActiveScheduler::Add(self);				// Add to active scheduler
       
   247 	return self;
       
   248 	}
       
   249 	
       
   250 void CConfigChangeNotifier::DoCancel()
       
   251 	{
       
   252 #ifdef _DEBUG
       
   253 	RDebug::Print(_L(": CConfigChangeNotifier::DoCancel"));
       
   254 #endif
       
   255 	TheUsbPort.NotifyConfigChangeCancel();
       
   256 	}
       
   257 
       
   258 void CConfigChangeNotifier::StartL()
       
   259 	{
       
   260 	if (IsActive())
       
   261 		{
       
   262 		RDebug::Print(_L(": Config chabge Notifier Already active"));
       
   263 		return;
       
   264 		}
       
   265 	TheUsbPort.NotifyConfigChange(iStatus, TheUsbConfigBuf);
       
   266 	SetActive();
       
   267 	}
       
   268 
       
   269 void CConfigChangeNotifier::RunL()
       
   270 	{
       
   271 #ifdef _DEBUG
       
   272 	RDebug::Print(_L(": Config Change Notifier activated"));
       
   273 #endif
       
   274 	// Set the serial port config
       
   275 	TCommConfig aConfig;
       
   276 	TheUsbPort.Config(aConfig);
       
   277 	TheSerialPort.SetConfig(aConfig);
       
   278 	StartL();
       
   279 	}
       
   280 
       
   281 //
       
   282 //----- class CSignalChageNotifier -----------------------------------------
       
   283 //
       
   284 CSignalChangeNotifier::CSignalChangeNotifier(TBool aIsUsbPort)
       
   285 	:CActive(CActive::EPriorityStandard),
       
   286 	iIsUsbPort(aIsUsbPort)
       
   287 	{
       
   288 	}
       
   289 
       
   290 CSignalChangeNotifier* CSignalChangeNotifier::NewL(TBool aIsUsbPort)
       
   291 	{
       
   292 	CSignalChangeNotifier* self = new (ELeave) CSignalChangeNotifier(aIsUsbPort);
       
   293 	CActiveScheduler::Add(self);				// Add to active scheduler
       
   294 	return self;
       
   295 	}
       
   296 	
       
   297 void CSignalChangeNotifier::DoCancel()
       
   298 	{
       
   299 #ifdef _DEBUG
       
   300 	RDebug::Print(_L(": CSignalChangeNotifier::DoCancel"));
       
   301 #endif
       
   302 	if (iIsUsbPort)
       
   303 		TheUsbPort.NotifySignalChangeCancel();
       
   304 	else 
       
   305 		TheSerialPort.NotifySignalChangeCancel();
       
   306 	}
       
   307 
       
   308 void CSignalChangeNotifier::StartL()
       
   309 	{
       
   310 	if (IsActive())
       
   311 		{
       
   312 		RDebug::Print(_L(": Signal Notifier Already active"));
       
   313 		return;
       
   314 		}
       
   315 	else if (iIsUsbPort)
       
   316 		{
       
   317 		RDebug::Print(_L(": Starting Signal Notifier for USB"));
       
   318 		TheUsbPort.NotifySignalChange(iStatus, iInSignals, KSignalDCEInputs | KSignalDCEOutputs | KSignalBreak);
       
   319 		}
       
   320 	else 
       
   321 		{
       
   322 		RDebug::Print(_L(": Starting Signal Notifier for serial port"));
       
   323 		TheSerialPort.NotifySignalChange(iStatus, iInSignals, KSignalDTEInputs);
       
   324 		}
       
   325 
       
   326 	SetActive();
       
   327 	}
       
   328 
       
   329 void CSignalChangeNotifier::RunL()
       
   330 	{
       
   331 	// if this object is for USB,
       
   332 	if (iIsUsbPort)
       
   333 		{
       
   334 #ifdef _DEBUG
       
   335 		RDebug::Print(_L(": Signal Change Notifier activated for USB - %x"), iInSignals);
       
   336 #endif
       
   337 		if ( iInSignals & KRTSChanged )
       
   338 			{
       
   339 			if ( iInSignals & KSignalRTS )
       
   340 				{
       
   341 #ifdef _DEBUG
       
   342 				RDebug::Print(_L(": Serial RTS on"));
       
   343 #endif
       
   344 				TheSerialPort.SetSignals( KSignalRTS, 0 );
       
   345 				//TheUsbPort.SetSignals( KSignalCTS, 0 );
       
   346 				}
       
   347 			else
       
   348 				{
       
   349 #ifdef _DEBUG
       
   350 				RDebug::Print(_L(": Serial RTS off"));
       
   351 #endif
       
   352 				TheSerialPort.SetSignals( 0, KSignalRTS );
       
   353 				//TheUsbPort.SetSignals( 0, KSignalCTS );
       
   354 				}
       
   355 			}
       
   356 	
       
   357 		if ( iInSignals & KDTRChanged )
       
   358 			{
       
   359 			if ( iInSignals & KSignalDTR )
       
   360 				{
       
   361 #ifdef _DEBUG
       
   362 				RDebug::Print(_L(": Serial DTR on"));
       
   363 #endif
       
   364 				TheSerialPort.SetSignals( KSignalDTR, 0 );
       
   365 				//TheUsbPort.SetSignals( KSignalDSR, 0 );
       
   366 				}
       
   367 			else
       
   368 				{
       
   369 #ifdef _DEBUG
       
   370 				RDebug::Print(_L(": Serial DTR off"));
       
   371 #endif
       
   372 				TheSerialPort.SetSignals( 0, KSignalDTR );
       
   373 				//TheUsbPort.SetSignals( 0, KSignalDSR );
       
   374 				}
       
   375 			}
       
   376 
       
   377 		}
       
   378 	else 
       
   379 		{
       
   380 #ifdef _DEBUG
       
   381 		RDebug::Print(_L(": Signal Change Notifier activated for Serial port - %x"), iInSignals);
       
   382 #endif
       
   383 		if ( iInSignals & KCTSChanged )
       
   384 			{
       
   385 			if ( iInSignals & KSignalCTS )
       
   386 				{
       
   387 #ifdef _DEBUG
       
   388 				RDebug::Print(_L(": USB CTS on"));
       
   389 #endif
       
   390 				TheUsbPort.SetSignals( KSignalCTS, 0 );
       
   391 				}
       
   392 			else
       
   393 				{
       
   394 #ifdef _DEBUG
       
   395 				RDebug::Print(_L(": USB CTS off"));
       
   396 #endif
       
   397 				TheUsbPort.SetSignals( 0, KSignalCTS );
       
   398 				}
       
   399 			}
       
   400 		if ( iInSignals & KDSRChanged )
       
   401 			{
       
   402 			if ( iInSignals & KSignalDSR )
       
   403 				{
       
   404 #ifdef _DEBUG
       
   405 				RDebug::Print(_L(": USB DSR on"));
       
   406 #endif
       
   407 				TheUsbPort.SetSignals( KSignalDSR, 0 );
       
   408 				}
       
   409 			else
       
   410 				{
       
   411 #ifdef _DEBUG
       
   412 				RDebug::Print(_L(": USB DSR off"));
       
   413 #endif
       
   414 				TheUsbPort.SetSignals( 0, KSignalDSR );
       
   415 				}
       
   416 			}
       
   417 
       
   418 		if ( iInSignals & KDCDChanged )
       
   419 			{
       
   420 				if ( iInSignals & KSignalDCD )
       
   421 				{
       
   422 #ifdef _DEBUG
       
   423 				RDebug::Print(_L(": USB DCD on"));
       
   424 #endif
       
   425 				TheUsbPort.SetSignals( KSignalDCD, 0 );
       
   426 				}
       
   427 			else
       
   428 				{
       
   429 #ifdef _DEBUG
       
   430 				RDebug::Print(_L(": USB DCD off"));
       
   431 #endif
       
   432 				TheUsbPort.SetSignals( 0, KSignalDCD );
       
   433 				}
       
   434 			}
       
   435 
       
   436 		if ( iInSignals & KRNGChanged )
       
   437 			{
       
   438 			if ( iInSignals & KSignalRNG )
       
   439 				{
       
   440 #ifdef _DEBUG
       
   441 				RDebug::Print(_L(": USB RNG on"));
       
   442 #endif
       
   443 				TheUsbPort.SetSignals( KSignalRNG, 0 );
       
   444 				}
       
   445 			else
       
   446 				{
       
   447 #ifdef _DEBUG
       
   448 				RDebug::Print(_L(": USB RNG off"));
       
   449 #endif
       
   450 				TheUsbPort.SetSignals( 0, KSignalRNG );
       
   451 				}
       
   452 			}
       
   453 		}
       
   454 	StartL();
       
   455 	}
       
   456 
       
   457 //
       
   458 //----- class CFControlChangeNotifier -----------------------------------------
       
   459 //
       
   460 CFControlChangeNotifier::CFControlChangeNotifier()
       
   461 	:CActive(CActive::EPriorityStandard)
       
   462 	{
       
   463 	}
       
   464 
       
   465 CFControlChangeNotifier* CFControlChangeNotifier::NewL()
       
   466 	{
       
   467 	CFControlChangeNotifier* self = new (ELeave) CFControlChangeNotifier();
       
   468 	CActiveScheduler::Add(self);				// Add to active scheduler
       
   469 	return self;
       
   470 	}
       
   471 	
       
   472 void CFControlChangeNotifier::DoCancel()
       
   473 	{
       
   474 #ifdef _DEBUG
       
   475 	RDebug::Print(_L(": CFControlChangeNotifier::DoCancel()"));
       
   476 #endif
       
   477 	TheUsbPort.NotifyFlowControlChangeCancel();
       
   478 	}
       
   479 
       
   480 void CFControlChangeNotifier::StartL()
       
   481 	{
       
   482 	if (IsActive())
       
   483 		{
       
   484 		RDebug::Print(_L(": FlowControl Notifier Already active"));
       
   485 		return;
       
   486 		}
       
   487 	TheUsbPort.NotifyFlowControlChange(iStatus, iFlowControl);
       
   488 	SetActive();
       
   489 	}
       
   490 
       
   491 void CFControlChangeNotifier::RunL()
       
   492 	{
       
   493 #ifdef _DEBUG
       
   494 	RDebug::Print(_L(": FlowControl Notifier activated"));
       
   495 #endif
       
   496 	StartL();
       
   497 	}
       
   498 
       
   499 //
       
   500 // --- class CActiveConsole -------------------------------------------------
       
   501 //
       
   502 
       
   503 CActiveConsole::CActiveConsole()
       
   504 	: CActive(CActive::EPriorityStandard),
       
   505 	iIsRunning(EFalse)
       
   506 	{}
       
   507 
       
   508 
       
   509 CActiveConsole* CActiveConsole::NewLC()
       
   510 	{
       
   511 	CActiveConsole* self = new (ELeave) CActiveConsole();
       
   512 	self->ConstructL();
       
   513 	return self;
       
   514 	}
       
   515 
       
   516 
       
   517 CActiveConsole* CActiveConsole::NewL()
       
   518 	{
       
   519 	CActiveConsole* self = NewLC();
       
   520 	return self;
       
   521 	}
       
   522 
       
   523 CActiveConsole::~CActiveConsole()
       
   524 	{
       
   525 	delete iUsbToSerial;
       
   526 	delete iSerialToUsb;
       
   527 	}
       
   528 
       
   529 void CActiveConsole::DoCancel()
       
   530 	{
       
   531 #ifdef _DEBUG
       
   532 	RDebug::Print(_L(": CActiveConsole::DoCancel()"));
       
   533 #endif
       
   534 	iUsbConfigChangeNotifier->Cancel();
       
   535 	iSerialSignalChangeNotifier->Cancel();
       
   536 	iUsbSignalChangeNotifier->Cancel();
       
   537 	//iUsbFControlNotifier->Cancel();
       
   538 	
       
   539 	iUsbToSerial->Cancel();
       
   540 	iSerialToUsb->Cancel();
       
   541 	
       
   542 	iIsRunning = EFalse;
       
   543 	}
       
   544 
       
   545 void CActiveConsole::RunL()
       
   546 	{
       
   547 #ifdef _DEBUG
       
   548 	RDebug::Print(_L(": CActiveConsole::RunL()"));
       
   549 #endif
       
   550 	ProcessKeyPressL();
       
   551 	WaitForKeyPress();
       
   552 	}
       
   553 
       
   554 void CActiveConsole::ConstructL()
       
   555 	{
       
   556 #ifdef _DEBUG
       
   557 	RDebug::Print(_L(": CActiveConsole::ConstructL()"));
       
   558 #endif
       
   559 	CActiveScheduler::Add(this);				// Add to active scheduler
       
   560 
       
   561 	// Create 2 reader writer AOs/ threads
       
   562 	// 1 for reading from USb and writing to serial
       
   563 	iUsbToSerial = CDuplex::NewL();
       
   564 	iUsbToSerial->SetRxPort(&TheUsbPort);
       
   565 	iUsbToSerial->SetTxPort(&TheSerialPort);
       
   566 	
       
   567 	// 1 for reading from Serial and writing to Usb
       
   568 	iSerialToUsb = CDuplex::NewL();
       
   569 	iSerialToUsb->SetRxPort(&TheSerialPort);
       
   570 	iSerialToUsb->SetTxPort(&TheUsbPort);
       
   571 
       
   572 	iSerialSignalChangeNotifier = CSignalChangeNotifier::NewL(EFalse);
       
   573 	iUsbSignalChangeNotifier = CSignalChangeNotifier::NewL(ETrue);
       
   574 //	iUsbFControlNotifier = CFControlChangeNotifier::NewL();
       
   575 	iUsbConfigChangeNotifier = CConfigChangeNotifier::NewL();
       
   576 	}
       
   577 
       
   578 void CActiveConsole::Start()
       
   579 	{
       
   580 #ifdef _DEBUG
       
   581 	RDebug::Print(_L(": CActiveConsole::Start()"));
       
   582 #endif
       
   583 	if (iIsRunning)
       
   584 		{
       
   585 		TheWindow.Write(_L(": Invalid Key pressed, already running\n"));
       
   586 		return;
       
   587 		}
       
   588 	iIsRunning = ETrue;
       
   589 	TInt ignoreErr;
       
   590 	TRAP(ignoreErr, iUsbConfigChangeNotifier->StartL());
       
   591 	TRAP(ignoreErr, iSerialSignalChangeNotifier->StartL());
       
   592 	TRAP(ignoreErr, iUsbSignalChangeNotifier->StartL());
       
   593 //	iUsbFControlNotifier->StartL();
       
   594 	
       
   595 	TRAP(ignoreErr, iUsbToSerial->StartL());
       
   596 	TRAP(ignoreErr, iSerialToUsb->StartL());
       
   597 #ifdef _DEBUG
       
   598 	RDebug::Print(_L(": CActiveConsole::Start() done"));
       
   599 #endif
       
   600 	}
       
   601 
       
   602 void CActiveConsole::WaitForKeyPress()
       
   603 	{
       
   604 	if (IsActive())
       
   605 		{
       
   606 		RDebug::Print(_L(": ActiveConsole -- Already Running\n"));
       
   607 		return;
       
   608 		}
       
   609 		
       
   610 	TheWindow.Write(_L("Press Escape to STOP\n"));
       
   611 	TheWindow.Write(_L("Press Enter to EXIT\n"));
       
   612 	if (!iIsRunning)
       
   613 		{
       
   614 		TheWindow.Write(_L("Press Spacebar to START test\n"));
       
   615 		}
       
   616 	else
       
   617 		{
       
   618 		TheWindow.Write(_L("1 DTR on	A DTR off\n"));
       
   619 		TheWindow.Write(_L("2 RTS on	B RTS off\n"));
       
   620 		TheWindow.Write(_L("3 DSR on	C DSR off\n"));
       
   621 		TheWindow.Write(_L("4 CTS on	D CTS off\n"));
       
   622 		TheWindow.Write(_L("5 DCD on	E DCD off\n"));
       
   623 		TheWindow.Write(_L("6 RNG on	F RNG off\n"));
       
   624 		TheWindow.Write(_L("7 BRK on	G BRK off\n"));
       
   625 		}
       
   626 	TheWindow.Write(_L("\n"));
       
   627 	TheWindow.Write(_L("\n"));
       
   628 	TheWindow.Read(iKeypress, iStatus);
       
   629 	SetActive();
       
   630 	}
       
   631 
       
   632 void CActiveConsole::ProcessKeyPressL()
       
   633 	{
       
   634 #ifdef _DEBUG
       
   635 	RDebug::Print(_L(": CActiveConsole::ProcessKeyPressL()"));
       
   636 #endif
       
   637 	switch (iKeypress.Code())
       
   638 		{
       
   639 		case EKeyEscape:
       
   640 			RDebug::Print(_L(": Stopping CactiveConsole"));
       
   641 			DoCancel();
       
   642 			break;
       
   643 		case EKeySpace:
       
   644 			RDebug::Print(_L(": Starting"));
       
   645 			Start();
       
   646 			break;
       
   647 		case EKeyEnter:
       
   648 			RDebug::Print(_L(": Exiting"));
       
   649 			DoCancel();
       
   650 
       
   651 			RDebug::Print(_L(": Closing ports and servers"));
       
   652 	
       
   653 			TheUsbPort.Close();
       
   654 			TheSerialPort.Close();
       
   655 			TheUsbServer.Close();
       
   656 			TheCommServer.Close();
       
   657 
       
   658 			CActiveScheduler::Stop();
       
   659 			break;
       
   660 		case '1':
       
   661 			TheUsbPort.SetSignals ( KSignalDTR, 0 );
       
   662 			break;
       
   663 		case '2':
       
   664 			TheUsbPort.SetSignals ( KSignalRTS, 0 );
       
   665 			break;
       
   666 		case '3':
       
   667 			TheUsbPort.SetSignals ( KSignalDSR, 0 );
       
   668 			break;
       
   669 		case '4':
       
   670 			TheUsbPort.SetSignals ( KSignalCTS, 0 );
       
   671 			break;
       
   672 		case '5':
       
   673 			TheUsbPort.SetSignals ( KSignalDCD, 0 );
       
   674 			break;
       
   675 		case '6':
       
   676 			TheUsbPort.SetSignals ( KSignalRNG, 0 );
       
   677 			break;
       
   678 		case '7':
       
   679 			TheUsbPort.Break ( iBreakRequest, 1000 );
       
   680 			break;
       
   681 		case 'A':
       
   682 			TheUsbPort.SetSignals ( 0, KSignalDTR );
       
   683 			break;
       
   684 		case 'B':
       
   685 			TheUsbPort.SetSignals ( 0, KSignalRTS );
       
   686 			break;
       
   687 		case 'C':
       
   688 			TheUsbPort.SetSignals ( 0, KSignalDSR );
       
   689 			break;
       
   690 		case 'D':
       
   691 			TheUsbPort.SetSignals ( 0, KSignalCTS );
       
   692 			break;
       
   693 		case 'E':
       
   694 			TheUsbPort.SetSignals ( 0, KSignalDCD );
       
   695 			break;
       
   696 		case 'F':
       
   697 			TheUsbPort.SetSignals ( 0, KSignalRNG );
       
   698 			break;
       
   699 		default:
       
   700 			TheWindow.Write(_L(": Invalid Key pressed\n"));
       
   701 		}
       
   702 	}
       
   703 
       
   704 /*
       
   705 LOCAL_C TInt RateToInt(TBps aRate)
       
   706 	{
       
   707 	switch (aRate)
       
   708 		{
       
   709 	case EBps115200:	return 115200;
       
   710     case EBps57600:	return 57600;
       
   711     case EBps38400:	return 38400;
       
   712     case EBps19200:	return 19200;
       
   713     case EBps9600:	return 9600;
       
   714 	case EBps7200:	return 7200;
       
   715     case EBps4800:	return 4800;
       
   716 	case EBps3600:	return 3600;
       
   717     case EBps2400:	return 2400;
       
   718 	case EBps2000:	return 2000;
       
   719 	case EBps1800:	return 1800;
       
   720     case EBps1200:	return 1200;
       
   721     case EBps600:	return 600;
       
   722     case EBps300:	return 300;
       
   723     case EBps150:	return 150;
       
   724 	case EBps134:	return 134;
       
   725     case EBps110:	return 110;
       
   726 	case EBps75:	return 75;
       
   727 	case EBps50:	return 50;
       
   728 	default:	return -1;
       
   729 		}
       
   730 	}
       
   731 
       
   732 LOCAL_C void ConfigString(TDes &aBuf, const TCommNotificationV01 &aConfig)
       
   733 	{
       
   734 	// Config
       
   735 	aBuf.Format(_L(" %d "), RateToInt(aConfig.iRate));
       
   736 	switch (aConfig.iParity)
       
   737 		{
       
   738 	case EParityEven: aBuf.Append(_L("E")); break;
       
   739 	case EParityOdd: aBuf.Append(_L("O")); break;
       
   740 	case EParityNone: aBuf.Append(_L("N")); break;
       
   741     default: break;
       
   742 		}
       
   743 	switch (aConfig.iDataBits)
       
   744 		{
       
   745 	case EData5: aBuf.Append(_L("5")); break;
       
   746 	case EData6: aBuf.Append(_L("6")); break;
       
   747 	case EData7: aBuf.Append(_L("7")); break;
       
   748 	case EData8: aBuf.Append(_L("8")); break;
       
   749     default: break;
       
   750 		}
       
   751 	if (aConfig.iStopBits==EStop1)
       
   752 		aBuf.Append(_L("1 "));
       
   753 	else
       
   754 		aBuf.Append(_L("2 "));
       
   755 
       
   756 	aBuf.Append(_L("Use:"));
       
   757 	if (aConfig.iHandshake==0)
       
   758 		aBuf.Append(_L("NoControl "));
       
   759 	if (aConfig.iHandshake&(KConfigObeyXoff|KConfigSendXoff))
       
   760 		aBuf.Append(_L("XonXoff "));
       
   761 	if (aConfig.iHandshake&KConfigObeyCTS)
       
   762 		aBuf.Append(_L("CTS/RTS "));
       
   763 	if (aConfig.iHandshake&KConfigObeyDSR)
       
   764 		aBuf.Append(_L("DSR/DTR "));
       
   765 	if (aConfig.iHandshake&KConfigWriteBufferedComplete)
       
   766 		aBuf.Append(_L("Early "));
       
   767 	//|KConfigObeyDCD|KConfigFailDCD|))
       
   768 
       
   769 //	if (aConfig.iBreak==TEiger::EBreakOn)
       
   770 //		aBuf.Append(_L("Brk "));
       
   771 //	if (aConfig.iFifo==EFifoEnable)
       
   772 //		aBuf.Append(_L("Fifo "));
       
   773 	}
       
   774 	
       
   775 LOCAL_C void PrintCaps()
       
   776 	{
       
   777 	TCommCaps2 caps;
       
   778 	TheUsbPort.Caps(caps);
       
   779 	TUint notifycaps = caps().iNotificationCaps;
       
   780 	RDebug::Print(_L("Capabilities:\n"));
       
   781 	if (notifycaps&KNotifySignalsChangeSupported)
       
   782 		RDebug::Print(_L("Notify Signals Change supported\n"));
       
   783 	if (notifycaps&KNotifyRateChangeSupported)
       
   784 		RDebug::Print(_L("Notify Rate Change supported\n"));
       
   785 	if (notifycaps&KNotifyDataFormatChangeSupported)
       
   786 		RDebug::Print(_L("Notify Data Format Change supported\n"));
       
   787 	if (notifycaps&KNotifyHandshakeChangeSupported)
       
   788 		RDebug::Print(_L("Notify Handshake Change supported\n"));
       
   789 	if (notifycaps&KNotifyBreakSupported)
       
   790 		RDebug::Print(_L("Notify Break supported\n"));
       
   791 	if (notifycaps&KNotifyFlowControlChangeSupported)
       
   792 		RDebug::Print(_L("Notify Flow Control Change supported\n"));
       
   793 	if (notifycaps&KNotifyDataAvailableSupported)
       
   794 		RDebug::Print(_L("Notify Data Available supported\n"));
       
   795 	if (notifycaps&KNotifyOutputEmptySupported)
       
   796 		RDebug::Print(_L("Notify Output Empty supported\n"));
       
   797 	RDebug::Print(_L("\n"));
       
   798 	if ((caps().iRoleCaps)&KCapsRoleSwitchSupported)
       
   799 		RDebug::Print(_L("Role switching is supported\n"));
       
   800 	RDebug::Print(_L("\n"));
       
   801 	if ((caps().iFlowControlCaps)&KCapsFlowControlStatusSupported)
       
   802 		RDebug::Print(_L("Retrieve flow control status is supported\n"));
       
   803 	}
       
   804 */
       
   805 LOCAL_C void DoInitL()
       
   806 	{
       
   807 	// Do the necessary initialisation of the C32, 2 comm ports, USB stuff.
       
   808 	// Load Device Drivers
       
   809 	TInt r;
       
   810 
       
   811 	RDebug::Print(_L("E32Main: Load USB LDD \n"));
       
   812 	r=User::LoadLogicalDevice(USBLDD_NAME);
       
   813 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   814 		{
       
   815 		RDebug::Print(_L("E32Main: Failed 0x%X\n\r"),r);
       
   816 		User::Panic(_L("T_TERM"), ELoadLogicalDeviceErr);
       
   817 		}
       
   818 
       
   819 	RDebug::Print(_L("E32Main: Load Serial PDD \n "));
       
   820 	r=User::LoadPhysicalDevice(SERIALPDD_NAME);
       
   821 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   822 		{
       
   823 		RDebug::Print(_L("E32Main: Failed 0x%X\n\r"),r);
       
   824 		User::Panic(_L("T_TERM"), ELoadLogicalDeviceErr);
       
   825 		}
       
   826 
       
   827 	RDebug::Print(_L("E32Main: Load SERIAL LDD \n"));
       
   828 	r=User::LoadLogicalDevice(SERIALLDD_NAME);
       
   829 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   830 		{
       
   831 		RDebug::Print(_L("E32Main: Failed 0x%X\n\r"),r);
       
   832 		User::Panic(_L("T_TERM"), ELoadLogicalDeviceErr);
       
   833 		}
       
   834 	RDebug::Print(_L("E32Main: Loaded Device Drivers\n"));
       
   835 	
       
   836 	r = StartC32();
       
   837 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   838 		{
       
   839 		RDebug::Print(_L("E32Main: Failed to start C32. Error = %d"), r);
       
   840 		User::Panic(_L("T_TERM"), EOpenErr);
       
   841 		}
       
   842 	RDebug::Print(_L("E32Main: Started c32"));
       
   843 
       
   844 	r = TheUsbServer.Connect();
       
   845 	if (r!=KErrNone)
       
   846 		{
       
   847 		RDebug::Print(_L("E32Main: Failed to connect to UsbMan Server. Error = %d"), r);
       
   848 		User::Panic(_L("T_TERM"), EOpenErr);
       
   849 		}
       
   850 	RDebug::Print(_L("E32Main: Connected to UsbMan Server"));
       
   851 
       
   852 	TRequestStatus startStatus;
       
   853 	TheUsbServer.Start(startStatus);
       
   854 	User::WaitForRequest(startStatus);
       
   855 	if (startStatus != KErrNone && startStatus != KErrAlreadyExists)
       
   856 		{
       
   857 		RDebug::Print(_L("E32Main: Failed to Start USB services. Error = %d"), r);
       
   858 		User::Panic(_L("T_TERM"), EOpenErr);
       
   859 		}
       
   860 	RDebug::Print(_L("E32Main: Started USB services"));
       
   861 
       
   862 	// Comms Config
       
   863 	r = TheCommServer.Connect();
       
   864 	if (r!=KErrNone)
       
   865 		{
       
   866 		RDebug::Print(_L("E32Main: Failed to Connect to C32. Error = %d"), r);
       
   867 		User::Panic(_L("T_TERM"), EOpenErr);
       
   868 		}
       
   869 	RDebug::Print(_L("E32Main: Connected to C32 Server"));
       
   870 
       
   871 	r = TheCommServer.LoadCommModule(USBCSY_NAME);
       
   872 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   873 		{
       
   874 		RDebug::Print(_L("E32Main: Failed to load USB CSY. Error = %d"), r);
       
   875 		User::Panic(_L("T_TERM"), EOpenErr);
       
   876 		}
       
   877 	RDebug::Print(_L("E32Main: Loaded USB CSY"));
       
   878 
       
   879 	r = TheCommServer.LoadCommModule(SERIALCSY_NAME);
       
   880 	if (r!=KErrNone && r!=KErrAlreadyExists)
       
   881 		{
       
   882 		RDebug::Print(_L("E32Main: Failed to load serial CSY. Error = %d"), r);
       
   883 		User::Panic(_L("T_TERM"), EOpenErr);
       
   884 		}
       
   885 	RDebug::Print(_L("E32Main: Loaded Serial CSY"));
       
   886 
       
   887 	r = TheUsbPort.Open(TheCommServer, USBPORT_NAME,ECommExclusive,ECommRoleDCE); // Comm port
       
   888 	if (r!=KErrNone)
       
   889 		{
       
   890 		RDebug::Print(_L("E32Main: Failed to Open USB Comm Port. Error = %d"), r);
       
   891 		User::Panic(_L("T_TERM"), EOpenErr);
       
   892 		}
       
   893 	RDebug::Print(_L("E32Main: Opened USB Comm Port"));
       
   894 
       
   895 
       
   896 	r = TheSerialPort.Open(TheCommServer, SERIALPORT_NAME,ECommExclusive,ECommRoleDTE); // Comm port
       
   897 	if (r!=KErrNone)
       
   898 		{
       
   899 		RDebug::Print(_L("E32Main: Failed to Open Serial Comm Port. Error = %d"), r);
       
   900 		User::Panic(_L("T_TERM"), EOpenErr);
       
   901 		}
       
   902 	RDebug::Print(_L("E32Main: Opened Serial Comm Port"));
       
   903 
       
   904 	// Print the caps
       
   905 	//PrintCaps();
       
   906 
       
   907 	RDebug::Print(_L("E32Main: Reading Serial Comm Port config"));
       
   908 	TheSerialPort.Config(TheSerialConfigBuf);	// get config
       
   909 	RDebug::Print(_L("E32Main: Reading Usb Comm Port config"));
       
   910 	TheUsbPort.Config(TheUsbConfigBuf);	// get config
       
   911 
       
   912 	TBuf<80> buf;
       
   913 	buf.FillZ();
       
   914 	//ConfigString(buf, TheUsbConfig);
       
   915 	RDebug::Print(_L("E32Main: Old USB Port Settings"));
       
   916 	RDebug::Print(buf);
       
   917 	RDebug::Print(_L(""));
       
   918 	buf.FillZ();
       
   919 	//ConfigString(buf, TheSerialConfig);
       
   920 	RDebug::Print(_L("E32Main: Old Serial Port Settings"));
       
   921 	RDebug::Print(buf);
       
   922 	RDebug::Print(_L(""));
       
   923 
       
   924 	TCommConfig serialConfig;
       
   925 	TheSerialPort.Config(serialConfig);	// get config
       
   926 	serialConfig().iHandshake = 0;
       
   927 	serialConfig().iRate = EBps1152000;
       
   928 	TheSerialPort.SetConfig(serialConfig);
       
   929 
       
   930 	RDebug::Print(_L(""));	}
       
   931 
       
   932 /// Main function
       
   933 //
       
   934 GLDEF_C TInt E32Main()
       
   935 	{
       
   936 	RDebug::Print(_L("E32Main: Starting!"));
       
   937 	// Construct the active scheduler
       
   938 	CActiveScheduler* myScheduler = new (ELeave) CActiveScheduler();
       
   939 
       
   940 	// Install as the active scheduler
       
   941 	CActiveScheduler::Install(myScheduler);
       
   942 
       
   943 	// Create objects and console handler
       
   944 	// Open the window asap
       
   945 	TheWindow.Init(_L("TERM"),TSize(KConsFullScreen,KConsFullScreen));
       
   946 
       
   947 	TheWindow.Control(_L("+Maximize +Newline"));
       
   948 	RDebug::Print(_L("E32Main: Initialised Window!"));
       
   949 
       
   950 	DoInitL();
       
   951 	
       
   952 	// Does the construction, 
       
   953 	CActiveConsole* myActiveConsole = CActiveConsole::NewL();
       
   954 
       
   955     // Call request function to start the test
       
   956 	myActiveConsole->WaitForKeyPress();	
       
   957 	// Start active scheduler
       
   958 	CActiveScheduler::Start();
       
   959 
       
   960 	// Suspend thread for 2 secs
       
   961 	User::After(2000000);
       
   962 
       
   963 	delete myActiveConsole;
       
   964 	return KErrNone;
       
   965 	}
       
   966