diff --git a/samples/bluetooth/peripheral_nus/CMakeLists.txt b/samples/bluetooth/peripheral_nus/CMakeLists.txt new file mode 100644 index 00000000000..2da8d777ccb --- /dev/null +++ b/samples/bluetooth/peripheral_nus/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(peripheral_nus) + +target_sources(app PRIVATE + src/main.c +) diff --git a/samples/bluetooth/peripheral_nus/README.rst b/samples/bluetooth/peripheral_nus/README.rst new file mode 100644 index 00000000000..2cbf68f80ca --- /dev/null +++ b/samples/bluetooth/peripheral_nus/README.rst @@ -0,0 +1,26 @@ +.. _peripheral_nus: + +Bluetooth: Peripheral NUS +######################### + +Overview +******** + +This sample demonstrates the usage of the NUS service (Nordic UART Service) as a serial +endpoint to exchange data. In this case, the sample assumes the data is UTF-8 encoded, +but it may be binary data. Once the user connects to the device and subscribes to the TX +characteristic, it will start receiving periodic notifications with "Hello World!\n". + +Requirements +************ + +* BlueZ running on the host, or +* A board with BLE support + +Building and Running +******************** + +This sample can be found under :zephyr_file:`samples/bluetooth/peripheral_nus` in the +Zephyr tree. + +See :ref:`bluetooth samples section ` for details. diff --git a/samples/bluetooth/peripheral_nus/prj.conf b/samples/bluetooth/peripheral_nus/prj.conf new file mode 100644 index 00000000000..8eafb75f19c --- /dev/null +++ b/samples/bluetooth/peripheral_nus/prj.conf @@ -0,0 +1,3 @@ +CONFIG_BT=y +CONFIG_BT_PERIPHERAL=y +CONFIG_BT_NUS=y diff --git a/samples/bluetooth/peripheral_nus/sample.yaml b/samples/bluetooth/peripheral_nus/sample.yaml new file mode 100644 index 00000000000..af70193ec1e --- /dev/null +++ b/samples/bluetooth/peripheral_nus/sample.yaml @@ -0,0 +1,13 @@ +sample: + name: Bluetooth Peripheral NUS + description: Demonstrates the NUS GATT Service (Nordic UART Service) +tests: + sample.bluetooth.peripheral_nus: + harness: bluetooth + platform_allow: + - qemu_cortex_m3 + - qemu_x86 + - nrf52840dk/nrf52840 + integration_platforms: + - qemu_cortex_m3 + tags: bluetooth diff --git a/samples/bluetooth/peripheral_nus/src/main.c b/samples/bluetooth/peripheral_nus/src/main.c new file mode 100644 index 00000000000..e94a45a0e08 --- /dev/null +++ b/samples/bluetooth/peripheral_nus/src/main.c @@ -0,0 +1,86 @@ +/* + * 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 void notif_enabled(bool enabled, void *ctx) +{ + ARG_UNUSED(ctx); + + printk("%s() - %s\n", __func__, (enabled ? "Enabled" : "Disabled")); +} + +static void received(struct bt_conn *conn, const void *data, uint16_t len, void *ctx) +{ + char message[CONFIG_BT_L2CAP_TX_MTU + 1] = ""; + + ARG_UNUSED(conn); + ARG_UNUSED(ctx); + + memcpy(message, data, MIN(sizeof(message) - 1, len)); + printk("%s() - Len: %d, Message: %s\n", __func__, len, message); +} + +struct bt_nus_cb nus_listener = { + .notif_enabled = notif_enabled, + .received = received, +}; + +int main(void) +{ + int err; + + printk("Sample - Bluetooth Peripheral NUS\n"); + + err = bt_nus_cb_register(&nus_listener, NULL); + if (err) { + printk("Failed to register NUS callback: %d\n", err); + return err; + } + + err = bt_enable(NULL); + if (err) { + printk("Failed to enable bluetooth: %d\n", err); + return err; + } + + err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd)); + if (err) { + printk("Failed to start advertising: %d\n", err); + return err; + } + + printk("Initialization complete\n"); + + while (true) { + const char *hello_world = "Hello World!\n"; + + k_sleep(K_SECONDS(3)); + + err = bt_nus_send(NULL, hello_world, strlen(hello_world)); + printk("Data send - Result: %d\n", err); + + if (err < 0 && (err != -EAGAIN) && (err != -ENOTCONN)) { + return err; + } + } + + return 0; +}