samples: bluetooth: nus: Add Peripheral NUS sample
Demonstrating basic usage of NUS to exchange data. Signed-off-by: Luis Ubieda <luisf@croxel.com>
This commit is contained in:
parent
f6b1b24a50
commit
d834ec875e
5 changed files with 135 additions and 0 deletions
7
samples/bluetooth/peripheral_nus/CMakeLists.txt
Normal file
7
samples/bluetooth/peripheral_nus/CMakeLists.txt
Normal file
|
@ -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
|
||||
)
|
26
samples/bluetooth/peripheral_nus/README.rst
Normal file
26
samples/bluetooth/peripheral_nus/README.rst
Normal file
|
@ -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 <bluetooth-samples>` for details.
|
3
samples/bluetooth/peripheral_nus/prj.conf
Normal file
3
samples/bluetooth/peripheral_nus/prj.conf
Normal file
|
@ -0,0 +1,3 @@
|
|||
CONFIG_BT=y
|
||||
CONFIG_BT_PERIPHERAL=y
|
||||
CONFIG_BT_NUS=y
|
13
samples/bluetooth/peripheral_nus/sample.yaml
Normal file
13
samples/bluetooth/peripheral_nus/sample.yaml
Normal file
|
@ -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
|
86
samples/bluetooth/peripheral_nus/src/main.c
Normal file
86
samples/bluetooth/peripheral_nus/src/main.c
Normal file
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* 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 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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue