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