ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
dashboard.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file dashboard.h
6 * @brief Real-time SVG telemetry dashboard (PROTOCORE_ENABLE_DASHBOARD).
7 *
8 * Widgets are declared once in a fixed compile-time protocore_widget table - no heap,
9 * fixed at link. protocore_dashboard_begin() serves three things at @p path:
10 * - GET path the self-contained SVG dashboard page (from web);
11 * - GET path/layout the widget table serialized as a JSON array;
12 * - SSE path/stream a live stream of the current values.
13 * The page fetches the layout, renders one SVG widget per entry, and updates them
14 * from the SSE value stream. The application feeds readings with
15 * protocore_dashboard_set(key, value) and pushes them with protocore_dashboard_publish().
16 *
17 * The widget-table -> JSON serializers (layout + values) are pure and have no
18 * server dependency, so they unit-test on the host. Requires PROTOCORE_ENABLE_SSE.
19 *
20 * @author Douglas Quigg (dstroy0)
21 * @date 2026
22 */
23
24#ifndef PROTOCORE_DASHBOARD_H
25#define PROTOCORE_DASHBOARD_H
26
27#include "protocore_config.h" // the entry point: protocore_types.h for the widths
28
29#if PROTOCORE_ENABLE_DASHBOARD
30
31EMBED_BEGIN_DECLS
32
33// PROTOCORE_DASHBOARD_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
34// it into its arena. A caller takes them once and passes the pointer to every call. How they
35// are carved is this module's and is never named here.
36
37/** @brief Widget rendering / interaction style. */
38EMBED_ENUM_BYTE(protocore_widget_type,
39 // Display widgets - updated from the SSE value stream.
40 PROTOCORE_WIDGET_VALUE = 0, ///< plain numeric readout
41 PROTOCORE_WIDGET_GAUGE, ///< radial arc gauge over [min, max]
42 PROTOCORE_WIDGET_BAR, ///< horizontal bar over [min, max]
43 PROTOCORE_WIDGET_SPARKLINE, ///< recent-history SVG line over [min, max]
44 PROTOCORE_WIDGET_CHART, ///< dense Canvas line chart over [min, max]
45 // Control widgets - send values back to the device over WebSocket.
46 PROTOCORE_WIDGET_BUTTON, ///< momentary button -> control value 1
47 PROTOCORE_WIDGET_TOGGLE, ///< on/off toggle -> control value 0/1 (reflects SSE state)
48 PROTOCORE_WIDGET_SLIDER ///< range slider over [min, max] -> control value
49);
50
51/** @brief Control callback: invoked when a control widget sends a value over WebSocket. */
52typedef void (*protocore_control_cb)(const char *key, float value);
53
54/** @brief One dashboard widget, declared in a fixed compile-time table. */
55typedef struct
56{
57 protocore_widget_type type; ///< rendering style.
58 const char *label; ///< display label.
59 const char *key; ///< telemetry source key (matches protocore_dashboard_set()).
60 float min; ///< scale minimum (gauge / bar / sparkline).
61 float max; ///< scale maximum.
62 const char *unit; ///< unit suffix shown by the widget (may be "").
63} protocore_widget;
64
65/** @brief What configure takes: widgets, count. */
66typedef struct
67{
68 const protocore_widget *widgets;
69 uint8_t count;
70} DashboardConfigureArgs;
71
72/** @brief What set takes: key, value. */
73typedef struct
74{
75 const char *key;
76 float value;
77} DashboardSetArgs;
78
79/** @brief What layout_json takes: out, cap. */
80typedef struct
81{
82 char *out;
83 uint32_t cap;
84} DashboardLayoutJsonArgs;
85
86/** @brief What values_json takes: out, cap. */
87typedef struct
88{
89 char *out;
90 uint32_t cap;
91} DashboardValuesJsonArgs;
92
93/** @brief What on_control takes: cb. */
94typedef struct
95{
96 protocore_control_cb cb;
97} DashboardOnControlArgs;
98
99/** @brief What parse_control takes: msg, key_out, key_cap, value_out. */
100typedef struct
101{
102 const char *msg;
103 char *key_out;
104 size_t key_cap;
105 float *value_out;
106} DashboardParseControlArgs;
107
108/** @brief What dispatch_control takes: msg. */
109typedef struct
110{
111 const char *msg;
112} DashboardDispatchControlArgs;
113
114/** @brief What begin takes: path, widgets, count. */
115typedef struct
116{
117 const char *path;
118 const protocore_widget *widgets;
119 uint8_t count;
120} DashboardBeginArgs;
121
122/**
123 * @brief Real-time SVG telemetry dashboard (PROTOCORE_ENABLE_DASHBOARD).
124 *
125 * A caller sets the members a call takes, invokes it through ::Dashboard with the bytes it runs
126 * out of, and reads the outcome off the same handle.
127 *
128 * Dashboard.configure_args.widgets = ...;
129 * Dashboard.configure_args.count = ...;
130 * Dashboard.configure(work);
131 *
132 * @var DashboardNs::configure_args what configure takes: widgets, count
133 * @var DashboardNs::set_args what set takes: key, value
134 * @var DashboardNs::layout_json_args what layout_json takes: out, cap
135 * @var DashboardNs::values_json_args what values_json takes: out, cap
136 * @var DashboardNs::on_control_args what on_control takes: cb
137 * @var DashboardNs::parse_control_args what parse_control takes: msg, key_out, key_cap, value_out
138 * @var DashboardNs::dispatch_control_args what dispatch_control takes: msg
139 * @var DashboardNs::begin_args what begin takes: path, widgets, count
140 * @var DashboardNs::ok true if well-formed; writes the key (bounded by key_cap) and value
141 * @var DashboardNs::value number of characters written, or 0 if cap is too small
142 * @var DashboardNs::configure bind the widget table and reset every value to 0
143 * @var DashboardNs::set set a widget's current value by key. false if the key is unknown
144 * @var DashboardNs::layout_json serialize the widget layout as a JSON array into out
145 * @var DashboardNs::values_json serialize the current values as a JSON object {key:value,...} into ...
146 * @var DashboardNs::on_control register the callback invoked when a control widget sends a value
147 * @var DashboardNs::parse_control parse a control message `{"k":"<key>","v":<number>}` from the page
148 * @var DashboardNs::dispatch_control parse a control message and invoke the registered control callback
149 * @var DashboardNs::begin serve the dashboard at path (page, layout JSON, and SSE value ...
150 * @var DashboardNs::publish broadcast the current values to all SSE subscribers (after ...
151 *
152 * @c work is PROTOCORE_DASHBOARD_BORROW bytes the CALLER took, at an address it knows. It is not held past the call, so
153 * nothing here aliases it. How those bytes are carved is this module's and is never named here.
154 */
155typedef struct
156{
157 DashboardConfigureArgs configure_args;
158 DashboardSetArgs set_args;
159 DashboardLayoutJsonArgs layout_json_args;
160 DashboardValuesJsonArgs values_json_args;
161 DashboardOnControlArgs on_control_args;
162 DashboardParseControlArgs parse_control_args;
163 DashboardDispatchControlArgs dispatch_control_args;
164 DashboardBeginArgs begin_args;
165 embed_bool ok;
166 int32_t value;
167} DashboardVars;
168
169/** @brief The operands and the outcome. */
170extern DashboardVars DashboardV;
171
172/** @brief The entries. */
173typedef struct
174{
175 void (*const configure)(uint8_t *work);
176 void (*const set)(uint8_t *work);
177 void (*const layout_json)(uint8_t *work);
178 void (*const values_json)(uint8_t *work);
179 void (*const on_control)(uint8_t *work);
180 void (*const parse_control)(const uint8_t *work);
181 void (*const dispatch_control)(uint8_t *work);
182 void (*const begin)(uint8_t *work);
183 void (*const publish)(uint8_t *work);
184} DashboardNs;
185
186// What the table binds, defined once in the .c and taking one parameter each: everything
187// else an entry needs is an operand in DashboardV or a region of the borrow at a fixed offset.
188void protocore_dashboard_configure(uint8_t *work);
189void protocore_dashboard_set(uint8_t *work);
190void protocore_dashboard_layout_json(uint8_t *work);
191void protocore_dashboard_values_json(uint8_t *work);
192void protocore_dashboard_on_control(uint8_t *work);
193void protocore_dashboard_parse_control(const uint8_t *work);
194void protocore_dashboard_dispatch_control(uint8_t *work);
195void protocore_dashboard_begin(uint8_t *work);
196void protocore_dashboard_publish(uint8_t *work);
197
198// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
199// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
200// `Dashboard.configure(work)` resolves to a named function and becomes a DIRECT call. An extern table
201// leaves the call indirect and the symbol live at every level, -O2 -flto included.
202static const DashboardNs Dashboard __attribute__((unused)) = {
203 .configure = protocore_dashboard_configure,
204 .set = protocore_dashboard_set,
205 .layout_json = protocore_dashboard_layout_json,
206 .values_json = protocore_dashboard_values_json,
207 .on_control = protocore_dashboard_on_control,
208 .parse_control = protocore_dashboard_parse_control,
209 .dispatch_control = protocore_dashboard_dispatch_control,
210 .begin = protocore_dashboard_begin,
211 .publish = protocore_dashboard_publish,
212};
213
214/**
215 * @brief The PROTOCORE_DASHBOARD_BORROW bytes this module's state lives in.
216 *
217 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
218 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
219 * walks, so the state lasts the life of the program.
220 *
221 * @return the span.
222 */
223uint8_t *protocore_dashboard_span(void);
224
225EMBED_END_DECLS
226
227#endif // PROTOCORE_ENABLE_DASHBOARD
228
229#endif // PROTOCORE_DASHBOARD_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.