Rebase private OTA libs on 2.7

This commit is contained in:
Pim van Pelt
2018-10-28 12:12:24 +01:00
parent 497c62b382
commit 9ee393bc8a
29 changed files with 3836 additions and 521 deletions

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2014-2018 Cesanta Software Limited
* All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the ""License"");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an ""AS IS"" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
bool cc3200_upd_init(void);
const char *cc3200_upd_get_fs_container_prefix(void);
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2014-2018 Cesanta Software Limited
* All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the ""License"");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an ""AS IS"" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
void esp32_updater_early_init();
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2014-2016 Cesanta Software Limited
* All rights reserved
*/
/*
* OTA API.
*
* See https://mongoose-os.com/docs/mos/userguide/ota.md for more details about
* Mongoose OS OTA mechanism.
*/
#ifndef CS_FW_SRC_MGOS_UPDATER_H_
#define CS_FW_SRC_MGOS_UPDATER_H_
#include <stdbool.h>
#include "frozen.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
struct mgos_upd_file_info {
char name[50];
uint32_t size;
uint32_t processed;
};
struct mgos_upd_info {
/* Data from the manifest, available from BEGIN until END */
struct json_token name;
struct json_token platform;
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;
};
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* CS_FW_SRC_MGOS_UPDATER_H_ */

View File

@ -0,0 +1,132 @@
/*
* Copyright (c) 2014-2016 Cesanta Software Limited
* All rights reserved
*
* Common bits of code handling update process.
* Driven externaly by data source - mg_rpc or POST file upload.
*/
#ifndef CS_FW_SRC_MGOS_UPDATER_COMMON_H_
#define CS_FW_SRC_MGOS_UPDATER_COMMON_H_
#include <stdbool.h>
#include <stdint.h>
#include "frozen.h"
#include "mgos_event.h"
#include "mgos_timers.h"
#include "mgos_updater.h"
#include "mgos_updater_hal.h"
#include "mongoose.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
struct update_context;
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" */
MGOS_OTA_STATE_ERROR, /* "error" */
MGOS_OTA_STATE_SUCCESS, /* "success" */
};
struct mgos_ota_status {
bool is_committed;
int commit_timeout;
int partition;
enum mgos_ota_state state;
const char *msg; /* stringified state */
int progress_percent; /* valid only for "progress" state */
};
const char *mgos_ota_state_str(enum mgos_ota_state state);
struct update_context {
int update_state; /* Internal state machine - parsing zip, etc */
enum mgos_ota_state ota_state; /* Externally visible */
const char *status_msg;
char *zip_file_url;
size_t zip_file_size;
size_t bytes_already_downloaded;
size_t last_reported_bytes;
double last_reported_time;
const char *data;
size_t data_len;
struct mbuf unprocessed;
struct mgos_upd_info info;
uint32_t current_file_crc;
uint32_t current_file_crc_calc;
bool current_file_has_descriptor;
bool ignore_same_version;
bool need_reboot;
int result;
mgos_updater_result_cb result_cb;
char *manifest_data;
char file_name[50];
struct mgos_upd_hal_ctx *dev_ctx;
mgos_timer_id wdt;
int wdt_timeout_ms;
/* Network connection associated with this update, if any.
* It is only used in case update times out - it is closed. */
struct mg_connection *nc;
/*
* At the end of update this struct is written to a file
* and then restored after reboot.
*/
struct update_file_context {
int commit_timeout;
} fctx __attribute__((packed));
};
struct update_context *updater_context_create(int timeout);
/*
* Returns updater context of the update in progress. If no update is in
* progress, returns NULL.
*/
struct update_context *updater_context_get_current(void);
int updater_process(struct update_context *ctx, const char *data, size_t len);
void updater_finish(struct update_context *ctx);
void updater_context_free(struct update_context *ctx);
int updater_finalize(struct update_context *ctx);
int is_write_finished(struct update_context *ctx);
int is_update_finished(struct update_context *ctx);
int is_reboot_required(struct update_context *ctx);
void mgos_upd_boot_finish(bool is_successful, bool is_first);
bool mgos_upd_commit();
bool mgos_upd_is_committed();
bool mgos_upd_revert(bool reboot);
bool mgos_upd_get_status(struct mgos_ota_status *);
int mgos_upd_get_commit_timeout();
bool mgos_upd_set_commit_timeout(int commit_timeout);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* CS_FW_SRC_MGOS_UPDATER_COMMON_H_ */

