kerneltest/e32test/usbho/t_otgdi/inc/b2bwatchers.h
changeset 0 a41df078684a
child 43 c1f20ce4abcf
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 #ifndef B2BWATCHERS_H
       
     2 #define B2BWATCHERS_H
       
     3 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 // All rights reserved.
       
     5 // This component and the accompanying materials are made available
       
     6 // under the terms of the License "Eclipse Public License v1.0"
       
     7 // which accompanies this distribution, and is available
       
     8 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 //
       
    10 // Initial Contributors:
       
    11 // Nokia Corporation - initial contribution.
       
    12 //
       
    13 // Contributors:
       
    14 //
       
    15 // Description:
       
    16 // @internalComponent
       
    17 // 
       
    18 //
       
    19 
       
    20 
       
    21 #include <d32otgdi.h>	// OTGDI thunk headder (under test)
       
    22 #include <d32usbc.h>
       
    23 #include "debugmacros.h"
       
    24 #include "testcasefactory.h"
       
    25 #include "testpolicy.h"
       
    26 #include "testcaseroot.h"
       
    27 
       
    28 
       
    29 // forward:
       
    30 class CNotifyWatcherBase;	
       
    31 
       
    32 // types:
       
    33 enum TWatcherNotifyType
       
    34 	{
       
    35 	EWatcherTimeouts	=1,	// step watchdog event
       
    36 	EWatcherState,			// OTG states
       
    37 	EWatcherEvent,			// ... events
       
    38 	EWatcherMessage,		// ... messages
       
    39 	EWatcherPeripheralState,// Peripheral device state (undefined, attached, powered etc.)
       
    40 	EWatcherAConnectionIdle,// (A-Device only) whether the connection is idle or busy
       
    41 	EWatcherInvalid
       
    42 	};
       
    43 
       
    44 /* Applicable values in the value member - these are identical to the RUsbOtgDriver enum values, 
       
    45 * but are passed as TInt to be generic
       
    46 *	enum TOtgEvent
       
    47 *		{
       
    48 *		EEventAPlugInserted		,
       
    49 *		EEventAPlugRemoved		,
       
    50 *		EEventVbusRaised		,
       
    51 *		EEventVbusDropped		,
       
    52 *		EEventSrpInitiated		,
       
    53 *		EEventSrpReceived		,
       
    54 *		EEventHnpEnabled		,
       
    55 *		EEventHnpDisabled		,
       
    56 *		EEventHnpSupported		,
       
    57 *		EEventHnpAltSupported	,
       
    58 *		EEventRoleChangedToHost	,
       
    59 *		EEventRoleChangedToDevice,
       
    60 *		EEventRoleChangedToIdle								
       
    61 *		};
       
    62 *
       
    63 *	enum TOtgState
       
    64 *		{
       
    65 *		EStateReset					,
       
    66 *		EStateAIdle					,
       
    67 *		EStateAHost					,
       
    68 *		EStateAPeripheral			,
       
    69 *		EStateAVbusError			,						
       
    70 *		EStateBIdle					,
       
    71 *		EStateBPeripheral			,
       
    72 *		EStateBHost					
       
    73 *		};
       
    74 *
       
    75 *	enum TOtgMessage
       
    76 *		{
       
    77 *		EEventQueueOverflow					,
       
    78 *		EStateQueueOverflow					,
       
    79 *		EMessageQueueOverflow				,
       
    80 *		EMessageBadState					,
       
    81 *		EMessageStackNotStarted				,
       
    82 *		EMessageVbusAlreadyRaised			,
       
    83 *		EMessageSrpForbidden				,
       
    84 *		EMessageBusControlProblem			,
       
    85 *		EMessageVbusError					,
       
    86 *		EMessageSrpTimeout					,
       
    87 *		EMessageSrpActive					,
       
    88 *		EMessageSrpNotPermitted				,
       
    89 *		EMessageHnpNotPermitted				,
       
    90 *		EMessageHnpNotEnabled				,
       
    91 *		EMessageHnpNotSuspended				,
       
    92 *		EMessageVbusPowerUpNotPermitted		,
       
    93 *		EMessageVbusPowerUpError			,
       
    94 *		EMessageVbusPowerDownNotPermitted	,
       
    95 *		EMessageVbusClearErrorNotPermitted	,
       
    96 *		};
       
    97   */	
       
    98 
       
    99 /*
       
   100 From d32usbc.h : Device states available from client stack
       
   101 
       
   102 enum TUsbcDeviceState
       
   103 	{
       
   104 	EUsbcDeviceStateUndefined,								// 0
       
   105 	EUsbcDeviceStateAttached,								// 1
       
   106 	EUsbcDeviceStatePowered,								// 2
       
   107 	EUsbcDeviceStateDefault,								// 3
       
   108 	EUsbcDeviceStateAddress,								// 4
       
   109 	EUsbcDeviceStateConfigured,								// 5
       
   110 	EUsbcDeviceStateSuspended,								// 6
       
   111  	EUsbcNoState = 0xff										// 255 (used as a place holder)
       
   112 	};
       
   113 
       
   114 */
       
   115 
       
   116 /* Used to save,encapsulate events so we can validate them at the end of a step
       
   117  */
       
   118 class TOtgObservedEvent
       
   119 	{
       
   120 public:
       
   121 	TOtgObservedEvent() {iValue = EWatcherInvalid;};
       
   122 	
       
   123 	TOtgObservedEvent(TWatcherNotifyType aType, TInt aValue) : iType(aType), iValue(aValue) {};
       
   124 	TBool operator == (const TOtgObservedEvent&other) {return(other.iType==iType && other.iValue==iValue);};
       
   125 	
       
   126 	// getters
       
   127 	TWatcherNotifyType 	GetType() {return(iType);};
       
   128 	TInt 				GetValue() {return(iValue);};
       
   129 private:	
       
   130 	TWatcherNotifyType iType; 
       
   131 	TInt iValue;
       
   132 	};
       
   133 
       
   134 
       
   135 /* OTG generic mixin. fires when otg event/state recieved - the Base class auto re-issues 
       
   136  * the request after handling the event by calling IssueAgain()
       
   137  */ 
       
   138 class MOtgNotificationHandler
       
   139 	{
       
   140 public:
       
   141 	virtual void HandleEvent(TWatcherNotifyType aType, TInt aNotificationValue)=0; 
       
   142 	};
       
   143 
       
   144 
       
   145 /* Sets up and then Collects notifications the user wanted
       
   146  * Become active when the last event we wanted fires
       
   147  */
       
   148 class CNotifyCollector : public MOtgNotificationHandler
       
   149 	{
       
   150 public:
       
   151 	CNotifyCollector(TRequestStatus &aStatus);
       
   152 	virtual ~CNotifyCollector();
       
   153 
       
   154 	// Create and start the observers
       
   155 	void CreateObserversL(COtgRoot &aOtgDriver);
       
   156 	// Stop and destroy the observers
       
   157 	void DestroyObservers();
       
   158 	
       
   159 	// add an object to the Q
       
   160 	void AddRequiredNotification(const TWatcherNotifyType aType, TInt aValue);
       
   161 
       
   162 	//	Add an event to the list of events that indicates an instant failure
       
   163 	//	(e.g. the B-Device becoming configured during a short-circuit role swap)
       
   164 	void AddFailureNotification(const TWatcherNotifyType aType, TInt aValue);
       
   165 
       
   166 	/* Ppecify a step timeout in MILLISECONDS */
       
   167 	void AddStepTimeout(TInt aTimeoutMs) {
       
   168 		AddRequiredNotification(EWatcherTimeouts, aTimeoutMs);};
       
   169 	
       
   170 	void ClearAllEvents(TBool aClearRecieved =ETrue, TBool aClearRequired =ETrue); 
       
   171 	
       
   172 	
       
   173 	TBool EventReceivedAlready(const TOtgObservedEvent &aEvent);
       
   174 	
       
   175 	// from MOtgNotificationHandler
       
   176 	// ... process the event, if all the needed events are satisfied, complete the object
       
   177 	void HandleEvent(TWatcherNotifyType aType, TInt aNotificationValue); 
       
   178 	
       
   179 	// getters
       
   180 	CNotifyWatcherBase* GetWatcher(TWatcherNotifyType aType);
       
   181 	
       
   182 	TInt DurationElapsed();
       
   183 
       
   184 private:
       
   185 	void AddRequiredOrFailureNotification(TWatcherNotifyType aType, TInt aValue, TBool aEventMeansFailure);
       
   186 	TBool IsFailureEvent(TOtgObservedEvent& aEvent);
       
   187 	
       
   188 	void CompleteStep(TInt aCompletionCode);
       
   189 
       
   190 private:
       
   191 	TRequestStatus	&iStatusStep;	// KTestCaseWatchdogTO = failure, the test-step waits on this. 
       
   192 
       
   193 	RArray<CNotifyWatcherBase*> iNotifyObjects;	// observers
       
   194 	
       
   195 	RArray<TOtgObservedEvent>   iRequiredEvents;
       
   196 	RArray<TOtgObservedEvent>	iReceivedEvents;
       
   197 	RArray<TOtgObservedEvent>	iFailureEvents;
       
   198 	
       
   199 	// duration timer
       
   200 	TTime iTimeStarted;
       
   201 	};
       
   202 
       
   203 
       
   204 /* -----------------------------------------------------------------------------------------------
       
   205  * OTG Notification handler Base (Generic). We call these notification watchers, because they abstract 
       
   206  * OTG Messages, OTG Events, OTG States or a Watchdog timeout
       
   207  */
       
   208 class CNotifyWatcherBase : public CActive
       
   209 	{
       
   210 public:
       
   211 	virtual ~CNotifyWatcherBase() {LOG_FUNC  };
       
   212 	
       
   213 	virtual void StartWatching(TInt aInterval) {LOG_FUNC  TInt n(aInterval); IssueAgain(); SetActive(); };
       
   214 	// getter
       
   215 	TWatcherNotifyType GetType() {return(iWatchType);};		
       
   216 	
       
   217 protected:
       
   218 	CNotifyWatcherBase (MOtgNotificationHandler &aHandler, const TWatcherNotifyType aWatchType, COtgRoot &aRoot) : 
       
   219 				iOtgRoot(aRoot), 
       
   220 				iWatchType(aWatchType),
       
   221 				iHandler(aHandler),	
       
   222 				CActive(EPriorityStandard) { CActiveScheduler::Add(this); };
       
   223 	
       
   224 	// from CActive
       
   225 		void RunL();
       
   226  
       
   227 	// override in child
       
   228 
       
   229 public:		
       
   230 	virtual TInt GetEventValue() =0;
       
   231 	virtual TInt IssueAgain()    =0; 	// override 
       
   232 	virtual void DisplayEvent()  =0;		
       
   233 	
       
   234 protected:
       
   235 	MOtgNotificationHandler & iHandler;
       
   236 	COtgRoot 				&iOtgRoot; 	// driver
       
   237 	TWatcherNotifyType 		iWatchType; 
       
   238 	};
       
   239 
       
   240 
       
   241 //-----------------------------------------------------------------------------------------
       
   242 // watchdog watcher,
       
   243 // this watcher converts a CTimer event into a Notification, NOTE: it does not re-Issue the timer
       
   244 class COtgWatchdogWatcher : public CNotifyWatcherBase
       
   245 		{
       
   246 	public:
       
   247 		static COtgWatchdogWatcher* NewL(MOtgNotificationHandler &wdHandler, 
       
   248 										const TWatcherNotifyType aWatchType, 
       
   249 										COtgRoot &aOtgRoot);
       
   250 		void ConstructL();
       
   251 		virtual ~COtgWatchdogWatcher() {LOG_FUNC ;Cancel();};
       
   252 		
       
   253 		void StartTimer(TInt aIntervalMs);
       
   254 		void StartWatching(TInt aIntervalMs) {StartTimer(aIntervalMs); };
       
   255 		
       
   256  
       
   257 		
       
   258 		TInt IssueAgain() { LOG_FUNC ASSERT(0); return(0);};
       
   259 		void DoCancel() {LOG_FUNC ;iTimer.Cancel();};
       
   260 		void DisplayEvent() {LOG_FUNC ASSERT(0); }; // should never fire this
       
   261 	TInt GetEventValue() {return(iIntervalMs);};
       
   262 	void StepExpired(TInt aInterval);		
       
   263 	
       
   264 protected:
       
   265 	COtgWatchdogWatcher(MOtgNotificationHandler &aHandler, const TWatcherNotifyType aWatchType, COtgRoot &aOtgRoot) :
       
   266 		CNotifyWatcherBase(aHandler, aWatchType, aOtgRoot)
       
   267 			{};
       
   268 	// from CNotifyWatcherBase
       
   269 	void RunL();
       
   270 		
       
   271 protected:
       
   272 	TInt 	iIntervalMs;
       
   273 	RTimer 	iTimer;
       
   274 	};
       
   275 
       
   276 
       
   277 /*--------------------------------------------------------------------------------------------
       
   278  * STATE Watcher
       
   279  * Like the other OTG watchers, this watcher will renew (resubscribe) to the relevant notification
       
   280  * again after it has fired.
       
   281  */
       
   282 class COtgStateWatcher : public CNotifyWatcherBase
       
   283 	{
       
   284 public:
       
   285 	static COtgStateWatcher* NewL(MOtgNotificationHandler &wdHandler, const TWatcherNotifyType aWatchType, COtgRoot &aOtgRoot);
       
   286 	void ConstructL() {};
       
   287 	
       
   288 	virtual ~COtgStateWatcher() {LOG_FUNC Cancel(); };
       
   289 	
       
   290 	TInt IssueAgain() { iOtgRoot.otgQueueOtgStateRequest(iState , iStatus); return(ETrue);};
       
   291 	void DisplayEvent(); 
       
   292 
       
   293 	TInt GetEventValue() {return(iState);};
       
   294 	
       
   295 	void DoCancel() {LOG_FUNC iOtgRoot.otgCancelOtgStateRequest();};
       
   296 
       
   297 protected:
       
   298 	COtgStateWatcher(MOtgNotificationHandler &aHandler, const TWatcherNotifyType aWatchType, COtgRoot &aOtgRoot) :
       
   299 		CNotifyWatcherBase(aHandler, aWatchType, aOtgRoot)
       
   300 			{};
       
   301 	
       
   302 	RUsbOtgDriver::TOtgState iState;
       
   303 	};
       
   304 
       
   305 
       
   306 //---------------------------------------
       
   307 // EVENT Watcher
       
   308 class COtgEventWatcher : public CNotifyWatcherBase
       
   309 	{
       
   310 public:
       
   311 	static COtgEventWatcher* NewL(MOtgNotificationHandler &wdHandler, const TWatcherNotifyType aWatchType, COtgRoot &aOtgRoot);
       
   312 	void ConstructL() {};
       
   313 	virtual ~COtgEventWatcher() {LOG_FUNC Cancel(); };		
       
   314 	
       
   315 	TInt IssueAgain() { iOtgRoot.otgQueueOtgEventRequest(iEvent , iStatus); return(ETrue);};
       
   316 	void DisplayEvent(); 
       
   317 	TInt GetEventValue() {return(iEvent);};
       
   318 	void DoCancel() { LOG_FUNC iOtgRoot.otgCancelOtgEventRequest();};
       
   319 
       
   320 protected:
       
   321 	COtgEventWatcher(MOtgNotificationHandler &aHandler, const TWatcherNotifyType aWatchType, COtgRoot &aOtgRoot) : 
       
   322 		CNotifyWatcherBase(aHandler, aWatchType, aOtgRoot)
       
   323 			{};
       
   324 	
       
   325 	RUsbOtgDriver::TOtgEvent iEvent;
       
   326 	};
       
   327 
       
   328 
       
   329 //---------------------------------------
       
   330 // MESSAGE Watcher
       
   331 class COtgMessageWatcher : public CNotifyWatcherBase
       
   332 	{
       
   333 public:
       
   334 	static COtgMessageWatcher* NewL(MOtgNotificationHandler &wdHandler, const TWatcherNotifyType aWatchType, COtgRoot &aOtgRoot);
       
   335 	void ConstructL() {};
       
   336 	virtual ~COtgMessageWatcher() {LOG_FUNC Cancel(); };	
       
   337 	
       
   338 	TInt IssueAgain() { iOtgRoot.otgQueueOtgMessageRequest(iMessage, iStatus); return(ETrue);};
       
   339 	void DisplayEvent();
       
   340 	TInt GetEventValue() {return(iMessage);};
       
   341 	void DoCancel() {LOG_FUNC iOtgRoot.otgCancelOtgMessageRequest();};
       
   342 
       
   343 protected:
       
   344 	COtgMessageWatcher(MOtgNotificationHandler &aHandler, const TWatcherNotifyType aWatchType, COtgRoot &aOtgRoot) : 
       
   345 		CNotifyWatcherBase(aHandler, aWatchType, aOtgRoot)
       
   346 			{};
       
   347 	
       
   348 	RUsbOtgDriver::TOtgMessage iMessage;
       
   349 	};
       
   350 	
       
   351 // Client state watcher
       
   352 class CPeripheralStateWatcher : public CNotifyWatcherBase
       
   353 	{
       
   354 public:
       
   355 	static CPeripheralStateWatcher* NewL(MOtgNotificationHandler &wdHandler, const TWatcherNotifyType aWatchType, COtgRoot &aOtgRoot);
       
   356 	void ConstructL() {};
       
   357 	virtual ~CPeripheralStateWatcher() {LOG_FUNC Cancel(); };	
       
   358 	
       
   359 	TInt IssueAgain() { iOtgRoot.otgQueuePeripheralStateRequest(iPeripheralState, iStatus); return(ETrue);};
       
   360 	void DisplayEvent();
       
   361 	TInt GetEventValue() {return(iPeripheralState);};
       
   362 	void DoCancel() {LOG_FUNC iOtgRoot.otgCancelPeripheralStateRequest();};
       
   363 
       
   364 protected:
       
   365 	CPeripheralStateWatcher(MOtgNotificationHandler &aHandler, const TWatcherNotifyType aWatchType, COtgRoot &aOtgRoot) : 
       
   366 		CNotifyWatcherBase(aHandler, aWatchType, aOtgRoot)
       
   367 			{};
       
   368 	
       
   369 	TUint iPeripheralState;	//	*usually* to be interpreted as TUsbcDeviceState, see RDevUsbcClient::AlternateDeviceStatusNotify
       
   370 	};
       
   371 
       
   372 // Client state watcher
       
   373 class CAConnectionIdleWatcher : public CNotifyWatcherBase
       
   374 	{
       
   375 public:
       
   376 	static CAConnectionIdleWatcher* NewL(MOtgNotificationHandler &wdHandler, const TWatcherNotifyType aWatchType, COtgRoot &aOtgRoot);
       
   377 	void ConstructL() {};
       
   378 	virtual ~CAConnectionIdleWatcher() {LOG_FUNC Cancel(); };	
       
   379 	
       
   380 	TInt IssueAgain() { iOtgRoot.otgQueueAConnectionIdleRequest(iAConnectionIdle, iStatus); return(ETrue);};
       
   381 	void DisplayEvent();
       
   382 	TInt GetEventValue() {return(iAConnectionIdle);};
       
   383 	void DoCancel() {LOG_FUNC iOtgRoot.otgCancelAConnectionIdleRequest();};
       
   384 
       
   385 protected:
       
   386 	CAConnectionIdleWatcher(MOtgNotificationHandler &aHandler, const TWatcherNotifyType aWatchType, COtgRoot &aOtgRoot) : 
       
   387 		CNotifyWatcherBase(aHandler, aWatchType, aOtgRoot)
       
   388 			{};
       
   389 	
       
   390 	void RunL();	//	From CActive
       
   391 
       
   392 	RUsbOtgDriver::TOtgConnection iAConnectionIdle;
       
   393 	};
       
   394 
       
   395 
       
   396 #endif // B2BWATCHERS_H