From 3460b48a6d3af423e8592add99432172523639ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20de=20Giessen?= Date: Fri, 8 Dec 2023 18:57:07 +0100 Subject: [PATCH 1/3] extmod/nimble: Override configuration options set in nimble_port_init. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This moves the runtime initialisation of `ble_hs_cfg` to happen after `nimble_port_init()`. That is consistent with the order used in NimBLE examples. On the ESP32 port this is needed because the ESP-IDF sets up the default RAM secret store callbacks in its implementation of `nimble_port_init()` (specifically, it calls `esp_nimble_init()` which in turn calls `ble_store_ram_init()`). We want to override those with our own callbacks to implement the `IRQ_[GS]ET_SECRET` events in Python. Signed-off-by: Daniël van de Giessen --- extmod/nimble/modbluetooth_nimble.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index c053ac56cf2ba..b4610564cd33c 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -592,17 +592,6 @@ int mp_bluetooth_init(void) { mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_STARTING; - ble_hs_cfg.reset_cb = reset_cb; - ble_hs_cfg.sync_cb = sync_cb; - ble_hs_cfg.gatts_register_cb = gatts_register_cb; - ble_hs_cfg.store_status_cb = ble_store_util_status_rr; - - #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING - ble_hs_cfg.store_read_cb = ble_secret_store_read; - ble_hs_cfg.store_write_cb = ble_secret_store_write; - ble_hs_cfg.store_delete_cb = ble_secret_store_delete; - #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING - MP_STATE_PORT(bluetooth_nimble_root_pointers) = m_new0(mp_bluetooth_nimble_root_pointers_t, 1); mp_bluetooth_gatts_db_create(&MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db); @@ -622,6 +611,17 @@ int mp_bluetooth_init(void) { DEBUG_printf("mp_bluetooth_init: nimble_port_init\n"); nimble_port_init(); + ble_hs_cfg.reset_cb = reset_cb; + ble_hs_cfg.sync_cb = sync_cb; + ble_hs_cfg.gatts_register_cb = gatts_register_cb; + ble_hs_cfg.store_status_cb = ble_store_util_status_rr; + + #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + ble_hs_cfg.store_read_cb = ble_secret_store_read; + ble_hs_cfg.store_write_cb = ble_secret_store_write; + ble_hs_cfg.store_delete_cb = ble_secret_store_delete; + #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + // Make sure that the HCI UART and event handling task is running. mp_bluetooth_nimble_port_start(); From d1e15b928107e2459b65c7a2484517e9e2a830cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20de=20Giessen?= Date: Tue, 12 Dec 2023 15:56:13 +0100 Subject: [PATCH 2/3] extmod/nimble: Enable key distribution to support bonding. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This sets the BLE key distribution parameters at runtime. This isn't needed in most ports since we already set the default values in `extmod/nimble/syscfg/syscfg.h`; however in the ESP32 port that headerfile is not used, and the default values in the ESP-IDF don't enable key distribution nor can we change those defaults via `sdkconfig`. Thus we're setting these values explicitly at runtime. Signed-off-by: Daniël van de Giessen --- extmod/nimble/modbluetooth_nimble.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index b4610564cd33c..5bab43a0ccea0 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -617,6 +617,8 @@ int mp_bluetooth_init(void) { ble_hs_cfg.store_status_cb = ble_store_util_status_rr; #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING + ble_hs_cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID | BLE_SM_PAIR_KEY_DIST_SIGN; + ble_hs_cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID | BLE_SM_PAIR_KEY_DIST_SIGN; ble_hs_cfg.store_read_cb = ble_secret_store_read; ble_hs_cfg.store_write_cb = ble_secret_store_write; ble_hs_cfg.store_delete_cb = ble_secret_store_delete; From 678707c8b07323c5b914778708a2858387c3b60c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20de=20Giessen?= Date: Fri, 22 Dec 2023 16:30:04 +0100 Subject: [PATCH 3/3] docs/library/bluetooth: Add note that ESP32 supports pairing/bonding. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pairing and bonding was fixed for the ESP32 in the two previous commits. Signed-off-by: Daniël van de Giessen --- docs/library/bluetooth.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/library/bluetooth.rst b/docs/library/bluetooth.rst index fb3c23400e51d..f8b154dd11448 100644 --- a/docs/library/bluetooth.rst +++ b/docs/library/bluetooth.rst @@ -722,7 +722,7 @@ Pairing and bonding and ``_IRQ_SET_SECRET`` events. **Note:** This is currently only supported when using the NimBLE stack on - STM32 and Unix (not ESP32). + ESP32, STM32 and Unix. .. method:: BLE.gap_pair(conn_handle, /)