View File

@ -0,0 +1,108 @@
/*
* Copyright (c) 2014-2016 Cesanta Software Limited
* All rights reserved
*
* Updater HAL. Devices need to implement this interface.
*/
#ifndef CS_FW_SRC_MGOS_UPDATER_HAL_H_
#define CS_FW_SRC_MGOS_UPDATER_HAL_H_
#include <inttypes.h>
#include "common/mbuf.h"
#include "common/mg_str.h"
#include "frozen.h"
#include "mgos_updater.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
struct mgos_upd_hal_ctx *mgos_upd_hal_ctx_create(void);
const char *mgos_upd_get_status_msg(struct mgos_upd_hal_ctx *ctx);
/*
* Process the firmware manifest. Parsed manifest is available in ctx->manifest,
* and for convenience the "parts" key within it is in ctx->parts.
* Return >= 0 if ok, < 0 + ctx->status_msg on error.
*/
int mgos_upd_begin(struct mgos_upd_hal_ctx *ctx, struct json_token *parts);
/*
* Decide what to do with the next file.
* In case of abort, message should be provided in status_msg.
*/
enum mgos_upd_file_action {
MGOS_UPDATER_ABORT,
MGOS_UPDATER_PROCESS_FILE,
MGOS_UPDATER_SKIP_FILE,
};
enum mgos_upd_file_action mgos_upd_file_begin(
struct mgos_upd_hal_ctx *ctx, const struct mgos_upd_file_info *fi);
#ifndef MGOS_UPDATER_DATA_CHUNK_SIZE
#define MGOS_UPDATER_DATA_CHUNK_SIZE 512
#endif
/*
* Process a chunk of file data. Data will be delivered to this function in
* MGOS_UPDATER_DATA_CHUNK_SIZE chunks.
* Return number of bytes processed (0 .. data.len)
* or < 0 for error. In case of error, message should be provided in status_msg.
*/
int mgos_upd_file_data(struct mgos_upd_hal_ctx *ctx,
const struct mgos_upd_file_info *fi, struct mg_str data);
/*
* Finalize a file. Remainder of the data (if any) is passed,
* number of bytes of that data processed should be returned. The amount of data
* will be less than MGOS_UPDATER_DATA_CHUNK_SIZE.
* Value equal to data.len is an indication of success,
* < 0 + ctx->status_msg on error.
*/
int mgos_upd_file_end(struct mgos_upd_hal_ctx *ctx,
const struct mgos_upd_file_info *fi, struct mg_str data);
/*
* Finalize the update.
* Return >= 0 if ok, < 0 + ctx->status_msg on error.
*/
int mgos_upd_finalize(struct mgos_upd_hal_ctx *ctx);
bool mgos_upd_is_first_boot(void);
void mgos_upd_hal_ctx_free(struct mgos_upd_hal_ctx *ctx);
/* Apply update on first boot, usually involves merging filesystem. */
int mgos_upd_apply_update(void);
/*
* Create a snapshot of currently running firmware (including FS) in
* a currently inactive slot. There must be no uncommitted update
* in progress.
* Returns slot id used for snapshot or < 0 in case of error.
*/
int mgos_upd_create_snapshot(void);
struct mgos_upd_boot_state {
/* Slot that will be used to load firmware during next boot. */
int active_slot;
/* Whether the boot configuration is committed or not.
* Reboot with uncommitted configration reverts to revert_slot. */
bool is_committed;
/* Slot that will be used in case of revert, explicit or implicit. */
int revert_slot;
};
bool mgos_upd_boot_get_state(struct mgos_upd_boot_state *bs);
bool mgos_upd_boot_set_state(const struct mgos_upd_boot_state *bs);
/* Shortcuts for get and set */
void mgos_upd_boot_commit(void);
void mgos_upd_boot_revert(void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* CS_FW_SRC_MGOS_UPDATER_HAL_H_ */

View File

@ -0,0 +1,22 @@
/*
* Copyright (c) 2014-2016 Cesanta Software Limited
* All rights reserved
*/
#ifndef CS_FW_SRC_MGOS_UPDATER_UTIL_H_
#define CS_FW_SRC_MGOS_UPDATER_UTIL_H_
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* Function to merge filesystems. */
bool mgos_upd_merge_fs(const char *old_fs_path, const char *new_fs_path);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* CS_FW_SRC_MGOS_UPDATER_UTIL_H_ */