mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-25 20:28:27 +00:00
Add MG_OTA_{ROLLBACK,STATE_SET,STATE_GET}
This commit is contained in:
1 parent
a6452f5311
commit
67f249c58a
16 files changed
+345
-131
No files matched your search
@@ -43,17 +43,36 @@ bool mg_random(void *buf, size_t len) {
|
||||
}
|
||||
|
||||
static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) { // New HTTP request received
|
||||
if (ev == MG_EV_HTTP_HDRS) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
// Start OTA on headers so body chunks stream directly to flash (no RAM buffer)
|
||||
if (mg_match(hm->uri, mg_str("/api/ota/update"), NULL)) {
|
||||
mg_http_start_ota(c, hm, NULL);
|
||||
}
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_match(hm->uri, mg_str("/api/tick"), NULL)) {
|
||||
mg_http_reply(c, 200, "", "{%m:%llu}\n", MG_ESC("tick"), hal_get_tick());
|
||||
} else if (mg_match(hm->uri, mg_str("/api/ota/commit"), NULL)) {
|
||||
c->data[0] = 1;
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
} else if (mg_match(hm->uri, mg_str("/api/ota/rollback"), NULL)) {
|
||||
c->data[0] = 2;
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
} else {
|
||||
mg_http_reply(c, 200, "", "Hi from Mongoose, tick %llu\n", hal_get_tick());
|
||||
}
|
||||
} else if (ev == MG_EV_CLOSE && c->data[0] == 1) {
|
||||
MG_OTA_STATE_SET(MG_OTA_CONFIRMED);
|
||||
NVIC_SystemReset();
|
||||
} else if (ev == MG_EV_CLOSE && c->data[0] == 2) {
|
||||
MG_OTA_STATE_SET(MG_OTA_CONFIRMED);
|
||||
MG_OTA_ROLLBACK();
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
hal_clock_init();
|
||||
hal_uart_init(UART_DEBUG, UART_DEBUG_TX_PIN, UART_DEBUG_RX_PIN, 115200);
|
||||
hal_rng_init();
|
||||
hal_gpio_output(LED_1);
|
||||
@@ -62,7 +81,11 @@ int main(void) {
|
||||
|
||||
hal_ethernet_init();
|
||||
|
||||
// Start a minimal web server
|
||||
MG_INFO(("Initialised. CPU clock: %lu MHz", SystemCoreClock / 1000000));
|
||||
|
||||
// Max delay 131 seconds
|
||||
MG_OTA_BOOT_CHECK(30);
|
||||
|
||||
struct mg_mgr mgr;
|
||||
mg_mgr_init(&mgr);
|
||||
mg_http_listen(&mgr, "http://0.0.0.0", http_ev_handler, NULL);
|
||||
|
||||
Reference in new issue
Block a user