From 2dcfb0efdddf14f446d7f6d2efbb729c31183b77 Mon Sep 17 00:00:00 2001 From: Roman PASSLER Date: Wed, 19 Jun 2024 10:57:25 +0200 Subject: [PATCH] dfu: Refactor reboot process using delayed work item The response 0x84 might not be sent, because the reboot is initiated already, so make the reboot process delayed with a work item. --- src/dfu.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/dfu.c b/src/dfu.c index 58bccdd..ef7fe29 100644 --- a/src/dfu.c +++ b/src/dfu.c @@ -25,6 +25,7 @@ static THINGSET_DEFINE_BYTES(bytes_item, bytes_buf, 0); static int32_t thingset_dfu_init(); static int32_t thingset_dfu_write(); static int32_t thingset_dfu_boot(); +static void thingset_dfu_reboot_work_handler(struct k_work *work); THINGSET_ADD_GROUP(TS_ID_ROOT, TS_ID_DFU, "DFU", THINGSET_NO_CALLBACK); THINGSET_ADD_FN_INT32(TS_ID_DFU, TS_ID_DFU_INIT, "xInit", &thingset_dfu_init, THINGSET_ANY_RW); @@ -36,6 +37,8 @@ static bool dfu_initialized = false; static struct flash_img_context flash_img_ctx; +K_WORK_DELAYABLE_DEFINE(reboot_work, &thingset_dfu_reboot_work_handler); + static int32_t thingset_dfu_init() { int err; @@ -99,9 +102,16 @@ static int32_t thingset_dfu_boot() return err; } - LOG_INF("DFU finished, rebooting..."); + LOG_INF("DFU finished, scheduling reboot..."); - sys_reboot(SYS_REBOOT_COLD); + /* Schedule the reboot work to be executed after 1 second */ + k_work_schedule(&reboot_work, K_SECONDS(1)); return 0; } + +static void thingset_dfu_reboot_work_handler(struct k_work *work) +{ + LOG_INF("Rebooting now..."); + sys_reboot(SYS_REBOOT_COLD); +}