ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
rf_bridge.cpp
Go to the documentation of this file.
1#include "rf_bridge.h"
4#include "esphome/core/log.h"
5#include <cinttypes>
6#include <cstring>
7
9
10static const char *const TAG = "rf_bridge";
11
13 ESP_LOGV(TAG, "Sending ACK");
14 this->write(RF_CODE_START);
15 this->write(RF_CODE_ACK);
16 this->write(RF_CODE_STOP);
17 this->flush();
18}
19
21 size_t at = this->rx_buffer_.size();
22 this->rx_buffer_.push_back(byte);
23 const uint8_t *raw = &this->rx_buffer_[0];
24
25 ESP_LOGVV(TAG, "Processing byte: 0x%02X", byte);
26
27 // Byte 0: Start
28 if (at == 0)
29 return byte == RF_CODE_START;
30
31 // Byte 1: Action
32 if (at == 1)
33 return byte >= RF_CODE_ACK && byte <= RF_CODE_RFIN_BUCKET;
34 uint8_t action = raw[1];
35
36 switch (action) {
37 case RF_CODE_ACK:
38 ESP_LOGD(TAG, "Action OK");
39 break;
40 case RF_CODE_LEARN_KO:
41 ESP_LOGD(TAG, "Learning timeout");
42 break;
43 case RF_CODE_LEARN_OK:
44 case RF_CODE_RFIN: {
45 if (byte != RF_CODE_STOP || at < RF_MESSAGE_SIZE + 2)
46 return true;
47
48 RFBridgeData data;
49 data.sync = (raw[2] << 8) | raw[3];
50 data.low = (raw[4] << 8) | raw[5];
51 data.high = (raw[6] << 8) | raw[7];
52 data.code = (raw[8] << 16) | (raw[9] << 8) | raw[10];
53
54 if (action == RF_CODE_LEARN_OK) {
55 ESP_LOGD(TAG, "Learning success");
56 }
57
58 ESP_LOGI(TAG,
59 "Received RFBridge Code: sync=0x%04" PRIX16 " low=0x%04" PRIX16 " high=0x%04" PRIX16
60 " code=0x%06" PRIX32,
61 data.sync, data.low, data.high, data.code);
62 this->data_callback_.call(data);
63 break;
64 }
65 case RF_CODE_LEARN_OK_NEW:
66 case RF_CODE_ADVANCED_RFIN: {
67 if (byte != RF_CODE_STOP) {
68 return at < (raw[2] + 3);
69 }
70
72
73 data.length = raw[2];
74 data.protocol = raw[3];
75 char next_byte[3]; // 2 hex chars + null
76 for (uint8_t i = 0; i + 1 < data.length; i++) {
77 buf_append_printf(next_byte, sizeof(next_byte), 0, "%02X", raw[4 + i]);
78 data.code += next_byte;
79 }
80
81 ESP_LOGI(TAG, "Received RFBridge Advanced Code: length=0x%02X protocol=0x%02X code=0x%s", data.length,
82 data.protocol, data.code.c_str());
83 this->advanced_data_callback_.call(data);
84 break;
85 }
86 case RF_CODE_RFIN_BUCKET: {
87 if (byte != RF_CODE_STOP) {
88 return true;
89 }
90
91 uint8_t buckets = raw[2] << 1;
92 std::string str;
93 char next_byte[3]; // 2 hex chars + null
94
95 for (uint32_t i = 0; i <= at; i++) {
96 buf_append_printf(next_byte, sizeof(next_byte), 0, "%02X", raw[i]);
97 str += next_byte;
98 if ((i > 3) && buckets) {
99 buckets--;
100 }
101 if ((i < 3) || (buckets % 2) || (i == at - 1)) {
102 str += " ";
103 }
104 }
105 ESP_LOGI(TAG, "Received RFBridge Bucket: %s", str.c_str());
106 break;
107 }
108 default:
109 ESP_LOGW(TAG, "Unknown action: 0x%02X", action);
110 break;
111 }
112
113 ESP_LOGVV(TAG, "Parsed: 0x%02X", byte);
114
115 if (byte == RF_CODE_STOP && action != RF_CODE_ACK)
116 this->ack_();
117
118 // return false to reset buffer
119 return false;
120}
121
122void RFBridgeComponent::write_byte_str_(const std::string &codes) {
123 uint8_t code;
124 int size = codes.length();
125 for (int i = 0; i < size; i += 2) {
126 code = strtol(codes.substr(i, 2).c_str(), nullptr, 16);
127 this->write(code);
128 }
129}
130
133 if (now - this->last_bridge_byte_ > 50) {
134 this->rx_buffer_.clear();
135 this->last_bridge_byte_ = now;
136 }
137
138 size_t avail = this->available();
139 while (avail > 0) {
140 uint8_t buf[64];
141 size_t to_read = std::min(avail, sizeof(buf));
142 if (!this->read_array(buf, to_read)) {
143 break;
144 }
145 avail -= to_read;
146 for (size_t i = 0; i < to_read; i++) {
147 if (this->rx_buffer_.size() > MAX_RX_BUFFER_SIZE) {
148 this->rx_buffer_.clear();
149 }
150 if (this->parse_bridge_byte_(buf[i])) {
151 ESP_LOGVV(TAG, "Parsed: 0x%02X", buf[i]);
152 this->last_bridge_byte_ = now;
153 } else {
154 this->rx_buffer_.clear();
155 }
156 }
157 }
158}
159
161 ESP_LOGD(TAG, "Sending code: sync=0x%04" PRIX16 " low=0x%04" PRIX16 " high=0x%04" PRIX16 " code=0x%06" PRIX32,
162 data.sync, data.low, data.high, data.code);
163 this->write(RF_CODE_START);
164 this->write(RF_CODE_RFOUT);
165 this->write((data.sync >> 8) & 0xFF);
166 this->write(data.sync & 0xFF);
167 this->write((data.low >> 8) & 0xFF);
168 this->write(data.low & 0xFF);
169 this->write((data.high >> 8) & 0xFF);
170 this->write(data.high & 0xFF);
171 this->write((data.code >> 16) & 0xFF);
172 this->write((data.code >> 8) & 0xFF);
173 this->write(data.code & 0xFF);
174 this->write(RF_CODE_STOP);
175 this->flush();
176}
177
179 ESP_LOGD(TAG, "Sending advanced code: length=0x%02X protocol=0x%02X code=0x%s", data.length, data.protocol,
180 data.code.c_str());
181 this->write(RF_CODE_START);
182 this->write(RF_CODE_RFOUT_NEW);
183 this->write(data.length & 0xFF);
184 this->write(data.protocol & 0xFF);
185 this->write_byte_str_(data.code);
186 this->write(RF_CODE_STOP);
187 this->flush();
188}
189
191 ESP_LOGD(TAG, "Learning mode");
192 this->write(RF_CODE_START);
193 this->write(RF_CODE_LEARN);
194 this->write(RF_CODE_STOP);
195 this->flush();
196}
197
199 ESP_LOGCONFIG(TAG, "RF_Bridge:");
200 this->check_uart_settings(19200);
201}
202
204 ESP_LOGI(TAG, "Advanced Sniffing on");
205 this->write(RF_CODE_START);
206 this->write(RF_CODE_SNIFFING_ON);
207 this->write(RF_CODE_STOP);
208 this->flush();
209}
210
212 ESP_LOGI(TAG, "Advanced Sniffing off");
213 this->write(RF_CODE_START);
214 this->write(RF_CODE_SNIFFING_OFF);
215 this->write(RF_CODE_STOP);
216 this->flush();
217}
218
220 ESP_LOGI(TAG, "Raw Bucket Sniffing on");
221 this->write(RF_CODE_START);
222 this->write(RF_CODE_RFIN_BUCKET);
223 this->write(RF_CODE_STOP);
224 this->flush();
225}
226
227void RFBridgeComponent::send_raw(const std::string &raw_code) {
228 ESP_LOGD(TAG, "Sending Raw Code: %s", raw_code.c_str());
229
230 this->write_byte_str_(raw_code);
231 this->flush();
232}
233
234void RFBridgeComponent::beep(uint16_t ms) {
235 ESP_LOGD(TAG, "Beeping for %hu ms", ms);
236
237 this->write(RF_CODE_START);
238 this->write(RF_CODE_BEEP);
239 this->write((ms >> 8) & 0xFF);
240 this->write(ms & 0xFF);
241 this->write(RF_CODE_STOP);
242 this->flush();
243}
244
245} // namespace esphome::rf_bridge
uint8_t raw[35]
Definition bl0939.h:0
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void send_raw(const std::string &code)
CallbackManager< void(RFBridgeAdvancedData)> advanced_data_callback_
Definition rf_bridge.h:76
void send_advanced_code(const RFBridgeAdvancedData &data)
CallbackManager< void(RFBridgeData)> data_callback_
Definition rf_bridge.h:75
void write_byte_str_(const std::string &codes)
void send_code(RFBridgeData data)
std::vector< uint8_t > rx_buffer_
Definition rf_bridge.h:72
UARTFlushResult flush()
Definition uart.h:48
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition uart.cpp:16
size_t write(uint8_t data)
Definition uart.h:57
uint16_t size
Definition helpers.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t