Skip to content

Commit

Permalink
Add MGOS_EVENT_OTA_BEGIN event
Browse files Browse the repository at this point in the history
CL: Add MGOS_EVENT_OTA_BEGIN event. Move event definitions to OTA lib.

PUBLISHED_FROM=91184eeb400ef8a38d3e7437620922e8dacebe2e
  • Loading branch information
cpq authored and cesantabot committed Aug 22, 2018
1 parent 921a6b0 commit ffb1971
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 69 deletions.
69 changes: 1 addition & 68 deletions include/mgos_updater.h
Expand Up @@ -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 */
Expand Down
9 changes: 9 additions & 0 deletions include/mgos_updater_common.h
Expand Up @@ -13,6 +13,7 @@
#include <stdint.h>

#include "frozen.h"
#include "mgos_event.h"
#include "mgos_timers.h"
#include "mgos_updater.h"
#include "mgos_updater_hal.h"
Expand All @@ -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" */
Expand Down
8 changes: 7 additions & 1 deletion src/mgos_updater_common.c
Expand Up @@ -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);
Expand Down

0 comments on commit ffb1971

Please sign in to comment.