ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
mbus.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 mbus.h
6 * @brief Wired M-Bus (Meter-Bus, EN 13757-2/-3) frame codec (PROTOCORE_ENABLE_MBUS).
7 *
8 * A pure, zero-heap builder + parser for the M-Bus link-layer frames used by utility meters
9 * (water / gas / heat / electricity), plus a walker for the EN 13757-3 variable-data records
10 * (DIF / VIF). M-Bus has three frame formats:
11 * @code
12 * single char : E5 (ACK)
13 * short frame : 10 C A CS 16
14 * long frame : 68 L L 68 C A CI [user data] CS 16 (L = 3 + data; CS = sum(C..data) mod 256)
15 * @endcode
16 * The control frame is just a long frame with no user data (L = 3). The checksum is the
17 * 8-bit sum of every octet from C through the end of the user data.
18 *
19 * The wired bus is a powered two-wire pair: the ESP32 talks to it over a UART through an
20 * M-Bus level converter (e.g. a TSS721-based master module). This codec is the framing +
21 * record layer, including decoding a record's raw value (integer / BCD / real) into a number and its VIF
22 * into a physical unit + decimal exponent; the UART transport is the application's. Bridge meters onto
23 * Wi-Fi by polling REQ_UD2 and publishing the decoded records.
24 *
25 * @author Douglas Quigg (dstroy0)
26 * @date 2026
27 */
28
29#ifndef PROTOCORE_MBUS_H
30#define PROTOCORE_MBUS_H
31
32#include "protocore_config.h" // the entry point: protocore_types.h for the widths
33
34#if PROTOCORE_ENABLE_MBUS
35
36EMBED_BEGIN_DECLS
37
38// This module holds nothing between calls, so it carves no borrow and states none. An entry
39// takes one all the same, and never reads it, so every namespace in the tree is invoked the
40// same way.
41
42#define MBUS_START_SHORT 0x10u ///< short-frame start octet
43#define MBUS_START_LONG 0x68u ///< long / control-frame start octet
44#define MBUS_STOP 0x16u ///< frame stop octet
45#define MBUS_ACK 0xE5u ///< single-character acknowledge
46
47// Common control-field (C) values.
48#define MBUS_C_SND_NKE 0x40u ///< initialize slave (link reset)
49#define MBUS_C_REQ_UD2 0x5Bu ///< request class-2 user data (FCB=0); 0x7B with FCB=1
50#define MBUS_C_REQ_UD1 0x5Au ///< request class-1 user data
51#define MBUS_C_SND_UD 0x53u ///< send user data to slave (FCB=0); 0x73 with FCB=1
52#define MBUS_C_RSP_UD 0x08u ///< response with user data (+ ACD/DFC bits)
53
54// Common control-information (CI) values.
55#define MBUS_CI_DATA_SEND 0x51u ///< data send (master -> slave)
56#define MBUS_CI_SELECT 0x52u ///< selection of slaves
57#define MBUS_CI_RSP_VARIABLE 0x72u ///< variable data response, long header (LSB first)
58#define MBUS_CI_RSP_FIXED 0x73u ///< fixed data response
59
60#define MBUS_MAX_DATA 252u ///< max user-data octets (L is one octet; 255 - 3)
61
62#define MBUS_VAR_HEADER_LEN 12u ///< octets of the fixed header preceding the records in a CI=0x72 response
63
64// Common medium / device-type codes (EN 13757-3 ยง6.4).
65#define MBUS_MEDIUM_OTHER 0x00u
66#define MBUS_MEDIUM_OIL 0x01u
67#define MBUS_MEDIUM_ELECTRICITY 0x02u
68#define MBUS_MEDIUM_GAS 0x03u
69#define MBUS_MEDIUM_HEAT_OUTLET 0x04u
70#define MBUS_MEDIUM_STEAM 0x05u
71#define MBUS_MEDIUM_WARM_WATER 0x06u
72#define MBUS_MEDIUM_WATER 0x07u
73#define MBUS_MEDIUM_HEAT_COST 0x08u
74#define MBUS_MEDIUM_HEAT_INLET 0x0Cu
75#define MBUS_MEDIUM_HEAT_COOLING 0x0Du
76#define MBUS_MEDIUM_COLD_WATER 0x16u
77
78/** @brief M-Bus frame kinds. */
79EMBED_ENUM_BYTE(MbusFrameType, MBUS_FRAME_NONE = 0,
80 MBUS_FRAME_ACK, ///< single 0xE5
81 MBUS_FRAME_SHORT, ///< 10 C A CS 16
82 MBUS_FRAME_LONG, ///< 68 L L 68 C A CI ... CS 16 (control frame = long with no data)
83);
84
85/** @brief A parsed M-Bus frame (data points into the caller's buffer). */
86typedef struct
87{
88 MbusFrameType type;
89 uint8_t c; ///< control field (short / long)
90 uint8_t a; ///< address field (short / long)
91 uint8_t ci; ///< control-information field (long only)
92 const uint8_t *data; ///< user data (long only), or nullptr
93 uint8_t data_len; ///< user-data length (long only)
94} MbusFrame;
95
96EMBED_ENUM_BYTE(MbusDifCoding,
97 MBUS_DIF_NONE = 0x0, ///< no data
98 MBUS_DIF_INT8 = 0x1, ///< 8-bit integer
99 MBUS_DIF_INT16 = 0x2, ///< 16-bit integer
100 MBUS_DIF_INT24 = 0x3, ///< 24-bit integer
101 MBUS_DIF_INT32 = 0x4, ///< 32-bit integer
102 MBUS_DIF_REAL32 = 0x5, ///< 32-bit IEEE-754 real
103 MBUS_DIF_INT48 = 0x6, ///< 48-bit integer
104 MBUS_DIF_INT64 = 0x7, ///< 64-bit integer
105 MBUS_DIF_READOUT = 0x8, ///< selection for readout (no data)
106 MBUS_DIF_BCD2 = 0x9, ///< 2-digit BCD (1 octet)
107 MBUS_DIF_BCD4 = 0xA, ///< 4-digit BCD (2 octets)
108 MBUS_DIF_BCD6 = 0xB, ///< 6-digit BCD (3 octets)
109 MBUS_DIF_BCD8 = 0xC, ///< 8-digit BCD (4 octets)
110 MBUS_DIF_VARIABLE = 0xD, ///< variable length (LVAR octet precedes the data)
111 MBUS_DIF_BCD12 = 0xE, ///< 12-digit BCD (6 octets)
112 MBUS_DIF_SPECIAL = 0xF, ///< special functions (no data)
113);
114
115/** @brief One decoded EN 13757-3 data record. */
116typedef struct
117{
118 uint8_t dif; ///< first data-information octet
119 uint8_t coding; ///< DIF low nibble (see MbusDifCoding)
120 uint8_t vif; ///< first value-information octet (0 if none)
121 const uint8_t *data; ///< value octets (points into the caller buffer)
122 uint8_t data_len; ///< value length in octets
123} MbusRecord;
124
125/** @brief Physical unit a VIF decodes to (the common EN 13757-3 measurement ranges). */
126EMBED_ENUM_BYTE(MbusUnit, MBUS_UNIT_UNKNOWN = 0,
127 MBUS_UNIT_WH, ///< energy, watt-hours
128 MBUS_UNIT_J, ///< energy, joules
129 MBUS_UNIT_M3, ///< volume, cubic metres
130 MBUS_UNIT_KG, ///< mass, kilograms
131 MBUS_UNIT_W, ///< power, watts
132 MBUS_UNIT_J_PER_H, ///< power, joules per hour
133 MBUS_UNIT_M3_PER_H, ///< volume flow, cubic metres per hour
134 MBUS_UNIT_CELSIUS, ///< temperature, degrees Celsius
135 MBUS_UNIT_K, ///< temperature difference, kelvin
136 MBUS_UNIT_BAR, ///< pressure, bar
137);
138
139/** @brief The decoded EN 13757-3 variable-data-structure fixed header. */
140typedef struct
141{
142 uint32_t id; ///< identification number (secondary-address serial), decoded from the 4-octet BCD
143 char manufacturer[4]; ///< 3-letter manufacturer code + NUL (decoded from the 2-octet field)
144 uint16_t manufacturer_raw; ///< the raw 2-octet manufacturer value (FLAG code)
145 uint8_t version; ///< device generation / version
146 uint8_t medium; ///< medium / device type (MBUS_MEDIUM_*)
147 uint8_t access_no; ///< access number (increments each readout)
148 uint8_t status; ///< status octet (error / alarm bits)
149 uint16_t signature; ///< signature word (usually 0)
150} MbusVarHeader;
151
152/** @brief What build_ack takes: buf, cap. */
153typedef struct
154{
155 uint8_t *buf;
156 size_t cap;
157} MbusBuildAckArgs;
158
159/** @brief What build_short takes: buf, cap, c, a. */
160typedef struct
161{
162 uint8_t *buf;
163 size_t cap;
164 uint8_t c;
165 uint8_t a;
166} MbusBuildShortArgs;
167
168/** @brief What build_long takes: buf, cap, c, a, ci, data, data_len. */
169typedef struct
170{
171 uint8_t *buf;
172 size_t cap;
173 uint8_t c;
174 uint8_t a;
175 uint8_t ci;
176 const uint8_t *data;
177 uint8_t data_len;
178} MbusBuildLongArgs;
179
180/** @brief What build_snd_nke takes: buf, cap, a. */
181typedef struct
182{
183 uint8_t *buf;
184 size_t cap;
185 uint8_t a;
186} MbusBuildSndNkeArgs;
187
188/** @brief What build_req_ud2 takes: buf, cap, a, fcb. */
189typedef struct
190{
191 uint8_t *buf;
192 size_t cap;
193 uint8_t a;
194 embed_bool fcb;
195} MbusBuildReqUd2Args;
196
197/** @brief What build_req_ud1 takes: buf, cap, a, fcb. */
198typedef struct
199{
200 uint8_t *buf;
201 size_t cap;
202 uint8_t a;
203 embed_bool fcb;
204} MbusBuildReqUd1Args;
205
206/** @brief What parse takes: buf, len, out, consumed. */
207typedef struct
208{
209 const uint8_t *buf;
210 size_t len;
211 MbusFrame *out;
212 size_t *consumed;
213} MbusParseArgs;
214
215/** @brief What dif_data_len takes: coding. */
216typedef struct
217{
218 uint8_t coding;
219} MbusDifDataLenArgs;
220
221/** @brief What record_next takes: body, len, pos, out. */
222typedef struct
223{
224 const uint8_t *body;
225 size_t len;
226 size_t *pos;
227 MbusRecord *out;
228} MbusRecordNextArgs;
229
230/** @brief What record_value_int takes: r, out. */
231typedef struct
232{
233 const MbusRecord *r;
234 int64_t *out;
235} MbusRecordValueIntArgs;
236
237/** @brief What record_value_real takes: r, out. */
238typedef struct
239{
240 const MbusRecord *r;
241 float *out;
242} MbusRecordValueRealArgs;
243
244/** @brief What vif_decode takes: vif, unit, exp10. */
245typedef struct
246{
247 uint8_t vif;
248 MbusUnit *unit;
249 int8_t *exp10;
250} MbusVifDecodeArgs;
251
252/** @brief What parse_var_header takes: body, len, out. */
253typedef struct
254{
255 const uint8_t *body;
256 size_t len;
257 MbusVarHeader *out;
258} MbusParseVarHeaderArgs;
259
260/**
261 * @brief Wired M-Bus (Meter-Bus, EN 13757-2/-3) frame codec (PROTOCORE_ENABLE_MBUS).
262 *
263 * A caller sets the members a call takes, invokes it through ::Mbus with the bytes it runs
264 * out of, and reads the outcome off the same handle.
265 *
266 * Mbus.build_ack_args.buf = ...;
267 * Mbus.build_ack_args.cap = ...;
268 * Mbus.build_ack(work);
269 * // Mbus.n is what the call reports
270 *
271 * @var MbusNs::build_ack_args what build_ack takes: buf, cap
272 * @var MbusNs::build_short_args what build_short takes: buf, cap, c, a
273 * @var MbusNs::build_long_args what build_long takes: buf, cap, c, a, ci, data, data_len
274 * @var MbusNs::build_snd_nke_args what build_snd_nke takes: buf, cap, a
275 * @var MbusNs::build_req_ud2_args what build_req_ud2 takes: buf, cap, a, fcb
276 * @var MbusNs::build_req_ud1_args what build_req_ud1 takes: buf, cap, a, fcb
277 * @var MbusNs::parse_args what parse takes: buf, len, out, consumed
278 * @var MbusNs::dif_data_len_args what dif_data_len takes: coding
279 * @var MbusNs::record_next_args what record_next takes: body, len, pos, out
280 * @var MbusNs::record_value_int_args what record_value_int takes: r, out
281 * @var MbusNs::record_value_real_args what record_value_real takes: r, out
282 * @var MbusNs::vif_decode_args what vif_decode takes: vif, unit, exp10
283 * @var MbusNs::parse_var_header_args what parse_var_header takes: body, len, out
284 * @var MbusNs::ok true for a decoded measurement VIF; false (unit UNKNOWN) for one ...
285 * @var MbusNs::n the count a call reports
286 * @var MbusNs::value the value a call reports
287 * @var MbusNs::build_ack single-character acknowledge (0xE5)
288 * @var MbusNs::build_short short frame: 10 C A CS 16
289 * @var MbusNs::build_long long frame: 68 L L 68 C A CI [data] CS 16. data_len 0 builds a ...
290 * @var MbusNs::build_snd_nke convenience: a SND_NKE (link reset) short frame to address a
291 * @var MbusNs::build_req_ud2 convenience: a REQ_UD2 short frame to address a (fcb toggles the ...
292 * @var MbusNs::build_req_ud1 convenience: a REQ_UD1 (class-1 / alarm data request) short frame ...
293 * @var MbusNs::parse parse one M-Bus frame from buf. Validates the start/stop octets, ...
294 * @var MbusNs::dif_data_len map a DIF low-nibble coding to its fixed data length (0 for ...
295 * @var MbusNs::record_next walk one data record at *pos within a long-frame body (the octets ...
296 * @var MbusNs::record_value_int decode a record's value as a signed 64-bit integer (the integer and ...
297 * @var MbusNs::record_value_real decode a record's value as an IEEE-754 float (only the REAL32 DIF ...
298 * @var MbusNs::vif_decode decode a VIF octet into its unit and the base-10 exponent applied ...
299 * @var MbusNs::parse_var_header decode the 12-octet variable-data-structure fixed header (the ...
300 *
301 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
302 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
303 * a caller drives every namespace the same way.
304 */
305typedef struct
306{
307 MbusBuildAckArgs build_ack_args;
308 MbusBuildShortArgs build_short_args;
309 MbusBuildLongArgs build_long_args;
310 MbusBuildSndNkeArgs build_snd_nke_args;
311 MbusBuildReqUd2Args build_req_ud2_args;
312 MbusBuildReqUd1Args build_req_ud1_args;
313 MbusParseArgs parse_args;
314 MbusDifDataLenArgs dif_data_len_args;
315 MbusRecordNextArgs record_next_args;
316 MbusRecordValueIntArgs record_value_int_args;
317 MbusRecordValueRealArgs record_value_real_args;
318 MbusVifDecodeArgs vif_decode_args;
319 MbusParseVarHeaderArgs parse_var_header_args;
320 embed_bool ok;
321 size_t n;
322 uint8_t value;
323} MbusVars;
324
325/** @brief The operands and the outcome. */
326extern MbusVars MbusV;
327
328/** @brief The entries. */
329typedef struct
330{
331 void (*const build_ack)(const uint8_t *work);
332 void (*const build_short)(const uint8_t *work);
333 void (*const build_long)(const uint8_t *work);
334 void (*const build_snd_nke)(const uint8_t *work);
335 void (*const build_req_ud2)(const uint8_t *work);
336 void (*const build_req_ud1)(const uint8_t *work);
337 void (*const parse)(const uint8_t *work);
338 void (*const dif_data_len)(const uint8_t *work);
339 void (*const record_next)(const uint8_t *work);
340 void (*const record_value_int)(const uint8_t *work);
341 void (*const record_value_real)(const uint8_t *work);
342 void (*const vif_decode)(const uint8_t *work);
343 void (*const parse_var_header)(const uint8_t *work);
344} MbusNs;
345
346// What the table binds, defined once in the .c and taking one parameter each: everything
347// else an entry needs is an operand in MbusV or a region of the borrow at a fixed offset.
348void protocore_mbus_build_ack(const uint8_t *work);
349void protocore_mbus_build_short(const uint8_t *work);
350void protocore_mbus_build_long(const uint8_t *work);
351void protocore_mbus_build_snd_nke(const uint8_t *work);
352void protocore_mbus_build_req_ud2(const uint8_t *work);
353void protocore_mbus_build_req_ud1(const uint8_t *work);
354void protocore_mbus_parse(const uint8_t *work);
355void protocore_mbus_dif_data_len(const uint8_t *work);
356void protocore_mbus_record_next(const uint8_t *work);
357void protocore_mbus_record_value_int(const uint8_t *work);
358void protocore_mbus_record_value_real(const uint8_t *work);
359void protocore_mbus_vif_decode(const uint8_t *work);
360void protocore_mbus_parse_var_header(const uint8_t *work);
361
362// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
363// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
364// `Mbus.build_ack(work)` resolves to a named function and becomes a DIRECT call. An extern table
365// leaves the call indirect and the symbol live at every level, -O2 -flto included.
366static const MbusNs Mbus __attribute__((unused)) = {
367 .build_ack = protocore_mbus_build_ack,
368 .build_short = protocore_mbus_build_short,
369 .build_long = protocore_mbus_build_long,
370 .build_snd_nke = protocore_mbus_build_snd_nke,
371 .build_req_ud2 = protocore_mbus_build_req_ud2,
372 .build_req_ud1 = protocore_mbus_build_req_ud1,
373 .parse = protocore_mbus_parse,
374 .dif_data_len = protocore_mbus_dif_data_len,
375 .record_next = protocore_mbus_record_next,
376 .record_value_int = protocore_mbus_record_value_int,
377 .record_value_real = protocore_mbus_record_value_real,
378 .vif_decode = protocore_mbus_vif_decode,
379 .parse_var_header = protocore_mbus_parse_var_header,
380};
381
382EMBED_END_DECLS
383
384#endif // PROTOCORE_ENABLE_MBUS
385
386#endif // PROTOCORE_MBUS_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.