Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pal: switch to multi-advertising BLE API #603

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ if SIDEWALK
config MAIN_STACK_SIZE
default 2048

config BT_ID_MAX
default 2

config SIDEWALK_SUBGHZ_SUPPORT
bool "Enable Sub-GHz link type support in Sidewalk libraries"
default y
Expand Down Expand Up @@ -142,6 +145,14 @@ config SIDEWALK_BLE_ADV_INT_SLOW
range 20 10240
default 1000

config SIDEWALK_BLE_ID
int "Set Sidewalk's BLE identity identifier"
range 1 255
ktaborowski marked this conversation as resolved.
Show resolved Hide resolved
default 1
help
Specify identifier used to distinguish Sidewalk related BLE
events.

config SIDEWALK_BLE_ADV_INT_TRANSITION
int "Duration of fast advertisement after sid_start in seconds"
range 1 2147483647
Expand Down
1 change: 1 addition & 0 deletions Kconfig.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ config SIDEWALK_BLE
imply BT_CTLR_CONN_RSSI
imply BT_CTLR_TX_PWR_DYNAMIC_CONTROL
imply BT_CTLR_ADVANCED_FEATURES
imply BT_EXT_ADV
help
Sidewalk Bluetooth Low Energy (BLE) module

Expand Down
3 changes: 3 additions & 0 deletions samples/sid_end_device/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ config BT_PERIPHERAL_PREF_LATENCY
config BT_PERIPHERAL_PREF_TIMEOUT
default 400

config BT_EXT_ADV_MAX_ADV_SET
default 2

config NVS_LOOKUP_CACHE_SIZE
default 256 if NVS

Expand Down
5 changes: 4 additions & 1 deletion samples/sid_end_device/sysbuild/ipc_radio/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ CONFIG_BT_HCI_RAW=y
CONFIG_BT_CTLR_ASSERT_HANDLER=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_CENTRAL=n
CONFIG_BT_MAX_CONN=1
CONFIG_BT_MAX_CONN=2
CONFIG_BT_BUF_ACL_RX_SIZE=502
CONFIG_BT_BUF_ACL_TX_SIZE=251
CONFIG_BT_CTLR_DATA_LENGTH_MAX=251

CONFIG_BT_CTLR_ADV_SET=3
CONFIG_BT_CTLR_ADV_EXT=y

# IPC
CONFIG_IPC_SERVICE=y
CONFIG_MBOX=y
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/license.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ licenses:
- sidewalk.*/subsys/config/common/src/.*(c|h)$
- sidewalk.*/subsys/hal(/.*)+h$
- sidewalk.*/subsys/hal/src/memory.c$
- sidewalk.*/subsys/sal/common/.*(c|h)$
- sidewalk.*/subsys/sal/common/.*/sid_.*(c|h)$
- sidewalk.*/subsys/semtech/include/semtech_radio_ifc.h$
- sidewalk.*/tools/.*
- sidewalk.*/tests/validation/storage_kv/.*(c|h)$
Expand Down
7 changes: 7 additions & 0 deletions subsys/sal/sid_pal/include/sid_ble_advert.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

#include <stdint.h>

/**
* @brief Initialize Bluetooth Advertising.
*
* @return Zero on success or (negative) error code on failure.
*/
int sid_ble_advert_init(void);

/**
* @brief Start Bluetooth advertising.
*
Expand Down
37 changes: 37 additions & 0 deletions subsys/sal/sid_pal/src/sid_ble_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,29 @@ static sid_error_t ble_adapter_set_tx_pwr(int8_t tx_power)
return SID_ERROR_NONE;
}

static int create_ble_id(void)
{
int ret;
size_t count = 0;

BUILD_ASSERT(CONFIG_SIDEWALK_BLE_ID < CONFIG_BT_ID_MAX, "CONFIG_BT_ID_MAX is too small.");

/* Check if Bluetooth identites weren't already created. */
bt_id_get(NULL, &count);
if (count > CONFIG_SIDEWALK_BLE_ID) {
return 0;
}

do {
ret = bt_id_create(NULL, NULL);
if (ret < 0) {
return ret;
}
} while (ret != CONFIG_SIDEWALK_BLE_ID);

return 0;
}

static sid_error_t ble_adapter_init(const sid_ble_config_t *cfg)
{
LOG_DBG("Sidewalk -> BLE");
Expand All @@ -231,6 +254,18 @@ static sid_error_t ble_adapter_init(const sid_ble_config_t *cfg)
return SID_ERROR_GENERIC;
}

err_code = create_ble_id();
if (err_code) {
LOG_ERR("BT ID init failed (err: %d)", err_code);
return SID_ERROR_GENERIC;
}

err_code = sid_ble_advert_init();
if (err_code) {
LOG_ERR("BT Advertisement failed (err: %d)", err_code);
return SID_ERROR_GENERIC;
}

sid_ble_conn_init();

