os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/FDFActor.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 // @file fdfactor.cpp
    15 // @internalComponent
    16 // 
    17 //
    18 
    19 #include "FDFActor.h"
    20 #include "testdebug.h"
    21 #include <Usb.h>
    22 #include "UsbDescriptorOffsets.h"
    23 #include "BaseTestCase.h"
    24 #include <e32property.h>
    25 
    26 extern RTest gtest;
    27 
    28 namespace NUnitTesting_USBDI
    29 	{
    30 static const TUid KWordOfDeathCat = {0x01066600};
    31 static const TInt KWordOfDeathKey = 0x01066601;
    32 
    33 static const TInt KNonExistantStringNumber = 97; //the device does not have a string descriptor associated with the number 97
    34 
    35 CActorFDF* CActorFDF::NewL(MUsbBusObserver& aObserver)
    36 	{
    37 	CActorFDF* self = new (ELeave) CActorFDF(aObserver);
    38 	CleanupStack::PushL(self);
    39 	self->ConstructL();
    40 	CleanupStack::Pop(self);
    41 	return self;
    42 	}
    43 
    44 
    45 CActorFDF::CActorFDF(MUsbBusObserver& aObserver)
    46 :	CActive(EPriorityStandard),
    47 	iObserver(aObserver)
    48 	{
    49 	}
    50 
    51 
    52 void CActorFDF::ConstructL()
    53 	{
    54 	LOG_FUNC
    55 	CActiveScheduler::Add(this);
    56 	
    57 	TInt err(iDriver.Open());
    58 	if(err != KErrNone)
    59 		{
    60 		RDebug::Printf("<Error %d> Unable to open driver channel",err);
    61 		User::Leave(err);
    62 		}		
    63 	
    64 	RDebug::Printf("PBASE-T_USBDI-xxxx: Stack starting");
    65 	/*
    66 	@SYMTestCaseID				PBASE-T_USBDI-xxxx
    67 	@SYMTestCaseDesc			Test for host stack initiation
    68 	@SYMFssID 
    69 	@SYMPREQ					1782
    70 	@SYMREQ						7097 [USBMAN : Activation and deactivation of USB Host functionality]
    71 	@SYMTestType				UT
    72 	@SYMTestPriority			1 
    73 	@SYMTestActions 			
    74 	@SYMTestExpectedResults	 	KErrNone from RUsbHubDriver::StartHost()
    75 	@SYMTestStatus				Implemented
    76 	*/
    77 	err = iDriver.StartHost();
    78 	if(err != KErrNone)
    79 		{
    80 		// Test case did not run successfully
    81 		RDebug::Printf("<Error %d> USB Host stack not starting",err);		
    82 		gtest(EFalse);
    83 		}
    84 	  
    85 	RDebug::Printf("starting t_usbhost_usbman.exe");
    86 	TInt r = iOtgUsbMan.Create(_L("t_usbhost_usbman.exe"), KNullDesC); //LIT does not seem to work here
    87 	if(r != KErrNone && r != KErrAlreadyExists)
    88 		{
    89   		RDebug::Printf("can't start t_usbhost_usbman.exe");
    90 		gtest(EFalse);
    91 		}
    92 	
    93     // create a publish/subscribe key to allow usbhost_usbman to be killed
    94     // cleanly
    95     static _LIT_SECURITY_POLICY_PASS(KAllowAllPolicy);
    96     r = RProperty::Define(KWordOfDeathCat, KWordOfDeathKey, RProperty::EInt,KAllowAllPolicy, KAllowAllPolicy, 0);
    97 	if(r != KErrNone && r != KErrAlreadyExists)
    98         {
    99         RDebug::Printf("Could not create the WordOfDeath P&S   (%d)", r);
   100  		gtest(EFalse);
   101         }
   102 
   103 	iOtgUsbMan.Resume();	
   104 	
   105 	User::After(1500000); //allow time for t_usbhost_usbman.exe to start	
   106 	}
   107 
   108 void KillTest()
   109 	{
   110 	RDebug::Printf("BEFORE gtest(EFalse)");
   111 	gtest(EFalse);
   112 	RDebug::Printf("AFTER gtest(EFalse)");
   113 	}
   114 
   115 CActorFDF::~CActorFDF()
   116 	{
   117 	LOG_FUNC
   118 	Cancel();
   119 	
   120 	// Destroy all test device objects that represented connected devices
   121 	
   122 	RHashMap<TUint,CUsbTestDevice*>::TIter it(iDevices);
   123 	TInt count(0);
   124 	for(count=0; count<iDevices.Count(); count++)
   125 		{
   126 		delete *it.NextValue();
   127 		}
   128 
   129 	RDebug::Printf("killing t_usbhost_usbman.exe");
   130 	TInt r = RProperty::Set(KWordOfDeathCat, KWordOfDeathKey, KErrAbort);   // Send the word of death
   131     if(r != KErrNone)
   132         {
   133         RDebug::Printf("failed to kill t_usbhost-usbhan  (%d)", r);      
   134 		RProperty::Delete(KWordOfDeathCat, KWordOfDeathKey); //try to clean this up ready for next test
   135 		User::After(1000000); //allow time for property to clean up
   136 		gtest(EFalse);
   137        }
   138 	User::After(1000000); //allow time for t_usbhost_usbman.exe to clean up
   139 
   140 	// Stop the USB Hub driver
   141 		
   142 	RDebug::Printf("UT-USBD-P1782-TN0001: Hub driver stopping...");
   143 
   144 	// Close the channel to the USB Hub driver	
   145 	iDriver.Close();	
   146 	
   147 	User::After(500000);
   148 	
   149     // delete word of death P&S
   150     r = RProperty::Delete(KWordOfDeathCat, KWordOfDeathKey);
   151     if(r != KErrNone)
   152         {
   153         //try again
   154 		User::After(1000000); //allow time for property to clean up
   155 		r = RProperty::Delete(KWordOfDeathCat, KWordOfDeathKey);
   156 		if(r != KErrNone)
   157 			//give up
   158 			{
   159 			User::After(1000000); //allow time for property to clean up JUST IN CASE it can despite returning an error!
   160 	        RDebug::Printf("failed to delete wordofdeath P&S  (%d)", r);
   161 			gtest(EFalse);
   162 			}
   163         }
   164 
   165 	}
   166 	
   167 void CActorFDF::DoCancel()
   168 	{
   169 	LOG_FUNC
   170 
   171 
   172 	RDebug::Printf("Cancelling bus event notifications");
   173 	iDriver.CancelWaitForBusEvent();
   174 	gtest((iStatus == KErrCancel) || (iStatus == KErrNone));	
   175 	RDebug::Printf("Bus event notifications successfully cancelled");	
   176 	}
   177 	
   178 void CActorFDF::Monitor()
   179 	{
   180 	LOG_FUNC
   181 
   182 	RDebug::Printf("Monitoring bus events");
   183 	iDriver.WaitForBusEvent(iBusEvent,iStatus);
   184 	SetActive();
   185 	}
   186 	
   187 	
   188 			
   189 CUsbTestDevice& CActorFDF::DeviceL(TUint aDeviceHandle)
   190 	{
   191 	return *iDevices.FindL(aDeviceHandle);
   192 	}
   193 	
   194 void CActorFDF::RunL()
   195 	{
   196 	LOG_FUNC
   197 
   198 	// Obtain completion code
   199 	TInt completionCode(iStatus.Int());
   200 	RDebug::Printf("Completion code  : %d",completionCode);
   201 	
   202 	 if(completionCode == KErrNone)
   203 		{
   204 		if(iBusEvent.iEventType == RUsbHubDriver::TBusEvent::EDeviceAttached)
   205 			{
   206 			// Device Attached
   207 			RDebug::Printf("Usb device attached: %d",iBusEvent.iDeviceHandle);
   208 			
   209 			// Create the test device object
   210 			iDevices.InsertL(iBusEvent.iDeviceHandle,CUsbTestDevice::NewL(iDriver,iBusEvent.iDeviceHandle,iObserver));
   211 			
   212 			// Notify observer
   213 			iObserver.DeviceInsertedL(iBusEvent.iDeviceHandle);
   214 			}
   215 		else if(iBusEvent.iEventType == RUsbHubDriver::TBusEvent::EDeviceRemoved)
   216 			{
   217 			// Device Removed
   218 			RDebug::Printf("Usb device removed: %d",iBusEvent.iDeviceHandle);
   219 			
   220 			// Notify observer
   221 			iObserver.DeviceRemovedL(iBusEvent.iDeviceHandle);
   222 			
   223 			// Destroy the device for the handle and remove from the map
   224 			delete iDevices.FindL(iBusEvent.iDeviceHandle);
   225 			iDevices.Remove(iBusEvent.iDeviceHandle);
   226 			}
   227 		else
   228 			{
   229 			// TODO: Upcall for USB Man etc
   230 			RDebug::Printf("<Warning> Bus event %d occured, still monitoring, reason = %d",iBusEvent.iEventType, iBusEvent.iReason);
   231 			iDriver.WaitForBusEvent(iBusEvent,iStatus);
   232 			SetActive();
   233 			}
   234 		}
   235 	else
   236 		{
   237 		RDebug::Printf("<Error %d> Bus event %d",completionCode,iBusEvent.iEventType);
   238 		iObserver.BusErrorL(completionCode);
   239 		}
   240 	}
   241 	
   242 
   243 TInt CActorFDF::RunError(TInt aError)
   244 	{
   245 	LOG_FUNC
   246 
   247 	RDebug::Printf("<Error %d> CActorFDF::RunError",aError);
   248 	return KErrNone;
   249 	}
   250 	
   251 	
   252 	
   253 	
   254 	
   255 	
   256 	
   257 	
   258 	
   259 	
   260 	
   261 CUsbTestDevice* CUsbTestDevice::NewL(RUsbHubDriver& aHubDriver,TUint aDeviceHandle,MUsbBusObserver& aObserver)
   262 	{
   263 	CUsbTestDevice* self = new (ELeave) CUsbTestDevice(aHubDriver,aDeviceHandle,aObserver);
   264 	CleanupStack::PushL(self);
   265 	self->ConstructL();
   266 	CleanupStack::Pop(self);
   267 	return self;
   268 	}
   269 	
   270 CUsbTestDevice::CUsbTestDevice(RUsbHubDriver& aHubDriver,TUint aHandle,MUsbBusObserver& aObserver)
   271 :	CActive(EPriorityUserInput),
   272 	iDriver(aHubDriver),
   273 	iHandle(aHandle),
   274 	iObserver(aObserver)
   275 	{
   276 	LOG_FUNC
   277 
   278 	CActiveScheduler::Add(this);
   279 	}
   280 	
   281 CUsbTestDevice::~CUsbTestDevice()
   282 	{
   283 	LOG_FUNC
   284 	Cancel();
   285 	iDevice.Close();
   286 	}
   287 	
   288 void CUsbTestDevice::ConstructL()
   289 	{
   290 	LOG_FUNC
   291 
   292 	// Open the usb device object
   293 	User::LeaveIfError(iDevice.Open(iDriver,iHandle));
   294 
   295 	TInt err(iDevice.GetDeviceDescriptor(iDeviceDescriptor));
   296 	if(err != KErrNone)
   297 		{
   298 		RDebug::Printf("<Error %d> Getting device (%d) descriptor",err,iHandle);
   299 		User::Leave(err);
   300 		}
   301 	
   302 	err = iDevice.GetConfigurationDescriptor(iConfigDescriptor);
   303 	if(err != KErrNone)
   304 		{
   305 		RDebug::Printf("<Error %d> Getting device (%d) configuration descriptor",err,iHandle);
   306 		User::Leave(err);
   307 		}
   308 
   309 	iDeviceSpec = iDeviceDescriptor.USBBcd();
   310 	iPid = iDeviceDescriptor.ProductId();
   311 	iVid = iDeviceDescriptor.VendorId();
   312 	
   313 	RDebug::Printf("%dmA configuration maximum power consumption",iConfigDescriptor.MaxPower()*2);
   314 	RDebug::Printf("%d number of interface(s)",iConfigDescriptor.NumInterfaces());
   315 	RDebug::Printf("Product Id=0x%04x, Vendor Id=0x%04x",iPid,iVid);
   316 	RDebug::Printf("TotalLength() = %d",iConfigDescriptor.TotalLength());
   317 
   318 	// The manufacturer string
   319 	err = iDevice.GetStringDescriptor(iManufacturerStringDesc,iManufacturerStringData,iDeviceDescriptor.ManufacturerIndex());
   320 	if(err != KErrNone)
   321 		{
   322 		RDebug::Printf("<Error %d> Getting device (%d) manufacturer string descriptor",err,iHandle);
   323 		User::Leave(err);
   324 		}
   325 	iManufacturerStringDesc->StringData(iManufacturerString);
   326 	
   327 	// The product string
   328 	err = iDevice.GetStringDescriptor(iProductStringDesc,iProductStringData,iDeviceDescriptor.ProductIndex());
   329 	if(err != KErrNone)
   330 		{
   331 		RDebug::Printf("<Error %d> Getting device (%d) product string descriptor",err,iHandle);
   332 		User::Leave(err);
   333 		}
   334 	iProductStringDesc->StringData(iProductString);
   335 		
   336 	// The serial number 
   337 	err = iDevice.GetStringDescriptor(iSerialNumberDesc,iSerialNumberStringData,iDeviceDescriptor.SerialNumberIndex());
   338 	if(err != KErrNone)
   339 		{
   340 		RDebug::Printf("<Error %d> Getting device (%d) serial number string descriptor",err,iHandle);
   341 		User::Leave(err);
   342 		}
   343 	iSerialNumberDesc->StringData(iSerialNumber);
   344 	
   345 	// The configuration string
   346 	err = iDevice.GetStringDescriptor(iConfigStringDesc,iConfigStringData,iConfigDescriptor.ConfigurationIndex());
   347 	if(err != KErrNone)
   348 		{
   349 		RDebug::Printf("<Error %d> Getting device (%d) configuration string descriptor",err,iHandle);
   350 		User::Leave(err);
   351 		}
   352 	iConfigStringDesc->StringData(iConfigString);
   353 	
   354 	// Trying to obtain a string descriptor that is not present, expecting a stall	
   355 	TBuf8<255> nonExistentString;
   356 	TUsbStringDescriptor* unusedStringDescriptor =  NULL;
   357 	err = iDevice.GetStringDescriptor(unusedStringDescriptor,nonExistentString,KNonExistantStringNumber);
   358 	if(err != KErrUsbStalled)
   359 		{
   360 		delete unusedStringDescriptor; //in case 'err ==  KErrNone', in which case this will need freeing
   361 		RDebug::Printf("GetStringDescriptor with nonExistentString didn't return KErrUsbStalled",err,iHandle);
   362 		User::Leave(err);
   363 		}
   364 	RDebug::Printf("String not present error(%d)",err);
   365 
   366 	// Get changes in device state
   367 	iDevice.QueueDeviceStateChangeNotification(iCurrentState,iStatus); // iCurrentState now holds the current device state
   368 	SetActive();
   369 	}
   370 	
   371 RUsbDevice& CUsbTestDevice::Device()
   372 	{
   373 	return iDevice;
   374 	}
   375 	
   376 TUint16 CUsbTestDevice::DeviceSpec() const
   377 	{
   378 	return iDeviceSpec;
   379 	}
   380 	
   381 TUint16 CUsbTestDevice::ProductId() const
   382 	{
   383 	return iPid;
   384 	}
   385 	
   386 TUint16 CUsbTestDevice::VendorId() const
   387 	{
   388 	return iVid;
   389 	}
   390 
   391 const TDesC16& CUsbTestDevice::SerialNumber() const
   392 	{
   393 	return iSerialNumber;
   394 	}
   395 
   396 const TDesC16& CUsbTestDevice::Manufacturer() const
   397 	{
   398 	return iManufacturerString;
   399 	}
   400 	
   401 const TDesC16& CUsbTestDevice::Product() const
   402 	{
   403 	return iProductString;
   404 	}
   405 	
   406 const TDesC16& CUsbTestDevice::ConfigurationString() const
   407 	{
   408 	return iConfigString;
   409 	}
   410 
   411 const TUsbConfigurationDescriptor& CUsbTestDevice::ConfigurationDescriptor() const
   412 	{
   413 	return iConfigDescriptor;
   414 	}
   415 		
   416 const TUsbDeviceDescriptor& CUsbTestDevice::DeviceDescriptor() const
   417 	{
   418 	return iDeviceDescriptor;
   419 	}
   420 
   421 void CUsbTestDevice::DoCancel()
   422 	{
   423 	LOG_FUNC
   424 
   425 	iDevice.CancelDeviceStateChangeNotification();
   426 	}
   427 
   428 
   429 void CUsbTestDevice::RunL()
   430 	{
   431 	LOG_FUNC
   432 
   433 	TInt completionCode(iStatus.Int());
   434 	RDebug::Printf("CUsbTestDevice::RunL completionCode(%d)",completionCode);
   435 
   436 	if(completionCode == KErrNone)
   437 		{
   438 		RUsbDevice::TDeviceState newState;
   439 		iDevice.QueueDeviceStateChangeNotification(newState,iStatus);
   440 		SetActive();
   441 		iObserver.DeviceStateChangeL(iCurrentState,newState,completionCode);
   442 		iCurrentState = newState;
   443 		}
   444 	}
   445 
   446 
   447 TInt CUsbTestDevice::RunError(TInt aError)
   448 	{
   449 	LOG_FUNC
   450 
   451 	RDebug::Printf("<Error %d>",aError);
   452 	return KErrNone;
   453 	}
   454 
   455 	}
   456