Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
edc0d4a32This file is pinned to the selected release. Download the original to open it in your design tool.
/***************
TBD-16 — dadamachines WebUI & REST API
(c) 2024-2026 Johannes Elias Lohbihler for dadamachines.
Licensed under the GNU General Public License (GPL 3.0):
https://www.gnu.org/licenses/gpl-3.0.txt
A commercial licence is available — contact https://dadamachines.com/contact/
Provided "as is" without any express or implied warranties.
See LICENSE in the repository root for full terms.
SPDX-License-Identifier: GPL-3.0-only
***************/
#include "MacroAPI.hpp"
#include "SPManager.hpp"
#include <cstring>
#include <string>
#include <vector>
#include <inttypes.h>
#include <dirent.h>
#include <sys/stat.h>
#include <cstdio>
#include "esp_vfs_fat.h"
#include "esp_log.h"
#include "esp_heap_caps.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h"
#include "helpers/ctagSampleRom.hpp"
#include "MacroTranslator.hpp"
#include "StorageOverlay.hpp"
#include "EngineDefinitionDataModel.hpp"
using namespace CTAG::REST;
using namespace rapidjson;
using namespace CTAG::MACROPRESETS;
static const char *MACRO_TAG = "MacroAPI";
static void set_api_headers(httpd_req_t *req) {
httpd_resp_set_hdr(req, "Cache-Control", "no-store");
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_set_hdr(req, "Connection", "close");
}
/** Send a JSON string as HTTP response */
static esp_err_t send_json(httpd_req_t *req, const char *json) {
set_api_headers(req);
httpd_resp_set_type(req, "application/json");
if (json) httpd_resp_sendstr(req, json);
else httpd_resp_send(req, NULL, 0);
return ESP_OK;
}
/** Send a simple JSON ok response */
static esp_err_t send_ok(httpd_req_t *req) {
return send_json(req, "{\"ok\":true}");
}
/**
* Read all .json files in a directory and return them as a rapidjson Array.
* Each element is the parsed JSON object from that file.
* Uses SPIRAM for the read buffer to keep internal heap free.
*/
static void read_all_json_in_dir(const char *dirPath, Value &outArray,
Document::AllocatorType &alloc) {
DIR *dir = opendir(dirPath);
if (!dir) {
ESP_LOGW(MACRO_TAG, "Cannot open dir %s", dirPath);
return;
}
char pathBuf[270];
char *fileBuf = (char *)heap_caps_malloc(8192, MALLOC_CAP_SPIRAM);
if (!fileBuf) {
closedir(dir);
ESP_LOGE(MACRO_TAG, "SPIRAM alloc failed for file buffer");
return;
}
struct dirent *ent;
while ((ent = readdir(dir)) != NULL) {
// Only .json files
size_t nlen = strlen(ent->d_name);
if (nlen < 6 || strcasecmp(ent->d_name + nlen - 5, ".json") != 0)
continue;
snprintf(pathBuf, sizeof(pathBuf) - 1, "%s/%s", dirPath, ent->d_name);
pathBuf[sizeof(pathBuf) - 1] = '\0';
FILE *fp = fopen(pathBuf, "r");
if (!fp) continue;
FileReadStream is(fp, fileBuf, 8192);
Document doc;
doc.ParseStream(is);
fclose(fp);
if (doc.HasParseError() || !doc.IsObject()) {
ESP_LOGW(MACRO_TAG, "Skip bad JSON: %s", ent->d_name);
continue;
}
Value copy(doc, alloc);
outArray.PushBack(copy, alloc);
}
heap_caps_free(fileBuf);
closedir(dir);
}
/**
* Read all JSON files from an overlay subdirectory (user overrides factory).
*/
static void read_all_json_overlay(const char *subdir, Value &outArray,
Document::AllocatorType &alloc) {
auto files = CTAG::STORAGE::listMergedDir(subdir);
char *fileBuf = (char *)heap_caps_malloc(8192, MALLOC_CAP_SPIRAM);
if (!fileBuf) {
ESP_LOGE(MACRO_TAG, "SPIRAM alloc failed for file buffer");
return;
}
for (const auto &fn : files) {
size_t nlen = fn.size();
if (nlen < 6 || strcasecmp(fn.c_str() + nlen - 5, ".json") != 0)
continue;
std::string path = CTAG::STORAGE::resolveFile(subdir, fn);
if (path.empty()) continue;
FILE *fp = fopen(path.c_str(), "r");
if (!fp) continue;
FileReadStream is(fp, fileBuf, 8192);
Document doc;
doc.ParseStream(is);
fclose(fp);
if (doc.HasParseError() || !doc.IsObject()) {
ESP_LOGW(MACRO_TAG, "Skip bad JSON: %s", fn.c_str());
continue;
}
Value copy(doc, alloc);
outArray.PushBack(copy, alloc);
}
heap_caps_free(fileBuf);
}
// Forward declarations for handlers defined below
static esp_err_t handle_get_trackdefaults(httpd_req_t *req);
static esp_err_t handle_save_trackdefaults(httpd_req_t *req);
static esp_err_t handle_get_active_trackdefault(httpd_req_t *req);
static esp_err_t handle_set_active_trackdefault(httpd_req_t *req);
/**
* GET /api/v2/macros — dispatched by ?action= query parameter.
*
* (no action / default) → track status (original behaviour)
* ?action=getall → bulk: { macroDefs:[], soundPresets:[], tracks:[] }
*
* The "getall" action replaces dozens of individual config-file fetches with
* a single HTTP round-trip, keeping total API calls within the ESP32 limit.
*/
esp_err_t MacroAPI::macroapi_get_handler(httpd_req_t *req) {
ESP_LOGD(MACRO_TAG, "GET Mem free int %d, SPIRAM %d",
heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL),
heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
char query[128] = {0};
char action[32] = {0};
httpd_req_get_url_query_str(req, query, sizeof(query));
httpd_query_key_value(query, "action", action, sizeof(action));
/* ── action=getall ── bulk fetch all macro data in one response ── */
if (strcmp(action, "getall") == 0) {
Document resp(kObjectType);
auto &alloc = resp.GetAllocator();
// 1) All macro definitions
Value defs(kArrayType);
read_all_json_overlay(CTAG::STORAGE::DIR_MACROS, defs, alloc);
resp.AddMember("macroDefs", defs, alloc);
// 2) All sound presets
Value presets(kArrayType);
read_all_json_overlay(CTAG::STORAGE::DIR_PRESETS, presets, alloc);
resp.AddMember("soundPresets", presets, alloc);
// 3) Current track state
Document trackDoc(kObjectType);
MacroTranslator::instance().SerializeStateInto(trackDoc);
if (trackDoc.HasMember("tracks"))
resp.AddMember("tracks", trackDoc["tracks"], alloc);
StringBuffer sb;
Writer<StringBuffer> writer(sb);
resp.Accept(writer);
return send_json(req, sb.GetString());
}
/* ── action=get_trackdefaults ── read boot-default presets ── */
if (strcmp(action, "get_trackdefaults") == 0) {
return handle_get_trackdefaults(req);
}
/* ── action=get_active_trackdefault ── which template is active ── */
if (strcmp(action, "get_active_trackdefault") == 0) {
return handle_get_active_trackdefault(req);
}
if (strcmp(action, "get_synthdefinitions") == 0) {
Document resp(kObjectType);
auto &alloc = resp.GetAllocator();
Document doc2(kObjectType);
EngineDefinitionDataModel::instance()->SerializeIntoJSON(doc2);
resp.AddMember("tracks", doc2["tracks"], alloc);
resp.AddMember("machines", doc2["machines"], alloc);
StringBuffer sb;
Writer<StringBuffer> writer(sb);
resp.Accept(writer);
return send_json(req, sb.GetString());
}
/* ── default: return current track state ── */
httpd_resp_set_type(req, "application/json");
Document resp(kObjectType);
auto &alloc = resp.GetAllocator();
Document doc2(kObjectType);
MacroTranslator::instance().SerializeStateInto(doc2);
resp.AddMember("tracks", doc2["tracks"], alloc);
StringBuffer sb;
Writer<StringBuffer> writer(sb);
resp.Accept(writer);
return send_json(req, sb.GetString());
}
/* RefreshMacros() parses dozens of JSON files and needs ~12 KB+ of stack.
* The HTTP task only has 8 KB (internal RAM — cannot increase without
* fragmenting memory and breaking audio DMA). We offload the work to a
* short-lived task whose stack is allocated from SPIRAM (abundant). */
static EventGroupHandle_t reloadEventGroup = nullptr;
static const int RELOAD_DONE_BIT = BIT0;
static void reload_task(void *) {
CTAG::AUDIO::SoundProcessorManager::DisablePluginProcessing();
CTAG::AUDIO::SoundProcessorManager::RefreshMacros();
CTAG::AUDIO::SoundProcessorManager::EnablePluginProcessing();
xEventGroupSetBits(reloadEventGroup, RELOAD_DONE_BIT);
vTaskDelete(nullptr);
}
static esp_err_t handle_reload(httpd_req_t *req) {
if (!reloadEventGroup) {
reloadEventGroup = xEventGroupCreate();
}
xEventGroupClearBits(reloadEventGroup, RELOAD_DONE_BIT);
TaskHandle_t h = nullptr;
BaseType_t ok = xTaskCreatePinnedToCoreWithCaps(
reload_task, "reload_macros", 16384, nullptr,
tskIDLE_PRIORITY + 3, &h, 0, MALLOC_CAP_SPIRAM);
if (ok != pdPASS) {
ESP_LOGE(MACRO_TAG, "Failed to create reload task");
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "reload task failed");
return ESP_FAIL;
}
/* Wait synchronously so the WebUI gets 200 only after reload is done. */
xEventGroupWaitBits(reloadEventGroup, RELOAD_DONE_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
return send_ok(req);
}
static esp_err_t handle_set_track_macro(httpd_req_t *req) {
char *content = (char *) heap_caps_malloc(req->content_len + 1, MALLOC_CAP_SPIRAM);
if (!content) {
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Out of memory");
return ESP_FAIL;
}
int ret = httpd_req_recv(req, content, req->content_len);
if (ret <= 0) {
heap_caps_free(content);
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
httpd_resp_send_408(req);
}
return ESP_FAIL;
}
content[req->content_len] = 0;
ESP_LOGD(MACRO_TAG, "set_track_macro data: %s", content);
CTAG::AUDIO::SoundProcessorManager::SetTrackParametersFromJSON(content);
heap_caps_free(content);
return send_ok(req);
}
static std::string trackDefaultsReadPath() {
return CTAG::STORAGE::resolveFile(CTAG::STORAGE::DIR_TRACKDEFAULTS, "default.json");
}
static std::string trackDefaultsWritePath() {
return CTAG::STORAGE::userFilePath(CTAG::STORAGE::DIR_TRACKDEFAULTS, "default.json");
}
/**
* GET ?action=get_trackdefaults
* Returns the contents of trackdefaults/default.json (or "{}" if missing).
*/
static esp_err_t handle_get_trackdefaults(httpd_req_t *req) {
std::string tdPath = trackDefaultsReadPath();
FILE *f = fopen(tdPath.c_str(), "r");
if (!f) {
ESP_LOGW(MACRO_TAG, "trackdefaults.json not found, returning {}");
return send_json(req, "{}");
}
fseek(f, 0, SEEK_END);
long sz = ftell(f);
fseek(f, 0, SEEK_SET);
if (sz <= 0 || sz >= 8192) {
fclose(f);
return send_json(req, "{}");
}
char *buf = (char *)heap_caps_malloc(sz + 1, MALLOC_CAP_SPIRAM);
if (!buf) {
fclose(f);
return send_json(req, "{}");
}
fread(buf, 1, sz, f);
buf[sz] = '\0';
fclose(f);
esp_err_t ret2 = send_json(req, buf);
heap_caps_free(buf);
return ret2;
}
/**
* POST ?action=save_trackdefaults
* Writes the request body (JSON) to user/trackdefaults/default.json.
* If the sampleKit field changed, also updates the active sample bank
* in sample_rom.json and reloads PSRAM so the device is immediately in sync.
*/
static esp_err_t handle_save_trackdefaults(httpd_req_t *req) {
if (req->content_len == 0 || req->content_len > 8192) {
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid body size");
return ESP_FAIL;
}
char *content = (char *)heap_caps_malloc(req->content_len + 1, MALLOC_CAP_SPIRAM);
if (!content) {
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Out of memory");
return ESP_FAIL;
}
int ret = httpd_req_recv(req, content, req->content_len);
if (ret <= 0) {
heap_caps_free(content);
if (ret == HTTPD_SOCK_ERR_TIMEOUT) httpd_resp_send_408(req);
return ESP_FAIL;
}
content[req->content_len] = '\0';
// Resolve the kit filename to an index for PSRAM switching
int kitIndex = -1;
{
Document doc;
doc.Parse(content);
if (!doc.HasParseError() && doc.IsObject() &&
doc.HasMember("kit") && doc["kit"].IsString()) {
std::string kitFile = doc["kit"].GetString();
kitIndex = CTAG::SP::HELPERS::ctagSampleRom::GetBankIndexFromFileName(kitFile);
}
}
FILE *f = fopen(trackDefaultsWritePath().c_str(), "w");
if (!f) {
heap_caps_free(content);
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Cannot write file");
return ESP_FAIL;
}
fwrite(content, 1, req->content_len, f);
fclose(f);
ESP_LOGI(MACRO_TAG, "Saved trackdefaults.json (%d bytes)", req->content_len);
heap_caps_free(content);
// Sync PSRAM with the requested kit so the device is ready at next boot
if (kitIndex >= 0) {
ESP_LOGI(MACRO_TAG, "Switching active sample bank to %d and reloading PSRAM", kitIndex);
CTAG::AUDIO::SoundProcessorManager::DisablePluginProcessing();
CTAG::SP::HELPERS::ctagSampleRom::SetActiveSampleBank(static_cast<uint8_t>(kitIndex));
CTAG::SP::HELPERS::ctagSampleRom::RefreshDataStructure();
CTAG::AUDIO::SoundProcessorManager::EnablePluginProcessing();
}
return send_ok(req);
}
/**
* GET ?action=get_active_trackdefault
* Returns {"name":"<template>"} from user/config/active-trackdefault.txt.
* Falls back to "default" if the file doesn't exist.
*/
static esp_err_t handle_get_active_trackdefault(httpd_req_t *req) {
std::string name = "default";
std::string cfgPath = CTAG::STORAGE::userPath() + "/" + CTAG::STORAGE::DIR_CONFIG + "/active-trackdefault.txt";
FILE *f = fopen(cfgPath.c_str(), "r");
if (f) {
char buf[64] = {};
if (fgets(buf, sizeof(buf), f)) {
size_t len = strlen(buf);
while (len > 0 && (buf[len-1] == '\n' || buf[len-1] == '\r')) buf[--len] = '\0';
if (len > 0) name = buf;
}
fclose(f);
}
Document resp(kObjectType);
auto &alloc = resp.GetAllocator();
resp.AddMember("name", Value(name.c_str(), alloc), alloc);
StringBuffer sb;
Writer<StringBuffer> writer(sb);
resp.Accept(writer);
return send_json(req, sb.GetString());
}
/**
* POST ?action=set_active_trackdefault
* Body: {"name":"<template>"}
* Writes the template name to user/config/active-trackdefault.txt.
*/
static esp_err_t handle_set_active_trackdefault(httpd_req_t *req) {
if (req->content_len == 0 || req->content_len > 256) {
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid body size");
return ESP_FAIL;
}
char *content = (char *)heap_caps_malloc(req->content_len + 1, MALLOC_CAP_SPIRAM);
if (!content) {
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Out of memory");
return ESP_FAIL;
}
int ret = httpd_req_recv(req, content, req->content_len);
if (ret <= 0) {
heap_caps_free(content);
if (ret == HTTPD_SOCK_ERR_TIMEOUT) httpd_resp_send_408(req);
return ESP_FAIL;
}
content[req->content_len] = '\0';
Document doc;
doc.Parse(content);
heap_caps_free(content);
if (doc.HasParseError() || !doc.IsObject() || !doc.HasMember("name") || !doc["name"].IsString()) {
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Expected {\"name\":\"...\"}");
return ESP_FAIL;
}
std::string name = doc["name"].GetString();
if (name.empty() || name.size() > 60) {
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid template name");
return ESP_FAIL;
}
std::string cfgDir = CTAG::STORAGE::userPath() + "/" + CTAG::STORAGE::DIR_CONFIG;
mkdir(cfgDir.c_str(), 0755);
std::string cfgPath = cfgDir + "/active-trackdefault.txt";
FILE *f = fopen(cfgPath.c_str(), "w");
if (!f) {
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Cannot write config");
return ESP_FAIL;
}
fwrite(name.c_str(), 1, name.size(), f);
fclose(f);
ESP_LOGI(MACRO_TAG, "Set active track default to \"%s\"", name.c_str());
return send_ok(req);
}
esp_err_t MacroAPI::macroapi_post_handler(httpd_req_t *req) {
ESP_LOGD(MACRO_TAG, "POST Mem free int %d, SPIRAM %d",
heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL),
heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
size_t qlen = httpd_req_get_url_query_len(req);
char action[32] = {0};
char defId[64] = {0};
if (qlen > 0) {
char *query = (char *)malloc(qlen + 1);
httpd_req_get_url_query_str(req, query, qlen + 1);
httpd_query_key_value(query, "action", action, sizeof(action));
httpd_query_key_value(query, "id", defId, sizeof(defId));
free(query);
}
if (strcmp(action, "reload") == 0) {
if (defId[0] != '\0') {
/* Targeted single-defion reload — lightweight, runs inline */
ESP_LOGI(MACRO_TAG, "Targeted reload for id=%s", defId);
CTAG::AUDIO::SoundProcessorManager::RefreshSingleMacro(std::string(defId));
return send_ok(req);
}
return handle_reload(req);
}
else if (strcmp(action, "set_track_parameters") == 0) {
return handle_set_track_macro(req);
}
else if (strcmp(action, "update_track") == 0) {
return handle_set_track_macro(req);
}
else if (strcmp(action, "save_trackdefaults") == 0) {
return handle_save_trackdefaults(req);
}
else if (strcmp(action, "set_active_trackdefault") == 0) {
return handle_set_active_trackdefault(req);
}
httpd_resp_set_type(req, "text/html");
httpd_resp_send(req, NULL, 0);
return ESP_OK;
}