usbmgmt/usbmgrtest/usbcontrolapp/exampleusbcontrolapp/exampleusbcontrolapp.h
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 #ifndef EXAMPLEUSBCONTROLAPP_H
       
    19 #define EXAMPLEUSBCONTROLAPP_H
       
    20 
       
    21 #include <e32cmn.h>
       
    22 #include <e32base.h>
       
    23 #include <usbman.h>
       
    24 #include <e32property.h>
       
    25 #include "e32msgqueue.h"
       
    26 
       
    27 #include "usbcontrolappshared.h"
       
    28 
       
    29 
       
    30 class MControlAppEngineWatcherInterface;
       
    31 static const TInt KInactivityTimerPeriod = 10000000; // 10 sec
       
    32 
       
    33 
       
    34 NONSHARABLE_CLASS(CIdPinWatcher) : public CActive
       
    35 	{
       
    36 public:
       
    37 	static CIdPinWatcher* NewL(MControlAppEngineWatcherInterface& aControlAppEngine);
       
    38 	~CIdPinWatcher();
       
    39 
       
    40 private:
       
    41 	CIdPinWatcher(MControlAppEngineWatcherInterface& aControlAppEngine);
       
    42 	void ConstructL();
       
    43 	void SubscribeForNotification();
       
    44 	void DoCancel();
       
    45 	void RunL();
       
    46 
       
    47 private:
       
    48 	MControlAppEngineWatcherInterface&	iParentControlAppEngine;
       
    49 	RProperty							iIdPinProp;
       
    50 	};
       
    51 
       
    52 
       
    53 NONSHARABLE_CLASS(CVBusWatcher) : public CActive
       
    54 	{
       
    55 public:
       
    56 	static CVBusWatcher* NewL(MControlAppEngineWatcherInterface& aControlAppEngine);
       
    57 	~CVBusWatcher();
       
    58 
       
    59 private:
       
    60 	CVBusWatcher(MControlAppEngineWatcherInterface& aControlAppEngine);
       
    61 	void ConstructL();
       
    62 	void SubscribeForNotification();
       
    63 	void DoCancel();
       
    64 	void RunL();
       
    65 
       
    66 private:
       
    67 	MControlAppEngineWatcherInterface&	iParentControlAppEngine;
       
    68 	RProperty							iVBusProp;
       
    69 	};
       
    70 	
       
    71 
       
    72 NONSHARABLE_CLASS(CConnectionIdleWatcher) : public CActive
       
    73 	{
       
    74 public:
       
    75 	static CConnectionIdleWatcher* NewL(MControlAppEngineWatcherInterface& aControlAppEngine);
       
    76 	~CConnectionIdleWatcher();
       
    77 
       
    78 private:
       
    79 	CConnectionIdleWatcher(MControlAppEngineWatcherInterface& aControlAppEngine);
       
    80 	void ConstructL();
       
    81 	void SubscribeForNotification();
       
    82 	void DoCancel();
       
    83 	void RunL();
       
    84 
       
    85 private:
       
    86 	MControlAppEngineWatcherInterface&	iParentControlAppEngine;
       
    87 	RProperty							iConnIdleProp;
       
    88 	};
       
    89 
       
    90 
       
    91 NONSHARABLE_CLASS(CMessageWatcher) : public CActive
       
    92 	{
       
    93 public:
       
    94 	static CMessageWatcher* NewL(MControlAppEngineWatcherInterface& aControlAppEngine);
       
    95 	~CMessageWatcher();
       
    96 
       
    97 private:
       
    98 	CMessageWatcher(MControlAppEngineWatcherInterface& aControlAppEngine);
       
    99 	void ConstructL();
       
   100 
       
   101 	void DoCancel();
       
   102 	void RunL();
       
   103 
       
   104 public:
       
   105 	enum TMessageWatcherNotifications
       
   106 		{
       
   107 		EUsbMessageRequestSession,
       
   108 		EUsbMessageSrpReceived,
       
   109 		EErrUsbOtgSrpTimeout,
       
   110 		EErrUsbOtgVbusError
       
   111 		};
       
   112 
       
   113 private:
       
   114 	MControlAppEngineWatcherInterface&	iParentControlAppEngine;
       
   115 	TInt								iMessage;
       
   116 	};
       
   117 
       
   118 
       
   119 NONSHARABLE_CLASS(MControlAppStateMachineInterface)
       
   120 	{
       
   121 public: // All the events that the state machine can receive from outside:
       
   122 	//		Initial
       
   123 	virtual void StartUp()					= 0;
       
   124 	//		B-Stopped, B-Started
       
   125 	virtual void IdPinPresent()				= 0;
       
   126 	//		B-Stopped, A-Started
       
   127 	virtual void VBusRise()					= 0;
       
   128 	//		B-Started, A-Started
       
   129 	virtual void VBusDrop()					= 0;
       
   130 	//		A-Started, A-Stopped
       
   131 	virtual void IdPinAbsent()				= 0;
       
   132 	//		A-Started
       
   133 	virtual void ConnectionIdle()			= 0;
       
   134 	//		A-Started
       
   135 	virtual void ConnectionActive()			= 0;
       
   136 	//		B-Stopped, A-Stopped, B-Started, A-Started
       
   137 	virtual void RequestSessionCalled()		= 0;
       
   138 	//		B-Started
       
   139 	virtual void SrpTimeout()				= 0;
       
   140 	//		A-Stopped, A-Started
       
   141 	virtual void SrpDetected()				= 0;
       
   142 	//		A-Started, A-Stopped
       
   143 	virtual void VBusError()				= 0;
       
   144 	};
       
   145 
       
   146 class MControlAppEngineInterface;
       
   147 
       
   148 NONSHARABLE_CLASS(CControlAppStateMachine) : public CBase, public MControlAppStateMachineInterface
       
   149 	{
       
   150 // States are friend classes so that they can call private methods of the state machine.
       
   151 // We don't want those private methods visible outside the state machine.
       
   152 friend class CControlAppStateInitial;
       
   153 friend class CControlAppStateBServicesStopped;
       
   154 friend class CControlAppStateBServicesStarted;
       
   155 friend class CControlAppStateAServicesStarted;
       
   156 friend class CControlAppStateAServicesStopped;
       
   157 friend class CInactivityTimer;
       
   158 
       
   159 public:
       
   160 	static CControlAppStateMachine* NewL(MControlAppEngineInterface& aControlAppEngine);
       
   161 	~CControlAppStateMachine();
       
   162 private:
       
   163 	CControlAppStateMachine(MControlAppEngineInterface& aControlAppEngine);
       
   164 	void ConstructL();
       
   165 
       
   166 public: // From MControlAppStateMachineInterface, all the events that the state machine can receive from outside:
       
   167 	void StartUp();
       
   168 	void IdPinPresent();
       
   169 	void VBusRise();
       
   170 	void VBusDrop();
       
   171 	void IdPinAbsent();
       
   172 	void ConnectionIdle();
       
   173 	void ConnectionActive();
       
   174 	void RequestSessionCalled();
       
   175 	void SrpTimeout();
       
   176 	void SrpDetected();
       
   177 	void VBusError();
       
   178 
       
   179 private:
       
   180 	enum TControlAppState
       
   181 		{
       
   182 		EStateInitial,
       
   183 		EStateBServicesStopped,
       
   184 		EStateBServicesStarted,
       
   185 		EStateAServicesStopped,
       
   186 		EStateAServicesStarted
       
   187 		};
       
   188 	void SetState(TControlAppState aState);
       
   189 
       
   190 	void ResetInactivityTimer();
       
   191 	void CancelInactivityTimer();
       
   192 	void InactivityTimerExpired();
       
   193 
       
   194 
       
   195 	NONSHARABLE_CLASS(CInactivityTimer) : public CActive
       
   196 		{
       
   197 	public:
       
   198 		static CInactivityTimer* NewL(CControlAppStateMachine& aParentStateMachine);
       
   199 		~CInactivityTimer();
       
   200 		void Reset(); // Sets and resets timer. 
       
   201 		// Timer cancelled by calling CActive::Cancel()
       
   202 	private:
       
   203 		CInactivityTimer(CControlAppStateMachine& aParentStateMachine);
       
   204 		void ConstructL();		
       
   205 		void DoCancel();
       
   206 		void RunL();
       
   207 	private:
       
   208 		CControlAppStateMachine&	iParentStateMachine;
       
   209 		RTimer						iTimer;
       
   210 		};
       
   211 
       
   212 // Base State
       
   213 	NONSHARABLE_CLASS(CControlAppStateBase) : public CBase, public MControlAppStateMachineInterface
       
   214 		{
       
   215 	public:
       
   216 		CControlAppStateBase(CControlAppStateMachine& aParentStateMachine);
       
   217 		virtual void StartUp();
       
   218 		virtual void IdPinPresent();	
       
   219 		virtual void VBusRise();
       
   220 		virtual void VBusDrop();
       
   221 		virtual void IdPinAbsent();
       
   222 		virtual void ConnectionIdle();
       
   223 		virtual void ConnectionActive();
       
   224 		virtual void InactivityTimerExpired();	// Internally generated event
       
   225 		virtual void RequestSessionCalled();
       
   226 		virtual void SrpTriggered();			// Internally generated event
       
   227 		virtual void SrpTimeout();
       
   228 		virtual void SrpDetected();
       
   229 		virtual void VBusError();
       
   230 	protected:
       
   231 		CControlAppStateMachine& iParentStateMachine;
       
   232 		};
       
   233 
       
   234 // Initial State
       
   235 	NONSHARABLE_CLASS(CControlAppStateInitial) : public CControlAppStateBase
       
   236 		{
       
   237 	public:
       
   238 		CControlAppStateInitial(CControlAppStateMachine& aParentStateMachine);
       
   239 		// No action on these events which will come from Watchers when they are created.
       
   240 		// The Watchers will update the Engine with these values however and the 
       
   241 		// State Machine can act on those values on receiving the StartUp event.
       
   242 		void VBusRise();
       
   243 		void VBusDrop();
       
   244 		void IdPinPresent();
       
   245 		void IdPinAbsent();
       
   246 		void ConnectionIdle();
       
   247 		void ConnectionActive();
       
   248 		// Act on this events only:
       
   249 		void StartUp();
       
   250 		};
       
   251 
       
   252 // B-ServicesStopped State
       
   253 	NONSHARABLE_CLASS(CControlAppStateBServicesStopped) : public CControlAppStateBase
       
   254 		{
       
   255 	public:
       
   256 		CControlAppStateBServicesStopped(CControlAppStateMachine& aParentStateMachine);
       
   257 		void VBusRise();
       
   258 		void IdPinPresent();
       
   259 		void RequestSessionCalled();
       
   260 		void VBusDrop();
       
   261 		};
       
   262 
       
   263 // B-ServicesStarted State
       
   264 	NONSHARABLE_CLASS(CControlAppStateBServicesStarted) : public CControlAppStateBase
       
   265 		{
       
   266 	public:
       
   267 		CControlAppStateBServicesStarted(CControlAppStateMachine& aParentStateMachine);
       
   268 		void VBusDrop();	
       
   269 		void SrpTriggered(); // Internally generated event
       
   270 		void SrpTimeout();
       
   271 		void VBusRise(); // called when SRP has been successful
       
   272 		void IdPinPresent();
       
   273 		void RequestSessionCalled();
       
   274 		};
       
   275 
       
   276 // A-ServicesStarted State
       
   277 	NONSHARABLE_CLASS(CControlAppStateAServicesStarted) : public CControlAppStateBase
       
   278 		{
       
   279 	public:
       
   280 		CControlAppStateAServicesStarted(CControlAppStateMachine& aParentStateMachine);
       
   281 		void IdPinAbsent();
       
   282 		void SrpDetected();
       
   283 		void VBusRise();
       
   284 		void ConnectionIdle();
       
   285 		void ConnectionActive();
       
   286 		void InactivityTimerExpired(); // Internally generated event
       
   287 		void RequestSessionCalled();
       
   288 		void VBusError();
       
   289 		};
       
   290 
       
   291 // A-ServicesStopped State
       
   292 	NONSHARABLE_CLASS(CControlAppStateAServicesStopped) : public CControlAppStateBase
       
   293 		{
       
   294 	public:
       
   295 		CControlAppStateAServicesStopped(CControlAppStateMachine& aParentStateMachine);
       
   296 		void IdPinAbsent();
       
   297 		void SrpDetected();
       
   298 		void RequestSessionCalled();
       
   299 		void VBusError();
       
   300 		};
       
   301 
       
   302 private:
       
   303 	CControlAppStateBase*				iCurrentState;
       
   304 	CControlAppStateInitial*			iStateInitial;
       
   305 	CInactivityTimer*					iInactivityTimer;
       
   306 	CControlAppStateBServicesStopped*	iStateBServicesStopped;
       
   307 	CControlAppStateBServicesStarted*	iStateBServicesStarted;
       
   308 	CControlAppStateAServicesStarted*	iStateAServicesStarted;
       
   309 	CControlAppStateAServicesStopped*	iStateAServicesStopped;
       
   310 
       
   311 private:
       
   312 	MControlAppEngineInterface&			iParentControlAppEngine;
       
   313 	TBool								iTriggerSrp;
       
   314 	};
       
   315 
       
   316 
       
   317 NONSHARABLE_CLASS(MShutdownInterface)
       
   318 	{
       
   319 public:
       
   320 	virtual void Stop() const = 0;
       
   321 	};
       
   322 
       
   323 
       
   324 class CUsbControlAppEngine;
       
   325 
       
   326 NONSHARABLE_CLASS(CShutdownMonitor) : public CActive
       
   327 	{
       
   328 public:
       
   329 	static CShutdownMonitor* NewL(MShutdownInterface& aControlAppEngine);
       
   330 	~CShutdownMonitor();
       
   331 private:
       
   332 	CShutdownMonitor(MShutdownInterface& aControlAppEngine);
       
   333 	void ConstructL();
       
   334 	// From CActive
       
   335 	void DoCancel();
       
   336 	void RunL();
       
   337 private:
       
   338 	RProperty				iShutdownProp;
       
   339 	MShutdownInterface&		iParentControlAppEngine;
       
   340 	};
       
   341 
       
   342 
       
   343 NONSHARABLE_CLASS(MControlAppEngineInterface)
       
   344 	{
       
   345 public: // All the services the Engine provides that the State Machine calls on:
       
   346 	virtual TInt GetIdPin()									= 0;
       
   347 	virtual TInt GetVBus()									= 0;
       
   348 	virtual TInt StopUsbServices()							= 0;
       
   349 	virtual TInt StartUsbServices()							= 0;
       
   350 	virtual TInt EnableFunctionDriverLoading()				= 0;
       
   351 	virtual void DisableFunctionDriverLoading()				= 0;
       
   352 	virtual TInt BusRequest()								= 0;
       
   353 	virtual TInt BusDrop()									= 0;
       
   354 	virtual TInt BusRespondSrp()							= 0;
       
   355 	virtual TInt ClearVBusError()							= 0;
       
   356 	virtual void DisplayUserMessage(const TDesC& aUserMsg)	= 0;
       
   357 	};
       
   358 
       
   359 NONSHARABLE_CLASS(MControlAppEngineWatcherInterface)
       
   360 	{
       
   361 public: // All the methods the Engine provides that the Watcher classes can call:
       
   362 	virtual void SetIdPin(TInt aIdPin)				= 0;
       
   363 	virtual void SetVBus(TInt aVBus)				= 0;
       
   364 	virtual void SetConnectionIdle(TInt aConnIdle)	= 0;
       
   365 	virtual void MessageReceived(CMessageWatcher::TMessageWatcherNotifications aMessageNotification) = 0;
       
   366 	virtual RUsb& Usb()								= 0;
       
   367 	};
       
   368 
       
   369 NONSHARABLE_CLASS(CUsbControlAppEngine) : public CBase, public MControlAppEngineInterface, public MControlAppEngineWatcherInterface, public MShutdownInterface
       
   370 	{
       
   371 public:
       
   372 	static CUsbControlAppEngine* NewLC();
       
   373 	~CUsbControlAppEngine();
       
   374 	// Start/stop Active Scheduler
       
   375 	void Start();     
       
   376 	void Stop() const;
       
   377 	// From MControlAppEngineWatcherInterface, Watcher callbacks
       
   378 	void SetIdPin(TInt aIdPin);
       
   379 	void SetVBus(TInt aVBus);
       
   380 	void SetConnectionIdle(TInt aConnIdle);
       
   381 	void MessageReceived(CMessageWatcher::TMessageWatcherNotifications aMessageNotification);
       
   382 	RUsb& Usb();
       
   383 	// Getters
       
   384 	TInt GetIdPin();
       
   385 	TInt GetVBus();
       
   386 	// From MControlAppEngineInterface, called by State Machine
       
   387 	TInt StopUsbServices();
       
   388 	TInt StartUsbServices();
       
   389 	TInt EnableFunctionDriverLoading();
       
   390 	void DisableFunctionDriverLoading();
       
   391 	TInt BusRequest();
       
   392 	TInt BusDrop();
       
   393 	TInt BusRespondSrp();
       
   394 	TInt ClearVBusError();
       
   395 	void DisplayUserMessage(const TDesC& aUserMsg);
       
   396 
       
   397 private:
       
   398 	CUsbControlAppEngine();
       
   399 	void ConstructL();
       
   400 
       
   401 private: 
       
   402 	// Watcher related
       
   403 	CIdPinWatcher* 			iIdPinWatcher;
       
   404 	CVBusWatcher* 			iVBusWatcher;
       
   405 	CConnectionIdleWatcher* iConnIdleWatcher;
       
   406 	CMessageWatcher*		iMessageWatcher;
       
   407 	TInt					iIdPin;
       
   408 	TInt					iVBus;
       
   409 	TInt					iConnIdle;
       
   410 	// Resources
       
   411 	CControlAppStateMachine*	iStateMachine;
       
   412 	CShutdownMonitor*			iShutdownMonitor;
       
   413 	RUsb						iUsb;
       
   414 	//    RMsgQueue between exampleusbcontrolapp.exe & usbviewer.exe for displaying user messages
       
   415 	RMsgQueue<TBuf<KViewerNumCharactersOnLine> >	iViewerMsgQ; 
       
   416 	}; // CUsbControlAppEngine
       
   417 
       
   418 #endif // EXAMPLEUSBCONTROLAPP_H