Debian Codegraph @

contrib/ports/unix/proj/minimal/timer.c

raw
1 /*
2 * Copyright (c) 2006 Christiaan Simons.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25 * OF SUCH DAMAGE.
26 *
27 * This file is part of and a contribution to the lwIP TCP/IP stack.
28 *
29 * Author: Christiaan Simons <[email protected]>
30 */
31
32 /**
33 * @file
34 * Simple pre-allocated interval (reload) timers.
35 * @see timer.h
36 */
37
38 #include <stddef.h>
39 #include <signal.h>
40 #include <sys/time.h>
41 #include "timer.h"
42 #include "lwip/snmp.h"
43
44 static struct itimerval tmr;
45
46 struct itmr
47 {
48 volatile unsigned int interval;
49 volatile unsigned int cnt;
50 volatile unsigned char event;
51 };
52
53 static struct itmr timers[TIMER_NUM];
54
55
56 void sigalarm_handler(int sig);
57
58 /**
59 * Initializes interval timers.
60 */
61 void
62 timer_init(void)
63 {
64 unsigned char i;
65 struct itmr *tp;
66
67 tp = &timers[TIMER_NUM-1];
68 for(i = TIMER_NUM; i > 0; i--)
69 {
70 tp->event = 0;
71 tp->interval = 0;
72 tp->cnt = 0;
73 tp--;
74 }
75
76 signal(SIGALRM,sigalarm_handler);
77
78 /* timer reload is in 10msec steps */
79 tmr.it_interval.tv_sec = 0;
80 tmr.it_interval.tv_usec = 10000;
81 /* set to half period (enables timer) */
82 tmr.it_value.tv_sec = 0;
83 tmr.it_value.tv_usec = 5000;
84
85 setitimer(ITIMER_REAL,&tmr,NULL);
86 }
87
88 /**
89 * Configures timer.
90 *
91 * @param tmr the timer number from timer.h
92 * @param interval when > 0 enables this timer, 0 disables.
93 */
94 void
95 timer_set_interval(unsigned char tmr_num, unsigned int interval)
96 {
97 if (tmr_num < TIMER_NUM)
98 {
99 timers[tmr_num].interval = interval;
100 }
101 }
102
103
104 /**
105 * Returns timer event and restarts timer.
106 */
107 unsigned char
108 timer_testclr_evt(unsigned char tmr_num)
109 {
110 if (tmr_num < TIMER_NUM)
111 {
112 unsigned char evt;
113 struct itmr *tp;
114
115 tp = &timers[tmr_num];
116
117 evt = tp->event;
118 if (tp->event != 0)
119 {
120 tp->event = 0;
121 tp->cnt = tp->interval;
122 }
123 return evt;
124 }
125 else
126 {
127 return 0;
128 }
129 }
130
131 /**
132 * interrupting (!) sigalarm handler
133 */
134 void
135 sigalarm_handler(int sig)
136 {
137 unsigned char i;
138 struct itmr *tp;
139 LWIP_UNUSED_ARG(sig);
140
141 snmp_inc_sysuptime();
142
143 tp = &timers[TIMER_NUM-1];
144 for(i = TIMER_NUM; i > 0; i--)
145 {
146 if (tp->interval != 0)
147 {
148 /* timer is running */
149 if (tp->cnt == 0)
150 {
151 /* timer expired */
152 tp->event |= 1;
153 }
154 else
155 {
156 /* timer ticking */
157 tp->cnt--;
158 }
159 }
160 tp--;
161 }
162 }
163