forked from markqvist/RNode_Firmware
-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathWebSocketConsole.h
More file actions
58 lines (47 loc) · 2.28 KB
/
Copy pathWebSocketConsole.h
File metadata and controls
58 lines (47 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// WebSocketConsole — KISS↔WebSocket bridge for the embedded web console.
//
// Sits between WebSocketServer (RFC 6455 frames) and the firmware's KISS
// I/O. One WebSocket binary frame == one KISS frame on the wire, including
// the leading and trailing FEND markers.
//
// Outbound (firmware → browser):
// * Hook into Utilities.h::serial_write() via ws_console::on_serial_write(b).
// * We buffer bytes between KISS frame boundaries (FEND) and emit one
// binary WebSocket frame per complete KISS frame.
//
// Inbound (browser → firmware):
// * Each binary WS frame is treated as one KISS frame and pushed into
// serialFIFO; serial_poll() in RNode_Firmware.ino then parses it.
// * Text frames are not currently used (KISS is binary). They're
// accepted by WebSocketServer but ignored here.
//
// Single-client by design — matches Remote.h and TCPHostInterface.h.
#ifndef WEBSOCKET_CONSOLE_H
#define WEBSOCKET_CONSOLE_H
#if defined(ENABLE_WEBSOCKETS) && __has_include(<WiFi.h>)
#include <cstdint>
namespace ws_console {
// Initialize on the given TCP port (typically 81 alongside HTTP on 80).
// bind_public is honored only on native (Portduino) — false binds the
// listening socket to 127.0.0.1, true to 0.0.0.0. ESP32 always binds
// 0.0.0.0 regardless (the console is served over WiFi AP).
// Safe to call multiple times — second and subsequent calls no-op.
void init(uint16_t port, bool bind_public = false);
// Drive the underlying WebSocketServer. Call once per main-loop tick.
// Non-blocking.
void service();
// Outbound tap from serial_write(). Buffers bytes between KISS FEND
// markers and flushes a complete KISS frame as a single WS binary frame
// when a client is attached. Bytes are dropped on the floor when no
// client is attached — same behavior as the existing wifi/native TCP
// transports.
void on_serial_write(uint8_t byte);
// True when a client is connected and the WS handshake is complete.
bool client_attached();
// Tear down the underlying WebSocketServer (close listener, drop client,
// delete the singleton). Called from the native deferred-reboot path so
// the re-exec'd process can re-bind the same port. Idempotent.
void shutdown();
} // namespace ws_console
#endif // ENABLE_WEBSOCKETS && __has_include(<WiFi.h>)
#endif // WEBSOCKET_CONSOLE_H