Profiles seed presets, a default zone and a palette, and clone with their own copies; deleting a profile removes what it owned. Zones get the v1 list shape, set-current, current, clone and content-fragment. Devices are keyed by MAC with v1 validation and identify. Patterns: definitions, OTA file and manifest, upload, driver, and send over ESP-NOW (the led-driver pattern sources are embedded in the firmware). Pixel outputs get v1 validation and a DDP/Art-Net/sACN test pattern. Sequences, palettes and settings follow v1 replies and validation, and /ws forwards browser messages to the drivers. The resource handlers move out of api.c into res_*.c, patterns.c and outputs.c. cJSON now allocates from PSRAM: the store had grown enough to starve internal RAM and abort the mic at boot. /status reports free_internal. The Wi-Fi AP and led-tool routes are left out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
91 lines
2.7 KiB
C
91 lines
2.7 KiB
C
#include "ws.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "cJSON.h"
|
|
#include "engine.h"
|
|
#include "esp_log.h"
|
|
#include "store.h"
|
|
|
|
static const char *TAG = "ws";
|
|
|
|
#define FRAME_MAX 4096
|
|
|
|
static void send_text(httpd_req_t *req, const char *text)
|
|
{
|
|
httpd_ws_frame_t f = { .type = HTTPD_WS_TYPE_TEXT, .payload = (uint8_t *)text, .len = strlen(text) };
|
|
httpd_ws_send_frame_async(req->handle, httpd_req_to_sockfd(req), &f);
|
|
}
|
|
|
|
// One v1 driver message from the browser: "to" (a MAC) addresses one driver, otherwise it is broadcast
|
|
static void forward(httpd_req_t *req, const char *text)
|
|
{
|
|
cJSON *msg = cJSON_Parse(text);
|
|
if (!cJSON_IsObject(msg)) {
|
|
cJSON_Delete(msg);
|
|
send_text(req, "{\"error\":\"JSON object required\"}");
|
|
return;
|
|
}
|
|
cJSON *to = cJSON_DetachItemFromObject(msg, "to"), *targets = NULL;
|
|
if (cJSON_IsString(to)) {
|
|
targets = cJSON_CreateArray();
|
|
cJSON_AddItemToArray(targets, to);
|
|
} else {
|
|
cJSON_Delete(to);
|
|
}
|
|
cJSON *messages = cJSON_CreateArray();
|
|
cJSON_AddItemToArray(messages, msg);
|
|
store_lock();
|
|
esp_err_t err = engine_push(messages, targets);
|
|
store_unlock();
|
|
cJSON_Delete(messages);
|
|
cJSON_Delete(targets);
|
|
if (err != ESP_OK) {
|
|
send_text(req, "{\"error\":\"Send failed\"}");
|
|
}
|
|
}
|
|
|
|
static esp_err_t ws_handler(httpd_req_t *req)
|
|
{
|
|
if (req->method == HTTP_GET) {
|
|
if (httpd_ws_get_fd_info(req->handle, httpd_req_to_sockfd(req)) != HTTPD_WS_CLIENT_WEBSOCKET) {
|
|
httpd_resp_set_status(req, "426 Upgrade Required");
|
|
httpd_resp_set_hdr(req, "Upgrade", "websocket");
|
|
return httpd_resp_sendstr(req, "websocket only");
|
|
}
|
|
// The controller keeps no Wi-Fi driver connections, so the v1 snapshot is always empty
|
|
send_text(req, "{\"type\":\"device_tcp_snapshot\",\"connected_ips\":[]}");
|
|
return ESP_OK;
|
|
}
|
|
httpd_ws_frame_t f = { 0 };
|
|
esp_err_t err = httpd_ws_recv_frame(req, &f, 0);
|
|
if (err != ESP_OK) {
|
|
return err;
|
|
}
|
|
if (f.type != HTTPD_WS_TYPE_TEXT || f.len == 0) {
|
|
return ESP_OK;
|
|
}
|
|
if (f.len > FRAME_MAX) {
|
|
ESP_LOGW(TAG, "frame of %u bytes dropped", (unsigned)f.len);
|
|
return ESP_FAIL; // closes the connection
|
|
}
|
|
char *buf = malloc(f.len + 1);
|
|
if (!buf) {
|
|
return ESP_ERR_NO_MEM;
|
|
}
|
|
f.payload = (uint8_t *)buf;
|
|
err = httpd_ws_recv_frame(req, &f, f.len);
|
|
if (err == ESP_OK) {
|
|
buf[f.len] = 0;
|
|
forward(req, buf);
|
|
}
|
|
free(buf);
|
|
return err;
|
|
}
|
|
|
|
esp_err_t ws_register(httpd_handle_t server)
|
|
{
|
|
const httpd_uri_t u = { .uri = "/ws", .method = HTTP_GET, .handler = ws_handler, .is_websocket = true };
|
|
return httpd_register_uri_handler(server, &u);
|
|
}
|