-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathnrf_sdh_ble.c
More file actions
415 lines (347 loc) · 12.7 KB
/
nrf_sdh_ble.c
File metadata and controls
415 lines (347 loc) · 12.7 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <ble.h>
#include <bm/softdevice_handler/nrf_sdh.h>
#include <bm/softdevice_handler/nrf_sdh_ble.h>
#include <zephyr/logging/log.h>
#if defined(CONFIG_SOFTDEVICE_BSIM)
extern uint32_t g_app_ram_base;
extern uint32_t bsim_softdevice_ram_end;
#define APP_RAM_START (bsim_softdevice_ram_end)
#define SD_RAM_START (g_app_ram_base)
#else
#define APP_RAM_START DT_REG_ADDR(DT_CHOSEN(zephyr_sram))
#define SD_RAM_START DT_REG_ADDR(DT_NODELABEL(cpuapp_sram))
#endif
LOG_MODULE_DECLARE(nrf_sdh, CONFIG_NRF_SDH_LOG_LEVEL);
#define PERIPHERAL_LINKS \
COND_CODE_1(CONFIG_NRF_SDH_BLE_PERIPHERAL_LINK_COUNT, \
(CONFIG_NRF_SDH_BLE_PERIPHERAL_LINK_COUNT), (0))
#define CENTRAL_LINKS \
COND_CODE_1(CONFIG_NRF_SDH_BLE_CENTRAL_LINK_COUNT, \
(CONFIG_NRF_SDH_BLE_CENTRAL_LINK_COUNT), (0))
BUILD_ASSERT(PERIPHERAL_LINKS + CENTRAL_LINKS <= CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT,
"Invalid link configuration");
extern bool sdh_state_evt_observer_notify(enum nrf_sdh_state_evt state);
static uint32_t sd_ram_size;
const char *nrf_sdh_ble_evt_to_str(uint32_t evt)
{
int err;
static char buf[sizeof("BLE event: 0xFFFFFFFF")];
switch (evt) {
#if defined(CONFIG_NRF_SDH_STR_TABLES)
/* GAP */
case BLE_GAP_EVT_CONNECTED:
return "BLE_GAP_EVT_CONNECTED";
case BLE_GAP_EVT_DISCONNECTED:
return "BLE_GAP_EVT_DISCONNECTED";
case BLE_GAP_EVT_CONN_PARAM_UPDATE:
return "BLE_GAP_EVT_CONN_PARAM_UPDATE";
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
return "BLE_GAP_EVT_SEC_PARAMS_REQUEST";
case BLE_GAP_EVT_SEC_INFO_REQUEST:
return "BLE_GAP_EVT_SEC_INFO_REQUEST";
case BLE_GAP_EVT_PASSKEY_DISPLAY:
return "BLE_GAP_EVT_PASSKEY_DISPLAY";
case BLE_GAP_EVT_KEY_PRESSED:
return "BLE_GAP_EVT_KEY_PRESSED";
case BLE_GAP_EVT_AUTH_KEY_REQUEST:
return "BLE_GAP_EVT_AUTH_KEY_REQUEST";
case BLE_GAP_EVT_LESC_DHKEY_REQUEST:
return "BLE_GAP_EVT_LESC_DHKEY_REQUEST";
case BLE_GAP_EVT_AUTH_STATUS:
return "BLE_GAP_EVT_AUTH_STATUS";
case BLE_GAP_EVT_CONN_SEC_UPDATE:
return "BLE_GAP_EVT_CONN_SEC_UPDATE";
case BLE_GAP_EVT_TIMEOUT:
return "BLE_GAP_EVT_TIMEOUT";
case BLE_GAP_EVT_RSSI_CHANGED:
return "BLE_GAP_EVT_RSSI_CHANGED";
#if defined(CONFIG_SOFTDEVICE_CENTRAL)
case BLE_GAP_EVT_ADV_REPORT:
return "BLE_GAP_EVT_ADV_REPORT";
case BLE_GAP_EVT_SEC_REQUEST:
return "BLE_GAP_EVT_SEC_REQUEST";
case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
return "BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST";
#endif
case BLE_GAP_EVT_SCAN_REQ_REPORT:
return "BLE_GAP_EVT_SCAN_REQ_REPORT";
case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
return "BLE_GAP_EVT_PHY_UPDATE_REQUEST";
case BLE_GAP_EVT_PHY_UPDATE:
return "BLE_GAP_EVT_PHY_UPDATE";
#if defined(CONFIG_SOFTDEVICE_DATA_LENGTH_UPDATE)
case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
return "BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST";
case BLE_GAP_EVT_DATA_LENGTH_UPDATE:
return "BLE_GAP_EVT_DATA_LENGTH_UPDATE";
#endif
#if defined(CONFIG_SOFTDEVICE_QOS_CHANNEL_SURVEY_REPORT)
case BLE_GAP_EVT_QOS_CHANNEL_SURVEY_REPORT:
return "BLE_GAP_EVT_QOS_CHANNEL_SURVEY_REPORT";
#endif
case BLE_GAP_EVT_ADV_SET_TERMINATED:
return "BLE_GAP_EVT_ADV_SET_TERMINATED";
/* GATTS */
case BLE_GATTS_EVT_WRITE:
return "BLE_GATTS_EVT_WRITE";
case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
return "BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST";
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
return "BLE_GATTS_EVT_SYS_ATTR_MISSING";
case BLE_GATTS_EVT_HVC:
return "BLE_GATTS_EVT_HVC";
case BLE_GATTS_EVT_SC_CONFIRM:
return "BLE_GATTS_EVT_SC_CONFIRM";
case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
return "BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST";
case BLE_GATTS_EVT_TIMEOUT:
return "BLE_GATTS_EVT_TIMEOUT";
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
return "BLE_GATTS_EVT_HVN_TX_COMPLETE";
/* GATTC */
case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
return "BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP";
case BLE_GATTC_EVT_REL_DISC_RSP:
return "BLE_GATTC_EVT_REL_DISC_RSP";
case BLE_GATTC_EVT_CHAR_DISC_RSP:
return "BLE_GATTC_EVT_CHAR_DISC_RSP";
case BLE_GATTC_EVT_DESC_DISC_RSP:
return "BLE_GATTC_EVT_DESC_DISC_RSP";
case BLE_GATTC_EVT_ATTR_INFO_DISC_RSP:
return "BLE_GATTC_EVT_ATTR_INFO_DISC_RSP";
case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP:
return "BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP";
case BLE_GATTC_EVT_READ_RSP:
return "BLE_GATTC_EVT_READ_RSP";
case BLE_GATTC_EVT_CHAR_VALS_READ_RSP:
return "BLE_GATTC_EVT_CHAR_VALS_READ_RSP";
case BLE_GATTC_EVT_WRITE_RSP:
return "BLE_GATTC_EVT_WRITE_RSP";
case BLE_GATTC_EVT_HVX:
return "BLE_GATTC_EVT_HVX";
case BLE_GATTC_EVT_EXCHANGE_MTU_RSP:
return "BLE_GATTC_EVT_EXCHANGE_MTU_RSP";
case BLE_GATTC_EVT_TIMEOUT:
return "BLE_GATTC_EVT_TIMEOUT";
case BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE:
return "BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE";
#endif
default:
err = snprintf(buf, sizeof(buf), "BLE event: %#x", evt);
__ASSERT(err > 0, "Encode error");
__ASSERT(err < sizeof(buf), "Buffer too small");
(void)err;
return buf;
}
}
uint32_t nrf_sdh_ble_sd_ram_usage_get(void)
{
return sd_ram_size;
}
static int default_cfg_set(void)
{
int err;
ble_cfg_t ble_cfg;
const uint32_t app_ram_start = APP_RAM_START;
const uint8_t conn_cfg_tag = CONFIG_NRF_SDH_BLE_CONN_TAG;
/* Save a configuration fo the BLE stack in a given connection tag.
* If any of the calls to sd_ble_cfg_set() fail, log the error but carry on so that
* wrong RAM settings can be caught by nrf_sdh_ble_enable() and a meaningful error
* message will be printed to the user suggesting the correct value.
*/
/* Configure the connection count. */
memset(&ble_cfg, 0x00, sizeof(ble_cfg));
ble_cfg.conn_cfg.conn_cfg_tag = conn_cfg_tag;
ble_cfg.conn_cfg.params.gap_conn_cfg.conn_count = CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT;
ble_cfg.conn_cfg.params.gap_conn_cfg.event_length = CONFIG_NRF_SDH_BLE_GAP_EVENT_LENGTH;
err = sd_ble_cfg_set(BLE_CONN_CFG_GAP, &ble_cfg, app_ram_start);
if (err) {
LOG_WRN("Failed to set BLE_CONN_CFG_GAP, nrf_error %#x", err);
}
/* Configure the connection roles. */
memset(&ble_cfg, 0, sizeof(ble_cfg));
#if CONFIG_SOFTDEVICE_PERIPHERAL
ble_cfg.gap_cfg.role_count_cfg.periph_role_count =
CONFIG_NRF_SDH_BLE_PERIPHERAL_LINK_COUNT;
ble_cfg.gap_cfg.role_count_cfg.adv_set_count = BLE_GAP_ADV_SET_COUNT_DEFAULT;
#endif
#if CONFIG_SOFTDEVICE_CENTRAL
ble_cfg.gap_cfg.role_count_cfg.central_role_count =
CONFIG_NRF_SDH_BLE_CENTRAL_LINK_COUNT;
ble_cfg.gap_cfg.role_count_cfg.central_sec_count =
MIN(CONFIG_NRF_SDH_BLE_CENTRAL_LINK_COUNT,
BLE_GAP_ROLE_COUNT_CENTRAL_SEC_DEFAULT);
#endif
err = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &ble_cfg, app_ram_start);
if (err) {
LOG_WRN("Failed to set BLE_GAP_CFG_ROLE_COUNT, nrf_error %#x", err);
}
/* Configure the maximum ATT MTU. */
#if (CONFIG_NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 23)
memset(&ble_cfg, 0x00, sizeof(ble_cfg));
ble_cfg.conn_cfg.conn_cfg_tag = conn_cfg_tag;
ble_cfg.conn_cfg.params.gatt_conn_cfg.att_mtu = CONFIG_NRF_SDH_BLE_GATT_MAX_MTU_SIZE;
err = sd_ble_cfg_set(BLE_CONN_CFG_GATT, &ble_cfg, app_ram_start);
if (err) {
LOG_WRN("Failed to set BLE_CONN_CFG_GATT, nrf_error %#x", err);
}
#endif /* NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 23 */
/* Configure number of custom UUIDS. */
memset(&ble_cfg, 0, sizeof(ble_cfg));
ble_cfg.common_cfg.vs_uuid_cfg.vs_uuid_count = CONFIG_NRF_SDH_BLE_VS_UUID_COUNT;
err = sd_ble_cfg_set(BLE_COMMON_CFG_VS_UUID, &ble_cfg, app_ram_start);
if (err) {
LOG_WRN("Failed to set BLE_COMMON_CFG_VS_UUID, nrf_error %#x", err);
}
/* Configure the GATTS attribute table. */
memset(&ble_cfg, 0x00, sizeof(ble_cfg));
ble_cfg.gatts_cfg.attr_tab_size.attr_tab_size = CONFIG_NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE;
err = sd_ble_cfg_set(BLE_GATTS_CFG_ATTR_TAB_SIZE, &ble_cfg, app_ram_start);
if (err) {
LOG_WRN("Failed to set BLE_GATTS_CFG_ATTR_TAB_SIZE, nrf_error %#x", err);
}
/* Configure Service Changed characteristic. */
memset(&ble_cfg, 0x00, sizeof(ble_cfg));
ble_cfg.gatts_cfg.service_changed.service_changed =
IS_ENABLED(CONFIG_NRF_SDH_BLE_SERVICE_CHANGED);
err = sd_ble_cfg_set(BLE_GATTS_CFG_SERVICE_CHANGED, &ble_cfg, app_ram_start);
if (err) {
LOG_WRN("Failed to set BLE_GATTS_CFG_SERVICE_CHANGED, nrf_error %#x", err);
}
LOG_DBG("SoftDevice configuration applied");
return 0;
}
int nrf_sdh_ble_enable(uint8_t conn_cfg_tag)
{
int err;
uint32_t app_ram_minimum = APP_RAM_START;
default_cfg_set();
err = sd_ble_enable(&app_ram_minimum);
if (app_ram_minimum > APP_RAM_START) {
LOG_ERR("Insufficient RAM allocated for the SoftDevice! Change application RAM "
"start address from %#x to >= %#x", APP_RAM_START, app_ram_minimum);
} else if ((app_ram_minimum < APP_RAM_START) &&
IS_ENABLED(CONFIG_NRF_SDH_BLE_LOG_SD_RAM_USAGE)) {
LOG_INF("Application RAM start address can be lowered from %#x to %#x",
APP_RAM_START, app_ram_minimum);
}
if (IS_ENABLED(CONFIG_NRF_SDH_BLE_LOG_SD_RAM_USAGE)) {
LOG_INF("SoftDevice RAM location: %#x - %#x, size: %u bytes",
SD_RAM_START, app_ram_minimum, (app_ram_minimum - SD_RAM_START));
}
if (err) {
LOG_ERR("Failed to enable BLE, nrf_error %#x", err);
return err;
}
if (IS_ENABLED(CONFIG_NRF_SDH_LOG_SD_INFO)) {
ble_version_t ll_ver = {0};
(void)sd_ble_version_get(&ll_ver);
LOG_INF("SoftDevice Link Layer version %u.%u", ll_ver.version_number,
ll_ver.subversion_number);
}
LOG_DBG("SoftDevice BLE enabled");
sd_ram_size = app_ram_minimum - SD_RAM_START;
(void)sdh_state_evt_observer_notify(NRF_SDH_STATE_EVT_BLE_ENABLED);
return 0;
}
static uint16_t conn_handles[CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT] = {
[0 ... CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT - 1] = BLE_CONN_HANDLE_INVALID,
};
int nrf_sdh_ble_idx_get(uint16_t conn_handle)
{
/* Code size optimization when supporting only one connection. */
if (CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT == 1) {
ARG_UNUSED(conn_handle);
return 0;
}
for (int idx = 0; idx < ARRAY_SIZE(conn_handles); idx++) {
if (conn_handles[idx] == conn_handle) {
return idx;
}
}
return -1;
}
uint16_t nrf_sdh_ble_conn_handle_get(int idx)
{
if (CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT == 1) {
return conn_handles[0];
}
if (idx >= 0 && idx < ARRAY_SIZE(conn_handles)) {
return conn_handles[idx];
}
return BLE_CONN_HANDLE_INVALID;
}
static void idx_assign(uint16_t conn_handle)
{
if (CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT == 1) {
conn_handles[0] = conn_handle;
return;
}
__ASSERT(conn_handle != BLE_CONN_HANDLE_INVALID, "Got invalid conn_handle from SoftDevice");
for (int idx = 0; idx < ARRAY_SIZE(conn_handles); idx++) {
__ASSERT(conn_handles[idx] != conn_handle,
"conn_handle %#x is already assigned to idx %d", conn_handle, idx);
}
for (int idx = 0; idx < ARRAY_SIZE(conn_handles); idx++) {
if (conn_handles[idx] == BLE_CONN_HANDLE_INVALID) {
conn_handles[idx] = conn_handle;
LOG_DBG("Assigned idx %d to conn_handle %#x", idx, conn_handle);
return;
}
}
__ASSERT(false, "Failed to assign idx to conn_handle %#x", conn_handle);
LOG_ERR("Failed to assign idx to conn_handle %#x", conn_handle);
}
static void idx_unassign(uint16_t conn_handle)
{
if (CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT == 1) {
conn_handles[0] = BLE_CONN_HANDLE_INVALID;
return;
}
for (int idx = 0; idx < ARRAY_SIZE(conn_handles); idx++) {
if (conn_handles[idx] == conn_handle) {
conn_handles[idx] = BLE_CONN_HANDLE_INVALID;
LOG_DBG("Unassigned idx %d from conn_handle %#x", idx, conn_handle);
return;
}
}
__ASSERT(false, "Could not find any idx assigned to conn_handle %#x", conn_handle);
}
static void ble_evt_poll(void *context)
{
int err;
__aligned(4) static uint8_t evt_buffer[NRF_SDH_BLE_EVT_BUF_SIZE];
ble_evt_t * const ble_evt = (ble_evt_t *)evt_buffer;
while (true) {
uint16_t evt_len = (uint16_t)sizeof(evt_buffer);
err = sd_ble_evt_get(evt_buffer, &evt_len);
if (err) {
break;
}
LOG_DBG("%s", nrf_sdh_ble_evt_to_str(ble_evt->header.evt_id));
if (ble_evt->header.evt_id == BLE_GAP_EVT_CONNECTED) {
idx_assign(ble_evt->evt.gap_evt.conn_handle);
}
/* Forward the event to BLE observers. */
TYPE_SECTION_FOREACH(
struct nrf_sdh_ble_evt_observer, nrf_sdh_ble_evt_observers, obs) {
obs->handler(ble_evt, obs->context);
}
if (ble_evt->header.evt_id == BLE_GAP_EVT_DISCONNECTED) {
idx_unassign(ble_evt->evt.gap_evt.conn_handle);
}
}
/* An SoC event may have triggered this round of polling, and BLE may not be enabled */
__ASSERT((err == NRF_ERROR_NOT_FOUND) || (err == BLE_ERROR_NOT_ENABLED),
"Failed to receive SoftDevice BLE event, nrf_error %#x", err);
}
/* Listen to SoftDevice events */
NRF_SDH_STACK_EVT_OBSERVER(ble_evt_obs, ble_evt_poll, NULL, HIGH);