From 536c119404dacb8a739b96bda2bddc9bc8a12bf6 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 6 Aug 2026 09:49:08 -0700 Subject: [PATCH 1/2] Add pairing and bonding to Zephyr _bleio Board flash partitioning is changing so back up CIRCUITPY. It may change again when we introduce an MCUBoot bootloader for everything. This updates the nrf bsim models too. --- locale/circuitpython.pot | 30 +- ports/zephyr-cp/Kconfig | 15 + .../boards/adafruit_feather_rp2040.overlay | 8 +- .../boards/frdm_mcxn947_mcxn947_cpu0.overlay | 18 + ports/zephyr-cp/boards/frdm_rw612.overlay | 9 +- .../boards/frdm_rw612_rw612_cpu0.overlay | 9 +- .../mimxrt1170_evk_mimxrt1176_cm7.overlay | 9 +- .../nrf54lm20bsim/autogen_board_info.toml | 6 + ports/zephyr-cp/boards/native_sim.overlay | 8 +- .../boards/nrf5340bsim_nrf5340_cpuapp.overlay | 8 +- .../nrf54lm20bsim_nrf54lm20a_cpuapp.overlay | 9 +- .../boards/rpi_pico2_rp2350a_m33.overlay | 8 +- .../boards/rpi_pico2_rp2350a_m33_w.overlay | 8 +- .../zephyr-cp/boards/rpi_pico_rp2040.overlay | 8 +- .../boards/rpi_pico_rp2040_w.overlay | 8 +- ...h750b_dk_stm32h750xx_ext_flash_app.overlay | 10 +- ports/zephyr-cp/boards/stm32h7b3i_dk.overlay | 16 + ports/zephyr-cp/common-hal/_bleio/Adapter.c | 83 +++- .../zephyr-cp/common-hal/_bleio/Connection.c | 108 +++++- .../zephyr-cp/common-hal/_bleio/Connection.h | 9 + ports/zephyr-cp/common-hal/_bleio/__init__.c | 2 + ports/zephyr-cp/debug.conf | 21 ++ ports/zephyr-cp/prj.conf | 14 + .../tests/bsim/test_bsim_ble_advertising.py | 4 +- .../tests/bsim/test_bsim_ble_pairing.py | 353 ++++++++++++++++++ ports/zephyr-cp/zephyr-config/west.yml | 4 +- 26 files changed, 744 insertions(+), 41 deletions(-) create mode 100644 ports/zephyr-cp/tests/bsim/test_bsim_ble_pairing.py diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 38d498312ad..69b9a62bd78 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -1682,7 +1682,8 @@ msgstr "" msgid "Only IPv4 addresses supported" msgstr "" -#: ports/mimxrt10xx/common-hal/busio/SPI.c shared-bindings/busio/SPI.c +#: ports/mimxrt10xx/common-hal/busio/SPI.c +#: ports/raspberrypi/bindings/wiznet/PIO_SPI.c shared-bindings/busio/SPI.c msgid "Must provide MISO or MOSI pin" msgstr "" @@ -1738,12 +1739,14 @@ msgid "Unknown gatt error: 0x%04x" msgstr "" #: ports/nordic/common-hal/_bleio/__init__.c +#: ports/zephyr-cp/common-hal/_bleio/Connection.c msgid "" "Unspecified issue. Can be that the pairing prompt on the other device was " "declined or ignored." msgstr "" #: ports/nordic/common-hal/_bleio/__init__.c +#: ports/zephyr-cp/common-hal/_bleio/Connection.c #, c-format msgid "Unknown security error: 0x%04x" msgstr "" @@ -1844,6 +1847,17 @@ msgstr "" msgid "In-buffer elements must be <= 4 bytes long" msgstr "" +#: ports/raspberrypi/bindings/wiznet/PIO_SPI.c shared-bindings/bitbangio/I2C.c +#: shared-bindings/bitbangio/SPI.c shared-bindings/busio/I2C.c +#: shared-bindings/busio/SPI.c +msgid "Function requires lock" +msgstr "" + +#: ports/raspberrypi/bindings/wiznet/PIO_SPI.c shared-bindings/bitbangio/SPI.c +#: shared-bindings/busio/SPI.c +msgid "buffer slices must be of equal length" +msgstr "" + #: ports/raspberrypi/common-hal/alarm/touch/TouchAlarm.c #: ports/stm/common-hal/alarm/touch/TouchAlarm.c msgid "Touch alarms not available" @@ -3732,6 +3746,7 @@ msgid "file must be a file opened in byte mode" msgstr "" #: shared-bindings/audiodelays/Chorus.c shared-bindings/audiodelays/Echo.c +#: shared-bindings/audiodelays/Flanger.c #: shared-bindings/audiodelays/GranularPitchShift.c #: shared-bindings/audiodelays/MultiTapDelay.c #: shared-bindings/audiodelays/PitchShift.c @@ -3749,10 +3764,6 @@ msgstr "" msgid "bits_per_sample must be 16" msgstr "" -#: shared-bindings/audioi2sin/I2SIn.c -msgid "%q requires %q" -msgstr "" - #: shared-bindings/audioi2sin/I2SIn.c #, c-format msgid "invalid destination buffer, must be an array of type: %c" @@ -3762,15 +3773,6 @@ msgstr "" msgid "%q and %q must be different" msgstr "" -#: shared-bindings/bitbangio/I2C.c shared-bindings/bitbangio/SPI.c -#: shared-bindings/busio/I2C.c shared-bindings/busio/SPI.c -msgid "Function requires lock" -msgstr "" - -#: shared-bindings/bitbangio/SPI.c shared-bindings/busio/SPI.c -msgid "buffer slices must be of equal length" -msgstr "" - #: shared-bindings/bitmapfilter/__init__.c msgid "" "weights must be a sequence with an odd square number of elements (usually 9 " diff --git a/ports/zephyr-cp/Kconfig b/ports/zephyr-cp/Kconfig index 498d2d92abf..015864100ec 100644 --- a/ports/zephyr-cp/Kconfig +++ b/ports/zephyr-cp/Kconfig @@ -87,3 +87,18 @@ config BT_GATT_DYNAMIC_DB config BT_GATT_CLIENT default y + +config BT_SMP + default y + +config BT_BONDABLE + default y + +config SETTINGS + default y + +config BT_SETTINGS + default y + +config BT_KEYS_OVERWRITE_OLDEST + default y diff --git a/ports/zephyr-cp/boards/adafruit_feather_rp2040.overlay b/ports/zephyr-cp/boards/adafruit_feather_rp2040.overlay index 3cc026389f4..a19e2a066db 100644 --- a/ports/zephyr-cp/boards/adafruit_feather_rp2040.overlay +++ b/ports/zephyr-cp/boards/adafruit_feather_rp2040.overlay @@ -23,7 +23,13 @@ nvm_partition: partition@180000 { compatible = "zephyr,mapped-partition"; label = "nvm"; - reg = <0x180000 0x1000>; + reg = <0x180000 0x800>; + }; + + storage_partition: partition@180800 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x180800 0x800>; }; circuitpy_partition: partition@181000 { diff --git a/ports/zephyr-cp/boards/frdm_mcxn947_mcxn947_cpu0.overlay b/ports/zephyr-cp/boards/frdm_mcxn947_mcxn947_cpu0.overlay index 83242201c5b..f1a07471477 100644 --- a/ports/zephyr-cp/boards/frdm_mcxn947_mcxn947_cpu0.overlay +++ b/ports/zephyr-cp/boards/frdm_mcxn947_mcxn947_cpu0.overlay @@ -1,5 +1,23 @@ &w25q64jvssiq { /delete-node/ partitions; + + partitions { + #address-cells = <1>; + #size-cells = <1>; + ranges; + + storage_partition: partition@0 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x0 DT_SIZE_K(32)>; + }; + + circuitpy_partition: partition@8000 { + compatible = "zephyr,mapped-partition"; + label = "circuitpy"; + reg = <0x00008000 (DT_SIZE_M(8) - DT_SIZE_K(32))>; + }; + }; }; #include "../app.overlay" diff --git a/ports/zephyr-cp/boards/frdm_rw612.overlay b/ports/zephyr-cp/boards/frdm_rw612.overlay index 741d0702e03..e926394817e 100644 --- a/ports/zephyr-cp/boards/frdm_rw612.overlay +++ b/ports/zephyr-cp/boards/frdm_rw612.overlay @@ -1,10 +1,15 @@ &w25q512jvfiq { partitions { /delete-node/ partition@620000; - circuitpy_partition: partition@620000 { + storage_partition: partition@620000 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x00620000 DT_SIZE_K(32)>; + }; + circuitpy_partition: partition@628000 { compatible = "zephyr,mapped-partition"; label = "circuitpy"; - reg = <0x00620000 (DT_SIZE_M(58) - DT_SIZE_K(128))>; + reg = <0x00628000 (DT_SIZE_M(58) - DT_SIZE_K(128) - DT_SIZE_K(32))>; }; }; }; diff --git a/ports/zephyr-cp/boards/frdm_rw612_rw612_cpu0.overlay b/ports/zephyr-cp/boards/frdm_rw612_rw612_cpu0.overlay index 5a4d840c446..e57b5b6d331 100644 --- a/ports/zephyr-cp/boards/frdm_rw612_rw612_cpu0.overlay +++ b/ports/zephyr-cp/boards/frdm_rw612_rw612_cpu0.overlay @@ -1,10 +1,15 @@ &w25q512jvfiq { partitions { /delete-node/ storage_partition; - circuitpy_partition: partition@620000 { + storage_partition: partition@620000 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x00620000 DT_SIZE_K(32)>; + }; + circuitpy_partition: partition@628000 { compatible = "zephyr,mapped-partition"; label = "circuitpy"; - reg = <0x00620000 (DT_SIZE_M(58) - DT_SIZE_K(128))>; + reg = <0x00628000 (DT_SIZE_M(58) - DT_SIZE_K(128) - DT_SIZE_K(32))>; }; } diff --git a/ports/zephyr-cp/boards/mimxrt1170_evk_mimxrt1176_cm7.overlay b/ports/zephyr-cp/boards/mimxrt1170_evk_mimxrt1176_cm7.overlay index 6a0ed82182e..b8f707dac27 100644 --- a/ports/zephyr-cp/boards/mimxrt1170_evk_mimxrt1176_cm7.overlay +++ b/ports/zephyr-cp/boards/mimxrt1170_evk_mimxrt1176_cm7.overlay @@ -1,10 +1,15 @@ &is25wp128 { partitions { /delete-node/ partition@e20000; - circuitpy_partition: partition@e20000 { + storage_partition: partition@e20000 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x00e20000 DT_SIZE_K(32)>; + }; + circuitpy_partition: partition@e28000 { compatible = "zephyr,mapped-partition"; label = "circuitpy"; - reg = <0x00e20000 (DT_SIZE_M(2) - DT_SIZE_K(128))>; + reg = <0x00e28000 (DT_SIZE_M(2) - DT_SIZE_K(128) - DT_SIZE_K(32))>; }; }; }; diff --git a/ports/zephyr-cp/boards/native/nrf54lm20bsim/autogen_board_info.toml b/ports/zephyr-cp/boards/native/nrf54lm20bsim/autogen_board_info.toml index 6a3ea5b39d5..6a470b5906b 100644 --- a/ports/zephyr-cp/boards/native/nrf54lm20bsim/autogen_board_info.toml +++ b/ports/zephyr-cp/boards/native/nrf54lm20bsim/autogen_board_info.toml @@ -121,3 +121,9 @@ wifi = false zephyr_display = false zephyr_kernel = false zlib = true +# extmod modules shared with MicroPython +asyncio = true +binascii = true +json = true +re = true +select = true diff --git a/ports/zephyr-cp/boards/native_sim.overlay b/ports/zephyr-cp/boards/native_sim.overlay index 18dfa816107..f94f71726a8 100644 --- a/ports/zephyr-cp/boards/native_sim.overlay +++ b/ports/zephyr-cp/boards/native_sim.overlay @@ -31,7 +31,13 @@ circuitpy_partition: partition@0 { compatible = "zephyr,mapped-partition"; label = "circuitpy"; - reg = <0x00000000 DT_SIZE_K(2040)>; + reg = <0x00000000 DT_SIZE_K(2032)>; + }; + + storage_partition: partition@1fc000 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x001fc000 DT_SIZE_K(8)>; }; nvm_partition: partition@1fe000 { diff --git a/ports/zephyr-cp/boards/nrf5340bsim_nrf5340_cpuapp.overlay b/ports/zephyr-cp/boards/nrf5340bsim_nrf5340_cpuapp.overlay index 8f575d30475..5f0c9fd7af8 100644 --- a/ports/zephyr-cp/boards/nrf5340bsim_nrf5340_cpuapp.overlay +++ b/ports/zephyr-cp/boards/nrf5340bsim_nrf5340_cpuapp.overlay @@ -20,7 +20,13 @@ circuitpy_partition: partition@0 { compatible = "zephyr,mapped-partition"; label = "circuitpy"; - reg = <0x00000000 DT_SIZE_K(1024)>; + reg = <0x00000000 DT_SIZE_K(1008)>; + }; + + storage_partition: partition@fc000 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x000fc000 DT_SIZE_K(16)>; }; }; }; diff --git a/ports/zephyr-cp/boards/nrf54lm20bsim_nrf54lm20a_cpuapp.overlay b/ports/zephyr-cp/boards/nrf54lm20bsim_nrf54lm20a_cpuapp.overlay index fb74a6313f3..ed5831ec5a3 100644 --- a/ports/zephyr-cp/boards/nrf54lm20bsim_nrf54lm20a_cpuapp.overlay +++ b/ports/zephyr-cp/boards/nrf54lm20bsim_nrf54lm20a_cpuapp.overlay @@ -21,8 +21,15 @@ #size-cells = <1>; circuitpy_partition: partition@0 { + compatible = "zephyr,mapped-partition"; label = "circuitpy"; - reg = <0x00000000 DT_SIZE_K(512)>; + reg = <0x00000000 (DT_SIZE_K(2036) - DT_SIZE_K(32))>; + }; + + storage_partition: partition@1f5000 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x001f5000 DT_SIZE_K(32)>; }; }; }; diff --git a/ports/zephyr-cp/boards/rpi_pico2_rp2350a_m33.overlay b/ports/zephyr-cp/boards/rpi_pico2_rp2350a_m33.overlay index a3c2a9911c0..ca0400b6645 100644 --- a/ports/zephyr-cp/boards/rpi_pico2_rp2350a_m33.overlay +++ b/ports/zephyr-cp/boards/rpi_pico2_rp2350a_m33.overlay @@ -13,7 +13,13 @@ nvm_partition: partition@180000 { compatible = "zephyr,mapped-partition"; label = "nvm"; - reg = <0x180000 0x1000>; + reg = <0x180000 0x800>; + }; + + storage_partition: partition@180800 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x180800 0x800>; }; circuitpy_partition: partition@181000 { diff --git a/ports/zephyr-cp/boards/rpi_pico2_rp2350a_m33_w.overlay b/ports/zephyr-cp/boards/rpi_pico2_rp2350a_m33_w.overlay index a3c2a9911c0..ca0400b6645 100644 --- a/ports/zephyr-cp/boards/rpi_pico2_rp2350a_m33_w.overlay +++ b/ports/zephyr-cp/boards/rpi_pico2_rp2350a_m33_w.overlay @@ -13,7 +13,13 @@ nvm_partition: partition@180000 { compatible = "zephyr,mapped-partition"; label = "nvm"; - reg = <0x180000 0x1000>; + reg = <0x180000 0x800>; + }; + + storage_partition: partition@180800 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x180800 0x800>; }; circuitpy_partition: partition@181000 { diff --git a/ports/zephyr-cp/boards/rpi_pico_rp2040.overlay b/ports/zephyr-cp/boards/rpi_pico_rp2040.overlay index d3ff27ddc74..1cdd4ca2033 100644 --- a/ports/zephyr-cp/boards/rpi_pico_rp2040.overlay +++ b/ports/zephyr-cp/boards/rpi_pico_rp2040.overlay @@ -23,7 +23,13 @@ nvm_partition: partition@180000 { compatible = "zephyr,mapped-partition"; label = "nvm"; - reg = <0x180000 0x1000>; + reg = <0x180000 0x800>; + }; + + storage_partition: partition@180800 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x180800 0x800>; }; circuitpy_partition: partition@181000 { diff --git a/ports/zephyr-cp/boards/rpi_pico_rp2040_w.overlay b/ports/zephyr-cp/boards/rpi_pico_rp2040_w.overlay index d3ff27ddc74..1cdd4ca2033 100644 --- a/ports/zephyr-cp/boards/rpi_pico_rp2040_w.overlay +++ b/ports/zephyr-cp/boards/rpi_pico_rp2040_w.overlay @@ -23,7 +23,13 @@ nvm_partition: partition@180000 { compatible = "zephyr,mapped-partition"; label = "nvm"; - reg = <0x180000 0x1000>; + reg = <0x180000 0x800>; + }; + + storage_partition: partition@180800 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x180800 0x800>; }; circuitpy_partition: partition@181000 { diff --git a/ports/zephyr-cp/boards/stm32h750b_dk_stm32h750xx_ext_flash_app.overlay b/ports/zephyr-cp/boards/stm32h750b_dk_stm32h750xx_ext_flash_app.overlay index c1d22c6261e..7e038bb77aa 100644 --- a/ports/zephyr-cp/boards/stm32h750b_dk_stm32h750xx_ext_flash_app.overlay +++ b/ports/zephyr-cp/boards/stm32h750b_dk_stm32h750xx_ext_flash_app.overlay @@ -2,10 +2,16 @@ partitions { /delete-node/ partition@7800000; - circuitpy_partition: partition@7800000 { + storage_partition: partition@7800000 { + compatible = "zephyr,mapped-partition"; + label = "storage"; + reg = <0x7800000 DT_SIZE_K(32)>; + }; + + circuitpy_partition: partition@7808000 { compatible = "zephyr,mapped-partition"; label = "circuitpy"; - reg = <0x7800000 DT_SIZE_M(8)>; + reg = <0x7808000 (DT_SIZE_M(8) - DT_SIZE_K(32))>; }; }; }; diff --git a/ports/zephyr-cp/boards/stm32h7b3i_dk.overlay b/ports/zephyr-cp/boards/stm32h7b3i_dk.overlay index c2b5f3129c4..d871e728e0d 100644 --- a/ports/zephyr-cp/boards/stm32h7b3i_dk.overlay +++ b/ports/zephyr-cp/boards/stm32h7b3i_dk.overlay @@ -1,5 +1,21 @@ &mx25lm51245 { /delete-node/ partitions; + + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + storage_partition: partition@0 { + label = "storage"; + reg = <0x00000000 DT_SIZE_K(32)>; + }; + + circuitpy_partition: partition@8000 { + label = "circuitpy"; + reg = <0x00008000 (DT_SIZE_M(64) - DT_SIZE_K(32))>; + }; + }; }; &sram5 { diff --git a/ports/zephyr-cp/common-hal/_bleio/Adapter.c b/ports/zephyr-cp/common-hal/_bleio/Adapter.c index 59813f9ad89..ac2b1948eb7 100644 --- a/ports/zephyr-cp/common-hal/_bleio/Adapter.c +++ b/ports/zephyr-cp/common-hal/_bleio/Adapter.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "py/gc.h" #include "py/runtime.h" @@ -122,6 +123,8 @@ static void bleio_connection_clear(bleio_connection_internal_t *self) { } self->connection_obj = mp_const_none; + self->pair_status = PAIR_NOT_PAIRED; + self->sec_err = 0; } static void bleio_connection_release(bleio_connection_internal_t *connection, uint8_t reason) { @@ -159,13 +162,31 @@ static void bleio_connected_cb(struct bt_conn *conn, uint8_t err) { } static void bleio_disconnected_cb(struct bt_conn *conn, uint8_t reason) { - printk("disconnected %p\n", conn); bleio_connection_release(bleio_connection_find_by_conn(conn), reason); } +static void bleio_security_changed_cb(struct bt_conn *conn, bt_security_t level, + enum bt_security_err err) { + bleio_connection_internal_t *connection = bleio_connection_find_by_conn(conn); + if (connection == NULL) { + return; + } + + if (err == BT_SECURITY_ERR_SUCCESS && level > BT_SECURITY_L1) { + // Security was established (encryption enabled). + // This happens both on first-time pairing and when reconnecting + // with stored bond keys. + connection->pair_status = PAIR_PAIRED; + } else if (err != BT_SECURITY_ERR_SUCCESS) { + connection->pair_status = PAIR_NOT_PAIRED; + connection->sec_err = (uint8_t)err; + } +} + BT_CONN_CB_DEFINE(bleio_connection_callbacks) = { .connected = bleio_connected_cb, .disconnected = bleio_disconnected_cb, + .security_changed = bleio_security_changed_cb, }; static void scan_recv_cb(const struct bt_le_scan_recv_info *info, struct net_buf_simple *buf) { @@ -257,9 +278,15 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable } if (!bt_is_ready()) { int err = bt_enable(NULL); - if (err != 0) { + if (err != 0 && err != -EALREADY) { raise_zephyr_error(err); } + + // bt_init() returns early without setting BT_DEV_READY when + // CONFIG_BT_SETTINGS=y and no identity is loaded yet. + // Load settings so the BT settings handler fires and calls + // bt_finalize_init() which sets BT_DEV_READY. + settings_load(); } ble_adapter_enabled = true; return; @@ -621,11 +648,61 @@ mp_obj_t common_hal_bleio_adapter_connect(bleio_adapter_obj_t *self, bleio_addre return bleio_connection_new_from_internal(connection); } +struct bond_collect_ctx { + bt_addr_le_t *addrs; + size_t *count; + size_t max; +}; + +static void bond_iterator_collect(const struct bt_bond_info *info, void *user_data) { + struct bond_collect_ctx *ctx = (struct bond_collect_ctx *)user_data; + if (*ctx->count < ctx->max) { + bt_addr_le_copy(&ctx->addrs[*ctx->count], &info->addr); + (*ctx->count)++; + } +} + +static void bond_iterator_check(const struct bt_bond_info *info, void *user_data) { + (void)info; + bool *has_bonds = (bool *)user_data; + *has_bonds = true; +} + void common_hal_bleio_adapter_erase_bonding(bleio_adapter_obj_t *self) { - mp_raise_NotImplementedError(NULL); + // Unpair all bonded devices for all local identities. + for (uint8_t id = 0; id < CONFIG_BT_ID_MAX; id++) { + // bt_unpair takes an addr; use bt_foreach_bond to iterate and unpair. + // We need to collect addresses first since we can't unpair during iteration. + bt_addr_le_t addrs[CONFIG_BT_MAX_PAIRED]; + size_t addr_count = 0; + + bt_foreach_bond(id, bond_iterator_collect, &(struct bond_collect_ctx) { + .addrs = addrs, + .count = &addr_count, + .max = CONFIG_BT_MAX_PAIRED + }); + + for (size_t i = 0; i < addr_count; i++) { + bt_unpair(id, &addrs[i]); + } + } + + // Reset pairing state on all connections + for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) { + bleio_connections[i].pair_status = PAIR_NOT_PAIRED; + bleio_connections[i].sec_err = 0; + } } bool common_hal_bleio_adapter_is_bonded_to_central(bleio_adapter_obj_t *self) { + // Check if any bond exists for identity 0 + for (uint8_t id = 0; id < CONFIG_BT_ID_MAX; id++) { + bool has_bonds = false; + bt_foreach_bond(id, bond_iterator_check, &has_bonds); + if (has_bonds) { + return true; + } + } return false; } diff --git a/ports/zephyr-cp/common-hal/_bleio/Connection.c b/ports/zephyr-cp/common-hal/_bleio/Connection.c index ecc5f23a525..336e90e7674 100644 --- a/ports/zephyr-cp/common-hal/_bleio/Connection.c +++ b/ports/zephyr-cp/common-hal/_bleio/Connection.c @@ -14,6 +14,7 @@ #include #include "py/runtime.h" +#include "shared/runtime/interrupt_char.h" #include "bindings/zephyr_kernel/__init__.h" #include "shared-bindings/_bleio/__init__.h" #include "shared-bindings/_bleio/Characteristic.h" @@ -24,7 +25,6 @@ #include "common-hal/_bleio/__init__.h" #include "common-hal/_bleio/Characteristic.h" #include "supervisor/port_heap.h" -#include "supervisor/shared/tick.h" // Discovery context passed through Zephyr callbacks. typedef struct { @@ -385,8 +385,107 @@ static void discover_characteristics_for_service(struct bt_conn *conn, } } +// ===== PAIRING / BONDING ===== + +// Auth info callbacks to track pairing completion/failure +static struct bt_conn_auth_info_cb auth_info_cb; +static bool auth_info_cb_registered = false; + +static void on_pairing_complete(struct bt_conn *conn, bool bonded) { + for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) { + if (bleio_connections[i].conn == conn) { + bleio_connections[i].pair_status = PAIR_PAIRED; + bleio_connections[i].sec_err = 0; + break; + } + } +} + +static void on_pairing_failed(struct bt_conn *conn, enum bt_security_err reason) { + for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) { + if (bleio_connections[i].conn == conn) { + bleio_connections[i].pair_status = PAIR_NOT_PAIRED; + bleio_connections[i].sec_err = (uint8_t)reason; + break; + } + } +} + +void bleio_connection_register_auth_callbacks(void) { + if (!auth_info_cb_registered) { + auth_info_cb.pairing_complete = on_pairing_complete; + auth_info_cb.pairing_failed = on_pairing_failed; + bt_conn_auth_info_cb_register(&auth_info_cb); + auth_info_cb_registered = true; + } +} + void common_hal_bleio_connection_pair(bleio_connection_internal_t *self, bool bond) { - mp_raise_NotImplementedError(NULL); + (void)bond; // Bonding is enabled globally via CONFIG_BT_BONDABLE=y + + if (self == NULL || self->conn == NULL) { + mp_raise_bleio_BluetoothError(MP_ERROR_TEXT("Not connected")); + } + + // Already paired? + if (self->pair_status == PAIR_PAIRED) { + return; + } + + // Ensure auth callbacks are registered + bleio_connection_register_auth_callbacks(); + + self->pair_status = PAIR_WAITING; + self->sec_err = 0; + + int err = bt_conn_set_security(self->conn, BT_SECURITY_L2); + if (err != 0) { + self->pair_status = PAIR_NOT_PAIRED; + raise_zephyr_error(err); + } + + // bt_conn_set_security may return 0 immediately if already encrypted + // (e.g., reconnection with stored bond keys). Check current security level. + if (bt_conn_get_security(self->conn) > BT_SECURITY_L1) { + self->pair_status = PAIR_PAIRED; + return; + } + + // Wait for pairing to complete. Zephyr's SMP reports each attempt via + // pairing_failed before auto-restarting security (smp.c), so a stale-bond + // retry shows up as an intermediate BT_SECURITY_ERR_PIN_OR_KEY_MISSING + // followed by a fresh attempt that can succeed. Ride through those and + // rely on Zephyr to signal the end: success (PAIR_PAIRED), SMP_TIMEOUT + // dropping the link (observed as conn == NULL), or a user interrupt. + while (self->pair_status != PAIR_PAIRED + && self->conn != NULL + && !mp_hal_is_interrupted()) { + RUN_BACKGROUND_TASKS; + } + + if (mp_hal_is_interrupted()) { + if (self->conn != NULL) { + bt_conn_auth_cancel(self->conn); + } + self->pair_status = PAIR_NOT_PAIRED; + return; + } + + if (self->pair_status == PAIR_PAIRED) { + return; + } + + // Reuse the messages shared with the nordic _bleio implementation + // (check_sec_status). A dropped link clears sec_err, so it falls through + // to the "unspecified issue" message alongside the explicit UNSPECIFIED + // error code rather than introducing a zephyr-specific "Pairing failed" + // string. + uint8_t sec_err_code = self->sec_err; + if (sec_err_code == BT_SECURITY_ERR_UNSPECIFIED || sec_err_code == 0) { + mp_raise_bleio_SecurityError(MP_ERROR_TEXT("Unspecified issue. Can be that the pairing prompt on the other device was declined or ignored.")); + } else { + mp_raise_bleio_SecurityError(MP_ERROR_TEXT("Unknown security error: 0x%04x"), sec_err_code); + } } void common_hal_bleio_connection_disconnect(bleio_connection_internal_t *self) { @@ -434,7 +533,10 @@ mp_int_t common_hal_bleio_connection_get_max_packet_length(bleio_connection_inte } bool common_hal_bleio_connection_get_paired(bleio_connection_obj_t *self) { - return false; + if (self == NULL || self->connection == NULL) { + return false; + } + return self->connection->pair_status == PAIR_PAIRED; } mp_obj_tuple_t *common_hal_bleio_connection_discover_remote_services(bleio_connection_obj_t *self, mp_obj_t service_uuids_whitelist) { diff --git a/ports/zephyr-cp/common-hal/_bleio/Connection.h b/ports/zephyr-cp/common-hal/_bleio/Connection.h index dc14125db5f..323e9393103 100644 --- a/ports/zephyr-cp/common-hal/_bleio/Connection.h +++ b/ports/zephyr-cp/common-hal/_bleio/Connection.h @@ -13,9 +13,17 @@ struct bt_conn; +typedef enum { + PAIR_NOT_PAIRED, + PAIR_WAITING, + PAIR_PAIRED, +} pair_status_t; + typedef struct { struct bt_conn *conn; mp_obj_t connection_obj; + volatile pair_status_t pair_status; + uint8_t sec_err; // Security error code from pairing attempt } bleio_connection_internal_t; typedef struct { @@ -25,3 +33,4 @@ typedef struct { } bleio_connection_obj_t; mp_obj_t bleio_connection_new_from_internal(bleio_connection_internal_t *connection); +void bleio_connection_register_auth_callbacks(void); diff --git a/ports/zephyr-cp/common-hal/_bleio/__init__.c b/ports/zephyr-cp/common-hal/_bleio/__init__.c index 2dfe5af0060..e3c208a7d1d 100644 --- a/ports/zephyr-cp/common-hal/_bleio/__init__.c +++ b/ports/zephyr-cp/common-hal/_bleio/__init__.c @@ -11,6 +11,7 @@ #include "common-hal/_bleio/Adapter.h" #include "common-hal/_bleio/__init__.h" #include "bindings/zephyr_kernel/__init__.h" +#include "common-hal/_bleio/Connection.h" #include "supervisor/shared/bluetooth/bluetooth.h" #include "supervisor/shared/tick.h" @@ -20,6 +21,7 @@ bleio_adapter_obj_t common_hal_bleio_adapter_obj; void common_hal_bleio_init(void) { common_hal_bleio_adapter_obj.base.type = &bleio_adapter_type; bleio_adapter_reset(&common_hal_bleio_adapter_obj); + bleio_connection_register_auth_callbacks(); } void bleio_user_reset(void) { diff --git a/ports/zephyr-cp/debug.conf b/ports/zephyr-cp/debug.conf index 9d1e3d5c0cd..625ef45198f 100644 --- a/ports/zephyr-cp/debug.conf +++ b/ports/zephyr-cp/debug.conf @@ -13,6 +13,16 @@ CONFIG_FRAME_POINTER=y CONFIG_FLASH_LOG_LEVEL_DBG=y CONFIG_LOG_MODE_IMMEDIATE=y +# Bluetooth: enable BT host debug logging so success paths print, not just +# LOG_ERR. In particular bt_keys logs "Stored keys for " (LOG_DBG) on a +# successful bt_keys_store(); without this only the failure message +# "Failed to save keys (err -116)" is visible. Also enable the adjacent +# modules (SMP drives pairing, BT_SETTINGS is the NVS backend that returns +# the store err) so the -EALREADY (-116) store-failure context is complete. +CONFIG_BT_KEYS_LOG_LEVEL_DBG=y +CONFIG_BT_SETTINGS_LOG_LEVEL_DBG=y +CONFIG_BT_SMP_LOG_LEVEL_DBG=y + # Bump stacks for debug build - verbose logging consumes more stack. CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096 CONFIG_MAIN_STACK_SIZE=32768 @@ -21,3 +31,14 @@ CONFIG_UDC_DWC2_STACK_SIZE=4096 CONFIG_USBD_THREAD_STACK_SIZE=4096 CONFIG_USBD_MSC_STACK_SIZE=4096 CONFIG_IDLE_STACK_SIZE=1024 + +# Bluetooth stacks: debug logging + assertions + LOG_MODE_IMMEDIATE consume +# substantial extra stack in the BT host RX workqueue (runs all event callbacks, +# including SMP/pairing) and the long workqueue (deferred SMP/pairing/crypto + +# NVS bond-key store). Zephyr's own SMP/pairing test app +# (tests/bluetooth/tester/prj.conf) uses 4096 for BT_RX_STACK_SIZE. +# (BT_CTLR_RX_PRIO_STACK_SIZE is bumped in the Zephyr controller Kconfig +# default since it's a hidden symbol. BT_HCI_TX/BT_CTLR_RX are also hidden and +# haven't overflowed — left at their defaults.) +CONFIG_BT_RX_STACK_SIZE=4096 +CONFIG_BT_LONG_WQ_STACK_SIZE=4096 diff --git a/ports/zephyr-cp/prj.conf b/ports/zephyr-cp/prj.conf index a54df43e82f..31f6623aaaf 100644 --- a/ports/zephyr-cp/prj.conf +++ b/ports/zephyr-cp/prj.conf @@ -9,6 +9,11 @@ CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_FLASH_MAP_LABELS=y CONFIG_MAIN_STACK_SIZE=24288 +# The system workqueue is used by BT settings delayed store (NVS flash writes +# during bond key save). RRAM flash drivers on nRF54L need more than the +# default 1024 bytes to avoid stack overflow. +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=3072 + CONFIG_THREAD_STACK_INFO=y CONFIG_ARCH_POSIX_UPDATE_STACK_INFO=y CONFIG_STACK_SENTINEL=n @@ -50,7 +55,16 @@ CONFIG_MBEDTLS=y # Override Kconfig default not taking effect CONFIG_BT_BUF_ACL_RX_SIZE=255 +CONFIG_BT_RX_STACK_SIZE=2048 +CONFIG_BT_LONG_WQ_STACK_SIZE=3072 CONFIG_MBEDTLS_BUILTIN=y CONFIG_PSA_CRYPTO=y CONFIG_PSA_WANT_ALG_SHA_1=y CONFIG_PSA_WANT_ALG_SHA_256=y + +# Settings storage backend for BT bond keys +# SECTOR_COUNT is set high — the init code caps it to what fits in the storage +# partition: min(sector_count, partition_size / sector_size) +CONFIG_NVS=y +CONFIG_SETTINGS_NVS=y +CONFIG_SETTINGS_NVS_SECTOR_COUNT=256 diff --git a/ports/zephyr-cp/tests/bsim/test_bsim_ble_advertising.py b/ports/zephyr-cp/tests/bsim/test_bsim_ble_advertising.py index a3b00b0b055..40b74ee854a 100644 --- a/ports/zephyr-cp/tests/bsim/test_bsim_ble_advertising.py +++ b/ports/zephyr-cp/tests/bsim/test_bsim_ble_advertising.py @@ -142,9 +142,7 @@ def test_bsim_tx_power_default_rssi(board, bsim_phy, circuitpython, zephyr_sampl # Observer: "Device found: (RSSI ), type , AD data len " # Advertisement is 12 bytes: flags (3) + name (9). # With 40 dB channel attenuation and 0 dBm TX → RSSI ~ -39. - # nRF54l bsim model has a TXPOWER register mapping discrepancy that - # reads 0 dBm as 2 dBm, giving RSSI ~ -37 instead. - expected_rssi = -37 if "nrf54" in board else -39 + expected_rssi = -39 rssi_pattern = re.compile(r"RSSI (-?\d+)\), type \d+, AD data len 12") all_rssi = [int(m.group(1)) for m in rssi_pattern.finditer(obs_output)] logger.info("RSSI values: %s", all_rssi) diff --git a/ports/zephyr-cp/tests/bsim/test_bsim_ble_pairing.py b/ports/zephyr-cp/tests/bsim/test_bsim_ble_pairing.py new file mode 100644 index 00000000000..75f6875ee2e --- /dev/null +++ b/ports/zephyr-cp/tests/bsim/test_bsim_ble_pairing.py @@ -0,0 +1,353 @@ +# SPDX-FileCopyrightText: 2025 Scott Shawcroft for Adafruit Industries +# SPDX-License-Identifier: MIT + +"""BLE pairing tests for nrf5340bsim.""" + +import pytest + +# nrf54lm20bsim LE encryption is not yet functional in bsim. Enable it when it +# does. Real hardware works. +pytestmark = pytest.mark.circuitpython_board("native_nrf5340bsim") + +BSIM_PERIPHERAL_CODE = """\ +import _bleio +import time + +adapter = _bleio.adapter + +name = b"PAIRPERIPH" +advertisement = bytes((2, 0x01, 0x06, len(name) + 1, 0x09)) + name + +print("peripheral start") +adapter.start_advertising(advertisement, connectable=True) +print("peripheral advertising", adapter.advertising) + +# Wait for connection +timeout = time.monotonic() + 12.0 +while not adapter.connected and time.monotonic() < timeout: + time.sleep(0.01) + +if not adapter.connected: + print("peripheral connect timed out") + raise SystemExit(1) + +print("peripheral connected", adapter.connected) + +# Wait for pairing to complete (central initiates it) +# The peripheral's SMP module responds automatically. +timeout = time.monotonic() + 10.0 +paired = False +while time.monotonic() < timeout: + if adapter.connected and len(adapter.connections) > 0: + conn = adapter.connections[0] + if conn.paired: + paired = True + break + time.sleep(0.1) + +print("peripheral paired", paired) + +# Wait for disconnect +timeout = time.monotonic() + 10.0 +while adapter.connected and time.monotonic() < timeout: + time.sleep(0.1) + +print("peripheral disconnected", adapter.connected, len(adapter.connections)) +""" + +BSIM_CENTRAL_CODE = """\ +import _bleio +import time + +adapter = _bleio.adapter + +print("central start") +target = None +for entry in adapter.start_scan(timeout=8.0, active=True): + if entry.connectable and b"PAIRPERIPH" in entry.advertisement_bytes: + target = entry.address + print("central found target") + break +adapter.stop_scan() +print("central have target", target is not None) + +if target is None: + raise RuntimeError("No connectable target found") + +connection = adapter.connect(target, timeout=5.0) +print("central connected", connection.connected, adapter.connected, len(adapter.connections)) + +# Pair with the peripheral (Just Works pairing) +connection.pair() +print("central paired", connection.paired) + +# Disconnect cleanly +connection.disconnect() + +timeout = time.monotonic() + 4.0 +while (connection.connected or adapter.connected) and time.monotonic() < timeout: + time.sleep(0.1) + +print("central disconnected", connection.connected, adapter.connected, len(adapter.connections)) +""" + + +@pytest.mark.duration(22) +@pytest.mark.circuitpy_drive({"code.py": BSIM_PERIPHERAL_CODE}) +@pytest.mark.circuitpy_drive({"code.py": BSIM_CENTRAL_CODE}) +def test_bsim_pairing_cp_to_cp(bsim_phy, circuitpython1, circuitpython2): + """Two CP instances: device 0 peripheral, device 1 central pairs to it.""" + peripheral = circuitpython1 + central = circuitpython2 + + central.wait_until_done() + peripheral.wait_until_done() + + periph_output = peripheral.serial.all_output + central_output = central.serial.all_output + + # Peripheral assertions + assert "peripheral start" in periph_output + assert "peripheral advertising True" in periph_output + assert "peripheral connected True" in periph_output + assert "peripheral paired True" in periph_output + assert "peripheral disconnected False 0" in periph_output + + # Central assertions + assert "central start" in central_output + assert "central found target" in central_output + assert "central have target True" in central_output + assert "central connected True True 1" in central_output + assert "central paired True" in central_output + assert "central disconnected False False 0" in central_output + + +BSIM_CENTRAL_PAIRED_PROPERTY_CODE = """\ +import _bleio +import time + +adapter = _bleio.adapter + +print("central start") +target = None +for entry in adapter.start_scan(timeout=8.0, active=True): + if entry.connectable and b"PAIRPERIPH" in entry.advertisement_bytes: + target = entry.address + print("central found target") + break +adapter.stop_scan() + +if target is None: + raise RuntimeError("No connectable target found") + +connection = adapter.connect(target, timeout=5.0) +print("central connected", connection.connected) + +# Check paired is False before pairing +print("central paired before", connection.paired) + +connection.pair() +print("central paired after", connection.paired) + +connection.disconnect() + +timeout = time.monotonic() + 4.0 +while (connection.connected or adapter.connected) and time.monotonic() < timeout: + time.sleep(0.1) + +print("central disconnected", connection.connected) +""" + + +@pytest.mark.duration(22) +@pytest.mark.circuitpy_drive({"code.py": BSIM_PERIPHERAL_CODE}) +@pytest.mark.circuitpy_drive({"code.py": BSIM_CENTRAL_PAIRED_PROPERTY_CODE}) +def test_bsim_pairing_paired_property(bsim_phy, circuitpython1, circuitpython2): + """Verify connection.paired transitions from False to True after pairing.""" + peripheral = circuitpython1 + central = circuitpython2 + + central.wait_until_done() + peripheral.wait_until_done() + + periph_output = peripheral.serial.all_output + central_output = central.serial.all_output + + assert "peripheral paired True" in periph_output + + assert "central paired before False" in central_output + assert "central paired after True" in central_output + assert "central disconnected False" in central_output + + +BSIM_PERIPHERAL_BOND_CODE = """\ +import _bleio +import time + +adapter = _bleio.adapter + +name = b"BONDPERIPH" +advertisement = bytes((2, 0x01, 0x06, len(name) + 1, 0x09)) + name + +print("peripheral start") + +# First connection - pair +adapter.start_advertising(advertisement, connectable=True) +print("peripheral advertising", adapter.advertising) + +timeout = time.monotonic() + 12.0 +while not adapter.connected and time.monotonic() < timeout: + time.sleep(0.01) + +if not adapter.connected: + print("peripheral connect1 timed out") + raise SystemExit(1) + +print("peripheral connected1", adapter.connected) + +# Wait for pairing +timeout = time.monotonic() + 10.0 +paired = False +while time.monotonic() < timeout: + if adapter.connected and len(adapter.connections) > 0: + conn = adapter.connections[0] + if conn.paired: + paired = True + break + time.sleep(0.1) + +print("peripheral paired1", paired) + +# Wait for disconnect +timeout = time.monotonic() + 10.0 +while adapter.connected and time.monotonic() < timeout: + time.sleep(0.1) + +print("peripheral disconnected1", adapter.connected, len(adapter.connections)) + +# Second connection - bond should persist through storage. +# Calling pair() uses the stored bond keys without re-pairing. +time.sleep(0.5) +adapter.start_advertising(advertisement, connectable=True) +print("peripheral advertising2", adapter.advertising) + +timeout = time.monotonic() + 12.0 +while not adapter.connected and time.monotonic() < timeout: + time.sleep(0.01) + +if not adapter.connected: + print("peripheral connect2 timed out") + raise SystemExit(1) + +print("peripheral connected2", adapter.connected) + +# Request security to restore bond encryption +conn = adapter.connections[0] +conn.pair() +print("peripheral paired2", conn.paired) + +# Wait for disconnect +timeout = time.monotonic() + 10.0 +while adapter.connected and time.monotonic() < timeout: + time.sleep(0.1) + +print("peripheral disconnected2", adapter.connected) +""" + +BSIM_CENTRAL_BOND_CODE = """\ +import _bleio +import time + +adapter = _bleio.adapter + +print("central start") +target_addr = None +for entry in adapter.start_scan(timeout=8.0, active=True): + if entry.connectable and b"BONDPERIPH" in entry.advertisement_bytes: + target_addr = entry.address + print("central found target") + break +adapter.stop_scan() +print("central have target", target_addr is not None) + +if target_addr is None: + raise RuntimeError("No connectable target found") + +# First connection - pair +connection = adapter.connect(target_addr, timeout=5.0) +print("central connected1", connection.connected, adapter.connected, len(adapter.connections)) + +print("central paired before1", connection.paired) +connection.pair() +print("central paired after1", connection.paired) + +connection.disconnect() + +timeout = time.monotonic() + 4.0 +while (connection.connected or adapter.connected) and time.monotonic() < timeout: + time.sleep(0.1) + +print("central disconnected1", connection.connected, adapter.connected) + +# Second connection - bond should be restored from storage. +# Calling pair() uses the stored bond keys without re-pairing. +time.sleep(1.0) +connection2 = adapter.connect(target_addr, timeout=5.0) +print("central connected2", connection2.connected, adapter.connected, len(adapter.connections)) + +connection2.pair() +print("central paired bond", connection2.paired) + +connection2.disconnect() + +timeout = time.monotonic() + 4.0 +while (connection2.connected or adapter.connected) and time.monotonic() < timeout: + time.sleep(0.1) + +print("central disconnected2", connection2.connected, adapter.connected) +""" + + +@pytest.mark.duration(35) +@pytest.mark.circuitpy_drive({"code.py": BSIM_PERIPHERAL_BOND_CODE}) +@pytest.mark.circuitpy_drive({"code.py": BSIM_CENTRAL_BOND_CODE}) +def test_bsim_bonding_persistence(bsim_phy, circuitpython1, circuitpython2): + """Verify bonds are saved to storage and survive disconnect/reconnect. + + Central pairs with peripheral, disconnects, then reconnects. + On the second connection the bond should be restored from storage + and connection.paired should be True without calling pair() again. + """ + peripheral = circuitpython1 + central = circuitpython2 + + central.wait_until_done() + peripheral.wait_until_done() + + periph_output = peripheral.serial.all_output + central_output = central.serial.all_output + + # Peripheral assertions - first connection + assert "peripheral start" in periph_output + assert "peripheral advertising True" in periph_output + assert "peripheral connected1 True" in periph_output + assert "peripheral paired1 True" in periph_output + assert "peripheral disconnected1 False 0" in periph_output + # Peripheral - second connection (private advertising, bond restored from storage) + assert "peripheral advertising2 True" in periph_output + assert "peripheral connected2 True" in periph_output + assert "peripheral paired2 True" in periph_output + assert "peripheral disconnected2 False" in periph_output + + # Central assertions - first connection + assert "central start" in central_output + assert "central found target" in central_output + assert "central have target True" in central_output + assert "central connected1 True True 1" in central_output + assert "central paired before1 False" in central_output + assert "central paired after1 True" in central_output + assert "central disconnected1 False False" in central_output + # Central - second connection (bond restored from storage) + assert "central connected2 True True 1" in central_output + assert "central paired bond True" in central_output + assert "central disconnected2 False False" in central_output diff --git a/ports/zephyr-cp/zephyr-config/west.yml b/ports/zephyr-cp/zephyr-config/west.yml index 241ca53aceb..cff4be3993c 100644 --- a/ports/zephyr-cp/zephyr-config/west.yml +++ b/ports/zephyr-cp/zephyr-config/west.yml @@ -4,10 +4,10 @@ manifest: projects: - name: nrf_hw_models url: https://github.com/tannewt/ext_nRF_hw_models - revision: c2927847bdf2ee0af9c5459f4155c67f39f6837a + revision: 41cb69cd08848cc31e91ed62d01d397511981919 path: modules/bsim_hw_models/nrf_hw_models - name: zephyr url: https://github.com/adafruit/zephyr - revision: 63f054c88ee158a3d755bd59a26ef874f650efa2 + revision: 3c402d80bd86628065f2a7d2af4a55a42556b08e clone-depth: 100 import: true From fe448736810b84e3ff1ebd836d4ee2b5ebd480d4 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 14 Aug 2026 15:58:07 -0700 Subject: [PATCH 2/2] Update error message --- devices/ble_hci/common-hal/_bleio/__init__.c | 2 +- locale/circuitpython.pot | 2 +- ports/nordic/common-hal/_bleio/__init__.c | 2 +- ports/zephyr-cp/common-hal/_bleio/Connection.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/devices/ble_hci/common-hal/_bleio/__init__.c b/devices/ble_hci/common-hal/_bleio/__init__.c index f9fdbc50f64..bfe39888753 100644 --- a/devices/ble_hci/common-hal/_bleio/__init__.c +++ b/devices/ble_hci/common-hal/_bleio/__init__.c @@ -30,7 +30,7 @@ bool vm_used_ble; // switch (sec_status) { // case BLE_GAP_SEC_STATUS_UNSPECIFIED: -// mp_raise_bleio_SecurityError(MP_ERROR_TEXT("Unspecified issue. Can be that the pairing prompt on the other device was declined or ignored.")); +// mp_raise_bleio_SecurityError(MP_ERROR_TEXT("Unspecified issue. The pairing request on the other device may have been declined or ignored.")); // return; // default: // mp_raise_bleio_SecurityError(MP_ERROR_TEXT("Unknown security error: 0x%04x"), sec_status); diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 69b9a62bd78..04e901af82d 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -1741,7 +1741,7 @@ msgstr "" #: ports/nordic/common-hal/_bleio/__init__.c #: ports/zephyr-cp/common-hal/_bleio/Connection.c msgid "" -"Unspecified issue. Can be that the pairing prompt on the other device was " +"Unspecified issue. The pairing request on the other device may have been " "declined or ignored." msgstr "" diff --git a/ports/nordic/common-hal/_bleio/__init__.c b/ports/nordic/common-hal/_bleio/__init__.c index 9dc58d7687c..3d76b93ff15 100644 --- a/ports/nordic/common-hal/_bleio/__init__.c +++ b/ports/nordic/common-hal/_bleio/__init__.c @@ -70,7 +70,7 @@ void check_sec_status(uint8_t sec_status) { switch (sec_status) { case BLE_GAP_SEC_STATUS_UNSPECIFIED: - mp_raise_bleio_SecurityError(MP_ERROR_TEXT("Unspecified issue. Can be that the pairing prompt on the other device was declined or ignored.")); + mp_raise_bleio_SecurityError(MP_ERROR_TEXT("Unspecified issue. The pairing request on the other device may have been declined or ignored.")); return; default: mp_raise_bleio_SecurityError(MP_ERROR_TEXT("Unknown security error: 0x%04x"), sec_status); diff --git a/ports/zephyr-cp/common-hal/_bleio/Connection.c b/ports/zephyr-cp/common-hal/_bleio/Connection.c index 336e90e7674..8381fe28a24 100644 --- a/ports/zephyr-cp/common-hal/_bleio/Connection.c +++ b/ports/zephyr-cp/common-hal/_bleio/Connection.c @@ -482,7 +482,7 @@ void common_hal_bleio_connection_pair(bleio_connection_internal_t *self, bool bo // string. uint8_t sec_err_code = self->sec_err; if (sec_err_code == BT_SECURITY_ERR_UNSPECIFIED || sec_err_code == 0) { - mp_raise_bleio_SecurityError(MP_ERROR_TEXT("Unspecified issue. Can be that the pairing prompt on the other device was declined or ignored.")); + mp_raise_bleio_SecurityError(MP_ERROR_TEXT("Unspecified issue. The pairing request on the other device may have been declined or ignored.")); } else { mp_raise_bleio_SecurityError(MP_ERROR_TEXT("Unknown security error: 0x%04x"), sec_err_code); }