From 6fbbd6de24a8958745668343a8f13c6fa3c60668 Mon Sep 17 00:00:00 2001 From: Luis Ubieda Date: Wed, 6 Mar 2024 13:17:38 -0500 Subject: [PATCH] bluetooth: nus: Add Kconfig option to auto-start Bluetooth LE Enables compatibility of NUS for codebases that are not Bluetooth-centric (e.g: Non-bluetooth samples that use UART over Bluetooth LE for Console, Logging, Shell, others). Signed-off-by: Luis Ubieda --- subsys/bluetooth/services/nus/CMakeLists.txt | 4 +++ subsys/bluetooth/services/nus/Kconfig.nus | 7 ++++ .../services/nus/bt_nus_auto_start_bt.c | 36 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 subsys/bluetooth/services/nus/bt_nus_auto_start_bt.c diff --git a/subsys/bluetooth/services/nus/CMakeLists.txt b/subsys/bluetooth/services/nus/CMakeLists.txt index 31ecfa68b1..a15271cba3 100644 --- a/subsys/bluetooth/services/nus/CMakeLists.txt +++ b/subsys/bluetooth/services/nus/CMakeLists.txt @@ -8,3 +8,7 @@ zephyr_library_sources( nus.c nus_inst.c ) + +zephyr_library_sources_ifdef(CONFIG_BT_NUS_AUTO_START_BLUETOOTH + bt_nus_auto_start_bt.c +) diff --git a/subsys/bluetooth/services/nus/Kconfig.nus b/subsys/bluetooth/services/nus/Kconfig.nus index b74a359980..1841226d7c 100644 --- a/subsys/bluetooth/services/nus/Kconfig.nus +++ b/subsys/bluetooth/services/nus/Kconfig.nus @@ -16,4 +16,11 @@ config BT_NUS_DEFAULT_INSTANCE BT_NUS_INST_DEFINE(), it may not be beneficial having an internal instance as well. +config BT_NUS_AUTO_START_BLUETOOTH + bool "Auto-enable Bluetooth stack and start LE advertisements" + help + Auto-Enable the Bluetooth stack and start advertising with the NUS + UUID. Useful to run applications that inherently do not deal with + Bluetooth (e.g: Non-Bluetooth samples using UART over Bluetooth LE). + endif # BT_NUS diff --git a/subsys/bluetooth/services/nus/bt_nus_auto_start_bt.c b/subsys/bluetooth/services/nus/bt_nus_auto_start_bt.c new file mode 100644 index 0000000000..be7b16998a --- /dev/null +++ b/subsys/bluetooth/services/nus/bt_nus_auto_start_bt.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024 Croxel, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#define DEVICE_NAME CONFIG_BT_DEVICE_NAME +#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) + +static const struct bt_data ad[] = { + BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), + BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), +}; + +static const struct bt_data sd[] = { + BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NUS_SRV_VAL), +}; + +static int bt_nus_auto_start(void) +{ + int err; + + err = bt_enable(NULL); + __ASSERT_NO_MSG(!err); + + err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd)); + __ASSERT_NO_MSG(!err); + + return 0; +} + +SYS_INIT(bt_nus_auto_start, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);