resourcemgmt/hwresourcesmgr/test/mocksy/src/cmocksyengine.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     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 "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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "cmocksyengine.h"
       
    17 #include "rotatingstrbuf.h"
       
    18 
       
    19 #define KMaxLogEntries 30
       
    20 #define KCallbackBaseTime (1*100*1000)	// 0.1S
       
    21 
       
    22 /**
       
    23 Constructor
       
    24 */
       
    25 EXPORT_C CMockSYEngine::CMockSYEngine()
       
    26 	:iWaitingEventQue(_FOFF(TMockSYEvent,iWaitingEventQueLink))
       
    27 	,iPendingEventQue(_FOFF(TMockSYEvent,iPendingEventQueLink))
       
    28 	{
       
    29 	}
       
    30 
       
    31 /**
       
    32 2nd phase construction 
       
    33 */
       
    34 EXPORT_C void CMockSYEngine::ConstructL()
       
    35 	{
       
    36 	iTimer = CPeriodic::NewL(EPriorityHigh);
       
    37 	iRotatingLog = CRotatingStrBuf::NewL(KMaxLogEntries);
       
    38 	}
       
    39 
       
    40 /**
       
    41 destructor
       
    42 */
       
    43 EXPORT_C CMockSYEngine::~CMockSYEngine()
       
    44 	{
       
    45 	Reset();
       
    46 	delete iTimer;
       
    47 	delete iRotatingLog;
       
    48 	iListeners.Close();
       
    49 	}
       
    50 
       
    51 /**
       
    52 Add a new listener to the listener list
       
    53 */
       
    54 void CMockSYEngine::AddListenerL(MMockSYEngineListener& aListener)
       
    55 	{
       
    56 	iListeners.AppendL(&aListener);
       
    57 	}
       
    58 
       
    59 void CMockSYEngine::RemoveListener(MMockSYEngineListener& aListener)
       
    60 	{
       
    61 	TInt idx=0;
       
    62 	while (idx < iListeners.Count())
       
    63 		{
       
    64 		if (iListeners[idx]==&aListener)
       
    65 			{
       
    66 			iListeners.Remove(idx);
       
    67 			break;
       
    68 			}
       
    69 		idx++;
       
    70 		}
       
    71 	}
       
    72 
       
    73 void CMockSYEngine::NotifyListeners(MMockSYEngineListener::TNotificationType aNotification)
       
    74 	{
       
    75 	for (TInt idx=0; idx<iListeners.Count(); idx++)
       
    76 		{
       
    77 		iListeners[idx]->Notify(aNotification);
       
    78 		}
       
    79 	}
       
    80 
       
    81 /**
       
    82 Called by the SY, handle received command by verifing that is the expected command
       
    83 and queuing the conresponding completions
       
    84 Return KErrCorrupt it it's not the expected command
       
    85 */
       
    86 EXPORT_C TInt CMockSYEngine::ExecuteCommandL(TInt aCmdId, MMockSyEngineData& aData)
       
    87 	{
       
    88 	return ExecuteCommandL(aCmdId, aData, ETrue);		
       
    89 	}
       
    90 	
       
    91 EXPORT_C TInt CMockSYEngine::ExecuteCommandL(TInt aCmdId, MMockSyEngineData& aData, TBool aCheckData)
       
    92 	{
       
    93 	if (iWaitingEventQue.IsEmpty())
       
    94 		{
       
    95 		if(CanIgnoreUnexpectedIpc(aCmdId))
       
    96 			{
       
    97 			LogExpectError(aCmdId, aData, 0, KNullDesC8, ETrue); // ETrue means this error has been ignored
       
    98 			return KErrNone;
       
    99 			}
       
   100 		LogExpectError(aCmdId,aData,0,KNullDesC8);
       
   101 		Failure();
       
   102 		return KErrCorrupt;
       
   103 		}
       
   104 	TMockSYEvent* event = iWaitingEventQue.First();
       
   105 	// Top of the queue is supposed to be an EMessage event !
       
   106 	ASSERT(event->iEventType == TMockSYEvent::EMessage);
       
   107 	
       
   108 	// validate expected call
       
   109 	if ( (aCmdId!=  event->iCmdId) ||  (aCheckData ? !(aData == event->iData) : EFalse) )
       
   110 		{
       
   111 		if(CanIgnoreUnexpectedIpc(aCmdId))
       
   112 			{
       
   113 			LogExpectError(aCmdId, aData, event->iCmdId, KNullDesC8, ETrue); // ETrue means this error has been ignored
       
   114 			return KErrNone;
       
   115 			}
       
   116 		// failure ! it's the wrong event
       
   117 		LogExpectError(aCmdId,aData,event->iCmdId,event->iData);
       
   118 		Failure();
       
   119 		return KErrCorrupt;
       
   120 		}
       
   121 	else
       
   122 		{		
       
   123 		TInt resultCode = event->iResultCode;
       
   124 		TBool leave = event->iLeave;
       
   125 		LogRequest(aCmdId,aData,resultCode);
       
   126 		// remove the event from the waiting queue
       
   127 		event->iWaitingEventQueLink.Deque();
       
   128 		event->iData.Close();
       
   129 		delete event;
       
   130 		
       
   131 		// and push all following ECompletion events to the pending queue
       
   132 		if (!iWaitingEventQue.IsEmpty())
       
   133 			event = iWaitingEventQue.First();
       
   134 		while (!iWaitingEventQue.IsEmpty() && (event->iEventType == TMockSYEvent::ECompletion))
       
   135 			{
       
   136 			event->iWaitingEventQueLink.Deque();
       
   137 			iPendingEventQue.Add(*event,event->iDelay);
       
   138 			if (!iWaitingEventQue.IsEmpty())
       
   139 				event = iWaitingEventQue.First();
       
   140 			}
       
   141 		// start the timer
       
   142 		if (!iPendingEventQue.IsEmpty() && !iTimer->IsActive())
       
   143 			{
       
   144 			iTimer->Start(KCallbackBaseTime,KCallbackBaseTime,TCallBack(CMockSYEngine::TimerCallbackL, this));
       
   145 			}
       
   146 		// notify when both queue are empty
       
   147 		if (iPendingEventQue.IsEmpty() && iWaitingEventQue.IsEmpty())
       
   148 			{
       
   149 			NotifyListeners(MMockSYEngineListener::EHandlingTerminated);
       
   150 			}
       
   151 		if (leave)
       
   152 			{
       
   153 			User::Leave(resultCode);
       
   154 			}
       
   155 		return resultCode;
       
   156 		}
       
   157 	}
       
   158 
       
   159 
       
   160 /**
       
   161 Queue an event in the waiting event queue
       
   162 */
       
   163 EXPORT_C void CMockSYEngine::DoQueueEventL(TMockSYEvent::TEventType aType, TInt aCmdId, HBufC8* aData, 
       
   164 	TInt aResultCode, TBool aLeave, TInt aDelay)
       
   165 	{
       
   166 	TMockSYEvent* event = new (ELeave)TMockSYEvent;
       
   167 	event->iEventType 	= aType;
       
   168 	event->iCmdId 		= aCmdId;
       
   169 	event->iResultCode 	= aResultCode;
       
   170 	event->iDelay 		= aDelay;
       
   171 	event->iLeave		= aLeave;
       
   172 	event->iData.Assign(aData);
       
   173 	if ((aType == TMockSYEvent::ECompletion) && iWaitingEventQue.IsEmpty())
       
   174 		{
       
   175 		// it's a ECompletion msg and the waiting queue is empty, put the event directly in pending queue
       
   176 		iPendingEventQue.Add(*event,event->iDelay);
       
   177 		if (!iTimer->IsActive())
       
   178 			{
       
   179 			iTimer->Start(KCallbackBaseTime,KCallbackBaseTime,TCallBack(CMockSYEngine::TimerCallbackL, this));
       
   180 			}
       
   181 		}
       
   182 	else
       
   183 		{
       
   184 		iWaitingEventQue.AddLast(*event);
       
   185 		}
       
   186 	}
       
   187 
       
   188 /**
       
   189 	Returns EFalse indicating no IPC error can be ignored.
       
   190  */
       
   191 EXPORT_C TBool CMockSYEngine::CanIgnoreUnexpectedIpc(TInt /*aCmdId*/)
       
   192 	{
       
   193 	return EFalse;
       
   194 	}
       
   195 
       
   196 /**
       
   197 Return the next log line from the rotating log buffer
       
   198 */
       
   199 HBufC* CMockSYEngine::GetNextLogLine()
       
   200 	{
       
   201 	return iRotatingLog->Get();
       
   202 	}
       
   203 
       
   204 /**
       
   205 Periodical timer callback: generate completions
       
   206 */
       
   207 void CMockSYEngine::DoTimerCallbackL()
       
   208 	{
       
   209 	iPendingEventQue.CountDown();
       
   210 	TMockSYEvent* event = iPendingEventQue.RemoveFirst();
       
   211 	while(event != NULL )
       
   212 		{
       
   213 		LogCompletion(event->iCmdId,event->iData, event->iResultCode);
       
   214 		this->DoCompleteEventL(*event);
       
   215 		event->iData.Close();
       
   216 		delete event;
       
   217 		event = iPendingEventQue.RemoveFirst();
       
   218 		}
       
   219 	// stop the timer if there is no more pending messages
       
   220 	if (iPendingEventQue.IsEmpty())
       
   221 		{
       
   222 		iTimer->Cancel();
       
   223 		// notify when both queue are empty
       
   224 		if (iWaitingEventQue.IsEmpty())
       
   225 			{
       
   226 			NotifyListeners(MMockSYEngineListener::EHandlingTerminated);
       
   227 			}
       
   228 		}
       
   229 	}
       
   230 
       
   231 
       
   232 /**
       
   233 This function is called when an receive message doesn't correspond to the expected one
       
   234 */
       
   235 EXPORT_C void CMockSYEngine::Failure()
       
   236 	{
       
   237 	iFailure = ETrue;
       
   238 	NotifyListeners(MMockSYEngineListener::EFailure);
       
   239 	Reset();
       
   240 	}
       
   241 
       
   242 /**
       
   243 Reset waiting and pending event queue
       
   244 */
       
   245 EXPORT_C void CMockSYEngine::Reset()
       
   246 	{
       
   247 	TMockSYEvent* event;
       
   248 	while (!iWaitingEventQue.IsEmpty())
       
   249 		{
       
   250 		event = iWaitingEventQue.First();
       
   251 		event->iWaitingEventQueLink.Deque();
       
   252 		event->iData.Close();
       
   253 		delete event;
       
   254 		}
       
   255 	while (!iPendingEventQue.IsEmpty())
       
   256 		{
       
   257 		iPendingEventQue.CountDown();
       
   258 		event = iPendingEventQue.RemoveFirst();
       
   259 		while(event != NULL )
       
   260 			{
       
   261 			event->iData.Close();
       
   262 			delete event;
       
   263 			event = iPendingEventQue.RemoveFirst();
       
   264 			}
       
   265 		}
       
   266 	}
       
   267 
       
   268 EXPORT_C void CMockSYEngine::LogRequest(TInt aCmdId, const MMockSyEngineData& /*aData*/,TInt aResultCode)
       
   269 	{
       
   270 	TBuf<KMaxLogLineSize> buffer;
       
   271 	buffer.Format(_L(">>> Cmd=%d Err=%d"),aCmdId, aResultCode);
       
   272 	Log(buffer);	
       
   273 	}
       
   274 
       
   275 EXPORT_C void CMockSYEngine::LogCompletion(TInt aCmdId, const TDesC8& /*aData*/,TInt aResultCode)
       
   276 	{
       
   277 	TBuf<KMaxLogLineSize> buffer;
       
   278 	buffer.Format(_L("<<< Cmd=%d Err=%d"),aCmdId, aResultCode);
       
   279 	Log(buffer);	
       
   280 	}
       
   281 
       
   282 EXPORT_C void CMockSYEngine::LogExpectError(TInt aCmdId, const MMockSyEngineData& /*aData*/,
       
   283 	TInt aExpectedCmd,const TDesC8& /*aExpectedData*/, TBool /*aIsErrorIgnored*/)
       
   284 	{
       
   285 	TBuf<KMaxLogLineSize> buffer;
       
   286 	buffer.Format(_L("ERROR: Expected Cmd=%d Received Cmd=%d"),aExpectedCmd, aCmdId);
       
   287 	Log(buffer);	
       
   288 	}
       
   289 
       
   290 EXPORT_C void CMockSYEngine::Log(const TDesC& aDesc)
       
   291 	{
       
   292 	iRotatingLog->Put(aDesc);	
       
   293 	}
       
   294 
       
   295 TInt CMockSYEngine::TimerCallbackL(TAny* aPtr)
       
   296 	{
       
   297 	static_cast<CMockSYEngine*>(aPtr)->DoTimerCallbackL();
       
   298 	return 0;
       
   299 	}
       
   300 EXPORT_C void CMockSYEngine::CheckAndUpdateTransId(TUint8 aTransId, TInt aCommandId)
       
   301     {
       
   302     TMockSYEvent* event = NULL;
       
   303     TDblQueIter<TMockSYEvent> iter(iWaitingEventQue);
       
   304     			
       
   305     while(iter)
       
   306         {
       
   307         event = (TMockSYEvent*)iter++;
       
   308         if((event->iCmdId == aCommandId) && (event->iData[0] == 0xFF) && (event->iEventType == TMockSYEvent::ECompletion))
       
   309             {
       
   310             event->iData[0] = aTransId;
       
   311             event->iData[1] = 1;
       
   312             break;
       
   313             }
       
   314         }
       
   315     }