1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/sys/printk.h>
8 #include <zephyr/bluetooth/bluetooth.h>
9
10 /* Maximum supported AD data length, use a value supported by the Controller,
11 * Bluetooth Core Specification define minimum of 31 bytes will be supported by
12 * all Controllers, can be a maximum of 1650 bytes when supported.
13 */
14 #if defined(CONFIG_BT_CTLR_ADV_DATA_LEN_MAX)
15 #define BT_AD_DATA_LEN_MAX CONFIG_BT_CTLR_ADV_DATA_LEN_MAX
16 #else
17 #define BT_AD_DATA_LEN_MAX 31U
18 #endif
19
20 /* Size of AD data format length field in octets */
21 #define BT_AD_DATA_FORMAT_LEN_SIZE 1U
22
23 /* Size of AD data format type field in octets */
24 #define BT_AD_DATA_FORMAT_TYPE_SIZE 1U
25
26 /* Maximum value of AD data format length field (8-bit) */
27 #define BT_AD_DATA_FORMAT_LEN_MAX 255U
28
29 /* Device name length, size minus one null character */
30 #define BT_DEVICE_NAME_LEN (sizeof(CONFIG_BT_DEVICE_NAME) - 1U)
31
32 /* Device name length in AD data format, 2 bytes for length and type overhead */
33 #define BT_DEVICE_NAME_AD_DATA_LEN (BT_AD_DATA_FORMAT_LEN_SIZE + \
34 BT_AD_DATA_FORMAT_TYPE_SIZE + \
35 BT_DEVICE_NAME_LEN)
36
37 /* Maximum manufacturer data length, considering ad data format overhead and
38 * the included device name in ad data format.
39 */
40 #define BT_MFG_DATA_LEN_MAX (MIN((BT_AD_DATA_FORMAT_LEN_MAX - \
41 BT_AD_DATA_FORMAT_TYPE_SIZE), \
42 (BT_AD_DATA_LEN_MAX - \
43 BT_AD_DATA_FORMAT_LEN_SIZE - \
44 BT_AD_DATA_FORMAT_TYPE_SIZE)))
45 #define BT_MFG_DATA_LEN (MIN(BT_MFG_DATA_LEN_MAX, \
46 (BT_AD_DATA_LEN_MAX - \
47 BT_AD_DATA_FORMAT_LEN_SIZE - \
48 BT_AD_DATA_FORMAT_TYPE_SIZE - \
49 BT_DEVICE_NAME_AD_DATA_LEN)))
50 /*
51 * Datalength is an integer, so BT_MFG_DATA_LEN can not be larger than 255.
52 * To ensure that we need to chain PDUs we therefore add manufacturer data
53 * twice when chaining is enabled
54 */
55 static uint8_t mfg_data[BT_MFG_DATA_LEN] = { 0xFF, 0xFF, };
56
57 static const struct bt_data ad[] = {
58 BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, sizeof(mfg_data)),
59 #if CONFIG_BT_CTLR_ADV_DATA_LEN_MAX > 255
60 BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, sizeof(mfg_data)),
61 #endif
62 BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
63 };
64
65 static struct bt_le_ext_adv *adv[CONFIG_BT_EXT_ADV_MAX_ADV_SET];
66
broadcaster_multiple(void)67 int broadcaster_multiple(void)
68 {
69 struct bt_le_adv_param adv_param = {
70 .id = BT_ID_DEFAULT,
71 .sid = 0U, /* Supply unique SID when creating advertising set */
72 .secondary_max_skip = 0U,
73 .options = BT_LE_ADV_OPT_EXT_ADV,
74 .interval_min = BT_GAP_ADV_FAST_INT_MIN_2,
75 .interval_max = BT_GAP_ADV_FAST_INT_MAX_2,
76 .peer = NULL,
77 };
78 int err;
79
80 for (int index = 0; index < CONFIG_BT_EXT_ADV_MAX_ADV_SET; index++) {
81 /* Use advertising set instance index as SID */
82 adv_param.sid = index;
83
84 /* Create a non-connectable advertising set */
85 err = bt_le_ext_adv_create(&adv_param, NULL, &adv[index]);
86 if (err) {
87 printk("Failed to create advertising set %d (err %d)\n",
88 index, err);
89 return err;
90 }
91
92 /* Set extended advertising data */
93 err = bt_le_ext_adv_set_data(adv[index], ad, ARRAY_SIZE(ad),
94 NULL, 0);
95 if (err) {
96 printk("Failed to set advertising data for set %d "
97 "(err %d)\n", index, err);
98 return err;
99 }
100
101 /* Start extended advertising set */
102 err = bt_le_ext_adv_start(adv[index],
103 BT_LE_EXT_ADV_START_DEFAULT);
104 if (err) {
105 printk("Failed to start extended advertising set %d "
106 "(err %d)\n", index, err);
107 return err;
108 }
109
110 printk("Started Extended Advertising Set %d.\n", index);
111 }
112
113 return 0;
114 }
115