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