Select one of the symbols to view example projects that use it.
 
Outline
#include <errno.h>
#include "osi/hash_map.h"
#include "osi/alarm.h"
#include "osi/hash_functions.h"
#include "mesh/config.h"
#include "mesh/common.h"
bm_alarm_hash_map
BLE_MESH_ALARM_HASH_MAP_SIZE
alarm_t
k_uptime_get()
k_uptime_get_32()
bt_mesh_timer_init()
bt_mesh_timer_deinit()
k_delayed_work_init(struct k_delayed_work *, k_work_handler_t)
k_delayed_work_submit(struct k_delayed_work *, int32_t)
k_delayed_work_submit_periodic(struct k_delayed_work *, int32_t)
k_delayed_work_cancel(struct k_delayed_work *)
k_delayed_work_free(struct k_delayed_work *)
k_delayed_work_remaining_get(struct k_delayed_work *)
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/bt/esp_ble_mesh/common/timer.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2016 Intel Corporation * SPDX-FileCopyrightText: 2016 Wind River Systems, Inc. * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <errno.h> #include "osi/hash_map.h" #include "osi/alarm.h" #include "osi/hash_functions.h" #include "mesh/config.h" #include "mesh/common.h"6 includes static hash_map_t *bm_alarm_hash_map; static const size_t BLE_MESH_ALARM_HASH_MAP_SIZE = 20 + CONFIG_BLE_MESH_PBA_SAME_TIME + CONFIG_BLE_MESH_PBG_SAME_TIME; typedef struct alarm_t { /* timer id point to here */ esp_timer_handle_t alarm_hdl; osi_alarm_callback_t cb; void *cb_data; int64_t deadline_us; }{ ... } osi_alarm_t; int64_t k_uptime_get(void) { /* k_uptime_get_32 is in in milliseconds, * but esp_timer_get_time is in microseconds *//* ... */ return (esp_timer_get_time() / 1000); }{ ... } uint32_t k_uptime_get_32(void) { /* k_uptime_get_32 is in in milliseconds, * but esp_timer_get_time is in microseconds *//* ... */ return (uint32_t)(esp_timer_get_time() / 1000); }{ ... } void bt_mesh_timer_init(void) { bm_alarm_hash_map = hash_map_new(BLE_MESH_ALARM_HASH_MAP_SIZE, hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL); __ASSERT(bm_alarm_hash_map, "Failed to create hash map"); }{ ... } #if CONFIG_BLE_MESH_DEINIT void bt_mesh_timer_deinit(void) { if (bm_alarm_hash_map) { hash_map_free(bm_alarm_hash_map); bm_alarm_hash_map = NULL; }{...} }{ ... } /* ... */#endif /* CONFIG_BLE_MESH_DEINIT */ int k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler) { osi_alarm_t *alarm = NULL; if (!work || !bm_alarm_hash_map) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; }{...} k_work_init(&work->work, handler); bt_mesh_alarm_lock(); alarm = hash_map_get(bm_alarm_hash_map, work); if (alarm) { BT_ERR("Init, alarm already exists"); bt_mesh_alarm_unlock(); return -EEXIST; }{...} alarm = osi_alarm_new("bt_mesh", (osi_alarm_callback_t)handler, (void *)&work->work, 0); if (alarm == NULL) { BT_ERR("Init, alarm not created"); bt_mesh_alarm_unlock(); return -EIO; }{...} if (!hash_map_set(bm_alarm_hash_map, work, (void *)alarm)) { BT_ERR("Init, alarm not set"); bt_mesh_alarm_unlock(); return -EIO; }{...} // Just init the work timer only, don't start it. osi_alarm_cancel(alarm); bt_mesh_alarm_unlock(); return 0; }{ ... } int k_delayed_work_submit(struct k_delayed_work *work, int32_t delay) { osi_alarm_t *alarm = NULL; if (!work || !bm_alarm_hash_map) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; }{...} /* If delay is 0, call the corresponding timeout handler. */ if (delay == 0) { k_work_submit(&work->work); return 0; }{...} bt_mesh_alarm_lock(); alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { BT_WARN("Submit, alarm not found"); bt_mesh_alarm_unlock(); return -EINVAL; }{...} // Cancel the alarm first, before start the alarm. osi_alarm_cancel(alarm); osi_alarm_set(alarm, delay); bt_mesh_alarm_unlock(); return 0; }{ ... } int k_delayed_work_submit_periodic(struct k_delayed_work *work, int32_t period) { osi_alarm_t *alarm = NULL; if (!work || !bm_alarm_hash_map) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; }{...} /* If period is 0, call the corresponding timeout handler. */ if (period == 0) { k_work_submit(&work->work); return 0; }{...} bt_mesh_alarm_lock(); alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { BT_WARN("Submit, alarm not found"); bt_mesh_alarm_unlock(); return -EINVAL; }{...} /* Cancel the alarm first before starting it. */ osi_alarm_cancel(alarm); osi_alarm_set_periodic(alarm, period); bt_mesh_alarm_unlock(); return 0; }{ ... } int k_delayed_work_cancel(struct k_delayed_work *work) { osi_alarm_t *alarm = NULL; if (!work || !bm_alarm_hash_map) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; }{...} bt_mesh_alarm_lock(); alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { BT_WARN("Cancel, alarm not found"); bt_mesh_alarm_unlock(); return -EINVAL; }{...} osi_alarm_cancel(alarm); alarm->deadline_us = 0; bt_mesh_alarm_unlock(); return 0; }{ ... } int k_delayed_work_free(struct k_delayed_work *work) { osi_alarm_t *alarm = NULL; if (!work || !bm_alarm_hash_map) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; }{...} bt_mesh_alarm_lock(); alarm = hash_map_get(bm_alarm_hash_map, work); if (alarm == NULL) { BT_DBG("Free, alarm not found"); bt_mesh_alarm_unlock(); return -EINVAL; }{...} osi_alarm_cancel(alarm); hash_map_erase(bm_alarm_hash_map, work); bt_mesh_alarm_unlock(); return 0; }{ ... } int32_t k_delayed_work_remaining_get(struct k_delayed_work *work) { osi_alarm_t *alarm = NULL; int32_t time = 0; if (!work || !bm_alarm_hash_map) { BT_ERR("%s, Invalid parameter", __func__); return 0; }{...} bt_mesh_alarm_lock(); alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { BT_WARN("Get time, alarm not found"); bt_mesh_alarm_unlock(); return 0; }{...} time = osi_alarm_get_remaining_ms(alarm); bt_mesh_alarm_unlock(); return time; }{ ... }
Details