Skip to content

Commit

Permalink
app: allow DFU and Sidewalk to run in parallel
Browse files Browse the repository at this point in the history
remove disabling of Sidewalk when entering to DFU mode

Signed-off-by: Robert Gałat <[email protected]>
  • Loading branch information
RobertGalatNordic committed Nov 14, 2024
1 parent 871ea7b commit d9da400
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 67 deletions.
4 changes: 0 additions & 4 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ if SIDEWALK
config MAIN_STACK_SIZE
default 2048

config BT_ID_MAX
default 3 if SIDEWALK_DFU
default 2

config SIDEWALK_SUBGHZ_SUPPORT
bool "Enable Sub-GHz link type support in Sidewalk libraries"
default y
Expand Down
8 changes: 8 additions & 0 deletions Kconfig.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ config SIDEWALK_BLE
help
Sidewalk Bluetooth Low Energy (BLE) module

config BT_ID_MAX
default 3 if SIDEWALK_DFU
default 2

config BT_APP_IFC
bool "Enable BT app wrapper"
default y if SIDEWALK_BLE

config SIDEWALK_ASSERT
bool
default SIDEWALK
Expand Down
4 changes: 4 additions & 0 deletions samples/sid_end_device/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ config BT_EXT_ADV_MAX_ADV_SET
default 3 if SIDEWALK_DFU
default 2

config BT_MAX_CONN
default 2 if SIDEWALK_DFU
default 1

config NVS_LOOKUP_CACHE_SIZE
default 256 if NVS

Expand Down
4 changes: 0 additions & 4 deletions samples/sid_end_device/src/hello/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,9 @@ static void app_btn_dfu_state(uint32_t unused)
ARG_UNUSED(unused);
static bool go_to_dfu_state = true;
if (go_to_dfu_state) {
sidewalk_event_send(sidewalk_event_exit, NULL, NULL);
sidewalk_event_send(app_event_enter_dfu_mode, NULL, NULL);
application_state_working(&global_state_notifier, false);
} else {
sidewalk_event_send(app_event_exit_dfu_mode, NULL, NULL);
sidewalk_event_send(sidewalk_event_autostart, NULL, NULL);
application_state_working(&global_state_notifier, true);
}

go_to_dfu_state = !go_to_dfu_state;
Expand Down
2 changes: 0 additions & 2 deletions samples/sid_end_device/src/sensor_monitoring/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,9 @@ static void app_btn_dfu_state(uint32_t unused)
ARG_UNUSED(unused);
static bool go_to_dfu_state = true;
if (go_to_dfu_state) {
sidewalk_event_send(sidewalk_event_exit, NULL, NULL);
sidewalk_event_send(app_event_enter_dfu_mode, NULL, NULL);
} else {
sidewalk_event_send(app_event_exit_dfu_mode, NULL, NULL);
sidewalk_event_send(sidewalk_event_autostart, NULL, NULL);
}

go_to_dfu_state = !go_to_dfu_state;
Expand Down
2 changes: 2 additions & 0 deletions subsys/sal/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ zephyr_include_directories(sid_pal_ifc)
zephyr_include_directories(sid_time_ops)

add_subdirectory_ifdef(CONFIG_SIDEWALK_ON_DEV_CERT sid_on_dev_cert)

zephyr_library_sources(sid_ifc/bt_app_callbacks.c)
2 changes: 1 addition & 1 deletion subsys/sal/common/sid_ifc/bt_app_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ int app_bt_enable(bt_ready_cb_t cb)
bt_enable_count++;
if (cb) {
cb(0);
return 0;
}
return 0;
}

int app_bt_disable()
Expand Down
30 changes: 25 additions & 5 deletions subsys/sal/common/sid_ifc/bt_app_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,41 @@
#include <stdbool.h>
#include <zephyr/bluetooth/bluetooth.h>

#if defined (CONFIG_BT_APP_IFC)

/**
* @brief Wrapper for @bt_enable, with reference tracking.
* Real @bt_enable is called only of first call to app_bt_enable
*
* @param cb callback passed to @bt_enable
* @return int result from @bt_enable call or 0 if called multiple times
*/
int app_bt_enable(bt_ready_cb_t cb);


/**
* @brief Wrapper for @bt_disable.
* This function removes internal reference.
* If the internal reference counter shows 0, real @bt_disable is called
*
* @return int result from @bt_disable or 0 if app_bt_enable has been called more than app_bt_disable
*/
int app_bt_disable();


