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 <luisf@croxel.com>
This commit is contained in:
Luis Ubieda 2024-03-06 13:17:38 -05:00 committed by Carles Cufí
parent 205994b87b
commit 6fbbd6de24
3 changed files with 47 additions and 0 deletions

View file

@ -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
)

View file

@ -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

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Croxel, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/services/nus.h>
#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);