Files
led-controller/main/api_util.h
T
jimmyandClaude Opus 5.5 57182188ba feat: port the remaining v1 API to the v2 controller
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>
2026-11-03 09:58:17 +13:00

51 lines
2.4 KiB
C

#pragma once
#include <stdbool.h>
#include <stddef.h>
#include "cJSON.h"
#include "esp_err.h"
#include "esp_http_server.h"
// Helpers shared by the REST handlers (api.c and the res_*.c resource modules)
// Path segments of "/res/id/action?query" (at most three)
typedef char api_seg_t[3][64];
int api_split(const char *uri, api_seg_t seg);
// Send obj as JSON; own=true deletes it afterwards
esp_err_t api_json(httpd_req_t *req, const char *status, cJSON *obj, bool own);
esp_err_t api_error(httpd_req_t *req, const char *status, const char *msg);
esp_err_t api_message(httpd_req_t *req, const char *status, const char *msg);
// 200 {message} for ESP_OK, otherwise a matching error status
esp_err_t api_result(httpd_req_t *req, esp_err_t err, const char *ok_msg);
// Parsed JSON object body, or NULL after an error response has been sent. api_body_opt treats an
// empty body as {}.
cJSON *api_body(httpd_req_t *req);
cJSON *api_body_opt(httpd_req_t *req);
// Shallow merge of src into dst, skipping "id"
void api_merge(cJSON *dst, const cJSON *src);
// Set (replace or add) one key
void api_set(cJSON *obj, const char *key, cJSON *val);
// Record id as text from a string or number value; false (buf empty) otherwise
bool api_id(const cJSON *v, char *buf, size_t len);
// Add rec under the collection's next id (with an "id" field when rec is an object); id gets the key
void api_add(const char *coll, cJSON *rec, char *id, size_t len);
// Current profile id (settings.current_profile_id, else the first profile), or NULL
const char *api_current_profile(void);
bool api_preset_in_profile(const cJSON *rec);
// Resource handlers, called with the store locked. Each returns true when it answered the request
// (its result in *ret); false hands the request to the generic collection CRUD.
bool profiles_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret);
bool zones_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret);
bool presets_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret);
bool palettes_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret);
bool sequences_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret);
bool devices_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret);
bool patterns_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret);
bool outputs_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret);