ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
sparkplug.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 sparkplug.h
6 * @brief Sparkplug B topic and payload codec (PROTOCORE_ENABLE_SPARKPLUG) - a zero-heap builder and
7 * reader for the Sparkplug B topic namespace and its Google Protocol Buffers payload.
8 *
9 * **The governing standard is not IETF.** Sparkplug is an Eclipse Foundation specification:
10 * Sparkplug 3.0.0, Eclipse Sparkplug Contributors. It rides MQTT, which is an OASIS standard -
11 * Sparkplug 3.0.0 sec 1.5 (Normative References) names MQTT Version 3.1.1 Plus Errata 01, OASIS
12 * Standard Incorporating Approved Errata 01, and MQTT Version 5.0, OASIS Standard. No RFC governs
13 * either one, and none is cited here.
14 *
15 * Sparkplug 3.0.0 sec 4.1 gives the topic:
16 *
17 * namespace/group_id/message_type/edge_node_id/[device_id]
18 *
19 * The namespace element is the UTF-8 constant `spBv1.0` (sec 4.1.1). group_id (sec 4.1.2),
20 * edge_node_id (sec 4.1.4) and device_id (sec 4.1.5) are UTF-8 excluding the reserved `+`, `/` and
21 * `#`. sec 4.1.3 defines nine message_type values. sec 4.1.5 states device_id MUST be included with
22 * DBIRTH, DDEATH, DDATA and DCMD, and MUST NOT be included with NBIRTH, NDEATH, NDATA, NCMD and
23 * STATE. The separator between elements is the MQTT topic level separator (MQTT Version 5.0
24 * sec 4.7.1.1).
25 *
26 * Sparkplug 3.0.0 sec 6.4.1 (Google Protocol Buffer Schema) gives the payload. Payload carries
27 * timestamp(1), metrics(2, repeated), seq(3), uuid(4) and body(5). Each Metric carries name(1),
28 * alias(2), timestamp(3), datatype(4), is_historical(5), is_transient(6), is_null(7), metadata(8),
29 * properties(9), and one member of the value oneof - int_value(10), long_value(11), float_value(12),
30 * double_value(13), boolean_value(14), string_value(15), bytes_value(16), dataset_value(17),
31 * template_value(18), extension_value(19).
32 *
33 * This codec covers name, alias, timestamp, datatype and the first six value members. is_historical,
34 * is_transient, is_null, metadata, properties, bytes_value, dataset_value, template_value and
35 * extension_value are neither written nor reported.
36 *
37 * Built on the Protocol Buffers codec (services/iot/protobuf) and published with the MQTT client.
38 *
39 * The module exports one symbol, @ref Sparkplug. Everything in sparkplug.c has internal linkage.
40 *
41 * @author Douglas Quigg (dstroy0)
42 * @date 2026
43 */
44
45#ifndef PROTOCORE_SPARKPLUG_H
46#define PROTOCORE_SPARKPLUG_H
47
48#include "protocore_config.h" // the entry point: protocore_types.h for the widths
49
50#if PROTOCORE_ENABLE_SPARKPLUG
51
52EMBED_BEGIN_DECLS
53
54/** @brief Sparkplug 3.0.0 sec 4.1.1: the namespace element for the Sparkplug B payload definition. */
55#define SPB_NAMESPACE "spBv1.0"
56
57// Sparkplug 3.0.0 sec 4.1.3 message_type elements.
58#define SPB_MSG_NBIRTH "NBIRTH" ///< birth certificate for Sparkplug Edge Nodes
59#define SPB_MSG_NDEATH "NDEATH" ///< death certificate for Sparkplug Edge Nodes
60#define SPB_MSG_DBIRTH "DBIRTH" ///< birth certificate for Devices
61#define SPB_MSG_DDEATH "DDEATH" ///< death certificate for Devices
62#define SPB_MSG_NDATA "NDATA" ///< Edge Node data message
63#define SPB_MSG_DDATA "DDATA" ///< Device data message
64#define SPB_MSG_NCMD "NCMD" ///< Edge Node command message
65#define SPB_MSG_DCMD "DCMD" ///< Device command message
66#define SPB_MSG_STATE "STATE" ///< Sparkplug Host Application state message
67
68// Sparkplug 3.0.0 sec 6.4.16 DataType, the codes Metric.datatype takes.
69#define SPB_DT_UNKNOWN 0
70#define SPB_DT_INT8 1
71#define SPB_DT_INT16 2
72#define SPB_DT_INT32 3
73#define SPB_DT_INT64 4
74#define SPB_DT_UINT8 5
75#define SPB_DT_UINT16 6
76#define SPB_DT_UINT32 7
77#define SPB_DT_UINT64 8
78#define SPB_DT_FLOAT 9
79#define SPB_DT_DOUBLE 10
80#define SPB_DT_BOOLEAN 11
81#define SPB_DT_STRING 12
82#define SPB_DT_DATETIME 13
83#define SPB_DT_TEXT 14
84#define SPB_DT_UUID 15
85#define SPB_DT_DATASET 16
86#define SPB_DT_BYTES 17
87#define SPB_DT_FILE 18
88#define SPB_DT_TEMPLATE 19
89#define SPB_DT_PROPERTYSET 20
90#define SPB_DT_PROPERTYSETLIST 21
91#define SPB_DT_INT8_ARRAY 22
92#define SPB_DT_INT16_ARRAY 23
93#define SPB_DT_INT32_ARRAY 24
94#define SPB_DT_INT64_ARRAY 25
95#define SPB_DT_UINT8_ARRAY 26
96#define SPB_DT_UINT16_ARRAY 27
97#define SPB_DT_UINT32_ARRAY 28
98#define SPB_DT_UINT64_ARRAY 29
99#define SPB_DT_FLOAT_ARRAY 30
100#define SPB_DT_DOUBLE_ARRAY 31
101#define SPB_DT_BOOLEAN_ARRAY 32
102#define SPB_DT_STRING_ARRAY 33
103#define SPB_DT_DATETIME_ARRAY 34
104
105/** @brief Which member of the Metric value oneof carries the value (Sparkplug 3.0.0 sec 6.4.1). */
106EMBED_ENUM_BYTE(SpbMetricKind,
107 SPB_M_INT, ///< int_value, field 10, uint32
108 SPB_M_LONG, ///< long_value, field 11, uint64
109 SPB_M_FLOAT, ///< float_value, field 12
110 SPB_M_DOUBLE, ///< double_value, field 13
111 SPB_M_BOOL, ///< boolean_value, field 14
112 SPB_M_STRING, ///< string_value, field 15
113);
114
115/** @brief One Metric to encode (Sparkplug 3.0.0 sec 6.4.6). A null name and a false has_* omit the field. */
116typedef struct
117{
118 const char *name; ///< name, field 1; omit on a DATA metric addressed by alias
119 embed_bool has_alias;
120 uint64_t alias; ///< alias, field 2
121 embed_bool has_timestamp;
122 uint64_t timestamp; ///< timestamp, field 3, milliseconds since epoch in UTC
123 uint32_t datatype; ///< datatype, field 4, an SPB_DT_* code
124 SpbMetricKind kind; ///< which value oneof member below is written
125 uint32_t int_value; ///< int_value, field 10
126 uint64_t long_value; ///< long_value, field 11
127 float float_value; ///< float_value, field 12
128 double double_value; ///< double_value, field 13
129 embed_bool bool_value; ///< boolean_value, field 14
130 const char *string_value; ///< string_value, field 15
131} SpbMetric;
132
133/** @brief A decoded Payload's top-level fields (Sparkplug 3.0.0 sec 6.4.5); metrics are iterated separately. */
134typedef struct
135{
136 embed_bool has_timestamp;
137 uint64_t timestamp; ///< timestamp, field 1, milliseconds since epoch in UTC
138 embed_bool has_seq;
139 uint64_t seq; ///< seq, field 3
140} SpbPayloadHeader;
141
142/** @brief A decoded Metric (Sparkplug 3.0.0 sec 6.4.6). name and string_value point INTO the source, un-terminated. */
143typedef struct
144{
145 const char *name; ///< name, field 1, or nullptr when the metric is addressed by alias
146 size_t name_len;
147 embed_bool has_alias;
148 uint64_t alias; ///< alias, field 2
149 embed_bool has_timestamp;
150 uint64_t timestamp; ///< timestamp, field 3
151 uint32_t datatype; ///< datatype, field 4, an SPB_DT_* code
152 embed_bool has_value; ///< false when no value oneof member was present
153 SpbMetricKind kind; ///< which value member is set, valid when @ref has_value
154 uint32_t int_value; ///< int_value, field 10
155 uint64_t long_value; ///< long_value, field 11
156 float float_value; ///< float_value, field 12
157 double double_value; ///< double_value, field 13
158 embed_bool bool_value; ///< boolean_value, field 14
159 const char *string_value; ///< string_value bytes, field 15, un-terminated, or nullptr
160 size_t string_value_len;
161} SpbMetricDecoded;
162
163/** @brief Sparkplug 3.0.0 sec 4.1 topic namespace elements, less the fixed namespace element. */
164typedef struct
165{
166 const char *group_id; ///< group_id (sec 4.1.2)
167 const char *message_type; ///< message_type (sec 4.1.3), one of the SPB_MSG_* constants
168 const char *edge_node_id; ///< edge_node_id (sec 4.1.4)
169 const char *device_id; ///< device_id (sec 4.1.5); NULL for an Edge Node topic
170} SpbTopicArgs;
171
172/** @brief Where a built topic string lands. */
173typedef struct
174{
175 char *out; ///< the buffer a topic build writes into
176 size_t cap; ///< how much room it has, the NUL included
177} SpbTopicOutArgs;
178
179/** @brief Where encoded Protocol Buffers octets land. */
180typedef struct
181{
182 uint8_t *buf; ///< the buffer a Payload or Metric build writes into
183 size_t cap; ///< how many octets it holds
184} SpbOutArgs;
185
186/** @brief The Payload header fields a build stamps (Sparkplug 3.0.0 sec 6.4.5). */
187typedef struct
188{
189 uint64_t timestamp; ///< timestamp, field 1, milliseconds since epoch; MUST be UTC
190 uint64_t seq; ///< seq, field 3; 0..255, incrementing by one and wrapping to zero
191} SpbPayloadArgs;
192
193/** @brief The Metrics a build serializes (Sparkplug 3.0.0 sec 6.4.6). */
194typedef struct
195{
196 const SpbMetric *list; ///< the Metric array; a Metric build serializes list[0]
197 size_t count; ///< how many of them a Payload build writes
198} SpbMetricsArgs;
199
200/** @brief The octets a decode reads, and the cursor an iteration carries across calls. */
201typedef struct
202{
203 const uint8_t *buf; ///< the encoded Payload or Metric being read
204 size_t len; ///< how many octets it holds
205 size_t cursor; ///< the metrics iteration position; set 0 to start, advanced by each call
206} SpbSourceArgs;
207
208/**
209 * @brief The Sparkplug B codec: the sec 4.1 topic namespace and the sec 6.4.1 payload schema.
210 *
211 * A caller sets the members a call takes, invokes it through ::Sparkplug, and reads the outcome off
212 * the same handle.
213 *
214 * No slot member: the codec holds no rows, so no call names one.
215 *
216 * @var SparkplugNs::topic the topic namespace elements a topic build joins (sec 4.1)
217 * @var SparkplugNs::topic_out the buffer a topic build writes into
218 * @var SparkplugNs::out the buffer a Payload or Metric build writes into
219 * @var SparkplugNs::payload the Payload header fields a build stamps (sec 6.4.5)
220 * @var SparkplugNs::metrics the Metrics a build serializes (sec 6.4.6)
221 * @var SparkplugNs::source the octets a decode reads, and its metrics cursor
222 * @var SparkplugNs::ok a call's true/false outcome; a build reports whether it wrote anything
223 * @var SparkplugNs::n the length a build wrote, 0 if it did not fit
224 * @var SparkplugNs::header the Payload header a parse decoded (sec 6.4.5)
225 * @var SparkplugNs::metric the Metric a parse decoded (sec 6.4.6)
226 * @var SparkplugNs::metric_bytes the next Metric sub-message an iteration reports, pointing into @c source
227 * @var SparkplugNs::metric_len how many octets that sub-message holds
228 * @var SparkplugNs::build_topic join `spBv1.0/group_id/message_type/edge_node_id[/device_id]` into @c topic_out
229 * @var SparkplugNs::build_metric serialize @c metrics.list[0] as one Metric message into @c out
230 * @var SparkplugNs::build_payload serialize @c payload plus @c metrics.count Metrics as one Payload into @c out
231 * @var SparkplugNs::parse_payload read a Payload's timestamp and seq from @c source into @c header
232 * @var SparkplugNs::next_metric report the next metrics(2) sub-message of a Payload and advance @c source.cursor
233 * @var SparkplugNs::parse_metric decode the Metric in @c source into @c metric
234 */
235typedef struct
236{
237 SpbTopicArgs topic; ///< what a topic says
238 SpbTopicOutArgs topic_out; ///< where that topic lands
239 SpbOutArgs out; ///< where encoded octets land
240 SpbPayloadArgs payload; ///< what a Payload header says
241 SpbMetricsArgs metrics; ///< what a build serializes
242 SpbSourceArgs source; ///< what a decode reads
243 embed_bool ok;
244 size_t n;
245 SpbPayloadHeader header;
246 SpbMetricDecoded metric;
247 const uint8_t *metric_bytes;
248 size_t metric_len;
249} SparkplugVars;
250
251/** @brief The operands and the outcome. */
252extern SparkplugVars SparkplugV;
253
254/** @brief The entries. */
255typedef struct
256{
257 void (*const build_topic)(const uint8_t *work);
258 void (*const build_metric)(const uint8_t *work);
259 void (*const build_payload)(uint8_t *work);
260 void (*const parse_payload)(const uint8_t *work);
261 void (*const next_metric)(const uint8_t *work);
262 void (*const parse_metric)(const uint8_t *work);
263} SparkplugNs;
264
265// What the table binds, defined once in the .c and taking one parameter each: everything
266// else an entry needs is an operand in SparkplugV or a region of the borrow at a fixed offset.
267void protocore_sparkplug_build_topic(const uint8_t *work);
268void protocore_sparkplug_build_metric(const uint8_t *work);
269void protocore_sparkplug_build_payload(uint8_t *work);
270void protocore_sparkplug_parse_payload(const uint8_t *work);
271void protocore_sparkplug_next_metric(const uint8_t *work);
272void protocore_sparkplug_parse_metric(const uint8_t *work);
273
274// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
275// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
276// `Sparkplug.build_topic(work)` resolves to a named function and becomes a DIRECT call. An extern table
277// leaves the call indirect and the symbol live at every level, -O2 -flto included.
278static const SparkplugNs Sparkplug __attribute__((unused)) = {
279 .build_topic = protocore_sparkplug_build_topic,
280 .build_metric = protocore_sparkplug_build_metric,
281 .build_payload = protocore_sparkplug_build_payload,
282 .parse_payload = protocore_sparkplug_parse_payload,
283 .next_metric = protocore_sparkplug_next_metric,
284 .parse_metric = protocore_sparkplug_parse_metric,
285};
286
287/**
288 * @brief The PROTOCORE_SPARKPLUG_BORROW bytes this module's state lives in.
289 *
290 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
291 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
292 * walks, so the state lasts the life of the program.
293 *
294 * @return the span.
295 */
296uint8_t *protocore_sparkplug_span(void);
297
298EMBED_END_DECLS
299
300#endif // PROTOCORE_ENABLE_SPARKPLUG
301
302#endif // PROTOCORE_SPARKPLUG_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.