ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
gpio_map.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 gpio_map.h
6 * @brief Browser GPIO pin-mapper / diagnostics (PROTOCORE_ENABLE_GPIO_MAP).
7 *
8 * Exposes a compile-time table of GPIO pins (number, label, configured direction,
9 * live level) as JSON so a browser diag panel can show the pin map and toggle
10 * outputs. The live read and write go through protocore_platform_gpio_* where a pin seam
11 * exists; the JSON serializer and the control-POST parser are pure and host-tested.
12 * No allocation: the pin table is caller-owned and the JSON is written into a
13 * caller buffer.
14 *
15 * @author Douglas Quigg (dstroy0)
16 * @date 2026
17 */
18
19#ifndef PROTOCORE_GPIO_MAP_H
20#define PROTOCORE_GPIO_MAP_H
21
22#include "protocore_config.h" // the entry point: protocore_types.h for the widths
23
24#if PROTOCORE_ENABLE_GPIO_MAP
25
26EMBED_BEGIN_DECLS
27
28/**
29 * @brief Configured direction of a mapped pin (how the panel renders / drives it).
30 *
31 * The members carry the type's own name, `PROTOCORE_GPIO_DIR_`, and not a bare `PROTOCORE_GPIO_`. The board
32 * profiles spell their pin-mode argument `PROTOCORE_GPIO_IN` / `PROTOCORE_GPIO_OUT` as `#define`s, and a macro
33 * rewrites a token before the compiler sees a declaration at all (docs/SYMBOLS.md section 2), so a
34 * member sharing one of those names is replaced by its number wherever both headers reach one
35 * translation unit. The two encodings are also different numbers, which is what made the collision
36 * silent: the profile numbers a direction 0/1/2/3 as IN/OUT/PULLUP/PULLDOWN, this enum in
37 * declaration order.
38 */
39EMBED_ENUM_BYTE(protocore_gpio_dir,
40 PROTOCORE_GPIO_DIR_IN = 0, ///< read-only input.
41 PROTOCORE_GPIO_DIR_IN_PULLUP, ///< input with internal pull-up.
42 PROTOCORE_GPIO_DIR_IN_PULLDOWN, ///< input with internal pull-down.
43 PROTOCORE_GPIO_DIR_OUT, ///< output (drivable from the panel).
44);
45
46/** @brief One mapped GPIO pin. */
47typedef struct
48{
49 uint8_t pin; ///< GPIO number.
50 const char *label; ///< human label (null-terminated, caller-owned).
51 protocore_gpio_dir dir; ///< pin direction.
52 uint8_t level; ///< live level (0 / 1); filled by protocore_gpio_read.
53} protocore_gpio_pin;
54
55// ---------------------------------------------------------------------------
56// Host-testable core
57// ---------------------------------------------------------------------------
58
59/** @brief The pin table a call walks, and the pin it names. */
60typedef struct
61{
62 const protocore_gpio_pin *pins; ///< the table a call reads
63 protocore_gpio_pin *pins_rw; ///< the same table, where a sample writes levels back into it
64 uint8_t count; ///< how many pins it holds
65 uint8_t pin; ///< the pin a write or a lookup names
66 uint8_t level; ///< the level a write drives
67 protocore_gpio_dir dir; ///< the direction a name lookup names
68 const char *path; ///< the route the map is served on
69} GpioArgs;
70
71/** @brief The request body a set parses, and where its two fields land. */
72typedef struct
73{
74 const char *body; ///< the submitted body
75 size_t len; ///< its length
76 uint8_t *pin_out; ///< where the parsed pin lands
77 uint8_t *level_out; ///< where the parsed level lands
78} GpioParseArgs;
79
80/** @brief Where a report is written. */
81typedef struct
82{
83 char *out; ///< where the JSON lands
84 uint32_t cap; ///< how much room it has
85} GpioOutArgs;
86
87/**
88 * @brief The pin map and its HTTP surface.
89 *
90 * A caller sets the members a call takes, invokes it through ::GpioMap, and reads the outcome off
91 * the same handle. The pin table is the caller's.
92 *
93 * @var GpioMapNs::args the pin table a call walks, and the pin it names
94 * @var GpioMapNs::parse_args the request body a set parses, and where its fields land
95 * @var GpioMapNs::out_args where a report is written
96 * @var GpioMapNs::ok a call's true/false outcome
97 * @var GpioMapNs::text the direction name a lookup reports
98 * @var GpioMapNs::n bytes a report wrote, or < 0 when it did not fit
99 * @var GpioMapNs::dir_name the wire name for a direction
100 * @var GpioMapNs::json serialize the pin table
101 * @var GpioMapNs::parse_set parse a set request into its pin and level
102 * @var GpioMapNs::is_output whether the named pin is configured as an output
103 * @var GpioMapNs::begin_pins drive every pin in the table to its configured direction
104 * @var GpioMapNs::sample read every input pin's level back into the table
105 * @var GpioMapNs::write drive one output pin
106 * @var GpioMapNs::begin install the map's route and arm its pins
107 *
108 * No storage member: the pin table is the caller's and the pins themselves live in the part.
109 */
110typedef struct
111{
112 GpioArgs args;
113 GpioParseArgs parse_args;
114 GpioOutArgs out_args;
115 embed_bool ok;
116 const char *text;
117 int32_t n;
118} GpioMapVars;
119
120/** @brief The operands and the outcome. */
121extern GpioMapVars GpioMapV;
122
123/** @brief The entries. */
124typedef struct
125{
126 void (*const dir_name)(const uint8_t *work);
127 void (*const json)(const uint8_t *work);
128 void (*const parse_set)(const uint8_t *work);
129 void (*const is_output)(const uint8_t *work);
130 void (*const begin_pins)(const uint8_t *work);
131 void (*const sample)(const uint8_t *work);
132 void (*const write)(const uint8_t *work);
133 void (*const begin)(const uint8_t *work);
134} GpioMapNs;
135
136// What the table binds, defined once in the .c and taking one parameter each: everything
137// else an entry needs is an operand in GpioMapV or a region of the borrow at a fixed offset.
138void protocore_gpio_map_dir_name(const uint8_t *work);
139void protocore_gpio_map_json(const uint8_t *work);
140void protocore_gpio_map_parse_set(const uint8_t *work);
141void protocore_gpio_map_is_output(const uint8_t *work);
142void protocore_gpio_map_begin_pins(const uint8_t *work);
143void protocore_gpio_map_sample(const uint8_t *work);
144void protocore_gpio_map_write(const uint8_t *work);
145void protocore_gpio_map_begin(const uint8_t *work);
146
147// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
148// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
149// `GpioMap.dir_name(work)` resolves to a named function and becomes a DIRECT call. An extern table
150// leaves the call indirect and the symbol live at every level, -O2 -flto included.
151static const GpioMapNs GpioMap __attribute__((unused)) = {
152 .dir_name = protocore_gpio_map_dir_name,
153 .json = protocore_gpio_map_json,
154 .parse_set = protocore_gpio_map_parse_set,
155 .is_output = protocore_gpio_map_is_output,
156 .begin_pins = protocore_gpio_map_begin_pins,
157 .sample = protocore_gpio_map_sample,
158 .write = protocore_gpio_map_write,
159 .begin = protocore_gpio_map_begin,
160};
161
162EMBED_END_DECLS
163
164#endif // PROTOCORE_ENABLE_GPIO_MAP
165
166#endif // PROTOCORE_GPIO_MAP_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.