xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/include/nimble/nimble_npl.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_H_
21 #define _NIMBLE_NPL_H_
22 
23 #include <stdbool.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 struct ble_npl_event;
32 typedef void ble_npl_event_fn(struct ble_npl_event *ev);
33 typedef void ble_npl_task_fn(void *parameter);
34 
35 enum ble_npl_error {
36     BLE_NPL_OK = 0,
37     BLE_NPL_ENOMEM = 1,
38     BLE_NPL_EINVAL = 2,
39     BLE_NPL_INVALID_PARAM = 3,
40     BLE_NPL_MEM_NOT_ALIGNED = 4,
41     BLE_NPL_BAD_MUTEX = 5,
42     BLE_NPL_TIMEOUT = 6,
43     BLE_NPL_ERR_IN_ISR = 7,
44     BLE_NPL_ERR_PRIV = 8,
45     BLE_NPL_OS_NOT_STARTED = 9,
46     BLE_NPL_ENOENT = 10,
47     BLE_NPL_EBUSY = 11,
48     BLE_NPL_ERROR = 12,
49 };
50 
51 typedef enum ble_npl_error ble_npl_error_t;
52 
53 /* Include OS-specific definitions */
54 #include "nimble/nimble_npl_os.h"
55 
56 /*
57  * Generic
58  */
59 
60 bool ble_npl_os_started(void);
61 
62 void *ble_npl_get_current_task_id(void);
63 
64 /*
65  * Event queue
66  */
67 
68 void ble_npl_eventq_init(struct ble_npl_eventq *evq);
69 
70 struct ble_npl_event *ble_npl_eventq_get(struct ble_npl_eventq *evq,
71                                          ble_npl_time_t tmo);
72 
73 void ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev);
74 
75 void ble_npl_eventq_remove(struct ble_npl_eventq *evq,
76                            struct ble_npl_event *ev);
77 
78 void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn,
79                         void *arg);
80 
81 bool ble_npl_event_is_queued(struct ble_npl_event *ev);
82 
83 void *ble_npl_event_get_arg(struct ble_npl_event *ev);
84 
85 void ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg);
86 
87 bool ble_npl_eventq_is_empty(struct ble_npl_eventq *evq);
88 
89 void ble_npl_event_run(struct ble_npl_event *ev);
90 
91 /*
92  * Mutexes
93  */
94 
95 ble_npl_error_t ble_npl_mutex_init(struct ble_npl_mutex *mu);
96 
97 ble_npl_error_t ble_npl_mutex_pend(struct ble_npl_mutex *mu,
98                                    ble_npl_time_t timeout);
99 
100 ble_npl_error_t ble_npl_mutex_release(struct ble_npl_mutex *mu);
101 
102 /*
103  * Semaphores
104  */
105 
106 ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens);
107 
108 ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem,
109                                  ble_npl_time_t timeout);
110 
111 ble_npl_error_t ble_npl_sem_release(struct ble_npl_sem *sem);
112 
113 uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem);
114 
115 /*
116  * Callouts
117  */
118 
119 void ble_npl_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
120                           ble_npl_event_fn *ev_cb, void *ev_arg);
121 
122 ble_npl_error_t ble_npl_callout_reset(struct ble_npl_callout *co,
123                                       ble_npl_time_t ticks);
124 
125 void ble_npl_callout_stop(struct ble_npl_callout *co);
126 
127 bool ble_npl_callout_is_active(struct ble_npl_callout *co);
128 
129 ble_npl_time_t ble_npl_callout_get_ticks(struct ble_npl_callout *co);
130 
131 ble_npl_time_t ble_npl_callout_remaining_ticks(struct ble_npl_callout *co,
132                                                ble_npl_time_t time);
133 
134 void ble_npl_callout_set_arg(struct ble_npl_callout *co,
135                              void *arg);
136 /*
137  * Time functions
138  */
139 
140 ble_npl_time_t ble_npl_time_get(void);
141 
142 ble_npl_error_t ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks);
143 
144 ble_npl_error_t ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms);
145 
146 ble_npl_time_t ble_npl_time_ms_to_ticks32(uint32_t ms);
147 
148 uint32_t ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks);
149 
150 void ble_npl_time_delay(ble_npl_time_t ticks);
151 
152 /*
153  * Hardware-specific
154  *
155  * These symbols should be most likely defined by application since they are
156  * specific to hardware, not to OS.
157  */
158 
159 #if NIMBLE_CFG_CONTROLLER
160 
161 void ble_npl_hw_set_isr(int irqn, void (*addr)(void));
162 
163 #endif
164 
165 uint32_t ble_npl_hw_enter_critical(void);
166 
167 void ble_npl_hw_exit_critical(uint32_t ctx);
168 
169 void ble_hs_thread_startup(void);
170 
171 #ifdef __cplusplus
172 }
173 #endif
174 
175 #endif  /* _NIMBLE_NPL_H_ */
176