feat(v2): OTA firmware update over Ethernet
POST /ota writes the image to the idle app slot and reboots; GET /ota reports the running slot. App rollback is enabled, and a new image confirms itself after 10 s. Adds make ota HOST=<ip>, tools/ota.py and the led-tool --ota update. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8d78af1246
commit
6c16998691
8 files changed
+212
-4
No files matched your search
@@ -1,6 +1,6 @@
|
||||
idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c"
|
||||
idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "ota.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES esp_eth esp_netif esp_event esp_driver_spi esp_driver_gpio nvs_flash
|
||||
REQUIRES esp_eth esp_netif esp_event esp_driver_spi esp_driver_gpio nvs_flash app_update esp_app_format
|
||||
esp_http_server esp_timer esp_driver_i2s esp_wifi esp_rom json esp_driver_gpio
|
||||
PRIV_REQUIRES littlefs)
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "rel.h"
|
||||
#include "web.h"
|
||||
#include "audio.h"
|
||||
#include "ota.h"
|
||||
|
||||
static const char *TAG = "eth";
|
||||
|
||||
@@ -88,6 +89,7 @@ static void start_webserver(void)
|
||||
httpd_register_uri_handler(s_server, &status_uri);
|
||||
web_register(s_server);
|
||||
audio_register(s_server);
|
||||
ota_register(s_server);
|
||||
api_register(s_server);
|
||||
ESP_LOGI(TAG, "HTTP server started on port %d", config.server_port);
|
||||
}
|
||||
@@ -215,6 +217,7 @@ void app_main(void)
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
ota_confirm_later();
|
||||
ESP_ERROR_CHECK(store_init());
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
#include "ota.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_app_format.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_timer.h"
|
||||
|
||||
static const char *TAG = "ota";
|
||||
|
||||
#define CHUNK 4096
|
||||
#define CONFIRM_AFTER_US (10 * 1000000LL)
|
||||
|
||||
static bool s_busy;
|
||||
|
||||
static void reboot_cb(void *arg)
|
||||
{
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
static void confirm_cb(void *arg)
|
||||
{
|
||||
esp_err_t err = esp_ota_mark_app_valid_cancel_rollback();
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "running image confirmed");
|
||||
}
|
||||
}
|
||||
|
||||
void ota_confirm_later(void)
|
||||
{
|
||||
esp_ota_img_states_t state;
|
||||
const esp_partition_t *run = esp_ota_get_running_partition();
|
||||
if (esp_ota_get_state_partition(run, &state) != ESP_OK || state != ESP_OTA_IMG_PENDING_VERIFY) {
|
||||
return;
|
||||
}
|
||||
static esp_timer_handle_t t;
|
||||
const esp_timer_create_args_t args = { .callback = confirm_cb, .name = "ota_confirm" };
|
||||
if (esp_timer_create(&args, &t) == ESP_OK) {
|
||||
esp_timer_start_once(t, CONFIRM_AFTER_US);
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t fail(httpd_req_t *req, const char *status, const char *msg)
|
||||
{
|
||||
ESP_LOGE(TAG, "%s", msg);
|
||||
httpd_resp_set_status(req, status);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
char buf[160];
|
||||
snprintf(buf, sizeof(buf), "{\"error\":\"%s\"}", msg);
|
||||
return httpd_resp_sendstr(req, buf);
|
||||
}
|
||||
|
||||
static esp_err_t ota_post_handler(httpd_req_t *req)
|
||||
{
|
||||
if (req->content_len == 0) {
|
||||
return fail(req, "400 Bad Request", "empty body");
|
||||
}
|
||||
const esp_partition_t *part = esp_ota_get_next_update_partition(NULL);
|
||||
if (!part) {
|
||||
return fail(req, "500 Internal Server Error", "no update partition");
|
||||
}
|
||||
if (req->content_len > part->size) {
|
||||
return fail(req, "413 Payload Too Large", "image larger than the app slot");
|
||||
}
|
||||
if (s_busy) {
|
||||
return fail(req, "409 Conflict", "update already running");
|
||||
}
|
||||
s_busy = true;
|
||||
|
||||
char *buf = malloc(CHUNK);
|
||||
if (!buf) {
|
||||
s_busy = false;
|
||||
return fail(req, "500 Internal Server Error", "out of memory");
|
||||
}
|
||||
|
||||
esp_ota_handle_t handle = 0;
|
||||
esp_err_t err = esp_ota_begin(part, req->content_len, &handle);
|
||||
if (err != ESP_OK) {
|
||||
free(buf);
|
||||
s_busy = false;
|
||||
return fail(req, "500 Internal Server Error", "ota begin failed");
|
||||
}
|
||||
ESP_LOGI(TAG, "receiving %u bytes into %s", (unsigned)req->content_len, part->label);
|
||||
|
||||
size_t got = 0;
|
||||
const char *bad = NULL;
|
||||
while (got < req->content_len) {
|
||||
int want = req->content_len - got;
|
||||
int r = httpd_req_recv(req, buf, want > CHUNK ? CHUNK : want);
|
||||
if (r == HTTPD_SOCK_ERR_TIMEOUT) {
|
||||
continue;
|
||||
}
|
||||
if (r <= 0) {
|
||||
bad = "receive failed";
|
||||
break;
|
||||
}
|
||||
if (got == 0 && (r < (int)(sizeof(esp_image_header_t)) || (uint8_t)buf[0] != ESP_IMAGE_HEADER_MAGIC)) {
|
||||
bad = "not an ESP32 app image";
|
||||
break;
|
||||
}
|
||||
if (esp_ota_write(handle, buf, r) != ESP_OK) {
|
||||
bad = "flash write failed";
|
||||
break;
|
||||
}
|
||||
got += r;
|
||||
}
|
||||
free(buf);
|
||||
if (bad) {
|
||||
esp_ota_abort(handle);
|
||||
s_busy = false;
|
||||
return fail(req, "400 Bad Request", bad);
|
||||
}
|
||||
// esp_ota_end checks the image checksum and hash
|
||||
if (esp_ota_end(handle) != ESP_OK) {
|
||||
s_busy = false;
|
||||
return fail(req, "400 Bad Request", "image failed validation");
|
||||
}
|
||||
if (esp_ota_set_boot_partition(part) != ESP_OK) {
|
||||
s_busy = false;
|
||||
return fail(req, "500 Internal Server Error", "set boot partition failed");
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "update ok, rebooting");
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_sendstr(req, "{\"message\":\"update written, rebooting\"}");
|
||||
|
||||
static esp_timer_handle_t t;
|
||||
const esp_timer_create_args_t args = { .callback = reboot_cb, .name = "ota_reboot" };
|
||||
if (esp_timer_create(&args, &t) == ESP_OK) {
|
||||
esp_timer_start_once(t, 500 * 1000);
|
||||
} else {
|
||||
esp_restart();
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t ota_get_handler(httpd_req_t *req)
|
||||
{
|
||||
const esp_partition_t *run = esp_ota_get_running_partition();
|
||||
const esp_app_desc_t *app = esp_app_get_description();
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf),
|
||||
"{\"partition\":\"%s\",\"version\":\"%s\",\"built\":\"%s %s\",\"idf\":\"%s\"}",
|
||||
run ? run->label : "?", app->version, app->date, app->time, app->idf_ver);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_sendstr(req, buf);
|
||||
}
|
||||
|
||||
esp_err_t ota_register(httpd_handle_t server)
|
||||
{
|
||||
const httpd_uri_t post = { .uri = "/ota", .method = HTTP_POST, .handler = ota_post_handler };
|
||||
const httpd_uri_t get = { .uri = "/ota", .method = HTTP_GET, .handler = ota_get_handler };
|
||||
esp_err_t err = httpd_register_uri_handler(server, &post);
|
||||
if (err == ESP_OK) {
|
||||
err = httpd_register_uri_handler(server, &get);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
// Register POST /ota (raw firmware .bin as the body) and GET /ota (running image info).
|
||||
// A good upload is written to the idle app slot, becomes the boot image and the board reboots.
|
||||
esp_err_t ota_register(httpd_handle_t server);
|
||||
|
||||
// Call once at boot: confirms the running image after it has stayed up for a few seconds, so
|
||||
// a new image that crashes on start rolls back to the previous one.
|
||||
void ota_confirm_later(void);
|
||||
@@ -16,3 +16,5 @@ CONFIG_FREERTOS_HZ=1000
|
||||
CONFIG_ESP_WIFI_ESPNOW_MAX_ENCRYPT_NUM=7
|
||||
CONFIG_ESPNOW_CHANNEL=5
|
||||
CONFIG_BEAT_TEST_GPIO=4
|
||||
# A new OTA image must confirm itself (ota_confirm_later) or the bootloader goes back to the old one
|
||||
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
|
||||
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Upload a firmware image to the controller over Ethernet.
|
||||
|
||||
ota.py <host> [build/led_controller_v2.bin]
|
||||
"""
|
||||
import http.client
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
host = sys.argv[1] if len(sys.argv) > 1 else sys.exit(__doc__)
|
||||
path = sys.argv[2] if len(sys.argv) > 2 else os.path.join(os.path.dirname(__file__), "..", "build", "led_controller_v2.bin")
|
||||
size = os.path.getsize(path)
|
||||
|
||||
conn = http.client.HTTPConnection(host, 80, timeout=60)
|
||||
conn.request("GET", "/ota")
|
||||
print("running:", conn.getresponse().read().decode())
|
||||
|
||||
print(f"uploading {path} ({size} bytes)")
|
||||
with open(path, "rb") as f:
|
||||
conn.request("POST", "/ota", body=f, headers={"Content-Length": str(size),
|
||||
"Content-Type": "application/octet-stream"})
|
||||
r = conn.getresponse()
|
||||
body = r.read().decode()
|
||||
print(r.status, body)
|
||||
sys.exit(0 if r.status == 200 else 1)
|
||||
Reference in new issue
Block a user