From 52a2ce45de353267776aaa25856c9d144653f142 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 17 Aug 2020 10:53:00 +1000 Subject: [PATCH] extmod/modbluetooth: Allow using mp_hal_get_mac as a static address. Generally a controller should either have its own public address hardcoded, or loaded by the driver (e.g. cywbt43). However, for a controller that has no public address where you still want a long-term stable address, this allows you to use a static address generated by the port. Typically on STM32 this will be an LAA, but a board might override this. --- extmod/btstack/modbluetooth_btstack.c | 17 ++++++++++++++++- extmod/nimble/modbluetooth_nimble.c | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index b028a5d1ac..773234b9b2 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -508,10 +508,12 @@ STATIC void btstack_init_deinit_timeout_handler(btstack_timer_source_t *ds) { mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_TIMEOUT; } +#if !MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS STATIC void btstack_static_address_ready(void *arg) { DEBUG_printf("btstack_static_address_ready.\n"); *(volatile bool *)arg = true; } +#endif STATIC bool set_public_address(void) { bd_addr_t local_addr; @@ -534,14 +536,27 @@ STATIC void set_random_address(void) { } else #endif // MICROPY_BLUETOOTH_BTSTACK_ZEPHYR_STATIC_ADDRESS { + bd_addr_t static_addr; + + #if MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS + + DEBUG_printf("set_random_address: Generating static address using mp_hal_get_mac\n"); + mp_hal_get_mac(MP_HAL_MAC_BDADDR, static_addr); + // Mark it as STATIC (not RPA or NRPA). + static_addr[0] |= 0xc0; + + #else + DEBUG_printf("set_random_address: Generating random static address.\n"); btstack_crypto_random_t sm_crypto_random_request; - bd_addr_t static_addr; volatile bool ready = false; btstack_crypto_random_generate(&sm_crypto_random_request, static_addr, 6, &btstack_static_address_ready, (void *)&ready); while (!ready) { MICROPY_EVENT_POLL_HOOK } + + #endif // MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS + DEBUG_printf("set_random_address: Address generated.\n"); gap_random_address_set(static_addr); } diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 1c782943ab..4222f58fa8 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -157,10 +157,22 @@ STATIC bool has_public_address(void) { STATIC void set_random_address(bool nrpa) { int rc; (void)rc; - DEBUG_printf("sync_cb: Generating random static address\n"); ble_addr_t addr; - rc = ble_hs_id_gen_rnd(nrpa ? 1 : 0, &addr); - assert(rc == 0); + #if MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS + if (!nrpa) { + DEBUG_printf("set_random_address: Generating static address using mp_hal_get_mac\n"); + uint8_t hal_mac_addr[6]; + mp_hal_get_mac(MP_HAL_MAC_BDADDR, hal_mac_addr); + addr = create_nimble_addr(BLE_ADDR_RANDOM, hal_mac_addr); + // Mark it as STATIC (not RPA or NRPA). + addr.val[5] |= 0xc0; + } else + #endif + { + DEBUG_printf("set_random_address: Generating random static address\n"); + rc = ble_hs_id_gen_rnd(nrpa ? 1 : 0, &addr); + assert(rc == 0); + } rc = ble_hs_id_set_rnd(addr.val); assert(rc == 0); rc = ble_hs_util_ensure_addr(1);