From 2ccaeef95cd020ce86e7a565d5c054f8e638bd86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Rosi=C5=84ski?= Date: Sat, 23 Dec 2023 16:39:44 +0100 Subject: [PATCH 1/4] add peripheral example --- .npmignore | 2 - zephyr/peripheral_lbs/.gitignore | 6 + zephyr/peripheral_lbs/CMakeLists.txt | 17 ++ zephyr/peripheral_lbs/Kconfig | 24 ++ zephyr/peripheral_lbs/Kconfig.sysbuild | 10 + zephyr/peripheral_lbs/README.rst | 168 ++++++++++++ zephyr/peripheral_lbs/VERSION | 5 + .../boards/thingy53_nrf5340_cpuapp.conf | 19 ++ .../boards/thingy53_nrf5340_cpuapp_ns.conf | 19 ++ zephyr/peripheral_lbs/prj.conf | 17 ++ zephyr/peripheral_lbs/prj_minimal.conf | 111 ++++++++ zephyr/peripheral_lbs/sample.yaml | 75 ++++++ zephyr/peripheral_lbs/src/main.c | 248 ++++++++++++++++++ 13 files changed, 719 insertions(+), 2 deletions(-) create mode 100644 zephyr/peripheral_lbs/.gitignore create mode 100644 zephyr/peripheral_lbs/CMakeLists.txt create mode 100644 zephyr/peripheral_lbs/Kconfig create mode 100644 zephyr/peripheral_lbs/Kconfig.sysbuild create mode 100644 zephyr/peripheral_lbs/README.rst create mode 100644 zephyr/peripheral_lbs/VERSION create mode 100644 zephyr/peripheral_lbs/boards/thingy53_nrf5340_cpuapp.conf create mode 100644 zephyr/peripheral_lbs/boards/thingy53_nrf5340_cpuapp_ns.conf create mode 100644 zephyr/peripheral_lbs/prj.conf create mode 100644 zephyr/peripheral_lbs/prj_minimal.conf create mode 100644 zephyr/peripheral_lbs/sample.yaml create mode 100644 zephyr/peripheral_lbs/src/main.c diff --git a/.npmignore b/.npmignore index d9f9990..690b24a 100644 --- a/.npmignore +++ b/.npmignore @@ -14,5 +14,3 @@ /assigned-numbers /.prettierignore /.prettierrc -/.eslintignore -/.eslintrc \ No newline at end of file diff --git a/zephyr/peripheral_lbs/.gitignore b/zephyr/peripheral_lbs/.gitignore new file mode 100644 index 0000000..635a99b --- /dev/null +++ b/zephyr/peripheral_lbs/.gitignore @@ -0,0 +1,6 @@ +# editors +*.swp +*~ + +# build +/build*/ diff --git a/zephyr/peripheral_lbs/CMakeLists.txt b/zephyr/peripheral_lbs/CMakeLists.txt new file mode 100644 index 0000000..4800b6f --- /dev/null +++ b/zephyr/peripheral_lbs/CMakeLists.txt @@ -0,0 +1,17 @@ +# +# Copyright (c) 2018 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(peripheral_lbs) + +# NORDIC SDK APP START +target_sources(app PRIVATE + src/main.c +) + +# NORDIC SDK APP END +zephyr_library_include_directories(.) diff --git a/zephyr/peripheral_lbs/Kconfig b/zephyr/peripheral_lbs/Kconfig new file mode 100644 index 0000000..96645f7 --- /dev/null +++ b/zephyr/peripheral_lbs/Kconfig @@ -0,0 +1,24 @@ +# +# Copyright (c) 2018 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +source "Kconfig.zephyr" + +menu "Nordic LED-Button BLE GATT service sample" + +config BT_LBS_SECURITY_ENABLED + bool "Enable security" + default y + select BT_SMP + select BT_SETTINGS + select FLASH + select FLASH_PAGE_LAYOUT + select FLASH_MAP + select NVS + select SETTINGS + help + "Enable BLE security for the LED-Button service" + +endmenu diff --git a/zephyr/peripheral_lbs/Kconfig.sysbuild b/zephyr/peripheral_lbs/Kconfig.sysbuild new file mode 100644 index 0000000..3f4664a --- /dev/null +++ b/zephyr/peripheral_lbs/Kconfig.sysbuild @@ -0,0 +1,10 @@ +# +# Copyright (c) 2023 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +source "${ZEPHYR_BASE}/share/sysbuild/Kconfig" + +config NRF_DEFAULT_BLUETOOTH + default y diff --git a/zephyr/peripheral_lbs/README.rst b/zephyr/peripheral_lbs/README.rst new file mode 100644 index 0000000..880dfa9 --- /dev/null +++ b/zephyr/peripheral_lbs/README.rst @@ -0,0 +1,168 @@ +.. _peripheral_lbs: + +Bluetooth: Peripheral LBS +######################### + +.. contents:: + :local: + :depth: 2 + +The peripheral LBS sample demonstrates how to use the :ref:`lbs_readme`. + +Requirements +************ + +The sample supports the following development kits: + +.. table-from-sample-yaml:: + +.. include:: /includes/tfm.txt + +The sample also requires a smartphone or tablet running a compatible application. +The `Testing`_ instructions refer to `nRF Connect for Mobile`_, but you can also use other similar applications (for example, `nRF Blinky`_ or `nRF Toolbox`_). + +.. note:: + |thingy53_sample_note| + +Overview +******** + +When connected, the sample sends the state of **Button 1** on the development kit to the connected device, such as a phone or tablet. +The mobile application on the device can display the received button state and control the state of **LED 3** on the development kit. + +You can also use this sample to control the color of the RGB LED on the nRF52840 Dongle or Thingy:53. + +User interface +************** + +The user interface of the sample depends on the hardware platform you are using. + +nRF52840 Dongle +=============== + +Green LED: + Blinks, toggling on/off every second, when the main loop is running and the device is advertising. + +RGB LED: + The RGB LED channels are used independently to display the following information: + + * Red - If Dongle is connected. + * Green - If user set the LED using Nordic LED Button Service. + +Button 1: + Send a notification with the button state: "pressed" or "released". + +Thingy:53 +========= + +RGB LED: + The RGB LED channels are used independently to display the following information: + + * Red - If the main loop is running (that is, the device is advertising). + The LED blinks with a period of two seconds, duty cycle 50%. + * Green - If the device is connected. + * Blue - If user set the LED using Nordic LED Button Service. + + For example, if Thingy:53 is connected over Bluetooth, the LED color toggles between green and yellow. + The green LED channel is kept on and the red LED channel is blinking. + +Button 1: + Send a notification with the button state: "pressed" or "released". + +Development kits +================ + +LED 1: + Blinks when the main loop is running (that is, the device is advertising) with a period of two seconds, duty cycle 50%. + +LED 2: + Lit when the development kit is connected. + +LED 3: + Lit when the development kit is controlled remotely from the connected device. + +Button 1: + Send a notification with the button state: "pressed" or "released". + +Building and running +******************** +.. |sample path| replace:: :file:`samples/bluetooth/peripheral_lbs` + +.. include:: /includes/build_and_run_ns.txt + +Minimal build +============= + +You can build the sample with a minimum configuration as a demonstration of how to reduce code size and RAM usage, using the ``-DCONF_FILE='prj_minimal.conf'`` flag in your build. + +See :ref:`cmake_options` for instructions on how to add this option to your build. +For example, when building on the command line, you can add the option as follows: + +.. code-block:: console + + west build samples/bluetooth/peripheral_lbs -- -DCONF_FILE='prj_minimal.conf' + +.. _peripheral_lbs_testing: + +Testing +======= + +After programming the sample to your dongle or development kit, test it by performing the following steps: + +1. Start the `nRF Connect for Mobile`_ application on your smartphone or tablet. +#. Power on the development kit or insert your dongle into the USB port. +#. Connect to the device from the application. + The device is advertising as ``Nordic_LBS``. + The services of the connected device are shown. +#. In **Nordic LED Button Service**, enable notifications for the **Button** characteristic. +#. Press **Button 1** on the device. +#. Observe that notifications with the following values are displayed: + + * ``Button released`` when **Button 1** is released. + * ``Button pressed`` when **Button 1** is pressed. + +#. Write the following values to the LED characteristic in the **Nordic LED Button Service**. + Depending on the hardware platform, this produces results described in the table. + ++------------------------+---------+----------------------------------------------+ +| Hardware platform | Value | Effect | ++========================+=========+==============================================+ +| Development kit | ``OFF`` | Switch the **LED3** off. | ++ +---------+----------------------------------------------+ +| | ``ON`` | Switch the **LED3** on. | ++------------------------+---------+----------------------------------------------+ +| nRF52840 Dongle | ``OFF`` | Switch the green channel of the RGB LED off. | ++ +---------+----------------------------------------------+ +| | ``ON`` | Switch the green channel of the RGB LED on. | ++------------------------+---------+----------------------------------------------+ +| Thingy:53 | ``OFF`` | Switch the blue channel of the RGB LED off. | ++ +---------+----------------------------------------------+ +| | ``ON`` | Switch the blue channel of the RGB LED on. | ++------------------------+---------+----------------------------------------------+ + +Dependencies +************ + +This sample uses the following |NCS| libraries: + +* :ref:`lbs_readme` +* :ref:`dk_buttons_and_leds_readme` + +In addition, it uses the following Zephyr libraries: + +* :file:`include/zephyr/types.h` +* :file:`lib/libc/minimal/include/errno.h` +* :file:`include/sys/printk.h` +* :file:`include/sys/byteorder.h` +* :ref:`GPIO Interface ` +* :ref:`zephyr:bluetooth_api`: + + * :file:`include/bluetooth/bluetooth.h` + * :file:`include/bluetooth/hci.h` + * :file:`include/bluetooth/conn.h` + * :file:`include/bluetooth/uuid.h` + * :file:`include/bluetooth/gatt.h` + +The sample also uses the following secure firmware component: + +* :ref:`Trusted Firmware-M ` diff --git a/zephyr/peripheral_lbs/VERSION b/zephyr/peripheral_lbs/VERSION new file mode 100644 index 0000000..956d85e --- /dev/null +++ b/zephyr/peripheral_lbs/VERSION @@ -0,0 +1,5 @@ +VERSION_MAJOR = 2 +VERSION_MINOR = 5 +PATCHLEVEL = 0 +VERSION_TWEAK = 0 +EXTRAVERSION = diff --git a/zephyr/peripheral_lbs/boards/thingy53_nrf5340_cpuapp.conf b/zephyr/peripheral_lbs/boards/thingy53_nrf5340_cpuapp.conf new file mode 100644 index 0000000..214aa1e --- /dev/null +++ b/zephyr/peripheral_lbs/boards/thingy53_nrf5340_cpuapp.conf @@ -0,0 +1,19 @@ +# +# Copyright (c) 2021 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +################################################################################ +# Application overlay - Thingy:53 DFU + +CONFIG_NCS_SAMPLE_MCUMGR_BT_OTA_DFU=y + +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096 + +# Use deferred logging +CONFIG_NCS_SAMPLES_DEFAULTS=n +CONFIG_LOG=y +CONFIG_ASSERT=y +CONFIG_ASSERT_NO_COND_INFO=y +CONFIG_ASSERT_NO_MSG_INFO=y +CONFIG_HW_STACK_PROTECTION=y diff --git a/zephyr/peripheral_lbs/boards/thingy53_nrf5340_cpuapp_ns.conf b/zephyr/peripheral_lbs/boards/thingy53_nrf5340_cpuapp_ns.conf new file mode 100644 index 0000000..214aa1e --- /dev/null +++ b/zephyr/peripheral_lbs/boards/thingy53_nrf5340_cpuapp_ns.conf @@ -0,0 +1,19 @@ +# +# Copyright (c) 2021 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +################################################################################ +# Application overlay - Thingy:53 DFU + +CONFIG_NCS_SAMPLE_MCUMGR_BT_OTA_DFU=y + +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096 + +# Use deferred logging +CONFIG_NCS_SAMPLES_DEFAULTS=n +CONFIG_LOG=y +CONFIG_ASSERT=y +CONFIG_ASSERT_NO_COND_INFO=y +CONFIG_ASSERT_NO_MSG_INFO=y +CONFIG_HW_STACK_PROTECTION=y diff --git a/zephyr/peripheral_lbs/prj.conf b/zephyr/peripheral_lbs/prj.conf new file mode 100644 index 0000000..63a6818 --- /dev/null +++ b/zephyr/peripheral_lbs/prj.conf @@ -0,0 +1,17 @@ +# +# Copyright (c) 2018 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +CONFIG_NCS_SAMPLES_DEFAULTS=y + +CONFIG_BT=y +CONFIG_BT_PERIPHERAL=y +CONFIG_BT_DEVICE_NAME="Nordic_LBS" + +# Enable the LBS service +CONFIG_BT_LBS=y +CONFIG_BT_LBS_POLL_BUTTON=y +CONFIG_DK_LIBRARY=y + +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048 diff --git a/zephyr/peripheral_lbs/prj_minimal.conf b/zephyr/peripheral_lbs/prj_minimal.conf new file mode 100644 index 0000000..41d943b --- /dev/null +++ b/zephyr/peripheral_lbs/prj_minimal.conf @@ -0,0 +1,111 @@ +# BEGIN Configurations from basic/minimal + +CONFIG_NCS_SAMPLES_DEFAULTS=y + +CONFIG_BT=y +CONFIG_BT_PERIPHERAL=y +CONFIG_BT_DEVICE_NAME="Nordic_LBS" + +# Enable the LBS service +CONFIG_BT_LBS=y +CONFIG_BT_LBS_POLL_BUTTON=y +CONFIG_DK_LIBRARY=y + +# Drivers and peripherals +CONFIG_I2C=n +CONFIG_WATCHDOG=n +CONFIG_GPIO=n +CONFIG_SPI=n +CONFIG_SERIAL=n + +# Power management + +# Interrupts +CONFIG_DYNAMIC_INTERRUPTS=n +CONFIG_IRQ_OFFLOAD=n + +# Memory protection +CONFIG_THREAD_STACK_INFO=n +CONFIG_THREAD_CUSTOM_DATA=n +CONFIG_FPU=n + +# Boot +CONFIG_BOOT_BANNER=n +CONFIG_BOOT_DELAY=0 + +# Console +CONFIG_CONSOLE=n +CONFIG_UART_CONSOLE=n +CONFIG_STDOUT_CONSOLE=n +CONFIG_PRINTK=n +CONFIG_EARLY_CONSOLE=n + +# Build +CONFIG_SIZE_OPTIMIZATIONS=y + +# ARM +CONFIG_ARM_MPU=n +# END Configurations from basic/minimal + +# In order to correctly tune the stack sizes for the threads the following +# Configurations can enabled to print the current use: +# CONFIG_THREAD_NAME=y +# CONFIG_THREAD_ANALYZER=y +# CONFIG_THREAD_ANALYZER_AUTO=y +# CONFIG_THREAD_ANALYZER_RUN_UNLOCKED=y +# CONFIG_THREAD_ANALYZER_USE_PRINTK=y +# CONFIG_CONSOLE=y +# CONFIG_UART_CONSOLE=y +# CONFIG_SERIAL=y +# CONFIG_PRINTK=y + +# Example output of thread analyzer +# SDC RX : unused 816 usage 208 / 1024 (20 %) +# BT RX : unused 1784 usage 416 / 2200 (18 %) +# BT TX : unused 992 usage 544 / 1536 (35 %) +# thread_analyzer : unused 136 usage 376 / 512 (73 %) +# sysworkq : unused 1824 usage 224 / 2048 (10 %) +# MPSL signal : unused 520 usage 504 / 1024 (49 %) +# idle 00 : unused 192 usage 64 / 256 (25 %) +# main : unused 136 usage 888 / 1024 (86 %) +# Configurations set based on thread analyzer output. +CONFIG_BT_RX_STACK_SIZE=1024 +CONFIG_BT_HCI_TX_STACK_SIZE_WITH_PROMPT=y +CONFIG_BT_HCI_TX_STACK_SIZE=640 +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=512 +CONFIG_MPSL_WORK_STACK_SIZE=640 +CONFIG_MAIN_STACK_SIZE=1024 +CONFIG_IDLE_STACK_SIZE=128 +CONFIG_ISR_STACK_SIZE=1024 + +# Disable features not needed +CONFIG_TIMESLICING=n +CONFIG_MINIMAL_LIBC_MALLOC=n +CONFIG_LOG=n +CONFIG_ASSERT=n + +# Disable Bluetooth features not needed +CONFIG_BT_DEBUG_NONE=y +CONFIG_BT_ASSERT=n +CONFIG_BT_DATA_LEN_UPDATE=n +CONFIG_BT_PHY_UPDATE=n +CONFIG_BT_GATT_CACHING=n +CONFIG_BT_GATT_SERVICE_CHANGED=n +CONFIG_BT_GAP_PERIPHERAL_PREF_PARAMS=n +CONFIG_BT_SETTINGS_CCC_LAZY_LOADING=y +CONFIG_BT_HCI_VS_EXT=n + +# Disable Bluetooth controller features not needed +CONFIG_BT_CTLR_PRIVACY=n +CONFIG_BT_CTLR_PHY_2M=n + +# Reduce Bluetooth buffers +CONFIG_BT_BUF_EVT_DISCARDABLE_COUNT=1 +CONFIG_BT_BUF_EVT_DISCARDABLE_SIZE=43 +CONFIG_BT_BUF_EVT_RX_COUNT=2 + +CONFIG_BT_CONN_TX_MAX=2 +CONFIG_BT_L2CAP_TX_BUF_COUNT=2 +CONFIG_BT_CTLR_RX_BUFFERS=1 +CONFIG_BT_BUF_ACL_TX_COUNT=3 +CONFIG_BT_BUF_ACL_TX_SIZE=27 diff --git a/zephyr/peripheral_lbs/sample.yaml b/zephyr/peripheral_lbs/sample.yaml new file mode 100644 index 0000000..647371e --- /dev/null +++ b/zephyr/peripheral_lbs/sample.yaml @@ -0,0 +1,75 @@ +sample: + description: Bluetooth Low Energy LED Button service sample + name: BLE LED Button service +tests: + sample.bluetooth.peripheral_lbs: + build_only: true + integration_platforms: + - nrf52dk_nrf52832 + - nrf52840dk_nrf52840 + - nrf5340dk_nrf5340_cpuapp + - nrf5340dk_nrf5340_cpuapp_ns + - thingy53_nrf5340_cpuapp + - thingy53_nrf5340_cpuapp_ns + platform_allow: nrf52dk_nrf52832 nrf52840dk_nrf52840 nrf52dk_nrf52810 + nrf5340dk_nrf5340_cpuapp nrf5340dk_nrf5340_cpuapp_ns thingy53_nrf5340_cpuapp + thingy53_nrf5340_cpuapp_ns + tags: bluetooth ci_build + sample.bluetooth.peripheral_lbs_minimal: + build_only: true + extra_args: OVERLAY_CONFIG=prj_minimal.conf + integration_platforms: + - nrf52dk_nrf52810 + - nrf52840dk_nrf52811 + - nrf52833dk_nrf52820 + platform_allow: nrf52dk_nrf52810 nrf52840dk_nrf52811 nrf52833dk_nrf52820 + tags: bluetooth ci_build + sample.bluetooth.peripheral_lbs_no_security: + build_only: true + extra_args: CONFIG_BT_LBS_SECURITY_ENABLED=n + integration_platforms: + - nrf52dk_nrf52832 + - nrf52840dk_nrf52840 + - nrf5340dk_nrf5340_cpuapp + - nrf5340dk_nrf5340_cpuapp_ns + - thingy53_nrf5340_cpuapp + - thingy53_nrf5340_cpuapp_ns + platform_allow: nrf52dk_nrf52832 nrf52840dk_nrf52840 nrf52dk_nrf52810 + nrf5340dk_nrf5340_cpuapp nrf5340dk_nrf5340_cpuapp_ns thingy53_nrf5340_cpuapp + thingy53_nrf5340_cpuapp_ns + tags: bluetooth ci_build + sample.bluetooth.peripheral_lbs_bt_ota_dfu: + build_only: true + integration_platforms: + - nrf52dk_nrf52832 + - nrf52840dk_nrf52840 + - nrf5340dk_nrf5340_cpuapp + - nrf5340dk_nrf5340_cpuapp_ns + platform_allow: nrf52dk_nrf52832 nrf52840dk_nrf52840 nrf5340dk_nrf5340_cpuapp + nrf5340dk_nrf5340_cpuapp_ns + tags: bluetooth ci_build + extra_configs: + - CONFIG_BOOTLOADER_MCUBOOT=y + - CONFIG_NCS_SAMPLE_MCUMGR_BT_OTA_DFU=y + sample.bluetooth.peripheral_lbs_bt_ota_dfu.direct_xip: + build_only: true + integration_platforms: + - nrf52840dk_nrf52840 + platform_allow: nrf52840dk_nrf52840 + tags: bluetooth ci_build + extra_args: mcuboot_CONFIG_BOOT_DIRECT_XIP=y + extra_configs: + - CONFIG_BOOTLOADER_MCUBOOT=y + - CONFIG_NCS_SAMPLE_MCUMGR_BT_OTA_DFU=y + - CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP=y + sample.bluetooth.peripheral_lbs_bt_ota_dfu.direct_xip.revert: + build_only: true + integration_platforms: + - nrf52840dk_nrf52840 + platform_allow: nrf52840dk_nrf52840 + tags: bluetooth ci_build + extra_args: mcuboot_CONFIG_BOOT_DIRECT_XIP=y mcuboot_CONFIG_BOOT_DIRECT_XIP_REVERT=y + extra_configs: + - CONFIG_BOOTLOADER_MCUBOOT=y + - CONFIG_NCS_SAMPLE_MCUMGR_BT_OTA_DFU=y + - CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP_WITH_REVERT=y diff --git a/zephyr/peripheral_lbs/src/main.c b/zephyr/peripheral_lbs/src/main.c new file mode 100644 index 0000000..7d8ecdf --- /dev/null +++ b/zephyr/peripheral_lbs/src/main.c @@ -0,0 +1,248 @@ +/* + * Copyright (c) 2018 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include + +#include + +#define DEVICE_NAME CONFIG_BT_DEVICE_NAME +#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) + + +#define RUN_STATUS_LED DK_LED1 +#define CON_STATUS_LED DK_LED2 +#define RUN_LED_BLINK_INTERVAL 1000 + +#define USER_LED DK_LED3 + +#define USER_BUTTON DK_BTN1_MSK + +static bool app_button_state; + +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_LBS_VAL), +}; + +static void connected(struct bt_conn *conn, uint8_t err) +{ + if (err) { + printk("Connection failed (err %u)\n", err); + return; + } + + printk("Connected\n"); + + dk_set_led_on(CON_STATUS_LED); +} + +static void disconnected(struct bt_conn *conn, uint8_t reason) +{ + printk("Disconnected (reason %u)\n", reason); + + dk_set_led_off(CON_STATUS_LED); +} + +#ifdef CONFIG_BT_LBS_SECURITY_ENABLED +static void security_changed(struct bt_conn *conn, bt_security_t level, + enum bt_security_err err) +{ + char addr[BT_ADDR_LE_STR_LEN]; + + bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + + if (!err) { + printk("Security changed: %s level %u\n", addr, level); + } else { + printk("Security failed: %s level %u err %d\n", addr, level, + err); + } +} +#endif + +BT_CONN_CB_DEFINE(conn_callbacks) = { + .connected = connected, + .disconnected = disconnected, +#ifdef CONFIG_BT_LBS_SECURITY_ENABLED + .security_changed = security_changed, +#endif +}; + +#if defined(CONFIG_BT_LBS_SECURITY_ENABLED) +static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey) +{ + char addr[BT_ADDR_LE_STR_LEN]; + + bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + + printk("Passkey for %s: %06u\n", addr, passkey); +} + +static void auth_cancel(struct bt_conn *conn) +{ + char addr[BT_ADDR_LE_STR_LEN]; + + bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + + printk("Pairing cancelled: %s\n", addr); +} + +static void pairing_complete(struct bt_conn *conn, bool bonded) +{ + char addr[BT_ADDR_LE_STR_LEN]; + + bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + + printk("Pairing completed: %s, bonded: %d\n", addr, bonded); +} + +static void pairing_failed(struct bt_conn *conn, enum bt_security_err reason) +{ + char addr[BT_ADDR_LE_STR_LEN]; + + bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + + printk("Pairing failed conn: %s, reason %d\n", addr, reason); +} + +static struct bt_conn_auth_cb conn_auth_callbacks = { + .passkey_display = auth_passkey_display, + .cancel = auth_cancel, +}; + +static struct bt_conn_auth_info_cb conn_auth_info_callbacks = { + .pairing_complete = pairing_complete, + .pairing_failed = pairing_failed +}; +#else +static struct bt_conn_auth_cb conn_auth_callbacks; +static struct bt_conn_auth_info_cb conn_auth_info_callbacks; +#endif + +static void app_led_cb(bool led_state) +{ + dk_set_led(USER_LED, led_state); +} + +static bool app_button_cb(void) +{ + return app_button_state; +} + +static struct bt_lbs_cb lbs_callbacs = { + .led_cb = app_led_cb, + .button_cb = app_button_cb, +}; + +static void button_changed(uint32_t button_state, uint32_t has_changed) +{ + if (has_changed & USER_BUTTON) { + uint32_t user_button_state = button_state & USER_BUTTON; + + bt_lbs_send_button_state(user_button_state); + app_button_state = user_button_state ? true : false; + } +} + +static int init_button(void) +{ + int err; + + err = dk_buttons_init(button_changed); + if (err) { + printk("Cannot init buttons (err: %d)\n", err); + } + + return err; +} + +int main(void) +{ + int blink_status = 0; + int err; + + printk("Starting Bluetooth Peripheral LBS example\n"); + + err = dk_leds_init(); + if (err) { + printk("LEDs init failed (err %d)\n", err); + return 0; + } + + err = init_button(); + if (err) { + printk("Button init failed (err %d)\n", err); + return 0; + } + + if (IS_ENABLED(CONFIG_BT_LBS_SECURITY_ENABLED)) { + err = bt_conn_auth_cb_register(&conn_auth_callbacks); + if (err) { + printk("Failed to register authorization callbacks.\n"); + return 0; + } + + err = bt_conn_auth_info_cb_register(&conn_auth_info_callbacks); + if (err) { + printk("Failed to register authorization info callbacks.\n"); + return 0; + } + } + + err = bt_enable(NULL); + if (err) { + printk("Bluetooth init failed (err %d)\n", err); + return 0; + } + + printk("Bluetooth initialized\n"); + + if (IS_ENABLED(CONFIG_SETTINGS)) { + settings_load(); + } + + err = bt_lbs_init(&lbs_callbacs); + if (err) { + printk("Failed to init LBS (err:%d)\n", err); + return 0; + } + + err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), + sd, ARRAY_SIZE(sd)); + if (err) { + printk("Advertising failed to start (err %d)\n", err); + return 0; + } + + printk("Advertising successfully started\n"); + + for (;;) { + dk_set_led(RUN_STATUS_LED, (++blink_status) % 2); + k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL)); + } +} From b5d172f3acc2d929a06af717ad096b85a84d72a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Rosi=C5=84ski?= Date: Sat, 23 Dec 2023 16:41:43 +0100 Subject: [PATCH 2/4] update example --- README.md | 1 + examples/le-central-lbs.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index 17df5eb..d2b760c 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,7 @@ class App extends NbleGapCentral { return; // ignore } console.log(e); + } finally { console.log("Disconnecting..."); await this.disconnect(event.connectionHandle).catch(() => {}); } diff --git a/examples/le-central-lbs.ts b/examples/le-central-lbs.ts index dc7dc82..0a08ce3 100644 --- a/examples/le-central-lbs.ts +++ b/examples/le-central-lbs.ts @@ -109,6 +109,7 @@ class App extends NbleGapCentral { return; // ignore } console.log(e); + } finally { console.log("Disconnecting..."); await this.disconnect(event.connectionHandle).catch(() => {}); } From b3ea6dfe19b7105e89d713f855903ffd07c0ecce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Rosi=C5=84ski?= Date: Sat, 23 Dec 2023 16:44:15 +0100 Subject: [PATCH 3/4] remove prettier from npmignore (experiment) --- .npmignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.npmignore b/.npmignore index 690b24a..9c00799 100644 --- a/.npmignore +++ b/.npmignore @@ -12,5 +12,3 @@ /tsconfig.prod.json /gatt-profiles.json /assigned-numbers -/.prettierignore -/.prettierrc From 191b44210a38b76243bbc4ecef600d2b299948c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Rosi=C5=84ski?= Date: Sat, 23 Dec 2023 16:45:06 +0100 Subject: [PATCH 4/4] update lbs example --- CHANGELOG.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b5d303..71ee9ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. +## [1.1.3] - 2023-12-23 + +- Update LBS example + ## [1.1.2] - 2023-12-23 - Improve connection stability diff --git a/package.json b/package.json index 2d815e8..ed512a2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bluetooth-hci", - "version": "1.1.2", + "version": "1.1.3", "description": "Bluetooth HCI host implementation", "main": "lib/src/index.js", "types": "lib/src/index.d.ts",