os/boardsupport/haitest/bspsvs/suite/e32/src/T_RDevUsbcClientData.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2005-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 // This contains implementation for CT_RDevUsbcClientData class
    18 
    19 
    20 
    21 #include <f32file.h>
    22 #include <d32usbc.h>
    23 #include "T_RDevUsbcClientData.h" 
    24 
    25 
    26 
    27 /*@{*/
    28 //LITs to identify the commands
    29 _LIT(KCmdNewL,									"NewL");
    30 _LIT(KCmdUtilityLoadLogicalDevice, 				"LoadLogicalDevice");
    31 _LIT(KCmdOpen, 					    			"Open");
    32 _LIT(KCmdDeviceCaps,							"DeviceCaps");
    33 _LIT(KCmdUtilitySetEndPoint, 					"SetEndPoint");
    34 _LIT(KCmdEndpointCaps,							"EndpointCaps");
    35 _LIT(KCmdSetInterface, 							"SetInterface");
    36 _LIT(KCmdGetDeviceDescriptorSize,				"GetDeviceDescriptorSize");
    37 _LIT(KCmdGetDeviceDescriptor,					"GetDeviceDescriptor");
    38 _LIT(KCmdSetDeviceDescriptor,					"SetDeviceDescriptor");
    39 _LIT(KCmdDestructor,							"~");
    40 /*@}*/
    41 
    42 
    43 /*@{*/
    44 //LITs to read from the ini file
    45 _LIT(KMode, 									"mode");
    46 _LIT(KDirection, 								"direction");
    47 _LIT(KBufferingMask, 							"bandwidthBufferingMask");
    48 _LIT(KSupportsConnect,							"supportsConnect");
    49 _LIT(KSupportsSelfPowered, 						"supportsSelfPowered");
    50 _LIT(KSupportsRemoteWakeup, 					"supportsRemoteWakeup");
    51 _LIT(KUSBVersionLow, 							"USBVersionLow");
    52 _LIT(KUSBVersionHigh,							"USBVersionHigh");
    53 _LIT(KVendorIDLow, 								"VendorIDLow");
    54 _LIT(KVendorIDHigh, 							"VendorIDHigh");
    55 _LIT(KProductIDLow, 							"ProductIDLow");
    56 _LIT(KProductIDHigh, 							"ProductIDHigh");
    57 _LIT(KReleaseNumberLow, 						"ReleaseNumberLow");
    58 _LIT(KReleaseNumberHigh, 						"ReleaseNumberHigh");
    59 _LIT(KDriver, 									"driver");
    60 _LIT(KEXPECTED, 								"expected");
    61 /*@}*/
    62 
    63 /*@{*/
    64 _LIT16(interfaceName, "USB interface");
    65 /*@}*/
    66 
    67 /*@{*/
    68 _LIT(KResourceDMA, 							"ResourceDMA");
    69 _LIT(KResourceDoubleBuffering,				"ResourceDoubleBuffering");
    70 /*@}*/
    71 const CDataWrapperBase::TEnumEntryTable CT_RDevUsbcClientData::iEUsbcEndpointResource[] = 
    72 	{ 
    73 	{ KResourceDMA,					EUsbcEndpointResourceDMA },		// Requests the use of DMA =0
    74 	{ KResourceDoubleBuffering,		EUsbcEndpointResourceDoubleBuffering },	//Requests the use of double FIFO buffering = 1	
    75 	};
    76 
    77 /**
    78  * Two phase constructor
    79  *
    80  * @leave	system wide error
    81  */
    82 CT_RDevUsbcClientData* CT_RDevUsbcClientData::NewL()
    83 	{
    84 	CT_RDevUsbcClientData*	ret=new (ELeave) CT_RDevUsbcClientData();
    85 	CleanupStack::PushL(ret);
    86 	ret->ConstructL();
    87 	CleanupStack::Pop(ret);
    88 	return ret;
    89 	}
    90 
    91 /**
    92  * Protected constructor. First phase construction
    93  */
    94 CT_RDevUsbcClientData::CT_RDevUsbcClientData()
    95 	:
    96 	iChannel(NULL),
    97 	iInterface(NULL),
    98 	iEndpointData(),
    99 	iConfiguredEndpoints(0),
   100 	iTxMaxPacketSize(0),
   101 	iCaps(),
   102 	iActiveCallback(NULL),
   103 	iBuffer(NULL),
   104     iBuffer_verify(NULL),
   105     iDeviceDescriptorSize(0)
   106 	{
   107 	
   108 	}
   109 
   110 /**
   111  * Second phase construction
   112  *
   113  * @internalComponent
   114  *
   115  * @return	N/A
   116  *
   117  * @pre		None
   118  * @post	None
   119  *
   120  * @leave	system wide error
   121  */
   122 void CT_RDevUsbcClientData::ConstructL()
   123 	{
   124 	iActiveCallback = CActiveCallback::NewL(*this);	
   125 	}
   126 
   127 /**
   128  * Public destructor
   129  */
   130 CT_RDevUsbcClientData::~CT_RDevUsbcClientData()
   131 	{		
   132 	// Empty arrays and delete objects
   133 
   134 	DestroyData();
   135 
   136 	if (iActiveCallback)
   137 		{
   138 		delete iActiveCallback;
   139 		iActiveCallback = NULL;
   140 		}
   141 	}
   142 
   143 /**
   144  *  RunL is called when an asynchronous call has completed
   145  * 
   146  *  @param aActive - a pointer to an active object
   147  *  @param aIndex - Index of the asynchronous call
   148  * 
   149  */
   150 void CT_RDevUsbcClientData::RunL(CActive* aActive, TInt aIndex)
   151 	{
   152 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::RunL"));
   153 	DecOutstanding(); // One of the async calls has completed 
   154 	TBool straySignal = EFalse; 
   155 		  
   156 	if(aActive == iActiveCallback)
   157 		{
   158 		INFO_PRINTF1(_L("Asynchronous task has completed. RunL  called"));
   159 		}		
   160 	else
   161 		{ 
   162 		ERR_PRINTF1(_L("Stray RunL signal"));
   163 		SetBlockResult(EFail);
   164 		straySignal = ETrue;		
   165 		}
   166 	
   167 	if( !straySignal ) 
   168 		{ 
   169 		TInt err = aActive->iStatus.Int(); 
   170 		if( err != KErrNone ) 
   171 			{ 
   172 			ERR_PRINTF2(_L("RunL Error %d"), err); 
   173 			SetAsyncError( aIndex, err );		
   174 			} 
   175 		else
   176 			{ 
   177 			INFO_PRINTF1(_L("RunL  completed successfully"));
   178 			}
   179 		}
   180 	INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::RunL"));
   181 	}
   182 
   183 
   184 /**
   185  * Return a pointer to the object that the data wraps
   186  *
   187  * @return	pointer to the object that the data wraps
   188  */
   189 TAny* CT_RDevUsbcClientData::GetObject()
   190 	{
   191 	return iChannel;
   192 	}
   193 
   194 /**
   195  * Returns the wrapper.
   196  * 
   197  * @return pointer to the Wrapper class
   198  */
   199 
   200 
   201 
   202 /**
   203  * Process a command read from the ini file
   204  *
   205  * @param aCommand			The command to process
   206  * @param aSection			The section in the ini containing data for the command
   207  * @param aAsyncErrorIndex	Command index for async calls to return errors to
   208  * @return					ETrue if the command is processed
   209  * @leave					System wide error
   210  */
   211 TBool CT_RDevUsbcClientData::DoCommandL(const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt /*aAsyncErrorIndex*/)
   212 	{
   213 	TBool ret=ETrue;
   214 		
   215 	if(aCommand == KCmdNewL )
   216 		{
   217 		DoCmdNewL();
   218 		}
   219 	else if(aCommand == KCmdUtilityLoadLogicalDevice)
   220 		{
   221 		DoCmdUtilityLoadLogicalDevice(aSection);
   222 		}
   223 	else if(aCommand == KCmdOpen)
   224 		{
   225 		DoCmdOpen();
   226 		}
   227 	else if(aCommand == KCmdDeviceCaps)
   228 		{
   229 		DoCmdDeviceCaps(aSection);
   230 		}	
   231 	else if(aCommand == KCmdGetDeviceDescriptorSize)
   232 		{
   233 		DoCmdGetDeviceDescriptorSize(aSection);
   234 		}
   235 	else if(aCommand == KCmdGetDeviceDescriptor)
   236 		{
   237 		DoCmdGetDeviceDescriptor(aSection);
   238 		}
   239 	else if(aCommand == KCmdSetDeviceDescriptor)
   240 		{
   241 		DoCmdSetDeviceDescriptor(aSection);
   242 		}
   243 	else if(aCommand == KCmdUtilitySetEndPoint)
   244 		{
   245 		DoCmdUtilitySetEndPointL(aSection);
   246 		}	
   247 	else if(aCommand == KCmdEndpointCaps)
   248 		{
   249 		DoCmdEndpointCaps();
   250 		}
   251 	else if(aCommand == KCmdSetInterface)
   252 		{
   253 		DoCmdSetInterface(aSection);
   254 		}	
   255 	else if(aCommand == KCmdDestructor)
   256 		{
   257 		DoCmdDestructor();
   258 		}
   259 	else
   260 		{
   261 		ERR_PRINTF1(_L("Unknown command"));
   262 		ret = EFalse;
   263 		}
   264 	
   265 	return ret;
   266 	}
   267 
   268 
   269 
   270 /**
   271  * Creante an object of RDevUsbcClient
   272  */
   273 void  CT_RDevUsbcClientData::DoCmdNewL()
   274 	{
   275 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::DoCmdNewL"));
   276 	DestroyData();
   277 	iChannel = new (ELeave) RDevUsbcClient();
   278 	INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::DoCmdNewL"));
   279 	}
   280 /**
   281  * Loads Logical Device for USB.
   282  */
   283 void CT_RDevUsbcClientData::DoCmdUtilityLoadLogicalDevice(const TTEFSectionName& aSection)
   284 	{
   285 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::DoCmdUtilityLoadLogicalDevice"));
   286 	TInt error(KErrNone);
   287 	TBool dataOk = ETrue;
   288 	TPtrC lddName;
   289 	TBufC<9> value;	
   290 	if(!GetStringFromConfig(aSection, KDriver, lddName))
   291 		{
   292 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), & KDriver);
   293 		SetBlockResult(EFail);
   294 		dataOk = EFalse;
   295 		}	
   296 	if(dataOk)
   297 		{	
   298 		value = lddName;		
   299 		INFO_PRINTF2(_L("Driver: %S"), & value);
   300 		error = User::LoadLogicalDevice(value);		
   301 		
   302 		if(error == KErrAlreadyExists)
   303 			{
   304 			INFO_PRINTF2(_L("USB - Logical device driver loaded with %d, error will be changed to KErrNone"), error);
   305 			error = KErrNone;
   306 			}
   307 		if(error != KErrNone)
   308 			{
   309 			ERR_PRINTF2(_L("USB - Could not load logical device driver. Error %d"), error);
   310 			}
   311 		else
   312 			{
   313 			INFO_PRINTF2(_L("USB - Logical device driver loaded with %d"), error);
   314 			}	
   315 		SetError(error);
   316 		}		
   317 	INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::DoCmdUtilityLoadLogicalDevice"));
   318 	}
   319 
   320 /**
   321  * Opens a USB channel 
   322  */
   323 void CT_RDevUsbcClientData::DoCmdOpen()
   324 	{
   325 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::DoCmdOpen"));
   326 	// Open USB driver channel
   327 	TInt error = iChannel->Open(USB::KUSBChannelOpenParameter);
   328 
   329 	if (error != KErrNone)
   330 		{
   331 		ERR_PRINTF2(_L("ERROR %d when opening channel"), error);
   332 		}
   333 	else
   334 		{
   335 		INFO_PRINTF2(_L("Channel open returns: %d "), error);
   336 		}
   337 
   338 	SetError(error);
   339 	INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::DoCmdOpen"));
   340 	}
   341 
   342 /**
   343  * Reads device capabilities and verifies them
   344  * @param aSection		The section in the ini containing data for the command
   345  */
   346 void CT_RDevUsbcClientData::DoCmdDeviceCaps(const TTEFSectionName& aSection)
   347 	{
   348 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::DoCmdDeviceCaps"));
   349 	TBool supportsConnect;
   350 	TBool dataOk = ETrue;
   351 	if(!GetBoolFromConfig(aSection, KSupportsConnect, supportsConnect))
   352 		{
   353 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KSupportsConnect);
   354 		SetBlockResult(EFail);
   355 		dataOk = EFalse;
   356 		}
   357 	
   358 	TBool supportsSelfPowered;
   359 	if(!GetBoolFromConfig(aSection, KSupportsSelfPowered, supportsSelfPowered))
   360 		{
   361 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KSupportsSelfPowered);
   362 		SetBlockResult(EFail);
   363 		dataOk = EFalse;
   364 		}
   365 	
   366 	TBool supportsRemoteWakeup;
   367 	if(!GetBoolFromConfig(aSection, KSupportsRemoteWakeup, supportsRemoteWakeup))
   368 		{
   369 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KSupportsRemoteWakeup);
   370 		SetBlockResult(EFail);
   371 		dataOk = EFalse;
   372 		}
   373 	if(dataOk)
   374 		{
   375 		TInt error = iChannel->DeviceCaps(iCaps);
   376 		if (error != KErrNone)
   377 			{
   378 			ERR_PRINTF2(_L("Failed to read device caps [%d]"), error);
   379 			SetError(error);
   380 			}
   381 		else
   382 			{
   383 			// Check that reported device capabilities are what they should be
   384 			INFO_PRINTF2(_L("Device supports %d endpoints"), iCaps().iTotalEndpoints );
   385 			INFO_PRINTF2(_L("Device supports Software-Connect: %s"), iCaps().iConnect ? _S("yes") : _S("no"));
   386 			if(!iCaps().iConnect && supportsConnect)
   387 				{
   388 				ERR_PRINTF1(_L("Device supports Software-Connect feature IS NOT SUPPORTED"));
   389 				SetBlockResult(EFail);
   390 				}
   391 
   392 			INFO_PRINTF2(_L("Device is Self-Powered: %s"), iCaps().iSelfPowered ? _S("yes") : _S("no"));
   393 			if(!iCaps().iSelfPowered && supportsSelfPowered)
   394 				{
   395 				ERR_PRINTF1(_L("Device is Self-Powered feature IS NOT SUPPORTED"));
   396 				SetBlockResult(EFail);
   397 				}
   398 
   399 			INFO_PRINTF2(_L("Device supports Remote-Wakeup:	%s"),iCaps().iRemoteWakeup ? _S("yes") : _S("no"));
   400 			if(!iCaps().iRemoteWakeup && supportsRemoteWakeup)
   401 				{
   402 				ERR_PRINTF1(_L("Device supports Remote-Wakeup feature IS NOT SUPPORTED"));
   403 				SetBlockResult(EFail);
   404 				}
   405 			}		
   406 		}
   407 	INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::DoCmdDeviceCaps"));
   408 	}
   409 
   410 /**
   411  * Reads device endpoint capabilities and prints them
   412  */
   413 void CT_RDevUsbcClientData::DoCmdEndpointCaps()
   414 	{
   415 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::DoCmdEndpointCaps"));
   416 	TPtr8 endpointData(reinterpret_cast<TUint8*>(iEndpointData), sizeof(iEndpointData), sizeof(iEndpointData));
   417 	TInt error = iChannel->EndpointCaps(endpointData);
   418 
   419 	if (error != KErrNone)
   420 		{
   421 		ERR_PRINTF2(_L("Failed to read endpoint caps [%d]"), error);
   422 		SetError(error);
   423 		}
   424 	else
   425 		{
   426 		INFO_PRINTF1(_L("User endpoint capabilities:"));
   427 		for (TInt i = 0; i < iCaps().iTotalEndpoints; i++)
   428 			{
   429 			const TUsbcEndpointCaps* caps = &iEndpointData[i].iCaps;
   430 			INFO_PRINTF4(_L("Endpoint %02d: MaxPackeSize=%s TypeDirM = 0x%08x "),
   431 						i + 1,
   432 						(caps->iSizes & KUsbEpSizeCont ) ? _S("Continuous") :
   433 						((caps->iSizes & KUsbEpSize8) ? _S("8 bytes") :
   434 						((caps->iSizes & KUsbEpSize16) ? _S("16 bytes") :
   435 						((caps->iSizes & KUsbEpSize32) ? _S("32 bytes") :
   436 						((caps->iSizes & KUsbEpSize64) ? _S("64 bytes") :
   437 						((caps->iSizes & KUsbEpSize128) ? _S("128 bytes") :
   438 						((caps->iSizes & KUsbEpSize256) ? _S("256 bytes") :
   439 						((caps->iSizes & KUsbEpSize512) ? _S("512 bytes") :
   440 						((caps->iSizes & KUsbEpSize1023) ? _S("1023 bytes") :
   441 						((caps->iSizes & KUsbEpSize1024) ? _S("1024 bytes") :
   442 						_S("Unknown")))))))))),
   443 						caps->iTypesAndDir);
   444 			}
   445 		}
   446 	INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::DoCmdEndpointCaps"));
   447 	}
   448 
   449 
   450 /*
   451  * Inits USB Device Descriptors - Get the device descriptor size
   452  */
   453 
   454 void CT_RDevUsbcClientData::DoCmdGetDeviceDescriptorSize(const TTEFSectionName& aSection)
   455 	{	
   456 	// Read the size of the descriptors and create buffer to store them.
   457 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::DoCmdGetDeviceDescriptorSize"));
   458     TInt expected;
   459     if(!GetIntFromConfig(aSection, KEXPECTED, expected))
   460 		{
   461 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KEXPECTED);
   462 		SetBlockResult(EFail);
   463 		}
   464     else
   465         {
   466         iDeviceDescriptorSize = 0;
   467     	INFO_PRINTF1(_L("Initializing device descriptors"));
   468         INFO_PRINTF1(_L("void CT_RDevUsbcClientData::GetDeviceDescriptorSize() BEGIN"));
   469     	iChannel->GetDeviceDescriptorSize(iDeviceDescriptorSize);	
   470         
   471         if(expected != iDeviceDescriptorSize)
   472             {
   473             ERR_PRINTF3(_L("Expected size != actual size, expected = %d, actual = %d "),expected, iDeviceDescriptorSize);
   474             SetBlockResult(EFail);
   475             }
   476         }
   477         // initialize buffer which used to verify.
   478         
   479             
   480     INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::DoCmdGetDeviceDescriptorSize"));
   481 	}
   482 
   483 /**
   484  * Inits USB Device Descriptors - Get device descriptor
   485  */
   486 
   487 void CT_RDevUsbcClientData::DoCmdGetDeviceDescriptor(const TTEFSectionName& aSection)
   488 	{
   489 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::DoCmdGetDeviceDescriptor"));
   490     
   491     //initialize descriptor buffer.
   492     TRAPD(error, iBuffer = HBufC8::NewL(iDeviceDescriptorSize));
   493     if(error != KErrNone || !iBuffer)
   494     	{
   495     	ERR_PRINTF2(_L("Failed to initialize device descriptor [%d]"),error);
   496     	SetError(error);
   497     	}
   498     TRAPD(error2, iBuffer_verify = HBufC8::NewL(iDeviceDescriptorSize));
   499     if(error2 != KErrNone || !iBuffer_verify)
   500     	{
   501     	ERR_PRINTF2(_L("Failed to initialize device descriptor [%d]"),error2);
   502     	SetError(error2);
   503     	}  
   504     
   505 	TInt err;	
   506 	TPtr8 devDescrPtr = iBuffer->Des();
   507 	// Read the current descriptors.	
   508 	err = iChannel->GetDeviceDescriptor(devDescrPtr);
   509 	if (err != KErrNone)
   510 		{
   511 		ERR_PRINTF2(_L("Failed to get the device descriptor [%d]"),err);
   512 		SetError(err);
   513 		}   
   514         
   515     // Read descriptors from ini and compare.
   516     TInt USBVersionLow;
   517 	TBool dataOk = ETrue;
   518 	if(!GetIntFromConfig(aSection, KUSBVersionLow, USBVersionLow))
   519 		{
   520 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KUSBVersionLow);
   521 		SetBlockResult(EFail);
   522 		dataOk = EFalse;
   523 		}
   524 	TInt USBVersionHigh;
   525 	if(!GetIntFromConfig(aSection, KUSBVersionHigh, USBVersionHigh))
   526 		{
   527 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KUSBVersionHigh);
   528 		SetBlockResult(EFail);
   529 		dataOk = EFalse;
   530 		}
   531 	TInt VendorIDLow;
   532 	if(!GetIntFromConfig(aSection, KVendorIDLow, VendorIDLow))
   533 		{
   534 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KVendorIDLow);
   535 		SetBlockResult(EFail);
   536 		dataOk = EFalse;
   537 		}
   538 	TInt VendorIDHigh;
   539 	if(!GetIntFromConfig(aSection, KVendorIDHigh, VendorIDHigh))
   540 		{
   541 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KVendorIDHigh);
   542 		SetBlockResult(EFail);
   543 		dataOk = EFalse;
   544 		}
   545 	// product ID for test device
   546 	TInt ProductIDLow;
   547 	if(!GetIntFromConfig(aSection, KProductIDLow, ProductIDLow))
   548 		{
   549 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KProductIDLow);
   550 		SetBlockResult(EFail);
   551 		dataOk = EFalse;
   552 		}
   553 	TInt ProductIDHigh;
   554 	if(!GetIntFromConfig(aSection, KProductIDHigh, ProductIDHigh))
   555 		{
   556 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KProductIDHigh);
   557 		SetBlockResult(EFail);
   558 		dataOk = EFalse;
   559 		}
   560 	// release number
   561 	TInt ReleaseNumberLow;
   562 	if(!GetIntFromConfig(aSection, KReleaseNumberLow, ReleaseNumberLow))
   563 		{
   564 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KReleaseNumberLow);
   565 		SetBlockResult(EFail);
   566 		dataOk = EFalse;
   567 		}
   568 	TInt ReleaseNumberHigh;
   569 	if(!GetIntFromConfig(aSection, KReleaseNumberHigh, ReleaseNumberHigh))
   570 		{
   571 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KReleaseNumberHigh);
   572 		SetBlockResult(EFail);
   573 		dataOk = EFalse;
   574 		}			
   575 	// Device descriptor data is presented in bytes (Little endian)
   576 	// defined in the .ini file
   577 	// The following presents numeric values device uses to introduce
   578 	// herself to USB host in enumeration process
   579 	if(dataOk)
   580 		{
   581 		TPtr8 devDescrPtr_verify = iBuffer_verify->Des();
   582 		err = iChannel->GetDeviceDescriptor(devDescrPtr_verify);
   583 		if (err != KErrNone)
   584 			{
   585 			ERR_PRINTF2(_L("Failed to get the device descriptor [%d]"),err);
   586 			SetError(err);
   587 			}   
   588 		// USB version
   589 		devDescrPtr_verify[USB::KSpecOffset] = USBVersionLow;
   590 		devDescrPtr_verify[USB::KSpecOffset + 1] = USBVersionHigh;
   591 
   592 		devDescrPtr_verify[USB::KVendorOffset] = VendorIDLow;
   593 		devDescrPtr_verify[USB::KVendorOffset + 1] = VendorIDHigh;
   594 
   595 		// product ID for test device
   596 		devDescrPtr_verify[USB::KProductIDOffset] = ProductIDLow;
   597 		devDescrPtr_verify[USB::KProductIDOffset + 1] = ProductIDHigh;
   598 
   599 		// Set release number
   600 		devDescrPtr_verify[USB::KReleaseOffset] = ReleaseNumberLow;
   601 		devDescrPtr_verify[USB::KReleaseOffset + 1] = ReleaseNumberHigh;
   602 
   603         if(devDescrPtr != devDescrPtr_verify)
   604             {
   605             ERR_PRINTF1(_L("Expected DeviceDescriptor is not the same as actual. "));
   606             SetBlockResult(EFail);
   607             }
   608 		}	
   609         
   610         delete iBuffer;
   611 		iBuffer = NULL;
   612         delete iBuffer_verify;
   613 		iBuffer_verify = NULL;
   614 
   615 	INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::DoCmdGetDeviceDescriptor"));
   616 	}
   617 
   618 /**
   619  * Inits USB Device Descriptors - Set device descriptor
   620  * @param aSection - The section in the ini containing data for the command
   621  */
   622 void CT_RDevUsbcClientData::DoCmdSetDeviceDescriptor(const TTEFSectionName& aSection)
   623 	{
   624 	// USB version
   625 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::DoCmdSetDeviceDescriptor"));
   626     
   627     iDeviceDescriptorSize = 0;
   628     INFO_PRINTF1(_L("Initializing device descriptors"));
   629     iChannel->GetDeviceDescriptorSize(iDeviceDescriptorSize);	
   630     INFO_PRINTF2(_L("Device descriptor size = %d"), iDeviceDescriptorSize);
   631     
   632     //initialize descriptor buffer.
   633     TRAPD(err, iBuffer = HBufC8::NewL(iDeviceDescriptorSize));
   634     if(err != KErrNone || !iBuffer)
   635     	{
   636     	ERR_PRINTF2(_L("Failed to initialize device descriptor [%d]"),err);
   637     	SetError(err);
   638     	}
   639     
   640 	TInt error;
   641 	TInt USBVersionLow;
   642 	TBool dataOk = ETrue;
   643 	if(!GetIntFromConfig(aSection, KUSBVersionLow, USBVersionLow))
   644 		{
   645 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KUSBVersionLow);
   646 		SetBlockResult(EFail);
   647 		dataOk = EFalse;
   648 		}
   649 	TInt USBVersionHigh;
   650 	if(!GetIntFromConfig(aSection, KUSBVersionHigh, USBVersionHigh))
   651 		{
   652 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KUSBVersionHigh);
   653 		SetBlockResult(EFail);
   654 		dataOk = EFalse;
   655 		}
   656 
   657 	TInt VendorIDLow;
   658 	if(!GetIntFromConfig(aSection, KVendorIDLow, VendorIDLow))
   659 		{
   660 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KVendorIDLow);
   661 		SetBlockResult(EFail);
   662 		dataOk = EFalse;
   663 		}
   664 	TInt VendorIDHigh;
   665 	if(!GetIntFromConfig(aSection, KVendorIDHigh, VendorIDHigh))
   666 		{
   667 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KVendorIDHigh);
   668 		SetBlockResult(EFail);
   669 		dataOk = EFalse;
   670 		}
   671 	// product ID for test device
   672 	TInt ProductIDLow;
   673 	if(!GetIntFromConfig(aSection, KProductIDLow, ProductIDLow))
   674 		{
   675 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KProductIDLow);
   676 		SetBlockResult(EFail);
   677 		dataOk = EFalse;
   678 		}
   679 	TInt ProductIDHigh;
   680 	if(!GetIntFromConfig(aSection, KProductIDHigh, ProductIDHigh))
   681 		{
   682 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KProductIDHigh);
   683 		SetBlockResult(EFail);
   684 		dataOk = EFalse;
   685 		}
   686 	// release number
   687 	TInt ReleaseNumberLow;
   688 	if(!GetIntFromConfig(aSection, KReleaseNumberLow, ReleaseNumberLow))
   689 		{
   690 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KReleaseNumberLow);
   691 		SetBlockResult(EFail);
   692 		dataOk = EFalse;
   693 		}
   694 	TInt ReleaseNumberHigh;
   695 	if(!GetIntFromConfig(aSection, KReleaseNumberHigh, ReleaseNumberHigh))
   696 		{
   697 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KReleaseNumberHigh);
   698 		SetBlockResult(EFail);
   699 		dataOk = EFalse;
   700 		}			
   701 	// Device descriptor data is presented in bytes (Little endian)
   702 	// defined in the .ini file
   703 	// The following presents numeric values device uses to introduce
   704 	// herself to USB host in enumeration process
   705 	if(dataOk)
   706 		{
   707 		TPtr8 devDescrPtr = iBuffer->Des();
   708 		devDescrPtr.FillZ(devDescrPtr.MaxLength());
   709 
   710 		// USB version
   711 		devDescrPtr[USB::KSpecOffset] = USBVersionLow;
   712 		devDescrPtr[USB::KSpecOffset + 1] = USBVersionHigh;
   713 
   714 		devDescrPtr[USB::KVendorOffset] = VendorIDLow;
   715 		devDescrPtr[USB::KVendorOffset + 1] = VendorIDHigh;
   716 
   717 		// product ID for test device
   718 		devDescrPtr[USB::KProductIDOffset] = ProductIDLow;
   719 		devDescrPtr[USB::KProductIDOffset + 1] = ProductIDHigh;
   720 
   721 		// Set release number
   722 		devDescrPtr[USB::KReleaseOffset] = ReleaseNumberLow;
   723 		devDescrPtr[USB::KReleaseOffset + 1] = ReleaseNumberHigh;
   724 
   725 		INFO_PRINTF1(_L("Setting device descriptors"));
   726 		// Store the descriptors.
   727 		error = iChannel->SetDeviceDescriptor(devDescrPtr);
   728 		if (error != KErrNone)
   729 			{
   730 			ERR_PRINTF2(_L("Failed to set the device descriptor [%d]"),error);
   731 			delete iBuffer;
   732 			SetError(error);
   733 			}
   734 		else
   735 			{
   736 			delete iBuffer;
   737 			iBuffer = NULL;
   738 			}
   739 		
   740 		INFO_PRINTF1(_L("Device descriptors set"));
   741 		}	
   742 	INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::DoCmdSetDeviceDescriptor"));
   743 	}
   744 
   745 /**
   746  * Sets USB EndPoints
   747  * @param aSection - The section in the ini containing data for the command
   748  */
   749 
   750 void CT_RDevUsbcClientData::DoCmdUtilitySetEndPointL(const TTEFSectionName& aSection)
   751 	{
   752 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::DoCmdUtilitySetEndPointL"));
   753 	TPtrC16 mode;
   754 	TBool dataOk = ETrue;
   755 	if(!GetStringFromConfig(aSection, KMode, mode))
   756 		{
   757 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KMode);
   758 		SetBlockResult(EFail);
   759 		dataOk = EFalse;
   760 		}
   761 	TPtrC16 direction;
   762 	if(!GetStringFromConfig(aSection, KDirection, direction))
   763 		{
   764 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KDirection);
   765 		SetBlockResult(EFail);
   766 		dataOk = EFalse;
   767 		}
   768 	if(dataOk)
   769 		{
   770 		TInt error(KErrNone);	
   771 		if(mode == USB::KEPBulk)
   772 			{
   773 			INFO_PRINTF1(_L("Selected mode is KEPBulk"));
   774 			if(direction == USB::KEPIn)
   775 				{
   776 				error = SetEndpoint(KUsbEpTypeBulk, KUsbEpDirIn);
   777 				if (error != KErrNone)
   778 					{
   779 					ERR_PRINTF2(_L("Failed to set end point direction to KEPIn with error [%d]"), error);
   780 					SetBlockResult(EFail);					
   781 					}
   782 				}
   783 			else if(direction == USB::KEPOut)
   784 				{
   785 				error = SetEndpoint(KUsbEpTypeBulk, KUsbEpDirOut);
   786 				if (error != KErrNone)
   787 					{
   788 					ERR_PRINTF2(_L("Failed to set end point direction to KEPOut with error [%d]"), error);
   789 					SetBlockResult(EFail);
   790 					}
   791 				}
   792 			else
   793 				{
   794 				ERR_PRINTF2(_L("USB - INCORRECT direction %S"), direction.AllocL());
   795 				SetBlockResult(EFail);
   796 				}
   797 			}
   798 		else if(mode == USB::KEPInterrupt)
   799 			{
   800 			INFO_PRINTF1(_L("Selected mode is KEPInterrupt"));
   801 			if(direction == USB::KEPIn)
   802 				{
   803 				error = SetEndpoint(KUsbEpTypeInterrupt, KUsbEpDirIn);
   804 				if (error != KErrNone)
   805 					{
   806 					ERR_PRINTF2(_L("Failed to set end point direction to KEPIn with error [%d]"), error);
   807 					SetBlockResult(EFail);
   808 					}
   809 				}
   810 			else if(direction == USB::KEPOut)
   811 				{
   812 				error = SetEndpoint(KUsbEpTypeInterrupt, KUsbEpDirOut);
   813 				if (error != KErrNone)
   814 					{
   815 					ERR_PRINTF2(_L("Failed to set end point direction to KEPOut with error [%d]"), error);
   816 					SetBlockResult(EFail);
   817 					}
   818 				}
   819 			else
   820 				{
   821 				ERR_PRINTF2(_L("USB - INCORRECT direction %S"), direction.AllocL());
   822 				SetBlockResult(EFail);
   823 				}
   824 			}
   825 		else
   826 			{
   827 			ERR_PRINTF2(_L("USB - Mode not supported %S"), mode.AllocL());
   828 			SetBlockResult(EFail);
   829 			}
   830 		}			
   831 	INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::DoCmdUtilitySetEndPointL"));
   832 	}
   833 
   834 
   835 /**
   836  * Sets a new endpoint for interface handled by this channel
   837  * @param aEndpointType - USB endpoint type
   838  * @param aEndpointDirection - USB endpoint direction	
   839  */
   840 TInt CT_RDevUsbcClientData::SetEndpoint(TUint aEndpointType, TUint aEndpointDirection)
   841 	{
   842 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::SetEndpoint"));
   843 	if(iConfiguredEndpoints == KMaxEndpointsPerClient)
   844 		{
   845 		ERR_PRINTF2(_L("Maximum number of endpoints [%d] for clients reached "), KMaxEndpointsPerClient);
   846 		SetBlockResult(EFail);
   847 		return KErrArgument;
   848 		}
   849 
   850 	// search for the endpoints that match the settings user has given.
   851 	for (TInt i = 0; i < iCaps().iTotalEndpoints; ++i)
   852 		{
   853 		const TUsbcEndpointCaps* caps = &iEndpointData[i].iCaps;
   854 		const TInt maximumPacketSize = caps->MaxPacketSize();
   855 
   856 		// Check if endpoint in question matches with parameters we want to use for configuring an endpoint
   857 		if ( (caps->iTypesAndDir & (aEndpointType | aEndpointDirection)) == (aEndpointType | aEndpointDirection) )
   858 			{
   859 			INFO_PRINTF3(_L("Configuring %s endpoint with direction %s"),
   860 				(aEndpointType == KUsbEpTypeControl) ? _S("Control") :
   861 				((aEndpointType == KUsbEpTypeIsochronous) ? _S("Isochronous") :
   862 				((aEndpointType == KUsbEpTypeBulk) ? _S("Bulk") :
   863 				((aEndpointType == KUsbEpTypeInterrupt) ? _S("Interrupt") :
   864 				_S("Unknown")))),
   865 				(aEndpointDirection == KUsbEpDirIn) ? _S("In") :
   866 				((aEndpointDirection == KUsbEpDirOut) ? _S("Out") :
   867 				((aEndpointDirection == KUsbEpDirBidirect) ? _S("Bidirectional") :
   868 				_S("Unknown")))
   869 				);
   870 
   871 			iInterface().iEndpointData[iConfiguredEndpoints].iType = aEndpointType;
   872 			iInterface().iEndpointData[iConfiguredEndpoints].iDir = aEndpointDirection;
   873 			iInterface().iEndpointData[iConfiguredEndpoints].iSize = maximumPacketSize;
   874 			iTxMaxPacketSize = maximumPacketSize;
   875 
   876 			iConfiguredEndpoints++;
   877 
   878 			break;
   879 			}
   880 		}
   881 
   882 	INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::SetEndpoint"));
   883 	return KErrNone;
   884 	}
   885 
   886 
   887 
   888 /**
   889  * Sets interface with endpoints configured with CT_RDevUsbcClientData::SetEndpoint
   890  * Note that endpoints must be set before calling this method
   891  * 
   892  * @param aBandwithwidthBufferingMask TInt contains IN and OUT bandwidth buffering masks combined 
   893  *									 using binary OR operation
   894  *			NOTE: IT IS TEST CASE SCRIPTERS RESPONSIBILITY TO CHECK VALID (or invalid for negative cases) MASKS
   895  */
   896 void CT_RDevUsbcClientData::DoCmdSetInterface(const TTEFSectionName& aSection)
   897 	{
   898 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::DoCmdSetInterface"));
   899 	TInt bandwidthBufferingMask;
   900 	TBool dataOk = ETrue;
   901 	if(!GetIntFromConfig(aSection, KBufferingMask, bandwidthBufferingMask))
   902 		{
   903 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KBufferingMask);
   904 		SetBlockResult(EFail);
   905 		dataOk = EFalse;
   906 		}
   907 	if(dataOk)
   908 		{
   909 		INFO_PRINTF2(_L("bandwidthBufferingMask %d"), bandwidthBufferingMask);
   910 		INFO_PRINTF2(_L("Configuring the interface settings with %d endpoints"), iConfiguredEndpoints);
   911 
   912 		if( iConfiguredEndpoints == 0 )
   913 			{
   914 			ERR_PRINTF1(_L("No endpoints have been set! Set endpoints before configuring interface"));
   915 			SetBlockResult(EFail);
   916 			}
   917 		else
   918 			{
   919 			HBufC16* allocatedName = 0;
   920 			TRAPD(error, allocatedName = interfaceName().AllocL());
   921 		    if (error != KErrNone)
   922 		        {
   923 		        ERR_PRINTF2(_L("AllocL failed [%d]"), error);
   924 		        SetBlockResult(EFail);
   925 		        }
   926 		    else
   927 		    	{
   928 		    	iInterface().iString = allocatedName;
   929 				iInterface().iTotalEndpointsUsed = iConfiguredEndpoints;
   930 				iInterface().iClass.iClassNum = 0xff;
   931 				iInterface().iClass.iSubClassNum = 0xff;
   932 				iInterface().iClass.iProtocolNum = 0xff;
   933 				INFO_PRINTF2(_L("Using bandwidth buffering mask 0x%08x"), bandwidthBufferingMask);
   934 
   935 				error = iChannel->SetInterface(0, iInterface, bandwidthBufferingMask);
   936 
   937 				delete allocatedName;
   938 				allocatedName = NULL;
   939 				
   940 				if (error != KErrNone)
   941 					{
   942 					ERR_PRINTF2(_L("Failed to set the interface [%d]"), error);
   943 					SetError(error);
   944 					}
   945 				INFO_PRINTF2(_L("SetInterface returns [%d]"), error);
   946 		    	}			
   947 			}	    
   948 		}
   949 	INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::DoCmdSetInterface"));
   950 	}
   951 
   952 
   953 
   954 
   955 /**
   956  * Tell test class to stop waiting 
   957  * @param result from callback
   958  */
   959 void CT_RDevUsbcClientData::Signal(TInt aResult)
   960 	{
   961 	INFO_PRINTF2(_L("Signal %d"),aResult);
   962 	}
   963 
   964 
   965 /**
   966  * Destroyer
   967  */
   968 
   969 void  CT_RDevUsbcClientData::DoCmdDestructor()
   970 	{
   971 	INFO_PRINTF1(_L("*START*CT_RDevUsbcClientData::DoCmdDestructor"));
   972 	DestroyData();
   973 	INFO_PRINTF1(_L("*END*CT_RDevUsbcClientData::DoCmdDestructor"));
   974 	}
   975 
   976 void CT_RDevUsbcClientData::DestroyData()
   977 	{
   978 	if(iChannel)
   979 		{
   980 		iChannel->Close();
   981 		delete iChannel;
   982 		iChannel = NULL;
   983 		}
   984 	}