usbmgmt/usbmgr/usbman/server/SRC/CUsbDummyClassController.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 * Implements part of UsbMan USB Class Framework.
       
    16 *
       
    17 */
       
    18 
       
    19 /**
       
    20  @file
       
    21 */
       
    22 
       
    23 #include "CUsbDummyClassController.h"
       
    24 #include <usb_std.h>
       
    25 #include "inifile.h"
       
    26 #include <usb/usblogger.h>
       
    27 
       
    28 #ifdef __FLOG_ACTIVE
       
    29 _LIT8(KLogComponent, "USBSVR");
       
    30 #endif
       
    31 
       
    32 _LIT(KDummyControllerPanic, "UsbDummyCC"); // must be <=16 chars
       
    33 // Panic codes
       
    34 enum
       
    35 	{
       
    36 	// Bad value for the iSynchronicity member.
       
    37 	EDummyPanicBadSynchronicity = 0,
       
    38 	
       
    39 	// Used for environment errors we do not handle.
       
    40 	EDummyPanicUnhandledError = 1,
       
    41 	
       
    42 	// Used for bad iStatus and other state errors.
       
    43 	EDummyPanicBadState = 2,
       
    44 	
       
    45 	// Error reading ini file.
       
    46 	EDummyPanicBadIniFile = 3,
       
    47 	
       
    48 	// We already have our async request active.
       
    49 	EDummyPanicAlreadyActive = 4,
       
    50 	
       
    51 	// We already have a request outstanding from the device class. 
       
    52 	EDummyPanicOutstandingRequestFromDevice = 5,
       
    53 	
       
    54 	// Attempt to call Start() when in illegal state
       
    55 	EDummyPanicBadApiCallStart = 6,
       
    56 	
       
    57 	// Attempt to call Stop() when in illegal state
       
    58 	EDummyPanicBadApiCallStop = 7,
       
    59 	};
       
    60 
       
    61 const TInt KDummyClassPriority = 1;
       
    62 
       
    63 // Keys for reading the ini file.
       
    64 _LIT(KStartup, "Startup");
       
    65 _LIT(KShutdown, "Shutdown");
       
    66 _LIT(KType, "Type");
       
    67 _LIT(KTime, "Time");
       
    68 _LIT(KError, "Error");
       
    69 _LIT(KSync, "sync");
       
    70 _LIT(KAsync, "async");
       
    71 _LIT(KNever, "never");
       
    72 
       
    73 CUsbDummyClassController* CUsbDummyClassController::NewL(
       
    74 		MUsbClassControllerNotify& aOwner,
       
    75 		TUint aIndex)
       
    76 /**
       
    77  * Constructs a CUsbDummyClassController object.
       
    78  *
       
    79  * @param aOwner USB Device that owns and manages the class
       
    80  * @param aIndex The index number of the instance
       
    81  * @return Ownership of a new CUsbDummyClassController object
       
    82  */
       
    83 	{
       
    84 	LOG_STATIC_FUNC_ENTRY
       
    85 
       
    86 	CUsbDummyClassController* self = new(ELeave) CUsbDummyClassController(aOwner, aIndex);
       
    87 	CleanupStack::PushL(self);
       
    88 	self->ConstructL();
       
    89 	CleanupStack::Pop(self);
       
    90 	return self;
       
    91 	}
       
    92 
       
    93 
       
    94 CUsbDummyClassController* CUsbDummyClassController::NewL(
       
    95 	MUsbClassControllerNotify& aOwner, 
       
    96 	TUint aIndex, TInt aPriority)
       
    97 /**
       
    98  * Constructs a CUsbDummyClassController object.
       
    99  *
       
   100  * @param aOwner USB Device that owns and manages the class
       
   101  * @param aIndex The index number of the instance
       
   102  * @param aPriority The startup priority of the instance
       
   103  * @return Ownership of a new CUsbDummyClassController object
       
   104  */
       
   105  	{
       
   106 	LOG_STATIC_FUNC_ENTRY
       
   107 
       
   108  	CUsbDummyClassController* self = new(ELeave) CUsbDummyClassController(aOwner, aIndex, aPriority);
       
   109 	CleanupStack::PushL(self);
       
   110 	self->ConstructL();
       
   111 	CleanupStack::Pop(self);
       
   112 	return self;
       
   113  	}
       
   114  
       
   115 CUsbDummyClassController::CUsbDummyClassController(
       
   116 		MUsbClassControllerNotify& aOwner,
       
   117 		TUint aIndex)
       
   118 /**
       
   119  * Constructor.
       
   120  *
       
   121  * @param aOwner USB Device that owns and manages the class
       
   122  * @param aIndex The index number of the instance
       
   123  */
       
   124  :	CUsbClassControllerBase(aOwner, KDummyClassPriority),
       
   125 	iIndex(aIndex)
       
   126 	{
       
   127 	iState = EUsbServiceIdle; // needs explicit initialisation as non-zero
       
   128 	}
       
   129 
       
   130 CUsbDummyClassController::CUsbDummyClassController(
       
   131 		MUsbClassControllerNotify& aOwner,
       
   132 		TUint aIndex, TInt aPriority)
       
   133 		
       
   134 /**
       
   135  * Constructor.
       
   136  *
       
   137  * @param aOwner USB Device that owns and manages the class
       
   138  * @param aIndex The index number of the instance
       
   139  * @param aPriority a startup priority for the class controller
       
   140  */		
       
   141  : 	CUsbClassControllerBase(aOwner, aPriority),
       
   142  	iIndex(aIndex)
       
   143  	{
       
   144  	iState = EUsbServiceIdle; // needs explicit initialisation as non-zero
       
   145  	}
       
   146 		
       
   147 void CUsbDummyClassController::ConstructL()
       
   148 /**
       
   149  * Method to perform second phase construction.
       
   150  */
       
   151 	{
       
   152 	LEAVEIFERRORL(iTimer.CreateLocal());
       
   153 	}
       
   154 
       
   155 CUsbDummyClassController::~CUsbDummyClassController()
       
   156 /**
       
   157  * Destructor.
       
   158  */
       
   159 	{
       
   160 	Cancel();
       
   161 
       
   162 	iTimer.Close();
       
   163 	}
       
   164 	
       
   165 void CUsbDummyClassController::GetBehaviour(CIniFile& aIniFile, 
       
   166 											const TDesC& aSection, 
       
   167 											TBehaviour& aBehaviour)
       
   168 /**
       
   169  * Reads information from the ini file, from a given section, to the given 
       
   170  * behaviour structure.
       
   171  *
       
   172  * @param aIniFile The ini file to read from.
       
   173  * @param aSection The section to reads from.
       
   174  * @param aBehaviour The behaviour struct to read to.
       
   175  */
       
   176 	{
       
   177 	LOG_FUNC
       
   178 #ifdef __FLOG_ACTIVE
       
   179 	TBuf8<KMaxName> buf;
       
   180 	buf.Copy(aSection);
       
   181 	LOGTEXT2(_L8("\taSection = %S"), &buf);
       
   182 #endif // __FLOG_ACTIVE
       
   183 
       
   184 	TPtrC temp;
       
   185 	if ( !aIniFile.FindVar(aSection, KType(), temp) )
       
   186 		{
       
   187 		LOGTEXT2(_L8("\tPANICKING: can't find Type item in section %S"), &aSection);
       
   188 		_USB_PANIC(KDummyControllerPanic, EDummyPanicBadIniFile);
       
   189 		}
       
   190 	if ( temp == KSync )
       
   191 		{
       
   192 		aBehaviour.iSynchronicity = ESynchronous;
       
   193 		}
       
   194 	else if ( temp == KAsync )
       
   195 		{
       
   196 		aBehaviour.iSynchronicity = EAsynchronous;
       
   197 		}
       
   198 	else if ( temp == KNever )
       
   199 		{
       
   200 		aBehaviour.iSynchronicity = ENever;
       
   201 		}
       
   202 	else
       
   203 		{
       
   204 		LOGTEXT3(_L8("\tPANICKING: bad Type value (%S) in section %S"), &temp, &aSection);
       
   205 		_USB_PANIC(KDummyControllerPanic, EDummyPanicBadIniFile);
       
   206 		}
       
   207 	TInt delay;
       
   208 	if ( !aIniFile.FindVar(aSection, KTime(), delay) )
       
   209 		{
       
   210 		LOGTEXT2(_L8("\tPANICKING: can't find Time item in section %S"), &aSection);
       
   211 		_USB_PANIC(KDummyControllerPanic, EDummyPanicBadIniFile);
       
   212 		}
       
   213 	aBehaviour.iDelay = delay;
       
   214 	if ( !aIniFile.FindVar(aSection, KError(), aBehaviour.iErrorCode) )
       
   215 		{
       
   216 		LOGTEXT2(_L8("\tPANICKING: can't find Error item in section %S"), &aSection);
       
   217 		_USB_PANIC(KDummyControllerPanic, EDummyPanicBadIniFile);
       
   218 		}
       
   219 	}
       
   220 
       
   221 void CUsbDummyClassController::DoGetConfigL()
       
   222 /**
       
   223  * Reads the config from the ini file.
       
   224  */
       
   225 	{
       
   226 	LOG_FUNC
       
   227 
       
   228 	CIniFile* iniFile = CIniFile::NewL(_L("dummy.ini"));
       
   229 	CleanupStack::PushL(iniFile);
       
   230 	TName section;
       
   231 
       
   232 	// Read startup behaviour
       
   233 	section.Format(_L("%S.%d"), &KStartup(), iIndex);
       
   234 	GetBehaviour(*iniFile, section, iStartupBehaviour);
       
   235 	// Read shutdown behaviour
       
   236 	section.Format(_L("%S.%d"), &KShutdown(), iIndex);
       
   237 	GetBehaviour(*iniFile, section, iShutdownBehaviour);
       
   238 
       
   239 	CleanupStack::PopAndDestroy(iniFile);
       
   240 	}
       
   241 
       
   242 void CUsbDummyClassController::GetConfig()
       
   243 /**
       
   244  * Reads the config from the ini file.
       
   245  * Note that this is assumed to succeed. Any failure will break the test and 
       
   246  * it's much cleaner to panic out of the test entirely rather than leave it to 
       
   247  * the user to figure out what's gone wrong.
       
   248  */
       
   249 	{
       
   250 	LOG_FUNC
       
   251 
       
   252 	// Always use dummy.ini. The entity setting up the test is responsible for 
       
   253 	// copying the correct file to c:\\dummy.ini. The first found 
       
   254 	// by FindByDir will be used. TODO: enforce c:\\.
       
   255 	TRAPD(err, DoGetConfigL());
       
   256 	if ( err != KErrNone )
       
   257 		{
       
   258 		_USB_PANIC(KDummyControllerPanic, EDummyPanicUnhandledError);
       
   259 		}
       
   260 	
       
   261 	LOGTEXT2(_L8("\tLogging dummy class controller behaviour for instance %d"), iIndex);
       
   262 	LOGTEXT2(_L8("\tiStartupBehaviour.iSynchronicity = %d"), iStartupBehaviour.iSynchronicity);
       
   263 	LOGTEXT2(_L8("\tiStartupBehaviour.iDelay = %d"), iStartupBehaviour.iDelay.Int());
       
   264 	LOGTEXT2(_L8("\tiStartupBehaviour.iErrorCode = %d"), iStartupBehaviour.iErrorCode);
       
   265 	LOGTEXT2(_L8("\tiShutdownBehaviour.iSynchronicity = %d"), iShutdownBehaviour.iSynchronicity);
       
   266 	LOGTEXT2(_L8("\tiShutdownBehaviour.iDelay = %d"), iShutdownBehaviour.iDelay.Int());
       
   267 	LOGTEXT2(_L8("\tiShutdownBehaviour.iErrorCode = %d"), iShutdownBehaviour.iErrorCode);
       
   268 	}
       
   269 
       
   270 void CUsbDummyClassController::Start(TRequestStatus& aStatus)
       
   271 /**
       
   272  * Called by UsbMan to start this class.
       
   273  *
       
   274  * @param aStatus Will be completed with success or failure.
       
   275  */
       
   276 	{
       
   277 	LOG_FUNC
       
   278 	
       
   279 	//Start() should only be called if the CC is idle or started		
       
   280 	__ASSERT_DEBUG((iState == EUsbServiceIdle || iState == EUsbServiceStarted), 
       
   281 							_USB_PANIC(KDummyControllerPanic, EDummyPanicBadApiCallStart) );
       
   282 
       
   283 	// Get config from ini file. Note that can't be done once in ConstructL 
       
   284 	// because then, in the case of a CC which doesn't Stop, we'd never be 
       
   285 	// able to shut down USBMAN!
       
   286 	GetConfig();
       
   287 
       
   288 	// NB We enforce that the device doesn't re-post requests on us.
       
   289 	__ASSERT_DEBUG(!iReportStatus, 
       
   290 		_USB_PANIC(KDummyControllerPanic, EDummyPanicOutstandingRequestFromDevice));
       
   291 	aStatus = KRequestPending;
       
   292 	iReportStatus = &aStatus;
       
   293 	
       
   294 	iState = EUsbServiceStarting;
       
   295 
       
   296 	switch ( iStartupBehaviour.iSynchronicity )
       
   297 		{
       
   298 	case ESynchronous:
       
   299 		User::After(iStartupBehaviour.iDelay);
       
   300 		iState = EUsbServiceStarted;
       
   301 		User::RequestComplete(iReportStatus, iStartupBehaviour.iErrorCode);
       
   302 		iReportStatus = NULL;
       
   303 		break;
       
   304 
       
   305 	case EAsynchronous:
       
   306 		iTimer.After(iStatus, iStartupBehaviour.iDelay);
       
   307 		__ASSERT_DEBUG(!IsActive(), _USB_PANIC(KDummyControllerPanic, EDummyPanicAlreadyActive));		
       
   308 		SetActive();
       
   309 		break;
       
   310 
       
   311 	case ENever:
       
   312 		// Don't do anything and never complete
       
   313 		break;
       
   314 
       
   315 	default:
       
   316 		_USB_PANIC(KDummyControllerPanic, EDummyPanicBadSynchronicity);
       
   317 		break;
       
   318 		}
       
   319 	}
       
   320 
       
   321 void CUsbDummyClassController::Stop(TRequestStatus& aStatus)
       
   322 /**
       
   323  * Called by UsbMan to stop this class.
       
   324  *
       
   325  * @param aStatus Will be completed with success or failure.
       
   326  */
       
   327 	{
       
   328 	LOG_FUNC
       
   329 		
       
   330 	//Stop() should only be called if the CC is Started or Idle
       
   331 	__ASSERT_DEBUG((iState == EUsbServiceStarted || iState == EUsbServiceIdle), 
       
   332 				_USB_PANIC(KDummyControllerPanic, EDummyPanicBadApiCallStop));
       
   333 	
       
   334 	// Get config from ini file. Note that can't be done once in ConstructL 
       
   335 	// because then, in the case of a CC which doesn't Stop, we'd never be 
       
   336 	// able to shutdown USBMAN!
       
   337 	GetConfig();
       
   338 
       
   339 	// NB We enforce that the device doesn't re-post requests on us.
       
   340 	__ASSERT_DEBUG(!iReportStatus, 
       
   341 		_USB_PANIC(KDummyControllerPanic, EDummyPanicOutstandingRequestFromDevice));
       
   342 	aStatus = KRequestPending;
       
   343 	iReportStatus = &aStatus;
       
   344 
       
   345 	iState = EUsbServiceStopping;
       
   346 
       
   347 	switch ( iShutdownBehaviour.iSynchronicity )
       
   348 		{
       
   349 	case ESynchronous:
       
   350 		User::After(iShutdownBehaviour.iDelay);
       
   351 		iState = EUsbServiceIdle;
       
   352 		User::RequestComplete(iReportStatus, iShutdownBehaviour.iErrorCode);
       
   353 		iReportStatus = NULL;
       
   354 		break;
       
   355 
       
   356 	case EAsynchronous:
       
   357 		iTimer.After(iStatus, iShutdownBehaviour.iDelay);
       
   358     	__ASSERT_DEBUG(!IsActive(), _USB_PANIC(KDummyControllerPanic, EDummyPanicAlreadyActive));		
       
   359 		SetActive();
       
   360 		break;
       
   361 
       
   362 	case ENever:
       
   363 		// Don't do anything and never complete
       
   364 		break;
       
   365 
       
   366 	default:
       
   367 		_USB_PANIC(KDummyControllerPanic, EDummyPanicBadSynchronicity);
       
   368 		break;
       
   369 		}
       
   370 	}
       
   371 
       
   372 void CUsbDummyClassController::GetDescriptorInfo(TUsbDescriptor& aDescriptorInfo) const
       
   373 /**
       
   374  * Returns information about the interfaces supported by this class.
       
   375  *
       
   376  * @param aDescriptorInfo Will be filled in with interface information.
       
   377  */
       
   378 	{
       
   379 	aDescriptorInfo.iNumInterfaces = 0;
       
   380 	aDescriptorInfo.iLength = 0;
       
   381 	}
       
   382 
       
   383 void CUsbDummyClassController::RunL()
       
   384 /**
       
   385  * Standard active object RunL. 
       
   386  */
       
   387 	{
       
   388 	LOGTEXT3(_L8(">>CUsbDummyClassController::RunL [iStatus=%d,iState=%d]"),
       
   389 			iStatus.Int(), iState);
       
   390 
       
   391 	if ( iStatus != KErrNone )
       
   392 		{
       
   393 		// Panic runtime errors from the timer. We can't ignore them, and 
       
   394 		// there's no point trying to code round them. This is part of the 
       
   395 		// test framework and if it's failing we want to alert the user 
       
   396 		// without faffing around. (It invalidates the test.)
       
   397 		_USB_PANIC(KDummyControllerPanic, EDummyPanicUnhandledError);
       
   398 		}								  
       
   399 
       
   400 	__ASSERT_DEBUG(iReportStatus, 
       
   401 		_USB_PANIC(KDummyControllerPanic, EDummyPanicBadState));
       
   402 
       
   403 	switch ( iState )
       
   404 		{
       
   405 	case EUsbServiceStarting:
       
   406 		// Completion of asynchronous startup...
       
   407 		__ASSERT_DEBUG(iStartupBehaviour.iSynchronicity == EAsynchronous, 
       
   408 			_USB_PANIC(KDummyControllerPanic, EDummyPanicBadSynchronicity));
       
   409 		iState = EUsbServiceStarted;
       
   410 		User::RequestComplete(iReportStatus, iStartupBehaviour.iErrorCode);
       
   411 		iReportStatus = NULL;
       
   412 		break;
       
   413 
       
   414 	case EUsbServiceStopping:
       
   415 		// Completion of asynchronous shutdown...
       
   416 		__ASSERT_DEBUG(iShutdownBehaviour.iSynchronicity == EAsynchronous, 
       
   417 			_USB_PANIC(KDummyControllerPanic, EDummyPanicBadSynchronicity));
       
   418 		iState = EUsbServiceIdle;
       
   419 		User::RequestComplete(iReportStatus, iShutdownBehaviour.iErrorCode);
       
   420 		iReportStatus = NULL;
       
   421 		break;
       
   422 
       
   423 	case EUsbServiceIdle:
       
   424 	case EUsbServiceStarted:
       
   425 	default:
       
   426 		_USB_PANIC(KDummyControllerPanic, EDummyPanicBadState);
       
   427 		break;
       
   428 		}
       
   429 
       
   430 	LOGTEXT(_L8("<<CUsbDummyClassController::RunL"));
       
   431 	}
       
   432 
       
   433 void CUsbDummyClassController::DoCancel()
       
   434 /**
       
   435  * Standard active object cancellation function. 
       
   436  */
       
   437 	{
       
   438 	LOG_FUNC
       
   439 
       
   440 	// Note that CActive::Cancel does not call DoCancel unless we are active. 
       
   441 	// Therefore we are at this point active. Therefore, we should have 
       
   442 	// iReportStatus pointing to something. This is true whether we are in 
       
   443 	// our destructor or being cancelled by the device. It is also true that 
       
   444 	// (for us to be active at all) our synchronicity should be set to async 
       
   445 	// and the timer must have been CreateLocal'd successfully.
       
   446 
       
   447 	// Cancel may be called in one of the following situations: 
       
   448 	// 1/ In our destructor. 
       
   449 	// 2/ The device wants to cancel us in the middle of a Start before 
       
   450 	// issuing a Stop (or vice versa). Note that the device may cancel us in 
       
   451 	// the middle of a Start, then immediately issue another Start.
       
   452 	
       
   453 	// Cancel our own asynchronous operation.
       
   454 	__ASSERT_DEBUG(iTimer.Handle(), 
       
   455 		_USB_PANIC(KDummyControllerPanic, EDummyPanicBadState));
       
   456 	iTimer.Cancel();
       
   457 
       
   458 	// Update our iState. If we're starting, then roll back to idle. If we're 
       
   459 	// stopping, role back to started. Nothing else is legal.
       
   460 	switch ( iState )
       
   461 		{
       
   462 	case EUsbServiceStarting:
       
   463 		__ASSERT_DEBUG(iStartupBehaviour.iSynchronicity == EAsynchronous, 
       
   464 			_USB_PANIC(KDummyControllerPanic, EDummyPanicBadSynchronicity));
       
   465 		iState = EUsbServiceIdle;
       
   466 		break;
       
   467 
       
   468 	case EUsbServiceStopping:
       
   469 		__ASSERT_DEBUG(iShutdownBehaviour.iSynchronicity == EAsynchronous, 
       
   470 			_USB_PANIC(KDummyControllerPanic, EDummyPanicBadSynchronicity));
       
   471 		iState = EUsbServiceStarted;
       
   472 		break;
       
   473 
       
   474 	case EUsbServiceIdle:
       
   475 	case EUsbServiceStarted:
       
   476 	default:
       
   477 		_USB_PANIC(KDummyControllerPanic, EDummyPanicBadState);
       
   478 		break;
       
   479 		}
       
   480 
       
   481 	// Complete the client's request.	
       
   482 	__ASSERT_DEBUG(iReportStatus, 
       
   483 		_USB_PANIC(KDummyControllerPanic, EDummyPanicBadState));
       
   484 	User::RequestComplete(iReportStatus, KErrCancel); 
       
   485 	iReportStatus = NULL;
       
   486 	}
       
   487 
       
   488 TInt CUsbDummyClassController::RunError(TInt /*aError*/)
       
   489 /**
       
   490  * Standard active object error-handling function. 
       
   491  *
       
   492  * Should return KErrNone to avoid an active scheduler panic. This function
       
   493  * should never be called as there is another mechanism for catching errors.
       
   494  */
       
   495 	{
       
   496 	__ASSERT_DEBUG(EFalse, 
       
   497 		_USB_PANIC(KDummyControllerPanic, EDummyPanicBadState));
       
   498 	return KErrNone;
       
   499 	}
       
   500 
       
   501 //
       
   502 // End of file