DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
web_terminal.h
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 web_terminal.h
6 * @brief Browser "web serial" terminal over WebSocket (DETWS_ENABLE_WEB_TERMINAL).
7 *
8 * A zero-heap equivalent of the WebSerial-style remote serial monitor: it serves
9 * a self-contained terminal web page and a WebSocket endpoint on the same path.
10 * Device output is broadcast to every connected browser; each line a browser
11 * sends is delivered to a command callback. Rides the library's existing
12 * WebSocket layer (no extra connection state), so it is TLS-agnostic - the page
13 * auto-selects ws:// or wss:// from the page's own scheme.
14 *
15 * @code
16 * void on_cmd(const char *line, uint8_t client) {
17 * detws_web_terminal_printf("you said: %s\n", line);
18 * }
19 * void setup() {
20 * // ... wifi + server.on(...) ...
21 * detws_web_terminal_begin(server, "/terminal");
22 * detws_web_terminal_on_command(on_cmd);
23 * server.begin(80);
24 * }
25 * void loop() {
26 * server.handle();
27 * detws_web_terminal_printf("uptime %lu\n", millis()); // device -> browsers
28 * }
29 * @endcode
30 *
31 * No-op stubs when DETWS_ENABLE_WEB_TERMINAL is 0.
32 */
33
34#ifndef DETERMINISTICESPASYNCWEBSERVER_WEB_TERMINAL_H
35#define DETERMINISTICESPASYNCWEBSERVER_WEB_TERMINAL_H
36
37#include "dwserver.h"
38
39#if DETWS_ENABLE_WEB_TERMINAL
40
41/**
42 * @brief Callback for a line typed in a connected browser terminal.
43 * @param line Null-terminated command text (no trailing newline).
44 * @param client_id WebSocket client index that sent it (ws_pool[] slot).
45 */
46typedef void (*TermCommandCb)(const char *line, uint8_t client_id);
47
48/**
49 * @brief Register the terminal page + WebSocket endpoint on @p server.
50 *
51 * Serves the HTML page at @p path (GET) and accepts the terminal WebSocket at
52 * `<path>/ws`. Call before server.begin().
53 *
54 * @param server The web server to attach to (must outlive the terminal).
55 * @param path URL path for the page (default "/terminal").
56 */
57void detws_web_terminal_begin(DetWebServer &server, const char *path = "/terminal");
58
59/** @brief Install the command callback (browser -> device). Pass nullptr to clear. */
60void detws_web_terminal_on_command(TermCommandCb cb);
61
62/** @brief Broadcast text to every connected terminal browser (device -> browsers). */
63void detws_web_terminal_print(const char *s);
64
65/** @brief Like print() but appends a newline. */
66void detws_web_terminal_println(const char *s);
67
68/** @brief printf-style broadcast (capped at TERM_TX_BUF_SIZE). */
69void detws_web_terminal_printf(const char *fmt, ...)
70#if defined(__GNUC__)
71 __attribute__((format(printf, 1, 2)))
72#endif
73 ;
74
75/** @brief Number of browsers currently connected to the terminal. */
76uint8_t detws_web_terminal_client_count();
77
78#else // DETWS_ENABLE_WEB_TERMINAL == 0 -> no-op stubs
79
80typedef void (*TermCommandCb)(const char *line, uint8_t client_id);
81static inline void detws_web_terminal_begin(DetWebServer &, const char * = "/terminal")
82{
83}
84static inline void detws_web_terminal_on_command(TermCommandCb)
85{
86}
87static inline void detws_web_terminal_print(const char *)
88{
89}
90static inline void detws_web_terminal_println(const char *)
91{
92}
93static inline void detws_web_terminal_printf(const char *, ...)
94{
95}
96static inline uint8_t detws_web_terminal_client_count()
97{
98 return 0;
99}
100
101#endif // DETWS_ENABLE_WEB_TERMINAL
102
103#endif // DETERMINISTICESPASYNCWEBSERVER_WEB_TERMINAL_H
Single-port HTTP server with deterministic, zero-allocation execution.
Definition dwserver.h:348
Layer 7 (Application) - public HTTP routing API.
void(* TermCommandCb)(const char *line, uint8_t client_id)