Skip to content

Commit

Permalink
Advertiser API updated in each code example to comply with Bluetooth …
Browse files Browse the repository at this point in the history
…SDK v4.0
  • Loading branch information
attila-hegedus-silabs authored Jun 14, 2022
1 parent aaf4d1f commit 2b9516f
Show file tree
Hide file tree
Showing 67 changed files with 946 additions and 834 deletions.
6 changes: 4 additions & 2 deletions advertising/adv_or_scan_response_constructor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Description ##

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.
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.

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).

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

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

- Install the **Log** component (found under Application > Utility group)

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

Expand Down
69 changes: 35 additions & 34 deletions advertising/adv_or_scan_response_constructor/src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*
******************************************************************************/
#include "em_common.h"
#include "sl_app_assert.h"
#include "app_assert.h"
#include "sl_bluetooth.h"
#include "gatt_db.h"
#include "app.h"
#include "sl_app_log.h"
#include "app_log.h"

// The advertising set handle allocated from Bluetooth stack.
static uint8_t advertising_set_handle = 0xff;
static uint8_t ext_adv = 0;

/**************************************************************************//**
* Application Init.
Expand Down Expand Up @@ -68,7 +69,7 @@ void sl_bt_on_event(sl_bt_msg_t *evt)

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

Expand All @@ -86,13 +87,13 @@ void sl_bt_on_event(sl_bt_msg_t *evt)
0,
sizeof(system_id),
system_id);
sl_app_assert(sc == SL_STATUS_OK,
app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to write attribute\n",
(int)sc);

// Create an advertising set.
sc = sl_bt_advertiser_create_set(&advertising_set_handle);
sl_app_assert(sc == SL_STATUS_OK,
app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to create advertising set\n",
(int)sc);
// Set advertising interval to 100ms.
Expand All @@ -102,16 +103,19 @@ void sl_bt_on_event(sl_bt_msg_t *evt)
160, // max. adv. interval (milliseconds * 1.6)
0, // adv. duration
0); // max. num. adv. events
sl_app_assert(sc == SL_STATUS_OK,
app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to set advertising timing\n",
(int)sc);
demo_setup_adv(advertising_set_handle);
// Start general advertising and enable connections.
sc = sl_bt_advertiser_start(
if(ext_adv) sc = sl_bt_extended_advertiser_start(
advertising_set_handle,
advertiser_user_data,
advertiser_connectable_scannable);
sl_app_assert(sc == SL_STATUS_OK,
sl_bt_extended_advertiser_connectable,
0);
else sc = sl_bt_legacy_advertiser_start(
advertising_set_handle,
sl_bt_legacy_advertiser_connectable);
app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to start advertising\n",
(int)sc);
break;
Expand All @@ -125,11 +129,14 @@ void sl_bt_on_event(sl_bt_msg_t *evt)
// This event indicates that a connection was closed.
case sl_bt_evt_connection_closed_id:
// Restart advertising after client has disconnected.
sc = sl_bt_advertiser_start(
if(ext_adv) sc = sl_bt_extended_advertiser_start(
advertising_set_handle,
sl_bt_extended_advertiser_connectable,
0);
else sc = sl_bt_legacy_advertiser_start(
advertising_set_handle,
advertiser_user_data,
advertiser_connectable_scannable);
sl_app_assert(sc == SL_STATUS_OK,
sl_bt_legacy_advertiser_connectable);
app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to start advertising\n",
(int)sc);
break;
Expand All @@ -153,7 +160,6 @@ void demo_setup_adv(uint8_t handle)
sl_status_t sc;
const uint8_t flag_data = 0x6;
const uint8_t local_name_data[] = "AdvC";
const uint8_t *service_uuid = bg_gattdb_data.uuidtable_128;

/* https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers - To get your company ID*/
/* Below is an example to construct your manufacturer specific data with payload set to "KBA - Adv Constructor" */
Expand All @@ -176,40 +182,34 @@ void demo_setup_adv(uint8_t handle)
.data = local_name_data
},
/* Element 2 */
{
.ad_type = more_128_uuids,
.len = 16,
.data = service_uuid
},
/* Element 3 */
{
.ad_type = manufacturer_specific_data,
.len = sizeof(manu_data),
.data = manu_data
}
};

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

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

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

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

if (!adv) {
sl_app_log("input param null, aborting.\n");
app_log("input param null, aborting.\n");
return SL_STATUS_NULL_POINTER;
}

for (i = 0; i < adv->ele_num; i++) {
amout_bytes += adv->p_element[i].len + 2;
if (!adv->p_element[i].data) {
sl_app_log("adv unit payload data null, aborting.\n");
app_log("adv unit payload data null, aborting.\n");
return SL_STATUS_NULL_POINTER;
}
}
if (((amout_bytes > MAX_ADV_DATA_LENGTH) && !ext_adv)
|| ((amout_bytes > MAX_EXTENDED_ADV_LENGTH))) {
sl_app_log("Adv data too long [length = %d], aborting.\n", amout_bytes);
app_log("Adv data too long [length = %d], aborting.\n", amout_bytes);
return SL_STATUS_BT_CTRL_PACKET_TOO_LONG;
}

