DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
gpio_map_routes.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file gpio_map_routes.cpp
6 * @brief GPIO pin-mapper routes (GET serves the JSON, POST drives an output).
7 *
8 * Separated from the host-testable core (gpio_map.cpp) so the serializer + control
9 * parser unit-test without pulling in the server. The pin table is caller-owned.
10 */
11
13
14#if DETWS_ENABLE_GPIO_MAP
15
16#include "dwserver.h"
18
19// All gpio-map-routes state, owned by one instance (internal linkage): the server handle plus
20// the pin table pointer and count, grouped so it is one named owner, unreachable cross-TU.
21// (The route handlers are fixed-signature callbacks, so they reach this single owner directly.)
22struct GpioRoutesCtx
23{
24 DetWebServer *srv = nullptr;
25 DetwsGpioPin *pins = nullptr;
26 uint8_t count = 0;
27};
28static GpioRoutesCtx s_gpior;
29
30static void gpio_get_handler(uint8_t slot_id, HttpReq *req)
31{
32 (void)req;
33 detws_gpio_read(s_gpior.pins, s_gpior.count);
34 char buf[DETWS_GPIO_JSON_BUF];
35 detws_gpio_json(s_gpior.pins, s_gpior.count, buf, sizeof(buf));
36 if (s_gpior.srv)
37 s_gpior.srv->send(slot_id, 200, DET_MIME_JSON, buf);
38}
39
40static void gpio_post_handler(uint8_t slot_id, HttpReq *req)
41{
42 if (!s_gpior.srv)
43 return;
44 uint8_t pin;
45 uint8_t level;
46 if (!detws_gpio_parse_set((const char *)req->body, req->body_len, &pin, &level))
47 {
48 s_gpior.srv->send(slot_id, 400, DET_MIME_TEXT_PLAIN, "bad request");
49 return;
50 }
51 if (!detws_gpio_is_output(s_gpior.pins, s_gpior.count, pin))
52 {
53 s_gpior.srv->send(slot_id, 403, DET_MIME_TEXT_PLAIN, "pin not a mapped output");
54 return;
55 }
56 detws_gpio_write(pin, level);
57 detws_gpio_read(s_gpior.pins, s_gpior.count);
58 char buf[DETWS_GPIO_JSON_BUF];
59 detws_gpio_json(s_gpior.pins, s_gpior.count, buf, sizeof(buf));
60 s_gpior.srv->send(slot_id, 200, DET_MIME_JSON, buf);
61}
62
63void detws_gpio_map_begin(DetWebServer &server, const char *path, DetwsGpioPin *pins, uint8_t count)
64{
65 s_gpior.srv = &server;
66 s_gpior.pins = pins;
67 s_gpior.count = count;
68 detws_gpio_begin_pins(pins, count);
69 const char *p = (path && path[0]) ? path : "/gpio";
70 server.on(p, HttpMethod::HTTP_GET, gpio_get_handler);
71 server.on(p, HttpMethod::HTTP_POST, gpio_post_handler);
72}
73
74#endif // DETWS_ENABLE_GPIO_MAP
#define DETWS_GPIO_JSON_BUF
Stack buffer for the GPIO-map JSON (bytes).
Single-port HTTP server with deterministic, zero-allocation execution.
Definition dwserver.h:348
void on(const char *path, HttpMethod method, Handler callback)
Register a route handler.
Definition dwserver.cpp:759
Layer 7 (Application) - public HTTP routing API.
@ HTTP_POST
Non-idempotent create / action.
@ HTTP_GET
Safe, idempotent read.
Browser GPIO pin-mapper / diagnostics (DETWS_ENABLE_GPIO_MAP).
Shared HTTP Content-Type ("MIME") string constants (one source of truth).
Fully-parsed HTTP/1.1 request.
uint8_t body[BODY_BUF_SIZE+1]
Stored body bytes, always null-terminated.
size_t body_len
Bytes stored in body[] (≤ BODY_BUF_SIZE).