feat: web UI with AP, Wi-Fi client, mDNS, presets, OTA and DDP
- Wi-Fi access point led-XXXXXX plus a client network, configurable mDNS name and static client address - Preset tiles with an editor, drag to reorder, default presets, global brightness, Run and Edit modes, collapsible menu on phones - 35 patterns ported from led-driver, up to 512 LEDs, colour order, max brightness and data pin settings - Over-the-air updates (POST /ota) with rollback, and a CLI in tools/ota.py - DDP receiver on UDP 4048 with tools/ddp_test.py - LED refresh every 10 ms; lwIP sockets raised to 16 so mDNS, HTTP and DDP can run together Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
89f996096c
commit
9a04d28ecf
32 files changed
+6210
-398
No files matched your search
+449
-58
@@ -1,14 +1,20 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "net.h"
|
||||
#include "ota.h"
|
||||
#include "ddp.h"
|
||||
#include "patterns.h"
|
||||
#include "effects.h"
|
||||
#include "presets.h"
|
||||
#include "settings.h"
|
||||
#include "web.h"
|
||||
|
||||
@@ -24,14 +30,19 @@ extern const char style_css_end[] asm("_binary_style_css_end");
|
||||
|
||||
static httpd_handle_t s_server;
|
||||
|
||||
#define MAX_BODY 512
|
||||
#define MAX_BODY 1024
|
||||
#define MAX_SCAN 20
|
||||
|
||||
static esp_err_t send_embedded(httpd_req_t *req, const char *type, const char *start, const char *end)
|
||||
{
|
||||
httpd_resp_set_type(req, type);
|
||||
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
|
||||
return httpd_resp_send(req, start, end - start);
|
||||
// EMBED_TXTFILES includes the file's terminating NUL; don't send it
|
||||
size_t len = end - start;
|
||||
if (len > 0 && start[len - 1] == '\0') {
|
||||
len--;
|
||||
}
|
||||
return httpd_resp_send(req, start, len);
|
||||
}
|
||||
|
||||
static esp_err_t send_json(httpd_req_t *req, cJSON *obj)
|
||||
@@ -74,26 +85,14 @@ static bool read_body(httpd_req_t *req, char *buf, size_t cap)
|
||||
return true;
|
||||
}
|
||||
|
||||
static void read_rgb(const cJSON *arr, uint8_t *out)
|
||||
// Read a JSON body; NULL (no reply sent) if missing, too big or not JSON. Caller frees with cJSON_Delete.
|
||||
static cJSON *read_json(httpd_req_t *req)
|
||||
{
|
||||
if (!cJSON_IsArray(arr) || cJSON_GetArraySize(arr) != 3) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
cJSON *v = cJSON_GetArrayItem(arr, i);
|
||||
if (cJSON_IsNumber(v)) {
|
||||
int x = v->valueint;
|
||||
out[i] = (uint8_t)(x < 0 ? 0 : x > 255 ? 255 : x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void put_rgb(cJSON *obj, const char *key, const uint8_t *c)
|
||||
{
|
||||
cJSON *a = cJSON_AddArrayToObject(obj, key);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
cJSON_AddItemToArray(a, cJSON_CreateNumber(c[i]));
|
||||
char body[MAX_BODY];
|
||||
if (!read_body(req, body, sizeof(body))) {
|
||||
return NULL;
|
||||
}
|
||||
return cJSON_Parse(body);
|
||||
}
|
||||
|
||||
// GET / , /app.js , /style.css
|
||||
@@ -112,61 +111,45 @@ static esp_err_t style_css_get(httpd_req_t *req)
|
||||
return send_embedded(req, "text/css", style_css_start, style_css_end);
|
||||
}
|
||||
|
||||
// GET /api/status
|
||||
// GET /api/status : the running preset (same fields as a preset), plus device and network state
|
||||
static esp_err_t status_get(httpd_req_t *req)
|
||||
{
|
||||
pattern_params_t p = patterns_get();
|
||||
preset_t p;
|
||||
patterns_get(&p);
|
||||
char ap[16], ip[16];
|
||||
net_ap_ssid(ap, sizeof(ap));
|
||||
bool up = net_sta_status(ip, sizeof(ip));
|
||||
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
cJSON *o = preset_to_json(&p);
|
||||
cJSON_AddStringToObject(o, "ap_ssid", ap);
|
||||
cJSON_AddStringToObject(o, "hostname", n.hostname);
|
||||
cJSON_AddBoolToObject(o, "sta_connected", up);
|
||||
cJSON_AddStringToObject(o, "sta_ip", ip);
|
||||
cJSON_AddStringToObject(o, "mode", patterns_mode_name(p.mode));
|
||||
cJSON *modes = cJSON_AddArrayToObject(o, "modes");
|
||||
for (int i = 0; i < PM_COUNT; i++) {
|
||||
cJSON_AddItemToArray(modes, cJSON_CreateString(patterns_mode_name((pattern_mode_t)i)));
|
||||
cJSON_AddStringToObject(o, "selected", presets_selected());
|
||||
cJSON_AddNumberToObject(o, "global_brightness", patterns_get_global());
|
||||
cJSON *modes = cJSON_AddArrayToObject(o, "patterns");
|
||||
for (int i = 0; i < patterns_count(); i++) {
|
||||
cJSON_AddItemToArray(modes, cJSON_CreateString(patterns_name(i)));
|
||||
}
|
||||
put_rgb(o, "c1", p.c1);
|
||||
put_rgb(o, "c2", p.c2);
|
||||
cJSON_AddNumberToObject(o, "speed_ms", p.speed_ms);
|
||||
cJSON_AddNumberToObject(o, "brightness", p.brightness);
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
// POST /api/pattern {"mode","c1":[r,g,b],"c2":[r,g,b],"speed_ms","brightness"}
|
||||
// POST /api/pattern : change the running settings. Fields missing from the body keep their value.
|
||||
static esp_err_t pattern_post(httpd_req_t *req)
|
||||
{
|
||||
char body[MAX_BODY];
|
||||
if (!read_body(req, body, sizeof(body))) {
|
||||
return send_error(req, "400 Bad Request", "empty or too large body");
|
||||
}
|
||||
cJSON *in = cJSON_Parse(body);
|
||||
cJSON *in = read_json(req);
|
||||
if (!in) {
|
||||
return send_error(req, "400 Bad Request", "invalid JSON");
|
||||
}
|
||||
pattern_params_t p = patterns_get();
|
||||
cJSON *mode = cJSON_GetObjectItem(in, "mode");
|
||||
if (cJSON_IsString(mode)) {
|
||||
pattern_mode_t m = patterns_mode_from_name(mode->valuestring);
|
||||
if (m == PM_COUNT) {
|
||||
cJSON_Delete(in);
|
||||
return send_error(req, "400 Bad Request", "unknown mode");
|
||||
}
|
||||
p.mode = m;
|
||||
}
|
||||
read_rgb(cJSON_GetObjectItem(in, "c1"), p.c1);
|
||||
read_rgb(cJSON_GetObjectItem(in, "c2"), p.c2);
|
||||
cJSON *speed = cJSON_GetObjectItem(in, "speed_ms");
|
||||
if (cJSON_IsNumber(speed)) p.speed_ms = (uint32_t)speed->valueint;
|
||||
cJSON *bright = cJSON_GetObjectItem(in, "brightness");
|
||||
if (cJSON_IsNumber(bright)) {
|
||||
int b = bright->valueint;
|
||||
p.brightness = (uint8_t)(b < 0 ? 0 : b > 255 ? 255 : b);
|
||||
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||
}
|
||||
preset_t p;
|
||||
patterns_get(&p);
|
||||
bool ok = preset_from_json(in, &p);
|
||||
cJSON_Delete(in);
|
||||
if (!ok) {
|
||||
return send_error(req, "400 Bad Request", "unknown pattern");
|
||||
}
|
||||
patterns_set(p);
|
||||
return send_json(req, cJSON_CreateObject());
|
||||
}
|
||||
@@ -245,6 +228,399 @@ static esp_err_t wifi_scan_get(httpd_req_t *req)
|
||||
return send_json(req, root);
|
||||
}
|
||||
|
||||
// A DNS label: 1-63 letters, digits or hyphens, not starting or ending with a hyphen
|
||||
static bool valid_hostname(const char *s)
|
||||
{
|
||||
size_t n = strlen(s);
|
||||
if (n == 0 || n > 63 || s[0] == '-' || s[n - 1] == '-') {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char c = s[i];
|
||||
bool ok = (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-';
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool valid_ipv4(const char *s)
|
||||
{
|
||||
esp_ip4_addr_t a;
|
||||
return s && s[0] && esp_netif_str_to_ip4(s, &a) == ESP_OK;
|
||||
}
|
||||
|
||||
// GET /api/network : mDNS name and client address settings, with the current address
|
||||
static esp_err_t network_get(httpd_req_t *req)
|
||||
{
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
char ip[16];
|
||||
bool up = net_sta_status(ip, sizeof(ip));
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "hostname", n.hostname);
|
||||
cJSON_AddStringToObject(o, "mode", n.use_static ? "static" : "dhcp");
|
||||
cJSON_AddStringToObject(o, "ip", n.ip);
|
||||
cJSON_AddStringToObject(o, "netmask", n.netmask);
|
||||
cJSON_AddStringToObject(o, "gateway", n.gateway);
|
||||
cJSON_AddStringToObject(o, "dns", n.dns);
|
||||
cJSON_AddBoolToObject(o, "connected", up);
|
||||
cJSON_AddStringToObject(o, "current_ip", ip);
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
// POST /api/network {"hostname","mode":"dhcp"|"static","ip","netmask","gateway","dns"}
|
||||
// Missing fields keep their current value. Saves, applies and reconnects the client.
|
||||
static esp_err_t network_post(httpd_req_t *req)
|
||||
{
|
||||
cJSON *in = read_json(req);
|
||||
if (!in) {
|
||||
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||
}
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
|
||||
cJSON *h = cJSON_GetObjectItem(in, "hostname");
|
||||
if (cJSON_IsString(h)) {
|
||||
char name[64];
|
||||
strncpy(name, h->valuestring, sizeof(name) - 1);
|
||||
name[sizeof(name) - 1] = '\0';
|
||||
for (char *p = name; *p; p++) {
|
||||
if (*p >= 'A' && *p <= 'Z') *p = *p - 'A' + 'a';
|
||||
}
|
||||
if (!valid_hostname(name)) {
|
||||
cJSON_Delete(in);
|
||||
return send_error(req, "400 Bad Request", "hostname: 1-63 letters, digits or hyphens");
|
||||
}
|
||||
strncpy(n.hostname, name, sizeof(n.hostname) - 1);
|
||||
}
|
||||
|
||||
cJSON *mode = cJSON_GetObjectItem(in, "mode");
|
||||
if (cJSON_IsString(mode)) {
|
||||
if (strcmp(mode->valuestring, "static") == 0) {
|
||||
n.use_static = true;
|
||||
} else if (strcmp(mode->valuestring, "dhcp") == 0) {
|
||||
n.use_static = false;
|
||||
} else {
|
||||
cJSON_Delete(in);
|
||||
return send_error(req, "400 Bad Request", "mode must be dhcp or static");
|
||||
}
|
||||
}
|
||||
|
||||
struct { const char *key; char *dst; size_t len; } fields[] = {
|
||||
{ "ip", n.ip, sizeof(n.ip) },
|
||||
{ "netmask", n.netmask, sizeof(n.netmask) },
|
||||
{ "gateway", n.gateway, sizeof(n.gateway) },
|
||||
{ "dns", n.dns, sizeof(n.dns) },
|
||||
};
|
||||
for (auto &f : fields) {
|
||||
cJSON *v = cJSON_GetObjectItem(in, f.key);
|
||||
if (cJSON_IsString(v)) {
|
||||
strncpy(f.dst, v->valuestring, f.len - 1);
|
||||
f.dst[f.len - 1] = '\0';
|
||||
}
|
||||
}
|
||||
cJSON_Delete(in);
|
||||
|
||||
if (n.use_static) {
|
||||
if (!valid_ipv4(n.ip) || !valid_ipv4(n.netmask) || !valid_ipv4(n.gateway)) {
|
||||
return send_error(req, "400 Bad Request", "static mode needs ip, netmask and gateway as IPv4 addresses");
|
||||
}
|
||||
if (n.dns[0] && !valid_ipv4(n.dns)) {
|
||||
return send_error(req, "400 Bad Request", "dns must be an IPv4 address or empty");
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t err = net_save_network(&n);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Network apply failed: %s", esp_err_to_name(err));
|
||||
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
||||
}
|
||||
// With a static address the page's current address changes, so say where to go next
|
||||
char addr[80];
|
||||
if (n.use_static) {
|
||||
snprintf(addr, sizeof(addr), "%s", n.ip);
|
||||
} else {
|
||||
snprintf(addr, sizeof(addr), "%s.local", n.hostname);
|
||||
}
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "message", "saved; reconnecting");
|
||||
cJSON_AddStringToObject(o, "address", addr);
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
// ---- presets -----------------------------------------------------------------------------------
|
||||
|
||||
// ---- LED strip hardware ------------------------------------------------------------------------
|
||||
|
||||
static bool valid_order(const char *s)
|
||||
{
|
||||
if (strlen(s) != 3) return false;
|
||||
bool seen[3] = { false, false, false };
|
||||
for (int i = 0; i < 3; i++) {
|
||||
int k = s[i] == 'r' ? 0 : (s[i] == 'g' ? 1 : (s[i] == 'b' ? 2 : -1));
|
||||
if (k < 0 || seen[k]) return false;
|
||||
seen[k] = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// GPIOs that can drive the strip on the ESP32-C3: not the flash (11-17) or the USB (18, 19)
|
||||
static bool valid_pin(int pin)
|
||||
{
|
||||
return (pin >= 0 && pin <= 10) || pin == 20 || pin == 21;
|
||||
}
|
||||
|
||||
// GET /api/hardware : LED strip settings
|
||||
static esp_err_t hardware_get(httpd_req_t *req)
|
||||
{
|
||||
hw_cfg_t hw;
|
||||
settings_load_hw(&hw);
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "rgb_order", hw.order);
|
||||
cJSON_AddNumberToObject(o, "max_brightness", hw.max_brightness);
|
||||
cJSON_AddNumberToObject(o, "num_leds", hw.num_leds);
|
||||
cJSON_AddNumberToObject(o, "pin", hw.pin);
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
// POST /api/hardware {"rgb_order","max_brightness":0-255,"num_leds":1-512}. Missing fields keep their value.
|
||||
static esp_err_t hardware_post(httpd_req_t *req)
|
||||
{
|
||||
cJSON *in = read_json(req);
|
||||
if (!in) {
|
||||
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||
}
|
||||
hw_cfg_t hw;
|
||||
settings_load_hw(&hw);
|
||||
cJSON *v = cJSON_GetObjectItem(in, "rgb_order");
|
||||
if (cJSON_IsString(v)) {
|
||||
char order[4];
|
||||
for (int i = 0; i < 3; i++) order[i] = (char)(v->valuestring[i] >= 'A' && v->valuestring[i] <= 'Z' ? v->valuestring[i] + 32 : v->valuestring[i]);
|
||||
order[3] = '\0';
|
||||
if (!valid_order(order)) {
|
||||
cJSON_Delete(in);
|
||||
return send_error(req, "400 Bad Request", "rgb_order must be rgb, rbg, grb, gbr, brg or bgr");
|
||||
}
|
||||
strncpy(hw.order, order, sizeof(hw.order));
|
||||
}
|
||||
v = cJSON_GetObjectItem(in, "max_brightness");
|
||||
if (cJSON_IsNumber(v)) {
|
||||
if (v->valueint < 0 || v->valueint > 255) {
|
||||
cJSON_Delete(in);
|
||||
return send_error(req, "400 Bad Request", "max_brightness must be 0-255");
|
||||
}
|
||||
hw.max_brightness = (uint8_t)v->valueint;
|
||||
}
|
||||
v = cJSON_GetObjectItem(in, "num_leds");
|
||||
if (cJSON_IsNumber(v)) {
|
||||
if (v->valueint < 1 || v->valueint > FX_LEDS_MAX) {
|
||||
cJSON_Delete(in);
|
||||
return send_error(req, "400 Bad Request", "num_leds must be 1-512");
|
||||
}
|
||||
hw.num_leds = v->valueint;
|
||||
}
|
||||
v = cJSON_GetObjectItem(in, "pin");
|
||||
if (cJSON_IsNumber(v)) {
|
||||
if (!valid_pin(v->valueint)) {
|
||||
cJSON_Delete(in);
|
||||
return send_error(req, "400 Bad Request", "pin must be 0-10 or 20-21 (not 11-19, which the flash and USB use)");
|
||||
}
|
||||
hw.pin = v->valueint;
|
||||
}
|
||||
cJSON_Delete(in);
|
||||
|
||||
esp_err_t err = patterns_set_hardware(hw);
|
||||
if (err != ESP_OK) {
|
||||
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
||||
}
|
||||
err = settings_save_hw(&hw);
|
||||
if (err != ESP_OK) {
|
||||
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
||||
}
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "message", "saved");
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
// GET /api/ddp[?led=N] : DDP receiver state, and the colour shown on LED N (for checks)
|
||||
static esp_err_t ddp_get(httpd_req_t *req)
|
||||
{
|
||||
bool active;
|
||||
uint32_t packets;
|
||||
patterns_ddp_status(&active, &packets);
|
||||
int led = 0;
|
||||
char q[32];
|
||||
if (httpd_req_get_url_query_str(req, q, sizeof(q)) == ESP_OK) {
|
||||
char v[8];
|
||||
if (httpd_query_key_value(q, "led", v, sizeof(v)) == ESP_OK) {
|
||||
led = atoi(v);
|
||||
}
|
||||
}
|
||||
uint8_t px[3];
|
||||
patterns_shown_pixel(led, px);
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddBoolToObject(o, "active", active);
|
||||
cJSON_AddNumberToObject(o, "packets", packets);
|
||||
cJSON_AddNumberToObject(o, "port", DDP_PORT);
|
||||
cJSON_AddNumberToObject(o, "led", led);
|
||||
cJSON *px_arr = cJSON_AddArrayToObject(o, "rgb");
|
||||
for (int i = 0; i < 3; i++) {
|
||||
cJSON_AddItemToArray(px_arr, cJSON_CreateNumber(px[i]));
|
||||
}
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
// POST /api/presets/order {"names":[...]} : the presets' order on the front page
|
||||
static esp_err_t presets_order_post(httpd_req_t *req)
|
||||
{
|
||||
cJSON *in = read_json(req);
|
||||
if (!in) {
|
||||
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||
}
|
||||
cJSON *arr = cJSON_GetObjectItem(in, "names");
|
||||
if (!cJSON_IsArray(arr)) {
|
||||
cJSON_Delete(in);
|
||||
return send_error(req, "400 Bad Request", "names must be a list");
|
||||
}
|
||||
const char *names[PRESETS_MAX];
|
||||
size_t n = 0;
|
||||
cJSON *item;
|
||||
cJSON_ArrayForEach(item, arr) {
|
||||
if (n < PRESETS_MAX && cJSON_IsString(item)) {
|
||||
names[n++] = item->valuestring;
|
||||
}
|
||||
}
|
||||
presets_set_order(names, n);
|
||||
cJSON_Delete(in);
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "message", "saved");
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
// POST /api/presets/defaults : add the default presets that are missing
|
||||
static esp_err_t presets_defaults_post(httpd_req_t *req)
|
||||
{
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(o, "added", presets_add_defaults());
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
// GET /api/presets
|
||||
static esp_err_t presets_get(httpd_req_t *req)
|
||||
{
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON *arr = cJSON_AddArrayToObject(o, "presets");
|
||||
for (size_t i = 0; i < presets_count(); i++) {
|
||||
cJSON_AddItemToArray(arr, preset_to_json(presets_at(i)));
|
||||
}
|
||||
cJSON_AddStringToObject(o, "selected", presets_selected());
|
||||
cJSON_AddStringToObject(o, "startup", presets_startup());
|
||||
cJSON_AddNumberToObject(o, "max", PRESETS_MAX);
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
// POST /api/brightness {"brightness":0-255} : live brightness for every pattern, saved
|
||||
static esp_err_t brightness_post(httpd_req_t *req)
|
||||
{
|
||||
cJSON *in = read_json(req);
|
||||
if (!in) {
|
||||
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||
}
|
||||
cJSON *v = cJSON_GetObjectItem(in, "brightness");
|
||||
if (!cJSON_IsNumber(v) || v->valueint < 0 || v->valueint > 255) {
|
||||
cJSON_Delete(in);
|
||||
return send_error(req, "400 Bad Request", "brightness must be 0-255");
|
||||
}
|
||||
uint8_t level = (uint8_t)v->valueint;
|
||||
cJSON_Delete(in);
|
||||
patterns_set_global(level);
|
||||
hw_cfg_t hw;
|
||||
settings_load_hw(&hw);
|
||||
hw.global_brightness = level;
|
||||
settings_save_hw(&hw);
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "message", "saved");
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
// POST /api/presets : create or update by name. Missing fields take the running settings.
|
||||
static esp_err_t presets_post(httpd_req_t *req)
|
||||
{
|
||||
cJSON *in = read_json(req);
|
||||
if (!in) {
|
||||
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||
}
|
||||
cJSON *name = cJSON_GetObjectItem(in, "name");
|
||||
if (!cJSON_IsString(name) || !name->valuestring[0] || strlen(name->valuestring) >= 32) {
|
||||
cJSON_Delete(in);
|
||||
return send_error(req, "400 Bad Request", "name must be 1-31 characters");
|
||||
}
|
||||
preset_t pr;
|
||||
patterns_get(&pr);
|
||||
bool ok = preset_from_json(in, &pr);
|
||||
cJSON_Delete(in);
|
||||
if (!ok) {
|
||||
return send_error(req, "400 Bad Request", "unknown pattern");
|
||||
}
|
||||
|
||||
esp_err_t err = presets_save(&pr);
|
||||
if (err == ESP_ERR_NO_MEM) {
|
||||
return send_error(req, "400 Bad Request", "preset list is full");
|
||||
}
|
||||
if (err != ESP_OK) {
|
||||
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
||||
}
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "message", "saved");
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
// Shared body for select, delete and startup: {"name"}
|
||||
static esp_err_t preset_name_action(httpd_req_t *req, esp_err_t (*action)(const char *), const char *ok_msg)
|
||||
{
|
||||
cJSON *in = read_json(req);
|
||||
if (!in) {
|
||||
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||
}
|
||||
cJSON *name = cJSON_GetObjectItem(in, "name");
|
||||
if (!cJSON_IsString(name)) {
|
||||
cJSON_Delete(in);
|
||||
return send_error(req, "400 Bad Request", "name is required");
|
||||
}
|
||||
char buf[32];
|
||||
strncpy(buf, name->valuestring, sizeof(buf) - 1);
|
||||
buf[sizeof(buf) - 1] = '\0';
|
||||
cJSON_Delete(in);
|
||||
|
||||
esp_err_t err = action(buf);
|
||||
if (err == ESP_ERR_NOT_FOUND) {
|
||||
return send_error(req, "404 Not Found", "no such preset");
|
||||
}
|
||||
if (err != ESP_OK) {
|
||||
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
||||
}
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "message", ok_msg);
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
static esp_err_t select_post(httpd_req_t *req)
|
||||
{
|
||||
return preset_name_action(req, presets_select, "selected");
|
||||
}
|
||||
|
||||
static esp_err_t delete_post(httpd_req_t *req)
|
||||
{
|
||||
return preset_name_action(req, presets_delete, "deleted");
|
||||
}
|
||||
|
||||
static esp_err_t startup_post(httpd_req_t *req)
|
||||
{
|
||||
return preset_name_action(req, presets_set_startup, "startup set");
|
||||
}
|
||||
|
||||
static void add_uri(httpd_handle_t srv, const char *uri, httpd_method_t method, esp_err_t (*handler)(httpd_req_t *))
|
||||
{
|
||||
httpd_uri_t u = { .uri = uri, .method = method, .handler = handler, .user_ctx = NULL };
|
||||
@@ -254,7 +630,8 @@ static void add_uri(httpd_handle_t srv, const char *uri, httpd_method_t method,
|
||||
esp_err_t web_start(void)
|
||||
{
|
||||
httpd_config_t cfg = HTTPD_DEFAULT_CONFIG();
|
||||
cfg.max_uri_handlers = 12;
|
||||
cfg.max_uri_handlers = 30;
|
||||
cfg.stack_size = 8192;
|
||||
cfg.lru_purge_enable = true;
|
||||
cfg.max_open_sockets = 7;
|
||||
cfg.backlog_conn = 10;
|
||||
@@ -271,6 +648,20 @@ esp_err_t web_start(void)
|
||||
add_uri(s_server, "/api/wifi", HTTP_GET, wifi_get);
|
||||
add_uri(s_server, "/api/wifi", HTTP_POST, wifi_post);
|
||||
add_uri(s_server, "/api/wifi/scan", HTTP_GET, wifi_scan_get);
|
||||
add_uri(s_server, "/api/network", HTTP_GET, network_get);
|
||||
add_uri(s_server, "/api/network", HTTP_POST, network_post);
|
||||
add_uri(s_server, "/api/ddp", HTTP_GET, ddp_get);
|
||||
add_uri(s_server, "/api/hardware", HTTP_GET, hardware_get);
|
||||
add_uri(s_server, "/api/hardware", HTTP_POST, hardware_post);
|
||||
add_uri(s_server, "/api/presets", HTTP_GET, presets_get);
|
||||
add_uri(s_server, "/api/presets", HTTP_POST, presets_post);
|
||||
add_uri(s_server, "/api/presets/select", HTTP_POST, select_post);
|
||||
add_uri(s_server, "/api/presets/delete", HTTP_POST, delete_post);
|
||||
add_uri(s_server, "/api/presets/startup", HTTP_POST, startup_post);
|
||||
add_uri(s_server, "/api/presets/defaults", HTTP_POST, presets_defaults_post);
|
||||
add_uri(s_server, "/api/presets/order", HTTP_POST, presets_order_post);
|
||||
add_uri(s_server, "/api/brightness", HTTP_POST, brightness_post);
|
||||
ota_register(s_server);
|
||||
ESP_LOGI(TAG, "Web UI on port 80");
|
||||
return ESP_OK;
|
||||
}
|
||||
Reference in new issue
Block a user