ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
gateway.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#ifndef PROTOCORE_GATEWAY_H
5#define PROTOCORE_GATEWAY_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
9EMBED_BEGIN_DECLS
10
11/**
12 * @file gateway.h
13 * @brief Radio / wireless gateway bridge (PROTOCORE_ENABLE_GATEWAY) - the v5 southbound-to-
14northbound bridge.
15 *
16 * The generic gateway pattern that ties the hardware-ingest pipeline to the web stack. A
17 * southbound radio (LoRa / nRF24 / CC1101 / Zigbee / Z-Wave / ... reached over SPI / I2C /
18 * UART) is a **port**. When it receives a frame - the data-ready ISR reads it over DMA
19 * (mmgr/dma), posts it onto the FORWARD lane (services/system/preempt_queue), and a per-radio
20 * codec extracts the source node address and payload - you call protocore_gateway_uplink(). The
21 * gateway **envelopes** the frame (source address, port, RSSI, a sequence number) and
22 * **publishes it northbound** through the uplink callback, which you wire to MQTT / HTTP /
23 * WebSocket / UDP. A northbound command runs the other way through protocore_gateway_downlink() to the
24 * port's transmit callback (the radio's SPI / UART write).
25 *
26 * The radio transmit and the northbound publish are **callbacks** - the seam a real radio
27 * driver and a real protocol binding plug into - so the bridge is fully host- and
28 * device-testable with no radio hardware (the tests / example supply capturing callbacks
29 * and feed simulated frames). This is the northbound half; the DMA + FORWARD lane carry the
30 * bytes, and each radio's frame format is its own codec.
31 *
32 * Per-port uplink rate cap (fail-closed), a routing-key helper (protocore_gateway_topic() formats
33 * `<prefix>/<port>/<addr>`), and static tables (zero heap): PROTOCORE_GW_MAX_PORTS ports.
34 *
35 * @c work is PROTOCORE_GATEWAY_BORROW bytes the CALLER took, at an address it knows. It is not held past the call, so
36nothing here aliases it. How those bytes are
37 * carved is this module's and is never named here.
38 *
39 * @author Douglas Quigg (dstroy0)
40 * @date 2026
41 */
42
43/** @brief Southbound radio / bus kind a port bridges (informational + topic hint). */
44EMBED_ENUM_BYTE(protocore_gateway_kind, PROTOCORE_GW_OTHER = 0, PROTOCORE_GW_LORA, PROTOCORE_GW_NRF24,
45 PROTOCORE_GW_CC1101, PROTOCORE_GW_THREAD, PROTOCORE_GW_ZIGBEE, PROTOCORE_GW_ZWAVE, PROTOCORE_GW_ENOCEAN,
46 PROTOCORE_GW_SIGFOX, PROTOCORE_GW_WISUN, PROTOCORE_GW_NFC, PROTOCORE_GW_BLE, );
47
48/**
49 * @brief A northbound message: a southbound frame enveloped with its routing metadata.
50 * @ref payload points at the caller's bytes and is valid only for the duration of
51 * the uplink callback - copy what you publish asynchronously.
52 */
53typedef struct
54{
55 const uint8_t *payload; ///< frame payload bytes
56 uint32_t seq; ///< per-gateway uplink sequence (wraps)
57 uint16_t len; ///< payload length
58 uint16_t src_addr; ///< source node address on the radio
59 int16_t rssi; ///< received signal strength (0 if the driver has none)
60 uint8_t port_id; ///< the port the frame arrived on
61 protocore_gateway_kind kind; ///< protocore_gateway_kind of that port
63
64/**
65 * @brief Northbound publish: emit @p msg to MQTT / HTTP / WebSocket / UDP.
66 * @return true if the northbound stack accepted it; false drops (counted).
67 */
68typedef embed_bool (*protocore_gateway_uplink_fn)(const protocore_gateway_msg *msg, void *ctx);
69
70/**
71 * @brief Southbound transmit (downlink): send @p payload to @p dst_addr on @p port_id.
72 * @return true if the radio accepted the frame; false drops (counted).
73 */
74typedef embed_bool (*protocore_gateway_tx_fn)(uint8_t port_id, uint16_t dst_addr, const uint8_t *payload, uint16_t len,
75 void *ctx);
76
77/** @brief Southbound port (radio / bus) configuration passed to protocore_gateway_add_port(). */
78typedef struct
79{
80 uint8_t port_id; ///< caller-assigned id (used in topics and up/down-link calls).
81 protocore_gateway_kind kind; ///< protocore_gateway_kind.
82 protocore_gateway_tx_fn tx; ///< downlink transmit (may be null for a receive-only port).
83 void *ctx; ///< opaque, forwarded to @ref tx.
84 uint16_t rate_cap; ///< max uplink frames/second from this port (0 = unlimited).
86
87/** @brief Gateway counters (monotonic since the last protocore_gateway_reset()). */
88typedef struct
89{
90 uint32_t up_in; ///< protocore_gateway_uplink() calls
91 uint32_t up_published; ///< frames the uplink callback accepted
92 uint32_t up_dropped; ///< uplinks dropped (rate cap / no sink / refused / bad port)
93 uint32_t down_in; ///< protocore_gateway_downlink() calls
94 uint32_t down_sent; ///< downlinks the port transmit accepted
95 uint32_t down_dropped; ///< downlinks dropped (bad port / no tx / refused)
97
98/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
99typedef struct
100{
101 void (*reset)(uint8_t *);
102 embed_bool (*add_port)(uint8_t *, const protocore_gateway_port_config *);
103 void (*set_uplink_cb)(uint8_t *, protocore_gateway_uplink_fn, void *);
104 void (*set_topic_prefix)(uint8_t *, const char *);
105 embed_bool (*uplink)(uint8_t *, uint8_t, uint16_t, const uint8_t *, uint16_t, int16_t);
106 embed_bool (*downlink)(uint8_t *, uint8_t, uint16_t, const uint8_t *, uint16_t);
107 uint16_t (*topic)(uint8_t *, const protocore_gateway_msg *, char *, uint16_t);
108 void (*get_stats)(uint8_t *, protocore_gateway_stats *);
109} GatewayNs;
110PROTOCORE_NS_LAYOUT(GatewayNs, reset, add_port, set_uplink_cb, set_topic_prefix, uplink, downlink, topic, get_stats);
111
112/**
113 * @brief Clear all ports, the uplink sink, the topic prefix, and stats.
114 * @param work PROTOCORE_GATEWAY_BORROW bytes the caller took. Not held past the call.
115 */
116void protocore_gateway_reset(uint8_t *work);
117/**
118 * @brief Register a southbound port.
119 * @param work PROTOCORE_GATEWAY_BORROW bytes the caller took. Not held past the call.
120 * @param cfg Cfg
121 * @return EMBED_TRUE on success.
122 */
123embed_bool protocore_gateway_add_port(uint8_t *work, const protocore_gateway_port_config *cfg);
124/**
125 * @brief Install the northbound publish callback (required to publish .
126 * @param work PROTOCORE_GATEWAY_BORROW bytes the caller took. Not held past the call.
127 * @param fn Fn
128 * @param ctx Ctx
129 */
131/**
132 * @brief Set the topic prefix used by protocore_gateway_topic() .
133 * @param work PROTOCORE_GATEWAY_BORROW bytes the caller took. Not held past the call.
134 * @param prefix Prefix
135 */
136void protocore_gateway_set_topic_prefix(uint8_t *work, const char *prefix);
137/**
138 * @brief Bridge a received southbound frame northbound: envelope it and .
139 * @param work PROTOCORE_GATEWAY_BORROW bytes the caller took. Not held past the call.
140 * @param port_id Port id
141 * @param src_addr Src addr
142 * @param payload Payload
143 * @param len Len
144 * @param rssi Rssi
145 * @return EMBED_TRUE on success.
146 */
147embed_bool protocore_gateway_uplink(uint8_t *work, uint8_t port_id, uint16_t src_addr, const uint8_t *payload,
148 uint16_t len, int16_t rssi);
149/**
150 * @brief Bridge a northbound command southbound: transmit it on port_id's .
151 * @param work PROTOCORE_GATEWAY_BORROW bytes the caller took. Not held past the call.
152 * @param port_id Port id
153 * @param dst_addr Dst addr
154 * @param payload Payload
155 * @param len Len
156 * @return EMBED_TRUE on success.
157 */
158embed_bool protocore_gateway_downlink(uint8_t *work, uint8_t port_id, uint16_t dst_addr, const uint8_t *payload,
159 uint16_t len);
160/**
161 * @brief Format a northbound routing key `<prefix>/<port>/<addr>` for msg .
162 * @param work PROTOCORE_GATEWAY_BORROW bytes the caller took. Not held past the call.
163 * @param msg Msg
164 * @param buf Buf
165 * @param buflen Buflen
166 * @return The uint16_t.
167 */
168uint16_t protocore_gateway_topic(uint8_t *work, const protocore_gateway_msg *msg, char *buf, uint16_t buflen);
169/**
170 * @brief Copy the current gateway counters into out. The uplink rate window .
171 * @param work PROTOCORE_GATEWAY_BORROW bytes the caller took. Not held past the call.
172 * @param out Out
173 */
175
176/**
177 * @brief Northbound publish: emit @p msg to MQTT / HTTP / WebSocket / UDP.
178 * @return true if the northbound stack accepted it; false drops (counted).
179 */
180typedef embed_bool (*protocore_gateway_uplink_fn)(const protocore_gateway_msg *msg, void *ctx);
181/**
182 * @brief The PROTOCORE_GATEWAY_BORROW bytes this module's state lives in.
183 *
184 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
185 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
186 * walks, so the state lasts the life of the program.
187 *
188 * @return the span.
189 */
191
192/** @brief Module namespace. */
201
202EMBED_END_DECLS
203
204#endif // PROTOCORE_GATEWAY_H
void protocore_gateway_get_stats(uint8_t *work, protocore_gateway_stats *out)
Copy the current gateway counters into out. The uplink rate window .
void protocore_gateway_set_topic_prefix(uint8_t *work, const char *prefix)
Set the topic prefix used by protocore_gateway_topic() .
void protocore_gateway_reset(uint8_t *work)
Clear all ports, the uplink sink, the topic prefix, and stats.
embed_bool protocore_gateway_uplink(uint8_t *work, uint8_t port_id, uint16_t src_addr, const uint8_t *payload, uint16_t len, int16_t rssi)
Bridge a received southbound frame northbound: envelope it and .
uint8_t * protocore_gateway_span(void)
The PROTOCORE_GATEWAY_BORROW bytes this module's state lives in.
embed_bool protocore_gateway_downlink(uint8_t *work, uint8_t port_id, uint16_t dst_addr, const uint8_t *payload, uint16_t len)
Bridge a northbound command southbound: transmit it on port_id's .
embed_bool protocore_gateway_add_port(uint8_t *work, const protocore_gateway_port_config *cfg)
Register a southbound port.
uint16_t protocore_gateway_topic(uint8_t *work, const protocore_gateway_msg *msg, char *buf, uint16_t buflen)
Format a northbound routing key <prefix>/<port>/<addr> for msg .
embed_bool(* protocore_gateway_tx_fn)(uint8_t port_id, uint16_t dst_addr, const uint8_t *payload, uint16_t len, void *ctx)
Southbound transmit (downlink): send payload to dst_addr on port_id.
Definition gateway.h:74
embed_bool(* protocore_gateway_uplink_fn)(const protocore_gateway_msg *msg, void *ctx)
Northbound publish: emit msg to MQTT / HTTP / WebSocket / UDP.
Definition gateway.h:68
void protocore_gateway_set_uplink_cb(uint8_t *work, protocore_gateway_uplink_fn fn, void *ctx)
Install the northbound publish callback (required to publish .
EMBED_ENUM_BYTE(protocore_gateway_kind, PROTOCORE_GW_OTHER=0, PROTOCORE_GW_LORA, PROTOCORE_GW_NRF24, PROTOCORE_GW_CC1101, PROTOCORE_GW_THREAD, PROTOCORE_GW_ZIGBEE, PROTOCORE_GW_ZWAVE, PROTOCORE_GW_ENOCEAN, PROTOCORE_GW_SIGFOX, PROTOCORE_GW_WISUN, PROTOCORE_GW_NFC, PROTOCORE_GW_BLE,)
Southbound radio / bus kind a port bridges (informational + topic hint).
#define PROTOCORE_UNUSED
Suppress the unused warning. Every module ends in a namespace most callers only partly use.
Definition ns_contract.h:36
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
Dispatch table. Addressed by offset, so the layout is asserted below.
Definition gateway.h:100
void(* set_topic_prefix)(uint8_t *, const char *)
Definition gateway.h:104
embed_bool(* uplink)(uint8_t *, uint8_t, uint16_t, const uint8_t *, uint16_t, int16_t)
Definition gateway.h:105
uint16_t(* topic)(uint8_t *, const protocore_gateway_msg *, char *, uint16_t)
Definition gateway.h:107
void(* get_stats)(uint8_t *, protocore_gateway_stats *)
Definition gateway.h:108
embed_bool(* add_port)(uint8_t *, const protocore_gateway_port_config *)
Definition gateway.h:102
void(* set_uplink_cb)(uint8_t *, protocore_gateway_uplink_fn, void *)
Definition gateway.h:103
void(* reset)(uint8_t *)
Definition gateway.h:101
embed_bool(* downlink)(uint8_t *, uint8_t, uint16_t, const uint8_t *, uint16_t)
Definition gateway.h:106
A northbound message: a southbound frame enveloped with its routing metadata. payload points at the c...
Definition gateway.h:54
uint8_t port_id
the port the frame arrived on
Definition gateway.h:60
protocore_gateway_kind kind
protocore_gateway_kind of that port
Definition gateway.h:61
uint16_t len
payload length
Definition gateway.h:57
uint16_t src_addr
source node address on the radio
Definition gateway.h:58
const uint8_t * payload
frame payload bytes
Definition gateway.h:55
int16_t rssi
received signal strength (0 if the driver has none)
Definition gateway.h:59
uint32_t seq
per-gateway uplink sequence (wraps)
Definition gateway.h:56
Southbound port (radio / bus) configuration passed to protocore_gateway_add_port().
Definition gateway.h:79
protocore_gateway_kind kind
protocore_gateway_kind.
Definition gateway.h:81
void * ctx
opaque, forwarded to tx.
Definition gateway.h:83
protocore_gateway_tx_fn tx
downlink transmit (may be null for a receive-only port).
Definition gateway.h:82
uint8_t port_id
caller-assigned id (used in topics and up/down-link calls).
Definition gateway.h:80
uint16_t rate_cap
max uplink frames/second from this port (0 = unlimited).
Definition gateway.h:84
Gateway counters (monotonic since the last protocore_gateway_reset()).
Definition gateway.h:89
uint32_t down_dropped
downlinks dropped (bad port / no tx / refused)
Definition gateway.h:95
uint32_t up_in
protocore_gateway_uplink() calls
Definition gateway.h:90
uint32_t up_dropped
uplinks dropped (rate cap / no sink / refused / bad port)
Definition gateway.h:92
uint32_t down_in
protocore_gateway_downlink() calls
Definition gateway.h:93
uint32_t down_sent
downlinks the port transmit accepted
Definition gateway.h:94
uint32_t up_published
frames the uplink callback accepted
Definition gateway.h:91