ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
enocean.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_ENOCEAN_H
5#define PROTOCORE_ENOCEAN_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
9EMBED_BEGIN_DECLS
10
11/**
12 * @file enocean.h
13 * @brief EnOcean ESP3 serial codec (PROTOCORE_ENABLE_ENOCEAN) - energy-harvesting 868 MHz.
14 *
15 * A UART telegram codec for EnOcean Serial Protocol 3 (ESP3), the framing every USB /
16 * serial EnOcean gateway (TCM 310 / USB 300) speaks. A telegram is:
17 *
18 * 0x55 | data-len (2, big-endian) | opt-len (1) | packet-type (1) | CRC8H
19 * | data[data-len] | opt[opt-len] | CRC8D
20 *
21 * where CRC8H protects the 4 header bytes and CRC8D protects the data + optional data (both
22 * CRC-8, polynomial 0x07, init 0). protocore_esp3_parse() frames one telegram out of a byte stream,
23 * resynchronizing on a bad sync / CRC, and protocore_esp3_build() assembles one. This is the radio-
24 * plugin codec for the gateway: an inbound RADIO_ERP1 telegram carries a sender id (its
25 * source address) and payload. Bridge it northbound with protocore_gateway_uplink(). Pure - you feed
26 * it the UART bytes - so it is fully host-testable. See example EnOceanGateway.
27 *
28 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
29 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
30 * a caller drives every namespace the same way.
31 *
32 * @author Douglas Quigg (dstroy0)
33 * @date 2026
34 */
35
36// PROTOCORE_ENOCEAN_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
37// it into its arena. Its size and its offset are each a static_assert, so a feature
38// combination that does not fit fails to compile rather than overrunning at run time.
39
40/** @brief ESP3 sync byte that starts every telegram. */
41#define ESP3_SYNC 0x55
42
43// Common RORG (telegram-type) codes.
44#define PROTOCORE_ERP_RORG_RPS 0xF6 ///< Repeated Switch communication (rocker switches): 1 payload octet
45#define PROTOCORE_ERP_RORG_1BS 0xD5 ///< 1-byte communication (contacts): 1 payload octet
46#define PROTOCORE_ERP_RORG_4BS 0xA5 ///< 4-byte communication (sensors): 4 payload octets
47#define PROTOCORE_ERP_RORG_VLD 0xD2 ///< Variable-Length Data
48#define PROTOCORE_ERP_RORG_MSC 0xD1 ///< Manufacturer-Specific Communication
49#define PROTOCORE_ERP_RORG_ADT 0xA6 ///< Addressing Destination Telegram
50#define PROTOCORE_ERP_RORG_UTE 0xD4 ///< Universal Teach-in
51
52/** @brief ESP3 packet types (the common ones). */
53EMBED_ENUM_BYTE(protocore_esp3_type, ESP3_RADIO_ERP1 = 0x01, ESP3_RESPONSE = 0x02, ESP3_RADIO_SUB_TEL = 0x03,
54 ESP3_EVENT = 0x04, ESP3_COMMON_COMMAND = 0x05, ESP3_SMART_ACK = 0x06, ESP3_REMOTE_MAN = 0x07,
55 ESP3_RADIO_ERP2 = 0x0A, );
56
57/** @brief A parsed ESP3 telegram (pointers alias the caller's buffer). */
58typedef struct
59{
60 const uint8_t *data; ///< data field
61 const uint8_t *opt; ///< optional-data field
62 uint16_t data_len; ///< data length
63 uint8_t opt_len; ///< optional-data length
64 protocore_esp3_type type; ///< packet type (protocore_esp3_type)
66
67/** @brief A decoded ERP1 radio telegram (the payload aliases the caller's buffer). */
68typedef struct
69{
70 uint8_t rorg; ///< telegram type (PROTOCORE_ERP_RORG_*)
71 const uint8_t *payload; ///< RORG-specific data, or nullptr if none
72 uint8_t payload_len; ///< payload octets (data length - 6)
73 uint32_t sender_id; ///< 4-octet sender id (big-endian)
74 uint8_t status; ///< status octet (repeater count + telegram-type bits)
76
77/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
78typedef struct
79{
80 uint8_t (*esp3_crc8)(const uint8_t *, const uint8_t *, uint16_t);
81 int (*esp3_parse)(uint8_t *, const uint8_t *, uint16_t, protocore_esp3_packet *);
82 uint16_t (*esp3_build)(uint8_t *, protocore_esp3_type, const uint8_t *, uint16_t, const uint8_t *, uint8_t,
83 uint8_t *, uint16_t);
84 embed_bool (*erp1_parse)(const uint8_t *, const uint8_t *, uint16_t, protocore_erp1 *);
85 uint16_t (*erp1_build)(const uint8_t *, uint8_t *, uint16_t, uint8_t, const uint8_t *, uint8_t, uint32_t, uint8_t);
86} EnoceanNs;
87PROTOCORE_NS_LAYOUT(EnoceanNs, esp3_crc8, esp3_parse, esp3_build, erp1_parse, erp1_build);
88
89/**
90 * @brief CRC-8 used by ESP3 (polynomial 0x07, MSB-first, init 0x00).
91 * @param work PROTOCORE_ENOCEAN_BORROW bytes the caller took. Not held past the call.
92 * @param buf Buf
93 * @param len Len
94 * @return The uint8_t.
95 */
96uint8_t protocore_enocean_esp3_crc8(const uint8_t *work, const uint8_t *buf, uint16_t len);
97/**
98 * @brief Frame one ESP3 telegram from the front of raw.
99 * @param work PROTOCORE_ENOCEAN_BORROW bytes the caller took. Not held past the call.
100 * @param raw Raw
101 * @param len Len
102 * @param out Out
103 * @return The int.
104 */
105int protocore_enocean_esp3_parse(uint8_t *work, const uint8_t *raw, uint16_t len, protocore_esp3_packet *out);
106/**
107 * @brief Assemble an ESP3 telegram into out.
108 * @param work PROTOCORE_ENOCEAN_BORROW bytes the caller took. Not held past the call.
109 * @param type Type
110 * @param data Data
111 * @param data_len Data len
112 * @param opt Opt
113 * @param opt_len Opt len
114 * @param out Out
115 * @param cap Cap
116 * @return The uint16_t.
117 */
118uint16_t protocore_enocean_esp3_build(uint8_t *work, protocore_esp3_type type, const uint8_t *data, uint16_t data_len,
119 const uint8_t *opt, uint8_t opt_len, uint8_t *out, uint16_t cap);
120/**
121 * @brief Decode an ERP1 radio telegram: RORG + payload + 4-octet sender id + .
122 * @param work PROTOCORE_ENOCEAN_BORROW bytes the caller took. Not held past the call.
123 * @param data Data
124 * @param len Len
125 * @param out Out
126 * @return EMBED_TRUE on success.
127 */
128embed_bool protocore_enocean_erp1_parse(const uint8_t *work, const uint8_t *data, uint16_t len, protocore_erp1 *out);
129/**
130 * @brief Assemble an ERP1 radio telegram (the inverse of .
131 * @param work PROTOCORE_ENOCEAN_BORROW bytes the caller took. Not held past the call.
132 * @param out Out
133 * @param cap Cap
134 * @param rorg Rorg
135 * @param payload Payload
136 * @param payload_len Payload len
137 * @param sender_id Sender id
138 * @param status Status
139 * @return The uint16_t.
140 */
141uint16_t protocore_enocean_erp1_build(const uint8_t *work, uint8_t *out, uint16_t cap, uint8_t rorg,
142 const uint8_t *payload, uint8_t payload_len, uint32_t sender_id, uint8_t status);
143
144/** @brief Module namespace. */
150
151EMBED_END_DECLS
152
153#endif // PROTOCORE_ENOCEAN_H
uint8_t protocore_enocean_esp3_crc8(const uint8_t *work, const uint8_t *buf, uint16_t len)
CRC-8 used by ESP3 (polynomial 0x07, MSB-first, init 0x00).
EMBED_ENUM_BYTE(protocore_esp3_type, ESP3_RADIO_ERP1=0x01, ESP3_RESPONSE=0x02, ESP3_RADIO_SUB_TEL=0x03, ESP3_EVENT=0x04, ESP3_COMMON_COMMAND=0x05, ESP3_SMART_ACK=0x06, ESP3_REMOTE_MAN=0x07, ESP3_RADIO_ERP2=0x0A,)
ESP3 packet types (the common ones).
embed_bool protocore_enocean_erp1_parse(const uint8_t *work, const uint8_t *data, uint16_t len, protocore_erp1 *out)
Decode an ERP1 radio telegram: RORG + payload + 4-octet sender id + .
int protocore_enocean_esp3_parse(uint8_t *work, const uint8_t *raw, uint16_t len, protocore_esp3_packet *out)
Frame one ESP3 telegram from the front of raw.
uint16_t protocore_enocean_esp3_build(uint8_t *work, protocore_esp3_type type, const uint8_t *data, uint16_t data_len, const uint8_t *opt, uint8_t opt_len, uint8_t *out, uint16_t cap)
Assemble an ESP3 telegram into out.
uint16_t protocore_enocean_erp1_build(const uint8_t *work, uint8_t *out, uint16_t cap, uint8_t rorg, const uint8_t *payload, uint8_t payload_len, uint32_t sender_id, uint8_t status)
Assemble an ERP1 radio telegram (the inverse of .
#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 enocean.h:79
int(* esp3_parse)(uint8_t *, const uint8_t *, uint16_t, protocore_esp3_packet *)
Definition enocean.h:81
uint16_t(* erp1_build)(const uint8_t *, uint8_t *, uint16_t, uint8_t, const uint8_t *, uint8_t, uint32_t, uint8_t)
Definition enocean.h:85
uint8_t(* esp3_crc8)(const uint8_t *, const uint8_t *, uint16_t)
Definition enocean.h:80
embed_bool(* erp1_parse)(const uint8_t *, const uint8_t *, uint16_t, protocore_erp1 *)
Definition enocean.h:84
uint16_t(* esp3_build)(uint8_t *, protocore_esp3_type, const uint8_t *, uint16_t, const uint8_t *, uint8_t, uint8_t *, uint16_t)
Definition enocean.h:82
A decoded ERP1 radio telegram (the payload aliases the caller's buffer).
Definition enocean.h:69
const uint8_t * payload
RORG-specific data, or nullptr if none.
Definition enocean.h:71
uint8_t rorg
telegram type (PROTOCORE_ERP_RORG_*)
Definition enocean.h:70
uint8_t status
status octet (repeater count + telegram-type bits)
Definition enocean.h:74
uint32_t sender_id
4-octet sender id (big-endian)
Definition enocean.h:73
uint8_t payload_len
payload octets (data length - 6)
Definition enocean.h:72
A parsed ESP3 telegram (pointers alias the caller's buffer).
Definition enocean.h:59
uint8_t opt_len
optional-data length
Definition enocean.h:63
const uint8_t * data
data field
Definition enocean.h:60
uint16_t data_len
data length
Definition enocean.h:62
const uint8_t * opt
optional-data field
Definition enocean.h:61
protocore_esp3_type type
packet type (protocore_esp3_type)
Definition enocean.h:64