ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
abbwelcome_protocol.h
Go to the documentation of this file.
1#pragma once
2
5#include "remote_base.h"
6#include <array>
7#include <cinttypes>
8#include <utility>
9#include <vector>
10
11namespace esphome {
12namespace remote_base {
13
14static const uint8_t MAX_DATA_LENGTH = 15;
15static const uint8_t DATA_LENGTH_MASK = 0x3f;
16
17/*
18Message Format:
19 2 bytes: Sync (0x55FF)
20 1 bit: Retransmission flag (High means retransmission)
21 1 bit: Address length flag (Low means 2 bytes, High means 3 bytes)
22 2 bits: Unknown
23 4 bits: Data length (in bytes)
24 1 bit: Reply flag (High means this is a reply to a previous message with the same message type)
25 7 bits: Message type
26 2-3 bytes: Destination address
27 2-3 bytes: Source address
28 1 byte: Message ID (randomized, does not change for retransmissions)
29 0-? bytes: Data
30 1 byte: Checksum
31*/
32
34 public:
35 // Make default
36 ABBWelcomeData() : data_{0x55, 0xff} {}
37 // Make from initializer_list
38 ABBWelcomeData(std::initializer_list<uint8_t> data) : data_{} {
39 std::copy_n(data.begin(), std::min(data.size(), this->data_.size()), this->data_.begin());
40 }
41 // Make from vector
42 ABBWelcomeData(const std::vector<uint8_t> &data) : data_{} {
43 std::copy_n(data.begin(), std::min(data.size(), this->data_.size()), this->data_.begin());
44 }
45 // Default copy constructor
46 ABBWelcomeData(const ABBWelcomeData &) = default;
47
48 bool auto_message_id{false};
49
50 uint8_t *data() { return this->data_.data(); }
51 const uint8_t *data() const { return this->data_.data(); }
52 uint8_t size() const {
53 return std::min(static_cast<uint8_t>(6 + (2 * this->get_address_length()) + (this->data_[2] & DATA_LENGTH_MASK)),
54 static_cast<uint8_t>(this->data_.size()));
55 }
56 bool is_valid() const {
57 return this->data_[0] == 0x55 && this->data_[1] == 0xff &&
58 ((this->data_[2] & DATA_LENGTH_MASK) <= MAX_DATA_LENGTH) &&
59 (this->data_[this->size() - 1] == this->calc_cs_());
60 }
61 void set_retransmission(bool retransmission) {
62 if (retransmission) {
63 this->data_[2] |= 0x80;
64 } else {
65 this->data_[2] &= 0x7f;
66 }
67 }
68 bool get_retransmission() const { return this->data_[2] & 0x80; }
69 // set_three_byte_address must be called before set_source_address, set_destination_address, set_message_id and
70 // set_data!
71 void set_three_byte_address(bool three_byte_address) {
72 if (three_byte_address) {
73 this->data_[2] |= 0x40;
74 } else {
75 this->data_[2] &= 0xbf;
76 }
77 }
78 uint8_t get_three_byte_address() const { return (this->data_[2] & 0x40); }
79 uint8_t get_address_length() const { return this->get_three_byte_address() ? 3 : 2; }
80 void set_message_type(uint8_t message_type) { this->data_[3] = message_type; }
81 uint8_t get_message_type() const { return this->data_[3]; }
83 if (this->get_address_length() == 2) {
84 this->data_[4] = (address >> 8) & 0xff;
85 this->data_[5] = address & 0xff;
86 } else {
87 this->data_[4] = (address >> 16) & 0xff;
88 this->data_[5] = (address >> 8) & 0xff;
89 this->data_[6] = address & 0xff;
90 }
91 }
92 uint32_t get_destination_address() const {
93 if (this->get_address_length() == 2) {
94 return (this->data_[4] << 8) + this->data_[5];
95 }
96 return (this->data_[4] << 16) + (this->data_[5] << 8) + this->data_[6];
97 }
98 void set_source_address(uint32_t address) {
99 if (this->get_address_length() == 2) {
100 this->data_[6] = (address >> 8) & 0xff;
101 this->data_[7] = address & 0xff;
102 } else {
103 this->data_[7] = (address >> 16) & 0xff;
104 this->data_[8] = (address >> 8) & 0xff;
105 this->data_[9] = address & 0xff;
106 }
107 }
108 uint32_t get_source_address() const {
109 if (this->get_address_length() == 2) {
110 return (this->data_[6] << 8) + this->data_[7];
111 }
112 return (this->data_[7] << 16) + (this->data_[8] << 8) + this->data_[9];
113 }
114 void set_message_id(uint8_t message_id) { this->data_[4 + 2 * this->get_address_length()] = message_id; }
115 uint8_t get_message_id() const { return this->data_[4 + 2 * this->get_address_length()]; }
116 void set_data(std::vector<uint8_t> data) {
117 uint8_t size = std::min(MAX_DATA_LENGTH, static_cast<uint8_t>(data.size()));
118 this->data_[2] &= (0xff ^ DATA_LENGTH_MASK);
119 this->data_[2] |= (size & DATA_LENGTH_MASK);
120 if (size)
121 std::copy_n(data.begin(), size, this->data_.begin() + 5 + 2 * this->get_address_length());
122 }
123 std::vector<uint8_t> get_data() const {
124 std::vector<uint8_t> data(this->data_.begin() + 5 + 2 * this->get_address_length(),
125 this->data_.begin() + 5 + 2 * this->get_address_length() + this->get_data_size());
126 return data;
127 }
128 uint8_t get_data_size() const {
129 return std::min(MAX_DATA_LENGTH, static_cast<uint8_t>(this->data_[2] & DATA_LENGTH_MASK));
130 }
131 void finalize() {
132 if (this->auto_message_id && !this->get_retransmission() && !(this->data_[3] & 0x80)) {
133 this->set_message_id(static_cast<uint8_t>(random_uint32()));
134 }
135 this->data_[0] = 0x55;
136 this->data_[1] = 0xff;
137 this->data_[this->size() - 1] = this->calc_cs_();
138 }
139 std::string to_string(uint8_t max_print_bytes = 255) const {
140 std::string info;
141 if (this->is_valid()) {
142 info = str_sprintf(this->get_three_byte_address() ? "[%06" PRIX32 " %s %06" PRIX32 "] Type: %02X"
143 : "[%04" PRIX32 " %s %04" PRIX32 "] Type: %02X",
144 this->get_source_address(), this->get_retransmission() ? "ยป" : ">",
146 if (this->get_data_size())
147 info += str_sprintf(", Data: %s", format_hex_pretty(this->get_data()).c_str());
148 } else {
149 info = "[Invalid]";
150 }
151 uint8_t print_bytes = std::min(this->size(), max_print_bytes);
152 if (print_bytes)
153 info = str_sprintf("%s %s", format_hex_pretty(this->data_.data(), print_bytes).c_str(), info.c_str());
154 return info;
155 }
156 bool operator==(const ABBWelcomeData &rhs) const {
157 if (std::equal(this->data_.begin(), this->data_.begin() + this->size(), rhs.data_.begin()))
158 return true;
159 return (this->auto_message_id || rhs.auto_message_id) && this->is_valid() && rhs.is_valid() &&
160 (this->get_message_type() == rhs.get_message_type()) &&
161 (this->get_source_address() == rhs.get_source_address()) &&
162 (this->get_destination_address() == rhs.get_destination_address()) && (this->get_data() == rhs.get_data());
163 }
164 uint8_t &operator[](size_t idx) { return this->data_[idx]; }
165 const uint8_t &operator[](size_t idx) const { return this->data_[idx]; }
166
167 protected:
168 std::array<uint8_t, 12 + MAX_DATA_LENGTH> data_;
169 // Calculate checksum
170 uint8_t calc_cs_() const;
171};
172
173class ABBWelcomeProtocol : public RemoteProtocol<ABBWelcomeData> {
174 public:
175 void encode(RemoteTransmitData *dst, const ABBWelcomeData &src) override;
177 void dump(const ABBWelcomeData &data) override;
178
179 protected:
180 void encode_byte_(RemoteTransmitData *dst, uint8_t data) const;
181 bool decode_byte_(RemoteReceiveData &src, bool &done, uint8_t &data);
182};
183
185 public:
186 bool matches(RemoteReceiveData src) override {
187 auto data = ABBWelcomeProtocol().decode(src);
188 return data.has_value() && data.value() == this->data_;
189 }
190 void set_source_address(const uint32_t source_address) { this->data_.set_source_address(source_address); }
191 void set_destination_address(const uint32_t destination_address) {
192 this->data_.set_destination_address(destination_address);
193 }
194 void set_retransmission(const bool retransmission) { this->data_.set_retransmission(retransmission); }
195 void set_three_byte_address(const bool three_byte_address) { this->data_.set_three_byte_address(three_byte_address); }
196 void set_message_type(const uint8_t message_type) { this->data_.set_message_type(message_type); }
197 void set_message_id(const uint8_t message_id) { this->data_.set_message_id(message_id); }
198 void set_auto_message_id(const bool auto_message_id) { this->data_.auto_message_id = auto_message_id; }
199 void set_data(const std::vector<uint8_t> &data) { this->data_.set_data(data); }
200 void finalize() { this->data_.finalize(); }
201
202 protected:
204};
205
208
209template<typename... Ts> class ABBWelcomeAction : public RemoteTransmitterActionBase<Ts...> {
210 TEMPLATABLE_VALUE(uint32_t, source_address)
211 TEMPLATABLE_VALUE(uint32_t, destination_address)
212 TEMPLATABLE_VALUE(bool, retransmission)
213 TEMPLATABLE_VALUE(bool, three_byte_address)
214 TEMPLATABLE_VALUE(uint8_t, message_type)
215 TEMPLATABLE_VALUE(uint8_t, message_id)
216 TEMPLATABLE_VALUE(bool, auto_message_id)
217 void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
218 this->data_.func = func;
219 this->len_ = -1; // Sentinel value indicates template mode
220 }
221 void set_data_static(const uint8_t *data, size_t len) {
222 this->data_.data = data;
223 this->len_ = len; // Length >= 0 indicates static mode
224 }
225 void encode(RemoteTransmitData *dst, Ts... x) override {
226 ABBWelcomeData data;
227 data.set_three_byte_address(this->three_byte_address_.value(x...));
228 data.set_source_address(this->source_address_.value(x...));
229 data.set_destination_address(this->destination_address_.value(x...));
230 data.set_retransmission(this->retransmission_.value(x...));
231 data.set_message_type(this->message_type_.value(x...));
232 data.set_message_id(this->message_id_.value(x...));
233 data.auto_message_id = this->auto_message_id_.value(x...);
234 std::vector<uint8_t> data_vec;
235 if (this->len_ >= 0) {
236 // Static mode: copy from flash to vector
237 data_vec.assign(this->data_.data, this->data_.data + this->len_);
238 } else {
239 // Template mode: call function
240 data_vec = this->data_.func(x...);
241 }
242 data.set_data(data_vec);
243 data.finalize();
244 ABBWelcomeProtocol().encode(dst, data);
245 }
246
247 protected:
248 ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length
249 union Data {
250 std::vector<uint8_t> (*func)(Ts...); // Function pointer (stateless lambdas)
251 const uint8_t *data; // Pointer to static data in flash
253};
254
255} // namespace remote_base
256} // namespace esphome
uint8_t address
Definition bl0906.h:4
union esphome::remote_base::ABBWelcomeAction::Data data_
bool matches(RemoteReceiveData src) override
void set_retransmission(const bool retransmission)
void set_destination_address(const uint32_t destination_address)
void set_auto_message_id(const bool auto_message_id)
void set_message_type(const uint8_t message_type)
void set_data(const std::vector< uint8_t > &data)
void set_source_address(const uint32_t source_address)
void set_three_byte_address(const bool three_byte_address)
void set_data(std::vector< uint8_t > data)
bool operator==(const ABBWelcomeData &rhs) const
ABBWelcomeData(std::initializer_list< uint8_t > data)
void set_retransmission(bool retransmission)
void set_destination_address(uint32_t address)
void set_three_byte_address(bool three_byte_address)
const uint8_t & operator[](size_t idx) const
std::array< uint8_t, 12+MAX_DATA_LENGTH > data_
std::vector< uint8_t > get_data() const
void set_message_type(uint8_t message_type)
std::string to_string(uint8_t max_print_bytes=255) const
ABBWelcomeData(const ABBWelcomeData &)=default
ABBWelcomeData(const std::vector< uint8_t > &data)
void encode(RemoteTransmitData *dst, const ABBWelcomeData &src) override
void encode_byte_(RemoteTransmitData *dst, uint8_t data) const
void dump(const ABBWelcomeData &data) override
bool decode_byte_(RemoteReceiveData &src, bool &done, uint8_t &data)
optional< ABBWelcomeData > decode(RemoteReceiveData src) override
__int64 ssize_t
Definition httplib.h:178
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:500
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:17
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
Definition helpers.cpp:317
std::string str_sprintf(const char *fmt,...)
Definition helpers.cpp:222
uint16_t x
Definition tt21100.cpp:5