Skip to content

Commit

Permalink
sbdt: block callbacks if in SMP DFU mode
Browse files Browse the repository at this point in the history
prevent sidewalk from handling sbdt events in DFU mode

Signed-off-by: Robert Gałat <[email protected]>
  • Loading branch information
RobertGalatNordic committed Nov 22, 2024
1 parent b7e3e6b commit 05e7532
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 45 deletions.
16 changes: 0 additions & 16 deletions samples/sid_end_device/include/sbdt/dfu_file_transfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,4 @@ void app_file_transfer_demo_deinit(struct sid_handle *handle);
*/
void sidewalk_event_file_transfer(sidewalk_ctx_t *sid, void *ctx);

/**
* @brief Sidewalk event to deinit sbdt
*
* @param sid sidewalk_ctx_t object
* @param ctx Ignored
*/
void sidewalk_event_file_transfer_deinit(sidewalk_ctx_t *sid, void *ctx);

/**
* @brief Sidewalk event to init sbdt
*
* @param sid sidewalk_ctx_t object
* @param ctx Ignored
*/
void sidewalk_event_file_transfer_init(sidewalk_ctx_t *sid, void *ctx);

#endif /* FILE_TRANSFER_H */
8 changes: 2 additions & 6 deletions samples/sid_end_device/src/cli/app_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
#include <sidewalk_dfu/nordic_dfu.h>
#endif

#ifdef CONFIG_SIDEWALK_FILE_TRANSFER_DFU
#include <sbdt/dfu_file_transfer.h>
#endif
#include <cli/sbdt_shell_events.h>

#define CLI_CMD_OPT_LINK_BLE 1
#define CLI_CMD_OPT_LINK_FSK 2
Expand Down Expand Up @@ -307,9 +305,7 @@ static void app_event_enter_dfu_mode(sidewalk_ctx_t *sid, void *ctx)
// shell handlers
int cmd_nordic_dfu(const struct shell *shell, int32_t argc, const char **argv)
{
#ifdef CONFIG_SIDEWALK_FILE_TRANSFER_DFU
sidewalk_event_send(sidewalk_event_file_transfer_deinit, NULL, NULL);
#endif
sidewalk_event_send(sbdt_event_deinit, NULL, NULL);
sidewalk_event_send(app_event_enter_dfu_mode, (void *)shell, NULL);
return 0;
}
Expand Down
6 changes: 0 additions & 6 deletions samples/sid_end_device/src/hello/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,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) {
#ifdef CONFIG_SIDEWALK_FILE_TRANSFER_DFU
sidewalk_event_send(sidewalk_event_file_transfer_deinit, NULL, NULL);
#endif
sidewalk_event_send(app_event_enter_dfu_mode, NULL, NULL);
} else {
sidewalk_event_send(app_event_exit_dfu_mode, NULL, NULL);
#ifdef CONFIG_SIDEWALK_FILE_TRANSFER_DFU
sidewalk_event_send(sidewalk_event_file_transfer_init, NULL, NULL);
#endif
}

go_to_dfu_state = !go_to_dfu_state;
Expand Down
31 changes: 21 additions & 10 deletions samples/sid_end_device/src/sbdt/dfu_file_transfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include <zephyr/logging/log.h>
#include <sid_pal_crypto_ifc.h>
#include <stdio.h>
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
#include <sidewalk_dfu/nordic_dfu.h>
#endif

LOG_MODULE_REGISTER(file_transfer, CONFIG_SIDEWALK_LOG_LEVEL);

Expand Down Expand Up @@ -93,6 +96,15 @@ static void on_transfer_request(const struct sid_bulk_data_transfer_request *con
transfer_response->scratch_buffer_size = 0;
return;
}
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
if (nordic_dfu_is_in_dfu()) {
LOG_INF("Did not accept sbdt as application is in DFU mode");
transfer_response->status = SID_BULK_DATA_TRANSFER_ACTION_REJECT;
transfer_response->reject_reason = SID_BULK_DATA_TRANSFER_REJECT_REASON_GENERIC;
transfer_response->scratch_buffer_size = 0;
return;
}
#endif

