usbmgmt/usbmgrtest/t_usbmanager_suite/Stub3CC/src/Stub3CC.cpp
changeset 0 c9bc50fca66e
child 15 f92a4f87e424
equal deleted inserted replaced
-1:000000000000 0:c9bc50fca66e
       
     1 /*
       
     2 * Copyright (c) 1997-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 * Adheres to the UsbMan USB Class Controller API and talks to C32
       
    16 * to manage the stub3.CSY that is used to provide a virtual
       
    17 * serial port service to clients
       
    18 *
       
    19 */
       
    20 
       
    21 /**
       
    22  @file
       
    23 */
       
    24 
       
    25 #include "Stub3CC.h"
       
    26 #include <usb_std.h>
       
    27 #include <es_ini.h>
       
    28 #include <d32usbc.h>
       
    29 #include <usb/usblogger.h>
       
    30 
       
    31 #ifdef __FLOG_ACTIVE
       
    32 _LIT8(KLogComponent, "STUB3CC");
       
    33 #endif
       
    34 
       
    35 
       
    36 #include "usbmaninternalconstants.h"
       
    37  
       
    38 
       
    39 // Panic category 
       
    40 _LIT( Kstub3CcPanicCategory, "Usbstub3Cc" );
       
    41 
       
    42 
       
    43 /**
       
    44  * Panic codes for the USB stub3 Class Controller.
       
    45  */
       
    46 enum Tstub3CcPanic
       
    47 	{
       
    48 	/** Class called while in an illegal state */
       
    49 	EBadApiCall = 0,
       
    50 	/** Asynchronous function called (not needed, as all requests complete synchronously) */
       
    51 	EUnusedFunction = 1,
       
    52 	/** Error reading ini file. */
       
    53 	EPanicBadIniFile = 2,		
       
    54 	/** Bad value for the iNumberOfstub3Functions member.*/
       
    55 	EPanicBadNumberOfstub3Functions = 3,
       
    56 	
       
    57 	EPanicUnexpectedStatus,
       
    58 	EPanicUnexpectedState
       
    59 
       
    60 	};
       
    61 	
       
    62 _LIT16(KIfcName, "SubCC 1 Interface");
       
    63 const TInt KMaxPacketTypeInterrupt = 64;	
       
    64 const TInt KPollInterval  = 128; 
       
    65 
       
    66 
       
    67 
       
    68 
       
    69 /**
       
    70  * Constructs a CUsbstub3ClassController object
       
    71  *
       
    72  * @param	aOwner	USB Device that owns and manages the class
       
    73  *
       
    74  * @return	A new CUsbstub3ClassController object
       
    75  */
       
    76 CUsbstub3ClassController* CUsbstub3ClassController::NewL(
       
    77 	MUsbClassControllerNotify& aOwner)
       
    78 	{
       
    79 	CUsbstub3ClassController* r = new (ELeave) CUsbstub3ClassController(aOwner);
       
    80 	CleanupStack::PushL(r);
       
    81 	r->ConstructL();
       
    82 	CleanupStack::Pop();
       
    83 	return r;
       
    84 	}
       
    85 
       
    86 /**
       
    87  * Destructor
       
    88  */
       
    89 CUsbstub3ClassController::~CUsbstub3ClassController()
       
    90 	{
       
    91 	Cancel();
       
    92 
       
    93 	iTimer.Close();
       
    94 
       
    95 #ifndef __WINS__	
       
    96 	iLdd.Close();
       
    97 #endif
       
    98 
       
    99 	}
       
   100 
       
   101 
       
   102 
       
   103 /**
       
   104  * Constructor.
       
   105  *
       
   106  * @param	aOwner	USB Device that owns and manages the class
       
   107  */
       
   108 CUsbstub3ClassController::CUsbstub3ClassController(
       
   109 	MUsbClassControllerNotify& aOwner)
       
   110 	: CUsbClassControllerPlugIn(aOwner, Kstub3StartupPriority),
       
   111 	iStartDelay(Kstub3CCDefaultDelay),
       
   112 	iStopDelay(Kstub3CCDefaultDelay),
       
   113 	iFailToStart(EFalse),
       
   114 	iFailToStop(EFalse)	
       
   115 
       
   116 	{
       
   117 	iTimer.CreateLocal();
       
   118 	}
       
   119 
       
   120 
       
   121 
       
   122 /**
       
   123  * 2nd Phase Construction.
       
   124  */
       
   125 void CUsbstub3ClassController::ConstructL()
       
   126 	{
       
   127 	//read INI file
       
   128 	TInt ret;
       
   129 	CESockIniData* ini = 0;
       
   130 	_LIT(KIniFile, "c:\\testdata\\config\\stub3cc.ini");
       
   131 	TRAP(ret, ini=CESockIniData::NewL(KIniFile));	
       
   132 	if(ret!=KErrNone)		
       
   133 		return;	
       
   134 
       
   135 	CleanupStack::PushL(ini);
       
   136 	
       
   137 	TInt val;
       
   138 	if ((ini->FindVar(_L("0x10203289"),_L("StartDelay"), val)))
       
   139 		{
       
   140 		iStartDelay = val;
       
   141 		}
       
   142 	if ((ini->FindVar(_L("0x10203289"),_L("StopDelay"), val)))
       
   143 		{
       
   144 		iStopDelay = val;
       
   145 		}
       
   146 	if ((ini->FindVar(_L("0x10203289"),_L("FailToStart"), val)) && val!=0)
       
   147 		{
       
   148 		iFailToStart = ETrue;
       
   149 		}
       
   150 	if ((ini->FindVar(_L("0x10203289"),_L("FailToStop"), val)) && val!=0 )
       
   151 		{
       
   152 		iFailToStop = ETrue;
       
   153 		}
       
   154 	CleanupStack::PopAndDestroy(ini);						
       
   155 	}
       
   156 
       
   157 /**
       
   158  * Called by UsbMan when it wants to start the USB stub3 class. This always
       
   159  * completes immediately.
       
   160  *
       
   161  * @param aStatus The caller's request status, filled in with an error code
       
   162  */
       
   163 void CUsbstub3ClassController::Start(TRequestStatus& aStatus)
       
   164 	{
       
   165 	LOG_FUNC
       
   166 
       
   167 	aStatus = KRequestPending;
       
   168 	iReportStatus = &aStatus;
       
   169 	//If we are already started then just complete the request.
       
   170 	if (iState == EUsbServiceStarted)
       
   171 		{
       
   172 		User::RequestComplete(iReportStatus, KErrNone);
       
   173 		return;
       
   174 		}
       
   175 
       
   176 	if (iFailToStart)
       
   177 		{
       
   178 		User::RequestComplete(iReportStatus, KErrGeneral);
       
   179 		return;
       
   180 		}
       
   181 	
       
   182 	iState = EUsbServiceStarting;
       
   183 	
       
   184 #ifndef __WINS__	
       
   185 	TInt ret = iLdd.Open(0);
       
   186 	LOGTEXT2(_L8("Open LDD, ret=%d"), ret);
       
   187 	ret = SetUpInterface();
       
   188 	LOGTEXT2(_L8("SetUpInterface(), ret=%d"), ret);
       
   189 #endif	
       
   190 
       
   191 
       
   192 	iTimer.After(iStatus, iStartDelay*1000);  //convert from usec to msec
       
   193 	SetActive();
       
   194 	}
       
   195 
       
   196 /**
       
   197  * Called by UsbMan when it wants to stop the USB stub3 class.
       
   198  *
       
   199  * @param aStatus The caller's request status: always set to KErrNone
       
   200  */
       
   201 void CUsbstub3ClassController::Stop(TRequestStatus& aStatus)
       
   202 	{
       
   203 	LOG_FUNC
       
   204 
       
   205 	aStatus = KRequestPending;
       
   206 	iReportStatus = &aStatus;
       
   207 	//If we are already idle then just complete the request.
       
   208 	if (iState == EUsbServiceIdle)
       
   209 		{
       
   210 		User::RequestComplete(iReportStatus, KErrNone);
       
   211 		return;
       
   212 		}
       
   213 
       
   214 	if (iFailToStop)
       
   215 		{
       
   216 		User::RequestComplete(iReportStatus, KErrGeneral);
       
   217 		return;
       
   218 		}
       
   219 
       
   220 	iState = EUsbServiceStopping;
       
   221 
       
   222 #ifndef __WINS__	
       
   223 	iLdd.Close();
       
   224 #endif
       
   225 
       
   226 	
       
   227 	iTimer.After(iStatus, iStopDelay*1000);  //convert from usec to msec
       
   228 	SetActive();
       
   229 	}
       
   230 
       
   231 /**
       
   232  * Gets information about the descriptor which this class provides.
       
   233  *
       
   234  * @param aDescriptorInfo Descriptor info structure filled in by this function
       
   235  */
       
   236 void CUsbstub3ClassController::GetDescriptorInfo(TUsbDescriptor& aDescriptorInfo) const
       
   237 	{
       
   238 	LOG_FUNC
       
   239 
       
   240 	aDescriptorInfo.iLength = Kstub3DescriptorLength;
       
   241 	aDescriptorInfo.iNumInterfaces = Kstub3NumberOfInterfacesPerstub3Function;
       
   242 	}
       
   243 
       
   244 
       
   245 /**
       
   246  * Standard active object RunL. 
       
   247  */
       
   248 void CUsbstub3ClassController::RunL()
       
   249 	{
       
   250 	LOG_FUNC
       
   251 
       
   252 	__ASSERT_DEBUG( iStatus == KErrNone, _USB_PANIC(Kstub3CcPanicCategory, EPanicUnexpectedStatus) );
       
   253 	switch (iState)
       
   254 		{
       
   255 		case EUsbServiceStarting:
       
   256 			iState = EUsbServiceStarted;
       
   257 			break;
       
   258 		case EUsbServiceStopping:
       
   259 			iState = EUsbServiceIdle;
       
   260 			break;
       
   261 		default:	
       
   262 			_USB_PANIC(Kstub3CcPanicCategory, EPanicUnexpectedState);
       
   263 		}
       
   264 	*iReportStatus = KErrNone;	
       
   265 	User::RequestComplete(iReportStatus, iStatus.Int());	
       
   266 	}
       
   267 
       
   268 /**
       
   269  * Standard active object cancellation function. Never called because this
       
   270  * class has no asynchronous requests.
       
   271  */
       
   272 void CUsbstub3ClassController::DoCancel()
       
   273 	{
       
   274 
       
   275 	if (IsActive())
       
   276 		{
       
   277 		iTimer.Cancel();	
       
   278 		}
       
   279 	switch (iState)
       
   280 	{
       
   281 		case EUsbServiceStarting:
       
   282 			iState = EUsbServiceIdle;
       
   283 			break;
       
   284 		case EUsbServiceStopping:
       
   285 			iState = EUsbServiceStarted;
       
   286 			break;
       
   287 		default:	
       
   288 			_USB_PANIC(Kstub3CcPanicCategory, EPanicUnexpectedState);
       
   289 	}
       
   290 	*iReportStatus = KErrNone;		
       
   291 	User::RequestComplete(iReportStatus, KErrCancel);	
       
   292 	}
       
   293 
       
   294 /**
       
   295  * Standard active object error function. Never called because this class has
       
   296  * no asynchronous requests, and hence its RunL is never called.
       
   297  *
       
   298  * @param aError The error code (unused)
       
   299  * @return Always KErrNone to avoid an active scheduler panic
       
   300  */
       
   301 TInt CUsbstub3ClassController::RunError(TInt /*aError*/)
       
   302 	{
       
   303 	__ASSERT_DEBUG( EFalse, _USB_PANIC(Kstub3CcPanicCategory, EUnusedFunction) );
       
   304 	return KErrNone;
       
   305 	}
       
   306 
       
   307 TInt CUsbstub3ClassController::SetUpInterface()
       
   308 /**
       
   309  * Set up the interface for use. This involves finding a "Interrupt IN" 
       
   310  * endpoint and, if found, configuring the interface.
       
   311  */
       
   312 	{
       
   313 	LOG_FUNC
       
   314 
       
   315 	TUsbDeviceCaps dCaps;
       
   316 	TInt ret = iLdd.DeviceCaps(dCaps);
       
   317 	LOGTEXT(_L8("\tchecking result of DeviceCaps"));
       
   318 	if ( ret )
       
   319 		{
       
   320 		LOGTEXT2(_L8("<<CCdcControlInterface::SetUpInterface ret=%d"), ret);
       
   321 		return ret;
       
   322 		}
       
   323 
       
   324 	const TUint KRequiredNumberOfEndpoints = 1; // in addition to endpoint 0.
       
   325 
       
   326 	const TUint totalEndpoints = static_cast<TUint>(dCaps().iTotalEndpoints);
       
   327 	LOGTEXT2(_L8("\tiTotalEndpoints = %d"), totalEndpoints);
       
   328 	if ( totalEndpoints < KRequiredNumberOfEndpoints )
       
   329 		{
       
   330 		LOGTEXT2(_L8("<<CCdcControlInterface::SetUpInterface ret=%d"), 
       
   331 			KErrGeneral);
       
   332 		return KErrGeneral;
       
   333 		}
       
   334 	
       
   335 	// Endpoints
       
   336 	TUsbcEndpointData data[KUsbcMaxEndpoints];
       
   337 	TPtr8 dataptr(reinterpret_cast<TUint8*>(data), sizeof(data), sizeof(data));
       
   338 	ret = iLdd.EndpointCaps(dataptr);
       
   339 	LOGTEXT(_L8("\tchecking result of EndpointCaps"));
       
   340 	if ( ret )
       
   341 		{
       
   342 		LOGTEXT2(_L8("<<CCdcControlInterface::SetUpInterface ret=%d"), ret);
       
   343 		return ret;
       
   344 		}
       
   345 
       
   346 	// Set the active interface
       
   347 	TUsbcInterfaceInfoBuf ifc;
       
   348 	TBool epFound = EFalse;
       
   349 	for ( TUint i = 0 ; i < totalEndpoints ; i++ )
       
   350 		{
       
   351 		const TUsbcEndpointCaps* caps = &data[i].iCaps;
       
   352 		__ASSERT_DEBUG(caps,_USB_PANIC(Kstub3CcPanicCategory, EPanicUnexpectedStatus));
       
   353 
       
   354 		if (data[i].iInUse)
       
   355 			{
       
   356 			continue;
       
   357 			}
       
   358 
       
   359 		if ((caps->iTypesAndDir & (KUsbEpTypeInterrupt | KUsbEpDirIn)) == 
       
   360 			(KUsbEpTypeInterrupt | KUsbEpDirIn))
       
   361 			{
       
   362 			// EEndpoint1 is interrupt endpoint
       
   363 			ifc().iEndpointData[0].iType  = KUsbEpTypeInterrupt;
       
   364 			ifc().iEndpointData[0].iDir   = KUsbEpDirIn; 
       
   365 
       
   366 			//get the max packet size it can potentially support
       
   367 			//it's possible that it can support Isoch (1023) which is greater
       
   368 			//than max for Int at 64
       
   369 			TInt maxSize = Min(caps->MaxPacketSize(), KMaxPacketTypeInterrupt);
       
   370 			
       
   371 			ifc().iEndpointData[0].iSize  = maxSize;
       
   372 
       
   373 			ifc().iEndpointData[0].iInterval = KPollInterval; 
       
   374 			epFound = ETrue;
       
   375 			break;
       
   376 			}
       
   377 		}
       
   378 	LOGTEXT(_L8("\tchecking epFound"));
       
   379 	if ( !epFound )
       
   380 		{
       
   381 		LOGTEXT2(_L8("<<CCdcControlInterface::SetUpInterface ret=%d"), 
       
   382 			KErrGeneral);
       
   383 		return KErrGeneral;
       
   384 		}
       
   385 
       
   386 	TName ifcName(KIfcName);
       
   387 	ifc().iString = &ifcName;
       
   388 	ifc().iTotalEndpointsUsed = KRequiredNumberOfEndpoints;
       
   389 	// Codes taken from USBCDC 1.1.
       
   390 	ifc().iClass.iClassNum	  = 0x02; // Table 15- Communication Interface Class
       
   391 	ifc().iClass.iSubClassNum = 0x02; // Table 16- Abstract Control Model
       
   392 	ifc().iClass.iProtocolNum = 0x01; // Table 17- Hayes compatible
       
   393 
       
   394 	LOGTEXT(_L8("\tabout to call SetInterface"));
       
   395 	// Zero effectively indicates that alternate interfaces are not used.
       
   396 	ret = iLdd.SetInterface(0, ifc);
       
   397 
       
   398 	LOGTEXT2(_L8("<<CCdcControlInterface::SetUpInterface ret=%d"), ret);
       
   399 	return ret;
       
   400 	}
       
   401 
       
   402 //
       
   403 // End of file