Skip to content

Commit

Permalink
app: generalize ext adv for smp
Browse files Browse the repository at this point in the history
remove id from kconfig, as it should not be concern for the user.
collisions are prevented by using enum.

Signed-off-by: Robert Gałat <[email protected]>
  • Loading branch information
RobertGalatNordic committed Nov 13, 2024
1 parent 5b52afe commit 871ea7b
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 26 deletions.
11 changes: 2 additions & 9 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ config MAIN_STACK_SIZE
default 2048

config BT_ID_MAX
default 2
default 3 if SIDEWALK_DFU
default 2

config SIDEWALK_SUBGHZ_SUPPORT
bool "Enable Sub-GHz link type support in Sidewalk libraries"
Expand Down Expand Up @@ -145,14 +146,6 @@ 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
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 samples/sid_end_device/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ config BT_PERIPHERAL_PREF_TIMEOUT
default 400

config BT_EXT_ADV_MAX_ADV_SET
default 3 if SIDEWALK_DFU
default 2

config NVS_LOOKUP_CACHE_SIZE
Expand Down
43 changes: 43 additions & 0 deletions subsys/sal/common/sid_ifc/bt_app_callbacks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <bt_app_callbacks.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <stdbool.h>
#include <zephyr/kernel.h>

static uint32_t bt_enable_count = 0;

int app_bt_enable(bt_ready_cb_t cb)
{
if (bt_enable_count == 0) {
int ret = bt_enable(cb);
if (ret == 0) {
bt_enable_count++;
}
return ret;
}

bt_enable_count++;
if (cb) {
cb(0);
return 0;
}
}

int app_bt_disable()
{
if (bt_enable_count <= 0) {
bt_enable_count = 0;
return -EALREADY;
}
if (bt_enable_count == 1) {
bt_enable_count = 0;
return bt_disable();
} else {
bt_enable_count--;
return 0;
}
}
30 changes: 30 additions & 0 deletions subsys/sal/common/sid_ifc/bt_app_callbacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#ifndef SID_PAL_APP_CALLBACKS_H
#define SID_PAL_APP_CALLBACKS_H

#include <stdbool.h>
#include <zephyr/bluetooth/bluetooth.h>

int app_bt_enable(bt_ready_cb_t cb);

int app_bt_disable();

enum BT_id_values{
_BT_ID_DEFAULT = BT_ID_DEFAULT,
#if defined (CONFIG_SIDEWALK)
BT_ID_SIDEWALK,
#endif
#if defined (CONFIG_SIDEWALK_DFU)
BT_ID_SMP_DFU,
#endif
_BT_ID_MAX = CONFIG_BT_ID_MAX
};

BUILD_ASSERT(_BT_ID_MAX <= CONFIG_BT_ID_MAX, "Too many BT Ids! Configured limit is %d, but used %d", CONFIG_BT_ID_MAX, _BT_ID_MAX);

#endif
17 changes: 8 additions & 9 deletions subsys/sal/sid_pal/src/sid_ble_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <bt_app_callbacks.h>

#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/uuid.h>
Expand Down Expand Up @@ -218,20 +219,18 @@ 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;
if (count > BT_ID_SIDEWALK) {
return BT_ID_SIDEWALK;
}

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

return 0;
}
Expand All @@ -243,7 +242,7 @@ static sid_error_t ble_adapter_init(const sid_ble_config_t *cfg)