return SID_ERROR_NONE;
Expand Down Expand Up @@ -405,6 +440,8 @@ static sid_error_t ble_adapter_deinit(void)
LOG_DBG("Sidewalk -> BLE");
sid_ble_conn_deinit();

bt_id_delete(CONFIG_SIDEWALK_BLE_ID);
bt_id_reset(CONFIG_SIDEWALK_BLE_ID, NULL, NULL);
int err = bt_disable();

if (err) {
Expand Down
92 changes: 72 additions & 20 deletions subsys/sal/sid_pal/src/sid_ble_advert.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,24 @@ LOG_MODULE_REGISTER(sid_ble_advert, CONFIG_SIDEWALK_BLE_ADAPTER_LOG_LEVEL);
#endif

/* Advertising parameters. */
#define AMA_ADV_PARAM_FAST \
BT_LE_ADV_PARAM(AMA_ADV_OPTIONS, MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_FAST), \
MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_FAST + \
CONFIG_SIDEWALK_BLE_ADV_INT_PRECISION), \
NULL)

#define AMA_ADV_PARAM_SLOW \
BT_LE_ADV_PARAM(AMA_ADV_OPTIONS, MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_SLOW), \
MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_SLOW + \
CONFIG_SIDEWALK_BLE_ADV_INT_PRECISION), \
NULL)
static struct bt_le_adv_param adv_param_fast = {
.id = CONFIG_SIDEWALK_BLE_ID,
.options = (AMA_ADV_OPTIONS),
.interval_min = MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_FAST),
.interval_max = MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_FAST +
CONFIG_SIDEWALK_BLE_ADV_INT_PRECISION),
};

static struct bt_le_adv_param adv_param_slow = {
.id = CONFIG_SIDEWALK_BLE_ID,
.options = (AMA_ADV_OPTIONS),
.interval_min = MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_SLOW),
.interval_max = MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_SLOW +
CONFIG_SIDEWALK_BLE_ADV_INT_PRECISION),
};

static struct bt_le_ext_adv *adv_set_fast;
static struct bt_le_ext_adv *adv_set_slow;

/**
* @brief Advertising data items values size in bytes.
Expand All @@ -63,7 +70,7 @@ LOG_MODULE_REGISTER(sid_ble_advert, CONFIG_SIDEWALK_BLE_ADAPTER_LOG_LEVEL);

enum adv_data_items { ADV_DATA_FLAGS, ADV_DATA_SERVICES, ADV_DATA_MANUF_DATA };

typedef enum { BLE_ADV_DISABLE, BLE_ADV_ENABLE } sid_ble_adv_state_t;
typedef enum { BLE_ADV_DISABLE, BLE_ADV_FAST, BLE_ADV_SLOW } sid_ble_adv_state_t;

static void change_advertisement_interval(struct k_work *);
K_WORK_DELAYABLE_DEFINE(change_adv_work, change_advertisement_interval);
Expand Down Expand Up @@ -103,36 +110,77 @@ static uint8_t advert_manuf_data_copy(uint8_t *data, uint8_t data_len)
static void change_advertisement_interval(struct k_work *work)
{
ARG_UNUSED(work);
if (BLE_ADV_ENABLE == atomic_get(&adv_state)) {
if (bt_le_adv_stop()) {

struct bt_le_ext_adv_start_param ext_adv_start_param = { 0 };

if (BLE_ADV_FAST == atomic_get(&adv_state)) {
if (bt_le_ext_adv_stop(adv_set_fast)) {
atomic_set(&adv_state, BLE_ADV_DISABLE);
return;
}

if (bt_le_ext_adv_set_data(adv_set_slow, adv_data, ARRAY_SIZE(adv_data), NULL, 0)) {
atomic_set(&adv_state, BLE_ADV_DISABLE);
return;
}
if (bt_le_adv_start(AMA_ADV_PARAM_SLOW, adv_data, ARRAY_SIZE(adv_data), NULL, 0)) {

if (bt_le_ext_adv_start(adv_set_slow, &ext_adv_start_param)) {
atomic_set(&adv_state, BLE_ADV_DISABLE);
return;
}

atomic_set(&adv_state, BLE_ADV_SLOW);

LOG_DBG("BLE -> BLE");
}
}

int sid_ble_advert_init(void)
{
int ret;

ret = bt_le_ext_adv_create(&adv_param_fast, NULL, &adv_set_fast);
if (ret) {
LOG_ERR("Failed to create fast advertising set");
return ret;
}

ret = bt_le_ext_adv_create(&adv_param_slow, NULL, &adv_set_slow);
if (ret) {
LOG_ERR("Failed to create slow advertising set");
return ret;
}

return 0;
}

int sid_ble_advert_start(void)
{
k_work_reschedule(&change_adv_work, K_SECONDS(CONFIG_SIDEWALK_BLE_ADV_INT_TRANSITION));
int err = bt_le_adv_start(AMA_ADV_PARAM_FAST, adv_data, ARRAY_SIZE(adv_data), NULL, 0);

struct bt_le_ext_adv_start_param ext_adv_start_param = { 0 };
int err;

err = bt_le_ext_adv_set_data(adv_set_fast, adv_data, ARRAY_SIZE(adv_data), NULL, 0);
if (err) {
return err;
}
atomic_set(&adv_state, BLE_ADV_ENABLE);

err = bt_le_ext_adv_start(adv_set_fast, &ext_adv_start_param);
if (err) {
return err;
}

atomic_set(&adv_state, BLE_ADV_FAST);

return err;
}

int sid_ble_advert_stop(void)
{
k_work_cancel_delayable(&change_adv_work);
int err = bt_le_adv_stop();
int err = bt_le_ext_adv_stop(atomic_get(&adv_state) == BLE_ADV_FAST ? adv_set_fast :
adv_set_slow);

if (0 == err) {
atomic_set(&adv_state, BLE_ADV_DISABLE);
Expand All @@ -143,6 +191,8 @@ int sid_ble_advert_stop(void)

int sid_ble_advert_update(uint8_t *data, uint8_t data_len)
{
sid_ble_adv_state_t state = atomic_get(&adv_state);

if (!data || 0 == data_len) {
return -EINVAL;
}
Expand All @@ -151,8 +201,10 @@ int sid_ble_advert_update(uint8_t *data, uint8_t data_len)

int err = 0;

if (BLE_ADV_ENABLE == atomic_get(&adv_state)) {
err = bt_le_adv_update_data(adv_data, ARRAY_SIZE(adv_data), NULL, 0);
if (BLE_ADV_DISABLE != state) {
/* Update currently advertised set, the other one will be set on start/transition */
err = bt_le_ext_adv_set_data((state == BLE_ADV_FAST ? adv_set_fast : adv_set_slow),
adv_data, ARRAY_SIZE(adv_data), NULL, 0);
}