transfer_response->scratch_buffer = scratch_buffer_create(
transfer_request->file_id, transfer_request->minimum_scratch_buffer_size);
Expand All @@ -113,6 +125,13 @@ static void on_data_received(const struct sid_bulk_data_transfer_desc *const des
const struct sid_bulk_data_transfer_buffer *const buffer,
void *context)
{
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
if (nordic_dfu_is_in_dfu()) {
LOG_INF("Can not handle file transfer of new image. Application is in DFU mode");
return;
}
#endif

printk(JSON_NEW_LINE(JSON_OBJ(
JSON_LIST_2(JSON_NAME("on_data_received",
JSON_OBJ(JSON_VAL_sid_bulk_data_transfer_desc("desc", desc))),
Expand Down Expand Up @@ -215,6 +234,7 @@ static struct sid_bulk_data_transfer_event_callbacks ft_callbacks = {

void app_file_transfer_demo_init(struct sid_handle *handle)
{
LOG_INF("sid_bulk_data_transfer_init called");
scratch_buffer_init();

ft_callbacks.context = (void *)handle;
Expand All @@ -227,18 +247,9 @@ void app_file_transfer_demo_init(struct sid_handle *handle)

void app_file_transfer_demo_deinit(struct sid_handle *handle)
{
LOG_INF("sid_bulk_data_transfer_deinit called");
sid_error_t err = sid_bulk_data_transfer_deinit(handle);
if (err != SID_ERROR_NONE) {
LOG_ERR("sid_bulk_data_transfer_deinit returned %s", SID_ERROR_T_STR(err));
}
}

void sidewalk_event_file_transfer_deinit(sidewalk_ctx_t *sid, void *ctx)
{
app_file_transfer_demo_deinit(sid->handle);
}

void sidewalk_event_file_transfer_init(sidewalk_ctx_t *sid, void *ctx)
{
app_file_transfer_demo_init(sid->handle);
}
6 changes: 0 additions & 6 deletions samples/sid_end_device/src/sensor_monitoring/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,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) {
#ifdef CONFIG_SIDEWALK_FILE_TRANSFER_DFU
sidewalk_event_send(sidewalk_event_file_transfer_deinit, NULL, NULL);
#endif
sidewalk_event_send(app_event_enter_dfu_mode, NULL, NULL);
} else {
sidewalk_event_send(app_event_exit_dfu_mode, NULL, NULL);
#ifdef CONFIG_SIDEWALK_FILE_TRANSFER_DFU
sidewalk_event_send(sidewalk_event_file_transfer_init, NULL, NULL);
#endif
}

go_to_dfu_state = !go_to_dfu_state;
Expand Down
10 changes: 10 additions & 0 deletions utils/include/sidewalk_dfu/nordic_dfu.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <stdbool.h>

#ifndef NORDIC_DFU_H
#define NORDIC_DFU_H

Expand All @@ -19,4 +21,12 @@ int nordic_dfu_ble_start(void);
*/
int nordic_dfu_ble_stop(void);

/**
* @brief check if application is in dfu mode
*
* @return true if in DFU mode
* @return false
*/
bool nordic_dfu_is_in_dfu();

#endif /* NORDIC_DFU_H */
9 changes: 8 additions & 1 deletion utils/sidewalk_dfu/nordic_dfu.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static enum led_status_e {

static struct k_timer led_timer;
static struct k_timer exit_timer;
static volatile bool in_dfu_mode = false;

static void deinit_nordic_dfu(struct k_work *work)
{
Expand Down Expand Up @@ -196,6 +197,11 @@ static struct mgmt_callback dfu_mode_mgmt_cb = {
MGMT_EVT_OP_IMG_MGMT_DFU_PENDING | MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK,
};

bool nordic_dfu_is_in_dfu()
{
return in_dfu_mode;
}

static void pending_adv_start(struct k_work *work)
{
int err;
Expand Down Expand Up @@ -249,6 +255,7 @@ int nordic_dfu_ble_start(void)

LOG_INF("Advertising successfully started");

in_dfu_mode = true;
k_timer_init(&exit_timer, exit_dfu_mode, NULL);
k_timer_start(&exit_timer, K_MINUTES(CONFIG_DFU_UPLOAD_START_TIMEOUT), K_NO_WAIT);

Expand All @@ -262,7 +269,7 @@ int nordic_dfu_ble_start(void)
int nordic_dfu_ble_stop(void)
{
LOG_INF("Exiting DFU mode");

in_dfu_mode = false;
bt_conn_cb_unregister(&conn_callbacks);
mgmt_callback_unregister(&dfu_mode_mgmt_cb);

Expand Down

0 comments on commit 05e7532

Please sign in to comment.