From a93171b9bf884b14179abe2e963ee9421b3fec4b Mon Sep 17 00:00:00 2001 From: rbaron Date: Thu, 1 Dec 2022 19:48:36 +0100 Subject: [PATCH] Adds support for user-supplied BT address Also cleans up BLE configs --- .../prstlib/include/prstlib/macros.h | 2 ++ code/nrf-connect/samples/ble/Kconfig | 32 +++++++------------ code/nrf-connect/samples/ble/prj.conf | 1 - code/nrf-connect/samples/ble/src/ble.c | 13 ++++++++ 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/code/nrf-connect/prstlib/include/prstlib/macros.h b/code/nrf-connect/prstlib/include/prstlib/macros.h index e8e5764..dc3f82f 100644 --- a/code/nrf-connect/prstlib/include/prstlib/macros.h +++ b/code/nrf-connect/prstlib/include/prstlib/macros.h @@ -19,4 +19,6 @@ // Checks that expr evaluates to true, otherwise return 1. #define RET_CHECK(expr, msg) RET_IF_ERR_MSG(!(expr), msg) +#define UNUSED_OK(expr) (void)expr; + #endif // _PRST_MACROS_H_ \ No newline at end of file diff --git a/code/nrf-connect/samples/ble/Kconfig b/code/nrf-connect/samples/ble/Kconfig index 6bac203..6c505c1 100644 --- a/code/nrf-connect/samples/ble/Kconfig +++ b/code/nrf-connect/samples/ble/Kconfig @@ -1,34 +1,16 @@ source "Kconfig.zephyr" -choice PRST_NETWORK - prompt "b-parasite Network" - default PRST_NETWORK_BLE - -config PRST_NETWORK_NONE - bool "Network disabled" - -config PRST_NETWORK_BLE - bool "Uses BLE as the network" - depends on BT - -endchoice # PRST_NETWORK - config PRST_SLEEP_DURATION_SEC int "Sleep duration in seconds" default 600 -### -### BLE configs -### - config PRST_BLE_ADV_DURATION_SEC int "Advertising duration in seconds" - default 600 + default 1 choice PRST_BLE_ENCODING prompt "b-parasite BLE encoding" - depends on PRST_NETWORK_BLE default PRST_BLE_ENCODING_BPARASITE_V2 config PRST_BLE_ENCODING_BPARASITE_V2 @@ -45,7 +27,17 @@ endchoice # PRST_BLE_ENCODING config PRST_BLE_ENCODING_SERVICE_DATA_LEN int help - Size of the service data buffer + Size of the service data buffer. default 20 if PRST_BLE_ENCODING_BPARASITE_V2 default 18 if PRST_BLE_ENCODING_BTHOME_V1 default 19 if PRST_BLE_ENCODING_BTHOME_V2 + + +config PRST_BLE_HAS_USER_DEFINED_RANDOM_STATIC_ADDR + bool "Whether to use a custom BLE address" + +config PRST_BLE_USER_DEFINED_RANDOM_STATIC_ADDR + string "Overrides the device's BT address" + help + This address has to be random static (e.g.: f0:ca:f0:ca:01:d5). + depends on PRST_BLE_HAS_USER_DEFINED_RANDOM_STATIC_ADDR diff --git a/code/nrf-connect/samples/ble/prj.conf b/code/nrf-connect/samples/ble/prj.conf index 0bc81cc..7fb14f4 100644 --- a/code/nrf-connect/samples/ble/prj.conf +++ b/code/nrf-connect/samples/ble/prj.conf @@ -16,7 +16,6 @@ CONFIG_PM_DEVICE=y CONFIG_USE_SEGGER_RTT=y -# Otherwise a weird float multiplication error? CONFIG_NEWLIB_LIBC=y CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y diff --git a/code/nrf-connect/samples/ble/src/ble.c b/code/nrf-connect/samples/ble/src/ble.c index bc24249..1d84a83 100644 --- a/code/nrf-connect/samples/ble/src/ble.c +++ b/code/nrf-connect/samples/ble/src/ble.c @@ -21,6 +21,13 @@ static const struct bt_data sd[] = {}; static bt_addr_le_t mac_addr; +static int set_user_defined_bt_addr(const char *addr_str) { + bt_addr_le_t addr; + RET_IF_ERR(bt_addr_le_from_str(addr_str, "random", &addr)); + RET_IF_ERR(bt_id_create(&addr, NULL)); + return 0; +} + // bt_addr_le_t.a holds the MAC address in big-endian. static int get_mac_addr(bt_addr_le_t *out) { struct bt_le_oob oob; @@ -33,6 +40,12 @@ static int get_mac_addr(bt_addr_le_t *out) { } int prst_ble_init() { +#if CONFIG_PRST_BLE_HAS_USER_DEFINED_RANDOM_STATIC_ADDR + RET_IF_ERR(set_user_defined_bt_addr(CONFIG_PRST_BLE_USER_DEFINED_RANDOM_STATIC_ADDR)); +#else + UNUSED_OK(set_user_defined_bt_addr); +#endif // PRST_BLE_HAS_USER_DEFINED_MAC_ADDR + RET_IF_ERR(bt_enable(/*bt_reader_cb_t=*/NULL)); if (IS_ENABLED(CONFIG_SETTINGS)) { RET_IF_ERR_MSG(settings_load(), "Error in settings_load()");