From 05e753224f581a43bb96a511365cd9299fdfbb8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Ga=C5=82at?= Date: Fri, 22 Nov 2024 09:16:56 +0000 Subject: [PATCH] sbdt: block callbacks if in SMP DFU mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit prevent sidewalk from handling sbdt events in DFU mode Signed-off-by: Robert Gałat --- .../include/sbdt/dfu_file_transfer.h | 16 ---------- samples/sid_end_device/src/cli/app_shell.c | 8 ++--- samples/sid_end_device/src/hello/app.c | 6 ---- .../src/sbdt/dfu_file_transfer.c | 31 +++++++++++++------ .../src/sensor_monitoring/app.c | 6 ---- utils/include/sidewalk_dfu/nordic_dfu.h | 10 ++++++ utils/sidewalk_dfu/nordic_dfu.c | 9 +++++- 7 files changed, 41 insertions(+), 45 deletions(-) diff --git a/samples/sid_end_device/include/sbdt/dfu_file_transfer.h b/samples/sid_end_device/include/sbdt/dfu_file_transfer.h index 324873e3c0..b5540ab8a2 100644 --- a/samples/sid_end_device/include/sbdt/dfu_file_transfer.h +++ b/samples/sid_end_device/include/sbdt/dfu_file_transfer.h @@ -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 */ diff --git a/samples/sid_end_device/src/cli/app_shell.c b/samples/sid_end_device/src/cli/app_shell.c index 50a5df05a6..e47ef6014d 100644 --- a/samples/sid_end_device/src/cli/app_shell.c +++ b/samples/sid_end_device/src/cli/app_shell.c @@ -27,9 +27,7 @@ #include #endif -#ifdef CONFIG_SIDEWALK_FILE_TRANSFER_DFU -#include -#endif +#include #define CLI_CMD_OPT_LINK_BLE 1 #define CLI_CMD_OPT_LINK_FSK 2 @@ -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; } diff --git a/samples/sid_end_device/src/hello/app.c b/samples/sid_end_device/src/hello/app.c index 2f178fc5d6..2227b86c9a 100644 --- a/samples/sid_end_device/src/hello/app.c +++ b/samples/sid_end_device/src/hello/app.c @@ -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; diff --git a/samples/sid_end_device/src/sbdt/dfu_file_transfer.c b/samples/sid_end_device/src/sbdt/dfu_file_transfer.c index 0cd13d63a9..436f67249e 100644 --- a/samples/sid_end_device/src/sbdt/dfu_file_transfer.c +++ b/samples/sid_end_device/src/sbdt/dfu_file_transfer.c @@ -15,6 +15,9 @@ #include #include #include +#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE) +#include +#endif LOG_MODULE_REGISTER(file_transfer, CONFIG_SIDEWALK_LOG_LEVEL); @@ -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); @@ -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))), @@ -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; @@ -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); -} diff --git a/samples/sid_end_device/src/sensor_monitoring/app.c b/samples/sid_end_device/src/sensor_monitoring/app.c index 0e658a704f..b7d4e7919e 100644 --- a/samples/sid_end_device/src/sensor_monitoring/app.c +++ b/samples/sid_end_device/src/sensor_monitoring/app.c @@ -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; diff --git a/utils/include/sidewalk_dfu/nordic_dfu.h b/utils/include/sidewalk_dfu/nordic_dfu.h index 3b4d1cc3f6..593a1be85b 100644 --- a/utils/include/sidewalk_dfu/nordic_dfu.h +++ b/utils/include/sidewalk_dfu/nordic_dfu.h @@ -4,6 +4,8 @@ * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ +#include + #ifndef NORDIC_DFU_H #define NORDIC_DFU_H @@ -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 */ diff --git a/utils/sidewalk_dfu/nordic_dfu.c b/utils/sidewalk_dfu/nordic_dfu.c index 8f3eeff382..f64bff7dcf 100644 --- a/utils/sidewalk_dfu/nordic_dfu.c +++ b/utils/sidewalk_dfu/nordic_dfu.c @@ -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) { @@ -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; @@ -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); @@ -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);