os/kernelhwsrv/kerneltest/e32test/usb/t_usb_device/src/apitests.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2000-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 // e32test/usb/t_usb_device/src/apitests.cpp
    15 // USB Test Program T_USB_DEVICE, functional part.
    16 // Device-side part, to work against T_USB_HOST running on the host.
    17 // 
    18 //
    19 
    20 #include "general.h"									// CActiveControl, CActiveRW
    21 #include "config.h"
    22 #include "usblib.h"										// Helpers
    23 
    24 extern RTest test;
    25 extern TBool gVerbose;
    26 extern TBool gSkip;
    27 extern TBool gTempTest;
    28 
    29 _LIT16(KString_one, "Arbitrary String Descriptor Test String 1");
    30 _LIT16(KString_two, "Another Arbitrary String Descriptor Test String");
    31 
    32 void SetupDescriptors(LDDConfigPtr aLddPtr,RDEVCLIENT* aPort, TUint16 aPid = 0)
    33 	{
    34 	// === Device Descriptor
    35 	test.Start(_L("Set up descriptors"));
    36 
    37 	test.Next(_L("GetDeviceDescriptorSize"));
    38 	TInt deviceDescriptorSize = 0;
    39 	aPort->GetDeviceDescriptorSize(deviceDescriptorSize);
    40 	test_Equal(KUsbDescSize_Device,static_cast<TUint>(deviceDescriptorSize));
    41 
    42 	test.Next(_L("GetDeviceDescriptor"));
    43 	TBuf8<KUsbDescSize_Device> deviceDescriptor;
    44 	TInt r = aPort->GetDeviceDescriptor(deviceDescriptor);
    45 	test_KErrNone(r);
    46 
    47 	test.Next(_L("SetDeviceDescriptor"));
    48 	const TInt KUsbSpecOffset = 2;
    49 	const TInt KUsbDevClass = 4;
    50 	const TInt KUsbDevSubClass = 5;
    51 	const TInt KUsbDevProtocol = 6;
    52 	const TInt KUsbVendorIdOffset = 8;
    53 	const TInt KUsbProductIdOffset = 10;
    54 	const TInt KUsbDevReleaseOffset = 12;
    55 	// Change the USB spec number
    56 	deviceDescriptor[KUsbSpecOffset]   = LoByte(aLddPtr->iSpec);
    57 	deviceDescriptor[KUsbSpecOffset+1] = HiByte(aLddPtr->iSpec);
    58 	// Change the Device Class, SubClass and Protocol to zero so that they are not device specific
    59 	// and diferent clases can used on different interfaces
    60 	deviceDescriptor[KUsbDevClass] = 0;
    61 	deviceDescriptor[KUsbDevSubClass] = 0;
    62 	deviceDescriptor[KUsbDevProtocol] = 0;
    63 	// Change the device vendor ID (VID)
    64 	deviceDescriptor[KUsbVendorIdOffset]   = LoByte(aLddPtr->iVid);			// little endian!
    65 	deviceDescriptor[KUsbVendorIdOffset+1] = HiByte(aLddPtr->iVid);
    66 	// Change the device product ID (PID)
    67 	if (aPid != 0)
    68 		{	
    69 		deviceDescriptor[KUsbProductIdOffset]	= LoByte(aPid);		// little endian!
    70 		deviceDescriptor[KUsbProductIdOffset+1] = HiByte(aPid);		
    71 		}
    72 	else
    73 		{
    74 		deviceDescriptor[KUsbProductIdOffset]	= LoByte(aLddPtr->iPid);		// little endian!
    75 		deviceDescriptor[KUsbProductIdOffset+1] = HiByte(aLddPtr->iPid);		
    76 		}
    77 	// Change the device release number
    78 	deviceDescriptor[KUsbDevReleaseOffset]	 = LoByte(aLddPtr->iRelease);	// little endian!
    79 	deviceDescriptor[KUsbDevReleaseOffset+1] = HiByte(aLddPtr->iRelease);
    80 	r = aPort->SetDeviceDescriptor(deviceDescriptor);
    81 	test_KErrNone(r);
    82 
    83 	if (!gSkip)
    84 		{
    85 		test.Next(_L("GetDeviceDescriptor()"));
    86 		TBuf8<KUsbDescSize_Device> descriptor2;
    87 		r = aPort->GetDeviceDescriptor(descriptor2);
    88 		test_KErrNone(r);
    89 
    90 		test.Next(_L("Compare device descriptor with value set"));
    91 		r = descriptor2.Compare(deviceDescriptor);
    92 		test_KErrNone(r);	
    93 		}
    94 
    95 	// === Configuration Descriptor
    96 
    97 	test.Next(_L("GetConfigurationDescriptorSize"));
    98 	TInt configDescriptorSize = 0;
    99 	aPort->GetConfigurationDescriptorSize(configDescriptorSize);
   100 	test_Equal(KUsbDescSize_Config,static_cast<TUint>(configDescriptorSize));
   101 
   102 	test.Next(_L("GetConfigurationDescriptor"));
   103 	TBuf8<KUsbDescSize_Config> configDescriptor;
   104 	r = aPort->GetConfigurationDescriptor(configDescriptor);
   105 	test_KErrNone(r);
   106 
   107 	test.Next(_L("SetConfigurationDescriptor"));
   108 	// Change Self Power and Remote Wakeup
   109 	const TInt KUsbAttributesOffset = 7;
   110 	const TUint8 KUsbAttributeDefault = 0x80;
   111 	const TUint8 KUsbAttributeSelfPower = 0x40;
   112 	const TUint8 KUsbAttributeRemoteWakeup = 0x20;
   113 	configDescriptor[KUsbAttributesOffset] = KUsbAttributeDefault | (aLddPtr->iSelfPower ? KUsbAttributeSelfPower : 0) 
   114 													| (aLddPtr->iRemoteWakeup ? KUsbAttributeRemoteWakeup : 0);
   115 	// Change the reported max power
   116 	// 100mA (= 2 * 0x32) is the highest value allowed for a bus-powered device.
   117 	const TInt KUsbMaxPowerOffset = 8;
   118 	configDescriptor[KUsbMaxPowerOffset] = aLddPtr->iMaxPower;
   119 	r = aPort->SetConfigurationDescriptor(configDescriptor);
   120 	test_KErrNone(r);
   121 
   122 	if (!gSkip)
   123 		{
   124 		test.Next(_L("GetConfigurationDescriptor()"));
   125 		TBuf8<KUsbDescSize_Config> descriptor2;
   126 		r = aPort->GetConfigurationDescriptor(descriptor2);
   127 		test_KErrNone(r);
   128 
   129 		test.Next(_L("Compare configuration desc with value set"));
   130 		r = descriptor2.Compare(configDescriptor);
   131 		test_KErrNone(r);
   132 		}
   133 		
   134 	// === String Descriptors
   135 
   136 	test.Next(_L("SetStringDescriptor"));
   137 
   138 	// Set up any standard string descriptors that were defined in the xml config
   139 	if (aLddPtr->iManufacturer)
   140 		{
   141 		r = aPort->SetManufacturerStringDescriptor(* aLddPtr->iManufacturer);
   142 		test_KErrNone(r);
   143 		}
   144 		
   145 	if (aLddPtr->iProduct)
   146 		{
   147 		r = aPort->SetProductStringDescriptor(* aLddPtr->iProduct);
   148 		test_KErrNone(r);
   149 		}
   150 
   151 	if (aLddPtr->iSerialNumber)
   152 		{
   153 		r = aPort->SetSerialNumberStringDescriptor(* aLddPtr->iSerialNumber);
   154 		test_KErrNone(r);
   155 		}
   156 
   157 	// Set up two arbitrary string descriptors, which can be queried
   158 	// manually from the host side for testing purposes
   159 	
   160 	TBuf16<KUsbStringDescStringMaxSize / 2> wr_str(KString_one);
   161 	r = aPort->SetStringDescriptor(stridx1, wr_str);
   162 	test_KErrNone(r);
   163 
   164 	wr_str.FillZ(wr_str.MaxLength());
   165 	wr_str = KString_two;
   166 	r = aPort->SetStringDescriptor(stridx2, wr_str);
   167 	test_KErrNone(r);
   168 
   169 	test.End();
   170 	
   171 	}
   172 
   173 static void TestDeviceQualifierDescriptor(RDEVCLIENT* aPort)
   174 	{
   175 	test.Start(_L("Device_Qualifier Descriptor Manipulation"));
   176 
   177 	test.Next(_L("GetDeviceQualifierDescriptor()"));
   178 	TBuf8<KUsbDescSize_DeviceQualifier> descriptor;
   179 	TInt r = aPort->GetDeviceQualifierDescriptor(descriptor);
   180 	test_KErrNone(r);
   181 
   182 	test.Next(_L("SetDeviceQualifierDescriptor()"));
   183 	// Change the USB spec number to 3.00
   184 	descriptor[KDevDesc_SpecOffset]   = 0x00;
   185 	descriptor[KDevDesc_SpecOffset+1] = 0x03;
   186 	// Change the device class, subclass and protocol codes
   187 	descriptor[KDevDesc_DevClassOffset]    = 0xA1;
   188 	descriptor[KDevDesc_DevSubClassOffset] = 0xB2;
   189 	descriptor[KDevDesc_DevProtocolOffset] = 0xC3;
   190 	r = aPort->SetDeviceQualifierDescriptor(descriptor);
   191 	test_KErrNone(r);
   192 
   193 	test.Next(_L("GetDeviceQualifierDescriptor()"));
   194 	TBuf8<KUsbDescSize_DeviceQualifier> descriptor2;
   195 	r = aPort->GetDeviceQualifierDescriptor(descriptor2);
   196 	test_KErrNone(r);
   197 
   198 	test.Next(_L("Compare Device_Qualifier desc with value set"));
   199 	r = descriptor2.Compare(descriptor);
   200 	test_Equal(0,r);
   201 
   202 	test.End();
   203 	}
   204 
   205 
   206 
   207 static void	TestOtherSpeedConfigurationDescriptor(RDEVCLIENT* aPort)
   208 	{
   209 	test.Start(_L("Other_Speed_Configuration Desc Manipulation"));
   210 
   211 	test.Next(_L("GetOtherSpeedConfigurationDescriptor()"));
   212 	TBuf8<KUsbDescSize_OtherSpeedConfig> descriptor;
   213 	TInt r = aPort->GetOtherSpeedConfigurationDescriptor(descriptor);
   214 	test_KErrNone(r);
   215 
   216 	test.Next(_L("SetOtherSpeedConfigurationDescriptor()"));
   217 	// Invert Remote-Wakup support
   218 	descriptor[KConfDesc_AttribOffset] = (descriptor[KConfDesc_AttribOffset] ^ KUsbDevAttr_RemoteWakeup);
   219 	// Change the reported max power to 330mA (2 * 0xA5)
   220 	descriptor[KConfDesc_MaxPowerOffset] = 0xA5;
   221 	r = aPort->SetOtherSpeedConfigurationDescriptor(descriptor);
   222 	test_KErrNone(r);
   223 
   224 	test.Next(_L("GetOtherSpeedConfigurationDescriptor()"));
   225 	TBuf8<KUsbDescSize_OtherSpeedConfig> descriptor2;
   226 	r = aPort->GetOtherSpeedConfigurationDescriptor(descriptor2);
   227 	test_KErrNone(r);
   228 
   229 	test.Next(_L("Compare O_S_Config desc with value set"));
   230 	r = descriptor2.Compare(descriptor);
   231 	test_KErrNone(r);
   232 
   233 	test.End();
   234 	}
   235 
   236 
   237 static void TestInterfaceDescriptor(RDEVCLIENT* aPort, TInt aNumSettings)
   238 	{
   239 	test.Start(_L("Interface Descriptor Manipulation"));
   240 
   241 	// For all settings
   242 
   243 	TInt desc_size = 0;
   244 	TInt r = 0;
   245 	TBuf8<KUsbDescSize_Interface> descriptor;
   246 	TBuf8<KUsbDescSize_Interface> descriptor2;
   247 	for (TInt i =0; i < aNumSettings; i++)
   248 		{
   249 		
   250 		test.Next(_L("GetInterfaceDescriptorSize()"));
   251 		r = aPort->GetInterfaceDescriptorSize(i, desc_size);
   252 		if (r != KErrNone)
   253 			{
   254 			RDebug::Printf ("Error %d in GetInterfaceDescriptorSize %d\n",r,i);
   255 			}
   256 		test_KErrNone(r);
   257 		test_Equal(KUsbDescSize_Interface,static_cast<TUint>(desc_size));
   258 
   259 		test.Next(_L("GetInterfaceDescriptor()"));
   260 		r = aPort->GetInterfaceDescriptor(i, descriptor);
   261 		test_KErrNone(r);
   262 
   263 		test.Next(_L("SetInterfaceDescriptor()"));
   264 		// Change the interface protocol to 0x78(+)
   265 		TUint8 prot = 0x78;
   266 		if (descriptor[KIfcDesc_ProtocolOffset] == prot)
   267 			prot++;
   268 		descriptor[KIfcDesc_ProtocolOffset] = prot;
   269 		r = aPort->SetInterfaceDescriptor(i, descriptor);
   270 		test_KErrNone(r);
   271 
   272 		test.Next(_L("GetInterfaceDescriptor()"));
   273 		r = aPort->GetInterfaceDescriptor(i, descriptor2);
   274 		test_KErrNone(r);
   275 
   276 		test.Next(_L("Compare interface descriptor with value set"));
   277 		r = descriptor2.Compare(descriptor);
   278 		test_KErrNone(r);
   279 		}
   280 		
   281 	test.Next(_L("GetInterfaceDescriptor()"));
   282 	r = aPort->GetInterfaceDescriptor(aNumSettings, descriptor);
   283 	test_Equal(KErrNotFound,r);
   284 
   285 	test.Next(_L("SetInterfaceDescriptor()"));
   286 	r = aPort->SetInterfaceDescriptor(aNumSettings, descriptor);
   287 	test_Equal(KErrNotFound,r);
   288 		
   289 	test.End();
   290 	}
   291 
   292 
   293 static void TestClassSpecificDescriptors(RDEVCLIENT* aPort)
   294 	{
   295 	test.Start(_L("Class-specific Descriptor Manipulation"));
   296 
   297 	// First a class-specific Interface descriptor
   298 
   299 	test.Next(_L("SetCSInterfaceDescriptorBlock()"));
   300 	// choose arbitrary new descriptor size
   301 	const TInt KUsbDescSize_CS_Interface = KUsbDescSize_Interface + 10;
   302 	TBuf8<KUsbDescSize_CS_Interface> cs_ifc_descriptor;
   303 	cs_ifc_descriptor.FillZ(cs_ifc_descriptor.MaxLength());
   304 	cs_ifc_descriptor[KUsbDesc_SizeOffset] = KUsbDescSize_CS_Interface;
   305 	cs_ifc_descriptor[KUsbDesc_TypeOffset] = KUsbDescType_CS_Interface;
   306 	TInt r = aPort->SetCSInterfaceDescriptorBlock(0, cs_ifc_descriptor);
   307 	test_KErrNone(r);
   308 
   309 	test.Next(_L("GetCSInterfaceDescriptorBlockSize()"));
   310 	TInt desc_size = 0;
   311 	r = aPort->GetCSInterfaceDescriptorBlockSize(0, desc_size);
   312 	test_KErrNone(r);
   313 	test_Equal(KUsbDescSize_CS_Interface,desc_size);
   314 
   315 	test.Next(_L("GetCSInterfaceDescriptorBlock()"));
   316 	TBuf8<KUsbDescSize_CS_Interface> descriptor;
   317 	r = aPort->GetCSInterfaceDescriptorBlock(0, descriptor);
   318 	test_KErrNone(r);
   319 
   320 	test.Next(_L("Compare CS ifc descriptor with value set"));
   321 	r = descriptor.Compare(cs_ifc_descriptor);
   322 	test_KErrNone(r);
   323 
   324 	// Next a class-specific Endpoint descriptor
   325 
   326 	test.Next(_L("SetCSEndpointDescriptorBlock()"));
   327 	// choose arbitrary new descriptor size
   328 	const TInt KUsbDescSize_CS_Endpoint = KUsbDescSize_Endpoint + 5;
   329 	TBuf8<KUsbDescSize_CS_Endpoint> cs_ep_descriptor;
   330 	cs_ep_descriptor.FillZ(cs_ep_descriptor.MaxLength());
   331 	cs_ep_descriptor[KUsbDesc_SizeOffset] = KUsbDescSize_CS_Endpoint;
   332 	cs_ep_descriptor[KUsbDesc_TypeOffset] = KUsbDescType_CS_Endpoint;
   333 	r = aPort->SetCSEndpointDescriptorBlock(0, 2, cs_ep_descriptor);
   334 	test_KErrNone(r);
   335 
   336 	test.Next(_L("GetCSEndpointDescriptorBlockSize()"));
   337 	r = aPort->GetCSEndpointDescriptorBlockSize(0, 2, desc_size);
   338 	test_KErrNone(r);
   339 	test_Equal(KUsbDescSize_CS_Endpoint,desc_size);
   340 
   341 	test.Next(_L("GetCSEndpointDescriptorBlock()"));
   342 	TBuf8<KUsbDescSize_CS_Endpoint> descriptor2;
   343 	r = aPort->GetCSEndpointDescriptorBlock(0, 2, descriptor2);
   344 	test_KErrNone(r);
   345 
   346 	test.Next(_L("Compare CS ep descriptor with value set"));
   347 	r = descriptor2.Compare(cs_ep_descriptor);
   348 	test_KErrNone(r);
   349 
   350 	test.End();
   351 	}
   352 
   353 
   354 void TestEndpointDescriptor(RDEVCLIENT* aPort,TInt aIfSetting, TInt aEpNumber,TUsbcEndpointInfo aEpInfo)
   355 	{
   356 	test.Start(_L("Endpoint Descriptor Manipulation"));
   357 
   358 	TBuf8<KUsbDescSize_AudioEndpoint> epDescriptor;
   359 	TInt desc_size;
   360 	TInt r = aPort->GetEndpointDescriptorSize(aIfSetting, aEpNumber, desc_size);
   361 	test_KErrNone(r);
   362 	test_Equal(KUsbDescSize_Endpoint + aEpInfo.iExtra,static_cast<TUint>(desc_size));
   363 
   364 	r = aPort->GetEndpointDescriptor(aIfSetting, aEpNumber, epDescriptor);
   365 	test_KErrNone(r);
   366 
   367 	test(((aEpInfo.iDir & KUsbEpDirIn) && (epDescriptor[KEpDesc_AddressOffset] & 0x80) ||
   368 		!(aEpInfo.iDir & KUsbEpDirIn) && !(epDescriptor[KEpDesc_AddressOffset] & 0x80)) &&
   369 			EpTypeMask2Value(aEpInfo.iType) == (TUint)(epDescriptor[KEpDesc_AttributesOffset] & 0x03) &&
   370 			aEpInfo.iInterval == epDescriptor[KEpDesc_IntervalOffset]);
   371 			
   372 	// Change the endpoint poll interval
   373 	TUint8 ival = 0x66;
   374 	if (epDescriptor[KEpDesc_IntervalOffset] == ival)
   375 		ival++;
   376 
   377 
   378 	TUint8 saveAddr = 0;										// save the address
   379 	if (aEpInfo.iExtra > 0)
   380 		{
   381 		saveAddr = epDescriptor[KEpDesc_SynchAddressOffset];
   382 		TUint8 addr = 0x85;										// bogus address
   383 		if (epDescriptor[KEpDesc_SynchAddressOffset] == addr)
   384 			addr++;
   385 		epDescriptor[KEpDesc_SynchAddressOffset] = addr;
   386 		}
   387 
   388 	epDescriptor[KEpDesc_IntervalOffset] = ival;
   389 	r = aPort->SetEndpointDescriptor(aIfSetting, aEpNumber, epDescriptor);
   390 	test_KErrNone(r);
   391 
   392 	TBuf8<KUsbDescSize_AudioEndpoint> descriptor2;
   393 	r = aPort->GetEndpointDescriptor(aIfSetting, aEpNumber, descriptor2);
   394 	test_KErrNone(r);
   395 
   396 	r = descriptor2.Compare(epDescriptor);
   397 	test_KErrNone(r);
   398 
   399 	if (aEpInfo.iExtra > 0)
   400 		{
   401 		// Restore the endpoint synch address
   402 		epDescriptor[KEpDesc_SynchAddressOffset] = saveAddr;
   403 		}
   404 
   405 	// Restore the endpoint poll interval
   406 	epDescriptor[KEpDesc_IntervalOffset] = aEpInfo.iInterval;
   407 	r = aPort->SetEndpointDescriptor(aIfSetting, aEpNumber, epDescriptor);
   408 	test_KErrNone(r);
   409 
   410 	test.End();
   411 	}
   412 
   413 static void TestStandardStringDescriptors(RDEVCLIENT* aPort)
   414 	{
   415 	test.Start(_L("String Descriptor Manipulation"));
   416 
   417 	//
   418 	// --- LANGID code
   419 	//
   420 
   421 	test.Next(_L("GetStringDescriptorLangId()"));
   422 	TUint16 rd_langid_orig;
   423 	TInt r = aPort->GetStringDescriptorLangId(rd_langid_orig);
   424 	test_KErrNone(r);
   425 	test.Printf(_L("Original LANGID code: 0x%04X\n"), rd_langid_orig);
   426 
   427 	test.Next(_L("SetStringDescriptorLangId()"));
   428 	TUint16 wr_langid = 0x0809;								// English (UK) Language ID
   429 	if (wr_langid == rd_langid_orig)
   430 		wr_langid = 0x0444;									// Tatar Language ID
   431 	r = aPort->SetStringDescriptorLangId(wr_langid);
   432 	test_KErrNone(r);
   433 
   434 	test.Next(_L("GetStringDescriptorLangId()"));
   435 	TUint16 rd_langid;
   436 	r = aPort->GetStringDescriptorLangId(rd_langid);
   437 	test_KErrNone(r);
   438 	test.Printf(_L("New LANGID code: 0x%04X\n"), rd_langid);
   439 
   440 	test.Next(_L("Compare LANGID codes"));
   441 	test_Equal(wr_langid,rd_langid);
   442 
   443 	test.Next(_L("Restore original LANGID code"));
   444 	r = aPort->SetStringDescriptorLangId(rd_langid_orig);
   445 	test_KErrNone(r);
   446 	r = aPort->GetStringDescriptorLangId(rd_langid);
   447 	test_KErrNone(r);
   448 	test_Equal(rd_langid_orig,rd_langid);
   449 
   450 	//
   451 	// --- Manufacturer string
   452 	//
   453 
   454 	test.Next(_L("GetManufacturerStringDescriptor()"));
   455 	TBuf16<KUsbStringDescStringMaxSize / 2> rd_str_orig;
   456 	r = aPort->GetManufacturerStringDescriptor(rd_str_orig);
   457 	test(r == KErrNone || r == KErrNotFound);
   458 	TBool restore_string;
   459 	if (r == KErrNone)
   460 		{
   461 		test.Printf(_L("Original Manufacturer string: \"%lS\"\n"), &rd_str_orig);
   462 		restore_string = ETrue;
   463 		}
   464 	else
   465 		{
   466 		test.Printf(_L("No Manufacturer string set\n"));
   467 		restore_string = EFalse;
   468 		}
   469 
   470 	test.Next(_L("SetManufacturerStringDescriptor()"));
   471 	_LIT16(manufacturer, "Manufacturer Which Manufactures Devices");
   472 	TBuf16<KUsbStringDescStringMaxSize / 2> wr_str(manufacturer);
   473 	r = aPort->SetManufacturerStringDescriptor(wr_str);
   474 	test_KErrNone(r);
   475 
   476 	test.Next(_L("GetManufacturerStringDescriptor()"));
   477 	TBuf16<KUsbStringDescStringMaxSize / 2> rd_str;
   478 	r = aPort->GetManufacturerStringDescriptor(rd_str);
   479 	test_KErrNone(r);
   480 	test.Printf(_L("New Manufacturer string: \"%lS\"\n"), &rd_str);
   481 
   482 	test.Next(_L("Compare Manufacturer strings"));
   483 	r = rd_str.Compare(wr_str);
   484 	test_KErrNone(r);
   485 
   486 	test.Next(_L("SetManufacturerStringDescriptor()"));
   487 	_LIT16(manufacturer2, "Different Manufacturer Which Manufactures Different Devices");
   488 	wr_str.FillZ(wr_str.MaxLength());
   489 	wr_str = manufacturer2;
   490 	r = aPort->SetManufacturerStringDescriptor(wr_str);
   491 	test_KErrNone(r);
   492 
   493 	test.Next(_L("GetManufacturerStringDescriptor()"));
   494 	rd_str.FillZ(rd_str.MaxLength());
   495 	r = aPort->GetManufacturerStringDescriptor(rd_str);
   496 	test_KErrNone(r);
   497 	test.Printf(_L("New Manufacturer string: \"%lS\"\n"), &rd_str);
   498 
   499 	test.Next(_L("Compare Manufacturer strings"));
   500 	r = rd_str.Compare(wr_str);
   501 	test_KErrNone(r);
   502 
   503 	test.Next(_L("RemoveManufacturerStringDescriptor()"));
   504 	r = aPort->RemoveManufacturerStringDescriptor();
   505 	test_KErrNone(r);
   506 	r = aPort->GetManufacturerStringDescriptor(rd_str);
   507 	test_Equal(KErrNotFound,r);
   508 
   509 	if (restore_string)
   510 		{
   511 		test.Next(_L("Restore original string"));
   512 		r = aPort->SetManufacturerStringDescriptor(rd_str_orig);
   513 		test_KErrNone(r);
   514 		r = aPort->GetManufacturerStringDescriptor(rd_str);
   515 		test_KErrNone(r);
   516 		r = rd_str.Compare(rd_str_orig);
   517 		test_KErrNone(r);
   518 		}
   519 
   520 	//
   521 	// --- Product string
   522 	//
   523 
   524 	test.Next(_L("GetProductStringDescriptor()"));
   525 	rd_str_orig.FillZ(rd_str.MaxLength());
   526 	r = aPort->GetProductStringDescriptor(rd_str_orig);
   527 	test(r == KErrNone || r == KErrNotFound);
   528 	if (r == KErrNone)
   529 		{
   530 		test.Printf(_L("Old Product string: \"%lS\"\n"), &rd_str_orig);
   531 		restore_string = ETrue;
   532 		}
   533 	else
   534 		restore_string = EFalse;
   535 
   536 	test.Next(_L("SetProductStringDescriptor()"));
   537 	_LIT16(product, "Product That Was Produced By A Manufacturer");
   538 	wr_str.FillZ(wr_str.MaxLength());
   539 	wr_str = product;
   540 	r = aPort->SetProductStringDescriptor(wr_str);
   541 	test_KErrNone(r);
   542 
   543 	test.Next(_L("GetProductStringDescriptor()"));
   544 	rd_str.FillZ(rd_str.MaxLength());
   545 	r = aPort->GetProductStringDescriptor(rd_str);
   546 	test_KErrNone(r);
   547 	test.Printf(_L("New Product string: \"%lS\"\n"), &rd_str);
   548 
   549 	test.Next(_L("Compare Product strings"));
   550 	r = rd_str.Compare(wr_str);
   551 	test_KErrNone(r);
   552 
   553 	test.Next(_L("SetProductStringDescriptor()"));
   554 	_LIT16(product2, "Different Product That Was Produced By A Different Manufacturer");
   555 	wr_str.FillZ(wr_str.MaxLength());
   556 	wr_str = product2;
   557 	r = aPort->SetProductStringDescriptor(wr_str);
   558 	test_KErrNone(r);
   559 
   560 	test.Next(_L("GetProductStringDescriptor()"));
   561 	rd_str.FillZ(rd_str.MaxLength());
   562 	r = aPort->GetProductStringDescriptor(rd_str);
   563 	test_KErrNone(r);
   564 	test.Printf(_L("New Product string: \"%lS\"\n"), &rd_str);
   565 
   566 	test.Next(_L("Compare Product strings"));
   567 	r = rd_str.Compare(wr_str);
   568 	test_KErrNone(r);
   569 
   570 	test.Next(_L("RemoveProductStringDescriptor()"));
   571 	r = aPort->RemoveProductStringDescriptor();
   572 	test_KErrNone(r);
   573 	r = aPort->GetProductStringDescriptor(rd_str);
   574 	test_Equal(KErrNotFound,r);
   575 
   576 	if (restore_string)
   577 		{
   578 		test.Next(_L("Restore original string"));
   579 		r = aPort->SetProductStringDescriptor(rd_str_orig);
   580 		test_KErrNone(r);
   581 		r = aPort->GetProductStringDescriptor(rd_str);
   582 		test_KErrNone(r);
   583 		r = rd_str.Compare(rd_str_orig);
   584 		test_KErrNone(r);
   585 		}
   586 
   587 	//
   588 	// --- Serial Number string
   589 	//
   590 
   591 	test.Next(_L("GetSerialNumberStringDescriptor()"));
   592 	rd_str_orig.FillZ(rd_str.MaxLength());
   593 	r = aPort->GetSerialNumberStringDescriptor(rd_str_orig);
   594 	test(r == KErrNone || r == KErrNotFound);
   595 	if (r == KErrNone)
   596 		{
   597 		test.Printf(_L("Old Serial Number: \"%lS\"\n"), &rd_str_orig);
   598 		restore_string = ETrue;
   599 		}
   600 	else
   601 		restore_string = EFalse;
   602 
   603 	test.Next(_L("SetSerialNumberStringDescriptor()"));
   604 	_LIT16(serial, "000666000XYZ");
   605 	wr_str.FillZ(wr_str.MaxLength());
   606 	wr_str = serial;
   607 	r = aPort->SetSerialNumberStringDescriptor(wr_str);
   608 	test_KErrNone(r);
   609 
   610 	test.Next(_L("GetSerialNumberStringDescriptor()"));
   611 	rd_str.FillZ(rd_str.MaxLength());
   612 	r = aPort->GetSerialNumberStringDescriptor(rd_str);
   613 	test_KErrNone(r);
   614 	test.Printf(_L("New Serial Number: \"%lS\"\n"), &rd_str);
   615 
   616 	test.Next(_L("Compare Serial Number strings"));
   617 	r = rd_str.Compare(wr_str);
   618 	test_KErrNone(r);
   619 
   620 	test.Next(_L("SetSerialNumberStringDescriptor()"));
   621 	_LIT16(serial2, "Y11611193111711111Y");
   622 	wr_str.FillZ(wr_str.MaxLength());
   623 	wr_str = serial2;
   624 	r = aPort->SetSerialNumberStringDescriptor(wr_str);
   625 	test_KErrNone(r);
   626 
   627 	test.Next(_L("GetSerialNumberStringDescriptor()"));
   628 	rd_str.FillZ(rd_str.MaxLength());
   629 	r = aPort->GetSerialNumberStringDescriptor(rd_str);
   630 	test_KErrNone(r);
   631 	test.Printf(_L("New Serial Number: \"%lS\"\n"), &rd_str);
   632 
   633 	test.Next(_L("Compare Serial Number strings"));
   634 	r = rd_str.Compare(wr_str);
   635 	test_KErrNone(r);
   636 
   637 	test.Next(_L("RemoveSerialNumberStringDescriptor()"));
   638 	r = aPort->RemoveSerialNumberStringDescriptor();
   639 	test_KErrNone(r);
   640 	r = aPort->GetSerialNumberStringDescriptor(rd_str);
   641 	test_Equal(KErrNotFound,r);
   642 
   643 	if (restore_string)
   644 		{
   645 		test.Next(_L("Restore original string"));
   646 		r = aPort->SetSerialNumberStringDescriptor(rd_str_orig);
   647 		test_KErrNone(r);
   648 		r = aPort->GetSerialNumberStringDescriptor(rd_str);
   649 		test_KErrNone(r);
   650 		r = rd_str.Compare(rd_str_orig);
   651 		test_KErrNone(r);
   652 		}
   653 
   654 	//
   655 	// --- Configuration string
   656 	//
   657 
   658 	test.Next(_L("GetConfigurationStringDescriptor()"));
   659 	rd_str_orig.FillZ(rd_str.MaxLength());
   660 	r = aPort->GetConfigurationStringDescriptor(rd_str_orig);
   661 	test(r == KErrNone || r == KErrNotFound);
   662 	if (r == KErrNone)
   663 		{
   664 		test.Printf(_L("Old Configuration string: \"%lS\"\n"), &rd_str_orig);
   665 		restore_string = ETrue;
   666 		}
   667 	else
   668 		restore_string = EFalse;
   669 
   670 	test.Next(_L("SetConfigurationStringDescriptor()"));
   671 	_LIT16(config, "Relatively Simple Configuration That Is Still Useful");
   672 	wr_str.FillZ(wr_str.MaxLength());
   673 	wr_str = config;
   674 	r = aPort->SetConfigurationStringDescriptor(wr_str);
   675 	test_KErrNone(r);
   676 
   677 	test.Next(_L("GetConfigurationStringDescriptor()"));
   678 	rd_str.FillZ(rd_str.MaxLength());
   679 	r = aPort->GetConfigurationStringDescriptor(rd_str);
   680 	test_KErrNone(r);
   681 	test.Printf(_L("New Configuration string: \"%lS\"\n"), &rd_str);
   682 
   683 	test.Next(_L("Compare Configuration strings"));
   684 	r = rd_str.Compare(wr_str);
   685 	test_KErrNone(r);
   686 
   687 	test.Next(_L("SetConfigurationStringDescriptor()"));
   688 	_LIT16(config2, "Convenient Configuration That Can Be Very Confusing");
   689 	wr_str.FillZ(wr_str.MaxLength());
   690 	wr_str = config2;
   691 	r = aPort->SetConfigurationStringDescriptor(wr_str);
   692 	test_KErrNone(r);
   693 
   694 	test.Next(_L("GetConfigurationStringDescriptor()"));
   695 	rd_str.FillZ(rd_str.MaxLength());
   696 	r = aPort->GetConfigurationStringDescriptor(rd_str);
   697 	test_KErrNone(r);
   698 	test.Printf(_L("New Configuration string: \"%lS\"\n"), &rd_str);
   699 
   700 	test.Next(_L("Compare Configuration strings"));
   701 	r = rd_str.Compare(wr_str);
   702 	test_KErrNone(r);
   703 
   704 	test.Next(_L("RemoveConfigurationStringDescriptor()"));
   705 	r = aPort->RemoveConfigurationStringDescriptor();
   706 	test_KErrNone(r);
   707 	r = aPort->GetConfigurationStringDescriptor(rd_str);
   708 	test_Equal(KErrNotFound,r);
   709 
   710 	if (restore_string)
   711 		{
   712 		test.Next(_L("Restore original string"));
   713 		r = aPort->SetConfigurationStringDescriptor(rd_str_orig);
   714 		test_KErrNone(r);
   715 		r = aPort->GetConfigurationStringDescriptor(rd_str);
   716 		test_KErrNone(r);
   717 		r = rd_str.Compare(rd_str_orig);
   718 		test_KErrNone(r);
   719 		}
   720 
   721 	test.End();
   722 	}
   723 
   724 
   725 static void TestArbitraryStringDescriptors(RDEVCLIENT* aPort,TInt aNumSettings)
   726 	{
   727 	test.Start(_L("Arbitrary String Descriptor Manipulation"));
   728 
   729 	// First test string
   730 
   731 	test.Next(_L("GetStringDescriptor() 1"));
   732 	TBuf16<KUsbStringDescStringMaxSize / 2> rd_str;
   733 	TInt r = aPort->GetStringDescriptor(stridx1, rd_str);
   734 	test_KErrNone(r);
   735 
   736 	TBuf16<KUsbStringDescStringMaxSize / 2> wr_str(KString_one);
   737 	r = rd_str.Compare(wr_str);
   738 	test_KErrNone(r);
   739 
   740 	// Second test string
   741 
   742 	test.Next(_L("GetStringDescriptor() 2"));
   743 	rd_str.FillZ(rd_str.MaxLength());
   744 	r = aPort->GetStringDescriptor(stridx2, rd_str);
   745 	test_KErrNone(r);
   746 
   747 	wr_str = KString_two;
   748 	r = rd_str.Compare(wr_str);
   749 	test_KErrNone(r);
   750 
   751 	// Third test string
   752 
   753 	test.Next(_L("GetStringDescriptor() 3"));
   754 	rd_str.FillZ(rd_str.MaxLength());
   755 	r = aPort->GetStringDescriptor(stridx3, rd_str);
   756 	test_Equal(KErrNotFound,r);
   757 
   758 	test.Next(_L("SetStringDescriptor() 3"));
   759 	_LIT16(string_three, "Arbitrary String Descriptor Test String 3");
   760 	wr_str.FillZ(wr_str.MaxLength());
   761 	wr_str = string_three;
   762 	r = aPort->SetStringDescriptor(stridx3, wr_str);
   763 	test_KErrNone(r);
   764 
   765 	// In between we create another interface setting to see what happens
   766 	// to the existing string descriptor indices.
   767 	// (We don't have to test this on every platform -
   768 	// besides, those that don't support alt settings
   769 	// are by now very rare.)
   770 	if (SupportsAlternateInterfaces())
   771 		{
   772 		#ifdef USB_SC
   773 		TUsbcScInterfaceInfoBuf ifc;
   774 		#else
   775 		TUsbcInterfaceInfoBuf ifc;
   776 		#endif
   777 		_LIT16(string, "T_USB_DEVICE Bogus Test Interface (Next Setting)");
   778 		ifc().iString = const_cast<TDesC16*>(&string);
   779 		ifc().iTotalEndpointsUsed = 0;
   780 		TInt r = aPort->SetInterface(aNumSettings, ifc);
   781 		test_KErrNone(r);
   782 		}
   783 
   784 	test.Next(_L("GetStringDescriptor() 3"));
   785 	r = aPort->GetStringDescriptor(stridx3, rd_str);
   786 	test_KErrNone(r);
   787 	test.Printf(_L("New test string @ idx %d: \"%lS\"\n"), stridx3, &rd_str);
   788 
   789 	test.Next(_L("Compare test strings 3"));
   790 	r = rd_str.Compare(wr_str);
   791 	test_KErrNone(r);
   792 
   793 	// Remove string descriptors 3 and 4
   794 
   795 	test.Next(_L("RemoveStringDescriptor() 4"));
   796 	r = aPort->RemoveStringDescriptor(stridx4);
   797 	test_Equal(KErrNotFound,r);
   798 
   799 	test.Next(_L("RemoveStringDescriptor() 3"));
   800 	r = aPort->RemoveStringDescriptor(stridx3);
   801 	test_KErrNone(r);
   802 
   803 	r = aPort->GetStringDescriptor(stridx3, rd_str);
   804 	test_Equal(KErrNotFound,r);
   805 
   806 	if (SupportsAlternateInterfaces())
   807 		{
   808 		TInt r = aPort->ReleaseInterface(aNumSettings);
   809 		test_KErrNone(r);
   810 		}
   811 
   812 	test.End();
   813 	}
   814 
   815 void TestInvalidSetInterface (RDEVCLIENT* aPort,TInt aNumSettings)
   816 	{
   817 	#ifdef USB_SC
   818 	TUsbcScInterfaceInfoBuf ifc;
   819 	#else
   820 	TUsbcInterfaceInfoBuf ifc;
   821 	#endif
   822 	_LIT16(string, "T_USB_DEVICE Invalid Interface");
   823 	ifc().iString = const_cast<TDesC16*>(&string);
   824 	ifc().iTotalEndpointsUsed = 0;
   825 
   826 	test.Start(_L("Test Invalid Interface Setting"));
   827 
   828 	if (SupportsAlternateInterfaces())
   829 		{
   830 		TInt r = aPort->SetInterface(aNumSettings+1, ifc);
   831 		test_Compare(r,!=,KErrNone);		
   832 		}
   833 	
   834 	if (aNumSettings > 1)
   835 		{
   836 		TInt r = aPort->SetInterface(aNumSettings-1, ifc);
   837 		test_Compare(r,!=,KErrNone);		
   838 		}
   839 
   840 	TInt r = aPort->SetInterface(0, ifc);
   841 	test_Compare(r,!=,KErrNone);		
   842 
   843 	test.End();
   844 	}
   845 
   846 void TestInvalidReleaseInterface (RDEVCLIENT* aPort,TInt aNumSettings)
   847 	{
   848 	test.Start(_L("Test Invalid Interface Release"));
   849 
   850 	if (aNumSettings > 2)
   851 		{
   852 		TInt r = aPort->ReleaseInterface(aNumSettings-3);
   853 		test_Compare(r,!=,KErrNone);		
   854 		}
   855 
   856 	if (aNumSettings > 1)
   857 		{
   858 		TInt r = aPort->ReleaseInterface(aNumSettings-2);
   859 		test_Compare(r,!=,KErrNone);		
   860 		}
   861 
   862 	test.End();
   863 	}
   864 
   865 void TestDescriptorManipulation(TBool aHighSpeed, RDEVCLIENT* aPort, TInt aNumSettings)
   866 	{
   867 	test.Start(_L("Test USB Descriptor Manipulation"));
   868 
   869 	if (aHighSpeed)
   870 		{
   871 		TestDeviceQualifierDescriptor(aPort);
   872 
   873 		TestOtherSpeedConfigurationDescriptor(aPort);	
   874 		}
   875 
   876 	TestInterfaceDescriptor(aPort,aNumSettings);
   877 
   878 	TestClassSpecificDescriptors(aPort);
   879 
   880 	TestStandardStringDescriptors(aPort);
   881 
   882 	TestArbitraryStringDescriptors(aPort,aNumSettings);
   883 
   884 	test.End();
   885 	}
   886 
   887 
   888 void TestOtgExtensions(RDEVCLIENT* aPort)
   889 	{
   890 	test.Start(_L("Test Some OTG API Extensions"));
   891 
   892 	// Test OTG descriptor manipulation
   893 	test.Next(_L("Get OTG Descriptor Size"));
   894 	TInt size;
   895 	aPort->GetOtgDescriptorSize(size);
   896 	test_Equal(KUsbDescSize_Otg,static_cast<TUint>(size));
   897 
   898 	test.Next(_L("Get OTG Descriptor"));
   899 	TBuf8<KUsbDescSize_Otg> otgDesc;
   900 	TInt r = aPort->GetOtgDescriptor(otgDesc);
   901 	test(r == KErrNotSupported || r == KErrNone);
   902 
   903 	test.Next(_L("Set OTG Descriptor"));
   904 	TBool supportOtg = EFalse;
   905 	if (r == KErrNotSupported)
   906 		{
   907 		r = aPort->SetOtgDescriptor(otgDesc);
   908 		test_Equal(KErrNotSupported,r);
   909 		}
   910 	else
   911 		{
   912 		supportOtg = ETrue;
   913 		otgDesc[0] = KUsbDescSize_Otg;
   914 		otgDesc[1] = KUsbDescType_Otg;
   915 		otgDesc[2] = KUsbOtgAttr_SrpSupp;
   916 		r = aPort->SetOtgDescriptor(otgDesc);
   917 		test_KErrNone(r);
   918 		TBuf8<KUsbDescSize_Otg> desc;
   919 		r = aPort->GetOtgDescriptor(desc);
   920 		test_KErrNone(r);
   921 		test_Equal(0,desc.Compare(otgDesc));
   922 		}
   923 
   924 	// Test get/set OTG feature
   925 	test.Next(_L("Get OTG Features"));
   926 	TUint8 features;
   927 	r = aPort->GetOtgFeatures(features);
   928 	if (supportOtg)
   929 		{
   930 		test_KErrNone(r);
   931 		TBool b_HnpEnable = (features & KUsbOtgAttr_B_HnpEnable) ? ETrue : EFalse;
   932 		TBool a_HnpSupport = (features & KUsbOtgAttr_A_HnpSupport) ? ETrue : EFalse;
   933 		TBool a_AltHnpSupport = (features & KUsbOtgAttr_A_AltHnpSupport) ? ETrue : EFalse;
   934 		test.Printf(_L("### OTG Features:\nB_HnpEnable(%d)\nA_HnpSupport(%d)\nA_Alt_HnpSupport(%d)\n"),
   935 					b_HnpEnable, a_HnpSupport, a_AltHnpSupport);
   936 		}
   937 	else
   938 		{
   939 		test_Equal(KErrNotSupported,r);
   940 		test.Printf(_L("GetOtgFeatures() not supported\n"));
   941 		}
   942 
   943 	test.End();
   944 }
   945 
   946 
   947 void TestEndpoint0MaxPacketSizes(RDEVCLIENT* aPort)
   948 	{
   949 	test.Start(_L("Test Endpoint0 MaxPacketSizes"));
   950 
   951 	TUint32 sizes = aPort->EndpointZeroMaxPacketSizes();
   952 	TInt r = KErrNone;
   953 	TBool good;
   954 	TInt mpsize = 0;
   955 	for (TInt i = 0; i < 32; i++)
   956 		{
   957 		TUint bit = sizes & (1 << i);
   958 		if (bit != 0)
   959 			{
   960 			switch (bit)
   961 				{
   962 			case KUsbEpSizeCont:
   963 				good = EFalse;
   964 				break;
   965 			case KUsbEpSize8:
   966 				mpsize = 8;
   967 				good = ETrue;
   968 				break;
   969 			case KUsbEpSize16:
   970 				mpsize = 16;
   971 				good = ETrue;
   972 				break;
   973 			case KUsbEpSize32:
   974 				mpsize = 32;
   975 				good = ETrue;
   976 				break;
   977 			case KUsbEpSize64:
   978 				mpsize = 64;
   979 				good = ETrue;
   980 				break;
   981 			case KUsbEpSize128:
   982 			case KUsbEpSize256:
   983 			case KUsbEpSize512:
   984 			case KUsbEpSize1023:
   985 			default:
   986 				good = EFalse;
   987 				break;
   988 				}
   989 			if (good)
   990 				{
   991 				test.Printf(_L("Ep0 supports %d bytes MaxPacketSize\n"), mpsize);
   992 				}
   993 			else
   994 				{
   995 				test.Printf(_L("Bad Ep0 size: 0x%08x, failure will occur\n"), bit);
   996 				r = KErrGeneral;
   997 				}
   998 			}
   999 		}
  1000 	test_KErrNone(r);
  1001 
  1002     test.End();
  1003 	}
  1004 
  1005