From da026039ba6d59b560a2cba712d646c6fa5b8d04 Mon Sep 17 00:00:00 2001 From: Rahul Tank Date: Wed, 3 Sep 2025 17:25:44 +0530 Subject: [PATCH] feat(nimble): Add support for static passkey --- components/bt/host/nimble/Kconfig.in | 7 ++++++- components/bt/host/nimble/nimble | 2 +- components/bt/host/nimble/port/include/esp_nimble_cfg.h | 8 ++++++++ examples/bluetooth/nimble/blecent/README.md | 8 ++++++++ examples/bluetooth/nimble/blecent/main/main.c | 4 ++++ examples/bluetooth/nimble/bleprph/README.md | 8 ++++++++ examples/bluetooth/nimble/bleprph/main/main.c | 4 ++++ 7 files changed, 39 insertions(+), 2 deletions(-) diff --git a/components/bt/host/nimble/Kconfig.in b/components/bt/host/nimble/Kconfig.in index 37cdaeccfe..e05126678f 100644 --- a/components/bt/host/nimble/Kconfig.in +++ b/components/bt/host/nimble/Kconfig.in @@ -1250,7 +1250,12 @@ menu "Extra Features" help Enable connection subrate change feature - + config BT_NIMBLE_STATIC_PASSKEY + bool "Enable support for Static Passkey" + default y + depends on BT_NIMBLE_ENABLED && BT_NIMBLE_SECURITY_ENABLE + help + Enable support for user defined static passkey for SMP endmenu menu "NimBLE Mesh" diff --git a/components/bt/host/nimble/nimble b/components/bt/host/nimble/nimble index 686728c09e..445bdeb281 160000 --- a/components/bt/host/nimble/nimble +++ b/components/bt/host/nimble/nimble @@ -1 +1 @@ -Subproject commit 686728c09ec94a86afeeb58a936a9f6a4ab5fd83 +Subproject commit 445bdeb281ca49d324232c9f53bc30b7a4784ef9 diff --git a/components/bt/host/nimble/port/include/esp_nimble_cfg.h b/components/bt/host/nimble/port/include/esp_nimble_cfg.h index 445c0e2957..4fad474a43 100644 --- a/components/bt/host/nimble/port/include/esp_nimble_cfg.h +++ b/components/bt/host/nimble/port/include/esp_nimble_cfg.h @@ -2230,4 +2230,12 @@ #endif #endif +#ifndef MYNEWT_VAL_STATIC_PASSKEY +#ifdef CONFIG_BT_NIMBLE_STATIC_PASSKEY +#define MYNEWT_VAL_STATIC_PASSKEY CONFIG_BT_NIMBLE_STATIC_PASSKEY +#else +#define CONFIG_BT_NIMBLE_STATIC_PASSKEY (0) +#endif +#endif + #endif diff --git a/examples/bluetooth/nimble/blecent/README.md b/examples/bluetooth/nimble/blecent/README.md index 28ab5061ff..f0efe0eb95 100644 --- a/examples/bluetooth/nimble/blecent/README.md +++ b/examples/bluetooth/nimble/blecent/README.md @@ -61,6 +61,14 @@ idf.py menuconfig In the `Example Configuration` menu: * Change the `Peer Address` option if needed. +* Optional: enable static passkey support via `Component config -> Bluetooth -> NimBLE -> Enable support for Static Passkey`. + +Static passkey mode is useful for demos where you want to avoid interactive passkey entry. +When enabled, the example calls `ble_sm_configure_static_passkey(456789, true)` and NimBLE +automatically injects the passkey during pairing. Update the passkey in +`examples/bluetooth/nimble/blecent/main/main.c` if you want a different value. +Both devices must use the same 6-digit passkey, and you should only use a fixed +passkey for development or controlled environments. ### Build and Flash diff --git a/examples/bluetooth/nimble/blecent/main/main.c b/examples/bluetooth/nimble/blecent/main/main.c index b73ebe974f..652cbe05fe 100644 --- a/examples/bluetooth/nimble/blecent/main/main.c +++ b/examples/bluetooth/nimble/blecent/main/main.c @@ -1098,6 +1098,10 @@ app_main(void) ble_hs_cfg.store_status_cb = ble_store_util_status_rr; #if NIMBLE_BLE_CONNECT +#if MYNEWT_VAL(STATIC_PASSKEY) + ble_sm_configure_static_passkey(456789, true); +#endif + int rc; /* Initialize data structures to track connected peers. */ #if MYNEWT_VAL(BLE_INCL_SVC_DISCOVERY) || MYNEWT_VAL(BLE_GATT_CACHING_INCLUDE_SERVICES) diff --git a/examples/bluetooth/nimble/bleprph/README.md b/examples/bluetooth/nimble/bleprph/README.md index 390cbbc8d5..b1b64f47f9 100644 --- a/examples/bluetooth/nimble/bleprph/README.md +++ b/examples/bluetooth/nimble/bleprph/README.md @@ -41,6 +41,14 @@ In the `Example Configuration` menu: * Select I/O capabilities of device from `Example Configuration --> I/O Capability`, default is `Just_works`. * Enable/Disable other security related parameters `Bonding, MITM option, secure connection(SM SC)`. +* Optional: enable static passkey support via `Component config -> Bluetooth -> NimBLE -> Enable support for Static Passkey`. + +Static passkey mode is useful for demos where you want to avoid interactive passkey entry. +When enabled, the example calls `ble_sm_configure_static_passkey(456789, true)` and NimBLE +automatically injects the passkey during pairing. Update the passkey in +`examples/bluetooth/nimble/bleprph/main/main.c` if you want a different value. +Both devices must use the same 6-digit passkey, and you should only use a fixed +passkey for development or controlled environments. ### Build and Flash diff --git a/examples/bluetooth/nimble/bleprph/main/main.c b/examples/bluetooth/nimble/bleprph/main/main.c index d0eb370709..8b705fbd68 100644 --- a/examples/bluetooth/nimble/bleprph/main/main.c +++ b/examples/bluetooth/nimble/bleprph/main/main.c @@ -599,6 +599,10 @@ app_main(void) ble_hs_cfg.sm_their_key_dist |= BLE_SM_PAIR_KEY_DIST_ID; #endif +#if MYNEWT_VAL(STATIC_PASSKEY) && NIMBLE_BLE_CONNECT + ble_sm_configure_static_passkey(456789, true); +#endif + #if MYNEWT_VAL(BLE_GATTS) rc = gatt_svr_init(); assert(rc == 0);