ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ir_rf_base.cpp
Go to the documentation of this file.
1#include "ir_rf_base.h"
2
3#include <algorithm>
4#include <cinttypes>
5
6#include "esphome/core/log.h"
7
8#ifdef USE_API
12#endif
13
15
16ESPHOME_LOG_TAG(TAG, "ir_rf");
17
18#if defined(USE_API) && defined(USE_IR_RF)
19// A missing completion is answered as failed this long after the frame should have left the
20// wire, and an owed reply the client never reads is dropped this long after the first refusal
21static constexpr uint32_t API_REPLY_TIMEOUT_MS = 30000;
22#endif
23#ifdef USE_IR_RF_TRANSMIT_COMPLETE
24// Longest air time added to the deadline, in 16 ms ticks (8 min); with the 30 s above the
25// deadline stays within the signed 16 bit tick window loop() compares against
26static constexpr uint16_t API_REPLY_MAX_AIR_TICKS = 30000;
27#endif
28
29#if defined(USE_API) && defined(USE_IR_RF)
30static uint16_t api_reply_ticks_from_now() {
31 return static_cast<uint16_t>((App.get_loop_component_start_time() >> 4) + (API_REPLY_TIMEOUT_MS >> 4));
32}
33#endif
34
36 // merged, not assigned: a platform may have set a flag for its own hardware before setup()
37 if (this->has_transmitter())
38 this->supports_transmitter_ = true;
39 if (this->has_receiver())
40 this->supports_receiver_ = true;
41}
42
43bool IrRfEntity::transmit_raw_(const IrRfCallData &call, uint32_t carrier_frequency_hz) {
44 if (this->transmitter_ == nullptr) {
45 ESP_LOGW(TAG, "No transmitter configured");
46 return false;
47 }
48
49 if (!call.has_raw_timings()) {
50 ESP_LOGE(TAG, "No raw timings provided");
51 return false;
52 }
53
54 auto transmit_call = this->transmitter_->transmit();
55 auto *transmit_data = transmit_call.get_data();
56 transmit_data->set_carrier_frequency(carrier_frequency_hz);
57
58 if (call.is_packed()) {
59 // Zero-copy from packed protobuf data
60 transmit_data->set_data_from_packed_sint32(call.get_packed_data(), call.get_packed_length(),
61 call.get_packed_count());
62 ESP_LOGD(TAG, "Transmitting packed raw timings: count=%" PRIu16 ", repeat=%" PRIu32, call.get_packed_count(),
63 call.get_repeat_count());
64 } else if (call.is_base64url()) {
65 // Decode base64url (URL-safe) into transmit buffer
66 if (!transmit_data->set_data_from_base64url(call.get_base64url_data())) {
67 ESP_LOGE(TAG, "Invalid base64url data");
68 return false;
69 }
70 constexpr int32_t max_timing_us = 500000; // 500ms absolute max
71 for (int32_t timing : transmit_data->get_data()) {
72 int32_t abs_timing = timing < 0 ? -timing : timing;
73 if (abs_timing > max_timing_us) {
74 ESP_LOGE(TAG, "Invalid timing value: %" PRId32 " µs (max %" PRId32 ")", timing, max_timing_us);
75 return false;
76 }
77 }
78 ESP_LOGD(TAG, "Transmitting base64url raw timings: count=%zu, repeat=%" PRIu32, transmit_data->get_data().size(),
79 call.get_repeat_count());
80 } else {
81 // From vector (lambdas/automations)
82 transmit_data->set_data(call.get_raw_timings());
83 ESP_LOGD(TAG, "Transmitting raw timings: count=%zu, repeat=%" PRIu32, call.get_raw_timings().size(),
84 call.get_repeat_count());
85 }
86
87 // one answer for every backend: a frame that decoded to nothing is refused here
88 if (transmit_data->get_data().empty()) {
89 ESP_LOGE(TAG, "No raw timings provided");
90 return false;
91 }
92
93 if (call.get_repeat_count() > 0) {
94 transmit_call.set_send_times(call.get_repeat_count());
95 }
96
97#ifdef USE_IR_RF_TRANSMIT_COMPLETE
98 // only the API frame expect_api_reply_() armed claims the seq and extends its deadline
99 if (call.wants_api_reply() && this->api_reply_ == ApiReply::API_REPLY_WAITING) {
100 this->inflight_seq_ = transmit_call.get_seq();
101 // a long frame must not be answered as failed while still on the wire: the 30 s safety net
102 // starts after this frame's own air time (capped so the tick comparison cannot wrap)
103 uint64_t frame_us = 0;
104 for (const int64_t timing : transmit_data->get_data())
105 frame_us += timing < 0 ? -timing : timing;
106 const uint64_t air_ticks = (frame_us * std::max<uint32_t>(call.get_repeat_count(), 1) / 1000) >> 4;
107 this->api_reply_deadline_ += static_cast<uint16_t>(std::min<uint64_t>(air_ticks, API_REPLY_MAX_AIR_TICKS));
108 }
109#endif
110 transmit_call.perform();
111 return true;
112}
113
115#if defined(USE_API) && defined(USE_IR_RF)
116 // Internal entities are not listed to API clients, and their key may match a listed one
117 if (api::global_api_server != nullptr && !this->is_internal()) {
118#ifdef USE_DEVICES
119 uint32_t device_id = this->get_device_id();
120#else
121 uint32_t device_id = 0;
122#endif
124 }
125#endif
126 return false; // Don't consume the event, allow other listeners to process it
127}
128
129#if defined(USE_API) && defined(USE_IR_RF)
130
131#ifdef USE_IR_RF_TRANSMIT_COMPLETE
133 // only the frame this entity submitted; YAML automations and other entities share the transmitter
134 if (transmitter == this->transmitter_ && seq == this->inflight_seq_ &&
136 this->finish_api_reply_(sent);
137}
138#endif
139
141 // only an unpaced client gets here: its earlier request is answered as not started, and a reply
142 // the buffer still owes gets one last try; if that fails too the slot stays with it and the
143 // new request is refused rather than answered to nobody
145 this->finish_api_reply_(false);
146 if (this->api_reply_ != ApiReply::API_REPLY_NONE && !this->send_api_reply_()) {
147 ESP_LOGW(TAG, "'%s': transmit %s", LOG_STR_ARG(this->get_log_name()), LOG_STR_LITERAL("refused, reply still owed"));
148 this->refuse_api_call_(conn);
149 return false;
150 }
151 this->api_reply_connection_ = conn;
152 this->api_reply_deadline_ = api_reply_ticks_from_now();
154 return true;
155}
156
158 uint32_t device_id = 0;
159#ifdef USE_DEVICES
160 device_id = this->get_device_id();
161#endif
162 // no slot to retry from; a drop here is covered by the client's own timeout
163 if (!conn->send_infrared_rf_transmit_complete(device_id, this->get_object_id_hash(), false)) {
164 API_LOG_MSG_DROPPED(TAG, "IR/RF reply");
165 }
166}
167
170 if (!this->send_api_reply_()) {
171 // the retry gets its own window
172 this->api_reply_deadline_ = api_reply_ticks_from_now();
173 }
174}
175
177 uint32_t device_id = 0;
178#ifdef USE_DEVICES
179 device_id = this->get_device_id();
180#endif
181 // Refused by a full TCP buffer: the reply stays owed and loop() retries it, since a lost
182 // reply would stall the client's pacing for good (same shape as bluetooth_proxy)
183 if (!this->api_reply_connection_->send_infrared_rf_transmit_complete(device_id, this->get_object_id_hash(),
184 this->api_reply_ == ApiReply::API_REPLY_OWED_OK))
185 return false;
186 this->clear_api_reply_();
187 return true;
188}
189
190// Only runs while an API reply is pending: retries an owed one until it goes out or the client is
191// gone, and expires a transmit that never reported (a platform overriding control() without wiring
192// its transmitter's completion)
195 this->disable_loop();
196 return;
197 }
198 const auto remaining = static_cast<int16_t>(this->api_reply_deadline_ - (App.get_loop_component_start_time() >> 4));
200 if (this->send_api_reply_() || remaining > 0)
201 return;
202 ESP_LOGW(TAG, "'%s': transmit %s", LOG_STR_ARG(this->get_log_name()),
203 LOG_STR_LITERAL("reply dropped, client not reading"));
204 this->clear_api_reply_();
205 return;
206 }
207 if (remaining > 0)
208 return;
209 ESP_LOGW(TAG, "'%s': transmit %s", LOG_STR_ARG(this->get_log_name()), LOG_STR_LITERAL("never reported completion"));
210 this->finish_api_reply_(false);
211}
212
214 if (this->api_reply_connection_ == conn) {
215 this->clear_api_reply_();
216 }
217}
218#endif
219
220} // namespace esphome::ir_rf_base
221
222#ifdef USE_IR_RF_TRANSMIT_COMPLETE
224
225void ir_rf_transmit_complete(RemoteTransmitterBase *transmitter, uint16_t seq, bool sent) {
226#ifdef USE_INFRARED
227 for (auto *entity : App.get_infrareds()) {
228 entity->on_transmit_complete(transmitter, seq, sent);
229 }
230#endif
231#ifdef USE_RADIO_FREQUENCY
232 for (auto *entity : App.get_radio_frequencies()) {
233 entity->on_transmit_complete(transmitter, seq, sent);
234 }
235#endif
236}
237
238} // namespace esphome::remote_base
239#endif
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 disable_loop()
Disable this component's loop.
bool is_internal() const
uint32_t get_object_id_hash() const
uint32_t get_device_id() const
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
bool send_infrared_rf_transmit_complete(uint32_t device_id, uint32_t key, bool success)
void send_infrared_rf_receive_event(uint32_t device_id, uint32_t key, const std::vector< int32_t > *timings)
Raw timings of a transmit call, in one of three caller-owned forms.
Definition ir_rf_base.h:30
void on_transmit_complete(remote_base::RemoteTransmitterBase *transmitter, uint16_t seq, bool sent)
Called for every finished frame on every transmitter; answers the API request when the frame is the o...
void refuse_api_call_(api::APIConnection *conn)
api::APIConnection * api_reply_connection_
Definition ir_rf_base.h:165
bool on_receive(remote_base::RemoteReceiveData data) override
Forwards a received frame to the API; never consumes it, so other listeners still run.
bool expect_api_reply_(api::APIConnection *conn)
Claims the reply slot for conn; false when it still owes a reply that cannot be sent.
remote_base::RemoteTransmitterBase * transmitter_
Definition ir_rf_base.h:169
void setup() override
Reports the configured transports, listens on the receiver and hooks the transmitter's completion; a ...
void finish_api_reply_(bool success)
bool transmit_raw_(const IrRfCallData &call, uint32_t carrier_frequency_hz)
Hands the call's timings to the transmitter; the default transmit path of both entity types.
void on_api_connection_closed(api::APIConnection *conn)
The API server calls this when a client disconnects, so no reply goes to a stale pointer.
const RawTimings & get_raw_data() const
Definition remote_base.h:93
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:64
APIServer * global_api_server
ESPHOME_LOG_TAG(TAG, "ir_rf")
void ir_rf_transmit_complete(RemoteTransmitterBase *transmitter, uint16_t seq, bool sent)
Defined by ir_rf_base: answers the API request waiting on the entity that submitted seq; sent is fals...
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
uint16_t seq