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