diff options
author | Andrew Leech <andrew@alelec.net> | 2019-09-01 19:08:16 +1000 |
---|---|---|
committer | Jim Mussared <jim.mussared@gmail.com> | 2019-10-01 09:51:02 +1000 |
commit | 5dc592d117b68455e8b1c1ef9230090c143358dc (patch) | |
tree | 780b7f0ea118bacbcd7ad252f85c885a9dc6e2d0 | |
parent | 497dae45e713921bb8b017e92657eaa0dcd6f0fd (diff) |
extmod/modbluetooth_nimble: Use random addr if public isn't available.
-rw-r--r-- | extmod/modbluetooth_nimble.c | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/extmod/modbluetooth_nimble.c b/extmod/modbluetooth_nimble.c index 3a6d1cba4..21d71e3c9 100644 --- a/extmod/modbluetooth_nimble.c +++ b/extmod/modbluetooth_nimble.c @@ -277,7 +277,30 @@ STATIC void reset_cb(int reason) { } STATIC void sync_cb(void) { - ble_hs_util_ensure_addr(0); // prefer public address + int rc; + ble_addr_t addr; + + rc = ble_hs_util_ensure_addr(0); // prefer public address + if (rc != 0) { + // https://mynewt.apache.org/latest/tutorials/ble/eddystone.html#configure-the-nimble-stack-with-an-address + #if MICROPY_PY_BLUETOOTH_RANDOM_ADDR + rc = ble_hs_id_gen_rnd(1, &addr); + assert(rc == 0); + rc = ble_hs_id_set_rnd(addr.val); + assert(rc == 0); + #else + uint8_t addr_be[6]; + mp_hal_get_mac(MP_HAL_MAC_BDADDR, addr_be); + reverse_addr_byte_order(addr.val, addr_be); + // ble_hs_id_set_pub(addr.val); + rc = ble_hs_id_set_rnd(addr.val); + assert(rc == 0); + #endif + + rc = ble_hs_util_ensure_addr(0); // prefer public address + assert(rc == 0); + } + ble_svc_gap_device_name_set(MICROPY_PY_BLUETOOTH_DEFAULT_NAME); ble_state = BLE_STATE_ACTIVE; @@ -461,11 +484,23 @@ int mp_bluetooth_gap_advertise_start(bool connectable, uint16_t interval_ms, con }; ret = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL); - if (ret != 0) { - return ble_hs_err_to_errno(ret); + if (ret == 0) { + return 0; + } + ret = ble_gap_adv_start(BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL); + if (ret == 0) { + return 0; + } + ret = ble_gap_adv_start(BLE_OWN_ADDR_RPA_RANDOM_DEFAULT, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL); + if (ret == 0) { + return 0; + } + ret = ble_gap_adv_start(BLE_OWN_ADDR_RANDOM, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL); + if (ret == 0) { + return 0; } - return 0; + return ble_hs_err_to_errno(ret); } void mp_bluetooth_gap_advertise_stop(void) { |