/**
* @brief BT ids used for extended advertising.
* This allows to identify connections from different extended adverticements.
*/
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
_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);

BUILD_ASSERT(_BT_ID_MAX <= CONFIG_BT_ID_MAX, "Too many BT Ids! increase CONFIG_BT_ID_MAX, to match _BT_ID_MAX");
#endif
#endif
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 @@ -16,6 +16,13 @@
*/
int sid_ble_advert_init(void);

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

/**
* @brief Start Bluetooth advertising.
*
Expand Down
4 changes: 2 additions & 2 deletions subsys/sal/sid_pal/src/sid_ble_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static int create_ble_id(void)
/* Check if Bluetooth identites weren't already created. */
bt_id_get(NULL, &count);
if (count > BT_ID_SIDEWALK) {
return BT_ID_SIDEWALK;
return 0;
}

do {
Expand Down Expand Up @@ -438,7 +438,7 @@ static sid_error_t ble_adapter_deinit(void)
{
LOG_DBG("Sidewalk -> BLE");
sid_ble_conn_deinit();

sid_ble_advert_deinit();
bt_id_delete(BT_ID_SIDEWALK);
bt_id_reset(BT_ID_SIDEWALK, NULL, NULL);
int err = app_bt_disable();
Expand Down
70 changes: 51 additions & 19 deletions subsys/sal/sid_pal/src/sid_ble_advert.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
#include <sid_ble_uuid.h>

#include <sid_ble_uuid.h>
#include <bt_app_callbacks.h>

#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/logging/log.h>
#include <string.h>

LOG_MODULE_REGISTER(sid_ble_advert, CONFIG_SIDEWALK_BLE_ADAPTER_LOG_LEVEL);

Expand All @@ -37,23 +39,23 @@ LOG_MODULE_REGISTER(sid_ble_advert, CONFIG_SIDEWALK_BLE_ADAPTER_LOG_LEVEL);

/* Advertising parameters. */
static struct bt_le_adv_param adv_param_fast = {
.id = CONFIG_SIDEWALK_BLE_ID,
.id = BT_ID_SIDEWALK,
.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,
.id = BT_ID_SIDEWALK,
.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;
static struct bt_le_ext_adv *adv_set_fast = NULL;
static struct bt_le_ext_adv *adv_set_slow = NULL;

/**
* @brief Advertising data items values size in bytes.
Expand Down Expand Up @@ -114,18 +116,24 @@ static void change_advertisement_interval(struct k_work *work)
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)) {
int err = 0;
err = bt_le_ext_adv_stop(adv_set_fast);
if (err) {
atomic_set(&adv_state, BLE_ADV_DISABLE);
LOG_ERR("Failed to stop fast adv errno %d (%s)", err, strerror(err));
return;
}

if (bt_le_ext_adv_set_data(adv_set_slow, adv_data, ARRAY_SIZE(adv_data), NULL, 0)) {
err = bt_le_ext_adv_set_data(adv_set_slow, adv_data, ARRAY_SIZE(adv_data), NULL, 0);
if (err) {
atomic_set(&adv_state, BLE_ADV_DISABLE);
LOG_ERR("Failed to set adv data to slow adv errno %d (%s)", err,
strerror(err));
return;
}

if (bt_le_ext_adv_start(adv_set_slow, &ext_adv_start_param)) {
err = bt_le_ext_adv_start(adv_set_slow, &ext_adv_start_param);
if (err) {
atomic_set(&adv_state, BLE_ADV_DISABLE);
LOG_ERR("Failed to start slow adv errno %d (%s)", err, strerror(err));
return;
}

Expand All @@ -138,36 +146,60 @@ static void change_advertisement_interval(struct k_work *work)
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;
if (adv_set_fast == NULL) {
ret = bt_le_ext_adv_create(&adv_param_fast, NULL, &adv_set_fast);
if (ret) {
LOG_ERR("Failed to create fast advertising set errno %d (%s)", ret,
strerror(ret));
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;
if (adv_set_slow == NULL) {
ret = bt_le_ext_adv_create(&adv_param_slow, NULL, &adv_set_slow);
if (ret) {
LOG_ERR("Failed to create slow advertising set errno %d (%s)", ret,
strerror(ret));
return ret;
}
}

return 0;
}

int sid_ble_advert_deinit(void)
{
int err = bt_le_ext_adv_delete(adv_set_fast);
if (err) {
LOG_ERR("Failed to delete adv_set_fast errno %d (%s)", err, strerror(err));
return err;
}
adv_set_fast = NULL;
err = bt_le_ext_adv_delete(adv_set_slow);
if (err) {
LOG_ERR("Failed to delete adv_set_slow errno %d (%s)", err, strerror(err));
return err;
}
adv_set_slow = NULL;
return 0;
}

int sid_ble_advert_start(void)
{
k_work_reschedule(&change_adv_work, K_SECONDS(CONFIG_SIDEWALK_BLE_ADV_INT_TRANSITION));

struct bt_le_ext_adv_start_param ext_adv_start_param = { 0 };
int err;
int err = 0;

err = bt_le_ext_adv_set_data(adv_set_fast, adv_data, ARRAY_SIZE(adv_data), NULL, 0);
if (err) {
LOG_ERR("Failed to set adv data errno: %d (%s)", err, strerror(err));
return err;
}

err = bt_le_ext_adv_start(adv_set_fast, &ext_adv_start_param);
if (err) {
LOG_ERR("Failed to start adv errno: %d (%s)", err, strerror(err));
return err;
}

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

config SIDEWALK_BLE_ID
default 1
config BT_APP_IFC
default y

config BT_ID_MAX
default 2
Expand Down
8 changes: 6 additions & 2 deletions tests/unit_tests/pal_ble_adapter/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cmock_sid_ble_adapter_callbacks.h>
#include <cmock_sid_ble_service.h>
#include <errno.h>
#include <bt_app_callbacks.h>

#include <stdbool.h>

Expand Down Expand Up @@ -174,13 +175,15 @@ void test_sid_pal_ble_adapter_init(void)

TEST_ASSERT_EQUAL(SID_ERROR_NONE, sid_pal_ble_adapter_create(&p_test_ble_ifc));

bt_id_create_fake.return_val = CONFIG_SIDEWALK_BLE_ID;
bt_id_create_fake.return_val = BT_ID_SIDEWALK;
__cmock_sid_ble_conn_init_Expect();
__cmock_sid_ble_advert_init_IgnoreAndReturn(0);
TEST_ASSERT_EQUAL(SID_ERROR_NONE, p_test_ble_ifc->init(&test_ble_cfg));
app_bt_disable();

__cmock_sid_ble_conn_init_Expect();
TEST_ASSERT_EQUAL(SID_ERROR_NONE, p_test_ble_ifc->init(NULL));
app_bt_disable();

bt_enable_fake.return_val = -ENOENT;
TEST_ASSERT_EQUAL(SID_ERROR_GENERIC, p_test_ble_ifc->init(&test_ble_cfg));
Expand All @@ -193,11 +196,12 @@ void test_sid_pal_ble_adapter_deinit(void)
TEST_ASSERT_EQUAL(SID_ERROR_NONE, sid_pal_ble_adapter_create(&p_test_ble_ifc));
__cmock_settings_load_ExpectAndReturn(ESUCCESS);
__cmock_sid_ble_conn_init_Expect();
bt_id_create_fake.return_val = CONFIG_SIDEWALK_BLE_ID;
bt_id_create_fake.return_val = BT_ID_SIDEWALK;
__cmock_sid_ble_advert_init_IgnoreAndReturn(0);
TEST_ASSERT_EQUAL(SID_ERROR_NONE, p_test_ble_ifc->init(&test_ble_cfg));

__cmock_sid_ble_conn_deinit_Expect();
__cmock_sid_ble_advert_deinit_IgnoreAndReturn(0);
TEST_ASSERT_EQUAL(SID_ERROR_NONE, p_test_ble_ifc->deinit());
}

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

config BT_ID_MAX
default 2

config BT_APP_IFC
default y

module = SIDEWALK_BLE_ADAPTER
module-str = Sidewalk BLE interface
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config"
Expand All @@ -29,8 +35,4 @@ config SIDEWALK_BLE_ADV_INT_TRANSITION
int "test value for Sidewalk configuration macro"
default 30

config SIDEWALK_BLE_ID
int "test value for Sidewalk configuration"
default 1

source "Kconfig.zephyr"
6 changes: 3 additions & 3 deletions tests/unit_tests/sid_ble_connection/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,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

config BT_APP_IFC
default y

source "Kconfig.zephyr"
Loading

0 comments on commit d9da400

Please sign in to comment.