usbmgmt/usbmgrtest/t_usb_charging/src/activeconsole.cpp
changeset 49 93c0009bd947
equal deleted inserted replaced
48:21625e5de155 49:93c0009bd947
       
     1 /*
       
     2 * Copyright (c) 2006-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 *
       
    16 */
       
    17 
       
    18 #include <e32twin.h>
       
    19 #include <e32svr.h>
       
    20 #include "activeconsole.h"
       
    21 #include "testbase.h"
       
    22 #include "tests.h"
       
    23 #include "OstTraceDefinitions.h"
       
    24 #ifdef OST_TRACE_COMPILER_IN_USE
       
    25 #include "activeconsoleTraces.h"
       
    26 #endif
       
    27 
       
    28 
       
    29 CActiveConsole::CActiveConsole(CConsoleBase& aConsole)
       
    30  :	CActive(CActive::EPriorityStandard),
       
    31 	iConsole(aConsole)
       
    32 	{
       
    33 	OstTraceFunctionEntry0( CACTIVECONSOLE_CACTIVECONSOLE_ENTRY );
       
    34 	CActiveScheduler::Add(this);
       
    35 	OstTraceFunctionExit0( CACTIVECONSOLE_CACTIVECONSOLE_EXIT );
       
    36 	}
       
    37 
       
    38 CActiveConsole* CActiveConsole::NewLC(CConsoleBase& aConsole)
       
    39 	{
       
    40 	OstTraceFunctionEntry0( CACTIVECONSOLE_NEWLC_ENTRY );
       
    41 
       
    42 	CActiveConsole* self = new(ELeave) CActiveConsole(aConsole);
       
    43 	CleanupStack::PushL(self);
       
    44 	self->ConstructL();
       
    45 #ifdef COMMANDLINE_PARAM	
       
    46 	self->DisplayMainMenu();
       
    47 #endif	
       
    48 	OstTraceFunctionExit0( CACTIVECONSOLE_NEWLC_EXIT );
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 void CActiveConsole::ConstructL()
       
    53 	{
       
    54 	OstTraceFunctionEntry0( CACTIVECONSOLE_CONSTRUCTL_ENTRY );
       
    55 	// Launch of FDTest test.
       
    56 	iTest = gTestDefinitions[0].iFactoryL(*this);
       
    57 	OstTraceFunctionExit0( CACTIVECONSOLE_CONSTRUCTL_EXIT );
       
    58 	}
       
    59 
       
    60 CActiveConsole::~CActiveConsole()
       
    61 	{
       
    62 	OstTraceFunctionEntry0( DUP1_CACTIVECONSOLE_CACTIVECONSOLE_ENTRY );
       
    63 	Cancel();
       
    64 
       
    65 	StopCurrentTest();
       
    66 	OstTraceFunctionExit0( DUP1_CACTIVECONSOLE_CACTIVECONSOLE_EXIT );
       
    67 	}
       
    68 
       
    69 void CActiveConsole::DoCancel()
       
    70 	{
       
    71 	OstTraceFunctionEntry0( CACTIVECONSOLE_DOCANCEL_ENTRY );
       
    72 	iConsole.ReadCancel();
       
    73 	OstTraceFunctionExit0( CACTIVECONSOLE_DOCANCEL_EXIT );
       
    74 	}
       
    75 
       
    76 void CActiveConsole::StopCurrentTest()
       
    77 	{
       
    78 	OstTraceFunctionEntry0( CACTIVECONSOLE_STOPCURRENTTEST_ENTRY );
       
    79 	delete iTest;
       
    80 	iTest = NULL;
       
    81 	OstTraceFunctionExit0( CACTIVECONSOLE_STOPCURRENTTEST_EXIT );
       
    82 	}
       
    83 
       
    84 void CActiveConsole::RunL()
       
    85 // Only process when we get a return, otherwise cache- i.e. support multi-character selections
       
    86 	{
       
    87 	OstTraceFunctionEntry0( CACTIVECONSOLE_RUNL_ENTRY );
       
    88 	DoActionKeyL(iConsole.KeyCode());
       
    89 
       
    90 	// Repost asynchronous request.
       
    91 	RequestCharacter();
       
    92 	OstTraceFunctionExit0( CACTIVECONSOLE_RUNL_EXIT );
       
    93 	}
       
    94 
       
    95 void CActiveConsole::DoActionKeyL(TKeyCode aKeyCode)
       
    96 	{
       
    97 	OstTraceFunctionEntry0( CACTIVECONSOLE_DOACTIONKEYL_ENTRY );
       
    98 	WriteNoReturn(_L8("%c"), aKeyCode);
       
    99 
       
   100 	// Examine the key that just came in.
       
   101 	switch ( TUint(aKeyCode) )
       
   102 		{
       
   103 	case EKeyEscape:
       
   104 		{
       
   105 		Write(_L8("Exiting"));
       
   106 		CActiveScheduler::Stop();
       
   107 		OstTraceFunctionExit0( CACTIVECONSOLE_DOACTIONKEYL_EXIT );
       
   108 		return;
       
   109 		}
       
   110 
       
   111 	case EKeyEnter:
       
   112 		// Tell the test about what's in the buffer so far, if anything.
       
   113 		Write(_L8("You entered \'%S\'"), &iInputBuffer);
       
   114 		switch ( iInputBuffer.Length() )
       
   115 			{
       
   116 		case 0:
       
   117 			// Don't pass anything on- nothing to pass on.
       
   118 			break;
       
   119 
       
   120 		case 1:
       
   121 			if ( 	iInputBuffer == _L8("S") 
       
   122 				||	iInputBuffer == _L8("s") 
       
   123 				)
       
   124 				{
       
   125 				StopCurrentTest();
       
   126 				}
       
   127 			else
       
   128 				{
       
   129 				// Tell the test via the old 'single character' interface.
       
   130 				// If there is a test, then let it process the key. If there isn't a 
       
   131 				// test, we process it to (possibly) create and run a new test object.
       
   132 				if ( iTest )
       
   133 					{
       
   134 					TRAPD(err, iTest->ProcessKeyL((TKeyCode)iInputBuffer[0]));
       
   135 					if ( err )
       
   136 						{
       
   137 						Write(_L8("CTestBase::ProcessKeyL left with %d"), err);
       
   138 						StopCurrentTest();
       
   139 						}
       
   140 					}
       
   141 				else
       
   142 					{
       
   143 					SelectTestL();
       
   144 					}
       
   145 				}
       
   146 			iInputBuffer = KNullDesC8();
       
   147 			break;
       
   148 		
       
   149 		default:
       
   150 			// Tell the test via the new 'multi character' interface.
       
   151 			// If there is a test, then let it process the key. If there isn't a 
       
   152 			// test, we process it to (possibly) create and run a new test object.
       
   153 			if ( iTest )
       
   154 				{
       
   155 				TRAPD(err, iTest->ProcessKeyL(iInputBuffer));
       
   156 				if ( err )
       
   157 					{
       
   158 					Write(_L8("CTestBase::ProcessKeyL left with %d"), err);
       
   159 					StopCurrentTest();
       
   160 					}
       
   161 				}
       
   162 			else
       
   163 				{
       
   164 				SelectTestL();
       
   165 				}
       
   166 			iInputBuffer = KNullDesC8();
       
   167 			break;
       
   168 			}
       
   169 		DisplayMainMenu();
       
   170 		break;
       
   171 
       
   172 	default:
       
   173 		iInputBuffer.Append(aKeyCode);
       
   174 		break;
       
   175 		}
       
   176 	OstTraceFunctionExit0( DUP1_CACTIVECONSOLE_DOACTIONKEYL_EXIT );
       
   177 	}
       
   178 
       
   179 void CActiveConsole::RequestCharacter()
       
   180 	{
       
   181 	OstTraceFunctionEntry0( CACTIVECONSOLE_REQUESTCHARACTER_ENTRY );
       
   182 	iConsole.Read(iStatus);
       
   183 	SetActive();
       
   184 	OstTraceFunctionExit0( CACTIVECONSOLE_REQUESTCHARACTER_EXIT );
       
   185 	}
       
   186 
       
   187 void CActiveConsole::DisplayMainMenu()
       
   188 	{
       
   189 	OstTraceFunctionEntry0( CACTIVECONSOLE_DISPLAYMAINMENU_ENTRY );
       
   190 	Write(KNullDesC8);
       
   191 
       
   192 	// If there's a current test, display its step menu. Otherwise, display 
       
   193 	// all the available tests.
       
   194 	if ( iTest )
       
   195 		{
       
   196 		iTest->DisplayTestSpecificMenu();
       
   197 		Write(_L8("s - stop and close current test"));
       
   198 		}
       
   199 	else
       
   200 		{
       
   201 		const TUint numberOfTests = sizeof(gTestDefinitions) / sizeof(TTestDefinition);
       
   202 		for ( TUint ii = 0 ; ii < numberOfTests ; ii ++ )
       
   203 			{
       
   204 			Write(_L8("%d - %S"), ii, &gTestDefinitions[ii].iDescription);
       
   205 			}
       
   206 		}
       
   207 
       
   208 	Write(_L8("Escape - exit program"));
       
   209 	Write(KNullDesC8);
       
   210  	OstTraceFunctionExit0( CACTIVECONSOLE_DISPLAYMAINMENU_EXIT );
       
   211  	}
       
   212 
       
   213 void CActiveConsole::Write(TRefByValue<const TDesC8> aFmt, ...)
       
   214 	{
       
   215 	OstTraceFunctionEntry0( CACTIVECONSOLE_WRITE_ENTRY );
       
   216 	VA_LIST list;
       
   217 	VA_START(list, aFmt);
       
   218 
       
   219 	TBuf8<0x100> buf;
       
   220 	buf.AppendFormatList(aFmt, list);
       
   221 	TBuf<0x100> wideBuf;
       
   222 	wideBuf.Copy(buf);
       
   223 	iConsole.Write(wideBuf);
       
   224 	iConsole.Write(_L("\n"));
       
   225 
       
   226 	RDebug::Print(wideBuf);
       
   227 	OstTraceFunctionExit0( CACTIVECONSOLE_WRITE_EXIT );
       
   228 	}
       
   229 
       
   230 void CActiveConsole::WriteNoReturn(TRefByValue<const TDesC8> aFmt, ...)
       
   231 	{
       
   232 	OstTraceFunctionEntry0( CACTIVECONSOLE_WRITENORETURN_ENTRY );
       
   233 	VA_LIST list;
       
   234 	VA_START(list, aFmt);
       
   235 
       
   236 	TBuf8<0x100> buf;
       
   237 	buf.AppendFormatList(aFmt, list);
       
   238 	TBuf<0x100> wideBuf;
       
   239 	wideBuf.Copy(buf);
       
   240 	iConsole.Write(wideBuf);
       
   241 
       
   242 	RDebug::Print(wideBuf);
       
   243 	OstTraceFunctionExit0( CACTIVECONSOLE_WRITENORETURN_EXIT );
       
   244 	}
       
   245 
       
   246 TKeyCode CActiveConsole::Getch()
       
   247 	{
       
   248 	OstTraceFunctionEntry0( CACTIVECONSOLE_GETCH_ENTRY );
       
   249 	return iConsole.Getch();
       
   250 	}
       
   251 
       
   252 void CActiveConsole::SelectTestL()
       
   253 	{
       
   254 	OstTraceFunctionEntry0( CACTIVECONSOLE_SELECTTESTL_ENTRY );
       
   255 	StopCurrentTest();
       
   256 
       
   257 	// Pick a test out of the global array of tests.
       
   258 	const TUint numberOfTests = sizeof(gTestDefinitions) / sizeof (TTestDefinition);
       
   259 	TLex8 lex(iInputBuffer);
       
   260 	TUint index;
       
   261 	TInt err = lex.Val(index);
       
   262 
       
   263 	if (	err == KErrNone
       
   264 		&&	index < numberOfTests
       
   265 		)
       
   266 		{
       
   267 		iTest = gTestDefinitions[index].iFactoryL(*this);
       
   268 		}
       
   269 	else
       
   270 		{
       
   271 		Write(_L8("Unknown selection"));
       
   272 		}
       
   273 	OstTraceFunctionExit0( CACTIVECONSOLE_SELECTTESTL_EXIT );
       
   274 	}
       
   275 
       
   276 void CActiveConsole::TestFinished()
       
   277 /**
       
   278  * Called by the test when it has finished. Results in the destruction of the 
       
   279  * test.
       
   280  */
       
   281 	{
       
   282 	OstTraceFunctionEntry0( CACTIVECONSOLE_TESTFINISHED_ENTRY );
       
   283 	StopCurrentTest();
       
   284 	OstTraceFunctionExit0( CACTIVECONSOLE_TESTFINISHED_EXIT );
       
   285 	}
       
   286 
       
   287 TInt CActiveConsole::RunError(TInt aError)
       
   288 /**
       
   289  * Called by the Active Scheduler when a RunL in this active object leaves.
       
   290  */
       
   291 	{
       
   292 	OstTraceFunctionEntry0( CACTIVECONSOLE_RUNERROR_ENTRY );
       
   293 	// This actually happens when a test object fails to construct properly.
       
   294 	Write(_L8("Error creating test object: %d"), aError);
       
   295 
       
   296 	iInputBuffer = KNullDesC8();
       
   297 	DisplayMainMenu();
       
   298 
       
   299 	// It's OK to carry on with the program itself, so repost asynchronous 
       
   300 	// request.
       
   301 	RequestCharacter();
       
   302 
       
   303 	OstTraceFunctionExit0( CACTIVECONSOLE_RUNERROR_EXIT );
       
   304 	return KErrNone;
       
   305 	}
       
   306 
       
   307 void CActiveConsole::GetNumberL(TUint& aNumber)
       
   308 	{
       
   309 	OstTraceFunctionEntry0( CACTIVECONSOLE_GETNUMBERL_ENTRY );
       
   310 	TBuf<12> addrAsText;
       
   311 	addrAsText.Zero();
       
   312 	if ( aNumber != 0 )
       
   313 		{
       
   314 		addrAsText.Format(_L("%d"), aNumber);
       
   315 		}
       
   316 	WriteNoReturn(_L8("Enter a number: "));
       
   317 	if ( addrAsText.Length() > 0 )
       
   318 		{
       
   319 		TBuf8<100> narrowBuf;
       
   320 		narrowBuf.Copy(addrAsText);
       
   321 		WriteNoReturn(narrowBuf);
       
   322 		}
       
   323 	TKeyCode code;
       
   324 	TBuf<1> character;
       
   325 	FOREVER
       
   326 		{
       
   327 		code = Getch();
       
   328 		character.SetLength(0);
       
   329 		character.Append(code);
       
   330 	
       
   331 		// If <CR> finish editing string
       
   332 		if (code == 0x0d)
       
   333 			break;
       
   334 		
       
   335 		// if <BS> remove last character
       
   336 		if ((code == 0x08)&&(addrAsText.Length() != 0))
       
   337 			{
       
   338 			WriteNoReturn(_L8("%S"),&character);
       
   339 			addrAsText.SetLength((addrAsText.Length()-1));
       
   340 			}
       
   341 		else
       
   342 			{
       
   343 			if (addrAsText.Length() < addrAsText.MaxLength())
       
   344 				{
       
   345 				WriteNoReturn(_L8("%S"),&character);
       
   346 				addrAsText.Append(code);
       
   347 				}
       
   348 			}
       
   349 		}
       
   350 	//now extract the new address from the string...
       
   351 	if( !addrAsText.Length() )
       
   352 		{
       
   353 		addrAsText.Append('0'); //null string causes TLex::Val to return an error
       
   354 		}
       
   355 	TLex lex(addrAsText);
       
   356 	TInt err = lex.Val(aNumber, EDecimal);
       
   357 	User::LeaveIfError(err);
       
   358 	OstTraceFunctionExit0( CACTIVECONSOLE_GETNUMBERL_EXIT );
       
   359 	}