ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
iface_bridge.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 iface_bridge.h
6 * @brief User-defined address:port -> hardware-bus translation (PROTOCORE_ENABLE_IFACE_BRIDGE).
7 *
8 * A configurable "device server": the application registers rules mapping a listen address:port (plus
9 * TCP/UDP) to a hardware endpoint - a UART, an SPI chip-select, or an I2C address - so a network client
10 * talking to `x.x.x.x:nnnn` is transparently bridged to that bus. Two payload models:
11 *
12 * - STREAM (UART): raw bidirectional passthrough. Socket bytes are written to the UART and UART bytes
13 * flow back to the socket, with no framing (a classic serial-device server / ser2net).
14 * - TRANSACTION (SPI / I2C, also usable for UART): the socket carries framed write-then-read
15 * transactions, which is what master-initiated buses need. Each request frame is
16 * uint16 write_len (big-endian) || uint16 read_len (big-endian) || write_bytes[write_len]
17 * and the reply is the read_len bytes clocked/read back. The bus address (I2C 7-bit addr) or
18 * chip-select (SPI CS gpio) + clock/mode come from the rule's target, so the frame stays generic.
19 *
20 * This header is the pure, host-tested core: the fixed-capacity rule table (zero heap) and the
21 * transaction frame codec. The actual bus I/O (uart.h / spi.h / i2c.h) and the PROTO_BRIDGE listener are
22 * the bus step (iface_bridge_hw.*), kept separate exactly like the peripheral services.
23 *
24 * @author Douglas Quigg (dstroy0)
25 * @date 2026
26 */
27
28#ifndef PROTOCORE_IFACE_BRIDGE_H
29#define PROTOCORE_IFACE_BRIDGE_H
30
31#include "protocore_config.h" // the entry point: protocore_types.h for the widths
32
33#if PROTOCORE_ENABLE_IFACE_BRIDGE
34
35#include "shared/ip/ip.h" // the complete type a public struct below holds by value
36
37EMBED_BEGIN_DECLS
38
39// PROTOCORE_IFACE_BRIDGE_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
40// it into its arena. A caller takes them once and passes the pointer to every call. How they
41// are carved is this module's and is never named here.
42
43#define PROTOCORE_BRIDGE_TXN_HDR 4 ///< transaction frame header: write_len(2) + read_len(2), big-endian
44
45EMBED_ENUM_BYTE(BridgeBus, BRIDGE_BUS_UART = 0, BRIDGE_BUS_SPI = 1, BRIDGE_BUS_I2C = 2);
46
47EMBED_ENUM_BYTE(BridgeMode,
48 BRIDGE_MODE_STREAM = 0, ///< raw bidirectional passthrough (UART)
49 BRIDGE_MODE_TRANSACTION = 1 ///< framed write-then-read (SPI / I2C; also usable for UART)
50);
51
52EMBED_ENUM_BYTE(BridgeProto, BRIDGE_PROTO_TCP = 0, BRIDGE_PROTO_UDP = 1);
53
54typedef struct BridgeTarget
55{
56 BridgeBus bus;
57 BridgeMode mode;
58 uint8_t unit; ///< UART port # / SPI host # / I2C bus #
59 uint16_t addr_cs; ///< I2C 7-bit address, or SPI chip-select GPIO
60 uint32_t rate; ///< UART baud, or SPI/I2C clock (Hz)
61 uint8_t spi_mode; ///< SPI mode 0..3 (SPI only)
62 uint8_t bit_order; ///< 0 = MSB-first, 1 = LSB-first (SPI only)
63} BridgeTarget;
64
65typedef struct
66{
67 protocore_ip listen_ip; ///< bind address (x.x.x.x / [v6]); family PROTOCORE_IP_NONE = any interface
68 uint16_t listen_port; ///< nnnn
69 BridgeProto proto; ///< TCP or UDP
70 BridgeTarget target;
71 embed_bool used;
72} BridgeRule;
73
74/** @brief What add takes: rule. */
75typedef struct
76{
77 const BridgeRule *rule;
78} IfaceBridgeAddArgs;
79
80/** @brief What map takes: ip, port, proto, target. */
81typedef struct
82{
83 const char *ip;
84 uint16_t port;
85 BridgeProto proto;
86 const BridgeTarget *target;
87} IfaceBridgeMapArgs;
88
89/** @brief What find takes: port, proto. */
90typedef struct
91{
92 uint16_t port;
93 BridgeProto proto;
94} IfaceBridgeFindArgs;
95
96/** @brief What txn_parse takes: buf, len, write_len, read_len, ... */
97typedef struct
98{
99 const uint8_t *buf;
100 size_t len;
101 uint16_t *write_len;
102 uint16_t *read_len;
103 const uint8_t **write_data;
104} IfaceBridgeTxnParseArgs;
105
106/** @brief What txn_build takes: out, cap, write_data, write_len, ... */
107typedef struct
108{
109 uint8_t *out;
110 size_t cap;
111 const uint8_t *write_data;
112 uint16_t write_len;
113 uint16_t read_len;
114} IfaceBridgeTxnBuildArgs;
115
116/**
117 * @brief User-defined address:port -> hardware-bus translation (PROTOCORE_ENABLE_IFACE_BRIDGE). A configurable "device
118 * ...
119 *
120 * A caller sets the members a call takes, invokes it through ::IfaceBridge with the bytes it runs
121 * out of, and reads the outcome off the same handle.
122 *
123 * IfaceBridge.clear(work);
124 *
125 * @var IfaceBridgeNs::add_args what add takes: rule
126 * @var IfaceBridgeNs::map_args what map takes: ip, port, proto, target
127 * @var IfaceBridgeNs::find_args what find takes: port, proto
128 * @var IfaceBridgeNs::txn_parse_args what txn_parse takes: buf, len, write_len, read_len,
129 * @var IfaceBridgeNs::txn_build_args what txn_build takes: out, cap, write_data, write_len,
130 * @var IfaceBridgeNs::ok a call's true/false outcome
131 * @var IfaceBridgeNs::rule what a call reports
132 * @var IfaceBridgeNs::u8 what a call reports
133 * @var IfaceBridgeNs::n bytes written, or 0 if out is too small
134 * @var IfaceBridgeNs::clear clear
135 * @var IfaceBridgeNs::add add
136 * @var IfaceBridgeNs::map map
137 * @var IfaceBridgeNs::find find
138 * @var IfaceBridgeNs::count count
139 * @var IfaceBridgeNs::txn_parse parse a transaction request from a socket buffer. On a complete ...
140 * @var IfaceBridgeNs::txn_build build a transaction request frame (header + write payload) into out
141 *
142 * @c work is PROTOCORE_IFACE_BRIDGE_BORROW bytes the CALLER took, at an address it knows. It is not held past the call,
143 * so nothing here aliases it. How those bytes are carved is this module's and is never named here.
144 */
145typedef struct
146{
147 IfaceBridgeAddArgs add_args;
148 IfaceBridgeMapArgs map_args;
149 IfaceBridgeFindArgs find_args;
150 IfaceBridgeTxnParseArgs txn_parse_args;
151 IfaceBridgeTxnBuildArgs txn_build_args;
152 embed_bool ok;
153 const BridgeRule *rule;
154 uint8_t u8;
155 size_t n;
156} IfaceBridgeVars;
157
158/** @brief The operands and the outcome. */
159extern IfaceBridgeVars IfaceBridgeV;
160
161/** @brief The entries. */
162typedef struct
163{
164 void (*const clear)(uint8_t *work);
165 void (*const add)(uint8_t *work);
166 void (*const map)(uint8_t *work);
167 void (*const find)(uint8_t *work);
168 void (*const count)(uint8_t *work);
169 void (*const txn_parse)(const uint8_t *work);
170 void (*const txn_build)(const uint8_t *work);
171} IfaceBridgeNs;
172
173// What the table binds, defined once in the .c and taking one parameter each: everything
174// else an entry needs is an operand in IfaceBridgeV or a region of the borrow at a fixed offset.
175void protocore_iface_bridge_clear(uint8_t *work);
176void protocore_iface_bridge_add(uint8_t *work);
177void protocore_iface_bridge_map(uint8_t *work);
178void protocore_iface_bridge_find(uint8_t *work);
179void protocore_iface_bridge_count(uint8_t *work);
180void protocore_iface_bridge_txn_parse(const uint8_t *work);
181void protocore_iface_bridge_txn_build(const uint8_t *work);
182
183// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
184// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
185// `IfaceBridge.clear(work)` resolves to a named function and becomes a DIRECT call. An extern table
186// leaves the call indirect and the symbol live at every level, -O2 -flto included.
187static const IfaceBridgeNs IfaceBridge __attribute__((unused)) = {
188 .clear = protocore_iface_bridge_clear,
189 .add = protocore_iface_bridge_add,
190 .map = protocore_iface_bridge_map,
191 .find = protocore_iface_bridge_find,
192 .count = protocore_iface_bridge_count,
193 .txn_parse = protocore_iface_bridge_txn_parse,
194 .txn_build = protocore_iface_bridge_txn_build,
195};
196
197/**
198 * @brief The PROTOCORE_IFACE_BRIDGE_BORROW bytes this module's state lives in.
199 *
200 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
201 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
202 * walks, so the state lasts the life of the program.
203 *
204 * @return the span.
205 */
206uint8_t *protocore_iface_bridge_span(void);
207
208EMBED_END_DECLS
209
210#endif // PROTOCORE_ENABLE_IFACE_BRIDGE
211
212#endif // PROTOCORE_IFACE_BRIDGE_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.
Layer 3 (Network) - a family-tagged IP address (IPv4 or IPv6) with RFC-faithful text parsing,...
A v4 or v6 address in network (big-endian) byte order.
Definition ip.h:54