Add TLS and health backtrace to esp32

This commit is contained in:
cpq committed 2026-10-15 20:57:10 +01:00
1 parent c957f29f23
commit 2f2a5f2f02
8 files changed
+167 -263

No files matched your search

+1 -1
View File
@@ -3,6 +3,7 @@ idf_component_register(SRCS "main.c"
"../../../../mongoose.c"
INCLUDE_DIRS "."
"../../../.."
PRIV_REQUIRES mbedtls
REQUIRES esp_event
esp_netif
esp_partition
@@ -11,4 +12,3 @@ idf_component_register(SRCS "main.c"
espcoredump
nvs_flash
app_update)
component_compile_options(-DHTTP_URL="http://0.0.0.0:80")
+54 -127
View File
@@ -1,13 +1,9 @@
// Copyright (c) 2026 Cesanta Software Limited
// All rights reserved
#include <stdlib.h>
#include "esp_core_dump.h"
#include "esp_err.h"
#include "esp_partition.h"
#include "esp_heap_caps.h"
#include "esp_system.h"
#include "mongoose.h"
#include "sdkconfig.h"
#define WIFI_SSID "YOUR_WIFI_NETWORK_NAME" // SET THIS!
#define WIFI_PASS "YOUR_WIFI_PASSWORD" // SET THIS!
@@ -16,133 +12,56 @@
#define HTTP_URL "http://0.0.0.0:80"
#endif
#ifndef CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
#define CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH 0
#ifndef HTTPS_URL
#define HTTPS_URL "https://0.0.0.0:443"
#endif
#ifndef ESP_PARTITION_SUBTYPE_DATA_COREDUMP
#define ESP_PARTITION_SUBTYPE_DATA_COREDUMP 0x03
#endif
// Self-signed P-256 certificate and key for the HTTPS listener. Created by:
// openssl ecparam -genkey -name prime256v1 -noout -out key.pem
// openssl req -x509 -key key.pem -out cert.pem -days 3650 -subj /CN=mg
#define TLS_CERT \
"-----BEGIN CERTIFICATE-----\n" \
"MIIBgjCCASegAwIBAgIUIJOq77Qn7lHbH6WseMemeqorR50wCgYIKoZIzj0EAwIw\n" \
"FjEUMBIGA1UEAwwLbnVjbGVvLWg3MjMwHhcNMjYxMDEyMTUwMjE5WhcNMzYxMDA5\n" \
"MTUwMjE5WjAWMRQwEgYDVQQDDAtudWNsZW8taDcyMzBZMBMGByqGSM49AgEGCCqG\n" \
"SM49AwEHA0IABB6IQJNmzrsdQL+2BnZYS7GYOaj7sz8f6cjBmz3n8D9Em19XodlU\n" \
"uy4ybIvyhr766lL0ERoTmMUivb/lGmzx9C+jUzBRMB0GA1UdDgQWBBQqxMuc4gj7\n" \
"MTMzreWlRMNI3QMqTzAfBgNVHSMEGDAWgBQqxMuc4gj7MTMzreWlRMNI3QMqTzAP\n" \
"BgNVHRMBAf8EBTADAQH/MAoGCCqGSM49BAMCA0kAMEYCIQDY4xaJpuxq6b3rHK3b\n" \
"lOIQyfkQX+orCRqQUkseplqpqwIhAKswZFNn+2zUNRRNY8+cHmdIwmdpsmxtoWYP\n" \
"BCjX++LI\n" \
"-----END CERTIFICATE-----\n"
struct crash_download {
const esp_partition_t *partition;
size_t offset;
size_t size;
size_t sent;
};
static void crash_json(struct mg_connection *c) {
#if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
esp_core_dump_summary_t summary;
char reason[200] = "";
size_t address = 0, size = 0;
esp_err_t err = esp_core_dump_image_get(&address, &size);
esp_err_t sr = err == ESP_OK ? esp_core_dump_get_summary(&summary) : err;
esp_err_t rr = err == ESP_OK ? esp_core_dump_get_panic_reason(
reason, sizeof(reason)) : err;
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
"{%m:%s,%m:%d,%m:%u,%m:%u,%m:%m,%m:%m,%m:%d,%m:%d,%m:%m}\n",
MG_ESC("present"), err == ESP_OK ? "true" : "false",
MG_ESC("reset_reason"), (int) esp_reset_reason(),
MG_ESC("address"), (unsigned) address, MG_ESC("size"),
(unsigned) size, MG_ESC("panic_reason"),
MG_ESC(rr == ESP_OK ? reason : ""), MG_ESC("task"),
MG_ESC(sr == ESP_OK ? summary.exc_task : ""), MG_ESC("pc"),
sr == ESP_OK ? (int) summary.exc_pc : 0, MG_ESC("error"),
(int) err, MG_ESC("binary"), MG_ESC("/api/crash.bin"));
#else
mg_http_reply(c, 501, "Content-Type: application/json\r\n",
"{%m:false,%m:%d,%m:%m}\n", MG_ESC("present"),
MG_ESC("reset_reason"), (int) esp_reset_reason(),
MG_ESC("error"), MG_ESC("coredump to flash is disabled"));
#endif
}
static void crash_send(struct mg_connection *c) {
#if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
struct crash_download *d = (struct crash_download *) c->fn_data;
while (d != NULL && d->sent < d->size && c->send.len < 2048) {
uint8_t buf[512];
size_t left = d->size - d->sent;
size_t len = left > sizeof(buf) ? sizeof(buf) : left;
esp_err_t err = esp_partition_read(d->partition, d->offset + d->sent, buf,
len);
if (err != ESP_OK) {
MG_ERROR(("esp_partition_read: %s", esp_err_to_name(err)));
c->is_draining = 1;
return;
}
mg_send(c, buf, len);
d->sent += len;
}
if (d != NULL && d->sent >= d->size) {
free(d);
c->fn_data = NULL;
c->is_draining = 1;
}
#else
(void) c;
#endif
}
static void crash_bin(struct mg_connection *c) {
#if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
const esp_partition_t *p = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_COREDUMP, NULL);
struct crash_download *d;
size_t address = 0, size = 0, offset = 0;
esp_err_t err = esp_core_dump_image_get(&address, &size);
if (err != ESP_OK) {
mg_http_reply(c, 404, "Content-Type: application/json\r\n",
"{%m:%m}\n", MG_ESC("error"), MG_ESC(esp_err_to_name(err)));
} else if (p == NULL || address < p->address ||
address + size > p->address + p->size) {
mg_http_reply(c, 500, "Content-Type: application/json\r\n",
"{%m:%m}\n", MG_ESC("error"),
MG_ESC("invalid coredump partition"));
} else if ((d = (struct crash_download *) calloc(1, sizeof(*d))) == NULL) {
mg_http_reply(c, 500, "Content-Type: application/json\r\n",
"{%m:%m}\n", MG_ESC("error"), MG_ESC("out of memory"));
} else {
offset = address - p->address;
d->partition = p;
d->offset = offset;
d->size = size;
c->fn_data = d;
mg_printf(c,
"HTTP/1.1 200 OK\r\n"
"Content-Type: application/octet-stream\r\n"
"Content-Length: %u\r\n"
"Content-Disposition: attachment; filename=\"esp32-coredump."
"bin\"\r\n"
"\r\n",
(unsigned) size);
crash_send(c);
}
#else
mg_http_reply(c, 501, "Content-Type: application/json\r\n",
"{%m:%m}\n", MG_ESC("error"),
MG_ESC("coredump to flash is disabled"));
#endif
}
#define TLS_KEY \
"-----BEGIN EC PRIVATE KEY-----\n" \
"MHcCAQEEIJ/GBl0TlD52q9PMg1KWaNIdXqp8b/HyIsABmBpqC+O1oAoGCCqGSM49\n" \
"AwEHoUQDQgAEHohAk2bOux1Av7YGdlhLsZg5qPuzPx/pyMGbPefwP0SbX1eh2VS7\n" \
"LjJsi/KGvvrqUvQRGhOYxSK9v+UabPH0Lw==\n" \
"-----END EC PRIVATE KEY-----\n"
static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
if (ev == MG_EV_ACCEPT && c->is_tls) { // Accepted on the https:// listener
struct mg_tls_opts opts = {.cert = mg_str(TLS_CERT),
.key = mg_str(TLS_KEY)};
mg_tls_init(c, &opts);
} else if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (mg_match(hm->uri, mg_str("/api/crash.bin"), NULL)) {
crash_bin(c);
size_t total = heap_caps_get_total_size(MALLOC_CAP_DEFAULT);
size_t ram_free = heap_caps_get_free_size(MALLOC_CAP_DEFAULT);
if (mg_match(hm->uri, mg_str("/api/tick"), NULL)) {
mg_http_reply(c, 200, "", "{%m:%llu}\n", MG_ESC("tick"), mg_millis());
} else if (mg_match(hm->uri, mg_str("/api/crash"), NULL)) {
crash_json(c);
*(volatile uint32_t *) 0 = 0; // Store to address 0: CPU exception
} else {
mg_http_reply(c, 200, "", "Hi from Mongoose\n");
mg_http_reply(c, 200, "",
"Hi from Mongoose!\n"
"tick: %llu\n"
"ram used: %zu\n"
"ram free: %zu\n"
"ram min: %zu\n", // Lowest free heap since boot
mg_millis(), total - ram_free, ram_free,
heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT));
}
} else if (ev == MG_EV_POLL && c->fn_data != NULL) {
crash_send(c);
} else if (ev == MG_EV_CLOSE && c->fn_data != NULL) {
free(c->fn_data);
c->fn_data = NULL;
}
}
@@ -151,11 +70,19 @@ void app_main(void) {
wifi_init(WIFI_SSID, WIFI_PASS);
struct mg_mgr mgr;
mg_log_set(MG_LL_INFO);
mg_mgr_init(&mgr);
MG_INFO(("Mongoose version : v%s", MG_VERSION));
MG_INFO(("Listening on : %s", HTTP_URL));
mg_http_listen(&mgr, HTTP_URL, http_ev_handler, NULL);
mg_http_listen(&mgr, HTTPS_URL, http_ev_handler, NULL);
for (;;) mg_mgr_poll(&mgr, 1000);
MG_INFO(("Initialised. CPU clock: %lu MHz",
(unsigned long) esp_rom_get_cpu_ticks_per_us()));
MG_INFO(("RAM used: %zu, free: %zu",
heap_caps_get_total_size(MALLOC_CAP_DEFAULT) -
heap_caps_get_free_size(MALLOC_CAP_DEFAULT),
heap_caps_get_free_size(MALLOC_CAP_DEFAULT)));
MG_INFO(("Crash record: %M", mg_print_crash_record));
for (;;) {
mg_mgr_poll(&mgr, 1000);
}
}
@@ -1,3 +1,4 @@
#define MG_ARCH MG_ARCH_ESP32
#define MG_TLS MG_TLS_NONE
#define MG_TLS MG_TLS_BUILTIN
//#define MG_TLS MG_TLS_MBED