Skip to content

Commit e43580e

Browse files
committed
Refactor OTA API
1 parent 3319ce7 commit e43580e

File tree

27 files changed

+6091
-6519
lines changed

27 files changed

+6091
-6519
lines changed

examples/device-dashboard/net.c

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -196,49 +196,9 @@ static void handle_firmware_upload(struct mg_connection *c,
196196
mg_http_reply(c, 500, "", "mg_ota_end() failed\n", tot);
197197
} else {
198198
mg_http_reply(c, 200, s_json_header, "true\n");
199-
if (data.len == 0) {
200-
// Successful mg_ota_end() called, schedule device reboot
201-
mg_timer_add(c->mgr, 500, 0, (void (*)(void *)) mg_device_reset, NULL);
202-
}
203199
}
204200
}
205201

206-
static void handle_firmware_commit(struct mg_connection *c) {
207-
mg_http_reply(c, 200, s_json_header, "%s\n",
208-
mg_ota_commit() ? "true" : "false");
209-
}
210-
211-
static void handle_firmware_rollback(struct mg_connection *c) {
212-
mg_http_reply(c, 200, s_json_header, "%s\n",
213-
mg_ota_rollback() ? "true" : "false");
214-
}
215-
216-
static size_t print_status(void (*out)(char, void *), void *ptr, va_list *ap) {
217-
int fw = va_arg(*ap, int);
218-
return mg_xprintf(out, ptr, "{%m:%d,%m:%c%lx%c,%m:%u,%m:%u}\n",
219-
MG_ESC("status"), mg_ota_status(fw), MG_ESC("crc32"), '"',
220-
mg_ota_crc32(fw), '"', MG_ESC("size"), mg_ota_size(fw),
221-
MG_ESC("timestamp"), mg_ota_timestamp(fw));
222-
}
223-
224-
static void handle_firmware_status(struct mg_connection *c) {
225-
mg_http_reply(c, 200, s_json_header, "[%M,%M]\n", print_status,
226-
MG_FIRMWARE_CURRENT, print_status, MG_FIRMWARE_PREVIOUS);
227-
}
228-
229-
static void handle_device_reset(struct mg_connection *c) {
230-
mg_http_reply(c, 200, s_json_header, "true\n");
231-
mg_timer_add(c->mgr, 500, 0, (void (*)(void *)) mg_device_reset, NULL);
232-
}
233-
234-
static void handle_device_eraselast(struct mg_connection *c) {
235-
size_t ss = mg_flash_sector_size(), size = mg_flash_size();
236-
char *base = (char *) mg_flash_start(), *last = base + size - ss;
237-
if (mg_flash_bank() == 2) last -= size / 2;
238-
mg_flash_erase(last);
239-
mg_http_reply(c, 200, s_json_header, "true\n");
240-
}
241-
242202
// HTTP request handler function
243203
static void fn(struct mg_connection *c, int ev, void *ev_data) {
244204
if (ev == MG_EV_ACCEPT) {
@@ -270,16 +230,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
270230
handle_settings_set(c, hm->body);
271231
} else if (mg_match(hm->uri, mg_str("/api/firmware/upload"), NULL)) {
272232
handle_firmware_upload(c, hm);
273-
} else if (mg_match(hm->uri, mg_str("/api/firmware/commit"), NULL)) {
274-
handle_firmware_commit(c);
275-
} else if (mg_match(hm->uri, mg_str("/api/firmware/rollback"), NULL)) {
276-
handle_firmware_rollback(c);
277-
} else if (mg_match(hm->uri, mg_str("/api/firmware/status"), NULL)) {
278-
handle_firmware_status(c);
279-
} else if (mg_match(hm->uri, mg_str("/api/device/reset"), NULL)) {
280-
handle_device_reset(c);
281-
} else if (mg_match(hm->uri, mg_str("/api/device/eraselast"), NULL)) {
282-
handle_device_eraselast(c);
283233
} else {
284234
struct mg_http_serve_opts opts;
285235
memset(&opts, 0, sizeof(opts));

examples/esp32/device-dashboard/main/main.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ void app_main(void) {
2727
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
2828
}
2929

30-
#if MG_DEVICE == MG_DEVICE_CUSTOM
30+
#if MG_OTA == MG_OTA_CUSTOM
31+
enum {
32+
MG_OTA_UNAVAILABLE = 0, // No OTA information is present
33+
MG_OTA_FIRST_BOOT = 1, // Device booting the first time after the OTA
34+
MG_OTA_UNCOMMITTED = 2, // Ditto, but marking us for the rollback
35+
MG_OTA_COMMITTED = 3 // The firmware is good
36+
};
37+
enum { MG_FIRMWARE_CURRENT = 0, MG_FIRMWARE_PREVIOUS = 1 };
38+
3139
void *mg_flash_start(void) {
3240
return NULL;
3341
}
@@ -57,9 +65,7 @@ bool mg_flash_write(void *addr, const void *buf, size_t len) {
5765
void mg_device_reset(void) {
5866
esp_restart();
5967
}
60-
#endif
6168

62-
#if MG_OTA == MG_OTA_CUSTOM
6369
#include "esp_app_format.h"
6470
#include "esp_ota_ops.h"
6571

@@ -121,6 +127,14 @@ bool check_fw_header(const void* buf, size_t len) {
121127
return true;
122128
}
123129

130+
size_t mg_ota_size(int fw) {
131+
const esp_partition_t *p = get_partition_from_fw(fw);
132+
if (NULL == p) {
133+
return 0;
134+
}
135+
return p->size;
136+
}
137+
124138
bool mg_ota_begin(size_t new_firmware_size) {
125139
rx_checked = false;
126140
if (s_size) {
@@ -255,11 +269,4 @@ uint32_t mg_ota_timestamp(int fw) {
255269
return mktime(&datetime);
256270
}
257271

258-
size_t mg_ota_size(int fw) {
259-
const esp_partition_t *p = get_partition_from_fw(fw);
260-
if (NULL == p) {
261-
return 0;
262-
}
263-
return p->size;
264-
}
265272
#endif

examples/esp32/device-dashboard/main/mongoose_config.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
#define MG_ENABLE_PACKED_FS 1
44
#define MG_TLS MG_TLS_NONE // change to 'MG_TLS_MBED' to enable TLS
55
#define MG_OTA MG_OTA_CUSTOM
6-
#define MG_DEVICE MG_DEVICE_CUSTOM

examples/modbus-dashboard/net.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
241241
} else if (mg_match(hm->uri, mg_str("/api/modbus/exec"), NULL)) {
242242
handle_modbus_exec(c, hm->body);
243243
} else if (mg_match(hm->uri, mg_str("/api/device/reset"), NULL)) {
244-
mg_timer_add(c->mgr, 500, 0, (void (*)(void *)) mg_device_reset, NULL);
245244
mg_http_reply(c, 200, s_json_header, "true\n");
246245
} else {
247246
struct mg_http_serve_opts opts;

examples/mqtt-dashboard/device/net.c

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,6 @@ static void set_device_id(void) {
5353
g_device_id = strdup(buf);
5454
}
5555

56-
static size_t print_fw_status(void (*out)(char, void *), void *ptr,
57-
va_list *ap) {
58-
int fw = va_arg(*ap, int);
59-
return mg_xprintf(out, ptr, "{%m:%d,%m:%c%lx%c,%m:%u,%m:%u}",
60-
MG_ESC("status"), mg_ota_status(fw), MG_ESC("crc32"), '"',
61-
mg_ota_crc32(fw), '"', MG_ESC("size"), mg_ota_size(fw),
62-
MG_ESC("timestamp"), mg_ota_timestamp(fw));
63-
}
64-
6556
static size_t print_shorts(void (*out)(char, void *), void *ptr, va_list *ap) {
6657
uint16_t *array = va_arg(*ap, uint16_t *);
6758
int i, len = 0, num_elems = va_arg(*ap, int);
@@ -87,17 +78,15 @@ static void publish_status(struct mg_connection *c) {
8778

8879
// Print JSON notification into the io buffer
8980
mg_xprintf(mg_pfn_iobuf, &io,
90-
"{%m:%m,%m:{%m:%m,%m:%d,%m:%d,%m:[%M],%m:[%M],%m:%M,%m:%M}}", //
81+
"{%m:%m,%m:{%m:%m,%m:%d,%m:%d,%m:[%M],%m:[%M]}}", //
9182
MG_ESC("method"), MG_ESC("status.notify"), MG_ESC("params"), //
9283
MG_ESC("status"), MG_ESC("online"), //
9384
MG_ESC(("log_level")), s_device_config.log_level, //
9485
MG_ESC(("pin_count")), s_device_config.pin_count, //
9586
MG_ESC(("pin_map")), print_shorts, s_device_config.pin_map,
9687
s_device_config.pin_count, //
9788
MG_ESC(("pin_state")), print_bools, s_device_config.pin_state,
98-
s_device_config.pin_count, //
99-
MG_ESC(("crnt_fw")), print_fw_status, MG_FIRMWARE_CURRENT, //
100-
MG_ESC(("prev_fw")), print_fw_status, MG_FIRMWARE_PREVIOUS);
89+
s_device_config.pin_count);
10190

10291
memset(&pub_opts, 0, sizeof(pub_opts));
10392
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) {
160149
}
161150
}
162151

163-
static void rpc_ota_commit(struct mg_rpc_req *r) {
164-
if (mg_ota_commit()) {
165-
mg_rpc_ok(r, "%m", MG_ESC("ok"));
166-
} else {
167-
mg_rpc_err(r, 1, "Failed to commit the firmware");
168-
}
169-
}
170-
171-
static void rpc_device_reset(struct mg_rpc_req *r) {
172-
mg_rpc_ok(r, "%m", MG_ESC("ok"));
173-
mg_timer_add(s_conn->mgr, 500, 0, (void (*)(void *)) mg_device_reset, NULL);
174-
}
175-
176-
static void rpc_ota_rollback(struct mg_rpc_req *r) {
177-
if (mg_ota_rollback()) {
178-
mg_rpc_ok(r, "%m", MG_ESC("ok"));
179-
} else {
180-
mg_rpc_err(r, 1, "Failed to rollback to the previous firmware");
181-
}
182-
}
183-
184152
static void rpc_ota_upload(struct mg_rpc_req *r) {
185153
long ofs = mg_json_get_long(r->frame, "$.params.offset", -1);
186154
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) {
200168
mg_rpc_err(r, 1, "mg_ota_end() failed\n", tot);
201169
} else {
202170
mg_rpc_ok(r, "%m", MG_ESC("ok"));
203-
if (len == 0) { // Successful mg_ota_end() called, schedule device reboot
204-
mg_timer_add(s_conn->mgr, 500, 0, (void (*)(void *)) mg_device_reset,
205-
NULL);
206-
}
207171
}
208172
free(buf);
209173
}
@@ -297,11 +261,8 @@ void web_init(struct mg_mgr *mgr) {
297261

298262
// Configure JSON-RPC functions we're going to handle
299263
mg_rpc_add(&s_rpc, mg_str("config.set"), rpc_config_set, NULL);
300-
mg_rpc_add(&s_rpc, mg_str("ota.commit"), rpc_ota_commit, NULL);
301-
mg_rpc_add(&s_rpc, mg_str("ota.rollback"), rpc_ota_rollback, NULL);
302264
mg_rpc_add(&s_rpc, mg_str("ota.upload"), rpc_ota_upload, NULL);
303-
mg_rpc_add(&s_rpc, mg_str("device.reset"), rpc_device_reset, NULL);
304-
265+
305266
mg_timer_add(mgr, 3000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_reconnect,
306267
mgr);
307268
mg_timer_add(mgr, ping_interval_ms, MG_TIMER_REPEAT, timer_ping, mgr);

examples/nxp/rt1170-evk-make-baremetal-builtin/mongoose_config.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
// See https://mongoose.ws/documentation/#build-options
44
#define MG_ARCH MG_ARCH_NEWLIB
5-
#define MG_OTA MG_OTA_FLASH
6-
#define MG_DEVICE MG_DEVICE_RT1060
5+
#define MG_OTA MG_OTA_RT1060
76

87
#define MG_ENABLE_TCPIP 1
98
#define MG_ENABLE_DRIVER_IMXRT 1

examples/renesas/ek-ra6m4-make-baremetal-builtin/mongoose_config.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
// See https://mongoose.ws/documentation/#build-options
44
#define MG_ARCH MG_ARCH_NEWLIB
5-
#define MG_OTA MG_OTA_FLASH
6-
#define MG_DEVICE MG_DEVICE_STM32H5
75

86
#define MG_ENABLE_TCPIP 1
97
#define MG_ENABLE_CUSTOM_MILLIS 1

examples/wch/ch32v307-make-baremetal-builtin/main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
#define BLINK_PERIOD_MS 1000 // LED_PIN blinking period in millis
88

99
// This flash space resides at after the 0-wait 320k area
10-
static char *s_flash_space = (char *) (0x8000000 + 320 * 1024);
10+
// static char *s_flash_space = (char *) (0x8000000 + 320 * 1024);
1111

1212
bool web_load_settings(void *buf, size_t len) {
13-
if (*(uint32_t *) s_flash_space != SETTINGS_MAGIC) return false;
14-
memcpy(buf, s_flash_space, len);
15-
return true;
13+
(void) buf, (void) len;
14+
return false;
1615
}
1716

1817
bool web_save_settings(void *buf, size_t len) {
19-
return mg_flash_write(s_flash_space, buf, len);
18+
(void) buf, (void) len;
19+
return true;
2020
}
2121

2222
static void timer_fn(void *arg) {

examples/wch/ch32v307-make-baremetal-builtin/mongoose_config.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
// See https://mongoose.ws/documentation/#build-options
44
#define MG_ARCH MG_ARCH_NEWLIB
5-
#define MG_OTA MG_OTA_FLASH
6-
#define MG_DEVICE MG_DEVICE_CH32V307
5+
#define MG_OTA MG_OTA_CH32V307
76

87
#define MG_ENABLE_TCPIP 1
98
#define MG_ENABLE_CUSTOM_MILLIS 1

0 commit comments

Comments
 (0)