Update contrib.
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // testcaseb2broot.cpp
20 #include <e32std_private.h>
21 #include <u32std.h> // unicode builds
23 #include <e32base_private.h>
25 #include <e32Test.h> // RTest header
28 #include <e32def_private.h>
29 #include <d32otgdi.h> // OTGDI header
30 #include <d32usbc.h> // USBCC header
31 #include "testcaseroot.h"
32 #include "b2bwatchers.h"
36 void CNotifyWatcherBase::RunL()
39 iHandler.HandleEvent(iWatchType, GetEventValue()); // report the event upwards
46 CNotifyCollector::CNotifyCollector(TRequestStatus &aStatus) : iStatusStep(aStatus)
49 TTimeIntervalDays oneday(1);
50 iTimeStarted.HomeTime();
51 iTimeStarted += (oneday); // force all durations to produce a negative (invalid) value
55 /***************************************************************
58 CNotifyCollector::~CNotifyCollector()
62 ClearAllEvents(); // free event arrays
64 iNotifyObjects.Close();
65 iFailureEvents.Close();
66 iRequiredEvents.Close();
67 iReceivedEvents.Close();
71 /* The test-case calls this to clear the events stored.
72 * Both the expected and already recieved events get cleared, this method
73 * is typically called at the start of each test-step
75 void CNotifyCollector::ClearAllEvents(TBool aClearRecieved/*=ETrue*/, TBool aClearRequired /*=ETrue*/)
80 iRequiredEvents.Reset();
81 iFailureEvents.Reset();
85 iReceivedEvents.Reset();
90 /* Creates and starts all 4 observers
91 * Note: The Watchdog does not get started, because it needs an interval
93 void CNotifyCollector::CreateObserversL(COtgRoot &aOtgDriver)
97 ASSERT(aOtgDriver.LddLoaded());
99 for (watchType=EWatcherTimeouts; watchType < EWatcherInvalid; watchType++)
101 CNotifyWatcherBase *pWatcher=0;
102 switch ((TWatcherNotifyType )watchType)
104 case EWatcherTimeouts:
105 pWatcher = COtgWatchdogWatcher::NewL(*this, EWatcherTimeouts, aOtgDriver);
108 pWatcher = COtgStateWatcher::NewL(*this , EWatcherState, aOtgDriver);
111 pWatcher = COtgEventWatcher::NewL(*this, EWatcherEvent, aOtgDriver);
113 case EWatcherMessage:
114 pWatcher = COtgMessageWatcher::NewL(*this, EWatcherMessage, aOtgDriver);
116 case EWatcherPeripheralState:
117 pWatcher = CPeripheralStateWatcher::NewL(*this, EWatcherPeripheralState, aOtgDriver);
119 case EWatcherAConnectionIdle:
120 pWatcher = CAConnectionIdleWatcher::NewL(*this, EWatcherAConnectionIdle, aOtgDriver);
124 // the TRequest object is added to scheduler in it's own constructor
126 // add it to our list so we can kill them after the test.
127 iNotifyObjects.Append(pWatcher);
128 //LOG_VERBOSE3(_L("Added watcher type %d, TRequest= %08X.\n"), iType, (TInt)(&pWatcher->iStatus));
130 // start all watchers, except for the watchdog
131 if (watchType != EWatcherTimeouts)
133 pWatcher->StartWatching(-1);
136 test.Printf(_L("\n"));
140 /* NOTE: OTG must still be loaded or else we cannot cancel the outstanding event watches here!
142 void CNotifyCollector::DestroyObservers()
147 for (TInt idx=0; idx < iNotifyObjects.Count(); idx++)
149 LOG_VERBOSE2(_L(".. %d .."), idx);
150 delete iNotifyObjects[idx]; // they will call their own Cancel() methods
152 iNotifyObjects.Close();
156 void CNotifyCollector::AddRequiredNotification(TWatcherNotifyType aType, TInt aValue)
158 AddRequiredOrFailureNotification(aType, aValue, EFalse);
161 void CNotifyCollector:: AddFailureNotification(const TWatcherNotifyType aType, TInt aValue)
163 AddRequiredOrFailureNotification(aType, aValue, ETrue);
166 /* Checks that a watcher for the event exists, and then adds it to a list of events required for a PASS condition
167 * The timout event does not get added to this list.
168 * If the parameter is a time, the timer gets started
169 * If aEventMeansFailure is set True, then the reception of the event will cause the current test step to fail
171 void CNotifyCollector::AddRequiredOrFailureNotification(TWatcherNotifyType aType, TInt aValue, TBool aEventMeansFailure)
173 CNotifyWatcherBase *pWatcher=0;
175 TBuf<MAX_DSTRLEN> aDescription;
177 // print a usefull debug message
180 case EWatcherTimeouts:
183 COtgRoot::OtgStateString(static_cast<RUsbOtgDriver::TOtgState>(aValue), aDescription);
184 LOG_VERBOSE3(_L("AddRequiredNotification() State %d '%S' wanted\n"), aValue, &aDescription);
187 COtgRoot::OtgEventString(static_cast<RUsbOtgDriver::TOtgEvent>(aValue), aDescription);
188 LOG_VERBOSE3(_L("AddRequiredNotification() Event %d '%S' wanted\n"), aValue, &aDescription);
190 case EWatcherMessage:
191 COtgRoot::OtgMessageString(static_cast<RUsbOtgDriver::TOtgMessage>(aValue), aDescription);
192 LOG_VERBOSE3(_L("AddRequiredNotification() Message %d '%S' wanted\n"), aValue, &aDescription);
194 case EWatcherPeripheralState:
195 COtgRoot::PeripheralStateString(static_cast<TUint>(aValue), aDescription);
196 LOG_VERBOSE3(_L("AddRequiredNotification() Peripheral State %d '%S' wanted\n"), aValue, &aDescription);
198 case EWatcherAConnectionIdle:
199 COtgRoot::AConnectionIdleString(static_cast<RUsbOtgDriver::TOtgConnection>(aValue), aDescription);
200 LOG_VERBOSE3(_L("AddRequiredNotification() AConnectionIdle %d '%S' wanted\n"), aValue, &aDescription);
205 // Find the watcher if possible
206 while (index < iNotifyObjects.Count())
209 TEST_ASSERTION(iNotifyObjects[index]!=NULL, _L("iNotifyObjects element gone!"));
211 if (iNotifyObjects[index]->GetType() == aType)
213 pWatcher = iNotifyObjects[index];
219 TEST_ASSERTION(pWatcher!=NULL, _L("pWatcher=0!"));
220 if (aType == EWatcherTimeouts)
221 { // other watchers are already running, but we start the timer now
222 pWatcher->StartWatching(aValue);
225 { // timeouts are not added to the Q
226 TOtgObservedEvent evt(aType, aValue);
227 if(aEventMeansFailure)
229 iFailureEvents.Append(evt);
233 iRequiredEvents.Append(evt);
237 iStatusStep = KRequestPending;
238 iTimeStarted.HomeTime();
242 /* Return the number of milliseconds since the last call to AddRequiredNotification()
244 TInt CNotifyCollector::DurationElapsed()
250 TTimeIntervalMicroSeconds ivlMicro(TimeEnd.MicroSecondsFrom(iTimeStarted));
251 Millisec = (TInt)(ivlMicro.Int64())/1000; // USB times are in uSec, but in ms for the user layer
254 Millisec = -1; // TRUE for when the Notifiers are not yet being used.
259 /* Search for an event in the received Q
260 * @return :TRUE if the specified event has been received
262 TBool CNotifyCollector::EventReceivedAlready(const TOtgObservedEvent& aEvent)
264 for (TInt idx=0; idx < iReceivedEvents.Count(); idx++)
265 if (iReceivedEvents[idx] == aEvent)
270 /* Search for an event in the failure event queue
271 * @return :TRUE if the specified event does denote a failure
273 TBool CNotifyCollector::IsFailureEvent(TOtgObservedEvent &aEvent)
275 for (TInt idx=0; idx < iFailureEvents.Count(); idx++)
276 if (iFailureEvents[idx] == aEvent)
281 /* @return 0 if the watcher has not yet been created. (for instance early in the test)
283 CNotifyWatcherBase* CNotifyCollector::GetWatcher(TWatcherNotifyType aType)
285 CNotifyWatcherBase *pWatcher=0;
289 while (index < iNotifyObjects.Count())
292 TEST_ASSERTION(iNotifyObjects[index]!=NULL, _L("iNotifyObjects element gone!"));
294 if (iNotifyObjects[index]->GetType() == aType)
296 pWatcher = iNotifyObjects[index];
306 /* Process the event. The OTG watchers are responsible for renewing themselves
307 * but the Timer event does not renew
309 void CNotifyCollector::HandleEvent(TWatcherNotifyType aType, TInt aValue)
311 if (aType == EWatcherTimeouts)
313 test.Printf(_L("Step timed out..(%dms).\n\n"), GetWatcher(aType)->GetEventValue());
314 CompleteStep(KTestCaseWatchdogTO);
318 TOtgObservedEvent evt(aType, aValue);
320 iReceivedEvents.Append(evt); // store incomming evt
322 // Check to see whether the event denotes a failure for this event
323 if (IsFailureEvent(evt))
325 test.Printf(_L("This event denotes failure for this test\n"));
326 CompleteStep(KTestCaseFailureEventReceived);
330 if (iRequiredEvents.Count())
332 // itterate all required events, search for each one in the incomming events list
333 while (start< iRequiredEvents.Count())
335 //LOG_VERBOSE3(_L("Search for=[%d,%d] :"),
336 // iRequiredEvents[start].GetType(), iRequiredEvents[start].GetValue());
338 if (!EventReceivedAlready(iRequiredEvents[start]))
339 return; // missing still, continue
342 // found all the required events
343 LOG_VERBOSE1(_L("Found all.\n"));
344 CompleteStep(KErrNone);
348 test.Printf(_L("Warning : No required events!\n"));
352 // Complete the test step's TRequestStatus (checking it is currently KRequestPending
353 // to try and avoid multiple completions).
355 void CNotifyCollector::CompleteStep(TInt aCompletionCode)
357 if(iStatusStep.Int() != KRequestPending)
359 test.Printf(_L("Can't complete step - not KRequestPending!\n"));
363 TRequestStatus *StatusStepPtr = &iStatusStep;
364 User::RequestComplete(StatusStepPtr, aCompletionCode);
368 /****************************************************************************
369 * COtg Watchdog Watcher
371 COtgWatchdogWatcher *COtgWatchdogWatcher::NewL(MOtgNotificationHandler &wdHandler,
372 const TWatcherNotifyType aWatchType,
376 COtgWatchdogWatcher* self = new (ELeave) COtgWatchdogWatcher(wdHandler, aWatchType, aOtgRoot);
377 CleanupStack::PushL(self);
379 CleanupStack::Pop(self);
384 void COtgWatchdogWatcher::ConstructL()
388 iTimer.CreateLocal();
393 void COtgWatchdogWatcher::StepExpired(TInt aInterval)
396 iHandler.HandleEvent(EWatcherTimeouts, aInterval) ;
400 void COtgWatchdogWatcher::RunL()
403 StepExpired(iIntervalMs);
407 void COtgWatchdogWatcher::StartTimer(TInt aIntervalMs)
411 iIntervalMs = aIntervalMs; // save value for printing latter
412 if (IsActive()) //cancel the last timer we set, this is easier than cancelling it in each test-step
415 User::WaitForRequest(iStatus); // swallow it
416 iTimer.After(iStatus, aIntervalMs*1000);
420 iTimer.After(iStatus, aIntervalMs*1000);
423 LOG_VERBOSE2(_L("wd Timer %dms\n"), aIntervalMs)
427 /****************************************************************************
428 * OTG Event/State/Message Watchers
430 COtgMessageWatcher* COtgMessageWatcher::NewL(MOtgNotificationHandler &wdHandler,
431 const TWatcherNotifyType aWatchType,
435 COtgMessageWatcher* self = new (ELeave) COtgMessageWatcher(wdHandler, aWatchType, aOtgRoot);
436 CleanupStack::PushL(self);
438 CleanupStack::Pop(self);
443 void COtgMessageWatcher::DisplayEvent()
445 TBuf<MAX_DSTRLEN> aDescription;
446 iOtgRoot.OtgMessageString(iMessage, aDescription);
447 test.Printf(_L("Received Message %d '%S'\n"), iMessage, &aDescription);
451 COtgStateWatcher* COtgStateWatcher::NewL(MOtgNotificationHandler &wdHandler,
452 const TWatcherNotifyType aWatchType,
456 COtgStateWatcher* self = new (ELeave) COtgStateWatcher(wdHandler, aWatchType, aOtgRoot);
457 CleanupStack::PushL(self);
459 CleanupStack::Pop(self);
464 void COtgStateWatcher::DisplayEvent()
467 TBuf<MAX_DSTRLEN> aDescription;
468 iOtgRoot.OtgStateString(iState, aDescription);
469 test.Printf(_L("Received State %d '%S'\n"), iState, &aDescription);
473 COtgEventWatcher* COtgEventWatcher::NewL(MOtgNotificationHandler &wdHandler,
474 const TWatcherNotifyType aWatchType,
478 COtgEventWatcher* self = new (ELeave) COtgEventWatcher(wdHandler, aWatchType, aOtgRoot);
479 CleanupStack::PushL(self);
481 CleanupStack::Pop(self);
485 void COtgEventWatcher::DisplayEvent()
487 TBuf<MAX_DSTRLEN> aDescription;
488 iOtgRoot.OtgEventString(iEvent, aDescription);
489 test.Printf(_L("Received Event %d '%S'\n"), iEvent, &aDescription);
492 CPeripheralStateWatcher* CPeripheralStateWatcher::NewL(MOtgNotificationHandler &wdHandler,
493 const TWatcherNotifyType aWatchType,
497 CPeripheralStateWatcher* self = new (ELeave) CPeripheralStateWatcher(wdHandler, aWatchType, aOtgRoot);
498 CleanupStack::PushL(self);
500 CleanupStack::Pop(self);
504 void CPeripheralStateWatcher::DisplayEvent()
506 TBuf<MAX_DSTRLEN> aDescription;
507 iOtgRoot.PeripheralStateString(iPeripheralState, aDescription);
508 test.Printf(_L("Peripheral State %d '%S'\n"), iPeripheralState, &aDescription);
511 CAConnectionIdleWatcher* CAConnectionIdleWatcher::NewL(MOtgNotificationHandler &wdHandler,
512 const TWatcherNotifyType aWatchType,
516 CAConnectionIdleWatcher* self = new (ELeave) CAConnectionIdleWatcher(wdHandler, aWatchType, aOtgRoot);
517 CleanupStack::PushL(self);
519 CleanupStack::Pop(self);
523 void CAConnectionIdleWatcher::RunL()
525 // We need to override the RunL for this event type, as
526 // the semantics of the asynchronous function are somewhat
527 // different to the rest of the ones being serviced by
528 // CNotifyWatcherBase.
530 // In the case of QueueOtgConnectionNotification, the value
531 // passed in is updated *immediately* to reflect the current
532 // activity or otherwise of the connection (unlike the other
533 // async functions which update the value on completion).
534 // The completion in the case of QueueOtgConnectionNotification
535 // is used to indicate that the value has changed, and
536 // another request should be queued to pick up this new value.
538 // The practical upshot of this is that the IssueAgain needs
539 // to happen before the event is displayed and handled...
543 iHandler.HandleEvent(iWatchType, GetEventValue()); // report the event upwards
548 void CAConnectionIdleWatcher::DisplayEvent()
550 TBuf<MAX_DSTRLEN> aDescription;
551 iOtgRoot.AConnectionIdleString(iAConnectionIdle, aDescription);
552 test.Printf(_L("AConnectionIdle %d '%S'\n"), iAConnectionIdle, &aDescription);