Skip to content

Commit

Permalink
Bluetooth: Samples: Add Connection event callback sample
Browse files Browse the repository at this point in the history
The sample demonstrates how the connection event callback
feature can used to minimize latency between data sampling
and on air transmission.

Signed-off-by: Rubin Gerritsen <[email protected]>
  • Loading branch information
rugeGerritsen committed Jul 9, 2024
1 parent 0fbec6f commit 1c492c7
Show file tree
Hide file tree
Showing 9 changed files with 535 additions and 0 deletions.
17 changes: 17 additions & 0 deletions samples/bluetooth/conn_evt_cb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright (c) 2024 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(NONE)

zephyr_sources_ifdef(CONFIG_BT_CENTRAL src/central.c)
zephyr_sources_ifdef(CONFIG_BT_PERIPHERAL src/peripheral.c)

target_sources(app PRIVATE
src/main.c
)
136 changes: 136 additions & 0 deletions samples/bluetooth/conn_evt_cb/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
.. _ble_conn_evt_cb:

Bluetooth: Connection event callback
#####################################

.. contents::
:local:
:depth: 2

The Connection event callback sample demonstrates how to use the connection event prepare callback feature described in :ref:`ug_bt_conn_evt_cb`.
It uses the :ref:`latency_readme` and the :ref:`latency_client_readme` to showcase how this feature can be used to minimize the time between data sampling and data transmission.

Requirements
************

The sample supports the following development kits:

.. table-from-sample-yaml::

You can use any two of the development kits mentioned above and mix different development kits.

Additionally, the sample requires a connection to a computer with a serial terminal for each of the development kits.

Note that the feature involves triggering a (D)PPI task directly from the SoftDevice Controller link layer, and therefore it is expected that the application and SoftDevice Controller are running on the same core.

Building and running
********************
.. |sample path| replace:: :file:`samples/bluetooth/llpm`

.. include:: /includes/build_and_run.txt

Testing
=======

After programming the sample to both development kits, test it by performing the following steps:

1. Connect to both kits with a terminal emulator (for example, `nRF Connect Serial Terminal`_).
See :ref:`test_and_optimize` for the required settings and steps.
#. Reset both kits.
#. In one of the terminal emulators, type "c" to start the application on the connected board in the central role.
#. In the other terminal emulator, type "p" to start the application in the peripheral role.
#. Observe that latency measurements are printed in the terminals.

Sample output
=============

The result should look similar to the following output.

- For the central::

Starting connection event callback sample.
I: SoftDevice Controller build revision:
I: d6 da c7 ae 08 db 72 6f |......ro
I: 2a a3 26 49 2a 4d a8 b3 |*.&I*M..
I: 98 0e 07 7f |....
I: HW Platform: Nordic Semiconductor (0x0002)
I: HW Variant: nRF52x (0x0002)
I: Firmware: Standard Bluetooth controller (0x00) Version 214.51162 Build 1926957230
I: Identity: FA:BB:79:57:D6:45 (random)
I: HCI: version 5.4 (0x0d) revision 0x11fb, manufacturer 0x0059
I: LMP: version 5.4 (0x0d) subver 0x11fb
Choose device role - type c (central) or p (peripheral):
Central. Starting scanning
Scanning started
Device found: CF:99:32:A5:4B:11 (random) (RSSI -43)
Connected: CF:99:32:A5:4B:11 (random)
Service discovery completed
Latency: 1116 us, round trip latency: 51116 us
Latency: 1116 us, round trip latency: 51116 us
Latency: 1116 us, round trip latency: 51116 us
Latency: 1116 us, round trip latency: 51116 us
Latency: 1116 us, round trip latency: 51116 us
Latency: 1116 us, round trip latency: 51116 us
Latency: 1116 us, round trip latency: 51116 us
Latency: 1116 us, round trip latency: 51116 us
Latency: 1116 us, round trip latency: 51116 us

- For the peripheral::

Starting connection event callback sample.
I: SoftDevice Controller build revision:
I: d6 da c7 ae 08 db 72 6f |......ro
I: 2a a3 26 49 2a 4d a8 b3 |*.&I*M..
I: 98 0e 07 7f |....
I: HW Platform: Nordic Semiconductor (0x0002)
I: HW Variant: nRF52x (0x0002)
I: Firmware: Standard Bluetooth controller (0x00) Version 214.51162 Build 1926957230
I: Identity: CF:99:32:A5:4B:11 (random)
I: HCI: version 5.4 (0x0d) revision 0x11fb, manufacturer 0x0059
I: LMP: version 5.4 (0x0d) subver 0x11fb
Choose device role - type c (central) or p (peripheral):
Peripheral. Starting advertising
Advertising started
Connected: FA:BB:79:57:D6:45 (random)
Service discovery completed
Latency: 1666 us, round trip latency: 51666 us
Latency: 1086 us, round trip latency: 51086 us
Latency: 1116 us, round trip latency: 51116 us
Latency: 1116 us, round trip latency: 51116 us
Latency: 1086 us, round trip latency: 51086 us
Latency: 1666 us, round trip latency: 51666 us
Latency: 1086 us, round trip latency: 51086 us
Latency: 1116 us, round trip latency: 51116 us
Latency: 1116 us, round trip latency: 51116 us
Latency: 1116 us, round trip latency: 51116 us
Latency: 1666 us, round trip latency: 51666 us

