|
1 // Copyright (c) 2005-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 // Name : CSatTimer.cpp |
|
15 // Part of : Common Sim Atk TSY / commonsimatktsy |
|
16 // Implementation of the sat timer functions. |
|
17 // Version : 1.0 |
|
18 // |
|
19 |
|
20 |
|
21 |
|
22 // INCLUDE FILES |
|
23 #include "CSatTimer.h" // Class header |
|
24 #include "TfLogger.h" // For TFLOGSTRING |
|
25 #include "CSatNotificationsTsy.h" // Sat Tsy class |
|
26 #include "CSatDataPackage.h" // For data packages |
|
27 |
|
28 |
|
29 // ----------------------------------------------------------------------------- |
|
30 // CSatNotificationsTsy::NewL |
|
31 // Two-phased constructor. |
|
32 // ----------------------------------------------------------------------------- |
|
33 // |
|
34 CSatTimer* CSatTimer::NewL(CSatNotificationsTsy* aSatNotificationsTsy) |
|
35 { |
|
36 CSatTimer* const self = new ( ELeave ) CSatTimer(); |
|
37 CleanupStack::PushL( self ); |
|
38 self->ConstructL(aSatNotificationsTsy); |
|
39 CleanupStack::Pop(); |
|
40 return self; |
|
41 } |
|
42 |
|
43 // ----------------------------------------------------------------------------- |
|
44 // CSatTimer::ConstructL |
|
45 // Creates CSatTimer object. |
|
46 // ----------------------------------------------------------------------------- |
|
47 // |
|
48 void CSatTimer::ConstructL |
|
49 ( |
|
50 CSatNotificationsTsy* aSatNotificationsTsy |
|
51 ) |
|
52 { |
|
53 |
|
54 TFLOGSTRING( "CSAT: CSatTimer::ConstructL" ); |
|
55 iSatNotificationsTsy = aSatNotificationsTsy; |
|
56 iTimerTable = new ( ELeave ) RArray<TTimer>( KMaxNumberOfParallelTimers ); |
|
57 // Neutral priority, 0 |
|
58 iTimer = CHeartbeat::NewL( CActive::EPriorityStandard ); |
|
59 |
|
60 } |
|
61 |
|
62 // ----------------------------------------------------------------------------- |
|
63 // CSatTimer::CSatTimer |
|
64 // C++ constructor |
|
65 // ----------------------------------------------------------------------------- |
|
66 // |
|
67 CSatTimer::CSatTimer() |
|
68 { |
|
69 // None |
|
70 } |
|
71 |
|
72 |
|
73 // ----------------------------------------------------------------------------- |
|
74 // CSatTimer::~CSatTimer |
|
75 // Destructor |
|
76 // ----------------------------------------------------------------------------- |
|
77 // |
|
78 CSatTimer::~CSatTimer |
|
79 ( |
|
80 //None |
|
81 ) |
|
82 { |
|
83 TFLOGSTRING( "CSAT: CSatTimer::~CSatTimer" ); |
|
84 if ( iTimer ) |
|
85 { |
|
86 // Stop calling Beat... |
|
87 iTimer->Cancel(); |
|
88 delete iTimer; |
|
89 } |
|
90 |
|
91 if ( iTimerTable ) |
|
92 { |
|
93 iTimerTable->Close(); |
|
94 delete iTimerTable; |
|
95 } |
|
96 } |
|
97 |
|
98 // ----------------------------------------------------------------------------- |
|
99 // CSatTimer::Start |
|
100 // Starts timer. |
|
101 // ----------------------------------------------------------------------------- |
|
102 // |
|
103 TInt CSatTimer::Start |
|
104 ( |
|
105 TInt aTimerId, // Timer Id |
|
106 TUint32 aTimerValue // Timer value |
|
107 ) |
|
108 { |
|
109 TFLOGSTRING( "CSAT: CSatTimer::Start" ); |
|
110 TInt ret( KErrNone ); |
|
111 |
|
112 // Check if the entry is already in the table |
|
113 if ( CheckTimerTable( aTimerId ) ) |
|
114 { |
|
115 // Delete the old entry |
|
116 DeleteTimerById( aTimerId ); |
|
117 } |
|
118 |
|
119 // Calculate the trigger time (actual beat amount + timeout) |
|
120 TUint32 timeStamp = iBeatCounter + aTimerValue; |
|
121 // Create timer |
|
122 TTimer timer( aTimerId, iBeatCounter, timeStamp ); |
|
123 |
|
124 // Insert the entry in the table |
|
125 ret = iTimerTable->InsertInOrderAllowRepeats( timer, |
|
126 TTimer::OrderEntries ); |
|
127 |
|
128 // Check if active |
|
129 if ( ( KErrNone == ret ) && ( !iTimer->IsActive() ) ) |
|
130 { |
|
131 // Timer tick is on the second |
|
132 iTimer->Start( ETwelveOClock, this ); |
|
133 } |
|
134 |
|
135 return ret; |
|
136 |
|
137 } |
|
138 |
|
139 // ----------------------------------------------------------------------------- |
|
140 // CSatTimer::Stop |
|
141 // Stops timer |
|
142 // ----------------------------------------------------------------------------- |
|
143 // |
|
144 void CSatTimer::Stop |
|
145 ( |
|
146 //None |
|
147 ) |
|
148 { |
|
149 TFLOGSTRING( "CSAT: CSatTimer::Stop" ); |
|
150 // Check if active |
|
151 if ( iTimer->IsActive () ) |
|
152 { |
|
153 // Cancel beat method calling |
|
154 iTimer->Cancel(); |
|
155 } |
|
156 } |
|
157 |
|
158 // ----------------------------------------------------------------------------- |
|
159 // CSatTimer::Beat |
|
160 // Beat is called once every second. |
|
161 // ----------------------------------------------------------------------------- |
|
162 // |
|
163 void CSatTimer::Beat |
|
164 ( |
|
165 //None |
|
166 ) |
|
167 { |
|
168 TInt ret( KErrNone ); |
|
169 iBeatCounter++; |
|
170 TBool timeStampGreater( ETrue ); |
|
171 for( TInt numberOfTimers = iTimerTable->Count(); |
|
172 timeStampGreater && 0 < numberOfTimers; numberOfTimers-- ) |
|
173 { |
|
174 // Get the pointer to the TimerTable |
|
175 TTimer& timerTable = ( *iTimerTable )[ numberOfTimers - 1 ]; |
|
176 |
|
177 if ( timerTable.TimeStamp() <= iBeatCounter ) |
|
178 { |
|
179 // Proactive command ongoing |
|
180 if( !( iIsProactiveCommandOnGoing ) ) |
|
181 { |
|
182 // Call completion of the request due expired timer |
|
183 TRAPD( trapError, ret = iSatNotificationsTsy->TimerExpirationL( |
|
184 timerTable.TimerId(), |
|
185 iBeatCounter - timerTable.TimerStartTime() ) |
|
186 ); |
|
187 |
|
188 if ( trapError ) |
|
189 { |
|
190 ret = trapError; |
|
191 TFLOGSTRING2("CSAT: CSatTimer::Beat, Trap error: %d", |
|
192 trapError); |
|
193 } |
|
194 |
|
195 // Remove timer from the table |
|
196 if ( KErrNone == ret ) |
|
197 { |
|
198 DeleteTimerById( timerTable.TimerId() ); |
|
199 } |
|
200 } |
|
201 } |
|
202 else // TimerTable->iTimeStamp > iBeatCounter |
|
203 { |
|
204 // Table is ordered -> Break |
|
205 timeStampGreater = EFalse; |
|
206 } |
|
207 } |
|
208 } |
|
209 |
|
210 // ----------------------------------------------------------------------------- |
|
211 // CSatTimer::Synchronize |
|
212 // Called when synchronization is lost. The iBeatCounter is increased. |
|
213 // ----------------------------------------------------------------------------- |
|
214 // |
|
215 void CSatTimer::Synchronize |
|
216 ( |
|
217 // None |
|
218 ) |
|
219 { |
|
220 TFLOGSTRING( "CSAT: CSatTimer::Synchronize" ); |
|
221 iBeatCounter++; |
|
222 } |
|
223 |
|
224 // ----------------------------------------------------------------------------- |
|
225 // CSatTimer::CheckTimerTable |
|
226 // Check if an entry is in iEntryTable |
|
227 // ----------------------------------------------------------------------------- |
|
228 // |
|
229 TBool CSatTimer::CheckTimerTable |
|
230 ( |
|
231 TInt aTimerId |
|
232 ) |
|
233 { |
|
234 |
|
235 TFLOGSTRING( "CSAT: CSatTimer::CheckTimerTable" ); |
|
236 TBool ret( EFalse ); |
|
237 |
|
238 // Create the entry with meaningful values only |
|
239 TTimer timer( aTimerId, 0, 0 ); |
|
240 |
|
241 // Find the entry for this Handle in the list |
|
242 TInt position = iTimerTable->Find ( timer, TTimer::CompareEntries ); |
|
243 |
|
244 if ( KErrNotFound != position ) |
|
245 { |
|
246 ret = ETrue; |
|
247 } |
|
248 |
|
249 return ret; |
|
250 |
|
251 } |
|
252 |
|
253 // ----------------------------------------------------------------------------- |
|
254 // CSatTimer::DeleteTimerById |
|
255 // Deletes an entry from the entry table using the handle type |
|
256 // and the handle store as identificators. |
|
257 // ----------------------------------------------------------------------------- |
|
258 // |
|
259 TInt CSatTimer::DeleteTimerById |
|
260 ( |
|
261 TInt aTimerId // Timer identifier |
|
262 ) |
|
263 { |
|
264 |
|
265 TFLOGSTRING( "CSAT: CSatTimer::DeleteTimerById" ); |
|
266 TInt ret( KErrNotFound ); |
|
267 |
|
268 // Create the entry with meaningful values only |
|
269 TTimer timer( aTimerId, 0, 0 ); |
|
270 |
|
271 // Find the entry for this Handle in the list |
|
272 TInt position = iTimerTable->Find ( timer, |
|
273 TTimer::CompareEntries ); |
|
274 // Check if element is found |
|
275 if ( KErrNotFound != position ) |
|
276 { |
|
277 iTimerTable->Remove( position ); |
|
278 iTimerTable->Compress(); |
|
279 // Check if empty |
|
280 if ( 0 == iTimerTable->Count() ) |
|
281 { |
|
282 // Restart the beatCounter |
|
283 iBeatCounter = 0; |
|
284 // Stop timer till new entry is done |
|
285 Stop(); |
|
286 } |
|
287 ret = KErrNone; |
|
288 } |
|
289 |
|
290 return ret; |
|
291 } |
|
292 |
|
293 // ----------------------------------------------------------------------------- |
|
294 // CSatTimer::CurrentValueOfTimerById |
|
295 // Finds an entry from the entry table using the timer identification |
|
296 // ----------------------------------------------------------------------------- |
|
297 // |
|
298 TUint32 CSatTimer::CurrentValueOfTimerById |
|
299 ( |
|
300 TInt aTimerId // Timer identifier |
|
301 ) |
|
302 { |
|
303 TFLOGSTRING( "CSAT: CSatTimer::CurrentValueOfTimerById" ); |
|
304 TInt ret( KErrNotFound ); |
|
305 |
|
306 // Create the entry with meaningful values only |
|
307 TTimer timer( aTimerId, 0, 0 ); |
|
308 |
|
309 // Find the entry for this Handle in the list |
|
310 TInt position = iTimerTable->Find( timer, |
|
311 TTimer::CompareEntries ); |
|
312 |
|
313 // Check if element is found |
|
314 if ( KErrNotFound != position ) |
|
315 { |
|
316 // Get the pointer to the TimerTable |
|
317 TTimer& timerTable = ( *iTimerTable )[ position ]; |
|
318 |
|
319 // Get timeStamp |
|
320 TUint32 timerStamp = timerTable.TimeStamp(); |
|
321 |
|
322 // Current timer value = timer timeout value - current time |
|
323 ret = timerStamp - iBeatCounter; |
|
324 |
|
325 // If current timer value is smaller than zero set value to 0 |
|
326 // this can happen if there is some kind of delay before |
|
327 // timer is expired and iBeatCounter is still counting... |
|
328 if( 0 > ret ) |
|
329 { |
|
330 ret = 0; |
|
331 } |
|
332 } |
|
333 |
|
334 return ret; |
|
335 } |
|
336 |
|
337 // ----------------------------------------------------------------------------- |
|
338 // CSatTimer::SetProactiveCommandOnGoingStatus |
|
339 // Sets ProactiveCommandOngoing status |
|
340 // ----------------------------------------------------------------------------- |
|
341 // |
|
342 void CSatTimer::SetProactiveCommandOnGoingStatus |
|
343 ( |
|
344 TBool aStatus |
|
345 ) |
|
346 { |
|
347 TFLOGSTRING( "CSAT: CSatTimer::SetProactiveCommandOnGoingStatus" ); |
|
348 iIsProactiveCommandOnGoing = aStatus; |
|
349 } |
|
350 |
|
351 // ----------------------------------------------------------------------------- |
|
352 // CSatTimer::TTimer |
|
353 // Default constructor Sets timer information to the TimerTable |
|
354 // ----------------------------------------------------------------------------- |
|
355 // |
|
356 CSatTimer::TTimer::TTimer |
|
357 ( |
|
358 TInt aTimerId, // Timer id |
|
359 TUint32 aStartTime, // Timer start time |
|
360 TUint32 aTimeStamp // Time stamp |
|
361 ): iTimerId( aTimerId ), iStartTime( aStartTime ), |
|
362 iTimeStamp( aTimeStamp ) |
|
363 { |
|
364 TFLOGSTRING( "CSAT: TTimer::TTimer" ); |
|
365 } |
|
366 |
|
367 // ----------------------------------------------------------------------------- |
|
368 // CSatTimer::TimeStamp |
|
369 // Gets the value of iTimeStamp |
|
370 // ----------------------------------------------------------------------------- |
|
371 // |
|
372 TUint32 CSatTimer::TTimer::TimeStamp |
|
373 ( |
|
374 // None |
|
375 ) |
|
376 { |
|
377 TFLOGSTRING( "CSAT: TTimer::TimeStamp" ); |
|
378 return iTimeStamp; |
|
379 } |
|
380 |
|
381 // ----------------------------------------------------------------------------- |
|
382 // CSatTimer::TimerId |
|
383 // Gets the value of iTimerId |
|
384 // ----------------------------------------------------------------------------- |
|
385 // |
|
386 TInt CSatTimer::TTimer::TimerId |
|
387 ( |
|
388 // None |
|
389 ) |
|
390 { |
|
391 TFLOGSTRING( "CSAT: TTimer::TimerId" ); |
|
392 return iTimerId; |
|
393 } |
|
394 |
|
395 // ----------------------------------------------------------------------------- |
|
396 // CSatTimer::TimerStartTime |
|
397 // Gets the value of iStartTime |
|
398 // ----------------------------------------------------------------------------- |
|
399 // |
|
400 TUint32 CSatTimer::TTimer::TimerStartTime |
|
401 ( |
|
402 // None |
|
403 ) |
|
404 { |
|
405 TFLOGSTRING( "CSAT: TTimer::TimerStartTime" ); |
|
406 return iStartTime; |
|
407 } |
|
408 |
|
409 // ----------------------------------------------------------------------------- |
|
410 // CSatTimer::CompareEntries |
|
411 // Compares two entries are return if the are same of not. |
|
412 // ----------------------------------------------------------------------------- |
|
413 // |
|
414 TBool CSatTimer::TTimer::CompareEntries |
|
415 ( |
|
416 const CSatTimer::TTimer& aArg1, |
|
417 const CSatTimer::TTimer& aArg2 |
|
418 ) |
|
419 { |
|
420 TFLOGSTRING( "CSAT: CSatTimer::TTimer::CompareEntries" ); |
|
421 TBool ret( EFalse ); |
|
422 |
|
423 // We are interested only in the timer id |
|
424 if ( aArg1.iTimerId == aArg2.iTimerId ) |
|
425 { |
|
426 ret = ETrue; |
|
427 } |
|
428 else |
|
429 { |
|
430 TFLOGSTRING( "CSAT: CSatTimer::TTimer::CompareEntries, Not equal" ); |
|
431 } |
|
432 return ret; |
|
433 } |
|
434 |
|
435 // ----------------------------------------------------------------------------- |
|
436 // CSatTimer::OrderEntries |
|
437 // Compares two entries and returns which is the order between them. |
|
438 // ----------------------------------------------------------------------------- |
|
439 // |
|
440 TInt CSatTimer::TTimer::OrderEntries |
|
441 ( |
|
442 const CSatTimer::TTimer& aArg1, |
|
443 const CSatTimer::TTimer& aArg2 |
|
444 ) |
|
445 { |
|
446 TFLOGSTRING( "CSAT: CSatTimer::TTimer::OrderEntries" ); |
|
447 TInt ret( KFirstTimeStampSmaller ); |
|
448 |
|
449 // We are interested only in the time stamp |
|
450 if ( aArg1.iTimeStamp == aArg2.iTimeStamp ) |
|
451 { |
|
452 ret = KTimeStampsEqual; |
|
453 } |
|
454 else if ( aArg1.iTimeStamp > aArg2.iTimeStamp ) |
|
455 { |
|
456 ret = KFirstTimeStampBigger; |
|
457 } |
|
458 else |
|
459 { |
|
460 TFLOGSTRING( "CSAT: CSatTimer::TTimer::OrderEntries, \ |
|
461 KFirstTimeStampSmaller" ); |
|
462 } |
|
463 |
|
464 return ret; |
|
465 } |
|
466 |
|
467 // End of File |
|
468 |