comparison g23m/condat/ms/src/mfw/mfw_tim.c @ 0:509db1a7b7b8

initial import: leo2moko-r1
author Space Falcon <falcon@ivan.Harhan.ORG>
date Mon, 01 Jun 2015 03:24:05 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:509db1a7b7b8
1 /*
2 +--------------------------------------------------------------------+
3 | PROJECT: MMI-Framework (8417) $Workfile:: mfw_tim.c $|
4 | $Author:: Es $ CONDAT GmbH $Revision:: 12 $|
5 | CREATED: 21.09.98 $Modtime:: 2.03.00 11:49 $|
6 | STATE : code |
7 +--------------------------------------------------------------------+
8
9 MODULE : MFW_TIM
10
11 PURPOSE : timer handling functions
12
13 EXPORT :
14
15 TO DO :
16
17 $History:: mfw_tim.c $
18 *
19 * ***************** Version 12 *****************
20 * User: Es Date: 3.03.00 Time: 12:10
21 * Updated in $/GSM/Condat/MS/SRC/MFW
22 * timStart(), timSignal() etc.: safer handling of simultaneous
23 * timeouts; additional chain in timer control block.
24 *
25 * ***************** Version 11 *****************
26 * User: Nm Date: 18.02.00 Time: 13:57
27 * Updated in $/GSM/Condat/MS/SRC/MFW
28 *
29 * ***************** Version 10 *****************
30 * User: Nm Date: 18.02.00 Time: 12:34
31 * Updated in $/GSM/Condat/MS/SRC/MFW
32 * change the name timSetup to
33 * timSetTime
34 *
35 * ***************** Version 9 *****************
36 * User: Nm Date: 18.02.00 Time: 12:21
37 * Updated in $/GSM/Condat/MS/SRC/MFW
38 * add the function timSetup()
39 *
40 * ***************** Version 8 *****************
41 * User: Es Date: 14.06.99 Time: 12:14
42 * Updated in $/GSM/DEV/MS/SRC/MFW
43 *
44 * ***************** Version 7 *****************
45 * User: Es Date: 1.04.99 Time: 17:07
46 * Updated in $/GSM/DEV/MS/SRC/MFW
47 * removed lots of traces
48 *
49 * ***************** Version 6 *****************
50 * User: Es Date: 18.02.99 Time: 17:01
51 * Updated in $/GSM/DEV/MS/SRC/MFW
52 *
53 * ***************** Version 5 *****************
54 * User: Es Date: 17.02.99 Time: 19:11
55 * Updated in $/GSM/DEV/MS/SRC/MFW
56 *
57 * ***************** Version 4 *****************
58 * User: Es Date: 27.01.99 Time: 15:06
59 * Updated in $/GSM/DEV/MS/SRC/MFW
60 *
61 * ***************** Version 3 *****************
62 * User: Es Date: 14.01.99 Time: 17:19
63 * Updated in $/GSM/DEV/MS/SRC/MFW
64 *
65 * ***************** Version 2 *****************
66 * User: Es Date: 23.12.98 Time: 16:19
67 * Updated in $/GSM/DEV/MS/SRC/MFW
68 */
69
70
71 #define ENTITY_MFW
72
73 #if defined (NEW_FRAME)
74
75 #include "typedefs.h"
76 #include "vsi.h"
77 #include "custom.h"
78 #include "gsm.h"
79
80 #else
81
82 #include "STDDEFS.H"
83 #include "custom.h"
84 #include "gsm.h"
85 #include "vsi.h"
86
87 #endif
88
89 #include "mfw_mfw.h"
90 #include "mfw_sys.h"
91 #include "drv_tmr.h"
92 #include "mfw_tim.h"
93
94
95 static MfwTim *timRoot = 0; /* list of running clocks */
96 static int timTimeoutCount; /* overrun counter */
97 static int timTimeoutBusy; /* overrun marker */
98 static int timTimerPrecMs; /* minimum timer intervall */
99 static MfwTim *ActiveTOut = NULL; /* list of timeouts to be processed on this cycle*/
100 /* PATCH PMC 000721: use another pointer /
101 NDH 16/4/2003 : Make it static for use with timDelete also */
102 static MfwTim *timSavedNext = NULL;
103 /* END PATCH PMC 000721: use another pointer */
104
105 static void timInsert (MfwTim *t);
106 static void timRemove (MfwTim *t);
107 static void timAdjust (S32 t);
108 static int timFind (MfwTim *t);
109 static int timCommand (U32 cmd, void *h);
110
111 MfwHdr * current_mfw_elem;
112
113 /*
114 +--------------------------------------------------------------------+
115 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
116 | STATE : code ROUTINE : timInit |
117 +--------------------------------------------------------------------+
118
119 PURPOSE : initialize timer handler
120
121 */
122
123 MfwRes timInit (void)
124 {
125 void timTimeout (void);
126
127 timTimeoutCount = 0;
128 timTimeoutBusy = 0;
129 timRoot = 0;
130 mfwCommand[MfwTypTim] = (MfwCb) timCommand;
131 tmrInit(timTimeout);
132 tmrStart(1);
133 timTimerPrecMs = tmrStop();
134
135 return MfwResOk;
136 }
137
138
139 /*
140 +--------------------------------------------------------------------+
141 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
142 | STATE : code ROUTINE : timExit |
143 +--------------------------------------------------------------------+
144
145 PURPOSE : finalize timer handler
146
147 */
148
149 MfwRes timExit (void)
150 {
151 tmrExit();
152 mfwCommand[MfwTypTim] = 0;
153
154 return MfwResOk;
155 }
156
157
158 /*
159 +--------------------------------------------------------------------+
160 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
161 | STATE : code ROUTINE : timCreate |
162 +--------------------------------------------------------------------+
163
164 PURPOSE : create timer control
165
166 */
167
168 MfwHnd timCreate (MfwHnd w, S32 t, MfwCb f)
169 {
170 MfwHdr *hdr = (MfwHdr *) mfwAlloc(sizeof(MfwHdr));
171 MfwTim *tim = (MfwTim *) mfwAlloc(sizeof(MfwTim));
172 MfwHdr *insert_status =0;
173
174 if (!hdr || !tim)
175 {
176 TRACE_ERROR("ERROR: timCreate() Mem Alloc Failed.");
177
178 if(hdr)
179 mfwFree((U8*)hdr,sizeof(MfwHdr));
180
181 if(tim)
182 mfwFree((U8*)tim,sizeof(MfwTim));
183
184 return 0;
185 }
186
187 tim->time = t;
188 tim->left = 0;
189 tim->handler = f;
190 tim->next = 0;
191 // PATCH LE 06.06.00
192 // store mfw header address
193 tim->mfwHeader = hdr; /* SPR#1597 - SH - Change mfw_header to mfwHeader */
194 // END PATCH LE 06.06.00
195
196 hdr->data = tim;
197 hdr->type = MfwTypTim;
198
199 insert_status= mfwInsert(w,hdr);
200 if(!insert_status)
201 {
202 TRACE_ERROR("ERROR: timCreate() Failed to Install Handler. ");
203 mfwFree((U8*)hdr,sizeof(MfwHdr));
204 mfwFree((U8*)tim,sizeof(MfwTim));
205 return 0;
206 }
207 return insert_status;
208
209
210 }
211
212
213 /*
214 +--------------------------------------------------------------------+
215 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
216 | STATE : code ROUTINE : timDelete |
217 +--------------------------------------------------------------------+
218
219 PURPOSE : delete timer control block
220
221 */
222
223 MfwRes timDelete (MfwHnd h)
224 {
225 MfwRes res;
226
227 if (!h)
228 return MfwResIllHnd;
229
230 if (((MfwHdr *) h)->type != MfwTypTim)
231 return MfwResIllHnd;
232
233 res = (mfwRemove(h)) ? MfwResOk : MfwResIllHnd;
234
235 timStop(h);
236
237 mfwFree(((MfwHdr *) h)->data,sizeof(MfwTim));
238 mfwFree(h,sizeof(MfwHdr));
239
240 return res;
241 }
242
243
244 /*
245 +--------------------------------------------------------------------+
246 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
247 | STATE : code ROUTINE : timStart |
248 +--------------------------------------------------------------------+
249
250 PURPOSE : start timer
251
252 */
253
254 MfwRes timStart (MfwHnd h)
255 {
256 MfwTim *tc;
257 S32 left, diff;
258
259 if (!h)
260 return MfwResIllHnd;
261
262 if (((MfwHdr *) h)->type != MfwTypTim)
263 return MfwResIllHnd;
264
265 tc = ((MfwHdr *) h)->data;
266
267 if (tc->time <= 0)
268 return MfwResErr;
269
270 if (tc->time < timTimerPrecMs)
271 tc->time = timTimerPrecMs;
272
273 tc->left = tc->time;
274 left = tmrStop(); /* get systimer left time */
275 timRemove(tc); /* remove, if running */
276 if (left <= tc->left)
277 tc->left -= left; /* adjust for next timer */
278 else
279 {
280 diff = left - tc->left; /* correction value */
281 left = tc->left; /* new timeout */
282 tc->left = 0; /* this is the first */
283 timAdjust(diff); /* correct other timers */
284 }
285 timInsert(tc);
286 if (!left) /* no timer was running */
287 {
288 left = timRoot->left;
289 tc = timRoot;
290 while (tc)
291 {
292 tc->left -= left; /* adjust time left entry */
293 tc = tc->next;
294 }
295 }
296 tmrStart(left); /* restart timer */
297
298 return MfwResOk;
299 }
300
301
302 /*
303 +--------------------------------------------------------------------+
304 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
305 | STATE : code ROUTINE : timSetup |
306 +--------------------------------------------------------------------+
307
308 PURPOSE : Stop the current timer and reload it with the new timeout.
309
310
311 */
312
313 MfwRes timSetTime (MfwHnd h,S32 t)
314 {
315 MfwTim *tc;
316
317 if (!h)
318 return MfwResIllHnd;
319
320 if (((MfwHdr *) h)->type != MfwTypTim)
321 return MfwResIllHnd;
322
323 timStop (h); /* stop the current timer */
324
325 tc = ((MfwHdr *) h)->data;
326 tc->time = t; /* load with new timeout */
327 tc->left = 0;
328
329
330 return MfwResOk;
331 }
332
333
334 /*
335 +--------------------------------------------------------------------+
336 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
337 | STATE : code ROUTINE : timStop |
338 +--------------------------------------------------------------------+
339
340 PURPOSE : stop timer
341
342 */
343
344 MfwRes timStop (MfwHnd h)
345 {
346 MfwTim *tc;
347
348 if (!h)
349 return MfwResIllHnd;
350
351 if (((MfwHdr *) h)->type != MfwTypTim)
352 return MfwResIllHnd;
353
354 tc = ((MfwHdr *) h)->data;
355 tc->left = 0;
356
357 timRemove(tc);
358
359 return MfwResOk;
360 }
361
362
363 /*
364 +--------------------------------------------------------------------+
365 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
366 | STATE : code ROUTINE : timTime |
367 +--------------------------------------------------------------------+
368
369 PURPOSE : get timers remaining time
370
371 */
372
373 S32 timTime (MfwHnd h)
374 {
375 MfwTim *tc;
376
377 if (!h)
378 return 0;
379
380 if (((MfwHdr *) h)->type != MfwTypTim)
381 return 0;
382
383 tc = ((MfwHdr *) h)->data;
384
385 if (timFind(tc))
386 return (tmrLeft() + tc->left);
387
388 return 0;
389 }
390
391
392 /*
393 +--------------------------------------------------------------------+
394 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
395 | STATE : code ROUTINE : timSignal |
396 +--------------------------------------------------------------------+
397
398 PURPOSE : thread context time out
399
400 */
401
402 void timSignal (void)
403 {
404 S32 tout;
405 MfwTim *tc;
406 UBYTE temp;
407
408 if (!timRoot)
409 return;
410
411 ActiveTOut = timRoot;
412 ActiveTOut->next2 = 0;
413
414 while (timRoot && timRoot->left == 0)
415 {
416 timRoot->left = -1; /* flag timeout */
417 timRoot->next2 = timRoot->next; /* setup timeout chain */
418 timRoot = timRoot->next; /* remove element */
419
420 /* SPR#2029 - DS - Ensure second timer exists. Port of HLE fix. */
421 if (timRoot != 0)
422 timRoot->next2 = 0;
423 }
424
425 if (timRoot)
426 {
427 tout = timRoot->left;
428 tc = timRoot;
429 while (tc)
430 {
431 tc->left -= tout; /* adjust time left entry */
432 tc = tc->next;
433 }
434 tmrStart(tout); /* start next session */
435 }
436
437 while (ActiveTOut && ActiveTOut->left < 0) /* signal timout handlers */
438 {
439 ActiveTOut->left = 0;
440
441 /* PATCH PMC 000721: save the next pointer because the memory associated with
442 * to may be released in the timer handler function.
443 */
444 timSavedNext = ActiveTOut->next2;
445 /* END PATCH PMC 000721 */
446
447 if (ActiveTOut->handler)
448 {
449 // PATCH LE 06.06.00
450 // store current mfw elem
451 current_mfw_elem = ActiveTOut->mfwHeader; /* SPR#1597 - SH - Change mfw_header to mfwHeader */
452 // END PATCH LE 06.06.00
453
454 /* NM, p011b */
455 temp = dspl_Enable(0);
456 /* p011b end */
457
458 (*(ActiveTOut->handler))(ActiveTOut->time,ActiveTOut);
459
460 /* NM, p011c */
461 dspl_Enable(temp);
462 /* p011c end */
463
464 }
465 /* PATCH PMC 000721: use the SavedNext pointer to set ActiveTOut */
466 ActiveTOut = timSavedNext;
467 /* cq18182 pointer cleared here, this fix is only temporary as it seems to fix the current problem, however further investigation
468 is required as to why the timSavedNext pointer was not being cleared. 10-03-04 MZ. */
469 timSavedNext = NULL;
470 /* END PATCH PMC 000721 */
471 }
472
473 /* cq18182 add check and clear the pointer 10-03-04 MZ.*/
474 if(ActiveTOut != NULL)
475 ActiveTOut = NULL;
476
477 }
478
479
480 /*
481 +--------------------------------------------------------------------+
482 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
483 | STATE : code ROUTINE : timTimeout |
484 +--------------------------------------------------------------------+
485
486 PURPOSE : thread context time out
487
488 */
489
490 void timTimeout (void)
491 {
492 timTimeoutCount++;
493
494 if (timTimeoutBusy)
495 return;
496
497 timTimeoutBusy = 1;
498 while (timTimeoutCount)
499 {
500 timTimeoutCount--;
501 timSignal();
502 }
503 timTimeoutBusy = 0;
504 }
505
506
507 /*
508 +--------------------------------------------------------------------+
509 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
510 | STATE : code ROUTINE : timInsert |
511 +--------------------------------------------------------------------+
512
513 PURPOSE : insert timer into ordered list
514
515 */
516
517 static void timInsert (MfwTim *t)
518 {
519 MfwTim *prev, *curr;
520
521 if (!timRoot)
522 {
523 timRoot = t;
524 t->next = 0;
525 t->next2 = 0; /* cq18182 initialise the pointer 10-03-04 MZ. */
526 return;
527 }
528 if (t->left < timRoot->left)
529 {
530 t->next = timRoot;
531 t->next2 = 0; /* cq18182 initialise the pointer 10-03-04 MZ. */
532 timRoot = t;
533 return;
534 }
535 prev = timRoot;
536 curr = timRoot->next;
537 while (curr && t->left >= curr->left)
538 {
539 prev = curr;
540 curr = curr->next;
541 }
542 prev->next = t;
543 t->next = curr;
544 }
545
546
547 /*
548 +--------------------------------------------------------------------+
549 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
550 | STATE : code ROUTINE : timRemove |
551 +--------------------------------------------------------------------+
552
553 PURPOSE : remove timer from list
554
555 */
556
557 static void timRemove (MfwTim *t)
558 {
559 MfwTim *prev, *curr;
560 MfwTim *timCheck;
561 int doneFlag = 0;
562
563 if (t == 0)
564 return;
565
566 while (timRoot == t)
567 timRoot = t->next;
568
569 if (timRoot)
570 {
571 prev = timRoot;
572 curr = timRoot->next;
573
574 while (curr)
575 {
576 if (curr == t)
577 prev->next = curr->next;
578 else
579 prev = curr;
580 curr = curr->next;
581 }
582 }
583
584 /*
585 ** Ensure that the timer being deleted is not on the ActiveTOut List
586 */
587 if (timSavedNext)
588 {
589 if (timSavedNext == t)
590 {
591 timSavedNext = timSavedNext->next2;
592 }
593 else
594 {
595 timCheck = timSavedNext;
596 if(timCheck == NULL || (ULONG)timCheck > 33554432)
597 { /* cq18182 Additional traces to trap pointer overflow. 10-03-04 MZ. */
598 TRACE_EVENT_P1("ERROR: timCheck invalid 0x%08x - mfw_tim.c(593), quit the function", timCheck);
599 return;
600 }
601
602
603 while (timCheck != 0 && !doneFlag)
604 {
605
606 if(timCheck == NULL || (ULONG)timCheck > 33554432)
607 { /* cq18182 Additional traces to trap pointer overflow. 10-03-04 MZ. */
608 TRACE_EVENT_P1("ERROR: timCheck invalid 0x%08x - mfw_tim.c(603), quit the function", timCheck);
609 return;
610 }
611
612 if (timCheck->next2 == t)
613 {
614 timCheck->next2 = t->next2;
615
616 doneFlag = (int)1;
617 }
618 else
619 {
620 if(timCheck == NULL || (ULONG)timCheck > 33554432)
621 { /* cq18182 Additional traces to trap pointer overflow. 10-03-04 MZ. */
622 TRACE_EVENT_P1("ERROR: timCheck invalid 0x%08x - mfw_tim.c(617), quit the function", timCheck);
623 return;
624 }
625 timCheck = timCheck->next2;
626
627 }
628 }
629 }
630 }
631 }
632
633
634 /*
635 +--------------------------------------------------------------------+
636 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
637 | STATE : code ROUTINE : timAdjust |
638 +--------------------------------------------------------------------+
639
640 PURPOSE : adjust all timers in list
641
642 */
643
644 static void timAdjust (S32 t)
645 {
646 MfwTim *tc;
647
648 tc = timRoot;
649 while (tc)
650 {
651 tc->left += t;
652 tc = tc->next;
653 }
654 }
655
656
657 /*
658 +--------------------------------------------------------------------+
659 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
660 | STATE : code ROUTINE : timFind |
661 +--------------------------------------------------------------------+
662
663 PURPOSE : find timer in running list
664
665 */
666
667 static int timFind (MfwTim *t)
668 {
669 MfwTim *tc;
670
671 tc = timRoot;
672 while (tc)
673 {
674 if (tc == t)
675 return 1;
676 tc = tc->next;
677 }
678
679 return 0;
680 }
681
682
683 /*
684 +--------------------------------------------------------------------+
685 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
686 | STATE : code ROUTINE : timCommand |
687 +--------------------------------------------------------------------+
688
689 PURPOSE : handle mfw windows command
690
691 */
692
693 static int timCommand (U32 cmd, void *h)
694 {
695 switch (cmd)
696 {
697 case MfwCmdDelete: /* delete me */
698 if (!h)
699 return 0;
700 timDelete(h);
701 return 1;
702 default:
703 break;
704 }
705
706 return 0;
707 }
708