ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
pioneer_wt_protocol.cpp
Go to the documentation of this file.
2
3#include <cstring>
4
5#include "esphome/core/hal.h"
6
7namespace esphome::pioneer_wt {
8
9uint16_t frame_crc(const uint8_t *frame, size_t len) {
10 const uint16_t head = crc16be(frame, 8);
11 return crc16be(frame + HEADER_SIZE, len - HEADER_SIZE, head);
12}
13
14enum class AttrKind : uint8_t {
18 // One count byte, then that many bytes
20 // Two length bytes, then that many bytes
22};
23
24// The reader must know how every attribute is stored to step over the ones it does not use.
25// The IDs are grouped by storage.
26static constexpr uint16_t ONE_BYTE_IDS[] PROGMEM = {
27 0x0001, 0x0005, 0x000C, 0x000D, 0x000E, 0x0011, 0x0012, 0x0013, 0x0017, 0x001E, 0x0022, 0x0025, 0x0026, 0x0027,
28 0x002D, 0x0035, 0x005E, 0x0073, 0x0074, 0x00A4, 0x00C9, 0x00DF, 0x0148, 0x015E, 0x0173, 0x0224, 0x0225,
29};
30static constexpr uint16_t FOUR_BYTE_IDS[] PROGMEM = {
31 0x0002, 0x0003, 0x003D, 0x0041, 0x0042, 0x005C, 0x0060, 0x0064, 0x0065,
32 0x0072, 0x0095, 0x00BD, 0x00BE, 0x00BF, 0x00C0, 0x0221, 0x0222, 0x0227,
33};
34static constexpr uint16_t COUNT_PREFIXED_IDS[] PROGMEM = {0x0038, 0x0039};
35static constexpr uint16_t LENGTH_PREFIXED_IDS[] PROGMEM = {0x00FA, 0x00FB, 0x0147, 0x0223};
36
37template<size_t N> static bool contains(const uint16_t (&ids)[N], uint16_t id) {
38 for (const uint16_t &entry : ids) {
39 if (progmem_read_uint16(&entry) == id)
40 return true;
41 }
42 return false;
43}
44
45static AttrKind attr_kind(uint16_t id) {
46 if (contains(ONE_BYTE_IDS, id))
48 if (contains(FOUR_BYTE_IDS, id))
50 if (contains(COUNT_PREFIXED_IDS, id))
52 if (contains(LENGTH_PREFIXED_IDS, id))
55}
56
58 if (this->pos_ + 2 > this->len_)
59 return false;
60 const uint8_t *data = this->data_;
61 const uint16_t id = encode_uint16(data[this->pos_], data[this->pos_ + 1]);
62 size_t start = this->pos_ + 2;
63 size_t value_len;
64 const AttrKind kind = attr_kind(id);
65 switch (kind) {
67 value_len = 1;
68 break;
70 value_len = 4;
71 break;
73 if (start + 1 > this->len_)
74 return false;
75 value_len = data[start++];
76 break;
78 if (start + 2 > this->len_)
79 return false;
80 value_len = encode_uint16(data[start], data[start + 1]);
81 start += 2;
82 break;
83 default:
84 this->id_ = id;
85 this->unknown_ = true;
86 return false;
87 }
88 if (start + value_len > this->len_)
89 return false;
90 this->id_ = id;
91 const uint8_t *v = data + start;
92 if (kind == AttrKind::ATTR_KIND_ONE_BYTE) {
93 this->value_ = v[0];
94 } else if (kind == AttrKind::ATTR_KIND_FOUR_BYTES) {
95 this->value_ = encode_uint32(v[0], v[1], v[2], v[3]);
96 } else {
97 this->value_ = 0;
98 }
99 this->pos_ = start + value_len;
100 return true;
101}
102
103size_t build_frame(uint8_t *out, uint8_t flags, uint8_t type, uint8_t seq, uint8_t ack, uint8_t class_1,
104 uint8_t class_2, const uint8_t *body, size_t len) {
105 const size_t total = HEADER_SIZE + 2 + len;
106 out[0] = FRAME_START_1;
107 out[1] = FRAME_START_2;
108 out[2] = flags;
109 out[3] = type;
110 out[4] = seq;
111 out[5] = ack;
112 out[6] = 0x00;
113 out[LEN_POS] = total;
114 out[HEADER_SIZE] = class_1;
115 out[HEADER_SIZE + 1] = class_2;
116 if (len)
117 std::memcpy(out + HEADER_SIZE + 2, body, len);
118 put_be16(out + CRC_POS, frame_crc(out, total));
119 return total;
120}
121
122} // namespace esphome::pioneer_wt
bool next()
Moves to the next attribute. Returns false at the end of the body or at an unknown ID.
uint16_t flags
uint8_t type
std::vector< uint8_t > & out
Definition gif.cpp:49
const uint8_t STORE_YAML_DATA[] PROGMEM
The zstd YAML envelope generated by store_yaml, STORE_YAML_DATA_SIZE bytes in flash.
size_t build_frame(uint8_t *out, uint8_t flags, uint8_t type, uint8_t seq, uint8_t ack, uint8_t class_1, uint8_t class_2, const uint8_t *body, size_t len)
Builds a frame into out, which must hold HEADER_SIZE + 2 + len bytes. Returns the frame length.
uint16_t frame_crc(const uint8_t *frame, size_t len)
CRC-16/XMODEM over a whole frame, skipping the two checksum bytes at offset 8.
const void size_t len
Definition hal.h:68
uint16_t progmem_read_uint16(const uint16_t *addr)
Definition hal.h:53
uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly, bool refin, bool refout)
Definition helpers.cpp:154
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:1235
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
Definition helpers.h:1296
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1227
uint16_t seq
uint8_t ack