Skip to content

Commit 2b9516f

Browse files
Advertiser API updated in each code example to comply with Bluetooth SDK v4.0
1 parent aaf4d1f commit 2b9516f

File tree

67 files changed

+946
-834
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+946
-834
lines changed

advertising/adv_or_scan_response_constructor/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Description ##
44

5-
In Silicon Labs Bluetooth SDK v3.x you can use the API [sl_bt_advertiser_set_data()](https://docs.silabs.com/bluetooth/3.1/group-sl-bt-advertiser#ga706aeaf60049280fe9cc256f20ad4113) to set the advertisement and scan response data. Data has a standard format, whereas the data passed to above API is raw data uint8array. This example focuses on adding a middle layer between user application and the Bluetooth stack API to set the advertisement payload, which makes the payload more straightforward, visible, and easier to understand.
5+
In Silicon Labs Bluetooth SDK v3.x you can use the API [sl_bt_legacy_advertiser_set_data()](https://docs.silabs.com/bluetooth/3.3/a00043#gacfa5efdd898d261318b013713dda15ff) and [sl_bt_extended_advertiser_set_data()](https://docs.silabs.com/bluetooth/3.3/a00044#gac039eff642bda360752cfe15a12a4801) to set the advertisement and scan response data. Data has a standard format, whereas the data passed to above API is raw data uint8array. This example focuses on adding a middle layer between user application and the Bluetooth stack API to set the advertisement payload, which makes the payload more straightforward, visible, and easier to understand.
66

77
To learn more about the payload of Bluetooth advertisement, see [Bluetooth Advertising Data Basics](https://docs.silabs.com/bluetooth/latest/general/adv-and-scanning/bluetooth-adv-data-basics).
88

@@ -56,7 +56,9 @@ GSDK v3.1.1
5656
Then enable *Virtual COM UART* under its configuration
5757
![board control configure](images/enable_vir_com.png)
5858

59-
- Install the **Log** component (found under Bluetooth > Utility group)
59+
- Install the **Legacy Advertising** and **Extended Advertising** components, if they are not yet installed
60+
61+
- Install the **Log** component (found under Application > Utility group)
6062

6163
3. Replace the *app.c* and *app.h* file in the project with the provided *app.c*, *app.h*
6264

advertising/adv_or_scan_response_constructor/src/app.c

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
*
1616
******************************************************************************/
1717
#include "em_common.h"
18-
#include "sl_app_assert.h"
18+
#include "app_assert.h"
1919
#include "sl_bluetooth.h"
2020
#include "gatt_db.h"
2121
#include "app.h"
22-
#include "sl_app_log.h"
22+
#include "app_log.h"
2323

2424
// The advertising set handle allocated from Bluetooth stack.
2525
static uint8_t advertising_set_handle = 0xff;
26+
static uint8_t ext_adv = 0;
2627

2728
/**************************************************************************//**
2829
* Application Init.
@@ -68,7 +69,7 @@ void sl_bt_on_event(sl_bt_msg_t *evt)
6869

6970
// Extract unique ID from BT Address.
7071
sc = sl_bt_system_get_identity_address(&address, &address_type);
71-
sl_app_assert(sc == SL_STATUS_OK,
72+
app_assert(sc == SL_STATUS_OK,
7273
"[E: 0x%04x] Failed to get Bluetooth address\n",
7374
(int)sc);
7475

@@ -86,13 +87,13 @@ void sl_bt_on_event(sl_bt_msg_t *evt)
8687
0,
8788
sizeof(system_id),
8889
system_id);
89-
sl_app_assert(sc == SL_STATUS_OK,
90+
app_assert(sc == SL_STATUS_OK,
9091
"[E: 0x%04x] Failed to write attribute\n",
9192
(int)sc);
9293

9394
// Create an advertising set.
9495
sc = sl_bt_advertiser_create_set(&advertising_set_handle);
95-
sl_app_assert(sc == SL_STATUS_OK,
96+
app_assert(sc == SL_STATUS_OK,
9697
"[E: 0x%04x] Failed to create advertising set\n",
9798
(int)sc);
9899
// Set advertising interval to 100ms.
@@ -102,16 +103,19 @@ void sl_bt_on_event(sl_bt_msg_t *evt)
102103
160, // max. adv. interval (milliseconds * 1.6)
103104
0, // adv. duration
104105
0); // max. num. adv. events
105-
sl_app_assert(sc == SL_STATUS_OK,
106+
app_assert(sc == SL_STATUS_OK,
106107
"[E: 0x%04x] Failed to set advertising timing\n",
107108
(int)sc);
108109
demo_setup_adv(advertising_set_handle);
109110
// Start general advertising and enable connections.
110-
sc = sl_bt_advertiser_start(
111+
if(ext_adv) sc = sl_bt_extended_advertiser_start(
111112
advertising_set_handle,
112-
advertiser_user_data,
113-
advertiser_connectable_scannable);
114-
sl_app_assert(sc == SL_STATUS_OK,
113+
sl_bt_extended_advertiser_connectable,
114+
0);
115+
else sc = sl_bt_legacy_advertiser_start(
116+
advertising_set_handle,
117+
sl_bt_legacy_advertiser_connectable);
118+
app_assert(sc == SL_STATUS_OK,
115119
"[E: 0x%04x] Failed to start advertising\n",
116120
(int)sc);
117121
break;
@@ -125,11 +129,14 @@ void sl_bt_on_event(sl_bt_msg_t *evt)
125129
// This event indicates that a connection was closed.
126130
case sl_bt_evt_connection_closed_id:
127131
// Restart advertising after client has disconnected.
128-
sc = sl_bt_advertiser_start(
132+
if(ext_adv) sc = sl_bt_extended_advertiser_start(
133+
advertising_set_handle,
134+
sl_bt_extended_advertiser_connectable,
135+
0);
136+
else sc = sl_bt_legacy_advertiser_start(
129137
advertising_set_handle,
130-
advertiser_user_data,
131-
advertiser_connectable_scannable);
132-
sl_app_assert(sc == SL_STATUS_OK,
138+
sl_bt_legacy_advertiser_connectable);
139+
app_assert(sc == SL_STATUS_OK,
133140
"[E: 0x%04x] Failed to start advertising\n",
134141
(int)sc);
135142
break;
@@ -153,7 +160,6 @@ void demo_setup_adv(uint8_t handle)
153160
sl_status_t sc;
154161
const uint8_t flag_data = 0x6;
155162
const uint8_t local_name_data[] = "AdvC";
156-
const uint8_t *service_uuid = bg_gattdb_data.uuidtable_128;
157163

158164
/* https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers - To get your company ID*/
159165
/* Below is an example to construct your manufacturer specific data with payload set to "KBA - Adv Constructor" */
@@ -176,40 +182,34 @@ void demo_setup_adv(uint8_t handle)
176182
.data = local_name_data
177183
},
178184
/* Element 2 */
179-
{
180-
.ad_type = more_128_uuids,
181-
.len = 16,
182-
.data = service_uuid
183-
},
184-
/* Element 3 */
185185
{
186186
.ad_type = manufacturer_specific_data,
187187
.len = sizeof(manu_data),
188188
.data = manu_data
189189
}
190190
};
191191

192-
/* Set up advertisement payload with the first 3 elements - 0, 1 and 2 */
192+
/* Set up advertisement payload with the first 2 elements */
193193
adv_t adv = {
194194
.adv_handle = handle,
195195
.adv_packet_type = adv_packet,
196-
.ele_num = 3,
196+
.ele_num = 2,
197197
.p_element = ad_elements
198198
};
199-
sc = construct_adv(&adv, 0);
199+
sc = construct_adv(&adv, ext_adv);
200200
if (sc != SL_STATUS_OK) {
201-
sl_app_log("Check error here [%s:%u]\n", __FILE__, __LINE__);
201+
app_log("Check error here [%s:%u]\n", __FILE__, __LINE__);
202202
}
203203

204-
/* Set up scan response payload with the last (4th) element */
204+
/* Set up scan response payload with the last (3th) element */
205205
adv.adv_handle = handle;
206206
adv.adv_packet_type = scan_rsp;
207207
adv.ele_num = 1;
208-
adv.p_element = &ad_elements[3];
208+
adv.p_element = &ad_elements[2];
209209

210-
sc = construct_adv(&adv, 0);
210+
sc = construct_adv(&adv, ext_adv);
211211
if (sc != SL_STATUS_OK) {
212-
sl_app_log("Check error here [%s:%u]\n", __FILE__, __LINE__);
212+
app_log("Check error here [%s:%u]\n", __FILE__, __LINE__);
213213
}
214214
}
215215

