Add inline documentation for all src/ subsystems covering purpose, responsibilities, public API, and usage notes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
3.4 KiB
UARTTransport
Summary
UARTTransport wraps UARTDriver and connects it to the CommandBus for the UART slave interface (LCD panel / button board on Serial2). Unlike other transports, UART requires polling — loop() must be called each iteration of the Arduino main loop. A command whitelist is enforced before dispatch: only five commands are allowed from the UART slave. All other commands are silently dropped. This prevents a compromised or misbehaving slave from issuing sensitive commands (factory reset, OTA update, etc.). Replies are sent back over UART as newline-delimited JSON.
Public API
| Method | Description |
|---|---|
UARTTransport(UARTDriver& uart, CommandBus& bus) |
Constructor. |
void begin() |
Wires the message callback onto UARTDriver, then calls UARTDriver::begin() to initialise Serial2. |
void loop() |
Calls UARTDriver::loop() to poll for incoming bytes. Must be called from the Arduino main loop. |
bool isHealthy() const |
Returns UARTDriver::isReady(). |
String name() const |
Returns "UART". |
void broadcast(const String& msg) |
Sends a JSON string to the UART slave (newline appended by UARTDriver::send()). |
Command Whitelist
Only the following commands are accepted from the UART slave:
| Command | Purpose |
|---|---|
ping |
Connection check |
playback |
Play / stop from physical buttons |
clock.pause |
Button-triggered clock pause |
clock.resume |
Button-triggered clock resume |
system.get_time |
LCD requests current time (pull model, replaces old push) |
Any command not in this list is dropped with a LOG_WARNING and the client receives no response.
Initialization
CommunicationRouter::begin() calls _uartTransport.begin(), then adds the transport to the bus. CommunicationRouter::loop() calls _uartTransport.loop(), which is the only mandatory polling call in the system.
Wire Protocol
Messages are newline-delimited JSON on Serial2 (TX: GPIO 12, RX: GPIO 13, 115200 baud):
{ "v": 2, "cmd": "playback", "contents": { ... } }\n
Responses are sent back immediately after dispatch:
{ "status": "SUCCESS", "type": "playback", "message": "Playback command executed" }\n
JSON parse errors on the slave side generate an error response:
{ "status": "ERROR", "type": "parse_error", "payload": "<ArduinoJson error string>" }\n
Dependencies
UARTDriver— Serial2 I/O, line buffering, JSON parsingCommandBus— dispatchArduinoJson,Logging
Gotchas / Notes
loop()must be called. Without it, no UART messages are processed.CommunicationRouter::loop()handles this for you.- Whitelist is enforced before the bus. Dropped commands produce a
LOG_WARNINGbut no response to the slave. If the slave is polling for a response that never arrives, it will time out. Design slave firmware accordingly. - Buffer overflow drops the message.
UARTDriverhas a 1024-byte receive buffer. Messages longer than this are discarded and_errorCountis incremented. - Blocking flush.
UARTDriver::send()callsSerial.flush()after each response, which blocks until the UART TX buffer drains. At 115200 baud this is typically under 1 ms for short responses. - No v1 translation for UART. In practice the UART slave sends v2 commands, but
CommandBus::dispatch()will still translate v1 envelopes if"v": 1is set.