xref: /nrf52832-nimble/packages/NimBLE-latest/porting/npl/mynewt/include/nimble/nimble_npl_os.h (revision 042d53a763ad75cb1465103098bb88c245d95138)
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 #ifndef _NIMBLE_NPL_OS_H_
21 #define _NIMBLE_NPL_OS_H_
22 
23 #include <stdint.h>
24 #include <string.h>
25 #include "os/os.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #define BLE_NPL_TIME_FOREVER    (OS_TIMEOUT_NEVER)
32 
33 typedef os_time_t ble_npl_time_t;
34 typedef os_stime_t ble_npl_stime_t;
35 
36 /*
37  * This allows to cast between ble_npl_* and os_* structs to make NPL for Mynewt
38  * just a shim layer on top of kernel/os.
39  */
40 
41 struct ble_npl_event {
42     struct os_event ev;
43 };
44 
45 struct ble_npl_eventq {
46     struct os_eventq evq;
47 };
48 
49 struct ble_npl_callout {
50     struct os_callout co;
51 };
52 
53 struct ble_npl_mutex {
54     struct os_mutex mu;
55 };
56 
57 struct ble_npl_sem {
58     struct os_sem sem;
59 };
60 
61 static inline bool
ble_npl_os_started(void)62 ble_npl_os_started(void)
63 {
64     return os_started();
65 }
66 
67 static inline void *
ble_npl_get_current_task_id(void)68 ble_npl_get_current_task_id(void)
69 {
70     return os_sched_get_current_task();
71 }
72 
73 static inline struct ble_npl_eventq *
ble_npl_eventq_dflt_get(void)74 ble_npl_eventq_dflt_get(void)
75 {
76     return (struct ble_npl_eventq *) os_eventq_dflt_get();
77 }
78 
79 static inline void
ble_npl_eventq_init(struct ble_npl_eventq * evq)80 ble_npl_eventq_init(struct ble_npl_eventq *evq)
81 {
82     os_eventq_init(&evq->evq);
83 }
84 
85 static inline struct ble_npl_event *
ble_npl_eventq_get(struct ble_npl_eventq * evq,ble_npl_time_t tmo)86 ble_npl_eventq_get(struct ble_npl_eventq *evq, ble_npl_time_t tmo)
87 {
88     struct os_event *ev;
89 
90     if (tmo == BLE_NPL_TIME_FOREVER) {
91         ev = os_eventq_get(&evq->evq);
92     } else {
93         ev = os_eventq_poll((struct os_eventq **)&evq, 1, tmo);
94     }
95 
96     return (struct ble_npl_event *)ev;
97 }
98 
99 static inline void
ble_npl_eventq_put(struct ble_npl_eventq * evq,struct ble_npl_event * ev)100 ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
101 {
102     os_eventq_put(&evq->evq, &ev->ev);
103 }
104 
105 static inline void
ble_npl_eventq_remove(struct ble_npl_eventq * evq,struct ble_npl_event * ev)106 ble_npl_eventq_remove(struct ble_npl_eventq *evq,
107                       struct ble_npl_event *ev)
108 {
109     os_eventq_remove(&evq->evq, &ev->ev);
110 }
111 
112 static inline void
ble_npl_event_run(struct ble_npl_event * ev)113 ble_npl_event_run(struct ble_npl_event *ev)
114 {
115     ev->ev.ev_cb(&ev->ev);
116 }
117 
118 static inline bool
ble_npl_eventq_is_empty(struct ble_npl_eventq * evq)119 ble_npl_eventq_is_empty(struct ble_npl_eventq *evq)
120 {
121     return STAILQ_EMPTY(&evq->evq.evq_list);
122 }
123 
124 static inline void
ble_npl_event_init(struct ble_npl_event * ev,ble_npl_event_fn * fn,void * arg)125 ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn,
126                    void *arg)
127 {
128     memset(ev, 0, sizeof(*ev));
129     ev->ev.ev_queued = 0;
130     ev->ev.ev_cb = (os_event_fn *)fn;
131     ev->ev.ev_arg = arg;
132 }
133 
134 static inline bool
ble_npl_event_is_queued(struct ble_npl_event * ev)135 ble_npl_event_is_queued(struct ble_npl_event *ev)
136 {
137     return ev->ev.ev_queued;
138 }
139 
140 static inline void *
ble_npl_event_get_arg(struct ble_npl_event * ev)141 ble_npl_event_get_arg(struct ble_npl_event *ev)
142 {
143     return ev->ev.ev_arg;
144 }
145 
146 static inline void
ble_npl_event_set_arg(struct ble_npl_event * ev,void * arg)147 ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg)
148 {
149     ev->ev.ev_arg = arg;
150 }
151 
152 static inline ble_npl_error_t
ble_npl_mutex_init(struct ble_npl_mutex * mu)153 ble_npl_mutex_init(struct ble_npl_mutex *mu)
154 {
155     return os_mutex_init(&mu->mu);
156 }
157 
158 static inline ble_npl_error_t
ble_npl_mutex_pend(struct ble_npl_mutex * mu,ble_npl_time_t timeout)159 ble_npl_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t timeout)
160 {
161     return os_mutex_pend(&mu->mu, timeout);
162 }
163 
164 static inline ble_npl_error_t
ble_npl_mutex_release(struct ble_npl_mutex * mu)165 ble_npl_mutex_release(struct ble_npl_mutex *mu)
166 {
167     return os_mutex_release(&mu->mu);
168 }
169 
170 static inline ble_npl_error_t
ble_npl_sem_init(struct ble_npl_sem * sem,uint16_t tokens)171 ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens)
172 {
173     return os_sem_init(&sem->sem, tokens);
174 }
175 
176 static inline ble_npl_error_t
ble_npl_sem_pend(struct ble_npl_sem * sem,ble_npl_time_t timeout)177 ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout)
178 {
179     return os_sem_pend(&sem->sem, timeout);
180 }
181 
182 static inline ble_npl_error_t
ble_npl_sem_release(struct ble_npl_sem * sem)183 ble_npl_sem_release(struct ble_npl_sem *sem)
184 {
185     return os_sem_release(&sem->sem);
186 }
187 
188 static inline uint16_t
ble_npl_sem_get_count(struct ble_npl_sem * sem)189 ble_npl_sem_get_count(struct ble_npl_sem *sem)
190 {
191     return os_sem_get_count(&sem->sem);
192 }
193 
194 static inline void
ble_npl_callout_init(struct ble_npl_callout * co,struct ble_npl_eventq * evq,ble_npl_event_fn * ev_cb,void * ev_arg)195 ble_npl_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
196                      ble_npl_event_fn *ev_cb, void *ev_arg)
197 {
198     os_callout_init(&co->co, &evq->evq, (os_event_fn *)ev_cb, ev_arg);
199 }
200 
201 static inline ble_npl_error_t
ble_npl_callout_reset(struct ble_npl_callout * co,ble_npl_time_t ticks)202 ble_npl_callout_reset(struct ble_npl_callout *co, ble_npl_time_t ticks)
203 {
204     return os_callout_reset(&co->co, ticks);
205 }
206 
207 static inline void
ble_npl_callout_stop(struct ble_npl_callout * co)208 ble_npl_callout_stop(struct ble_npl_callout *co)
209 {
210     os_callout_stop(&co->co);
211 }
212 
213 static inline bool
ble_npl_callout_is_active(struct ble_npl_callout * co)214 ble_npl_callout_is_active(struct ble_npl_callout *co)
215 {
216     return os_callout_queued(&co->co);
217 }
218 
219 static inline ble_npl_time_t
ble_npl_callout_get_ticks(struct ble_npl_callout * co)220 ble_npl_callout_get_ticks(struct ble_npl_callout *co)
221 {
222     return co->co.c_ticks;
223 }
224 
225 static inline ble_npl_time_t
ble_npl_callout_remaining_ticks(struct ble_npl_callout * co,ble_npl_time_t time)226 ble_npl_callout_remaining_ticks(struct ble_npl_callout *co,
227                                 ble_npl_time_t time)
228 {
229     return os_callout_remaining_ticks(&co->co, time);
230 }
231 
232 static inline void
ble_npl_callout_set_arg(struct ble_npl_callout * co,void * arg)233 ble_npl_callout_set_arg(struct ble_npl_callout *co,
234                         void *arg)
235 {
236     co->co.c_ev.ev_arg = arg;
237 }
238 
239 static inline uint32_t
ble_npl_time_get(void)240 ble_npl_time_get(void)
241 {
242     return os_time_get();
243 }
244 
245 static inline ble_npl_error_t
ble_npl_time_ms_to_ticks(uint32_t ms,ble_npl_time_t * out_ticks)246 ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
247 {
248     return os_time_ms_to_ticks(ms, out_ticks);
249 }
250 
251 static inline ble_npl_error_t
ble_npl_time_ticks_to_ms(ble_npl_time_t ticks,uint32_t * out_ms)252 ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
253 {
254     return os_time_ticks_to_ms(ticks, out_ms);
255 }
256 
257 static inline ble_npl_time_t
ble_npl_time_ms_to_ticks32(uint32_t ms)258 ble_npl_time_ms_to_ticks32(uint32_t ms)
259 {
260     return os_time_ms_to_ticks32(ms);
261 }
262 
263 static inline uint32_t
ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)264 ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)
265 {
266     return os_time_ticks_to_ms32(ticks);
267 }
268 
269 static inline void
ble_npl_time_delay(ble_npl_time_t ticks)270 ble_npl_time_delay(ble_npl_time_t ticks)
271 {
272     return os_time_delay(ticks);
273 }
274 
275 static inline uint32_t
ble_npl_hw_enter_critical(void)276 ble_npl_hw_enter_critical(void)
277 {
278     return os_arch_save_sr();
279 }
280 
281 static inline void
ble_npl_hw_exit_critical(uint32_t ctx)282 ble_npl_hw_exit_critical(uint32_t ctx)
283 {
284     os_arch_restore_sr(ctx);
285 }
286 
287 #ifdef __cplusplus
288 }
289 #endif
290 
291 #endif  /* _NPL_H_ */
292