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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
20 #include "testdebug.h"
22 #include "UsbDescriptorOffsets.h"
23 #include "BaseTestCase.h"
24 #include <e32property.h>
28 namespace NUnitTesting_USBDI
30 static const TUid KWordOfDeathCat = {0x01066600};
31 static const TInt KWordOfDeathKey = 0x01066601;
33 static const TInt KNonExistantStringNumber = 97; //the device does not have a string descriptor associated with the number 97
35 CActorFDF* CActorFDF::NewL(MUsbBusObserver& aObserver)
37 CActorFDF* self = new (ELeave) CActorFDF(aObserver);
38 CleanupStack::PushL(self);
40 CleanupStack::Pop(self);
45 CActorFDF::CActorFDF(MUsbBusObserver& aObserver)
46 : CActive(EPriorityStandard),
52 void CActorFDF::ConstructL()
55 CActiveScheduler::Add(this);
57 TInt err(iDriver.Open());
60 RDebug::Printf("<Error %d> Unable to open driver channel",err);
64 RDebug::Printf("PBASE-T_USBDI-xxxx: Stack starting");
66 @SYMTestCaseID PBASE-T_USBDI-xxxx
67 @SYMTestCaseDesc Test for host stack initiation
70 @SYMREQ 7097 [USBMAN : Activation and deactivation of USB Host functionality]
74 @SYMTestExpectedResults KErrNone from RUsbHubDriver::StartHost()
75 @SYMTestStatus Implemented
77 err = iDriver.StartHost();
80 // Test case did not run successfully
81 RDebug::Printf("<Error %d> USB Host stack not starting",err);
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)
89 RDebug::Printf("can't start t_usbhost_usbman.exe");
93 // create a publish/subscribe key to allow usbhost_usbman to be killed
95 static _LIT_SECURITY_POLICY_PASS(KAllowAllPolicy);
96 r = RProperty::Define(KWordOfDeathCat, KWordOfDeathKey, RProperty::EInt,KAllowAllPolicy, KAllowAllPolicy, 0);
97 if(r != KErrNone && r != KErrAlreadyExists)
99 RDebug::Printf("Could not create the WordOfDeath P&S (%d)", r);
105 User::After(1500000); //allow time for t_usbhost_usbman.exe to start
110 RDebug::Printf("BEFORE gtest(EFalse)");
112 RDebug::Printf("AFTER gtest(EFalse)");
115 CActorFDF::~CActorFDF()
120 // Destroy all test device objects that represented connected devices
122 RHashMap<TUint,CUsbTestDevice*>::TIter it(iDevices);
124 for(count=0; count<iDevices.Count(); count++)
126 delete *it.NextValue();
129 RDebug::Printf("killing t_usbhost_usbman.exe");
130 TInt r = RProperty::Set(KWordOfDeathCat, KWordOfDeathKey, KErrAbort); // Send the word of death
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
138 User::After(1000000); //allow time for t_usbhost_usbman.exe to clean up
140 // Stop the USB Hub driver
142 RDebug::Printf("UT-USBD-P1782-TN0001: Hub driver stopping...");
144 // Close the channel to the USB Hub driver
149 // delete word of death P&S
150 r = RProperty::Delete(KWordOfDeathCat, KWordOfDeathKey);
154 User::After(1000000); //allow time for property to clean up
155 r = RProperty::Delete(KWordOfDeathCat, KWordOfDeathKey);
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);
167 void CActorFDF::DoCancel()
172 RDebug::Printf("Cancelling bus event notifications");
173 iDriver.CancelWaitForBusEvent();
174 gtest((iStatus == KErrCancel) || (iStatus == KErrNone));
175 RDebug::Printf("Bus event notifications successfully cancelled");
178 void CActorFDF::Monitor()
182 RDebug::Printf("Monitoring bus events");
183 iDriver.WaitForBusEvent(iBusEvent,iStatus);
189 CUsbTestDevice& CActorFDF::DeviceL(TUint aDeviceHandle)
191 return *iDevices.FindL(aDeviceHandle);
194 void CActorFDF::RunL()
198 // Obtain completion code
199 TInt completionCode(iStatus.Int());
200 RDebug::Printf("Completion code : %d",completionCode);
202 if(completionCode == KErrNone)
204 if(iBusEvent.iEventType == RUsbHubDriver::TBusEvent::EDeviceAttached)
207 RDebug::Printf("Usb device attached: %d",iBusEvent.iDeviceHandle);
209 // Create the test device object
210 iDevices.InsertL(iBusEvent.iDeviceHandle,CUsbTestDevice::NewL(iDriver,iBusEvent.iDeviceHandle,iObserver));
213 iObserver.DeviceInsertedL(iBusEvent.iDeviceHandle);
215 else if(iBusEvent.iEventType == RUsbHubDriver::TBusEvent::EDeviceRemoved)
218 RDebug::Printf("Usb device removed: %d",iBusEvent.iDeviceHandle);
221 iObserver.DeviceRemovedL(iBusEvent.iDeviceHandle);
223 // Destroy the device for the handle and remove from the map
224 delete iDevices.FindL(iBusEvent.iDeviceHandle);
225 iDevices.Remove(iBusEvent.iDeviceHandle);
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);
237 RDebug::Printf("<Error %d> Bus event %d",completionCode,iBusEvent.iEventType);
238 iObserver.BusErrorL(completionCode);
243 TInt CActorFDF::RunError(TInt aError)
247 RDebug::Printf("<Error %d> CActorFDF::RunError",aError);
261 CUsbTestDevice* CUsbTestDevice::NewL(RUsbHubDriver& aHubDriver,TUint aDeviceHandle,MUsbBusObserver& aObserver)
263 CUsbTestDevice* self = new (ELeave) CUsbTestDevice(aHubDriver,aDeviceHandle,aObserver);
264 CleanupStack::PushL(self);
266 CleanupStack::Pop(self);
270 CUsbTestDevice::CUsbTestDevice(RUsbHubDriver& aHubDriver,TUint aHandle,MUsbBusObserver& aObserver)
271 : CActive(EPriorityUserInput),
278 CActiveScheduler::Add(this);
281 CUsbTestDevice::~CUsbTestDevice()
288 void CUsbTestDevice::ConstructL()
292 // Open the usb device object
293 User::LeaveIfError(iDevice.Open(iDriver,iHandle));
295 TInt err(iDevice.GetDeviceDescriptor(iDeviceDescriptor));
298 RDebug::Printf("<Error %d> Getting device (%d) descriptor",err,iHandle);
302 err = iDevice.GetConfigurationDescriptor(iConfigDescriptor);
305 RDebug::Printf("<Error %d> Getting device (%d) configuration descriptor",err,iHandle);
309 iDeviceSpec = iDeviceDescriptor.USBBcd();
310 iPid = iDeviceDescriptor.ProductId();
311 iVid = iDeviceDescriptor.VendorId();
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());
318 // The manufacturer string
319 err = iDevice.GetStringDescriptor(iManufacturerStringDesc,iManufacturerStringData,iDeviceDescriptor.ManufacturerIndex());
322 RDebug::Printf("<Error %d> Getting device (%d) manufacturer string descriptor",err,iHandle);
325 iManufacturerStringDesc->StringData(iManufacturerString);
327 // The product string
328 err = iDevice.GetStringDescriptor(iProductStringDesc,iProductStringData,iDeviceDescriptor.ProductIndex());
331 RDebug::Printf("<Error %d> Getting device (%d) product string descriptor",err,iHandle);
334 iProductStringDesc->StringData(iProductString);
337 err = iDevice.GetStringDescriptor(iSerialNumberDesc,iSerialNumberStringData,iDeviceDescriptor.SerialNumberIndex());
340 RDebug::Printf("<Error %d> Getting device (%d) serial number string descriptor",err,iHandle);
343 iSerialNumberDesc->StringData(iSerialNumber);
345 // The configuration string
346 err = iDevice.GetStringDescriptor(iConfigStringDesc,iConfigStringData,iConfigDescriptor.ConfigurationIndex());
349 RDebug::Printf("<Error %d> Getting device (%d) configuration string descriptor",err,iHandle);
352 iConfigStringDesc->StringData(iConfigString);
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)
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);
364 RDebug::Printf("String not present error(%d)",err);
366 // Get changes in device state
367 iDevice.QueueDeviceStateChangeNotification(iCurrentState,iStatus); // iCurrentState now holds the current device state
371 RUsbDevice& CUsbTestDevice::Device()
376 TUint16 CUsbTestDevice::DeviceSpec() const
381 TUint16 CUsbTestDevice::ProductId() const
386 TUint16 CUsbTestDevice::VendorId() const
391 const TDesC16& CUsbTestDevice::SerialNumber() const
393 return iSerialNumber;
396 const TDesC16& CUsbTestDevice::Manufacturer() const
398 return iManufacturerString;
401 const TDesC16& CUsbTestDevice::Product() const
403 return iProductString;
406 const TDesC16& CUsbTestDevice::ConfigurationString() const
408 return iConfigString;
411 const TUsbConfigurationDescriptor& CUsbTestDevice::ConfigurationDescriptor() const
413 return iConfigDescriptor;
416 const TUsbDeviceDescriptor& CUsbTestDevice::DeviceDescriptor() const
418 return iDeviceDescriptor;
421 void CUsbTestDevice::DoCancel()
425 iDevice.CancelDeviceStateChangeNotification();
429 void CUsbTestDevice::RunL()
433 TInt completionCode(iStatus.Int());
434 RDebug::Printf("CUsbTestDevice::RunL completionCode(%d)",completionCode);
436 if(completionCode == KErrNone)
438 RUsbDevice::TDeviceState newState;
439 iDevice.QueueDeviceStateChangeNotification(newState,iStatus);
441 iObserver.DeviceStateChangeL(iCurrentState,newState,completionCode);
442 iCurrentState = newState;
447 TInt CUsbTestDevice::RunError(TInt aError)
451 RDebug::Printf("<Error %d>",aError);