1
6
7
17
18
24
25
31
32
39
40
41
42
43
44
45
46
47
48
57
58
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
102
103
104
105
106
107
108
109
110
111
112
113
117
118
119
120
121
122
123
124
125
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
196
197
198
202
203
204
205
206
207
208
209
210
211
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
246
247
248
249
250
251
252
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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
312
313
314
315
316
317
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
362
/* ... */
#include "esp_log.h"
#include "nvs_flash.h"
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "host/ble_hs.h"
#include "host/util/util.h"
#include "console/console.h"
#include "services/gap/ble_svc_gap.h"
#include "phy_prph.h"9 includes
static uint8_t ext_adv_pattern_1M[] = {
0x02, 0x01, 0x06,
0x03, 0x03, 0xab, 0xcd,
0x03, 0x03, 0xAB, 0xF2,
0x0e, 0X09, 'b', 'l', 'e', 'p', 'r', 'p', 'h', '-', 'p', 'h', 'y', '-', '1', 'M',
}{...};
static uint8_t ext_adv_pattern_2M[] = {
0x02, 0x01, 0x06,
0x03, 0x03, 0xab, 0xcd,
0x03, 0x03, 0xAB, 0xF2,
0x0e, 0X09, 'b', 'l', 'e', 'p', 'r', 'p', 'h', '-', 'p', 'h', 'y', '-', '2', 'M',
}{...};
static uint8_t ext_adv_pattern_coded[] = {
0x02, 0x01, 0x06,
0x03, 0x03, 0xab, 0xcd,
0x03, 0x03, 0xAB, 0xF2,
0x11, 0X09, 'b', 'l', 'e', 'p', 'r', 'p', 'h', '-', 'p', 'h', 'y', '-', 'c', 'o', 'd', 'e',
'd',
}{...};
static const char *tag = "NimBLE_BLE_PHY_PRPH";
static int bleprph_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t own_addr_type;
static uint8_t s_current_phy;
void ble_store_config_init(void);
void set_default_le_phy(uint8_t tx_phys_mask, uint8_t rx_phys_mask)
{
int rc = ble_gap_set_prefered_default_le_phy(tx_phys_mask, rx_phys_mask);
if (rc == 0) {
MODLOG_DFLT(INFO, "Default LE PHY set successfully");
}{...} else {
MODLOG_DFLT(ERROR, "Failed to set default LE PHY");
}{...}
}{ ... }
/* ... */
static void
bleprph_print_conn_desc(struct ble_gap_conn_desc *desc)
{
MODLOG_DFLT(INFO, "handle=%d our_ota_addr_type=%d our_ota_addr=",
desc->conn_handle, desc->our_ota_addr.type);
print_addr(desc->our_ota_addr.val);
MODLOG_DFLT(INFO, " our_id_addr_type=%d our_id_addr=",
desc->our_id_addr.type);
print_addr(desc->our_id_addr.val);
MODLOG_DFLT(INFO, " peer_ota_addr_type=%d peer_ota_addr=",
desc->peer_ota_addr.type);
print_addr(desc->peer_ota_addr.val);
MODLOG_DFLT(INFO, " peer_id_addr_type=%d peer_id_addr=",
desc->peer_id_addr.type);
print_addr(desc->peer_id_addr.val);
MODLOG_DFLT(INFO, " conn_itvl=%d conn_latency=%d supervision_timeout=%d "
"encrypted=%d authenticated=%d bonded=%d\n",
desc->conn_itvl, desc->conn_latency,
desc->supervision_timeout,
desc->sec_state.encrypted,
desc->sec_state.authenticated,
desc->sec_state.bonded);
}{ ... }
static struct os_mbuf *
ext_get_data(uint8_t ext_adv_pattern[], int size)
{
struct os_mbuf *data;
int rc;
data = os_msys_get_pkthdr(size, 0);
assert(data);
rc = os_mbuf_append(data, ext_adv_pattern, size);
assert(rc == 0);
return data;
}{ ... }
/* ... */
static void
ext_bleprph_advertise(void)
{
struct ble_gap_ext_adv_params params;
struct os_mbuf *data = NULL;
uint8_t instance = 1;
int rc;
memset (¶ms, 0, sizeof(params));
if (s_current_phy == BLE_HCI_LE_PHY_1M_PREF_MASK) {
params.scannable = 1;
params.legacy_pdu = 1;
}{...}
params.connectable = 1;
params.own_addr_type = BLE_OWN_ADDR_PUBLIC;
switch (s_current_phy) {
case BLE_HCI_LE_PHY_1M_PREF_MASK:
params.primary_phy = BLE_HCI_LE_PHY_1M;
params.secondary_phy = BLE_HCI_LE_PHY_1M;
data = ext_get_data(ext_adv_pattern_1M, sizeof(ext_adv_pattern_1M));
params.sid = 0;
break;
...
case BLE_HCI_LE_PHY_2M_PREF_MASK:
params.primary_phy = BLE_HCI_LE_PHY_1M;
params.secondary_phy = BLE_HCI_LE_PHY_2M;
data = ext_get_data(ext_adv_pattern_2M, sizeof(ext_adv_pattern_2M));
params.sid = 1;
break;
...
case BLE_HCI_LE_PHY_CODED_PREF_MASK:
params.primary_phy = BLE_HCI_LE_PHY_CODED;
params.secondary_phy = BLE_HCI_LE_PHY_CODED;
data = ext_get_data(ext_adv_pattern_coded, sizeof(ext_adv_pattern_coded));
params.sid = 2;
break;...
}{...}
params.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
params.itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
rc = ble_gap_ext_adv_configure(instance, ¶ms, NULL,
bleprph_gap_event, NULL);
assert (rc == 0);
rc = ble_gap_ext_adv_set_data(instance, data);
assert (rc == 0);
rc = ble_gap_ext_adv_start(instance, 0, 0);
assert (rc == 0);
}{ ... }
/* ... */
static int
bleprph_gap_event(struct ble_gap_event *event, void *arg)
{
struct ble_gap_conn_desc desc;
int rc;
switch (event->type) {
case BLE_GAP_EVENT_LINK_ESTAB:
MODLOG_DFLT(INFO, "connection %s; status=%d ",
event->link_estab.status == 0 ? "established" : "failed",
event->link_estab.status);
if (event->link_estab.status == 0) {
rc = ble_gap_conn_find(event->link_estab.conn_handle, &desc);
assert(rc == 0);
bleprph_print_conn_desc(&desc);
}{...}
MODLOG_DFLT(INFO, "\n");
if (event->link_estab.status != 0) {
ext_bleprph_advertise();
}{...}
return 0;
...
case BLE_GAP_EVENT_DISCONNECT:
MODLOG_DFLT(INFO, "disconnect; reason=%d ", event->disconnect.reason);
bleprph_print_conn_desc(&event->disconnect.conn);
MODLOG_DFLT(INFO, "\n");
switch (s_current_phy) {
case BLE_HCI_LE_PHY_1M_PREF_MASK:
s_current_phy = BLE_HCI_LE_PHY_2M_PREF_MASK;
break;
...
case BLE_HCI_LE_PHY_2M_PREF_MASK:
s_current_phy = BLE_HCI_LE_PHY_CODED_PREF_MASK;
break;
...
case BLE_HCI_LE_PHY_CODED_PREF_MASK:
return 0;
...
default:
return 0;...
}{...}
ext_bleprph_advertise();
return 0;
...
case BLE_GAP_EVENT_CONN_UPDATE:
MODLOG_DFLT(INFO, "connection updated; status=%d ",
event->conn_update.status);
rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc);
assert(rc == 0);
bleprph_print_conn_desc(&desc);
MODLOG_DFLT(INFO, "\n");
return 0;
...
case BLE_GAP_EVENT_ADV_COMPLETE:
MODLOG_DFLT(INFO, "advertise complete; reason=%d",
event->adv_complete.reason);
return 0;...
}{...}
return 0;
}{ ... }
static void
bleprph_on_reset(int reason)
{
MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
}{ ... }
static void
bleprph_on_sync(void)
{
int rc;
uint8_t all_phy;
rc = ble_hs_util_ensure_addr(0);
assert(rc == 0);
rc = ble_hs_id_infer_auto(0, &own_addr_type);
if (rc != 0) {
MODLOG_DFLT(ERROR, "error determining address type; rc=%d\n", rc);
return;
}{...}
uint8_t addr_val[6] = {0};
rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
MODLOG_DFLT(INFO, "Device Address: ");
print_addr(addr_val);
MODLOG_DFLT(INFO, "\n");
s_current_phy = BLE_HCI_LE_PHY_1M_PREF_MASK;
all_phy = BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK | BLE_HCI_LE_PHY_CODED_PREF_MASK;
set_default_le_phy(all_phy, all_phy);
ext_bleprph_advertise();
}{ ... }
void bleprph_host_task(void *param)
{
ESP_LOGI(tag, "BLE Host Task Started");
nimble_port_run();
nimble_port_freertos_deinit();
}{ ... }
void
app_main(void)
{
int rc;
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}{...}
ESP_ERROR_CHECK(ret);
ret = nimble_port_init();
if (ret != ESP_OK) {
MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", ret);
return;
}{...}
ble_hs_cfg.reset_cb = bleprph_on_reset;
ble_hs_cfg.sync_cb = bleprph_on_sync;
ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb;
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
ble_hs_cfg.sm_io_cap = CONFIG_EXAMPLE_IO_TYPE;
#ifdef CONFIG_EXAMPLE_BONDING
ble_hs_cfg.sm_bonding = 1;
#endif
#ifdef CONFIG_EXAMPLE_MITM
ble_hs_cfg.sm_mitm = 1;
#endif
#ifdef CONFIG_EXAMPLE_USE_SC
ble_hs_cfg.sm_sc = 1;
#else
ble_hs_cfg.sm_sc = 0;
#endif
#ifdef CONFIG_EXAMPLE_BONDING
ble_hs_cfg.sm_our_key_dist = 1;
ble_hs_cfg.sm_their_key_dist = 1;/* ... */
#endif
rc = gatt_svr_init_le_phy();
assert(rc == 0);
rc = ble_svc_gap_device_name_set("bleprph-phy");
assert(rc == 0);
ble_store_config_init();
nimble_port_freertos_init(bleprph_host_task);
rc = scli_init();
if (rc != ESP_OK) {
ESP_LOGE(tag, "scli_init() failed");
}{...}
}{ ... }