mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-31 13:00:49 +00:00
Refactor OTA API
This commit is contained in:
1 parent
3319ce78d3
commit
9b99f62311
27 files changed
+1716
-2143
No files matched your search
@@ -196,49 +196,9 @@ static void handle_firmware_upload(struct mg_connection *c,
|
||||
mg_http_reply(c, 500, "", "mg_ota_end() failed\n", tot);
|
||||
} else {
|
||||
mg_http_reply(c, 200, s_json_header, "true\n");
|
||||
if (data.len == 0) {
|
||||
// Successful mg_ota_end() called, schedule device reboot
|
||||
mg_timer_add(c->mgr, 500, 0, (void (*)(void *)) mg_device_reset, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_firmware_commit(struct mg_connection *c) {
|
||||
mg_http_reply(c, 200, s_json_header, "%s\n",
|
||||
mg_ota_commit() ? "true" : "false");
|
||||
}
|
||||
|
||||
static void handle_firmware_rollback(struct mg_connection *c) {
|
||||
mg_http_reply(c, 200, s_json_header, "%s\n",
|
||||
mg_ota_rollback() ? "true" : "false");
|
||||
}
|
||||
|
||||
static size_t print_status(void (*out)(char, void *), void *ptr, va_list *ap) {
|
||||
int fw = va_arg(*ap, int);
|
||||
return mg_xprintf(out, ptr, "{%m:%d,%m:%c%lx%c,%m:%u,%m:%u}\n",
|
||||
MG_ESC("status"), mg_ota_status(fw), MG_ESC("crc32"), '"',
|
||||
mg_ota_crc32(fw), '"', MG_ESC("size"), mg_ota_size(fw),
|
||||
MG_ESC("timestamp"), mg_ota_timestamp(fw));
|
||||
}
|
||||
|
||||
static void handle_firmware_status(struct mg_connection *c) {
|
||||
mg_http_reply(c, 200, s_json_header, "[%M,%M]\n", print_status,
|
||||
MG_FIRMWARE_CURRENT, print_status, MG_FIRMWARE_PREVIOUS);
|
||||
}
|
||||
|
||||
static void handle_device_reset(struct mg_connection *c) {
|
||||
mg_http_reply(c, 200, s_json_header, "true\n");
|
||||
mg_timer_add(c->mgr, 500, 0, (void (*)(void *)) mg_device_reset, NULL);
|
||||
}
|
||||
|
||||
static void handle_device_eraselast(struct mg_connection *c) {
|
||||
size_t ss = mg_flash_sector_size(), size = mg_flash_size();
|
||||
char *base = (char *) mg_flash_start(), *last = base + size - ss;
|
||||
if (mg_flash_bank() == 2) last -= size / 2;
|
||||
mg_flash_erase(last);
|
||||
mg_http_reply(c, 200, s_json_header, "true\n");
|
||||
}
|
||||
|
||||
// HTTP request handler function
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_ACCEPT) {
|
||||
@@ -270,16 +230,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
handle_settings_set(c, hm->body);
|
||||
} else if (mg_match(hm->uri, mg_str("/api/firmware/upload"), NULL)) {
|
||||
handle_firmware_upload(c, hm);
|
||||
} else if (mg_match(hm->uri, mg_str("/api/firmware/commit"), NULL)) {
|
||||
handle_firmware_commit(c);
|
||||
} else if (mg_match(hm->uri, mg_str("/api/firmware/rollback"), NULL)) {
|
||||
handle_firmware_rollback(c);
|
||||
} else if (mg_match(hm->uri, mg_str("/api/firmware/status"), NULL)) {
|
||||
handle_firmware_status(c);
|
||||
} else if (mg_match(hm->uri, mg_str("/api/device/reset"), NULL)) {
|
||||
handle_device_reset(c);
|
||||
} else if (mg_match(hm->uri, mg_str("/api/device/eraselast"), NULL)) {
|
||||
handle_device_eraselast(c);
|
||||
} else {
|
||||
struct mg_http_serve_opts opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
|
||||
@@ -27,7 +27,15 @@ void app_main(void) {
|
||||
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
|
||||
}
|
||||
|
||||
#if MG_DEVICE == MG_DEVICE_CUSTOM
|
||||
#if MG_OTA == MG_OTA_CUSTOM
|
||||
enum {
|
||||
MG_OTA_UNAVAILABLE = 0, // No OTA information is present
|
||||
MG_OTA_FIRST_BOOT = 1, // Device booting the first time after the OTA
|
||||
MG_OTA_UNCOMMITTED = 2, // Ditto, but marking us for the rollback
|
||||
MG_OTA_COMMITTED = 3 // The firmware is good
|
||||
};
|
||||
enum { MG_FIRMWARE_CURRENT = 0, MG_FIRMWARE_PREVIOUS = 1 };
|
||||
|
||||
void *mg_flash_start(void) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -57,9 +65,7 @@ bool mg_flash_write(void *addr, const void *buf, size_t len) {
|
||||
void mg_device_reset(void) {
|
||||
esp_restart();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if MG_OTA == MG_OTA_CUSTOM
|
||||
#include "esp_app_format.h"
|
||||
#include "esp_ota_ops.h"
|
||||
|
||||
@@ -121,6 +127,14 @@ bool check_fw_header(const void* buf, size_t len) {
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t mg_ota_size(int fw) {
|
||||
const esp_partition_t *p = get_partition_from_fw(fw);
|
||||
if (NULL == p) {
|
||||
return 0;
|
||||
}
|
||||
return p->size;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
rx_checked = false;
|
||||
if (s_size) {
|
||||
@@ -255,11 +269,4 @@ uint32_t mg_ota_timestamp(int fw) {
|
||||
return mktime(&datetime);
|
||||
}
|
||||
|
||||
size_t mg_ota_size(int fw) {
|
||||
const esp_partition_t *p = get_partition_from_fw(fw);
|
||||
if (NULL == p) {
|
||||
return 0;
|
||||
}
|
||||
return p->size;
|
||||
}
|
||||
#endif
|
||||
@@ -3,4 +3,3 @@
|
||||
#define MG_ENABLE_PACKED_FS 1
|
||||
#define MG_TLS MG_TLS_NONE // change to 'MG_TLS_MBED' to enable TLS
|
||||
#define MG_OTA MG_OTA_CUSTOM
|
||||
#define MG_DEVICE MG_DEVICE_CUSTOM
|
||||
@@ -241,7 +241,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
} else if (mg_match(hm->uri, mg_str("/api/modbus/exec"), NULL)) {
|
||||
handle_modbus_exec(c, hm->body);
|
||||
} else if (mg_match(hm->uri, mg_str("/api/device/reset"), NULL)) {
|
||||
mg_timer_add(c->mgr, 500, 0, (void (*)(void *)) mg_device_reset, NULL);
|
||||
mg_http_reply(c, 200, s_json_header, "true\n");
|
||||
} else {
|
||||
struct mg_http_serve_opts opts;
|
||||
|
||||
@@ -53,15 +53,6 @@ static void set_device_id(void) {
|
||||
g_device_id = strdup(buf);
|
||||
}
|
||||
|
||||
static size_t print_fw_status(void (*out)(char, void *), void *ptr,
|
||||
va_list *ap) {
|
||||
int fw = va_arg(*ap, int);
|
||||
return mg_xprintf(out, ptr, "{%m:%d,%m:%c%lx%c,%m:%u,%m:%u}",
|
||||
MG_ESC("status"), mg_ota_status(fw), MG_ESC("crc32"), '"',
|
||||
mg_ota_crc32(fw), '"', MG_ESC("size"), mg_ota_size(fw),
|
||||
MG_ESC("timestamp"), mg_ota_timestamp(fw));
|
||||
}
|
||||
|
||||
static size_t print_shorts(void (*out)(char, void *), void *ptr, va_list *ap) {
|
||||
uint16_t *array = va_arg(*ap, uint16_t *);
|
||||
int i, len = 0, num_elems = va_arg(*ap, int);
|
||||
@@ -87,7 +78,7 @@ static void publish_status(struct mg_connection *c) {
|
||||
|
||||
// Print JSON notification into the io buffer
|
||||
mg_xprintf(mg_pfn_iobuf, &io,
|
||||
"{%m:%m,%m:{%m:%m,%m:%d,%m:%d,%m:[%M],%m:[%M],%m:%M,%m:%M}}", //
|
||||
"{%m:%m,%m:{%m:%m,%m:%d,%m:%d,%m:[%M],%m:[%M]}}", //
|
||||
MG_ESC("method"), MG_ESC("status.notify"), MG_ESC("params"), //
|
||||
MG_ESC("status"), MG_ESC("online"), //
|
||||
MG_ESC(("log_level")), s_device_config.log_level, //
|
||||
@@ -95,9 +86,7 @@ static void publish_status(struct mg_connection *c) {
|
||||
MG_ESC(("pin_map")), print_shorts, s_device_config.pin_map,
|
||||
s_device_config.pin_count, //
|
||||
MG_ESC(("pin_state")), print_bools, s_device_config.pin_state,
|
||||
s_device_config.pin_count, //
|
||||
MG_ESC(("crnt_fw")), print_fw_status, MG_FIRMWARE_CURRENT, //
|
||||
MG_ESC(("prev_fw")), print_fw_status, MG_FIRMWARE_PREVIOUS);
|
||||
s_device_config.pin_count);
|
||||
|
||||
memset(&pub_opts, 0, sizeof(pub_opts));
|
||||
mg_snprintf(topic, sizeof(topic), "%s/%s/status", g_root_topic, g_device_id);
|
||||
@@ -160,27 +149,6 @@ static void rpc_config_set(struct mg_rpc_req *r) {
|
||||
}
|
||||
}
|
||||
|
||||
static void rpc_ota_commit(struct mg_rpc_req *r) {
|
||||
if (mg_ota_commit()) {
|
||||
mg_rpc_ok(r, "%m", MG_ESC("ok"));
|
||||
} else {
|
||||
mg_rpc_err(r, 1, "Failed to commit the firmware");
|
||||
}
|
||||
}
|
||||
|
||||
static void rpc_device_reset(struct mg_rpc_req *r) {
|
||||
mg_rpc_ok(r, "%m", MG_ESC("ok"));
|
||||
mg_timer_add(s_conn->mgr, 500, 0, (void (*)(void *)) mg_device_reset, NULL);
|
||||
}
|
||||
|
||||
static void rpc_ota_rollback(struct mg_rpc_req *r) {
|
||||
if (mg_ota_rollback()) {
|
||||
mg_rpc_ok(r, "%m", MG_ESC("ok"));
|
||||
} else {
|
||||
mg_rpc_err(r, 1, "Failed to rollback to the previous firmware");
|
||||
}
|
||||
}
|
||||
|
||||
static void rpc_ota_upload(struct mg_rpc_req *r) {
|
||||
long ofs = mg_json_get_long(r->frame, "$.params.offset", -1);
|
||||
long tot = mg_json_get_long(r->frame, "$.params.total", -1);
|
||||
@@ -200,10 +168,6 @@ static void rpc_ota_upload(struct mg_rpc_req *r) {
|
||||
mg_rpc_err(r, 1, "mg_ota_end() failed\n", tot);
|
||||
} else {
|
||||
mg_rpc_ok(r, "%m", MG_ESC("ok"));
|
||||
if (len == 0) { // Successful mg_ota_end() called, schedule device reboot
|
||||
mg_timer_add(s_conn->mgr, 500, 0, (void (*)(void *)) mg_device_reset,
|
||||
NULL);
|
||||
}
|
||||
}
|
||||
free(buf);
|
||||
}
|
||||
@@ -297,11 +261,8 @@ void web_init(struct mg_mgr *mgr) {
|
||||
|
||||
// Configure JSON-RPC functions we're going to handle
|
||||
mg_rpc_add(&s_rpc, mg_str("config.set"), rpc_config_set, NULL);
|
||||
mg_rpc_add(&s_rpc, mg_str("ota.commit"), rpc_ota_commit, NULL);
|
||||
mg_rpc_add(&s_rpc, mg_str("ota.rollback"), rpc_ota_rollback, NULL);
|
||||
mg_rpc_add(&s_rpc, mg_str("ota.upload"), rpc_ota_upload, NULL);
|
||||
mg_rpc_add(&s_rpc, mg_str("device.reset"), rpc_device_reset, NULL);
|
||||
|
||||
|
||||
mg_timer_add(mgr, 3000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_reconnect,
|
||||
mgr);
|
||||
mg_timer_add(mgr, ping_interval_ms, MG_TIMER_REPEAT, timer_ping, mgr);
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
// See https://mongoose.ws/documentation/#build-options
|
||||
#define MG_ARCH MG_ARCH_NEWLIB
|
||||
#define MG_OTA MG_OTA_FLASH
|
||||
#define MG_DEVICE MG_DEVICE_RT1060
|
||||
#define MG_OTA MG_OTA_RT1060
|
||||
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_DRIVER_IMXRT 1
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
// See https://mongoose.ws/documentation/#build-options
|
||||
#define MG_ARCH MG_ARCH_NEWLIB
|
||||
#define MG_OTA MG_OTA_FLASH
|
||||
#define MG_DEVICE MG_DEVICE_STM32H5
|
||||
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_CUSTOM_MILLIS 1
|
||||
|
||||
@@ -7,16 +7,16 @@
|
||||
#define BLINK_PERIOD_MS 1000 // LED_PIN blinking period in millis
|
||||
|
||||
// This flash space resides at after the 0-wait 320k area
|
||||
static char *s_flash_space = (char *) (0x8000000 + 320 * 1024);
|
||||
// static char *s_flash_space = (char *) (0x8000000 + 320 * 1024);
|
||||
|
||||
bool web_load_settings(void *buf, size_t len) {
|
||||
if (*(uint32_t *) s_flash_space != SETTINGS_MAGIC) return false;
|
||||
memcpy(buf, s_flash_space, len);
|
||||
return true;
|
||||
(void) buf, (void) len;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool web_save_settings(void *buf, size_t len) {
|
||||
return mg_flash_write(s_flash_space, buf, len);
|
||||
(void) buf, (void) len;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void timer_fn(void *arg) {
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
// See https://mongoose.ws/documentation/#build-options
|
||||
#define MG_ARCH MG_ARCH_NEWLIB
|
||||
#define MG_OTA MG_OTA_FLASH
|
||||
#define MG_DEVICE MG_DEVICE_CH32V307
|
||||
#define MG_OTA MG_OTA_CH32V307
|
||||
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_CUSTOM_MILLIS 1
|
||||
|
||||
Reference in new issue
Block a user