Skip to content

Commit b4dc35b

Browse files
author
franc0is
committed
WIP - simple timer implementation for POSIX platform
1 parent b84a353 commit b4dc35b

3 files changed

Lines changed: 102 additions & 14 deletions

File tree

include/coap/coap_timer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void coap_free_timer(coap_timer_t *timer);
1616

1717
void coap_timer_set(coap_timer_t *timer, coap_tick_t num_ticks);
1818

19-
void coap_timer_is_set(coap_timer_t *timer);
19+
char coap_timer_is_set(coap_timer_t *timer);
2020

2121
void coap_timer_unset(coap_timer_t *timer);
2222

include/coap/mem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ typedef enum {
3838
COAP_PDU,
3939
COAP_PDU_BUF,
4040
COAP_RESOURCE,
41-
COAP_RESOURCEATTR
41+
COAP_RESOURCEATTR,
42+
COAP_TIMER
4243
} coap_memory_tag_t;
4344

4445
#ifndef WITH_LWIP

platform/posix/platform_timer.c

Lines changed: 99 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,124 @@
11
#include "coap_timer.h"
22

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+
39
#ifdef __GNUC__
410
#define UNUSED __attribute__((unused))
511
#else
612
#define UNUSED
713
#endif
814

15+
struct coap_timer_t;
16+
917
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;
1123
};
1224

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+
};
1436

37+
setitimer(ITIMER_REAL, &it, NULL);
1538
}
1639

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);
1957
}
2058

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);
2362
}
2463

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;
2770
}
2871

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);
3176
}
3277

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;
35104
}
36105

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+
}
37124

0 commit comments

Comments
 (0)