return err;
Expand Down
22 changes: 19 additions & 3 deletions subsys/sal/sid_pal/src/sid_ble_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ static struct bt_conn_cb conn_callbacks = {

static struct bt_gatt_cb gatt_callbacks = { .att_mtu_updated = ble_mtu_cb };

static bool should_handle_event(struct bt_conn *conn) {
struct bt_conn_info conn_info = {};

if (!conn || bt_conn_get_info(conn, &conn_info) || conn_info.id != CONFIG_SIDEWALK_BLE_ID) {
return false;
}

return true;
}

/**
* @brief The function is called when a new connection is established.
*
Expand All @@ -41,12 +51,18 @@ static struct bt_gatt_cb gatt_callbacks = { .att_mtu_updated = ble_mtu_cb };
*/
static void ble_connect_cb(struct bt_conn *conn, uint8_t err)
{
const bt_addr_le_t *bt_addr_le;

if (!should_handle_event(conn)) {
return;
}

if (err) {
LOG_ERR("Connection failed (err %u)\n", err);
return;
}

const bt_addr_le_t *bt_addr_le = bt_conn_get_dst(conn);
bt_addr_le = bt_conn_get_dst(conn);

if (bt_addr_le) {
memcpy(conn_params.addr, bt_addr_le->a.val, BT_ADDR_SIZE);
Expand All @@ -72,10 +88,10 @@ static void ble_connect_cb(struct bt_conn *conn, uint8_t err)
*/
static void ble_disconnect_cb(struct bt_conn *conn, uint8_t reason)
{
if (!conn || conn_params.conn != conn) {
LOG_WRN("Unknow connection");
if (!should_handle_event(conn) || conn_params.conn != conn) {
return;
}

sid_ble_adapter_conn_disconnected((const uint8_t *)conn_params.addr);

k_mutex_lock(&bt_conn_mutex, K_FOREVER);
Expand Down
3 changes: 2 additions & 1 deletion subsys/sal/sid_pal/src/sid_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ sid_error_t sid_pal_storage_kv_record_delete(uint16_t group, uint16_t key)
#ifdef CONFIG_SIDEWALK_CRYPTO_PSA_KEY_STORAGE
psa_key_id_t key_id = storage2key_id(group, key);
if (SID_CRYPTO_KEYS_ID_IS_SIDEWALK_KEY(key_id)) {
return storage_key_delete_secure(key_id) ? SID_ERROR_NONE : SID_ERROR_STORAGE_ERASE_FAIL;
return storage_key_delete_secure(key_id) ? SID_ERROR_NONE :
SID_ERROR_STORAGE_ERASE_FAIL;
}
#endif /* CONFIG_SIDEWALK_CRYPTO_PSA_KEY_STORAGE */

Expand Down
6 changes: 6 additions & 0 deletions tests/unit_tests/pal_ble_adapter/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ config SIDEWALK_LOG_LEVEL
config SIDEWALK_BLE_ADAPTER_LOG_LEVEL
default 0

config SIDEWALK_BLE_ID
default 1

config BT_ID_MAX
default 2

source "Kconfig.zephyr"
Loading