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