1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <string.h>
24
25 #include <time.h>
26 #include <signal.h>
27
28 #include "nimble/nimble_npl.h"
29
30 static void
ble_npl_callout_timer_cb(union sigval sv)31 ble_npl_callout_timer_cb(union sigval sv)
32 {
33 struct ble_npl_callout *c = (struct ble_npl_callout *)sv.sival_ptr;
34 assert(c);
35
36 if (c->c_evq) {
37 ble_npl_eventq_put(c->c_evq, &c->c_ev);
38 } else {
39 c->c_ev.ev_cb(&c->c_ev);
40 }
41 }
42
ble_npl_callout_init(struct ble_npl_callout * c,struct ble_npl_eventq * evq,ble_npl_event_fn * ev_cb,void * ev_arg)43 void ble_npl_callout_init(struct ble_npl_callout *c,
44 struct ble_npl_eventq *evq,
45 ble_npl_event_fn *ev_cb,
46 void *ev_arg)
47 {
48 struct sigevent event;
49
50 /* Initialize the callout. */
51 memset(c, 0, sizeof(*c));
52 c->c_ev.ev_cb = ev_cb;
53 c->c_ev.ev_arg = ev_arg;
54 c->c_evq = evq;
55 c->c_active = false;
56
57 event.sigev_notify = SIGEV_THREAD;
58 event.sigev_value.sival_ptr = c; // put callout obj in signal args
59 event.sigev_notify_function = ble_npl_callout_timer_cb;
60 event.sigev_notify_attributes = NULL;
61
62 timer_create(CLOCK_REALTIME, &event, &c->c_timer);
63 }
64
ble_npl_callout_is_active(struct ble_npl_callout * c)65 bool ble_npl_callout_is_active(struct ble_npl_callout *c)
66 {
67 // TODO: seek native posix method to determine whether timer_t is active.
68 // TODO: fix bug where one-shot timer is still active after fired.
69 return c->c_active;
70 }
71
ble_npl_callout_inited(struct ble_npl_callout * c)72 int ble_npl_callout_inited(struct ble_npl_callout *c)
73 {
74 return (c->c_timer != NULL);
75 }
76
ble_npl_callout_reset(struct ble_npl_callout * c,ble_npl_time_t ticks)77 ble_npl_error_t ble_npl_callout_reset(struct ble_npl_callout *c,
78 ble_npl_time_t ticks)
79 {
80 struct itimerspec its;
81
82 if (ticks < 0) {
83 return BLE_NPL_EINVAL;
84 }
85
86 if (ticks == 0) {
87 ticks = 1;
88 }
89
90 c->c_ticks = ble_npl_time_get() + ticks;
91
92 its.it_interval.tv_sec = 0;
93 its.it_interval.tv_nsec = 0; // one shot
94 its.it_value.tv_sec = (ticks / 1000);
95 its.it_value.tv_nsec = (ticks % 1000) * 1000000; // expiration
96 its.it_value.tv_nsec %= 1000000000;
97 c->c_active = true;
98 timer_settime(c->c_timer, 0, &its, NULL);
99
100 return BLE_NPL_OK;
101 }
102
ble_npl_callout_queued(struct ble_npl_callout * c)103 int ble_npl_callout_queued(struct ble_npl_callout *c)
104 {
105 struct itimerspec its;
106 timer_gettime(c->c_timer, &its);
107
108 return ((its.it_value.tv_sec > 0) ||
109 (its.it_value.tv_nsec > 0));
110 }
111
ble_npl_callout_stop(struct ble_npl_callout * c)112 void ble_npl_callout_stop(struct ble_npl_callout *c)
113 {
114 if (!ble_npl_callout_inited(c)) {
115 return;
116 }
117
118 struct itimerspec its;
119 its.it_interval.tv_sec = 0;
120 its.it_interval.tv_nsec = 0;
121 its.it_value.tv_sec = 0;
122 its.it_value.tv_nsec = 0;
123 timer_settime(c->c_timer, 0, &its, NULL);
124 c->c_active = false;
125 }
126
127 ble_npl_time_t
ble_npl_callout_get_ticks(struct ble_npl_callout * co)128 ble_npl_callout_get_ticks(struct ble_npl_callout *co)
129 {
130 return co->c_ticks;
131 }
132
133 void
ble_npl_callout_set_arg(struct ble_npl_callout * co,void * arg)134 ble_npl_callout_set_arg(struct ble_npl_callout *co, void *arg)
135 {
136 co->c_ev.ev_arg = arg;
137 }
138
139 uint32_t
ble_npl_callout_remaining_ticks(struct ble_npl_callout * co,ble_npl_time_t now)140 ble_npl_callout_remaining_ticks(struct ble_npl_callout *co,
141 ble_npl_time_t now)
142 {
143 ble_npl_time_t rt;
144 uint32_t exp;
145
146 struct itimerspec its;
147 timer_gettime(co->c_timer, &its);
148
149 exp = its.it_value.tv_sec * 1000;
150
151 if (exp > now) {
152 rt = exp - now;
153 } else {
154 rt = 0;
155 }
156
157 return rt;
158 }
159