#include "drv_ota.h" #include #include #include #include #include "cJSON.h" #include "esp_log.h" #include "esp_random.h" #include "mbedtls/sha256.h" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" #include "freertos/semphr.h" #include "freertos/task.h" #include "rel.h" #include "store.h" static const char *TAG = "drv_ota"; #define WIRE_MAGIC 0x4C #define OP_BEGIN 0x07 #define OP_DATA 0x08 #define OP_END 0x09 #define OP_COMMIT 0x0A #define OP_ABORT 0x0B #define OP_MISSING 0x0C // Status codes from the driver enum { C_OK, C_READY, C_SAME, C_BADHASH, C_INCOMPLETE, C_BADPATH, C_IOERR, C_NOSESS, C_TOOBIG, C_NCODES }; static const char *const CODE_NAMES[] = { "ok", "ready", "same", "badhash", "incomplete", "badpath", "ioerr", "nosess", "toobig" }; #define CHUNK 944 // 4 link fragments; must match espnow_ota.CHUNK #define MAX_FILE 65536 #define MAX_CHUNKS ((MAX_FILE + CHUNK - 1) / CHUNK) #define MAX_MACS 32 #define MAX_PATH 128 #define ACK_MS 3000 // rel gives up after about 1 s; this only guards against a lost outcome #define BEGIN_MS 1000 // per try: the driver hashes its current copy first #define END_MS 2500 // per try: the driver hashes the staged copy (64 KB takes well under 1 s on a C3) #define CMD_TRIES 5 // status replies are not acked, so a lost one costs a resend #define REPAIR_ROUNDS 4 #define BCAST_GAP_MS 50 // between broadcast chunks: a C3 writing flash cannot take them faster typedef struct { uint8_t mac[6]; uint16_t sess; uint8_t op, code; uint8_t arg_len; uint8_t arg[24]; } status_t; typedef struct { uint8_t mac[6]; char hex[13]; int state; // C_READY while in the transfer, C_SAME, C_OK when written, else the error const char *err; // link-level failure text, when no driver code applies int repaired; } target_t; static QueueHandle_t s_status_q; static SemaphoreHandle_t s_busy; static void on_status(const uint8_t mac[6], const uint8_t *pkt, int len) { if (len < 6) { return; } status_t st = { .sess = pkt[2] | (pkt[3] << 8), .op = pkt[4], .code = pkt[5] }; memcpy(st.mac, mac, 6); st.arg_len = len - 6 > (int)sizeof(st.arg) ? sizeof(st.arg) : len - 6; memcpy(st.arg, pkt + 6, st.arg_len); xQueueSend(s_status_q, &st, 0); } // Wait for this driver's reply to op; replies left over from earlier requests are dropped static bool wait_status(const uint8_t *mac, uint16_t sess, uint8_t op, int timeout_ms, status_t *out) { TickType_t until = xTaskGetTickCount() + pdMS_TO_TICKS(timeout_ms); for (;;) { TickType_t now = xTaskGetTickCount(); if ((int32_t)(until - now) <= 0) { return false; } if (xQueueReceive(s_status_q, out, until - now) != pdTRUE) { return false; } if (out->sess == sess && out->op == op && memcmp(out->mac, mac, 6) == 0) { return true; } } } static void drain_status(void) { status_t st; while (xQueueReceive(s_status_q, &st, 0) == pdTRUE) { } } // Acked command to one driver, then its status reply. Returns the code, or -1 if the driver never answered. static int command(const uint8_t *mac, const uint8_t *pkt, size_t len, uint16_t sess, uint8_t op, int timeout_ms, status_t *st) { status_t local; st = st ? st : &local; // Neither the reply nor a given-up ack proves the driver missed it: every command is safe to repeat for (int t = 0; t < CMD_TRIES; t++) { rel_send_wait(mac, pkt, len, ACK_MS); if (wait_status(mac, sess, op, timeout_ms, st)) { return st->code; } } return -1; } static size_t data_pkt(uint8_t *pkt, uint16_t sess, int idx, const uint8_t *body, size_t size) { size_t off = (size_t)idx * CHUNK, n = size - off > CHUNK ? CHUNK : size - off; pkt[0] = WIRE_MAGIC; pkt[1] = OP_DATA; pkt[2] = sess & 255; pkt[3] = sess >> 8; pkt[4] = idx & 255; pkt[5] = idx >> 8; memcpy(pkt + 6, body + off, n); return 6 + n; } // Acked unicast of the chunks marked in want (NULL = all), two messages in flight. // rel_send copies each message, so one packet buffer is enough. A chunk whose ack never came // is left to the END/MISSING repair; only a run of them means the driver is gone. #define DEAD_AFTER 8 static bool chunk_done(const uint8_t *mac, uint16_t seq, int *dead) { if (rel_wait(mac, seq, ACK_MS) == ESP_OK) { *dead = 0; return true; } return ++*dead < DEAD_AFTER; } static bool send_chunks(const uint8_t *mac, uint16_t sess, const uint8_t *body, size_t size, const uint8_t *want, int *sent) { static uint8_t pkt[6 + CHUNK]; int cnt = (size + CHUNK - 1) / CHUNK; uint16_t fifo[2]; int head = 0, inflight = 0, dead = 0; for (int i = 0; i < cnt; i++) { if (want && !(want[i >> 3] & (1 << (i & 7)))) { continue; } if (inflight == 2) { if (!chunk_done(mac, fifo[head], &dead)) { return false; } head ^= 1; inflight--; } size_t n = data_pkt(pkt, sess, i, body, size); uint16_t seq; esp_err_t err = ESP_ERR_NO_MEM; for (int t = 0; t < 50 && err == ESP_ERR_NO_MEM; t++) { err = rel_send_seq(mac, pkt, n, false, 1, &seq); if (err == ESP_ERR_NO_MEM) { vTaskDelay(pdMS_TO_TICKS(10)); } } if (err != ESP_OK) { return false; } fifo[(head + inflight) & 1] = seq; inflight++; if (sent) { (*sent)++; } } for (; inflight > 0; inflight--, head ^= 1) { if (!chunk_done(mac, fifo[head], &dead)) { return false; } } return true; } static void broadcast_chunks(uint16_t sess, const uint8_t *body, size_t size) { static uint8_t pkt[6 + CHUNK]; int cnt = (size + CHUNK - 1) / CHUNK; for (int i = 0; i < cnt; i++) { size_t n = data_pkt(pkt, sess, i, body, size); for (int t = 0; t < 50 && rel_send(NULL, pkt, n, false, 1) == ESP_ERR_NO_MEM; t++) { vTaskDelay(pdMS_TO_TICKS(10)); } vTaskDelay(pdMS_TO_TICKS(BCAST_GAP_MS)); } } // END until the driver has every chunk: ask what is missing and resend it by unicast static void finish(target_t *t, uint16_t sess, const uint8_t *body, size_t size) { uint8_t end[4] = { WIRE_MAGIC, OP_END, sess & 255, sess >> 8 }; uint8_t miss[4] = { WIRE_MAGIC, OP_MISSING, sess & 255, sess >> 8 }; for (int round = 0; round < REPAIR_ROUNDS; round++) { int code = command(t->mac, end, sizeof(end), sess, OP_END, END_MS, NULL); if (code != C_INCOMPLETE) { t->state = code; t->err = code < 0 ? "no reply to end" : NULL; return; } status_t st; if (command(t->mac, miss, sizeof(miss), sess, OP_MISSING, BEGIN_MS, &st) != C_OK || st.arg_len < 2) { t->state = -1; t->err = "no reply to missing"; return; } int cnt = st.arg[0] | (st.arg[1] << 8); if (cnt != (int)((size + CHUNK - 1) / CHUNK) || st.arg_len < 2 + (cnt + 7) / 8) { t->state = -1; t->err = "bad missing reply"; return; } if (!send_chunks(t->mac, sess, body, size, st.arg + 2, &t->repaired)) { t->state = -1; t->err = "link lost"; return; } } t->state = C_INCOMPLETE; } static void transfer(target_t *ts, int n, uint16_t sess, const char *path, const uint8_t sha[32], const uint8_t *body, size_t size) { uint8_t begin[41 + MAX_PATH]; size_t plen = strlen(path); begin[0] = WIRE_MAGIC; begin[1] = OP_BEGIN; begin[2] = sess & 255; begin[3] = sess >> 8; for (int i = 0; i < 4; i++) { begin[4 + i] = (size >> (8 * i)) & 255; } memcpy(begin + 8, sha, 32); begin[40] = plen; memcpy(begin + 41, path, plen); int ready = 0; for (int i = 0; i < n; i++) { int code = command(ts[i].mac, begin, 41 + plen, sess, OP_BEGIN, BEGIN_MS, NULL); ts[i].state = code; ts[i].err = code < 0 ? "no reply to begin" : NULL; ready += code == C_READY; } if (ready == 0) { return; } if (ready > 1) { broadcast_chunks(sess, body, size); } else { for (int i = 0; i < n; i++) { if (ts[i].state == C_READY && !send_chunks(ts[i].mac, sess, body, size, NULL, NULL)) { ts[i].state = -1; ts[i].err = "link lost"; } } } for (int i = 0; i < n; i++) { if (ts[i].state == C_READY) { finish(&ts[i], sess, body, size); } } } static int parse_hex(const char *s, uint8_t *out, int n) { for (int i = 0; i < n; i++) { unsigned v; if (sscanf(s + 2 * i, "%2x", &v) != 1) { return -1; } out[i] = v; } return s[2 * n] == '\0' || s[2 * n] == ',' ? 0 : -1; } // mac=all, or a comma-separated list of 12-hex addresses static int parse_targets(const char *list, target_t *ts) { int n = 0; if (!strcmp(list, "all")) { store_lock(); const cJSON *d; cJSON_ArrayForEach(d, store_coll("device")) { const cJSON *tr = cJSON_GetObjectItem(d, "transport"), *addr = cJSON_GetObjectItem(d, "address"); if (n < MAX_MACS && cJSON_IsString(tr) && !strcmp(tr->valuestring, "espnow") && cJSON_IsString(addr) && strlen(addr->valuestring) == 12 && parse_hex(addr->valuestring, ts[n].mac, 6) == 0) { n++; } } store_unlock(); } else { for (const char *p = list; *p && n < MAX_MACS;) { if (parse_hex(p, ts[n].mac, 6) != 0) { return -1; } n++; p += 12; if (*p == ',') { p++; } } } for (int i = 0; i < n; i++) { uint8_t *m = ts[i].mac; snprintf(ts[i].hex, sizeof(ts[i].hex), "%02x%02x%02x%02x%02x%02x", m[0], m[1], m[2], m[3], m[4], m[5]); ts[i].state = -1; ts[i].err = NULL; ts[i].repaired = 0; } return n; } static esp_err_t send_json(httpd_req_t *req, const char *status, cJSON *obj) { char *s = cJSON_PrintUnformatted(obj); cJSON_Delete(obj); if (!s) { return httpd_resp_send_500(req); } httpd_resp_set_status(req, status); httpd_resp_set_type(req, "application/json"); esp_err_t r = httpd_resp_sendstr(req, s); free(s); return r; } static esp_err_t send_error(httpd_req_t *req, const char *status, const char *msg) { ESP_LOGW(TAG, "%s", msg); cJSON *o = cJSON_CreateObject(); cJSON_AddStringToObject(o, "error", msg); return send_json(req, status, o); } static cJSON *results(const target_t *ts, int n, bool file) { cJSON *o = cJSON_CreateObject(); for (int i = 0; i < n; i++) { cJSON *e = cJSON_AddObjectToObject(o, ts[i].hex); int s = ts[i].state; const char *result = s == C_OK ? (file ? "written" : "ok") : s == C_SAME ? "same" : "error"; cJSON_AddStringToObject(e, "result", result); if (s < 0) { cJSON_AddStringToObject(e, "code", ts[i].err ? ts[i].err : "no reply"); } else if (s != C_OK && s != C_SAME) { cJSON_AddStringToObject(e, "code", s < C_NCODES ? CODE_NAMES[s] : "unknown"); } if (file) { cJSON_AddNumberToObject(e, "repaired", ts[i].repaired); } } return o; } static esp_err_t handle(httpd_req_t *req, const char *action, const char *query, target_t *ts, int n, uint16_t sess) { if (!strcmp(action, "commit") || !strcmp(action, "abort")) { bool commit = action[0] == 'c'; uint8_t op = commit ? OP_COMMIT : OP_ABORT; uint8_t pkt[4] = { WIRE_MAGIC, op, sess & 255, sess >> 8 }; for (int i = 0; i < n; i++) { ts[i].state = command(ts[i].mac, pkt, sizeof(pkt), sess, op, BEGIN_MS, NULL); } return send_json(req, "200 OK", results(ts, n, false)); } if (strcmp(action, "file")) { return send_error(req, "404 Not Found", "unknown action"); } char path[MAX_PATH + 1], shahex[65]; uint8_t sha[32]; if (httpd_query_key_value(query, "path", path, sizeof(path)) != ESP_OK || !path[0] || httpd_query_key_value(query, "sha", shahex, sizeof(shahex)) != ESP_OK || strlen(shahex) != 64 || parse_hex(shahex, sha, 32) != 0) { return send_error(req, "400 Bad Request", "path and sha (64 hex) are required"); } for (const char *c = path; *c; c++) { // query values arrive undecoded: keep paths to plain names if (!(isalnum((unsigned char)*c) || *c == '_' || *c == '.' || *c == '/' || *c == '-')) { return send_error(req, "400 Bad Request", "path may only hold letters, digits and _ . / -"); } } if (req->content_len > MAX_FILE) { return send_error(req, "413 Payload Too Large", "file must be at most 65536 bytes"); } uint8_t *body = malloc(req->content_len + 1); // empty files (__init__.py) are fine if (!body) { return send_error(req, "500 Internal Server Error", "out of memory"); } size_t got = 0; while (got < req->content_len) { int r = httpd_req_recv(req, (char *)body + got, req->content_len - got); if (r == HTTPD_SOCK_ERR_TIMEOUT) { continue; } if (r <= 0) { free(body); return send_error(req, "400 Bad Request", "receive failed"); } got += r; } ESP_LOGI(TAG, "%s (%u B) to %d driver(s), session %u", path, (unsigned)got, n, sess); transfer(ts, n, sess, path, sha, body, got); free(body); return send_json(req, "200 OK", results(ts, n, true)); } static esp_err_t ota_post_handler(httpd_req_t *req) { const char *action = req->uri + strlen("/drivers/ota/"); size_t alen = strcspn(action, "?"); char act[16] = ""; if (alen < sizeof(act)) { memcpy(act, action, alen); act[alen] = '\0'; } char query[480], macs[400], sessbuf[8]; if (httpd_req_get_url_query_str(req, query, sizeof(query)) != ESP_OK || httpd_query_key_value(query, "mac", macs, sizeof(macs)) != ESP_OK || httpd_query_key_value(query, "sess", sessbuf, sizeof(sessbuf)) != ESP_OK) { return send_error(req, "400 Bad Request", "mac and sess are required"); } long sess = strtol(sessbuf, NULL, 10); if (sess < 1 || sess > 0xFFFF) { return send_error(req, "400 Bad Request", "sess must be 1..65535"); } static target_t ts[MAX_MACS]; if (xSemaphoreTake(s_busy, 0) != pdTRUE) { return send_error(req, "409 Conflict", "a driver update is already running"); } esp_err_t r; int n = parse_targets(macs, ts); if (n <= 0) { r = send_error(req, "400 Bad Request", n < 0 ? "bad mac list" : "no ESP-NOW drivers"); } else { drain_status(); r = handle(req, act, query, ts, n, (uint16_t)sess); } xSemaphoreGive(s_busy); return r; } cJSON *drv_ota_send(const char *macs, const char *path, const uint8_t *data, size_t len, bool commit, esp_err_t *err) { static target_t ts[MAX_MACS]; if (len > MAX_FILE || strlen(path) > MAX_PATH) { *err = ESP_ERR_INVALID_SIZE; return NULL; } if (!s_busy || xSemaphoreTake(s_busy, 0) != pdTRUE) { *err = ESP_ERR_INVALID_STATE; return NULL; } cJSON *out = NULL; int n = parse_targets(macs, ts); if (n <= 0) { *err = n < 0 ? ESP_ERR_INVALID_ARG : ESP_ERR_NOT_FOUND; } else { uint8_t sha[32]; mbedtls_sha256(data, len, sha, 0); uint16_t sess = (esp_random() % 0xFFFF) + 1; drain_status(); ESP_LOGI(TAG, "%s (%u B) to %d driver(s), session %u", path, (unsigned)len, n, sess); transfer(ts, n, sess, path, sha, data, len); out = results(ts, n, true); if (commit) { uint8_t pkt[4] = { WIRE_MAGIC, OP_COMMIT, sess & 255, sess >> 8 }; for (int i = 0; i < n; i++) { if (ts[i].state == C_OK) { command(ts[i].mac, pkt, sizeof(pkt), sess, OP_COMMIT, BEGIN_MS, NULL); } } } *err = ESP_OK; } xSemaphoreGive(s_busy); return out; } esp_err_t drv_ota_register(httpd_handle_t server) { s_status_q = xQueueCreate(16, sizeof(status_t)); s_busy = xSemaphoreCreateMutex(); if (!s_status_q || !s_busy) { return ESP_ERR_NO_MEM; } rel_set_ota_status_cb(on_status); const httpd_uri_t u = { .uri = "/drivers/ota/*", .method = HTTP_POST, .handler = ota_post_handler }; return httpd_register_uri_handler(server, &u); }