ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
senml.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 senml.h
6 * @brief Sensor Measurement Lists (SenML, RFC 8428): the Pack builders and the Record resolver.
7 *
8 * RFC 8428 sec 3 names the units: a SenML Record is "one measurement or configuration instance in
9 * time presented using the SenML data model", and a SenML Pack is "one or more SenML Records in an
10 * array structure". This module builds a Pack from a caller-owned Record array and resolves a Pack
11 * in place of a consumer.
12 *
13 * A Record carries Base Fields (RFC 8428 sec 4.1) that apply to it and the Records after it, and
14 * Regular Fields (sec 4.2) that apply to it alone. RFC 8428 sec 5.1.1:
15 * @code
16 * [
17 * {"n":"urn:dev:ow:10e2073a01080063","u":"Cel","v":23.1}
18 * ]
19 * @endcode
20 *
21 * The JSON representation (sec 5, application/senml+json) writes the labels as member names. The
22 * CBOR representation (sec 6, application/senml+cbor) writes the Table 4 integer map keys instead;
23 * that table is conclusive, so one walk feeds every binary encoding through @ref protocore_codec and
24 * the encoding is an argument. A Number that is integral is emitted as an integer, so a Time keeps
25 * full precision; otherwise it is emitted as a floating-point value.
26 *
27 * Both builders write into a caller buffer and report 0 bytes when it will not hold the whole Pack.
28 * The resolver (sec 4.6) folds the Base Name and Base Time into each Record, so each output Record
29 * carries a full Name and an absolute Time and stands alone.
30 *
31 * The module exports one symbol, @ref Senml. Everything in senml.c has internal linkage.
32 *
33 * @author Douglas Quigg (dstroy0)
34 * @date 2026
35 */
36
37#ifndef PROTOCORE_SENML_H
38#define PROTOCORE_SENML_H
39
40#include "protocore_config.h" // the entry point: protocore_types.h for the widths
41
42#if PROTOCORE_ENABLE_SENML
43
44#include "network_drivers/presentation/codec/codec.h" // protocore_codec: the binary encoding is an argument
45
46EMBED_BEGIN_DECLS
47
48/** @brief Longest resolved Name (Base Name concatenated with Name), the NUL included. */
49#define PROTOCORE_SENML_RESOLVED_NAME_MAX 96
50
51/** @brief Which value field a Record carries (RFC 8428 sec 4.2). */
52EMBED_ENUM_BYTE(SenmlValueKind,
53 SENML_VALUE_NONE, ///< no value field, as in a Base-Fields-only Record
54 SENML_VALUE_NUMBER, ///< Value (v), a Number; emitted as an integer when integral
55 SENML_VALUE_STRING, ///< String Value (vs)
56 SENML_VALUE_BOOLEAN, ///< Boolean Value (vb)
57);
58
59/**
60 * @brief One SenML Record (RFC 8428 sec 3): its Base Fields (sec 4.1) and Regular Fields (sec 4.2).
61 *
62 * Every string is borrowed, not copied, and a null one leaves its label out of the Pack.
63 */
64typedef struct
65{
66 const char *base_name; ///< Base Name (bn), prepended to the Names that follow
67 embed_bool has_base_time; ///< a Base Time is present
68 double base_time; ///< Base Time (bt), added to the Times that follow
69 const char *name; ///< Name (n)
70 const char *unit; ///< Unit (u)
71 SenmlValueKind value_kind; ///< which of the three value fields below this Record carries
72 double value; ///< Value (v)
73 const char *string_value; ///< String Value (vs)
74 embed_bool boolean_value; ///< Boolean Value (vb)
75 embed_bool has_time; ///< a Time is present
76 double time; ///< Time (t)
77} SenmlRecord;
78
79/**
80 * @brief One resolved SenML Record (RFC 8428 sec 4.6): no Base Fields left and no relative Time.
81 */
82typedef struct
83{
84 char name[PROTOCORE_SENML_RESOLVED_NAME_MAX]; ///< Name (n): the Base Name concatenated with the Name
85 const char *unit; ///< Unit (u), borrowed from the input Record
86 SenmlValueKind value_kind; ///< which value field this Record carries
87 double value; ///< Value (v)
88 const char *string_value; ///< String Value (vs)
89 embed_bool boolean_value; ///< Boolean Value (vb)
90 embed_bool has_time; ///< a Time is present
91 double time; ///< Time (t): the Base Time added to the Time
92} SenmlResolved;
93
94/** @brief RFC 8428 sec 3: the Pack a call reads, one array of Records. */
95typedef struct
96{
97 const SenmlRecord *records; ///< the Records the array holds
98 size_t count; ///< how many of them
99} SenmlPackArgs;
100
101/** @brief RFC 8428 sec 5: where the JSON representation (application/senml+json) lands. */
102typedef struct
103{
104 char *buf; ///< the buffer the text Pack is written into
105 size_t cap; ///< how much room it has, the NUL included
106} SenmlJsonArgs;
107
108/** @brief RFC 8428 sec 6: the binary encoding a Pack takes, and where it lands. */
109typedef struct
110{
111 const protocore_codec *codec; ///< the encoding the Table 4 integer labels are written through
112 uint8_t *buf; ///< the buffer the encoded Pack is written into
113 size_t cap; ///< how much room it has
114} SenmlBinaryArgs;
115
116/** @brief RFC 8428 sec 4.6: where the resolved Records land. */
117typedef struct
118{
119 SenmlResolved *out; ///< the array a resolve fills
120 size_t max; ///< how many Records that array holds
121} SenmlResolvedArgs;
122
123/**
124 * @brief The SenML Pack builders and the Record resolver.
125 *
126 * A caller sets the members a call takes, invokes it through ::Senml, and reads the outcome off the
127 * same handle.
128 *
129 * No slot member: a Pack is the whole unit every call names, so no call names a row.
130 *
131 * @var SenmlNs::pack the Records a build encodes or a resolve reads (RFC 8428 sec 3)
132 * @var SenmlNs::json where the JSON representation lands (RFC 8428 sec 5)
133 * @var SenmlNs::binary the encoding a Pack takes and where it lands (RFC 8428 sec 6)
134 * @var SenmlNs::resolved where the resolved Records land (RFC 8428 sec 4.6)
135 * @var SenmlNs::ok a call's true/false outcome
136 * @var SenmlNs::n the bytes a build wrote, excluding the NUL, or the Records a resolve
137 * produced; 0 when the call failed
138 * @var SenmlNs::json_build encode @c pack into @c json as application/senml+json (sec 5)
139 * @var SenmlNs::binary_build encode @c pack into @c binary through its codec, with the sec 6
140 * Table 4 integer labels
141 * @var SenmlNs::resolve carry the Base Name and Base Time across @c pack and fold them into
142 * each Record, into @c resolved (sec 4.6)
143 */
144typedef struct
145{
146 SenmlPackArgs pack; ///< what a call reads
147 SenmlJsonArgs json; ///< where the text Pack lands
148 SenmlBinaryArgs binary; ///< which encoding, and where the encoded Pack lands
149 SenmlResolvedArgs resolved; ///< where the resolved Records land
150 embed_bool ok;
151 size_t n;
152} SenmlVars;
153
154/** @brief The operands and the outcome. */
155extern SenmlVars SenmlV;
156
157/** @brief The entries. */
158typedef struct
159{
160 void (*const json_build)(uint8_t *work);
161 void (*const binary_build)(const uint8_t *work);
162 void (*const resolve)(const uint8_t *work);
163} SenmlNs;
164
165// What the table binds, defined once in the .c and taking one parameter each: everything
166// else an entry needs is an operand in SenmlV or a region of the borrow at a fixed offset.
167void protocore_senml_json_build(uint8_t *work);
168void protocore_senml_binary_build(const uint8_t *work);
169void protocore_senml_resolve(const uint8_t *work);
170
171// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
172// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
173// `Senml.json_build(work)` resolves to a named function and becomes a DIRECT call. An extern table
174// leaves the call indirect and the symbol live at every level, -O2 -flto included.
175static const SenmlNs Senml __attribute__((unused)) = {
176 .json_build = protocore_senml_json_build,
177 .binary_build = protocore_senml_binary_build,
178 .resolve = protocore_senml_resolve,
179};
180
181EMBED_END_DECLS
182
183#endif // PROTOCORE_ENABLE_SENML
184
185#endif // PROTOCORE_SENML_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.
One binary codec interface; a wire encoding is an instance of it.
A wire encoding: the ten writes, the peek, and the nine reads.
Definition codec.h:52