From c232d370fea903716dd8bae385f21e97999ba819 Mon Sep 17 00:00:00 2001 From: Michael Hunold Date: Wed, 4 Oct 2023 15:08:35 +0000 Subject: [PATCH 1/3] samples: central_uart: Replace hard-coded values with Kconfig entries In contrast to the peripheral_uart sample application the central_uart application uses hard-coded values for buffer size, UART Rx wait time and NUS write timeout. This patch replaces the hard-coded values with proper entries in a Kconfig file to make this sample match the peripheral_uart sample application. Signed-off-by: Michael Hunold --- samples/bluetooth/central_uart/Kconfig | 29 +++++++++++++++++++++++ samples/bluetooth/central_uart/src/main.c | 6 ++--- 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 samples/bluetooth/central_uart/Kconfig diff --git a/samples/bluetooth/central_uart/Kconfig b/samples/bluetooth/central_uart/Kconfig new file mode 100644 index 000000000000..7c1a29b00260 --- /dev/null +++ b/samples/bluetooth/central_uart/Kconfig @@ -0,0 +1,29 @@ +# +# Copyright (c) 2023 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +source "Kconfig.zephyr" + +menu "Nordic UART Service Client sample" + +config BT_NUS_UART_BUFFER_SIZE + int "UART payload buffer element size" + default 20 + help + Size of the payload buffer in each RX and TX FIFO element + +config BT_NUS_UART_RX_WAIT_TIME + int "Timeout for UART RX complete event" + default 50 + help + Wait for RX complete event time in microseconds + +config BT_NUS_WRITE_TIMEOUT + int "Timeout for NUS write complete event" + default 150 + help + Wait for NUS write complete event time in milliseconds + +endmenu diff --git a/samples/bluetooth/central_uart/src/main.c b/samples/bluetooth/central_uart/src/main.c index be4c1f0ae7d0..3b70cf66bbef 100644 --- a/samples/bluetooth/central_uart/src/main.c +++ b/samples/bluetooth/central_uart/src/main.c @@ -36,14 +36,14 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME); /* UART payload buffer element size. */ -#define UART_BUF_SIZE 20 +#define UART_BUF_SIZE CONFIG_BT_NUS_UART_BUFFER_SIZE #define KEY_PASSKEY_ACCEPT DK_BTN1_MSK #define KEY_PASSKEY_REJECT DK_BTN2_MSK -#define NUS_WRITE_TIMEOUT K_MSEC(150) +#define NUS_WRITE_TIMEOUT K_MSEC(CONFIG_BT_NUS_WRITE_TIMEOUT) #define UART_WAIT_FOR_BUF_DELAY K_MSEC(50) -#define UART_RX_TIMEOUT 50 +#define UART_RX_TIMEOUT CONFIG_BT_NUS_UART_RX_WAIT_TIME static const struct device *uart = DEVICE_DT_GET(DT_NODELABEL(uart0)); static struct k_work_delayable uart_work; From 6e0d30b07ed545414fb7efff9b5f036a0742c1c1 Mon Sep 17 00:00:00 2001 From: Michael Hunold Date: Thu, 5 Oct 2023 08:46:56 +0000 Subject: [PATCH 2/3] samples: central_uart: Add support for unsecure connection The peripheral_uart supports the Kconfig option BT_NUS_SECURITY_ENABLED which allows the sample application to be built with or without support for a secure connection. This option is not present in the central_uart sample, it unconditionally enforces a secure connection. If the peripheral_uart sample application is built without support for a secure connection, this leads to the following error and warning messages in the log file for the central_uart sample application when a connection is established: bt_smp: pairing failed (peer reason 0x5) central_uart: Security failed: xx:xx:xx:xx:xx:xx (random) level 1 err 5 central_uart: Pairing failed conn: xx:xx:xx:xx:xx:xx (random), reason 5 This patch adds BT_NUS_SECURITY_ENABLED to the central_uart sample application similarly to the peripheral_uart sample application, but the default behaviour when building the sample application is unchanged. Signed-off-by: Michael Hunold --- samples/bluetooth/central_uart/Kconfig | 7 +++++++ samples/bluetooth/central_uart/prj.conf | 1 - samples/bluetooth/central_uart/src/main.c | 13 ++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/samples/bluetooth/central_uart/Kconfig b/samples/bluetooth/central_uart/Kconfig index 7c1a29b00260..9fdc303b0a50 100644 --- a/samples/bluetooth/central_uart/Kconfig +++ b/samples/bluetooth/central_uart/Kconfig @@ -14,6 +14,13 @@ config BT_NUS_UART_BUFFER_SIZE help Size of the payload buffer in each RX and TX FIFO element +config BT_NUS_SECURITY_ENABLED + bool "Enable security" + default y + select BT_SMP + help + "Enable BLE security for the UART service client" + config BT_NUS_UART_RX_WAIT_TIME int "Timeout for UART RX complete event" default 50 diff --git a/samples/bluetooth/central_uart/prj.conf b/samples/bluetooth/central_uart/prj.conf index 7a7f864dbb58..f4e3dfa61320 100644 --- a/samples/bluetooth/central_uart/prj.conf +++ b/samples/bluetooth/central_uart/prj.conf @@ -14,7 +14,6 @@ CONFIG_UART_CONSOLE=y # Enable the BLE stack with GATT Client configuration CONFIG_BT=y CONFIG_BT_CENTRAL=y -CONFIG_BT_SMP=y CONFIG_BT_GATT_CLIENT=y # Enable the BLE modules from NCS diff --git a/samples/bluetooth/central_uart/src/main.c b/samples/bluetooth/central_uart/src/main.c index 3b70cf66bbef..6eec6d2a836a 100644 --- a/samples/bluetooth/central_uart/src/main.c +++ b/samples/bluetooth/central_uart/src/main.c @@ -384,12 +384,16 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) LOG_WRN("MTU exchange failed (err %d)", err); } +#ifdef CONFIG_BT_NUS_SECURITY_ENABLED err = bt_conn_set_security(conn, BT_SECURITY_L2); if (err) { LOG_WRN("Failed to set security: %d", err); gatt_discover(conn); } +#else + gatt_discover(conn); +#endif err = bt_scan_stop(); if ((!err) && (err != -EALREADY)) { @@ -420,6 +424,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) } } +#ifdef CONFIG_BT_NUS_SECURITY_ENABLED static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) { @@ -436,11 +441,14 @@ static void security_changed(struct bt_conn *conn, bt_security_t level, gatt_discover(conn); } +#endif BT_CONN_CB_DEFINE(conn_callbacks) = { .connected = connected, .disconnected = disconnected, +#ifdef CONFIG_BT_NUS_SECURITY_ENABLED .security_changed = security_changed +#endif }; static void scan_filter_match(struct bt_scan_device_info *device_info, @@ -515,7 +523,7 @@ static int scan_init(void) return err; } - +#ifdef CONFIG_BT_NUS_SECURITY_ENABLED static void auth_cancel(struct bt_conn *conn) { char addr[BT_ADDR_LE_STR_LEN]; @@ -553,11 +561,13 @@ static struct bt_conn_auth_info_cb conn_auth_info_callbacks = { .pairing_complete = pairing_complete, .pairing_failed = pairing_failed }; +#endif int main(void) { int err; +#ifdef CONFIG_BT_NUS_SECURITY_ENABLED err = bt_conn_auth_cb_register(&conn_auth_callbacks); if (err) { LOG_ERR("Failed to register authorization callbacks."); @@ -569,6 +579,7 @@ int main(void) printk("Failed to register authorization info callbacks.\n"); return 0; } +#endif err = bt_enable(NULL); if (err) { From 2bfdc4c950750056674ba36cf0677c661c45be69 Mon Sep 17 00:00:00 2001 From: Michael Hunold Date: Thu, 5 Oct 2023 09:04:58 +0000 Subject: [PATCH 3/3] samples: central_uart: Make UART_RX_TIMEOUT match peripheral_uart The peripheral_uart and central_uart sample applications use almost identical code for the UART handling. But one difference is the usage of the UART_RX_TIMEOUT value. It is used in uart_rx_enable() and is expected to be a value in microseconds. The peripheral_uart sample application uses a value of 50000 for UART_RX_TIMEOUT which corresponds to a timeout of 50ms, which is pretty reasonable for a timeout value for the UART. Due to whatever reasons the central_uart application uses a value of 50 for UART_RX_TIMEOUT which corresponds to a timeout of just 50 microseconds, which looks unreasonable. The assumption here now is that this was simply an oversight and really 50000 should have been used right from the beginning. This patch fixes this and makes the handling of peripheral_uart and central_uart identical when it comes to UART_RX_TIMEOUT. Signed-off-by: Michael Hunold --- samples/bluetooth/central_uart/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/bluetooth/central_uart/Kconfig b/samples/bluetooth/central_uart/Kconfig index 9fdc303b0a50..b1347590c712 100644 --- a/samples/bluetooth/central_uart/Kconfig +++ b/samples/bluetooth/central_uart/Kconfig @@ -23,7 +23,7 @@ config BT_NUS_SECURITY_ENABLED config BT_NUS_UART_RX_WAIT_TIME int "Timeout for UART RX complete event" - default 50 + default 50000 help Wait for RX complete event time in microseconds