From 2b559e2a404171a2dcc75df5001d882f5cd1647f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fin=20Maa=C3=9F?= Date: Wed, 24 Apr 2024 11:37:18 +0200 Subject: [PATCH] mgmt: hawkbit: delay autohandler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Be able to delay the next run of the hawkbit autohandler. Signed-off-by: Fin Maaß --- include/zephyr/mgmt/hawkbit.h | 22 ++++++++++++++++++++++ subsys/mgmt/hawkbit/hawkbit.c | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/zephyr/mgmt/hawkbit.h b/include/zephyr/mgmt/hawkbit.h index a0c7d6c280b9c5f..01c637aeb87fdd9 100644 --- a/include/zephyr/mgmt/hawkbit.h +++ b/include/zephyr/mgmt/hawkbit.h @@ -114,6 +114,28 @@ void hawkbit_autohandler(bool auto_reschedule); */ enum hawkbit_response hawkbit_autohandler_wait(uint32_t events, k_timeout_t timeout); +/** + * @brief Cancel the run of the hawkBit autohandler. + * + * @return a value from k_work_cancel_delayable(). + */ +int hawkbit_autohandler_cancel(void); + +/** + * @brief Set the delay for the next run of the autohandler. + * + * @details This function will only delay the next run of the autohandler. The delay will not + * persist after the autohandler runs. + * + * @param timeout The delay to set. + * @param if_bigger If true, the delay will be set only if the new delay is bigger than the current + * one. + * + * @return 0 if @a if_bigger was true and the current delay was bigger than the new one. + * @return otherwise, a value from k_work_reschedule(). + */ +int hawkbit_autohandler_set_delay(k_timeout_t timeout, bool if_bigger); + /** * @brief The hawkBit probe verify if there is some update to be performed. * diff --git a/subsys/mgmt/hawkbit/hawkbit.c b/subsys/mgmt/hawkbit/hawkbit.c index 11558720a9a9879..a983e53244373c8 100644 --- a/subsys/mgmt/hawkbit/hawkbit.c +++ b/subsys/mgmt/hawkbit/hawkbit.c @@ -1513,6 +1513,24 @@ enum hawkbit_response hawkbit_autohandler_wait(uint32_t events, k_timeout_t time return HAWKBIT_NO_RESPONSE; } +int hawkbit_autohandler_cancel(void) +{ + return k_work_cancel_delayable(&hawkbit_work_handle); +} + +int hawkbit_autohandler_set_delay(k_timeout_t timeout, bool if_bigger) +{ + if (!if_bigger || timeout.ticks > k_work_delayable_remaining_get(&hawkbit_work_handle)) { + hawkbit_autohandler_cancel(); + LOG_INF("Setting new delay for next run: %02u:%02u:%02u", + (uint32_t)(timeout.ticks / CONFIG_SYS_CLOCK_TICKS_PER_SEC) / 3600, + (uint32_t)((timeout.ticks / CONFIG_SYS_CLOCK_TICKS_PER_SEC) % 3600) / 60, + (uint32_t)(timeout.ticks / CONFIG_SYS_CLOCK_TICKS_PER_SEC) % 60); + return k_work_reschedule(&hawkbit_work_handle, timeout); + } + return 0; +} + void hawkbit_autohandler(bool auto_reschedule) { if (auto_reschedule) {