refactor: move the v2 controller to the repository root
The ESP-IDF project, docs, tools, tests and web UI move from v2/ to the root, and the Makefile, README, build scripts and test runner drop the v2/ prefix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
71bd320661
commit
1c222771a5
66 files changed
+30
-31
No files matched your search
+186
@@ -0,0 +1,186 @@
|
||||
#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 <string.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();
|
||||
// The build date is only recompiled with its own file, so the ELF hash is what tells two builds apart
|
||||
char sha[17];
|
||||
for (int i = 0; i < 8; i++) {
|
||||
snprintf(sha + 2 * i, 3, "%02x", app->app_elf_sha256[i]);
|
||||
}
|
||||
char buf[288];
|
||||
snprintf(buf, sizeof(buf),
|
||||
"{\"partition\":\"%s\",\"version\":\"%s\",\"built\":\"%s %s\",\"idf\":\"%s\",\"sha\":\"%s\"}",
|
||||
run ? run->label : "?", app->version, app->date, app->time, app->idf_ver, sha);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_sendstr(req, buf);
|
||||
}
|
||||
|
||||
static esp_err_t reboot_post_handler(httpd_req_t *req)
|
||||
{
|
||||
ESP_LOGI(TAG, "reboot requested");
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_sendstr(req, "{\"message\":\"rebooting\"}");
|
||||
|
||||
static esp_timer_handle_t t;
|
||||
const esp_timer_create_args_t args = { .callback = reboot_cb, .name = "reboot" };
|
||||
if (esp_timer_create(&args, &t) == ESP_OK) {
|
||||
esp_timer_start_once(t, 500 * 1000);
|
||||
} else {
|
||||
esp_restart();
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
const httpd_uri_t reboot = { .uri = "/reboot", .method = HTTP_POST, .handler = reboot_post_handler };
|
||||
if (err == ESP_OK) {
|
||||
err = httpd_register_uri_handler(server, &reboot);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
Reference in new issue
Block a user