Files
led-controller/main/ota.c
T
jimmyandClaude Haiku 5.5 f0615dc1b7 feat: users, roles and per-area permissions, production demo image
The controller checks every route against the signed-in user's role, with
levels none, view, control and edit per area. Guests get the permissions
of a built-in guest role. Auth stays off until the first user exists;
LED_ADMIN_USER and LED_ADMIN_PASSWORD create the first admin.

Sessions are cookies, passwords are PBKDF2-SHA256, and logins lock per
username after five failures. The simulator's WebSocket checks the view
permission at handshake and per frame; /ws needs devices control. The web
UI gets a sign-in button, a sign-in prompt on refused changes, and a users
and roles screen.

Also: the device build gets the bridge include path and forward type it
was missing, Dockerfile.sim runs non-root, and Dockerfile.prod with
docker-compose.prod.yml runs the controller on an internal network behind
Caddy for HTTPS.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-17 00:29:04 +13:00

188 lines
5.8 KiB
C

#include "auth.h"
#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 = auth_register_uri(server, &post);
if (err == ESP_OK) {
err = auth_register_uri(server, &get);
}
const httpd_uri_t reboot = { .uri = "/reboot", .method = HTTP_POST, .handler = reboot_post_handler };
if (err == ESP_OK) {
err = auth_register_uri(server, &reboot);
}
return err;
}