|
1 | 1 | #include "coap_timer.h" |
2 | 2 |
|
| 3 | +#include "mem.h" |
| 4 | + |
| 5 | +#include <string.h> |
| 6 | +#include <sys/time.h> // TODO needs something like HAVE_TIME_H |
| 7 | +#include <signal.h> // TODO needs something like HAVE_SIGNAL_H |
| 8 | + |
3 | 9 | #ifdef __GNUC__ |
4 | 10 | #define UNUSED __attribute__((unused)) |
5 | 11 | #else |
6 | 12 | #define UNUSED |
7 | 13 | #endif |
8 | 14 |
|
| 15 | +struct coap_timer_t; |
| 16 | + |
9 | 17 | struct coap_timer_t { |
10 | | - int time; |
| 18 | + coap_timer_t *next_timer; |
| 19 | + CoapTimerCallback cb; |
| 20 | + coap_tick_t base_time; |
| 21 | + coap_tick_t duration; |
| 22 | + void *data; |
11 | 23 | }; |
12 | 24 |
|
13 | | -void coap_timer_init(void) { |
| 25 | +static struct sigaction timer_sigaction; |
| 26 | + |
| 27 | +static struct coap_timer_t *running_timers; |
| 28 | + |
| 29 | +static void update_itimer(void) { |
| 30 | + coap_tick_t num_ticks = running_timers->duration - running_timers->base_time; |
| 31 | + |
| 32 | + struct itimerval it = { |
| 33 | + .it_value.tv_sec = num_ticks / COAP_TICKS_PER_SECOND, |
| 34 | + .it_value.tv_usec = num_ticks % COAP_TICKS_PER_SECOND * 1000, // MS -> US |
| 35 | + }; |
14 | 36 |
|
| 37 | + setitimer(ITIMER_REAL, &it, NULL); |
15 | 38 | } |
16 | 39 |
|
17 | | -coap_timer_t *coap_new_timer(CoapTimerCallback cb UNUSED, void *data UNUSED) { |
18 | | - return NULL; |
| 40 | +static void sigaction_handler(int sig, siginfo_t *si UNUSED, void *uc UNUSED) { |
| 41 | + // TODO assert(running_timers); |
| 42 | + // remove timer from the running list |
| 43 | + coap_timer_t *timer = running_timers; |
| 44 | + running_timers = running_timers->next_timer; |
| 45 | + |
| 46 | + // execute timer callback |
| 47 | + if (timer->cb) { |
| 48 | + timer->cb(timer->data); |
| 49 | + } |
| 50 | + |
| 51 | + if (running_timers) { |
| 52 | + // set the itimer again |
| 53 | + update_itimer(); |
| 54 | + } |
| 55 | + |
| 56 | + signal(sig, SIG_IGN); |
19 | 57 | } |
20 | 58 |
|
21 | | -void coap_free_timer(coap_timer_t *timer UNUSED) { |
22 | | - return; |
| 59 | +void coap_timer_init(void) { |
| 60 | + timer_sigaction.sa_sigaction = sigaction_handler; |
| 61 | + sigaction(SIGALRM, &timer_sigaction, NULL); |
23 | 62 | } |
24 | 63 |
|
25 | | -void coap_timer_set(coap_timer_t *timer UNUSED, coap_tick_t num_ticks UNUSED) { |
26 | | - return; |
| 64 | +coap_timer_t *coap_new_timer(CoapTimerCallback cb, void *data) { |
| 65 | + coap_timer_t *timer = coap_malloc_type(COAP_TIMER, sizeof(coap_timer_t)); |
| 66 | + memset(timer, 0, sizeof(coap_timer_t)); |
| 67 | + timer->cb = cb; |
| 68 | + timer->data = data; |
| 69 | + return timer; |
27 | 70 | } |
28 | 71 |
|
29 | | -void coap_timer_is_set(coap_timer_t *timer UNUSED) { |
30 | | - return; |
| 72 | +void coap_free_timer(coap_timer_t *timer) { |
| 73 | + // TODO remove from list |
| 74 | + memset(timer, 0, sizeof(coap_timer_t)); |
| 75 | + coap_free_type(COAP_TIMER, timer); |
31 | 76 | } |
32 | 77 |
|
33 | | -void coap_timer_unset(coap_timer_t *timer UNUSED) { |
34 | | - return; |
| 78 | +void coap_timer_set(coap_timer_t *timer, coap_tick_t num_ticks) { |
| 79 | + coap_timer_t **cur = &running_timers; |
| 80 | + while (*cur && |
| 81 | + (*cur)->duration - (*cur)->base_time < num_ticks) { |
| 82 | + cur = &(*cur)->next_timer; |
| 83 | + } |
| 84 | + *cur = timer; |
| 85 | + |
| 86 | + // set basetime |
| 87 | + coap_ticks(&timer->base_time); |
| 88 | + |
| 89 | + if (timer == running_timers) { |
| 90 | + update_itimer(); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +char coap_timer_is_set(coap_timer_t *timer) { |
| 95 | + coap_timer_t *cur = running_timers; |
| 96 | + while (cur) { |
| 97 | + if (cur == timer) { |
| 98 | + return 1; |
| 99 | + } |
| 100 | + cur = cur->next_timer; |
| 101 | + } |
| 102 | + |
| 103 | + return 0; |
35 | 104 | } |
36 | 105 |
|
| 106 | +void coap_timer_unset(coap_timer_t *timer) { |
| 107 | + char update_timer = (timer == running_timers); |
| 108 | + |
| 109 | + coap_timer_t **cur = &running_timers; |
| 110 | + while (*cur) { |
| 111 | + if (*cur == timer) { |
| 112 | + break; |
| 113 | + } |
| 114 | + cur = &(*cur)->next_timer; |
| 115 | + } |
| 116 | + *cur = (*cur)->next_timer; |
| 117 | + |
| 118 | + if (update_timer) { |
| 119 | + update_itimer(); |
| 120 | + } |
| 121 | + |
| 122 | + return; |
| 123 | +} |
37 | 124 |
|
0 commit comments