• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <assert.h>
24 #include <stdint.h>
25 #include <string.h>
26 
27 #include "wm_osal.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #define BLE_NPL_OS_ALIGNMENT    4
34 
35 #define BLE_NPL_TIME_FOREVER    0xffffffffUL
36 
37 /* This should be compatible with TickType_t */
38 typedef uint32_t ble_npl_time_t;
39 typedef int32_t ble_npl_stime_t;
40 
41 struct ble_npl_event {
42     bool queued;
43     ble_npl_event_fn *fn;
44     void *arg;
45 };
46 
47 struct ble_npl_eventq {
48     tls_os_queue_t *q;
49 };
50 
51 struct ble_npl_callout {
52     tls_os_timer_t *handle;
53     struct ble_npl_eventq *evq;
54     struct ble_npl_event ev;
55 };
56 
57 struct ble_npl_mutex {
58     tls_os_mutex_t *handle;
59 };
60 
61 struct ble_npl_sem {
62     tls_os_sem_t *handle;
63 };
64 
65 /*
66  * Simple APIs are just defined as static inline below, but some are a bit more
67  * complex or require some global state variables and thus are defined in .c
68  * file instead and static inline wrapper just calls proper implementation.
69  * We need declarations of these functions and they are defined in header below.
70  */
71 #include "os/npl_freertos.h"
72 
ble_npl_os_started(void)73 static inline bool ble_npl_os_started(void)
74 {
75     return tls_os_task_schedule_state() != 0;
76 }
77 
ble_npl_get_current_task_id(void)78 static inline void *ble_npl_get_current_task_id(void)
79 {
80     return (void *)tls_os_task_id();
81 }
82 
ble_npl_eventq_init(struct ble_npl_eventq * evq)83 static inline void ble_npl_eventq_init(struct ble_npl_eventq *evq)
84 {
85     tls_os_queue_create(&evq->q, 32); // 32:queue size
86 
87     if (evq->q == NULL) {
88         assert(0);
89     }
90 }
91 
ble_npl_eventq_deinit(struct ble_npl_eventq * evq)92 static inline void ble_npl_eventq_deinit(struct ble_npl_eventq *evq)
93 {
94     if (evq && evq->q) {
95         tls_os_queue_delete(evq->q);
96     }
97 }
98 
ble_npl_eventq_get(struct ble_npl_eventq * evq,ble_npl_time_t tmo)99 static inline struct ble_npl_event *ble_npl_eventq_get(struct ble_npl_eventq *evq, ble_npl_time_t tmo)
100 {
101     return npl_freertos_eventq_get(evq, tmo);
102 }
103 
ble_npl_eventq_put(struct ble_npl_eventq * evq,struct ble_npl_event * ev)104 static inline void ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
105 {
106     npl_freertos_eventq_put(evq, ev);
107 }
108 
ble_npl_eventq_remove(struct ble_npl_eventq * evq,struct ble_npl_event * ev)109 static inline void ble_npl_eventq_remove(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
110 {
111     npl_freertos_eventq_remove(evq, ev);
112 }
113 
ble_npl_event_run(struct ble_npl_event * ev)114 static inline void ble_npl_event_run(struct ble_npl_event *ev)
115 {
116     ev->fn(ev);
117 }
118 
ble_npl_eventq_is_empty(struct ble_npl_eventq * evq)119 static inline bool ble_npl_eventq_is_empty(struct ble_npl_eventq *evq)
120 {
121     return tls_os_queue_is_empty(evq->q);
122 }
123 
ble_npl_event_init(struct ble_npl_event * ev,ble_npl_event_fn * fn,void * arg)124 static inline void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn, void *arg)
125 {
126     memset_s(ev, sizeof(*ev), 0, sizeof(*ev));
127     ev->fn = fn;
128     ev->arg = arg;
129 }
130 
ble_npl_event_is_queued(struct ble_npl_event * ev)131 static inline bool ble_npl_event_is_queued(struct ble_npl_event *ev)
132 {
133     return ev->queued;
134 }
135 
ble_npl_event_get_arg(struct ble_npl_event * ev)136 static inline void *ble_npl_event_get_arg(struct ble_npl_event *ev)
137 {
138     return ev->arg;
139 }
140 
ble_npl_event_set_arg(struct ble_npl_event * ev,void * arg)141 static inline void ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg)
142 {
143     ev->arg = arg;
144 }
145 
ble_npl_mutex_init(struct ble_npl_mutex * mu)146 static inline ble_npl_error_t ble_npl_mutex_init(struct ble_npl_mutex *mu)
147 {
148     return npl_freertos_mutex_init(mu);
149 }
150 
ble_npl_mutex_deinit(struct ble_npl_mutex * mu)151 static inline ble_npl_error_t ble_npl_mutex_deinit(struct ble_npl_mutex *mu)
152 {
153     return npl_freertos_mutex_deinit(mu);
154 }
155 
ble_npl_mutex_pend(struct ble_npl_mutex * mu,ble_npl_time_t timeout)156 static inline ble_npl_error_t ble_npl_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t timeout)
157 {
158     return npl_freertos_mutex_pend(mu, timeout);
159 }
160 
ble_npl_mutex_release(struct ble_npl_mutex * mu)161 static inline ble_npl_error_t ble_npl_mutex_release(struct ble_npl_mutex *mu)
162 {
163     return npl_freertos_mutex_release(mu);
164 }
165 
ble_npl_sem_init(struct ble_npl_sem * sem,uint16_t tokens)166 static inline ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens)
167 {
168     return npl_freertos_sem_init(sem, tokens);
169 }
ble_npl_sem_deinit(struct ble_npl_sem * sem)170 static inline ble_npl_error_t ble_npl_sem_deinit(struct ble_npl_sem *sem)
171 {
172     return npl_freertos_sem_deinit(sem);
173 }
174 
ble_npl_sem_pend(struct ble_npl_sem * sem,ble_npl_time_t timeout)175 static inline ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout)
176 {
177     return npl_freertos_sem_pend(sem, timeout);
178 }
179 
ble_npl_sem_release(struct ble_npl_sem * sem)180 static inline ble_npl_error_t ble_npl_sem_release(struct ble_npl_sem *sem)
181 {
182     return npl_freertos_sem_release(sem);
183 }
184 
ble_npl_sem_get_count(struct ble_npl_sem * sem)185 static inline uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem)
186 {
187     return npl_freertos_get_sem_count(sem);
188 }
189 
ble_npl_callout_init(struct ble_npl_callout * co,struct ble_npl_eventq * evq,ble_npl_event_fn * ev_cb,void * ev_arg)190 static inline void ble_npl_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
191                                         ble_npl_event_fn *ev_cb, void *ev_arg)
192 {
193     npl_freertos_callout_init(co, evq, ev_cb, ev_arg);
194 }
195 
ble_npl_callout_deinit(struct ble_npl_callout * co)196 static inline void ble_npl_callout_deinit(struct ble_npl_callout *co)
197 {
198     if (co && co->handle) {
199         tls_os_timer_delete(co->handle);
200     }
201 }
202 
ble_npl_callout_reset(struct ble_npl_callout * co,ble_npl_time_t ticks)203 static inline ble_npl_error_t ble_npl_callout_reset(struct ble_npl_callout *co, ble_npl_time_t ticks)
204 {
205     return npl_freertos_callout_reset(co, ticks);
206 }
207 
ble_npl_callout_stop(struct ble_npl_callout * co)208 static inline void ble_npl_callout_stop(struct ble_npl_callout *co)
209 {
210     tls_os_timer_stop(co->handle);
211 }
212 
ble_npl_callout_is_active(struct ble_npl_callout * co)213 static inline bool ble_npl_callout_is_active(struct ble_npl_callout *co)
214 {
215     return tls_os_timer_active(co->handle) == 1;
216 }
217 
ble_npl_callout_get_ticks(struct ble_npl_callout * co)218 static inline ble_npl_time_t ble_npl_callout_get_ticks(struct ble_npl_callout *co)
219 {
220     return tls_os_timer_expirytime(co->handle);
221 }
222 
ble_npl_callout_remaining_ticks(struct ble_npl_callout * co,ble_npl_time_t time)223 static inline uint32_t ble_npl_callout_remaining_ticks(struct ble_npl_callout *co, ble_npl_time_t time)
224 {
225     return npl_freertos_callout_remaining_ticks(co, time);
226 }
227 
ble_npl_callout_set_arg(struct ble_npl_callout * co,void * arg)228 static inline void ble_npl_callout_set_arg(struct ble_npl_callout *co, void *arg)
229 {
230     co->ev.arg = arg;
231 }
232 
ble_npl_time_get(void)233 static inline uint32_t ble_npl_time_get(void)
234 {
235     return tls_os_get_time();
236 }
237 
ble_npl_time_ms_to_ticks(uint32_t ms,ble_npl_time_t * out_ticks)238 static inline ble_npl_error_t ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
239 {
240     return npl_freertos_time_ms_to_ticks(ms, out_ticks);
241 }
242 
ble_npl_time_ticks_to_ms(ble_npl_time_t ticks,uint32_t * out_ms)243 static inline ble_npl_error_t ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
244 {
245     return ble_npl_time_ticks_to_ms(ticks, out_ms);
246 }
247 
ble_npl_time_ms_to_ticks32(uint32_t ms)248 static inline ble_npl_time_t ble_npl_time_ms_to_ticks32(uint32_t ms)
249 {
250     return ms * HZ / 1000; // 1000; BYTE ALIGNMENT
251 }
252 
ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)253 static inline uint32_t ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)
254 {
255     return ticks * 1000 / HZ; // 1000; BYTE ALIGNMENT
256 }
257 
ble_npl_time_delay(ble_npl_time_t ticks)258 static inline void ble_npl_time_delay(ble_npl_time_t ticks)
259 {
260     tls_os_time_delay(ticks);
261 }
262 
263 #if NIMBLE_CFG_CONTROLLER
ble_npl_hw_set_isr(int irqn,void (* addr)(void))264 static inline void ble_npl_hw_set_isr(int irqn, void (*addr)(void))
265 {
266     npl_freertos_hw_set_isr(irqn, addr);
267 }
268 #endif
269 
ble_npl_hw_enter_critical(void)270 static inline uint32_t ble_npl_hw_enter_critical(void)
271 {
272     vPortEnterCritical();
273     return 0;
274 }
275 
ble_npl_hw_exit_critical(uint32_t ctx)276 static inline void ble_npl_hw_exit_critical(uint32_t ctx)
277 {
278     vPortExitCritical();
279 }
280 
281 #ifdef __cplusplus
282 }
283 #endif
284 
285 #endif  /* _NPL_H_ */