usbmgmt/usbmgrtest/usbcontrolapp/exampleusbcontrolapp/exampleusbcontrolapp.cpp
changeset 0 c9bc50fca66e
equal deleted inserted replaced
-1:000000000000 0:c9bc50fca66e
       
     1 /*
       
     2 * Copyright (c) 2008-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 
       
    18 #include "exampleusbcontrolapp.h"
       
    19 
       
    20 #include <e32cons.h>
       
    21 #include <e32debug.h>
       
    22 // For Message Watcher
       
    23 #include <d32otgdi_errors.h>
       
    24 #include <d32usbdi_errors.h>
       
    25 #include <usb/usbshared.h>
       
    26 
       
    27 
       
    28 #define LOG(A,B)	RDebug::Print(_L("UsbControlApp: " L##A),B)
       
    29 #define PNT(A)		RDebug::Print(_L("UsbControlApp: " L##A))
       
    30 #define BLN(A)		RDebug::Print(_L("" L##A))
       
    31 #define PANIC		Panic(__LINE__)
       
    32 
       
    33 
       
    34 #ifdef __USB_DEBUG__
       
    35 #define DBG_PANIC	Panic(__LINE__)
       
    36 #else
       
    37 #define DBG_PANIC
       
    38 #endif
       
    39 
       
    40 
       
    41 void Panic(TInt aLine)
       
    42 	{
       
    43 	RDebug::Printf("UsbControlApp: PANIC line=%d", aLine);
       
    44 	User::Panic(_L("USBCONTROLAPP"), aLine);
       
    45 	}
       
    46 
       
    47 void RunAppL()
       
    48 	{
       
    49 	CUsbControlAppEngine* engine = CUsbControlAppEngine::NewLC();
       
    50 	engine->Start();
       
    51 	CleanupStack::PopAndDestroy(engine);
       
    52 	}
       
    53 
       
    54 TInt E32Main()
       
    55 	{
       
    56 	__UHEAP_MARK;
       
    57 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
    58 	CActiveScheduler* activeScheduler = new CActiveScheduler;
       
    59 	TInt err = KErrNoMemory;
       
    60 	if(cleanup && activeScheduler)
       
    61 		{
       
    62 		CActiveScheduler::Install(activeScheduler);
       
    63 		PNT("*** UsbControlApp E32Main ***\n");
       
    64 		TRAP(err, RunAppL());
       
    65 		}
       
    66 	delete activeScheduler;
       
    67 	delete cleanup;
       
    68 	__UHEAP_MARKEND;
       
    69 	return err;
       
    70 	}
       
    71 
       
    72 
       
    73 CShutdownMonitor* CShutdownMonitor::NewL(MShutdownInterface& aControlAppEngine)
       
    74 	{
       
    75 	CShutdownMonitor* self = new(ELeave) CShutdownMonitor(aControlAppEngine);
       
    76 	CleanupStack::PushL(self);
       
    77 	self->ConstructL();
       
    78 	CleanupStack::Pop(self);
       
    79 	return self;
       
    80 	}
       
    81 
       
    82 CShutdownMonitor::CShutdownMonitor(MShutdownInterface& aControlAppEngine)
       
    83 	: CActive(EPriorityLow) // Low so all notifications that want to be serviced will be done first
       
    84 	, iParentControlAppEngine(aControlAppEngine)
       
    85 	{
       
    86 	CActiveScheduler::Add(this);
       
    87 	}
       
    88 
       
    89 void CShutdownMonitor::ConstructL()
       
    90 	{
       
    91 	// Monitor the KUsbControlAppShutdownKey property to tell us when to shut down
       
    92 	TInt err = iShutdownProp.Attach(KUidUsbControlAppCategory, KUsbControlAppShutdownKey);
       
    93 	LOG("CShutdownMonitor::ConstructL	 iShutdownProp.Attach() => %d", err);
       
    94 	User::LeaveIfError(err);
       
    95 	iShutdownProp.Subscribe(iStatus);
       
    96 	SetActive();
       
    97 	TInt val;
       
    98 	// Make sure the cuurent value is 0 - shut down when this changes to 1
       
    99 	err = iShutdownProp.Get(val);
       
   100 	LOG("CShutdownMonitor::ConstructL()	 iShutdownProp.Get(val)		val => %d", val);
       
   101 	LOG("CShutdownMonitor::ConstructL()	 iShutdownProp.Get(val)		err => %d", err);
       
   102 	User::LeaveIfError(err);
       
   103 	__ASSERT_ALWAYS(val==0, PANIC);
       
   104 	}
       
   105 
       
   106 CShutdownMonitor::~CShutdownMonitor()
       
   107 	{
       
   108 	iShutdownProp.Close();
       
   109 	}
       
   110 
       
   111 void CShutdownMonitor::RunL()
       
   112 	{
       
   113 	// Request to shut everything down made in USB Aware App
       
   114 	TInt val;
       
   115 	TInt err = iShutdownProp.Get(val);
       
   116 	LOG("CShutdownMonitor::RunL	 iShutdownProp.Get(val) err => %d", err);
       
   117 	LOG("CShutdownMonitor::RunL	 iShutdownProp.Get(val) val => %d", val);
       
   118 	Cancel(); // Not interested in any more notifications
       
   119 	iParentControlAppEngine.Stop(); // Stopping Active Scheduler will results in the destructor getting called
       
   120 	}
       
   121 
       
   122 void CShutdownMonitor::DoCancel()
       
   123 	{
       
   124 	iShutdownProp.Cancel();
       
   125 	}
       
   126 
       
   127 
       
   128 CUsbControlAppEngine* CUsbControlAppEngine::NewLC()
       
   129 	{
       
   130 	CUsbControlAppEngine* self = new(ELeave) CUsbControlAppEngine();
       
   131 	CleanupStack::PushL(self);
       
   132 	self->ConstructL();
       
   133 	PNT("\nConstructed Control App\n");
       
   134 	return self;
       
   135 	}
       
   136 
       
   137 CUsbControlAppEngine::~CUsbControlAppEngine()
       
   138 	{
       
   139 	PNT("\nClosing Control App\n");
       
   140 	iViewerMsgQ.Close();
       
   141 	delete iShutdownMonitor;
       
   142 	delete iStateMachine;
       
   143 	delete iMessageWatcher;
       
   144 	delete iConnIdleWatcher;
       
   145 	delete iVBusWatcher;
       
   146 	delete iIdPinWatcher;
       
   147 	iUsb.Close();
       
   148 	}
       
   149 
       
   150 CUsbControlAppEngine::CUsbControlAppEngine()
       
   151 	{
       
   152 	}
       
   153 
       
   154 void CUsbControlAppEngine::ConstructL()
       
   155 	{
       
   156 	// Start session to USBMAN
       
   157 	TInt err = iUsb.Connect();
       
   158 	LOG("CUsbControlAppEngine::ConstructL()   iUsb.Connect()   err=%d", err);
       
   159 	User::LeaveIfError(err);
       
   160 	// Set this as the single controller of USBMAN
       
   161 	err = iUsb.SetCtlSessionMode(ETrue);
       
   162 	LOG("CUsbControlAppEngine::ConstructL()   iUsb.SetCtlSessionMode(ETrue)   err=%d", err);
       
   163 	User::LeaveIfError(err);
       
   164 	iStateMachine	= CControlAppStateMachine::NewL(*this);
       
   165 	// Create Watcher Active Objects to monitor events
       
   166 	iIdPinWatcher	= CIdPinWatcher::NewL(*this);
       
   167 	iVBusWatcher	= CVBusWatcher::NewL(*this);
       
   168 	iConnIdleWatcher= CConnectionIdleWatcher::NewL(*this);
       
   169 	iMessageWatcher	= CMessageWatcher::NewL(*this);
       
   170 
       
   171 	iShutdownMonitor = CShutdownMonitor::NewL(*this);
       
   172 	// Create message queue between exampleusbcontrolapp.exe & usbviewer.exe for showing user messages
       
   173 	err = iViewerMsgQ.CreateGlobal(KControlAppViewerMsgQName, KControlAppViewerMsgQSlots);
       
   174 	LOG("CUsbControlAppEngine::ConstructL()	 iViewerMsgQ.CreateGlobal		err => %d", err);
       
   175 	User::LeaveIfError(err);
       
   176 	// Stop USB services if they are started so system can start from a known state
       
   177 	err = StopUsbServices();
       
   178 	LOG("CUsbControlAppEngine::ConstructL()   StopUsbServices()		err=%d", err);
       
   179 	User::LeaveIfError(err);
       
   180 	// Last thing - allow State Machine to find a main state by sending it a StartUp event
       
   181 	iStateMachine->StartUp();
       
   182 	}
       
   183 
       
   184 void CUsbControlAppEngine::Start()
       
   185 	{
       
   186 	CActiveScheduler::Start(); // Get everything running
       
   187 	}
       
   188 	
       
   189 void CUsbControlAppEngine::Stop() const
       
   190 	{
       
   191 	CActiveScheduler::Stop();
       
   192 	}
       
   193 
       
   194 // Called by State Machine
       
   195 TInt CUsbControlAppEngine::StopUsbServices()
       
   196 	{
       
   197 	TRequestStatus status;
       
   198 	iUsb.TryStop(status);
       
   199 	User::WaitForRequest(status);
       
   200 	LOG("CUsbControlAppEngine::StopUsbServices()   status.Int()=%d", status.Int());
       
   201 	return status.Int();
       
   202 	}
       
   203 
       
   204 TInt CUsbControlAppEngine::StartUsbServices()
       
   205 	{
       
   206 	const TInt KACMPersonality = 1;
       
   207 	TRequestStatus status;
       
   208 	iUsb.TryStart(KACMPersonality, status);
       
   209 	User::WaitForRequest(status);
       
   210 	LOG("CUsbControlAppEngine::StartUsbServices()   status.Int()=%d", status.Int());
       
   211 	return status.Int();
       
   212 	}
       
   213 
       
   214 TInt CUsbControlAppEngine::EnableFunctionDriverLoading()
       
   215 	{
       
   216 	TInt err = iUsb.EnableFunctionDriverLoading();
       
   217 	LOG("CUsbControlAppEngine::EnableFunctionDriverLoading()   err=%d", err);
       
   218 	return err;
       
   219 	}
       
   220 
       
   221 void CUsbControlAppEngine::DisableFunctionDriverLoading()
       
   222 	{
       
   223 	PNT("CUsbControlAppEngine::DisableFunctionDriverLoading()\n");
       
   224 	iUsb.DisableFunctionDriverLoading();
       
   225 	}
       
   226 
       
   227 TInt CUsbControlAppEngine::BusRequest()
       
   228 	{
       
   229 	TInt err = iUsb.BusRequest();
       
   230 	LOG("CUsbControlAppEngine::BusRequest()   err=%d", err);
       
   231 	return err;
       
   232 	}
       
   233 
       
   234 TInt CUsbControlAppEngine::BusDrop()
       
   235 	{
       
   236 	TInt err = iUsb.BusDrop();
       
   237 	LOG("CUsbControlAppEngine::BusDrop()   err=%d", err);
       
   238 	return err;
       
   239 	}
       
   240 
       
   241 TInt CUsbControlAppEngine::BusRespondSrp()
       
   242 	{
       
   243 	TInt err = iUsb.BusRespondSrp();
       
   244 	LOG("CUsbControlAppEngine::BusRespondSrp()   err=%d", err);
       
   245 	return err;
       
   246 	}
       
   247 
       
   248 TInt CUsbControlAppEngine::ClearVBusError()
       
   249 	{
       
   250 	TInt err = iUsb.BusClearError();
       
   251 	LOG("CUsbControlAppEngine::ClearVBusError()   err=%d", err);
       
   252 	return err;
       
   253 	}
       
   254 
       
   255 // Must divide the user message to be displayed into segments that will fit on the usbviewer.exe window.
       
   256 // Because the viewer.exe displays Events in a downward scrolling list with the latest entry at the top,
       
   257 // the user message segments must be sent in reverse order.
       
   258 void CUsbControlAppEngine::DisplayUserMessage(const TDesC& aUserMsg)
       
   259 	{
       
   260 	LOG("CUsbControlAppEngine::DisplayUserMessage(\"%S\")\n", &aUserMsg);
       
   261 	TInt msgLength = aUserMsg.Length();
       
   262 	TInt lastBitLength = msgLength % KEventLineMsgNumCharacters;
       
   263 	LOG("CUsbControlAppEngine::DisplayUserMessage	 lastBitLength => %d", lastBitLength);
       
   264 	if (lastBitLength) // show the 'last bit' segment of the message which is not divisible by KEventLineMsgNumCharacters
       
   265 		{
       
   266 		TBuf<KEventLineNumCharacters> buf(KPrefix);
       
   267 		TPtrC text = aUserMsg.Right(lastBitLength);
       
   268 		buf.Append(text);
       
   269 		LOG("CUsbControlAppEngine::DisplayUserMessage	 iViewerMsgQ.Send(buf) buf => %S", &buf);
       
   270 		TInt err = iViewerMsgQ.Send(buf);
       
   271 		LOG("CUsbControlAppEngine::DisplayUserMessage	 iViewerMsgQ.Send(buf) err => %d", err);
       
   272 		}
       
   273 	msgLength -= lastBitLength;
       
   274 	while (msgLength > 0) // show the segments of the message which are exactly divisible by KEventLineMsgNumCharacters
       
   275 		{
       
   276 		// TBuf of length KEventLineNumCharacters = KPrefix of length 2 + Mid bit of length KEventLineMsgNumCharacters
       
   277 		TBuf<KEventLineNumCharacters> buf(KPrefix);
       
   278 		TPtrC text = aUserMsg.Mid(msgLength-KEventLineMsgNumCharacters, KEventLineMsgNumCharacters);
       
   279 		buf.Append(text);
       
   280 		LOG("CUsbControlAppEngine::DisplayUserMessage	 iViewerMsgQ.Send(buf) buf => %S", &buf);
       
   281 		TInt err = iViewerMsgQ.Send(buf);
       
   282 		LOG("CUsbControlAppEngine::DisplayUserMessage	 iViewerMsgQ.Send(buf) err => %d", err);		
       
   283 		msgLength -= KEventLineMsgNumCharacters;
       
   284 		}
       
   285 	}
       
   286 
       
   287 // Called by Watchers. Will forward as event to State Machine.
       
   288 void CUsbControlAppEngine::SetIdPin(TInt aIdPin)
       
   289 	{
       
   290 	PNT("CUsbControlAppEngine::SetIdPin()\n");
       
   291 	iIdPin = aIdPin;
       
   292 	__ASSERT_ALWAYS(iStateMachine, PANIC);
       
   293 	aIdPin ? iStateMachine->IdPinPresent() : iStateMachine->IdPinAbsent();
       
   294 	}
       
   295 
       
   296 void CUsbControlAppEngine::SetVBus(TInt aVBus)
       
   297 	{
       
   298 	PNT("CUsbControlAppEngine::SetVBus()\n");
       
   299 	iVBus = aVBus;
       
   300 	__ASSERT_ALWAYS(iStateMachine, PANIC);
       
   301 	aVBus ? iStateMachine->VBusRise() : iStateMachine->VBusDrop();
       
   302 	}
       
   303 
       
   304 void CUsbControlAppEngine::SetConnectionIdle(TInt aConnIdle)
       
   305 	{
       
   306 	PNT("CUsbControlAppEngine::SetConnectionIdle()\n");
       
   307 	iConnIdle = aConnIdle;
       
   308 	__ASSERT_ALWAYS(iStateMachine, PANIC);
       
   309 	aConnIdle ? iStateMachine->ConnectionIdle() : iStateMachine->ConnectionActive();
       
   310 	}
       
   311 
       
   312 void CUsbControlAppEngine::MessageReceived(CMessageWatcher::TMessageWatcherNotifications aMessageNotification)
       
   313 	{
       
   314 	switch(aMessageNotification)
       
   315 		{
       
   316 	case CMessageWatcher::EUsbMessageRequestSession :
       
   317 		PNT("CUsbControlAppEngine::MessageReceived()	EUsbMessageRequestSession\n");
       
   318 		iStateMachine->RequestSessionCalled();
       
   319 		break;
       
   320 	case CMessageWatcher::EUsbMessageSrpReceived :
       
   321 		PNT("CUsbControlAppEngine::MessageReceived()	EUsbMessageSrpReceived\n");
       
   322 		iStateMachine->SrpDetected();
       
   323 		break;
       
   324 	case CMessageWatcher::EErrUsbOtgSrpTimeout :
       
   325 		PNT("CUsbControlAppEngine::MessageReceived()	EErrUsbOtgSrpTimeout\n");
       
   326 		iStateMachine->SrpTimeout();
       
   327 		break;
       
   328 	case CMessageWatcher::EErrUsbOtgVbusError :
       
   329 		PNT("CUsbControlAppEngine::MessageReceived()	EErrUsbOtgVbusError\n");
       
   330 		iStateMachine->VBusError();
       
   331 		break;
       
   332 	default:
       
   333 		PANIC;
       
   334 		}
       
   335 	}
       
   336 
       
   337 RUsb& CUsbControlAppEngine::Usb()
       
   338 	{
       
   339 	return iUsb;
       
   340 	}
       
   341 
       
   342 TInt CUsbControlAppEngine::GetIdPin()
       
   343 	{
       
   344 	return iIdPin;
       
   345 	}
       
   346 
       
   347 TInt CUsbControlAppEngine::GetVBus()
       
   348 	{
       
   349 	return iVBus;
       
   350 	}
       
   351 
       
   352 
       
   353 CControlAppStateMachine* CControlAppStateMachine::NewL(MControlAppEngineInterface& aControlAppEngine)
       
   354 	{
       
   355 	CControlAppStateMachine* self = new(ELeave) CControlAppStateMachine(aControlAppEngine);
       
   356 	CleanupStack::PushL(self);
       
   357 	self->ConstructL();
       
   358 	PNT("CControlAppStateMachine::NewL\n");
       
   359 	CleanupStack::Pop(self);
       
   360 	return self;
       
   361 	}
       
   362 
       
   363 CControlAppStateMachine::~CControlAppStateMachine()
       
   364 	{
       
   365 	delete iStateAServicesStopped;
       
   366 	delete iStateAServicesStarted;
       
   367 	delete iStateBServicesStarted;
       
   368 	delete iStateBServicesStopped;
       
   369 	delete iStateInitial;
       
   370 	delete iInactivityTimer;
       
   371 	}
       
   372 
       
   373 CControlAppStateMachine::CControlAppStateMachine(MControlAppEngineInterface& aControlAppEngine)
       
   374 	: iParentControlAppEngine(aControlAppEngine)
       
   375 	, iTriggerSrp(EFalse)
       
   376 	{
       
   377 	}
       
   378 
       
   379 void CControlAppStateMachine::ConstructL()
       
   380 	{
       
   381 	iInactivityTimer		= CInactivityTimer::NewL(*this);
       
   382 	iStateInitial			= new(ELeave) CControlAppStateInitial(*this);
       
   383 	iStateBServicesStopped	= new(ELeave) CControlAppStateBServicesStopped(*this);
       
   384 	iStateBServicesStarted	= new(ELeave) CControlAppStateBServicesStarted(*this);
       
   385 	iStateAServicesStarted	= new(ELeave) CControlAppStateAServicesStarted(*this);
       
   386 	iStateAServicesStopped	= new(ELeave) CControlAppStateAServicesStopped(*this);
       
   387 	SetState(EStateInitial);
       
   388 	}
       
   389 
       
   390 void CControlAppStateMachine::SetState(TControlAppState aState)
       
   391 	{
       
   392 	TPtrC StateString(NULL, 0);
       
   393 	//               123456789
       
   394 	_LIT(KInitial,	" Initial ");
       
   395 	_LIT(KBStopped, "B-Stopped");
       
   396 	_LIT(KBStarted, "B-Started");
       
   397 	_LIT(KAStopped, "A-Stopped");
       
   398 	_LIT(KAStarted, "A-Started");
       
   399 	switch(aState)
       
   400 		{
       
   401 	case EStateInitial :			
       
   402 		StateString.Set(KInitial);		
       
   403 		iCurrentState = iStateInitial;		
       
   404 		break;
       
   405 	case EStateBServicesStopped :	
       
   406 		StateString.Set(KBStopped);		
       
   407 		iCurrentState = iStateBServicesStopped;		
       
   408 		break;
       
   409 	case EStateBServicesStarted :	
       
   410 		StateString.Set(KBStarted);		
       
   411 		iCurrentState = iStateBServicesStarted;		
       
   412 		break;
       
   413 	case EStateAServicesStopped :	
       
   414 		StateString.Set(KAStopped);		
       
   415 		iCurrentState = iStateAServicesStopped;		
       
   416 		break;
       
   417 	case EStateAServicesStarted :	
       
   418 		StateString.Set(KAStarted);		
       
   419 		iCurrentState = iStateAServicesStarted;	
       
   420 		break;
       
   421 	default :	PANIC;
       
   422 		}
       
   423 	//                                         (123456789)  ***
       
   424 	BLN("");
       
   425 	PNT("******************************************************");
       
   426 	LOG("***  CControlAppStateMachine::SetState(%S)  ***", &StateString);
       
   427 	PNT("******************************************************");
       
   428 	BLN("");
       
   429 	}
       
   430 
       
   431 // State Machine's publicly visible interface...
       
   432 //		Initial
       
   433 void CControlAppStateMachine::StartUp()
       
   434 	{
       
   435 	PNT("CControlAppStateMachine::StartUp() - event will be forwarded to current state\n");
       
   436 	__ASSERT_ALWAYS(iCurrentState, PANIC);
       
   437 	// Could implement global USB enabled/disabled policy here by allowing/preventing event reaching current state
       
   438 	iCurrentState->StartUp();
       
   439 	}
       
   440 
       
   441 //		B-Stopped, B-Started
       
   442 void CControlAppStateMachine::IdPinPresent()
       
   443 	{
       
   444 	PNT("CControlAppStateMachine::IdPinPresent() - event will be forwarded to current state\n");
       
   445 	__ASSERT_ALWAYS(iCurrentState, PANIC);
       
   446 	// Could implement global USB enabled/disabled policy here by allowing/preventing event reaching current state
       
   447 	iCurrentState->IdPinPresent(); 
       
   448 	}
       
   449 	
       
   450 //		B-Stopped, A-Started
       
   451 void CControlAppStateMachine::VBusRise()
       
   452 	{
       
   453 	PNT("CControlAppStateMachine::VBusRise() - event will be forwarded to current state\n");
       
   454 	__ASSERT_ALWAYS(iCurrentState, PANIC);
       
   455 	// Could implement global USB enabled/disabled policy here by allowing/preventing event reaching current state		
       
   456 	iCurrentState->VBusRise();
       
   457 	}
       
   458 
       
   459 //		B-Started, A-Started
       
   460 void CControlAppStateMachine::VBusDrop()
       
   461 	{
       
   462 	PNT("CControlAppStateMachine::VBusDrop() - event will be forwarded to current state\n");
       
   463 	__ASSERT_ALWAYS(iCurrentState, PANIC);
       
   464 	// Could implement global USB enabled/disabled policy here by allowing/preventing event reaching current state
       
   465 	iCurrentState->VBusDrop();
       
   466 	}
       
   467 
       
   468 //		A-Started, A-Stopped
       
   469 void CControlAppStateMachine::IdPinAbsent()
       
   470 	{
       
   471 	PNT("CControlAppStateMachine::IdPinAbsent() - event will be forwarded to current state\n");
       
   472 	__ASSERT_ALWAYS(iCurrentState, PANIC);
       
   473 	// Could implement global USB enabled/disabled policy here by allowing/preventing event reaching current state
       
   474 	iCurrentState->IdPinAbsent();
       
   475 	}
       
   476 
       
   477 //		A-Started
       
   478 void CControlAppStateMachine::ConnectionIdle()
       
   479 	{
       
   480 	PNT("CControlAppStateMachine::ConnectionIdle() - event will be forwarded to current state\n");
       
   481 	__ASSERT_ALWAYS(iCurrentState, PANIC);
       
   482 	// Could implement global USB enabled/disabled policy here by allowing/preventing event reaching current state
       
   483 	iCurrentState->ConnectionIdle();
       
   484 	}
       
   485 
       
   486 //		A-Started
       
   487 void CControlAppStateMachine::ConnectionActive()
       
   488 	{
       
   489 	PNT("CControlAppStateMachine::ConnectionActive() - event will be forwarded to current state\n");
       
   490 	__ASSERT_ALWAYS(iCurrentState, PANIC);
       
   491 	// Could implement global USB enabled/disabled policy here by allowing/preventing event reaching current state
       
   492 	iCurrentState->ConnectionActive();
       
   493 	}
       
   494 
       
   495 //		B-Stopped, A-Stopped, B-Started, A-Started
       
   496 void CControlAppStateMachine::RequestSessionCalled()
       
   497 	{
       
   498 	PNT("CControlAppStateMachine::RequestSessionCalled() - event will be forwarded to current state\n");
       
   499 	__ASSERT_ALWAYS(iCurrentState, PANIC);
       
   500 	// Could implement global USB enabled/disabled policy here by allowing/preventing event reaching current state
       
   501 	iCurrentState->RequestSessionCalled();
       
   502 	}
       
   503 
       
   504 //		B-Started
       
   505 void CControlAppStateMachine::SrpTimeout()
       
   506 	{
       
   507 	PNT("CControlAppStateMachine::SrpTimeout() - event will be forwarded to current state\n");
       
   508 	__ASSERT_ALWAYS(iCurrentState, PANIC);
       
   509 	// Could implement global USB enabled/disabled policy here by allowing/preventing event reaching current state
       
   510 	iCurrentState->SrpTimeout();
       
   511 	}
       
   512 
       
   513 //		A-Stopped
       
   514 void CControlAppStateMachine::SrpDetected()
       
   515 	{
       
   516 	PNT("CControlAppStateMachine::SrpDetected() - event will be forwarded to current state\n");
       
   517 	__ASSERT_ALWAYS(iCurrentState, PANIC);
       
   518 	// Could implement global USB enabled/disabled policy here by allowing/preventing event reaching current state
       
   519 	iCurrentState->SrpDetected();
       
   520 	}
       
   521 
       
   522 //		A-Started, A-Stopped
       
   523 void CControlAppStateMachine::VBusError()
       
   524 	{
       
   525 	PNT("CControlAppStateMachine::VBusError() - event will be forwarded to current state\n");
       
   526 	__ASSERT_ALWAYS(iCurrentState, PANIC);
       
   527 	// Could implement global USB enabled/disabled policy here by allowing/preventing event reaching current state
       
   528 	iCurrentState->VBusError();
       
   529 	}
       
   530 
       
   531 void CControlAppStateMachine::ResetInactivityTimer()
       
   532 	{
       
   533 	PNT("CControlAppStateMachine::ResetInactivityTimer()\n");
       
   534 	iInactivityTimer->Reset();
       
   535 	}
       
   536 
       
   537 void CControlAppStateMachine::CancelInactivityTimer()
       
   538 	{
       
   539 	PNT("CControlAppStateMachine::CancelInactivityTimer()\n");
       
   540 	iInactivityTimer->Cancel();
       
   541 	}
       
   542 
       
   543 void CControlAppStateMachine::InactivityTimerExpired()
       
   544 	{
       
   545 	PNT("CControlAppStateMachine::InactivityTimerExpired()\n");
       
   546 	iCurrentState->InactivityTimerExpired();
       
   547 	}
       
   548 
       
   549 
       
   550 CControlAppStateMachine::CInactivityTimer* CControlAppStateMachine::CInactivityTimer::NewL(CControlAppStateMachine& aParentStateMachine)
       
   551 	{
       
   552 	PNT("CControlAppStateMachine::CInactivityTimer::NewL()\n");
       
   553 	CControlAppStateMachine::CInactivityTimer* self = new(ELeave) CControlAppStateMachine::CInactivityTimer(aParentStateMachine);
       
   554 	CleanupStack::PushL(self);
       
   555 	self->ConstructL();
       
   556 	CleanupStack::Pop(self);
       
   557 	return self;
       
   558 	}
       
   559 
       
   560 CControlAppStateMachine::CInactivityTimer::~CInactivityTimer()
       
   561 	{
       
   562 	PNT("CControlAppStateMachine::CInactivityTimer::~CInactivityTimer()\n");
       
   563 	Cancel();
       
   564 	iTimer.Close();
       
   565 	}
       
   566 
       
   567 CControlAppStateMachine::CInactivityTimer::CInactivityTimer(CControlAppStateMachine& aParentStateMachine)
       
   568 	: CActive(EPriorityStandard)
       
   569 	, iParentStateMachine(aParentStateMachine)
       
   570 	{
       
   571 	CActiveScheduler::Add(this);
       
   572 	}
       
   573 
       
   574 void CControlAppStateMachine::CInactivityTimer::ConstructL()
       
   575 	{
       
   576 	PNT("CControlAppStateMachine::CInactivityTimer::ConstructL\n");
       
   577 	TInt err = iTimer.CreateLocal();
       
   578 	LOG("CInactivityTimer::ConstructL	iTimer.CreateLocal() => %d",err);
       
   579 	User::LeaveIfError(err);
       
   580 	}
       
   581 
       
   582 void CControlAppStateMachine::CInactivityTimer::Reset()
       
   583 	{
       
   584 	PNT("CControlAppStateMachine::CInactivityTimer::Reset()\n");
       
   585 	if (IsActive())
       
   586 		{
       
   587 		PNT("CControlAppStateMachine::CInactivityTimer::Reset() - IsActive() so Cancel() before setting RTimer\n");
       
   588 		Cancel(); // Reset
       
   589 		}
       
   590 	iTimer.After(iStatus, KInactivityTimerPeriod); // 10 sec
       
   591 	SetActive();
       
   592 	}
       
   593 	
       
   594 void CControlAppStateMachine::CInactivityTimer::RunL()
       
   595 	{
       
   596 	PNT("CControlAppStateMachine::CInactivityTimer::RunL() - calling iParentState.InactivityTimerExpired()\n");
       
   597 	iParentStateMachine.InactivityTimerExpired();
       
   598 	}
       
   599 
       
   600 void CControlAppStateMachine::CInactivityTimer::DoCancel()
       
   601 	{
       
   602 	PNT("CControlAppStateMachine::CInactivityTimer::DoCancel()\n");
       
   603 	iTimer.Cancel();
       
   604 	}
       
   605 
       
   606 
       
   607 // Base state...
       
   608 CControlAppStateMachine::CControlAppStateBase::CControlAppStateBase(CControlAppStateMachine& aParentStateMachine)
       
   609 	: iParentStateMachine(aParentStateMachine)
       
   610 	{
       
   611 	}
       
   612 
       
   613 void CControlAppStateMachine::CControlAppStateBase::StartUp()
       
   614 	{
       
   615 	PNT("CControlAppStateMachine::CControlAppStateBase::StartUp() - ERROR: unexpected event in current state\n");
       
   616 	DBG_PANIC;
       
   617 	}
       
   618 
       
   619 void CControlAppStateMachine::CControlAppStateBase::IdPinPresent()
       
   620 	{
       
   621 	PNT("CControlAppStateMachine::CControlAppStateBase::IdPinPresent() - ERROR: unexpected event in current state\n");
       
   622 	DBG_PANIC;
       
   623 	}
       
   624 	
       
   625 void CControlAppStateMachine::CControlAppStateBase::VBusRise()
       
   626 	{
       
   627 	PNT("CControlAppStateMachine::CControlAppStateBase::VBusRise() - ERROR: unexpected event in current state\n");
       
   628 	DBG_PANIC;
       
   629 	}
       
   630 
       
   631 void CControlAppStateMachine::CControlAppStateBase::VBusDrop()
       
   632 	{
       
   633 	PNT("CControlAppStateMachine::CControlAppStateBase::VBusDrop() - ERROR: unexpected event in current state\n");
       
   634 	DBG_PANIC;
       
   635 	}
       
   636 
       
   637 void CControlAppStateMachine::CControlAppStateBase::IdPinAbsent()
       
   638 	{
       
   639 	PNT("CControlAppStateMachine::CControlAppStateBase::IdPinAbsent() - ERROR: unexpected event in current state\n");
       
   640 	DBG_PANIC;
       
   641 	}
       
   642 
       
   643 void CControlAppStateMachine::CControlAppStateBase::ConnectionIdle()
       
   644 	{
       
   645 	PNT("CControlAppStateMachine::CControlAppStateBase::ConnectionIdle() - ERROR: unexpected event in current state\n");
       
   646 	DBG_PANIC;
       
   647 	}
       
   648 
       
   649 void CControlAppStateMachine::CControlAppStateBase::ConnectionActive()
       
   650 	{
       
   651 	PNT("CControlAppStateMachine::CControlAppStateBase::ConnectionActive() - ERROR: unexpected event in current state\n");
       
   652 	DBG_PANIC;
       
   653 	}
       
   654 
       
   655 void CControlAppStateMachine::CControlAppStateBase::InactivityTimerExpired()
       
   656 	{
       
   657 	PNT("CControlAppStateMachine::CControlAppStateBase::InactivityTimerExpired() - ERROR: unexpected event in current state\n");
       
   658 	DBG_PANIC;
       
   659 	}
       
   660 
       
   661 void CControlAppStateMachine::CControlAppStateBase::RequestSessionCalled()
       
   662 	{
       
   663 	PNT("CControlAppStateMachine::CControlAppStateBase::RequestSessionCalled() - ERROR: unexpected event in current state\n");
       
   664 	DBG_PANIC;
       
   665 	}
       
   666 
       
   667 void CControlAppStateMachine::CControlAppStateBase::SrpTriggered()
       
   668 	{
       
   669 	PNT("CControlAppStateMachine::CControlAppStateBase::SrpTriggered() - ERROR: unexpected event in current state\n");
       
   670 	DBG_PANIC;
       
   671 	}
       
   672 
       
   673 void CControlAppStateMachine::CControlAppStateBase::SrpTimeout()
       
   674 	{
       
   675 	PNT("CControlAppStateMachine::CControlAppStateBase::SrpTimeout() - ERROR: unexpected event in current state\n");
       
   676 	DBG_PANIC;
       
   677 	}
       
   678 
       
   679 void CControlAppStateMachine::CControlAppStateBase::SrpDetected()
       
   680 	{
       
   681 	PNT("CControlAppStateMachine::CControlAppStateBase::SrpDetected() - ERROR: unexpected event in current state\n");
       
   682 	DBG_PANIC;
       
   683 	}
       
   684 
       
   685 void CControlAppStateMachine::CControlAppStateBase::VBusError()
       
   686 	{
       
   687 	PNT("CControlAppStateMachine::CControlAppStateBase::VBusError() - ERROR: unexpected event in current state\n");
       
   688 	DBG_PANIC;
       
   689 	}
       
   690 
       
   691 
       
   692 // Initial state...
       
   693 CControlAppStateMachine::CControlAppStateInitial::CControlAppStateInitial(CControlAppStateMachine& aParentStateMachine)
       
   694 	: CControlAppStateBase(aParentStateMachine)
       
   695 	{
       
   696 	}
       
   697 
       
   698 void CControlAppStateMachine::CControlAppStateInitial::VBusRise()
       
   699 	{
       
   700 	PNT("CControlAppStateMachine::CControlAppStateInitial::VBusRise() - no action: wait for StartUp event\n");
       
   701 	}
       
   702 
       
   703 void CControlAppStateMachine::CControlAppStateInitial::VBusDrop()
       
   704 	{
       
   705 	PNT("CControlAppStateMachine::CControlAppStateInitial::VBusDrop() - no action: wait for StartUp event\n");
       
   706 	}
       
   707 
       
   708 void CControlAppStateMachine::CControlAppStateInitial::IdPinPresent()
       
   709 	{
       
   710 	PNT("CControlAppStateMachine::CControlAppStateInitial::IdPinPresent() - no action: wait for StartUp event\n");
       
   711 	}
       
   712 
       
   713 void CControlAppStateMachine::CControlAppStateInitial::IdPinAbsent()
       
   714 	{
       
   715 	PNT("CControlAppStateMachine::CControlAppStateInitial::IdPinAbsent() - no action: wait for StartUp event\n");
       
   716 	}
       
   717 
       
   718 void CControlAppStateMachine::CControlAppStateInitial::ConnectionIdle()
       
   719 	{
       
   720 	PNT("CControlAppStateMachine::CControlAppStateInitial::ConnectionIdle() - no action: wait for StartUp event\n");
       
   721 	}
       
   722 
       
   723 void CControlAppStateMachine::CControlAppStateInitial::ConnectionActive()
       
   724 	{
       
   725 	PNT("CControlAppStateMachine::CControlAppStateInitial::ConnectionActive() - no action: wait for StartUp event\n");
       
   726 	}
       
   727 
       
   728 void CControlAppStateMachine::CControlAppStateInitial::StartUp()
       
   729 	{
       
   730 	PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()\n");
       
   731 	// Determine state to move to from the Initial
       
   732 	if (iParentStateMachine.iParentControlAppEngine.GetIdPin())
       
   733 		{
       
   734 		PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	Id Pin detected\n");
       
   735 		TInt err = iParentStateMachine.iParentControlAppEngine.StartUsbServices();
       
   736 		if (!err) // Started USB services
       
   737 			{
       
   738 			PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	Started USB services\n");
       
   739 			iParentStateMachine.SetState(EStateAServicesStarted);
       
   740 			err = iParentStateMachine.iParentControlAppEngine.EnableFunctionDriverLoading();
       
   741 			if (!err) // Started USB services & enabled FD loading
       
   742 				{
       
   743 				PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	Enabled FD loading\n");
       
   744 				}
       
   745 			else // Started USB services & couldn't enable FD loading
       
   746 				{
       
   747 				PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	Couldn't enable FD loading\n");
       
   748 				_LIT(KMsgCouldntEnableFdLoading,	"Error: Couldn't enable driver loading - can't function as host");
       
   749 				iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntEnableFdLoading);
       
   750 				}
       
   751 			// Regardless of whether enabling FD loading succeeded or not do BusRequest() to power VBus
       
   752 			err = iParentStateMachine.iParentControlAppEngine.BusRequest(); 
       
   753 			if (!err)
       
   754 				{
       
   755 				PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	BusRequest() successful\n");
       
   756 				}
       
   757 			else
       
   758 				{
       
   759 				PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	BusRequest() failed\n");
       
   760 				// This error only indicates whether USBMAN server got the request without any problem.
       
   761 				// VBus could still fail to go up but this is only likely if there is a Bus Error - this is 
       
   762 				// handled in a separate event though.
       
   763 				_LIT(KMsgCouldntPowerUsbDevice,	"Error: Couldn't power USB device - press 'R' to try again");
       
   764 				iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntPowerUsbDevice);
       
   765 				// CControlAppStateAServicesStarted::VBusRise() ensures Inactivity Timer is activated when the BusRequest()
       
   766 				// is successful, but when it fails we must ensure USB services will be eventually stopped:
       
   767 				iParentStateMachine.ResetInactivityTimer();
       
   768 				}
       
   769 			}
       
   770 		else // Couldn't start USB services
       
   771 			{
       
   772 			PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	Couldn't start USB services\n");
       
   773 			_LIT(KMsgCouldntStartUsb,	"Error: Couldn't start USB services");
       
   774 			iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntStartUsb);
       
   775 			iParentStateMachine.SetState(EStateAServicesStopped); // Since Id Pin is present
       
   776 			}
       
   777 		} // if (iParentStateMachine.iParentControlAppEngine.GetIdPin())
       
   778 	else // i.e. no Id Pin
       
   779 		{
       
   780 		if (iParentStateMachine.iParentControlAppEngine.GetVBus())
       
   781 			{
       
   782 			PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	Id Pin Absent & VBus detected\n");
       
   783 			TInt err = iParentStateMachine.iParentControlAppEngine.StartUsbServices();
       
   784 			if (!err) // Started USB services
       
   785 				{
       
   786 				PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	Started USB services\n");
       
   787 				iParentStateMachine.SetState(EStateBServicesStarted);
       
   788 				err = iParentStateMachine.iParentControlAppEngine.EnableFunctionDriverLoading();
       
   789 				if (!err) // Started USB services & enabled FD loading
       
   790 					{
       
   791 					PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	Enabled FD loading\n");
       
   792 					}
       
   793 				else // Started USB services & couldn't enable FD loading
       
   794 					{
       
   795 					PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	couldn't enable FD loading\n");
       
   796 					_LIT(KMsgCouldntEnableFdLoading,	"Error: Couldn't enable driver loading - can't function as host");
       
   797 					iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntEnableFdLoading);
       
   798 					}
       
   799 				}
       
   800 			else // Couldn't start USB services
       
   801 				{
       
   802 				PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	Couldn't start USB services\n");
       
   803 				iParentStateMachine.SetState(EStateBServicesStopped); // even though VBus
       
   804 				_LIT(KMsgCouldntStartUsb,	"Error: Couldn't start USB services");
       
   805 				iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntStartUsb);
       
   806 				}
       
   807 			} // if (iParentStateMachine.iParentControlAppEngine.GetVBus())
       
   808 		else  // if (iParentStateMachine.iParentControlAppEngine.GetVBus())
       
   809 			{
       
   810 			PNT("CControlAppStateMachine::CControlAppStateInitial::StartUp()	Id Pin Absent & VBus not detected\n");
       
   811 			iParentStateMachine.SetState(EStateBServicesStopped);
       
   812 			}
       
   813 		} // else i.e. no Id Pin
       
   814 	}
       
   815 
       
   816 
       
   817 // BServicesStopped state...
       
   818 CControlAppStateMachine::CControlAppStateBServicesStopped::CControlAppStateBServicesStopped(CControlAppStateMachine& aParentStateMachine)
       
   819 	: CControlAppStateBase(aParentStateMachine)
       
   820 	{
       
   821 	}
       
   822 
       
   823 // Power detected on VBus
       
   824 // Try to move to B-Started
       
   825 void CControlAppStateMachine::CControlAppStateBServicesStopped::VBusRise()
       
   826 	{
       
   827 	PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::VBusRise()	VBus detected\n");
       
   828 	TInt err = iParentStateMachine.iParentControlAppEngine.StartUsbServices();
       
   829 	if (!err) // Started USB services
       
   830 		{
       
   831 		PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::VBusRise()	Started USB services\n");
       
   832 		iParentStateMachine.SetState(EStateBServicesStarted);
       
   833 		err = iParentStateMachine.iParentControlAppEngine.EnableFunctionDriverLoading();
       
   834 		if (!err) // Started USB services & enabled FD loading
       
   835 			{
       
   836 			PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::VBusRise()	Enabled FD loading\n");
       
   837 			}
       
   838 		else // Started USB services & couldn't enable FD loading
       
   839 			{
       
   840 			PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::VBusRise()	couldn't enable FD loading\n");
       
   841 			_LIT(KMsgCouldntEnableFdLoading,	"Error: Couldn't enable driver loading - can't function as host");
       
   842 			iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntEnableFdLoading);
       
   843 			}
       
   844 		}
       
   845 	else // Couldn't start USB services
       
   846 		{
       
   847 		PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::VBusRise()	Couldn't start USB services\n");
       
   848 		_LIT(KMsgCouldntStartUsb,	"Error: Couldn't start USB services");
       
   849 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntStartUsb);
       
   850 		}
       
   851 	}
       
   852 
       
   853 // Id Pin detected
       
   854 // Try to move to A-Started
       
   855 void CControlAppStateMachine::CControlAppStateBServicesStopped::IdPinPresent()
       
   856 	{
       
   857 	PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::IdPinPresent()\n");
       
   858 	TInt err = iParentStateMachine.iParentControlAppEngine.StartUsbServices();
       
   859 	if (!err) // Started USB services
       
   860 		{
       
   861 		PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::IdPinPresent()	Started USB services\n");
       
   862 		iParentStateMachine.SetState(EStateAServicesStarted);
       
   863 		err = iParentStateMachine.iParentControlAppEngine.EnableFunctionDriverLoading();
       
   864 		if (!err) // Started USB services & enabled FD loading
       
   865 			{
       
   866 			PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::IdPinPresent()	Enabled FD loading\n");
       
   867 			}
       
   868 		else // Started USB services & couldn't enable FD loading
       
   869 			{
       
   870 			PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::IdPinPresent()	Couldn't enable FD loading\n");
       
   871 			_LIT(KMsgCouldntEnableFdLoading,	"Error: Couldn't enable driver loading - can't function as host");
       
   872 			iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntEnableFdLoading);
       
   873 			}
       
   874 		// Regardless of whether enabling FD loading succeeded or not, attempt to power VBus
       
   875 		err = iParentStateMachine.iParentControlAppEngine.BusRequest(); 
       
   876 		if (!err)
       
   877 			{
       
   878 			PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::IdPinPresent()	BusRequest() successful\n");
       
   879 			}
       
   880 		else
       
   881 			{
       
   882 			PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::IdPinPresent()	BusRequest() failed\n");
       
   883 			// This error only indicates whether USBMAN server got the request without any problem.
       
   884 			// VBus could still fail to go up but this is only likely if there is a Bus Error - this is 
       
   885 			// handled in a separate event though.
       
   886 			_LIT(KMsgCouldntPowerUsbDevice,	"Error: Couldn't power USB device - press 'R' to try again");
       
   887 			iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntPowerUsbDevice);
       
   888 			// CControlAppStateAServicesStarted::VBusRise() ensures Inactivity Timer is activated when the BusRequest()
       
   889 			// is successful, but when it fails we must ensure USB services will be eventually stopped:
       
   890 			iParentStateMachine.ResetInactivityTimer();
       
   891 			}
       
   892 		}
       
   893 	else // Couldn't start USB services
       
   894 		{
       
   895 		PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::IdPinPresent()	Couldn't start USB services\n");
       
   896 		iParentStateMachine.SetState(EStateAServicesStopped); // Since Id Pin is present
       
   897 		_LIT(KMsgCouldntStartUsb,	"Error: Couldn't start USB services");
       
   898 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntStartUsb);
       
   899 		}
       
   900 	}
       
   901 
       
   902 // RUsb::RequestSession() called from USB Aware App
       
   903 // Try to move to B-Started and trigger SRP
       
   904 void CControlAppStateMachine::CControlAppStateBServicesStopped::RequestSessionCalled()
       
   905 	{
       
   906 	PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::RequestSessionCalled()\n");
       
   907 	TInt err = iParentStateMachine.iParentControlAppEngine.StartUsbServices();
       
   908 	if (!err) // Started USB services
       
   909 		{
       
   910 		PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::RequestSessionCalled()	Started USB services\n");
       
   911 		iParentStateMachine.SetState(EStateBServicesStarted); // Do this before SrpTriggered()
       
   912 		err = iParentStateMachine.iParentControlAppEngine.EnableFunctionDriverLoading();
       
   913 		if (!err) // Started USB services & enabled FD loading
       
   914 			{
       
   915 			PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::RequestSessionCalled()	Enabled FD loading\n");	
       
   916 			iParentStateMachine.iCurrentState->SrpTriggered(); // Internally generated event in B-Started state
       
   917 			}
       
   918 		else // Started USB services & couldn't enable FD loading
       
   919 			{
       
   920 			PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::RequestSessionCalled()	Couldn't enable FD loading\n");
       
   921 			_LIT(KMsgCouldntEnableFdLoading,	"Error: Couldn't enable driver loading - can't function as host");
       
   922 			iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntEnableFdLoading);
       
   923 			// No point doing SRP since wont be able to enumerate peripheral
       
   924 			}
       
   925 		}
       
   926 	else // Couldn't start USB services
       
   927 		{
       
   928 		PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::RequestSessionCalled()	Couldn't start USB services\n");
       
   929 		_LIT(KMsgCouldntStartUsb,	"Error: Couldn't start USB services");
       
   930 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntStartUsb);
       
   931 		}
       
   932 	}
       
   933 
       
   934 void CControlAppStateMachine::CControlAppStateBServicesStopped::VBusDrop()
       
   935 	{
       
   936 	PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::VBusDrop()	no action: this A-Device dropped VBus before moving into this state\n");
       
   937 	DBG_PANIC;
       
   938 	}
       
   939 
       
   940 
       
   941 // BServicesStarted state...
       
   942 CControlAppStateMachine::CControlAppStateBServicesStarted::CControlAppStateBServicesStarted(CControlAppStateMachine& aParentStateMachine)
       
   943 	: CControlAppStateBase(aParentStateMachine)
       
   944 	{
       
   945 	}
       
   946 
       
   947 // VBus not being powered any more by remote device
       
   948 // Try to stop USB services and move to B-Stopped
       
   949 void CControlAppStateMachine::CControlAppStateBServicesStarted::VBusDrop()
       
   950 	{
       
   951 	PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::VBusDrop()\n");
       
   952 	TInt err = iParentStateMachine.iParentControlAppEngine.StopUsbServices();
       
   953 	if (!err) // Stopped USB services
       
   954 		{
       
   955 		PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::VBusDrop()	Stopped USB services\n");
       
   956 		iParentStateMachine.SetState(EStateBServicesStopped);
       
   957 		}
       
   958 	else // Couldn't stop USB services
       
   959 		{
       
   960 		PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::VBusDrop()	Couldn't stop USB services\n");
       
   961 		// Stay in current state B-Started.
       
   962 		// VBus probably dropped as a result of B-Plug being withdrawn (or remote A-Device powering down VBus).
       
   963 		// If A-Plug inserted, will move to A-Started from this state. (If VBus is re-powered, in the correct state too).
       
   964 		_LIT(KMsgCouldntStopUsb,	"Error: Couldn't stop USB services");
       
   965 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntStopUsb);
       
   966 		}
       
   967 	}
       
   968 
       
   969 // We've come from B-Stopped to trigger an SRP
       
   970 // If SRP times out (notification from USBMAN will be another separate event received) we'll return to B-Stopped.
       
   971 // If SRP successful, VBus will be powered and we can stay in the current state.
       
   972 void CControlAppStateMachine::CControlAppStateBServicesStarted::SrpTriggered()
       
   973 	{
       
   974 	PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::SrpTriggered()\n");
       
   975 	TInt err = iParentStateMachine.iParentControlAppEngine.BusRequest(); // SRP
       
   976 	// If no err, we don't have to do anything
       
   977 	if (!err)
       
   978 		{
       
   979 		PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::SrpTriggered()	BusRequest for SRP successful\n");
       
   980 		}
       
   981 	else // Was problem doing SRP - won't get SRP timeout event - go back to B-Stopped now
       
   982 		{
       
   983 		PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::SrpTriggered()	BusRequest for SRP failed\n");
       
   984 		_LIT(KMsgFailedBecomingHost,	"Error: Failed to become Host");
       
   985 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgFailedBecomingHost);
       
   986 		err = iParentStateMachine.iParentControlAppEngine.StopUsbServices();
       
   987 		if (!err) // Cleaned up by stopping USB services
       
   988 			{
       
   989 			PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::SrpTriggered()	Stopped USB services\n");
       
   990 			iParentStateMachine.SetState(EStateBServicesStopped);
       
   991 			}
       
   992 		else
       
   993 			{
       
   994 			PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::SrpTriggered()	Couldn't stop USB services\n");
       
   995 			_LIT(KMsgCouldntStopUsb,	"Error: Couldn't stop USB services");
       
   996 			iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntStopUsb);
       
   997 			// Stay in current state B-Started.
       
   998 			// Can re-try SRP by doing RequestSession() -> BusRequest() in this state.
       
   999 			// No event received if B-Plug is withdrawn.
       
  1000 			// If A-Plug inserted, will move to A-Started from this state.
       
  1001 			}
       
  1002 		}
       
  1003 	}
       
  1004 
       
  1005 // Message notification from USBMAN that SRP requested earlier has timed out.
       
  1006 // Try to move to B-Stopped
       
  1007 void CControlAppStateMachine::CControlAppStateBServicesStarted::SrpTimeout()
       
  1008 	{
       
  1009 	PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::SrpTimeout()\n");
       
  1010 	TInt err = iParentStateMachine.iParentControlAppEngine.StopUsbServices();
       
  1011 	if (!err) // Stopped USB services
       
  1012 		{
       
  1013 		PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::SrpTimeout()	Stopped USB services\n");
       
  1014 		iParentStateMachine.SetState(EStateBServicesStopped);
       
  1015 		}
       
  1016 	else // Couldn't stop USB services
       
  1017 		{
       
  1018 		PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::SrpTimeout()	Couldn't stop USB services\n");
       
  1019 		_LIT(KMsgCouldntStopUsb,	"Error: Couldn't stop USB services");
       
  1020 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntStopUsb);
       
  1021 		// Stay in current state B-Started. 
       
  1022 		// Can re-try SRP by doing RequestSession() -> BusRequest() in this state.
       
  1023 		// No event received if B-Plug is withdrawn.
       
  1024 		// If A-Plug inserted, will move to A-Started from this state.
       
  1025 		}
       
  1026 	}
       
  1027 
       
  1028 // Will see when SRP successful
       
  1029 void CControlAppStateMachine::CControlAppStateBServicesStarted::VBusRise()
       
  1030 	{
       
  1031 	PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::VBusRise()\n");
       
  1032 	}
       
  1033 
       
  1034 // Time taken to notice the drop in VBus by which time the Id Pin has been inserted.
       
  1035 // Cater for the IdPinPresent event in this state therefore.
       
  1036 // Try to power VBus and move to A-Started state
       
  1037 void CControlAppStateMachine::CControlAppStateBServicesStarted::IdPinPresent()
       
  1038 	{
       
  1039 	PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::IdPinPresent()\n");
       
  1040 	iParentStateMachine.SetState(EStateAServicesStarted); // Since Id Pin present and USB services still started.
       
  1041 	// USB services already started but may have failed to enable FD loading previously so try again (calling twice does no harm)
       
  1042 	TInt err = iParentStateMachine.iParentControlAppEngine.EnableFunctionDriverLoading();
       
  1043 	if (!err)
       
  1044 		{
       
  1045 		PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::IdPinPresent()	Enabled FD loading\n");	
       
  1046 		}
       
  1047 	else 
       
  1048 		{
       
  1049 		PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::IdPinPresent()	Couldn't enable FD loading\n");
       
  1050 		_LIT(KMsgCouldntEnableFdLoading,	"Error: Couldn't enable driver loading - can't function as host");
       
  1051 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntEnableFdLoading);
       
  1052 		}
       
  1053 	err = iParentStateMachine.iParentControlAppEngine.BusRequest();
       
  1054 	if (!err) // Powered VBus
       
  1055 		{
       
  1056 		PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::IdPinPresent()	Powered VBus\n");
       
  1057 		}
       
  1058 	else // Couldn't power VBus
       
  1059 		{
       
  1060 		PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::IdPinPresent()	Couldn't power VBus\n");
       
  1061 		// RequestSession() in A-Started will have the effect of powering VBus
       
  1062 		_LIT(KMsgCouldntPowerUsbDevice,	"Error: Couldn't power USB device - press 'R' to try again");
       
  1063 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntPowerUsbDevice);
       
  1064 		// CControlAppStateAServicesStarted::VBusRise() ensures Inactivity Timer is activated when the BusRequest()
       
  1065 		// is successful, but when it fails we must ensure USB services will be eventually stopped:
       
  1066 		iParentStateMachine.ResetInactivityTimer();
       
  1067 		}
       
  1068 	}
       
  1069 
       
  1070 // Let underlying OTG deal with what this means in this state
       
  1071 void CControlAppStateMachine::CControlAppStateBServicesStarted::RequestSessionCalled()
       
  1072 	{
       
  1073 	PNT("CControlAppStateMachine::CControlAppStateBServicesStarted::RequestSessionCalled()\n");
       
  1074 	(void) iParentStateMachine.iParentControlAppEngine.BusRequest(); // Not interested in error code
       
  1075 	}
       
  1076 
       
  1077 
       
  1078 // AServicesStarted state...
       
  1079 CControlAppStateMachine::CControlAppStateAServicesStarted::CControlAppStateAServicesStarted(CControlAppStateMachine& aParentStateMachine)
       
  1080 	: CControlAppStateBase(aParentStateMachine)
       
  1081 	{
       
  1082 	}
       
  1083 
       
  1084 // A-Plug has been removed
       
  1085 // Try to move to B-Stopped state
       
  1086 void CControlAppStateMachine::CControlAppStateAServicesStarted::IdPinAbsent()
       
  1087 	{
       
  1088 	PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::IdPinAbsent()\n");
       
  1089 	// BusDrop() not needed as VBus unpowered at a lower level if Id Pin not present
       
  1090 	TInt err = iParentStateMachine.iParentControlAppEngine.StopUsbServices();
       
  1091 	if (!err) // Stopped USB services
       
  1092 		{
       
  1093 		PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::IdPinAbsent()	Stopped USB services	(BusDrop unnecessary here)\n");
       
  1094 		iParentStateMachine.SetState(EStateBServicesStopped);
       
  1095 		}
       
  1096 	else // Couldn't stop USB services started & Id Pin absent - go to B-Started
       
  1097 		{
       
  1098 		PNT("CControlAppStateMachine::CControlAppStateBServicesStopped::IdPinAbsent()	Couldn't stop USB services\n");
       
  1099 		// Move to B-Started since no Id Pin but USB services still started.
       
  1100 		iParentStateMachine.SetState(EStateBServicesStarted);
       
  1101 		_LIT(KMsgCouldntStopUsb,	"Error: Couldn't stop USB services");
       
  1102 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntStopUsb);
       
  1103 		}
       
  1104 	}
       
  1105 
       
  1106 // Remote B-Device sent SRP
       
  1107 // It is possible for VBus to be down in A-Started state so SRP could arrive
       
  1108 void CControlAppStateMachine::CControlAppStateAServicesStarted::SrpDetected()
       
  1109 	{
       
  1110 	PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::SrpDetected()\n");
       
  1111 	TInt err = iParentStateMachine.iParentControlAppEngine.BusRespondSrp();
       
  1112 	if (!err)
       
  1113 		{
       
  1114 		PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::SrpDetected()	BusRespondSrp() succeeded\n");
       
  1115 		}
       
  1116 	else
       
  1117 		{
       
  1118 		PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::SrpDetected()	BusRespondSrp() error\n");
       
  1119 		_LIT(KMsgCouldntPowerUsbDevice,	"Error: Couldn't power USB device");
       
  1120 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntPowerUsbDevice);
       
  1121 		}
       
  1122 	}
       
  1123 
       
  1124 void CControlAppStateMachine::CControlAppStateAServicesStarted::VBusRise()
       
  1125 	{
       
  1126 	PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::VBusRise() - start Inactivity Timer...\n");
       
  1127 	iParentStateMachine.ResetInactivityTimer();
       
  1128 	}
       
  1129 
       
  1130 void CControlAppStateMachine::CControlAppStateAServicesStarted::ConnectionIdle()
       
  1131 	{
       
  1132 	PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::ConnectionIdle()\n");
       
  1133 	iParentStateMachine.ResetInactivityTimer();
       
  1134 	}
       
  1135 
       
  1136 void CControlAppStateMachine::CControlAppStateAServicesStarted::ConnectionActive()
       
  1137 	{
       
  1138 	PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::ConnectionActive()\n");
       
  1139 	iParentStateMachine.CancelInactivityTimer();
       
  1140 	}
       
  1141 
       
  1142 void CControlAppStateMachine::CControlAppStateAServicesStarted::InactivityTimerExpired()
       
  1143 	{
       
  1144 	PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::InactivityTimerExpired()\n");
       
  1145 	TInt err = iParentStateMachine.iParentControlAppEngine.BusDrop();
       
  1146 	if (!err)
       
  1147 		{
       
  1148 		PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::InactivityTimerExpired()	Dropped VBus\n");
       
  1149 		err = iParentStateMachine.iParentControlAppEngine.StopUsbServices();
       
  1150 		if (!err) // Stopped USB services
       
  1151 			{
       
  1152 			PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::InactivityTimerExpired()	Stopped USB services\n");
       
  1153 			iParentStateMachine.SetState(EStateAServicesStopped);
       
  1154 			}
       
  1155 		else // Couldn't stop USB services started & Id Pin present - stay in A-Started
       
  1156 			{
       
  1157 			PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::InactivityTimerExpired()	Couldn't stop USB services\n");
       
  1158 			_LIT(KMsgCouldntStopUsb,	"Error: Couldn't stop USB services");
       
  1159 			iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntStopUsb);
       
  1160 			}
       
  1161 		}
       
  1162 	else
       
  1163 		{
       
  1164 		PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::InactivityTimerExpired()	Can't drop VBus\n");
       
  1165 		_LIT(KMsgCouldntDropVBus,	"Error: Couldn't drop VBus to save power. Unplug device.");
       
  1166 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntDropVBus);
       
  1167 		}
       
  1168 	}
       
  1169 
       
  1170 // Let underlying OTG deal with what this means in this state
       
  1171 void CControlAppStateMachine::CControlAppStateAServicesStarted::RequestSessionCalled()
       
  1172 	{
       
  1173 	PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::RequestSessionCalled()\n");
       
  1174 	(void) iParentStateMachine.iParentControlAppEngine.BusRequest(); // Not interested in error code
       
  1175 	}
       
  1176 
       
  1177 void CControlAppStateMachine::CControlAppStateAServicesStarted::VBusError()
       
  1178 	{
       
  1179 	PNT("CControlAppStateMachine::CControlAppStateAServicesStarted::VBusError()\n");
       
  1180 	iParentStateMachine.CancelInactivityTimer(); // To prevent a BusDrop being done - VBus is already down.
       
  1181 	(void) iParentStateMachine.iParentControlAppEngine.ClearVBusError(); // Not interested in error code
       
  1182 	// A call to RequestSession() will be necessary to raise VBus again
       
  1183 	_LIT(KMsgVBusError,	"Error: Couldn't power USB device - please unplug");
       
  1184 	iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgVBusError);	
       
  1185 	}
       
  1186 
       
  1187 
       
  1188 // AServicesStopped state...
       
  1189 CControlAppStateMachine::CControlAppStateAServicesStopped::CControlAppStateAServicesStopped(CControlAppStateMachine& aParentStateMachine)
       
  1190 	: CControlAppStateBase(aParentStateMachine)
       
  1191 	{
       
  1192 	}
       
  1193 
       
  1194 // A-Plug been removed
       
  1195 // Try to move to B-Stopped
       
  1196 void CControlAppStateMachine::CControlAppStateAServicesStopped::IdPinAbsent()
       
  1197 	{
       
  1198 	PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::IdPinAbsent()\n");
       
  1199 	iParentStateMachine.SetState(EStateBServicesStopped);
       
  1200 	}
       
  1201 
       
  1202 // Remote B-Device sent SRP
       
  1203 // Try to move to A-Started
       
  1204 void CControlAppStateMachine::CControlAppStateAServicesStopped::SrpDetected()
       
  1205 	{
       
  1206 	PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::SrpDetected()\n");
       
  1207 	TInt err = iParentStateMachine.iParentControlAppEngine.StartUsbServices();
       
  1208 	if (!err) // Started USB services
       
  1209 		{
       
  1210 		PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::SrpDetected()	Started USB services\n");
       
  1211 		iParentStateMachine.SetState(EStateAServicesStarted);
       
  1212 		// Need to enable FD loading for when the roles revert back to their defaults
       
  1213 		// for after the role swap that follows the BusRespondSRP()
       
  1214 		err = iParentStateMachine.iParentControlAppEngine.EnableFunctionDriverLoading();
       
  1215 		if (!err) // Started USB services & enabled FD loading
       
  1216 			{
       
  1217 			PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::SrpDetected()	Enabled FD loading\n");
       
  1218 			}
       
  1219 		else // Started USB services & couldn't enable FD loading
       
  1220 			{
       
  1221 			PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::SrpDetected()	Couldn't enable FD loading\n");
       
  1222 			_LIT(KMsgCouldntEnableFdLoading,	"Error: Couldn't enable driver loading - can't function as host");
       
  1223 			iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntEnableFdLoading);
       
  1224 			}
       
  1225 		// Regardless of whether FD loading succeeded
       
  1226 		err = iParentStateMachine.iParentControlAppEngine.BusRespondSrp();
       
  1227 		if (!err)
       
  1228 			{
       
  1229 			PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::SrpDetected()	BusRespondSrp() succeeded\n");
       
  1230 			}
       
  1231 		else
       
  1232 			{
       
  1233 			PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::SrpDetected()	BusRespondSrp() error\n");
       
  1234 			// In A-Started, if no activity for a while (which will be the case if BusRespondSRP() failed = VBus unpowered)
       
  1235 			// Inactivity Timer will expire and send the system back to A-Stopped.
       
  1236 			_LIT(KMsgCouldntPowerUsbDevice,	"Error: Couldn't power USB device");
       
  1237 			iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntPowerUsbDevice);
       
  1238 			}
       
  1239 		}
       
  1240 	else // Couldn't start USB services
       
  1241 		{
       
  1242 		PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::SrpDetected()	Couldn't start USB services\n");
       
  1243 		_LIT(KMsgCouldntStartUsb,	"Error: Couldn't start USB services");
       
  1244 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntStartUsb);
       
  1245 		}
       
  1246 	}
       
  1247 
       
  1248 // Local USB Aware App would like to use USB services
       
  1249 // Try to move to A-Started
       
  1250 void CControlAppStateMachine::CControlAppStateAServicesStopped::RequestSessionCalled()
       
  1251 	{
       
  1252 	PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::RequestSessionCalled()\n");
       
  1253 	TInt err = iParentStateMachine.iParentControlAppEngine.StartUsbServices();
       
  1254 	if (!err) // Started USB services
       
  1255 		{
       
  1256 		PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::RequestSessionCalled()	Started USB services\n");
       
  1257 		iParentStateMachine.SetState(EStateAServicesStarted);
       
  1258 		// In A-Started, if no activity for a while, which will be the case if 
       
  1259 		// EnableFunctionDriverLoading() fails (= no host activity) or BusRequest() fails (= no VBus)
       
  1260 		// Inactivity Timer will expire soon and send the system back to A-Stopped.
       
  1261 		err = iParentStateMachine.iParentControlAppEngine.EnableFunctionDriverLoading();
       
  1262 		if (!err) // Started USB services & enabled FD loading
       
  1263 			{
       
  1264 			PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::RequestSessionCalled()	Enabled FD loading\n");
       
  1265 			}
       
  1266 		else // Started USB services & couldn't enable FD loading
       
  1267 			{
       
  1268 			PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::RequestSessionCalled()	Couldn't enable FD loading\n");
       
  1269 			_LIT(KMsgCouldntEnableFdLoading,	"Error: Couldn't enable driver loading - can't function as host");
       
  1270 			iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntEnableFdLoading);
       
  1271 			}
       
  1272 		// Regardless of whether enabling FD loading succeeded
       
  1273 		err = iParentStateMachine.iParentControlAppEngine.BusRequest();
       
  1274 		if (!err)
       
  1275 			{
       
  1276 			PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::RequestSessionCalled()	BusRequest() succeeded\n");
       
  1277 			}
       
  1278 		else
       
  1279 			{
       
  1280 			PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::RequestSessionCalled()	BusRequest() error\n");
       
  1281 			_LIT(KMsgCouldntPowerUsbDevice,	"Error: Couldn't power USB device");
       
  1282 			iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntPowerUsbDevice);
       
  1283 			// CControlAppStateAServicesStarted::VBusRise() ensures Inactivity Timer is activated when the BusRequest()
       
  1284 			// is successful, but when it fails we must ensure USB services will be eventually stopped:
       
  1285 			iParentStateMachine.ResetInactivityTimer();
       
  1286 			}
       
  1287 		}
       
  1288 	else // Couldn't start USB services
       
  1289 		{
       
  1290 		PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::RequestSessionCalled()	Couldn't start USB services\n");
       
  1291 		_LIT(KMsgCouldntStartUsb,	"Error: Couldn't start USB services");
       
  1292 		iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgCouldntStartUsb);
       
  1293 		}
       
  1294 	}
       
  1295 
       
  1296 void CControlAppStateMachine::CControlAppStateAServicesStopped::VBusError()
       
  1297 	{
       
  1298 	PNT("CControlAppStateMachine::CControlAppStateAServicesStopped::VBusError()\n");
       
  1299 	(void) iParentStateMachine.iParentControlAppEngine.ClearVBusError(); // Not interested in error code
       
  1300 	// A call to RequestSession() will be necessary to raise VBus again
       
  1301 	_LIT(KMsgVBusError,	"Error: Couldn't power USB device - please unplug");
       
  1302 	iParentStateMachine.iParentControlAppEngine.DisplayUserMessage(KMsgVBusError);
       
  1303 	}
       
  1304 
       
  1305 
       
  1306 CIdPinWatcher* CIdPinWatcher::NewL(MControlAppEngineWatcherInterface& aControlAppEngine)
       
  1307 	{
       
  1308 	CIdPinWatcher* self = new(ELeave) CIdPinWatcher(aControlAppEngine);
       
  1309 	CleanupStack::PushL(self);
       
  1310 	self->ConstructL();
       
  1311 	CleanupStack::Pop(self);
       
  1312 	return self;
       
  1313 	}
       
  1314 
       
  1315 CIdPinWatcher::~CIdPinWatcher()
       
  1316 	{
       
  1317 	Cancel();
       
  1318 	iIdPinProp.Close();
       
  1319 	}
       
  1320 
       
  1321 CIdPinWatcher::CIdPinWatcher(MControlAppEngineWatcherInterface& aControlAppEngine)
       
  1322 	: CActive(EPriorityStandard)
       
  1323 	, iParentControlAppEngine(aControlAppEngine)
       
  1324 	{
       
  1325 	CActiveScheduler::Add(this);
       
  1326 	}
       
  1327 
       
  1328 void CIdPinWatcher::ConstructL()
       
  1329 	{
       
  1330 	TInt err = iIdPinProp.Attach(KUidUsbManCategory, KUsbOtgIdPinPresentProperty);
       
  1331 	LOG("CIdPinWatcher::ConstructL iIdPinProp.Attach(KUidUsbManCategory, KUsbOtgIdPinPresentProperty) => %d",err);
       
  1332 	User::LeaveIfError(err);
       
  1333 
       
  1334 	SubscribeForNotification();
       
  1335 
       
  1336 	// Get the current value and update the Engine
       
  1337 	TInt val;
       
  1338 	err = iIdPinProp.Get(val);
       
  1339 	LOG("CIdPinWatcher::ConstructL iIdPinProp.Get(val) => %d",err);
       
  1340 	User::LeaveIfError(err);
       
  1341 	iParentControlAppEngine.SetIdPin(val);
       
  1342 	}
       
  1343 
       
  1344 void CIdPinWatcher::SubscribeForNotification()
       
  1345 	{
       
  1346 	iIdPinProp.Subscribe(iStatus);
       
  1347 	SetActive();
       
  1348 	}
       
  1349 	
       
  1350 void CIdPinWatcher::RunL()
       
  1351 	{
       
  1352 	SubscribeForNotification();
       
  1353 	// Get newly changed value
       
  1354 	TInt val;
       
  1355 	User::LeaveIfError(iIdPinProp.Get(val));
       
  1356 	// Update value in Engine
       
  1357 	iParentControlAppEngine.SetIdPin(val);
       
  1358 	}
       
  1359 
       
  1360 void CIdPinWatcher::DoCancel()
       
  1361 	{
       
  1362 	iIdPinProp.Cancel();
       
  1363 	}
       
  1364 
       
  1365 
       
  1366 	
       
  1367 CVBusWatcher* CVBusWatcher::NewL(MControlAppEngineWatcherInterface& aControlAppEngine)
       
  1368 	{
       
  1369 	CVBusWatcher* self = new(ELeave) CVBusWatcher(aControlAppEngine);
       
  1370 	CleanupStack::PushL(self);
       
  1371 	self->ConstructL();
       
  1372 	CleanupStack::Pop(self);
       
  1373 	return self;
       
  1374 	}
       
  1375 
       
  1376 CVBusWatcher::~CVBusWatcher()
       
  1377 	{
       
  1378 	Cancel();
       
  1379 	iVBusProp.Close();
       
  1380 	}
       
  1381 
       
  1382 CVBusWatcher::CVBusWatcher(MControlAppEngineWatcherInterface& aControlAppEngine)
       
  1383 	: CActive(EPriorityStandard)
       
  1384 	, iParentControlAppEngine(aControlAppEngine)
       
  1385 	{
       
  1386 	CActiveScheduler::Add(this);
       
  1387 	}
       
  1388 
       
  1389 void CVBusWatcher::ConstructL()
       
  1390 	{
       
  1391 	TInt err = iVBusProp.Attach(KUidUsbManCategory, KUsbOtgVBusPoweredProperty);
       
  1392 	LOG("CVBusWatcher::ConstructL iVBusProp.Attach(KUidUsbManCategory, KUsbOtgVBusPoweredProperty) => %d",err);
       
  1393 	User::LeaveIfError(err);
       
  1394 
       
  1395 	SubscribeForNotification();
       
  1396 
       
  1397 	// Get the current value and update the Engine
       
  1398 	TInt val;
       
  1399 	err = iVBusProp.Get(val);
       
  1400 	LOG("CVBusWatcher::ConstructL iVBusProp.Get(val) => %d",err);
       
  1401 	User::LeaveIfError(err);
       
  1402 	iParentControlAppEngine.SetVBus(val);
       
  1403 	}
       
  1404 
       
  1405 void CVBusWatcher::SubscribeForNotification()
       
  1406 	{
       
  1407 	iVBusProp.Subscribe(iStatus);
       
  1408 	SetActive();
       
  1409 	}
       
  1410 
       
  1411 void CVBusWatcher::RunL()
       
  1412 	{
       
  1413 	SubscribeForNotification();
       
  1414 	// Get newly changed value
       
  1415 	TInt val;
       
  1416 	User::LeaveIfError(iVBusProp.Get(val));
       
  1417 	// Update value in Engine
       
  1418 	iParentControlAppEngine.SetVBus(val);
       
  1419 	}
       
  1420 
       
  1421 void CVBusWatcher::DoCancel()
       
  1422 	{
       
  1423 	iVBusProp.Cancel();
       
  1424 	}
       
  1425 
       
  1426 
       
  1427 
       
  1428 CConnectionIdleWatcher* CConnectionIdleWatcher::NewL(MControlAppEngineWatcherInterface& aControlAppEngine)
       
  1429 	{
       
  1430 	CConnectionIdleWatcher* self = new(ELeave) CConnectionIdleWatcher(aControlAppEngine);
       
  1431 	CleanupStack::PushL(self);
       
  1432 	self->ConstructL();
       
  1433 	CleanupStack::Pop(self);
       
  1434 	return self;
       
  1435 	}
       
  1436 
       
  1437 CConnectionIdleWatcher::~CConnectionIdleWatcher()
       
  1438 	{
       
  1439 	Cancel();
       
  1440 	iConnIdleProp.Close();
       
  1441 	}
       
  1442 
       
  1443 CConnectionIdleWatcher::CConnectionIdleWatcher(MControlAppEngineWatcherInterface& aControlAppEngine)
       
  1444 	: CActive(EPriorityStandard)
       
  1445 	, iParentControlAppEngine(aControlAppEngine)
       
  1446 	{
       
  1447 	CActiveScheduler::Add(this);
       
  1448 	}
       
  1449 
       
  1450 void CConnectionIdleWatcher::ConstructL()
       
  1451 	{
       
  1452 	TInt err = iConnIdleProp.Attach(KUidUsbManCategory, KUsbOtgConnectionIdleProperty);
       
  1453 	LOG("CConnectionIdleWatcher::ConstructL iIdPinProp.Attach(KUidUsbManCategory, KUsbOtgConnectionIdleProperty) => %d",err);
       
  1454 	User::LeaveIfError(err);
       
  1455 
       
  1456 	SubscribeForNotification();
       
  1457 
       
  1458 	// Get the current value and update the Engine
       
  1459 	TInt val;
       
  1460 	err = iConnIdleProp.Get(val);
       
  1461 	LOG("CConnectionIdleWatcher::ConstructL iConnIdleProp.Get(val) => %d",err);
       
  1462 	User::LeaveIfError(err);
       
  1463 	iParentControlAppEngine.SetConnectionIdle(val);
       
  1464 	}
       
  1465 
       
  1466 void CConnectionIdleWatcher::SubscribeForNotification()
       
  1467 	{
       
  1468 	iConnIdleProp.Subscribe(iStatus);
       
  1469 	SetActive();
       
  1470 	}
       
  1471 	
       
  1472 void CConnectionIdleWatcher::RunL()
       
  1473 	{
       
  1474 	SubscribeForNotification();
       
  1475 	// Get newly changed value
       
  1476 	TInt val;
       
  1477 	User::LeaveIfError(iConnIdleProp.Get(val));
       
  1478 	// Update value in Engine
       
  1479 	iParentControlAppEngine.SetConnectionIdle(val);
       
  1480 	}
       
  1481 
       
  1482 void CConnectionIdleWatcher::DoCancel()
       
  1483 	{
       
  1484 	iConnIdleProp.Cancel();
       
  1485 	}
       
  1486 
       
  1487 
       
  1488 	
       
  1489 CMessageWatcher* CMessageWatcher::NewL(MControlAppEngineWatcherInterface& aControlAppEngine)
       
  1490 	{
       
  1491 	CMessageWatcher* self = new(ELeave) CMessageWatcher(aControlAppEngine);
       
  1492 	CleanupStack::PushL(self);
       
  1493 	self->ConstructL();
       
  1494 	CleanupStack::Pop(self);
       
  1495 	return self;
       
  1496 	}
       
  1497 
       
  1498 CMessageWatcher::~CMessageWatcher()
       
  1499 	{
       
  1500 	Cancel();
       
  1501 	}
       
  1502 
       
  1503 CMessageWatcher::CMessageWatcher(MControlAppEngineWatcherInterface& aControlAppEngine)
       
  1504 	: CActive(EPriorityStandard)
       
  1505 	, iParentControlAppEngine(aControlAppEngine)
       
  1506 	{
       
  1507 	CActiveScheduler::Add(this);
       
  1508 	}
       
  1509 
       
  1510 void CMessageWatcher::ConstructL()
       
  1511 	{
       
  1512 	iParentControlAppEngine.Usb().MessageNotification(iStatus, iMessage);
       
  1513 	SetActive();
       
  1514 	}
       
  1515 
       
  1516 void CMessageWatcher::DoCancel()
       
  1517 	{
       
  1518 	iParentControlAppEngine.Usb().MessageNotificationCancel();
       
  1519 	}
       
  1520 
       
  1521 void CMessageWatcher::RunL()
       
  1522 	{
       
  1523 	TInt err = iStatus.Int();
       
  1524 	if (err)
       
  1525 		{
       
  1526 		LOG("CMessageWatcher::RunL()	iStatus.Int()=%d", err);
       
  1527 		}
       
  1528 	else
       
  1529 		{
       
  1530 		switch(iMessage)
       
  1531 			{
       
  1532 			case KErrUsbOtgVbusError:			       	
       
  1533 				iParentControlAppEngine.MessageReceived(EErrUsbOtgVbusError);
       
  1534 				break;	
       
  1535 			case KErrUsbOtgSrpTimeout:			       	
       
  1536 				iParentControlAppEngine.MessageReceived(EErrUsbOtgSrpTimeout);
       
  1537 				break;
       
  1538 			case KUsbMessageSrpReceived:				
       
  1539 				iParentControlAppEngine.MessageReceived(EUsbMessageSrpReceived);
       
  1540 				break;
       
  1541 			case KUsbMessageRequestSession:				
       
  1542 				iParentControlAppEngine.MessageReceived(EUsbMessageRequestSession);
       
  1543 				break;
       
  1544 			default: // The State Machine is not interested in any other messages
       
  1545 				break;
       
  1546 			}
       
  1547 
       
  1548 		LOG("CMessageWatcher::RunL()	msg = %d", iMessage);
       
  1549 		} // else
       
  1550 
       
  1551 	iParentControlAppEngine.Usb().MessageNotification(iStatus, iMessage);
       
  1552 	SetActive();
       
  1553 	}
       
  1554 
       
  1555