feat: JSON API on the USB serial port for led-tool
One request per line on USB-Serial-JTAG (@api {"id","method","path","body"}),
one @api envelope back; @reboot restarts. Requests run on the httpd task
through the same dispatcher as the WebSocket, so the checks match the web UI
and never race an HTTP handler. Replies go out in chunks under a lock that log
output shares, so a reply over the TX buffer size isn't dropped and log lines
can't land inside one. The primary console moves to USB-Serial-JTAG.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4cd3bc7973
commit
762ec338bc
8 files changed
+266
-4
No files matched your search
+35
-2
@@ -12,6 +12,8 @@
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
#include "esp_wifi.h"
|
||||
@@ -1160,8 +1162,8 @@ static esp_err_t api_http_entry(httpd_req_t *req)
|
||||
return reinterpret_cast<api_fn_t>(req->user_ctx)(&a);
|
||||
}
|
||||
|
||||
#define WS_MSG_MAX 2048 // a request message
|
||||
#define WS_REPLY_MAX 8192 // a reply envelope
|
||||
#define WS_MSG_MAX WEB_API_MSG_MAX
|
||||
#define WS_REPLY_MAX WEB_API_REPLY_MAX
|
||||
|
||||
// Run an API route on this driver: target is the path, optionally with a query string.
|
||||
static void ws_local(const char *method, const char *target, const char *body, char *out, size_t cap)
|
||||
@@ -1234,6 +1236,37 @@ static void ws_dispatch(const char *msg, char *out, size_t cap)
|
||||
cJSON_Delete(top);
|
||||
}
|
||||
|
||||
// A call from outside the HTTP server (the USB config port). It runs on the server's own task, so it
|
||||
// never races an HTTP or WebSocket handler (the preset list has no lock of its own).
|
||||
struct api_call_t {
|
||||
const char *msg;
|
||||
char *out;
|
||||
size_t cap;
|
||||
SemaphoreHandle_t done;
|
||||
};
|
||||
|
||||
static void api_call_work(void *arg)
|
||||
{
|
||||
api_call_t *c = static_cast<api_call_t *>(arg);
|
||||
ws_dispatch(c->msg, c->out, c->cap);
|
||||
xSemaphoreGive(c->done);
|
||||
}
|
||||
|
||||
void web_api_call(const char *msg, char *out, size_t cap)
|
||||
{
|
||||
api_call_t c = { msg, out, cap, xSemaphoreCreateBinary() };
|
||||
if (!s_server || !c.done) {
|
||||
envelope(out, cap, 503, "{\"error\":\"web server not running\"}");
|
||||
} else if (httpd_queue_work(s_server, api_call_work, &c) != ESP_OK) {
|
||||
envelope(out, cap, 503, "{\"error\":\"server busy\"}");
|
||||
} else {
|
||||
xSemaphoreTake(c.done, portMAX_DELAY);
|
||||
}
|
||||
if (c.done) {
|
||||
vSemaphoreDelete(c.done);
|
||||
}
|
||||
}
|
||||
|
||||
// WebSocket handler: one text frame in, one text frame out
|
||||
// Another site's page must not drive this driver. A browser sends its Origin with the WebSocket
|
||||
// handshake, and only a page from the LAN (or this device) may open the socket. ESP-IDF has no hook
|
||||
|
||||
Reference in new issue
Block a user