Dependencies
*************

This sample uses the following |NCS| libraries:

* :ref:`latency_readme`
* :ref:`latency_client_readme`

This sample uses the following `sdk-nrfxlib`_ libraries:

* :ref:`nrfxlib:softdevice_controller`

In addition, it uses the following Zephyr libraries:

* :file:`include/console.h`
* :ref:`zephyr:kernel_api`:

* :file:`include/kernel.h`

* :file:`include/sys/printk.h`
* :file:`include/zephyr/types.h`
* :ref:`zephyr:bluetooth_api`:

* :file:`include/bluetooth/bluetooth.h`
* :file:`include/bluetooth/conn.h`
* :file:`include/bluetooth/gatt.h`
* :file:`include/bluetooth/scan.h`
* :file:`include/bluetooth/gatt_dm.h`
* :file:`include/bluetooth/conn_evt_prepare_cb.h`
25 changes: 25 additions & 0 deletions samples/bluetooth/conn_evt_cb/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# Copyright (c) 2024 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

CONFIG_NCS_SAMPLES_DEFAULTS=y

CONFIG_BT_DEVICE_NAME="Conn evt cb"
CONFIG_BT=y
CONFIG_BT_CONN_EVT_CB=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_CENTRAL=y
CONFIG_BT_MAX_CONN=2
CONFIG_BT_CTLR_SDC_PERIPHERAL_COUNT=1
CONFIG_BT_LATENCY=y
CONFIG_BT_LATENCY_CLIENT=y
CONFIG_BT_GATT_DM=y

CONFIG_CONSOLE=y
CONFIG_CONSOLE_SUBSYS=y
CONFIG_CONSOLE_HANDLER=y
CONFIG_CONSOLE_GETCHAR=y

CONFIG_HEAP_MEM_POOL_SIZE=2048
13 changes: 13 additions & 0 deletions samples/bluetooth/conn_evt_cb/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sample:
description: Connection event callback sample
name: Connection event callback
tests:
sample.bluetooth.conn_evt_cb:
sysbuild: true
build_only: true
integration_platforms:
- nrf52dk/nrf52832
- nrf52840dk/nrf52840
- nrf5340dk/nrf5340/cpunet
platform_allow: nrf52dk/nrf52832 nrf52840dk/nrf52840 nrf5340dk/nrf5340/cpunet
tags: bluetooth ci_build sysbuild
78 changes: 78 additions & 0 deletions samples/bluetooth/conn_evt_cb/src/central.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>

#define ADV_NAME_STR_MAX_LEN (sizeof(CONFIG_BT_DEVICE_NAME))

void scan_start(void);

static bool adv_data_parse_cb(struct bt_data *data, void *user_data)
{
char *name = user_data;
uint8_t len;

switch (data->type) {
case BT_DATA_NAME_SHORTENED:
case BT_DATA_NAME_COMPLETE:
len = MIN(data->data_len, ADV_NAME_STR_MAX_LEN - 1);
memcpy(name, data->data, len);
name[len] = '\0';
return false;
default:
return true;
}
}

static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
struct net_buf_simple *ad)
{
char name_str[ADV_NAME_STR_MAX_LEN] = {0};
char addr_str[BT_ADDR_LE_STR_LEN];
int err;

/* We're only interested in connectable events */
if (type != BT_GAP_ADV_TYPE_ADV_IND &&
type != BT_GAP_ADV_TYPE_ADV_DIRECT_IND) {
return;
}

bt_data_parse(ad, adv_data_parse_cb, name_str);

if (strncmp(name_str, CONFIG_BT_DEVICE_NAME, ADV_NAME_STR_MAX_LEN) != 0) {
return;
}

bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
printk("Device found: %s (RSSI %d)\n", addr_str, rssi);

if (bt_le_scan_stop()) {
return;
}

struct bt_conn *unused_conn;

err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
BT_LE_CONN_PARAM_DEFAULT, &unused_conn);
if (err) {
printk("Create conn to %s failed (%d)\n", addr_str, err);
scan_start();
}
}

void scan_start(void)
{
int err;

err = bt_le_scan_start(BT_LE_SCAN_ACTIVE, device_found);
if (err) {
printk("Scanning failed to start (err %d)\n", err);
return;
}

printk("Scanning started\n");
}
Loading

0 comments on commit 1c492c7

Please sign in to comment.