Commit 4fd7eb21 authored by Vinayak Kariappa Chettimada's avatar Vinayak Kariappa Chettimada Committed by Carles Cufi
Browse files

samples: Bluetooth: Multiple Broadcaster



Added Extended Advertising multiple advertising set
broadcaster sample.

Signed-off-by: default avatarVinayak Kariappa Chettimada <vich@nordicsemi.no>
parent ce0f65e7
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(broadcaster_multiple)

target_sources(app PRIVATE src/main.c)
+36 −0
Original line number Diff line number Diff line
.. _bluetooth-broadcaster-multiple-sample:

Bluetooth: Multiple Broadcaster
###############################

Overview
********

A simple application demonstrating the Bluetooth Low Energy Broadcaster that
uses multiple advertising sets functionality.

This sample advertises two non-connectable non-scannable advertising sets with
two different SID. Number of advertising sets can be increased by updating the
`CONFIG_BT_EXT_ADV_MAX_ADV_SET` value in the project configuration file.

When building this sample combined with a Bluetooth LE Controller, the
advertising data length can be increased from the default 31 bytes by updating
the Controller's `CONFIG_BT_CTLR_ADV_DATA_LEN_MAX` value. The size of the
manufacturer data is calculated to maximize the use of supported AD data length.

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

* A board with Bluetooth Low Energy with Extended Advertising support.

Building and Running
********************

This sample can be found under
:zephyr_file:`samples/bluetooth/broadcaster_multiple` in the Zephyr tree.

To test this sample use the Observer sample with Extended Scanning enabled,
found under
:zephyr_file:`samples/bluetooth/observer` in the Zephyr tree.

See :ref:`Bluetooth samples section <bluetooth-samples>` for details.
+23 −0
Original line number Diff line number Diff line
CONFIG_BT=y
CONFIG_BT_BROADCASTER=y
CONFIG_BT_EXT_ADV=y
CONFIG_BT_EXT_ADV_MAX_ADV_SET=2
CONFIG_BT_DEVICE_NAME="Broadcaster Multiple"

CONFIG_BT_DEBUG_LOG=y

# Zephyr Bluetooth LE Controller will need to use chain PDUs when AD data
# length > 191 bytes
# - 31 bytes will use 22 bytes for the default name in this sample plus 9 bytes
#   for manufacturer data
# - 191 bytes will use 22 bytes for the default name in this sample plus 169
#   bytes for manufacturer data
# - 277 bytes will use 22 bytes for the default name in this sample plus 255
#   bytes for manufacturer data
# CONFIG_BT_CTLR_ADV_DATA_LEN_MAX=192

# Increase Advertising PDU buffers to number of advertising sets times the
# number of chain PDUs per advertising set when using Zephyr Bluetooth LE
# Controller
# CONFIG_BT_CTLR_ADVANCED_FEATURES=y
# CONFIG_BT_CTLR_ADV_DATA_BUF_MAX=2
+7 −0
Original line number Diff line number Diff line
sample:
  name: Bluetooth Multiple Extended Advertising Broadcaster
tests:
  sample.bluetooth.multiple_broadcast:
    harness: bluetooth
    platform_allow: qemu_cortex_m3 qemu_x86 nrf51dk_nrf51422 nrf52_bsim nrf52dk_nrf52832
    tags: bluetooth
+115 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2022 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/sys/printk.h>
#include <zephyr/bluetooth/bluetooth.h>

/* Maximum supported AD data length, use a value supported by the Controller,
 * Bluetooth Core Specification define minimum of 31 bytes will be supported by
 * all Controllers, can be a maximum of 1650 bytes when supported.
 */
#if defined(CONFIG_BT_CTLR_ADV_DATA_LEN_MAX)
#define BT_AD_DATA_LEN_MAX CONFIG_BT_CTLR_ADV_DATA_LEN_MAX
#else
#define BT_AD_DATA_LEN_MAX 31U
#endif