@@ -220,20 +220,20 @@ sl_status_t construct_adv(const adv_t *adv, uint8_t ext_adv)
220220
sl_status_t sc;
221221

222222
if (!adv) {
223-
sl_app_log("input param null, aborting.\n");
223+
app_log("input param null, aborting.\n");
224224
return SL_STATUS_NULL_POINTER;
225225
}
226226

227227
for (i = 0; i < adv->ele_num; i++) {
228228
amout_bytes += adv->p_element[i].len + 2;
229229
if (!adv->p_element[i].data) {
230-
sl_app_log("adv unit payload data null, aborting.\n");
230+
app_log("adv unit payload data null, aborting.\n");
231231
return SL_STATUS_NULL_POINTER;
232232
}
233233
}
234234
if (((amout_bytes > MAX_ADV_DATA_LENGTH) && !ext_adv)
235235
|| ((amout_bytes > MAX_EXTENDED_ADV_LENGTH))) {
236-
sl_app_log("Adv data too long [length = %d], aborting.\n", amout_bytes);
236+
app_log("Adv data too long [length = %d], aborting.\n", amout_bytes);
237237
return SL_STATUS_BT_CTRL_PACKET_TOO_LONG;
238238
}
239239

@@ -244,8 +244,9 @@ sl_status_t construct_adv(const adv_t *adv, uint8_t ext_adv)
244244
memcpy(buf + amout_bytes, adv->p_element[i].data, adv->p_element[i].len);
245245
amout_bytes += adv->p_element[i].len;
246246
}
247-
sc = sl_bt_advertiser_set_data(adv->adv_handle, adv->adv_packet_type, amout_bytes, buf);
248-
sl_app_assert(sc == SL_STATUS_OK,
247+
if(ext_adv) sc = sl_bt_extended_advertiser_set_data(adv->adv_handle, amout_bytes, buf);
248+
else sc = sl_bt_legacy_advertiser_set_data(adv->adv_handle, adv->adv_packet_type, amout_bytes, buf);
249+
app_assert(sc == SL_STATUS_OK,
249250
"[E: 0x%04x] Failed to set advertising data\n",
250251
(int)sc);
251252
return SL_STATUS_OK;

advertising/adv_or_scan_response_constructor/src/app.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,11 @@ typedef struct {
8484
*
8585
* - <b>0:</b> Advertising packets
8686
* - <b>1:</b> Scan response packets
87-
* - <b>8:</b> Periodic advertising packets
8887
*
8988
*/
9089
typedef enum {
9190
adv_packet = 0,
92-
scan_rsp = 1,
93-
periodic_adv = 8
91+
scan_rsp = 1
9492
} adv_packet_type_t;
9593

9694
/**
@@ -110,14 +108,14 @@ typedef struct {
110108
/**
111109
* @brief construct_adv - set the corresponding advertising data to the stack.
112110
* Do not forget to modify the second parameter of
113-
* sl_bt_advertiser_start function to advertiser_user_data
111+
* sl_bt_legacy_advertiser_start function to advertiser_user_data
114112
*
115113
* @param adv - pointer to the @ref{adv_t} structure
116114
*
117115
* @param ext_adv - enable or disable the extended advertising. If extended
118116
* advertising is disabled, the maximum data length for advertising or scan
119117
* response is 31 bytes, otherwise, it's 253 or 191 bytes depending on the
120-
* advertising type. @ref{sl_bt_advertiser_set_data} API for more details.
118+
* advertising type. @ref{sl_bt_extended_advertiser_set_data} API for more details.
121119
*
122120
* @return status code base on the specific error that comes along with a
123121
* message for user to understand it

advertising/advertising_and_scanning_with_le_coded_phy/README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ The sync info field is used for Periodic Advertisement synchronization, not disc
3737
Another feature of these extended advertisements is that it is possible to specify the PHY used for the primary and secondary advertisements separately. To set the PHY used for advertisements, the following API function is used:
3838

3939
```C
40-
sl_bt_advertiser_set_phy(advertising_set_handle,
41-
gap_coded_phy,
42-
gap_coded_phy);
40+
sl_bt_extended_advertiser_set_phy(advertising_set_handle,
41+
gap_coded_phy,
42+
gap_coded_phy);
4343
```
4444
4545
The parameters indicate the advertisement set to be used, the PHY for primary advertisements, and the PHY used for secondary advertisements.
@@ -49,9 +49,9 @@ Advertisements on the coded PHY do not support active scanning. This means that
4949
To start advertising, the following API function is used:
5050
5151
```C
52-
sc = sl_bt_advertiser_start(advertising_set_handle,
53-
advertiser_general_discoverable,
54-
advertiser_connectable_non_scannable);
52+
sc = sl_bt_extended_advertiser_start(advertising_set_handle,
53+
connect,
54+
flags);
5555
```
5656

5757
Remaining advertising parameters can be set as they would with the traditional 1 Mbps PHY.
@@ -91,7 +91,9 @@ To test the example, you need to setup two radio boards as advertiser and scanne
9191
- Install **Retarget STDIO** component:
9292
![board control configure](images/add_log_3.png)
9393

94-
- Install the **Log** component (found under Bluetooth > Utility group)
94+
- Install the **Extended Advertising** component, if it is not yet installed
95+
96+
- Install the **Log** component (found under Application > Utility group)
9597
![add log driver](images/add_log_4.png)
9698

9799
4. Import the GATT configuration:
10.1 KB
Loading

advertising/advertising_and_scanning_with_le_coded_phy/src/advertiser/app.c

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
*
1616
******************************************************************************/
1717
#include "em_common.h"
18-
#include "sl_app_assert.h"
18+
#include "app_assert.h"
1919
#include "sl_bluetooth.h"
2020
#include "gatt_db.h"
2121
#include "app.h"
2222

2323
#include "sl_bt_api.h"
24-
#include "sl_app_log.h"
24+
#include "app_log.h"
2525

2626
// The advertising set handle allocated from Bluetooth stack.
2727
static uint8_t advertising_set_handle = 0xff;
@@ -70,11 +70,11 @@ void sl_bt_on_event(sl_bt_msg_t *evt)
7070

7171
// Extract unique ID from BT Address.
7272
sc = sl_bt_system_get_identity_address(&address, &address_type);
73-
sl_app_assert(sc == SL_STATUS_OK,
73+
app_assert(sc == SL_STATUS_OK,
7474
"[E: 0x%04x] Failed to get Bluetooth address\n",
7575
(int)sc);
7676

77-
sl_app_log("Bluetooth %s address: %02X:%02X:%02X:%02X:%02X:%02X\n",
77+
app_log("Bluetooth %s address: %02X:%02X:%02X:%02X:%02X:%02X\n",
7878
address_type ? "static random" : "public device",
7979
address.addr[5],
8080
address.addr[4],
@@ -97,64 +97,58 @@ void sl_bt_on_event(sl_bt_msg_t *evt)
9797
0,
9898
sizeof(system_id),
9999
system_id);
100-
sl_app_assert(sc == SL_STATUS_OK,
100+
app_assert(sc == SL_STATUS_OK,
101101
"[E: 0x%04x] Failed to write attribute\n",
102102
(int)sc);
103103

104104
// Create an advertising set.
105105
sc = sl_bt_advertiser_create_set(&advertising_set_handle);
106-
sl_app_assert(sc == SL_STATUS_OK,
106+
app_assert(sc == SL_STATUS_OK,
107107
"[E: 0x%04x] Failed to create advertising set\n",
108108
(int)sc);
109109

110110
// disable report scan request
111111
sc = sl_bt_advertiser_set_report_scan_request(advertising_set_handle,
112112
0);
113-
sl_app_assert(sc == SL_STATUS_OK,
113+
app_assert(sc == SL_STATUS_OK,
114114
"[E: 0x%04x] Failed to disable report scan request\n",
115115
(int)sc);
116116

117117
// set phy
118-
sc = sl_bt_advertiser_set_phy(advertising_set_handle,
119-
gap_coded_phy,
120-
gap_coded_phy);
121-
sl_app_assert(sc == SL_STATUS_OK,
118+
sc = sl_bt_extended_advertiser_set_phy(advertising_set_handle,
119+
sl_bt_gap_phy_coded,
120+
sl_bt_gap_phy_coded);
121+
app_assert(sc == SL_STATUS_OK,
122122
"[E: 0x%04x] Failed to set phy\n",
123123
(int)sc);
124124

125-
// Do not use legacy PDU - use extended advertisement instead.
126-
sc = sl_bt_advertiser_clear_configuration(advertising_set_handle,
127-
1);
128-
sl_app_assert(sc == SL_STATUS_OK,
129-
"[E: 0x%04x] Failed to clear config\n",
130-
(int)sc);
131-
132125
// Set advertising interval to 100ms.
133126
sc = sl_bt_advertiser_set_timing(
134127
advertising_set_handle,
135128
160, // min. adv. interval (milliseconds * 1.6)
136129
160, // max. adv. interval (milliseconds * 1.6)
137130
0, // adv. duration
138131
0); // max. num. adv. events
139-
sl_app_assert(sc == SL_STATUS_OK,
132+
app_assert(sc == SL_STATUS_OK,
140133
"[E: 0x%04x] Failed to set advertising timing\n",
141134
(int)sc);
142135

143136
// all primary advertising channels
144137
sc = sl_bt_advertiser_set_channel_map(advertising_set_handle,
145138
7);
146-
sl_app_assert(sc == SL_STATUS_OK,
139+
app_assert(sc == SL_STATUS_OK,
147140
"[E: 0x%04x] Failed to set channel map\n",
148141
(int)sc);
149142

150143
// Start general advertising and enable connections.
151-
sc = sl_bt_advertiser_start(
144+
sc = sl_bt_extended_advertiser_start(
152145
advertising_set_handle,
153-
advertiser_general_discoverable,
154-
advertiser_connectable_non_scannable);
155-
sl_app_assert(sc == SL_STATUS_OK,
146+
sl_bt_extended_advertiser_connectable,
147+
0);
148+
app_assert(sc == SL_STATUS_OK,
156149
"[E: 0x%04x] Failed to start advertising\n",
157150
(int)sc);
151+
app_log("Start advertising ...\n");
158152
break;
159153

160154
// -------------------------------
@@ -166,11 +160,11 @@ void sl_bt_on_event(sl_bt_msg_t *evt)
166160
// This event indicates that a connection was closed.
167161
case sl_bt_evt_connection_closed_id:
168162
// Restart advertising after client has disconnected.
169-
sc = sl_bt_advertiser_start(
163+
sc = sl_bt_extended_advertiser_start(
170164
advertising_set_handle,
171-
advertiser_general_discoverable,
172-
advertiser_connectable_non_scannable);
173-
sl_app_assert(sc == SL_STATUS_OK,
165+
sl_bt_extended_advertiser_connectable,
166+
0);
167+
app_assert(sc == SL_STATUS_OK,
174168
"[E: 0x%04x] Failed to start advertising\n",
175169
(int)sc);
176170
break;

0 commit comments

Comments
 (0)