Expand All @@ -244,8 +244,9 @@ sl_status_t construct_adv(const adv_t *adv, uint8_t ext_adv)
memcpy(buf + amout_bytes, adv->p_element[i].data, adv->p_element[i].len);
amout_bytes += adv->p_element[i].len;
}
sc = sl_bt_advertiser_set_data(adv->adv_handle, adv->adv_packet_type, amout_bytes, buf);
sl_app_assert(sc == SL_STATUS_OK,
if(ext_adv) sc = sl_bt_extended_advertiser_set_data(adv->adv_handle, amout_bytes, buf);
else sc = sl_bt_legacy_advertiser_set_data(adv->adv_handle, adv->adv_packet_type, amout_bytes, buf);
app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to set advertising data\n",
(int)sc);
return SL_STATUS_OK;
Expand Down
8 changes: 3 additions & 5 deletions advertising/adv_or_scan_response_constructor/src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,11 @@ typedef struct {
*
* - <b>0:</b> Advertising packets
* - <b>1:</b> Scan response packets
* - <b>8:</b> Periodic advertising packets
*
*/
typedef enum {
adv_packet = 0,
scan_rsp = 1,
periodic_adv = 8
scan_rsp = 1
} adv_packet_type_t;

/**
Expand All @@ -110,14 +108,14 @@ typedef struct {
/**
* @brief construct_adv - set the corresponding advertising data to the stack.
* Do not forget to modify the second parameter of
* sl_bt_advertiser_start function to advertiser_user_data
* sl_bt_legacy_advertiser_start function to advertiser_user_data
*
* @param adv - pointer to the @ref{adv_t} structure
*
* @param ext_adv - enable or disable the extended advertising. If extended
* advertising is disabled, the maximum data length for advertising or scan
* response is 31 bytes, otherwise, it's 253 or 191 bytes depending on the
* advertising type. @ref{sl_bt_advertiser_set_data} API for more details.
* advertising type. @ref{sl_bt_extended_advertiser_set_data} API for more details.
*
* @return status code base on the specific error that comes along with a
* message for user to understand it
Expand Down
16 changes: 9 additions & 7 deletions advertising/advertising_and_scanning_with_le_coded_phy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ The sync info field is used for Periodic Advertisement synchronization, not disc
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:

```C
sl_bt_advertiser_set_phy(advertising_set_handle,
gap_coded_phy,
gap_coded_phy);
sl_bt_extended_advertiser_set_phy(advertising_set_handle,
gap_coded_phy,
gap_coded_phy);
```
The parameters indicate the advertisement set to be used, the PHY for primary advertisements, and the PHY used for secondary advertisements.
Expand All @@ -49,9 +49,9 @@ Advertisements on the coded PHY do not support active scanning. This means that
To start advertising, the following API function is used:
```C
sc = sl_bt_advertiser_start(advertising_set_handle,
advertiser_general_discoverable,
advertiser_connectable_non_scannable);
sc = sl_bt_extended_advertiser_start(advertising_set_handle,
connect,
flags);
```

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

- Install the **Log** component (found under Bluetooth > Utility group)
- Install the **Extended Advertising** component, if it is not yet installed

- Install the **Log** component (found under Application > Utility group)
![add log driver](images/add_log_4.png)

4. Import the GATT configuration:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*
******************************************************************************/
#include "em_common.h"
#include "sl_app_assert.h"
#include "app_assert.h"
#include "sl_bluetooth.h"
#include "gatt_db.h"
#include "app.h"

#include "sl_bt_api.h"
#include "sl_app_log.h"
#include "app_log.h"

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

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

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

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

// disable report scan request
sc = sl_bt_advertiser_set_report_scan_request(advertising_set_handle,
0);
sl_app_assert(sc == SL_STATUS_OK,
app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to disable report scan request\n",
(int)sc);

// set phy
sc = sl_bt_advertiser_set_phy(advertising_set_handle,
gap_coded_phy,
gap_coded_phy);
sl_app_assert(sc == SL_STATUS_OK,
sc = sl_bt_extended_advertiser_set_phy(advertising_set_handle,
sl_bt_gap_phy_coded,
sl_bt_gap_phy_coded);
app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to set phy\n",
(int)sc);

// Do not use legacy PDU - use extended advertisement instead.
sc = sl_bt_advertiser_clear_configuration(advertising_set_handle,
1);
sl_app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to clear config\n",
(int)sc);

// Set advertising interval to 100ms.
sc = sl_bt_advertiser_set_timing(
advertising_set_handle,
160, // min. adv. interval (milliseconds * 1.6)
160, // max. adv. interval (milliseconds * 1.6)
0, // adv. duration
0); // max. num. adv. events
sl_app_assert(sc == SL_STATUS_OK,
app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to set advertising timing\n",
(int)sc);

// all primary advertising channels
sc = sl_bt_advertiser_set_channel_map(advertising_set_handle,
7);
sl_app_assert(sc == SL_STATUS_OK,
app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to set channel map\n",
(int)sc);

// Start general advertising and enable connections.
sc = sl_bt_advertiser_start(
sc = sl_bt_extended_advertiser_start(
advertising_set_handle,
advertiser_general_discoverable,
advertiser_connectable_non_scannable);
sl_app_assert(sc == SL_STATUS_OK,
sl_bt_extended_advertiser_connectable,
0);
app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to start advertising\n",
(int)sc);
app_log("Start advertising ...\n");
break;

// -------------------------------
Expand All @@ -166,11 +160,11 @@ void sl_bt_on_event(sl_bt_msg_t *evt)
// This event indicates that a connection was closed.
case sl_bt_evt_connection_closed_id:
// Restart advertising after client has disconnected.
sc = sl_bt_advertiser_start(
sc = sl_bt_extended_advertiser_start(
advertising_set_handle,
advertiser_general_discoverable,
advertiser_connectable_non_scannable);
sl_app_assert(sc == SL_STATUS_OK,
sl_bt_extended_advertiser_connectable,
0);
app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to start advertising\n",
(int)sc);
break;
Expand Down
Loading

0 comments on commit 2b9516f

Please sign in to comment.