/* Size of AD data format length field in octets */
#define BT_AD_DATA_FORMAT_LEN_SIZE 1U

/* Size of AD data format type field in octets */
#define BT_AD_DATA_FORMAT_TYPE_SIZE 1U

/* Maximum value of AD data format length field (8-bit) */
#define BT_AD_DATA_FORMAT_LEN_MAX 255U

/* Device name length, size minus one null character */
#define BT_DEVICE_NAME_LEN (sizeof(CONFIG_BT_DEVICE_NAME) - 1U)

/* Device name length in AD data format, 2 bytes for length and type overhead */
#define BT_DEVICE_NAME_AD_DATA_LEN (BT_AD_DATA_FORMAT_LEN_SIZE + \
				    BT_AD_DATA_FORMAT_TYPE_SIZE + \
				    BT_DEVICE_NAME_LEN)

/* Maximum manufacturer data length, considering ad data format overhead and
 * the included device name in ad data format.
 */
#define BT_MFG_DATA_LEN_MAX       (MIN((BT_AD_DATA_FORMAT_LEN_MAX - \
					BT_AD_DATA_FORMAT_TYPE_SIZE), \
				       (BT_AD_DATA_LEN_MAX - \
					BT_AD_DATA_FORMAT_LEN_SIZE - \
					BT_AD_DATA_FORMAT_TYPE_SIZE)))
#define BT_MFG_DATA_LEN           (MIN(BT_MFG_DATA_LEN_MAX, \
				       (BT_AD_DATA_LEN_MAX - \
					BT_AD_DATA_FORMAT_LEN_SIZE - \
					BT_AD_DATA_FORMAT_TYPE_SIZE - \
					BT_DEVICE_NAME_AD_DATA_LEN)))

static uint8_t mfg_data[BT_MFG_DATA_LEN] = { 0xFF, 0xFF, };

static const struct bt_data ad[] = {
	BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, sizeof(mfg_data)),
};

static struct bt_le_ext_adv *adv[CONFIG_BT_EXT_ADV_MAX_ADV_SET];

void main(void)
{
	struct bt_le_adv_param adv_param = {
		.id = BT_ID_DEFAULT,
		.sid = 0U, /* Supply unique SID when creating advertising set */
		.secondary_max_skip = 0U,
		.options = (BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_USE_NAME),
		.interval_min = BT_GAP_ADV_FAST_INT_MIN_2,
		.interval_max = BT_GAP_ADV_FAST_INT_MAX_2,
		.peer = NULL,
	};
	int err;

	printk("Starting Multiple Broadcaster Demo\n");

	/* Initialize the Bluetooth Subsystem */
	err = bt_enable(NULL);
	if (err) {
		printk("Bluetooth init failed (err %d)\n", err);
		return;
	}

	for (int index = 0; index < CONFIG_BT_EXT_ADV_MAX_ADV_SET; index++) {
		/* Use advertising set instance index as SID */
		adv_param.sid = index;

		/* Create a non-connectable non-scannable advertising set */
		err = bt_le_ext_adv_create(&adv_param, NULL, &adv[index]);
		if (err) {
			printk("Failed to create advertising set %d (err %d)\n",
			       index, err);
			return;
		}

		/* Set extended advertising data */
		err = bt_le_ext_adv_set_data(adv[index], ad, ARRAY_SIZE(ad),
					     NULL, 0);
		if (err) {
			printk("Failed to set advertising data for set %d "
			       "(err %d)\n", index, err);
			return;
		}

		/* Start extended advertising set */
		err = bt_le_ext_adv_start(adv[index],
					  BT_LE_EXT_ADV_START_DEFAULT);
		if (err) {
			printk("Failed to start extended advertising set %d "
			       "(err %d)\n", index, err);
			return;
		}

		printk("Started Extended Advertising Set %d.\n", index);
	}

	printk("Exiting %s thread.\n", __func__);
}