comparison src/ui/mfw/mfw_tim.c @ 3:67bfe9f274f6

src/ui: import of src/ui3 from Magnetite
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 16 Oct 2020 06:33:10 +0000
parents
children 92abb46dc1ba
comparison
equal deleted inserted replaced
2:3a14ee9a9843 3:67bfe9f274f6
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 (void)((*(ActiveTOut->handler))(ActiveTOut->time,ActiveTOut));/*a0393213 lint warnings removal - void cast is done to avoid lint warning though the function returns int*/
459
460
461 /* NM, p011c */
462 dspl_Enable(temp);
463 /* p011c end */
464
465 }
466 /* PATCH PMC 000721: use the SavedNext pointer to set ActiveTOut */
467 ActiveTOut = timSavedNext;
468 /* cq18182 pointer cleared here, this fix is only temporary as it seems to fix the current problem, however further investigation
469 is required as to why the timSavedNext pointer was not being cleared. 10-03-04 MZ. */
470 timSavedNext = NULL;
471 /* END PATCH PMC 000721 */
472 }
473
474 /* cq18182 add check and clear the pointer 10-03-04 MZ.*/
475 if(ActiveTOut != NULL)
476 ActiveTOut = NULL;
477
478 }
479
480
481 /*
482 +--------------------------------------------------------------------+
483 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
484 | STATE : code ROUTINE : timTimeout |
485 +--------------------------------------------------------------------+
486
487 PURPOSE : thread context time out
488
489 */
490
491 void timTimeout (void)
492 {
493 timTimeoutCount++;
494
495 if (timTimeoutBusy)
496 return;
497
498 timTimeoutBusy = 1;
499 while (timTimeoutCount)
500 {
501 timTimeoutCount--;
502 timSignal();
503 }
504 timTimeoutBusy = 0;
505 }
506
507
508 /*
509 +--------------------------------------------------------------------+
510 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
511 | STATE : code ROUTINE : timInsert |
512 +--------------------------------------------------------------------+
513
514 PURPOSE : insert timer into ordered list
515
516 */
517
518 static void timInsert (MfwTim *t)
519 {
520 MfwTim *prev, *curr;
521
522 if (!timRoot)
523 {
524 timRoot = t;
525 t->next = 0;
526 t->next2 = 0; /* cq18182 initialise the pointer 10-03-04 MZ. */
527 return;
528 }
529 if (t->left < timRoot->left)
530 {
531 t->next = timRoot;
532 t->next2 = 0; /* cq18182 initialise the pointer 10-03-04 MZ. */
533 timRoot = t;
534 return;
535 }
536 prev = timRoot;
537 curr = timRoot->next;
538 while (curr && t->left >= curr->left)
539 {
540 prev = curr;
541 curr = curr->next;
542 }
543 prev->next = t;
544 t->next = curr;
545 }
546
547
548 /*
549 +--------------------------------------------------------------------+
550 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
551 | STATE : code ROUTINE : timRemove |
552 +--------------------------------------------------------------------+
553
554 PURPOSE : remove timer from list
555
556 */
557
558 static void timRemove (MfwTim *t)
559 {
560 MfwTim *prev, *curr;
561 MfwTim *timCheck;
562 int doneFlag = 0;
563
564 if (t == 0)
565 return;
566
567 while (timRoot == t)
568 timRoot = t->next;
569
570 if (timRoot)
571 {
572 prev = timRoot;
573 curr = timRoot->next;
574
575 while (curr)
576 {
577 if (curr == t)
578 prev->next = curr->next;
579 else
580 prev = curr;
581 curr = curr->next;
582 }
583 }
584
585 /*
586 ** Ensure that the timer being deleted is not on the ActiveTOut List
587 */
588 if (timSavedNext)
589 {
590 if (timSavedNext == t)
591 {
592 timSavedNext = timSavedNext->next2;
593 }
594 else
595 {
596 timCheck = timSavedNext;
597 if(timCheck == NULL || (ULONG)timCheck > 33554432)
598 { /* cq18182 Additional traces to trap pointer overflow. 10-03-04 MZ. */
599 TRACE_EVENT_P1("ERROR: timCheck invalid 0x%08x - mfw_tim.c(593), quit the function", timCheck);
600 return;
601 }
602
603
604 while (timCheck != 0 && !doneFlag)
605 {
606
607 if(timCheck == NULL || (ULONG)timCheck > 33554432)
608 { /* cq18182 Additional traces to trap pointer overflow. 10-03-04 MZ. */
609 TRACE_EVENT_P1("ERROR: timCheck invalid 0x%08x - mfw_tim.c(603), quit the function", timCheck);
610 return;
611 }
612
613 if (timCheck->next2 == t)
614 {
615 timCheck->next2 = t->next2;
616
617 doneFlag = (int)1;
618 }
619 else
620 {
621 if(timCheck == NULL || (ULONG)timCheck > 33554432)
622 { /* cq18182 Additional traces to trap pointer overflow. 10-03-04 MZ. */
623 TRACE_EVENT_P1("ERROR: timCheck invalid 0x%08x - mfw_tim.c(617), quit the function", timCheck);
624 return;
625 }
626 timCheck = timCheck->next2;
627
628 }
629 }
630 }
631 }
632 }
633
634
635 /*
636 +--------------------------------------------------------------------+
637 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
638 | STATE : code ROUTINE : timAdjust |
639 +--------------------------------------------------------------------+
640
641 PURPOSE : adjust all timers in list
642
643 */
644
645 static void timAdjust (S32 t)
646 {
647 MfwTim *tc;
648
649 tc = timRoot;
650 while (tc)
651 {
652 tc->left += t;
653 tc = tc->next;
654 }
655 }
656
657
658 /*
659 +--------------------------------------------------------------------+
660 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
661 | STATE : code ROUTINE : timFind |
662 +--------------------------------------------------------------------+
663
664 PURPOSE : find timer in running list
665
666 */
667
668 static int timFind (MfwTim *t)
669 {
670 MfwTim *tc;
671
672 tc = timRoot;
673 while (tc)
674 {
675 if (tc == t)
676 return 1;
677 tc = tc->next;
678 }
679
680 return 0;
681 }
682
683
684 /*
685 +--------------------------------------------------------------------+
686 | PROJECT : MMI-Framework (8417) MODULE : MFW_TIM |
687 | STATE : code ROUTINE : timCommand |
688 +--------------------------------------------------------------------+
689
690 PURPOSE : handle mfw windows command
691
692 */
693
694 static int timCommand (U32 cmd, void *h)
695 {
696 switch (cmd)
697 {
698 case MfwCmdDelete: /* delete me */
699 if (!h)
700 return 0;
701 timDelete(h);
702 return 1;
703 default:
704 break;
705 }
706
707 return 0;
708 }
709