From ffb1971bdcc3e061d80fee90a5f47c3029cc7569 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Wed, 22 Aug 2018 09:05:32 +0100 Subject: [PATCH] Add MGOS_EVENT_OTA_BEGIN event CL: Add MGOS_EVENT_OTA_BEGIN event. Move event definitions to OTA lib. PUBLISHED_FROM=91184eeb400ef8a38d3e7437620922e8dacebe2e --- include/mgos_updater.h | 69 +---------------------------------- include/mgos_updater_common.h | 9 +++++ src/mgos_updater_common.c | 8 +++- 3 files changed, 17 insertions(+), 69 deletions(-) diff --git a/include/mgos_updater.h b/include/mgos_updater.h index 86c0865..72aa91a 100644 --- a/include/mgos_updater.h +++ b/include/mgos_updater.h @@ -34,79 +34,12 @@ struct mgos_upd_info { struct json_token version; struct json_token build_id; struct json_token parts; + bool abort; /* If MGOS_EVENT_OTA_BEGIN handler sets this to true, abort OTA */ /* Current file, available in PROGRESS. */ struct mgos_upd_file_info current_file; }; -enum mgos_upd_event { - /* ev_data = NULL */ - MGOS_UPD_EV_INIT = 1, - /* ev_data = const struct mgos_upd_info * */ - MGOS_UPD_EV_BEGIN = 2, - /* ev_data = const struct mgos_upd_info * */ - MGOS_UPD_EV_PROGRESS = 3, - /* ev_data = struct update_context * */ - MGOS_UPD_EV_END = 4, - /* ev_data = NULL */ - MGOS_UPD_EV_ROLLBACK = 5, - /* ev_data = NULL */ - MGOS_UPD_EV_COMMIT = 6, - /* ev_data = NULL */ - MGOS_UPD_EV_ERROR = 7, -}; - -/* - * User application can register a callback on FW update events. - * An event is dispatched to the callback at various stages: - * - INIT: the very beginning of an update, nothing is known yet. - * - BEGIN: Update manifest has been parsed, metadata fields are known. - * - PROGRESS: Invoked repeatedly as files are being processed. - * - END: Invoked at the end, with the overall result of the update. - * - * At the INIT and BEGIN stage the app can interfere and decline an update - * by returning false from the callback. - */ - -typedef bool (*mgos_upd_event_cb)(enum mgos_upd_event ev, const void *ev_arg, - void *cb_arg); - -/* - * Set update event callback. - * - * Example code: -``` -bool upd_cb(enum mgos_upd_event ev, const void *ev_arg, void *cb_arg) { - switch (ev) { - case MGOS_UPD_EV_INIT: { - LOG(LL_INFO, ("INIT")); - return true; - } - case MGOS_UPD_EV_BEGIN: { - const struct mgos_upd_info *info = (const struct mgos_upd_info *) ev_arg; - LOG(LL_INFO, ("BEGIN %.*s", (int) info->build_id.len, - info->build_id.ptr)); - return true; - } - case MGOS_UPD_EV_PROGRESS: { - const struct mgos_upd_info *info = (const struct mgos_upd_info *) ev_arg; - LOG(LL_INFO, ("Progress: %s %d of %d", info->current_file.name, - info->current_file.processed, info->current_file.size)); - break; - } - case MGOS_UPD_EV_END: { - int result = *((int *) ev_arg); - LOG(LL_INFO, ("END, result %d", result)); - break; - } - } - (void) cb_arg; - return false; -} -``` -*/ -void mgos_upd_set_event_cb(mgos_upd_event_cb cb, void *cb_arg); - #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/include/mgos_updater_common.h b/include/mgos_updater_common.h index 104e5cf..8e4927b 100644 --- a/include/mgos_updater_common.h +++ b/include/mgos_updater_common.h @@ -13,6 +13,7 @@ #include #include "frozen.h" +#include "mgos_event.h" #include "mgos_timers.h" #include "mgos_updater.h" #include "mgos_updater_hal.h" @@ -27,6 +28,14 @@ typedef void (*mgos_updater_result_cb)(struct update_context *ctx); struct mgos_upd_hal_ctx; /* This struct is defined by HAL and is opaque to us */ +#define MGOS_EVENT_OTA_BASE MGOS_EVENT_BASE('O', 'T', 'A') +enum mgos_event_ota { + MGOS_EVENT_OTA_BEGIN = + MGOS_EVENT_OTA_BASE, /* ev_data: struct mgos_upd_info */ + MGOS_EVENT_OTA_STATUS, /* ev_data: struct mgos_ota_status */ + MGOS_EVENT_OTA_REQUEST, /* ev_data: struct ota_request_param */ +}; + enum mgos_ota_state { MGOS_OTA_STATE_IDLE = 0, /* idle */ MGOS_OTA_STATE_PROGRESS, /* "progress" */ diff --git a/src/mgos_updater_common.c b/src/mgos_updater_common.c index 644b967..2e02607 100644 --- a/src/mgos_updater_common.c +++ b/src/mgos_updater_common.c @@ -465,7 +465,13 @@ static int updater_process_int(struct update_context *ctx, const char *data, LOG(LL_ERROR, ("Bad manifest: %d %s", ret, ctx->status_msg)); return ret; } - if (ctx->result) return ctx->result; + + ctx->info.abort = false; + mgos_event_trigger(MGOS_EVENT_OTA_BEGIN, &ctx->info); + if (ctx->info.abort) { + ctx->status_msg = "OTA aborted by the MGOS_EVENT_OTA_BEGIN handler"; + return -1; + } context_clear_current_file(ctx); updater_set_status(ctx, US_WAITING_FILE_HEADER);