ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
remote_base.cpp
Go to the documentation of this file.
1#include "remote_base.h"
3#include "esphome/core/log.h"
4
5namespace esphome::remote_base {
6
7ESPHOME_LOG_TAG(TAG, "remote_base");
8
9/* RemoteReceiveData */
10
12 if (!this->is_valid(offset))
13 return false;
14 const int32_t value = this->peek(offset);
15 const int32_t lo = this->lower_bound_(length);
16 const int32_t hi = this->upper_bound_(length);
17 return value >= 0 && lo <= value && value <= hi;
18}
19
21 if (!this->is_valid(offset))
22 return false;
23 const int32_t value = this->peek(offset);
24 const int32_t lo = this->lower_bound_(length);
25 return value >= 0 && lo <= value;
26}
27
29 if (!this->is_valid(offset))
30 return false;
31 const int32_t value = this->peek(offset);
32 const int32_t hi = this->upper_bound_(length);
33 return value >= 0 && value <= hi;
34}
35
37 if (!this->is_valid(offset))
38 return false;
39 const int32_t value = this->peek(offset);
40 const int32_t lo = this->lower_bound_(length);
41 const int32_t hi = this->upper_bound_(length);
42 return value <= 0 && lo <= -value && -value <= hi;
43}
44
46 if (!this->is_valid(offset))
47 return false;
48 const int32_t value = this->peek(offset);
49 const int32_t lo = this->lower_bound_(length);
50 return value <= 0 && lo <= -value;
51}
52
54 if (!this->is_valid(offset))
55 return false;
56 const int32_t value = this->peek(offset);
57 const int32_t hi = this->upper_bound_(length);
58 return value <= 0 && -value <= hi;
59}
60
62 if (!this->peek_mark(length))
63 return false;
64 this->advance();
65 return true;
66}
67
69 if (!this->peek_space(length))
70 return false;
71 this->advance();
72 return true;
73}
74
76 if (!this->peek_item(mark, space))
77 return false;
78 this->advance(2);
79 return true;
80}
81
83 if (!this->peek_space_at_least(space, 1) || !this->peek_mark(mark))
84 return false;
85 this->advance(2);
86 return true;
87}
88
89/* RemoteReceiverBinarySensorBase */
90
92 if (!this->matches(src))
93 return false;
94 this->publish_state(true);
95 yield();
96 this->publish_state(false);
97 return true;
98}
99
100/* RemoteReceiverBase */
101
102// Slots are counted at code generation; a registration from C++ setup() has none
103#ifdef REMOTE_BASE_LISTENER_COUNT
105 if (this->listeners_.size() == REMOTE_BASE_LISTENER_COUNT) {
106 ESP_LOGE(TAG, "No listener slot: register it from to_code() with remote_base.add_listener");
107 return;
108 }
109 this->listeners_.push_back(listener);
110}
111#endif
112
113#ifdef REMOTE_BASE_DUMPER_COUNT
115 if (this->dumpers_.empty()) {
116 this->dumpers_.assign_static(dumpers, count);
117 } else {
118 // register_dumper() ran first from another to_code(); the YAML dumpers stay first
119 this->dumpers_.prepend_copy(dumpers, count);
120 }
121}
122
123void RemoteReceiverBase::register_dumper(RemoteReceiverDumperBase *dumper) { this->dumpers_.append_copy(&dumper, 1); }
124
125// Only the first secondary dumper runs; from dump_config() so it is seen however the dumpers were registered
127 uint8_t secondaries = 0;
128 for (auto *dumper : this->dumpers_)
129 secondaries += dumper->is_secondary();
130 if (secondaries > 1) {
131 ESP_LOGW(TAG, "Only the first of %u secondary dumpers runs", secondaries);
132 }
133}
134#endif
135
137#ifdef REMOTE_BASE_LISTENER_COUNT
138 for (auto *listener : this->listeners_)
139 listener->on_receive(RemoteReceiveData(this->temp_, this->tolerance_, this->tolerance_mode_));
140#endif
141#ifdef REMOTE_BASE_DUMPER_COUNT
142 bool success = false;
143 RemoteReceiverDumperBase *secondary = nullptr;
144 for (auto *dumper : this->dumpers_) {
145 if (dumper->is_secondary()) {
146 if (secondary == nullptr)
147 secondary = dumper;
148 } else if (dumper->dump(RemoteReceiveData(this->temp_, this->tolerance_, this->tolerance_mode_))) {
149 success = true;
150 }
151 }
152 if (!success && secondary != nullptr)
153 secondary->dump(RemoteReceiveData(this->temp_, this->tolerance_, this->tolerance_mode_));
154#endif
155}
156
157void RemoteReceiverBinarySensorBase::dump_config() { LOG_BINARY_SENSOR("", "Remote Receiver Binary Sensor", this); }
158
159/* RemoteTransmitData */
160
161void RemoteTransmitData::set_data_from_packed_sint32(const uint8_t *data, size_t len, size_t count) {
162 this->data_.clear();
163 this->data_.reserve(count);
164
165 while (len > 0) {
166 // Parse varint (inline, no dependency on api component)
167 uint32_t raw = 0;
168 uint32_t shift = 0;
169 uint32_t consumed = 0;
170 for (; consumed < len && consumed < 5; consumed++) {
171 uint8_t byte = data[consumed];
172 raw |= (byte & 0x7F) << shift;
173 if ((byte & 0x80) == 0) {
174 consumed++;
175 break;
176 }
177 shift += 7;
178 }
179 if (consumed == 0)
180 break; // Parse error
181
182 // Zigzag decode: (n >> 1) ^ -(n & 1)
183 int32_t decoded = static_cast<int32_t>((raw >> 1) ^ (~(raw & 1) + 1));
184 this->data_.push_back(decoded);
185 data += consumed;
186 len -= consumed;
187 }
188}
189
190bool RemoteTransmitData::set_data_from_base64url(const std::string &base64url) {
191 return base64_decode_int32_vector(base64url, this->data_);
192}
193
194/* RemoteTransmitterBase */
195
200
201void RemoteTransmitterBase::transmit_encoded_(EncodeFn encode, const void *data, uint32_t send_times,
202 uint32_t send_wait) {
203 auto call = this->transmit();
204 encode(call.get_data(), data);
205 call.set_send_times(send_times);
206 call.set_send_wait(send_wait);
207 call.perform();
208}
209
210void RemoteTransmitterBase::send_(uint32_t send_times, uint32_t send_wait, [[maybe_unused]] uint16_t seq) {
211#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
212 const auto &vec = this->temp_.get_data();
213 char buffer[256];
214 size_t pos = buf_append_printf(buffer, sizeof(buffer), 0,
215 "Sending times=%" PRIu32 " wait=%" PRIu32 "ms: ", send_times, send_wait);
216
217 for (size_t i = 0; i < vec.size(); i++) {
218 const int32_t value = vec[i];
219 size_t prev_pos = pos;
220
221 if (i + 1 < vec.size()) {
222 pos = buf_append_printf(buffer, sizeof(buffer), pos, "%" PRId32 ", ", value);
223 } else {
224 pos = buf_append_printf(buffer, sizeof(buffer), pos, "%" PRId32, value);
225 }
226
227 if (pos >= sizeof(buffer) - 1) {
228 // buffer full, flush and continue
229 buffer[prev_pos] = '\0';
230 ESP_LOGVV(TAG, "%s", buffer);
231 if (i + 1 < vec.size()) {
232 pos = buf_append_printf(buffer, sizeof(buffer), 0, " %" PRId32 ", ", value);
233 } else {
234 pos = buf_append_printf(buffer, sizeof(buffer), 0, " %" PRId32, value);
235 }
236 }
237 }
238 if (pos != 0) {
239 ESP_LOGVV(TAG, "%s", buffer);
240 }
241#endif
243#ifdef USE_IR_RF_TRANSMIT_COMPLETE
244 this->current_seq_ = seq;
245#endif
246 this->send_internal(send_times, send_wait);
247}
248} // namespace esphome::remote_base
uint8_t raw[35]
Definition bl0939.h:0
void publish_state(bool new_state)
Publish a new state to the front-end.
bool peek_mark_at_most(uint32_t length, uint32_t offset=0) const
bool expect_item(uint32_t mark, uint32_t space)
bool peek_space(uint32_t length, uint32_t offset=0) const
bool peek_space_at_most(uint32_t length, uint32_t offset=0) const
int32_t peek(uint32_t offset=0) const
Definition remote_base.h:98
bool peek_item(uint32_t mark, uint32_t space, uint32_t offset=0) const
int32_t upper_bound_(uint32_t length) const
bool is_valid(uint32_t offset=0) const
Definition remote_base.h:97
bool peek_space_at_least(uint32_t length, uint32_t offset=0) const
int32_t lower_bound_(uint32_t length) const
bool peek_mark(uint32_t length, uint32_t offset=0) const
bool expect_pulse_with_gap(uint32_t mark, uint32_t space)
bool peek_mark_at_least(uint32_t length, uint32_t offset=0) const
void register_dumper(RemoteReceiverDumperBase *dumper)
For C++ registration, also from setup(); each call copies the list to the heap.
ConstVector< RemoteReceiverDumperBase *, true > dumpers_
void set_dumpers(RemoteReceiverDumperBase *const *dumpers, size_t count)
Codegen only (remote_base.add_dumper): the table must outlive this receiver.
void register_listener(RemoteReceiverListener *listener)
StaticVector< RemoteReceiverListener *, REMOTE_BASE_LISTENER_COUNT > listeners_
virtual bool matches(RemoteReceiveData src)=0
bool on_receive(RemoteReceiveData src) override
virtual bool dump(RemoteReceiveData src)=0
bool set_data_from_base64url(const std::string &base64url)
Set data from base64url-encoded little-endian int32 values Base64url is URL-safe: uses '-' instead of...
const RawTimings & get_data() const
Definition remote_base.h:66
void set_data_from_packed_sint32(const uint8_t *data, size_t len, size_t count)
Set data from packed protobuf sint32 buffer (zigzag + varint encoded)
void send_(uint32_t send_times, uint32_t send_wait, uint16_t seq)
void(*)(RemoteTransmitData *dst, const void *data) EncodeFn
virtual void send_internal(uint32_t send_times, uint32_t send_wait)=0
void transmit_encoded_(EncodeFn encode, const void *data, uint32_t send_times, uint32_t send_wait)
RemoteTransmitData temp_
Use same vector for all transmits, avoids many allocations.
virtual void flush_pending_completion()
Platforms that report completion later wait out the previous frame here, before send_() assigns the n...
void yield(void)
unsigned count
Definition gif.cpp:51
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "remote.abbwelcome")
uint32_t value
Definition hal.cpp:41
bool base64_decode_int32_vector(const std::string &base64, std::vector< int32_t > &out)
Decode base64/base64url string directly into vector of little-endian int32 values.
Definition helpers.cpp:732
const void size_t len
Definition hal.h:68
size_t size_t pos
Definition helpers.h:1333
const void * src
Definition hal.h:68
static void uint32_t
uint16_t seq
uint16_t length
Definition tt21100.cpp:0