LOG_INF("Enable BT");
int err_code;
err_code = bt_enable(NULL);
err_code = app_bt_enable(NULL);
switch (err_code) {
case -EALREADY:
case 0:
Expand Down Expand Up @@ -440,9 +439,9 @@ 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();
bt_id_delete(BT_ID_SIDEWALK);
bt_id_reset(BT_ID_SIDEWALK, NULL, NULL);
int err = app_bt_disable();

if (err) {
LOG_ERR("BT disable failed (error %d)", err);
Expand Down
6 changes: 4 additions & 2 deletions subsys/sal/sid_pal/src/sid_ble_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <errno.h>
#include <hci_utils.h>
#include <bt_app_callbacks.h>

#include <zephyr/kernel.h>
K_MUTEX_DEFINE(bt_conn_mutex);
Expand All @@ -33,10 +34,11 @@ 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) {
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) {
if (!conn || bt_conn_get_info(conn, &conn_info) || conn_info.id != BT_ID_SIDEWALK) {
return false;
}

Expand Down
47 changes: 41 additions & 6 deletions utils/sidewalk_dfu/nordic_dfu.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@
#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
#include <zephyr/sys/reboot.h>
#include <state_notifier/state_notifier.h>
#include <bt_app_callbacks.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(nordic_dfu, CONFIG_SIDEWALK_LOG_LEVEL);

#define LED_PERIOD_TOGGLE_ALL 500
#define LED_PERIOD_LOADING_WHEEL 150

static struct bt_le_ext_adv *adv;
static struct bt_le_ext_adv_start_param ext_adv_param = { .num_events = 0, .timeout = 0 };
static struct bt_le_adv_param adv_params = { .id = BT_ID_DEFAULT,
.sid = 0,
.secondary_max_skip = 0,
.options = BT_LE_ADV_OPT_CONNECTABLE,
.interval_min = BT_GAP_ADV_SLOW_INT_MIN,
.interval_max = BT_GAP_ADV_SLOW_INT_MAX,
.peer = NULL };

static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA_BYTES(BT_DATA_UUID128_ALL, 0x84, 0xaa, 0x60, 0x74, 0x52, 0x8a, 0x8b, 0x86, 0xd3,
Expand All @@ -37,6 +48,27 @@ static enum led_status_e {
static struct k_timer led_timer;
static struct k_timer exit_timer;

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

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

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

return BT_ID_DEFAULT;
}

static void led_action(struct k_timer *timer_id)
{
switch (led_status) {
Expand All @@ -63,8 +95,7 @@ static void led_action(struct k_timer *timer_id)

static void exit_dfu_mode(struct k_timer *timer_id)
{
LOG_ERR("DFU did not start or could not complete. Reset to exit dfu mode");
sys_reboot(SYS_REBOOT_COLD);
LOG_ERR("DFU did not start or could not complete. Exit dfu mode");
}

static enum mgmt_cb_return dfu_mode_cb(uint32_t event, enum mgmt_cb_return prev_status, int32_t *rc,
Expand Down Expand Up @@ -117,15 +148,19 @@ int nordic_dfu_ble_start(void)
application_state_dfu(&global_state_notifier, true);
dk_leds_init();

int err = bt_enable(NULL);
int err = app_bt_enable(NULL);
if (err && err != -EALREADY) {
LOG_ERR("Bluetooth enable failed (err %d)", err);
return err;
}

mgmt_callback_register(&dfu_mode_mgmt_cb);

err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
adv_params.id = create_ble_id();
err = bt_le_ext_adv_create(&adv_params, NULL, &adv);
err = bt_le_ext_adv_set_data(adv, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
err = bt_le_ext_adv_start(adv, &ext_adv_param);

if (err) {
LOG_ERR("Bluetooth advertising start failed (err %d)", err);
return err;
Expand All @@ -149,13 +184,13 @@ int nordic_dfu_ble_stop(void)

mgmt_callback_unregister(&dfu_mode_mgmt_cb);

int err = bt_le_adv_stop();
int err = bt_le_ext_adv_stop(adv);
if (err) {
LOG_ERR("Bluetooth advertising stop failed (err %d)", err);
return err;
}

err = bt_disable();
err = app_bt_disable();
if (err) {
LOG_ERR("Bluetooth disable failed (err %d)", err);
return err;
Expand Down

0 comments on commit 871ea7b

Please sign in to comment.