os/kernelhwsrv/kerneltest/e32test/usbho/t_otgdi/src/t_otgdi.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2007-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 // @internalComponent
    15 // 
    16 //
    17 
    18 
    19 #include <e32std.h>
    20 #include <e32std_private.h>
    21 #include <u32std.h> 	// unicode builds
    22 #include <e32base.h>
    23 #include <e32base_private.h>
    24 #include <e32cons.h>
    25 #include <e32Test.h>	// RTest headder
    26 #include <e32debug.h>
    27 #include "TestCaseFactory.h"
    28 #include "debugmacros.h"
    29 #include "testpolicy.h"
    30 #include "testengine.h"
    31 #include "testcaseroot.h"
    32 #include "b2bwatchers.h"
    33 
    34 
    35 
    36 TPtrC KMyApplicationName _L("t_otgdi.exe");
    37 
    38 // The RTest object (used for console in/output)
    39 RTest test(_L("OTGDI Unit/Dev. Testing"));
    40 
    41 // Parameters modified from the command-line
    42 // semi-automation mode off: (if we skip some keyboard prompts)
    43 TBool gSemiAutomated 	= EFalse;
    44 TBool gVerboseOutput 	= EFalse;
    45 TInt  gOpenIterations   = 3;	// default to 3 repeats (used for the open/close 
    46                                 // and other tests that use repeating)
    47 TInt  gOOMIterations    = 10;	// default to 10 allocs
    48 TBool gTestRoleMaster   = ETrue;   			// master by default
    49 TUint gUSBVidPid 		= KTestProductID; 	// Symbian VID+PID 0x2670
    50 
    51 
    52 extern RUsbOtgDriver  oUsbOtgDriver;
    53 
    54 // comment this line out for normal testing
    55 //#define TESTSOMETHING_DEF	1
    56 
    57 
    58 #ifdef TESTSOMETHING_DEF
    59 // Wait for Event with timeout, EFalse if we time out
    60 // @RETURNS : ETrue if the aStatus was signalled before the timeout
    61 TInt WaitForRequestWTime(TRequestStatus &aStatus, TTimeIntervalMicroSeconds32 aMicroseconds)
    62 	{
    63 	RTimer timer;
    64 	TRequestStatus statusT;
    65 	TRequestStatus *statarray[] = { &aStatus, &statusT};
    66 	
    67 	timer.CreateLocal();
    68 	timer.After(statusT, aMicroseconds);
    69 	User::WaitForNRequest( statarray, 2 );
    70 	if ( statusT == KRequestPending )
    71 		{
    72 		timer.Cancel();
    73 		return(ETrue);
    74 		}
    75 	else
    76 		{
    77 		return(EFalse);
    78 		}
    79 	}
    80 
    81 // Test basic API premises function:
    82 // define TESTSOMETHING_DEF if you want to test a new API or the priority of CActive 
    83 // or the scheduler + test framework is creating doubt.
    84 void ProtoTypeCode()
    85 	{
    86 		// This block of code is a prototype area, a rough approximation of a test 
    87 		// without any Active Schedulers interfering
    88 		TInt err(0);
    89 
    90 		{
    91 		TRequestStatus status1;	// calls
    92 		
    93 		RUsbOtgDriver::TOtgEvent   event;
    94 		TBuf<MAX_DSTRLEN> aDescription;
    95 		RUsbOtgDriver::TOtgIdPin	OTGIdPin;
    96 		RUsbOtgDriver::TOtgVbus   	OTGVBus;
    97 		RUsbOtgDriver::TOtgEvent 	OtgEvent;
    98 		//RUsbOtgDriver::TOtgIdPin   idPinState;
    99 
   100 		// LOAD OTG- User-driver					
   101 		err = User::LoadLogicalDevice(KOTGDeviceInterfaceDriverName);
   102 		if ( (err != KErrNone) && (err != KErrAlreadyExists) )
   103 			{
   104 			test.Printf(_L("<Error %d> Unable to load driver: %S"), err, &KOTGDeviceInterfaceDriverName);
   105 			}
   106 		err = oUsbOtgDriver.Open();
   107 		if (err != KErrNone)
   108 			test.Printf(_L("<Error %d> Unable to OPEN driver: %S"), err, &KOTGDeviceInterfaceDriverName);
   109 		else	
   110 			test.Printf(_L("OPEN driver: %S OK!"), &KOTGDeviceInterfaceDriverName);
   111 		
   112 		oUsbOtgDriver.StartStacks();
   113 		test.Printf(_L("Stack started\n"));
   114 		
   115 /*
   116 		for (TInt loop=0; loop <6; loop++)
   117 			// TEST Events
   118 			do		
   119 				{
   120 				test.Printf(_L("Waiting for OTG...\n"));
   121 				status1 = KRequestPending; // reset the status object
   122 				oUsbOtgDriver.QueueOtgEventRequest( event, status1 );
   123 				
   124 				// wait			
   125 				User::WaitForRequest(status1);
   126 				TInt completionCode(status1.Int());
   127 				CTestCaseRoot::OtgEventString(event, aDescription);
   128 				test.Printf(_L("Received event %d '%S' status(%d) \n"), event, &aDescription, completionCode);
   129 				}
   130 			while (event != RUsbOtgDriver::EEventAPlugInserted); // 'A' plug found
   131 
   132 */
   133 		test.Printf(_L("API QueueOtgIdPinNotification test:\n"));
   134 
   135 		for (TInt loop=0; loop <6; loop++)
   136 			// TEST Events
   137 			do		
   138 				{
   139 				test.Printf(_L("Waiting for OTG...\n"));
   140 				status1 = KRequestPending; // reset the status object
   141 				oUsbOtgDriver.QueueOtgIdPinNotification( OTGIdPin, status1 );
   142 				test.Printf(_L("Current pin %d  \n"), OTGIdPin);
   143 				
   144 				// wait			
   145 				User::WaitForRequest(status1);
   146 				TInt completionCode(status1.Int());
   147 				switch(OTGIdPin)
   148 					{
   149 					case RUsbOtgDriver::EIdPinAPlug:
   150 						aDescription = _L("A-plug");
   151 						break;
   152 					case RUsbOtgDriver::EIdPinBPlug:
   153 						aDescription = _L("B-plug");
   154 						break;
   155 					default:
   156 						aDescription = _L("other");
   157 						break;
   158 					}
   159 				test.Printf(_L("Received pin %d '%S' status(%d) \n"), OTGIdPin, &aDescription, completionCode);
   160 				}
   161 			while (OTGIdPin != RUsbOtgDriver::EIdPinAPlug); // 'A' plug found
   162 
   163 		test.Printf(_L("Press any key.\n"));
   164 		test.Getch();
   165 
   166 		test.Printf(_L("Shutting down stack.\n"));
   167 		oUsbOtgDriver.StopStacks();
   168 
   169 		oUsbOtgDriver.Close();
   170 
   171 		test.Printf(_L("Free the LDD.\n"));
   172 		err = User::FreeLogicalDevice( RUsbOtgDriver::Name() );			
   173 		if (err != KErrNone)
   174 			{
   175 			test.Printf(_L("<Error %d> Unable to UN-load driver: %S"), err, &KOTGDeviceInterfaceDriverName);
   176 			}		
   177 		test.Printf(_L("#############\nPress any key.\n"));
   178 		}
   179 		test.Getch();
   180 
   181 		test.Printf(_L("Free the LDD.\n"));
   182 		// end this process, if we do not want to run a test now as well
   183 		RProcess process;
   184 		process.Open(RProcess().Id());
   185 		process.Terminate(0); 
   186 	}
   187 #endif //TESTSOMETHING_DEF
   188 
   189 
   190 
   191 static void MainL()
   192 	{
   193 	LOG_FUNC
   194 	// Leave the hooks in for platform security
   195 #ifdef __DATA_CAGING__
   196 	RProcess().DataCaging(RProcess::EDataCagingOn);
   197 	RProcess().SecureApi(RProcess::ESecureApiOn);
   198 #endif		
   199 
   200 	// Identify the process and main thread
   201 	RProcess testProcess;
   202 	RThread().Process(testProcess);
   203 	testProcess.RenameMe(KMyApplicationName);
   204 	RThread().RenameMe(_L("t_otgdi.exe main thread"));
   205 
   206 	// Allocate and provide the console for output
   207 	test.SetConsole(Console::NewL(KMyApplicationName, TSize(KConsFullScreen,KConsFullScreen)));
   208 
   209 	
   210 	
   211 #ifdef TESTSOMETHING_DEF	
   212 	ProtoTypeCode()
   213 	// exitprocess
   214 #endif // TESTSOMETHING_DEF
   215 		
   216 	__UHEAP_MARK;
   217 
   218 	// Create a new active scheduler for this main thread
   219 	// we do this because console app has no scheduler, and we 
   220 	// want to be able to call async APIs
   221 	CActiveScheduler* sched = new (ELeave) CActiveScheduler;
   222 	CleanupStack::PushL(sched);
   223 	CActiveScheduler::Install(sched);
   224 	
   225 	__UHEAP_MARK;
   226 	// Create the test case Engine in USB host mode
   227 	CTestEngine* testEngine(NULL);
   228 	
   229 	TRAPD(err, testEngine = CTestEngine::NewL());
   230 	if (err == KErrNone)
   231 		{
   232 		CleanupStack::PushL(testEngine);
   233 			
   234 		// Synchronise with the client (not currently necessary, no peer)
   235 //DS	Commenting out as "not currently necessary"		RProcess::Rendezvous(KErrNone);
   236 		
   237 		// and start the active scheduler
   238 		CActiveScheduler::Start();
   239 
   240 		// display results
   241 		testEngine->Report();
   242 		
   243 		// done with the test engine now
   244 		CleanupStack::PopAndDestroy(testEngine);
   245 		}
   246 	else
   247 		{
   248 		if (-2 == err)
   249 			test.Printf(_L("Warning, no tests were selected!: %d\n"), err);
   250 		else
   251 			test.Printf(_L("Unable to create the test engine: %d\n"), err);
   252 		}
   253 
   254 	// test DONE, if we are running manual, have a delay
   255 	if (!gSemiAutomated)
   256 		{
   257 		// Get the engine to hang around so we can look at the screen output
   258 		test.Printf(KPressAnyKeyToEnd);
   259 		test.Getch();
   260 		}
   261 
   262 	__UHEAP_MARKEND;
   263 #ifdef _DEBUG
   264 	test.Printf(_L("Test heap leaks #1 OK\n"));
   265 #endif
   266 
   267 	CleanupStack::PopAndDestroy(sched);
   268 	__UHEAP_MARKEND;
   269 #ifdef _DEBUG
   270 	test.Printf(_L("Test heap leaks #2 OK\n"));
   271 #endif
   272 	
   273 	// Finish test and release resources - this ends up closing the console (our application window)
   274 	test.End();
   275 	test.Close();
   276 
   277 	}
   278 
   279 
   280 TInt E32Main()
   281 	{
   282 	LOG_FUNC
   283 	// Create the new trap-cleanup mechanism
   284 	CTrapCleanup* cleanup = CTrapCleanup::New();
   285 	
   286 	if (cleanup == NULL)
   287 		{
   288 		return KErrNoMemory;
   289 		}
   290 		
   291 	// Perform the tests
   292 	TRAPD(err,MainL());
   293 	if (err != KErrNone)
   294 		{
   295 		test.Printf(_L("MainL error: %d\n"),err);		
   296 		}
   297 	
   298 	delete cleanup;
   299 	
   300 	// Provide no error
   301 	return KErrNone;
   302 	}
   303