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