ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
5#include <utility>
6#include <vector>
7
11#include "esphome/core/log.h"
12
13// Maximum bytes to log in hex format for BLE writes (many logging buffers are 256 chars)
14static constexpr size_t BLE_WRITE_MAX_LOG_BYTES = 64;
15
16namespace esphome::ble_client {
17
18// placeholder class for static TAG .
20 public:
21 // could be made inline with C++17
22 static const char *const TAG;
23};
24
25// implement on_connect automation.
27 public:
29 void loop() override {}
30 void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
31 esp_ble_gattc_cb_param_t *param) override {
32 if (event == ESP_GATTC_SEARCH_CMPL_EVT) {
33 this->node_state = espbt::ClientState::ESTABLISHED;
34 this->trigger();
35 }
36 }
37};
38
39// on_disconnect automation
41 public:
43 void loop() override {}
44 void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
45 esp_ble_gattc_cb_param_t *param) override {
46 // test for CLOSE and not DISCONNECT - DISCONNECT can occur even if no virtual connection (OPEN event) occurred.
47 // So this will not trigger unless a complete open has previously succeeded.
48 switch (event) {
49 case ESP_GATTC_SEARCH_CMPL_EVT: {
50 this->node_state = espbt::ClientState::ESTABLISHED;
51 break;
52 }
53 case ESP_GATTC_CLOSE_EVT: {
54 this->trigger();
55 break;
56 }
57 default: {
58 break;
59 }
60 }
61 }
62};
63
65 public:
67 void loop() override {}
68 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override {
69 if (event == ESP_GAP_BLE_PASSKEY_REQ_EVT && this->parent_->check_addr(param->ble_security.auth_cmpl.bd_addr))
70 this->trigger();
71 }
72};
73
75 public:
77 void loop() override {}
78 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override {
79 if (event == ESP_GAP_BLE_PASSKEY_NOTIF_EVT && this->parent_->check_addr(param->ble_security.auth_cmpl.bd_addr)) {
80 this->trigger(param->ble_security.key_notif.passkey);
81 }
82 }
83};
84
86 public:
88 void loop() override {}
89 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override {
90 if (event == ESP_GAP_BLE_NC_REQ_EVT && this->parent_->check_addr(param->ble_security.auth_cmpl.bd_addr)) {
91 this->trigger(param->ble_security.key_notif.passkey);
92 }
93 }
94};
95
96// implement the ble_client.ble_write action.
97template<typename... Ts> class BLEClientWriteAction : public Action<Ts...>, public BLEClientNode {
98 public:
100 ble_client->register_ble_node(this);
101 ble_client_ = ble_client;
102 }
103
104 void set_service_uuid16(uint16_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
105 void set_service_uuid32(uint32_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
106 void set_service_uuid128(uint8_t *uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
107
108 void set_char_uuid16(uint16_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
109 void set_char_uuid32(uint32_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
110 void set_char_uuid128(uint8_t *uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
111
112 void set_value_template(std::vector<uint8_t> (*func)(Ts...)) {
113 this->value_.func = func;
114 this->len_ = -1; // Sentinel value indicates template mode
115 }
116
117 // Store pointer to static data in flash (no RAM copy)
118 void set_value_simple(const uint8_t *data, size_t len) {
119 this->value_.data = data;
120 this->len_ = len; // Length >= 0 indicates static mode
121 }
122
123 void play(const Ts &...x) override {}
124
125 void play_complex(const Ts &...x) override {
126 this->num_running_++;
127 this->var_ = std::make_tuple(x...);
128
129 bool result;
130 if (this->len_ >= 0) {
131 // Static mode: write directly from flash pointer
132 result = this->write(this->value_.data, this->len_);
133 } else {
134 // Template mode: call function and write the vector
135 std::vector<uint8_t> value = this->value_.func(x...);
136 result = this->write(value);
137 }
138
139 // on write failure, continue the automation chain rather than stopping so that e.g. disconnect can work.
140 if (!result)
141 this->play_next_(x...);
142 }
143
152 // initiate the write. Return true if all went well, will be followed by a WRITE_CHAR event.
153 bool write(const uint8_t *data, size_t len) {
154 if (this->node_state != espbt::ClientState::ESTABLISHED) {
155 esph_log_w(Automation::TAG, "Cannot write to BLE characteristic - not connected");
156 return false;
157 }
158#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
159 char hex_buf[format_hex_pretty_size(BLE_WRITE_MAX_LOG_BYTES)];
160 esph_log_vv(Automation::TAG, "Will write %d bytes: %s", len, format_hex_pretty_to(hex_buf, data, len));
161#endif
162 esp_err_t err =
163 esp_ble_gattc_write_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->char_handle_, len,
164 const_cast<uint8_t *>(data), this->write_type_, ESP_GATT_AUTH_REQ_NONE);
165 if (err != ESP_OK) {
166 esph_log_e(Automation::TAG, "Error writing to characteristic: %s!", esp_err_to_name(err));
167 return false;
168 }
169 return true;
170 }
171
172 bool write(const std::vector<uint8_t> &value) { return this->write(value.data(), value.size()); }
173
174 void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
175 esp_ble_gattc_cb_param_t *param) override {
176 switch (event) {
177 case ESP_GATTC_WRITE_CHAR_EVT:
178 // upstream code checked the MAC address, verify the characteristic.
179 if (param->write.handle == this->char_handle_)
180 this->parent()->run_later([this]() { this->play_next_tuple_(this->var_); });
181 break;
182 case ESP_GATTC_DISCONNECT_EVT:
183 if (this->num_running_ != 0)
184 this->stop_complex();
185 break;
186 case ESP_GATTC_SEARCH_CMPL_EVT: {
187 auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
188 if (chr == nullptr) {
189 esph_log_w("ble_write_action", "Characteristic %s was not found in service %s",
190 this->char_uuid_.to_string().c_str(), this->service_uuid_.to_string().c_str());
191 break;
192 }
193 this->char_handle_ = chr->handle;
194 this->char_props_ = chr->properties;
195 if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE) {
196 this->write_type_ = ESP_GATT_WRITE_TYPE_RSP;
197 esph_log_d(Automation::TAG, "Write type: ESP_GATT_WRITE_TYPE_RSP");
198 } else if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) {
199 this->write_type_ = ESP_GATT_WRITE_TYPE_NO_RSP;
200 esph_log_d(Automation::TAG, "Write type: ESP_GATT_WRITE_TYPE_NO_RSP");
201 } else {
202 esph_log_e(Automation::TAG, "Characteristic %s does not allow writing", this->char_uuid_.to_string().c_str());
203 break;
204 }
205 this->node_state = espbt::ClientState::ESTABLISHED;
206 esph_log_d(Automation::TAG, "Found characteristic %s on device %s", this->char_uuid_.to_string().c_str(),
207 ble_client_->address_str());
208 break;
209 }
210 default:
211 break;
212 }
213 }
214
215 private:
216 BLEClient *ble_client_;
217 ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length
218 union Value {
219 std::vector<uint8_t> (*func)(Ts...); // Function pointer (stateless lambdas)
220 const uint8_t *data; // Pointer to static data in flash
221 } value_;
222 espbt::ESPBTUUID service_uuid_;
223 espbt::ESPBTUUID char_uuid_;
224 std::tuple<Ts...> var_{};
225 uint16_t char_handle_{};
226 esp_gatt_char_prop_t char_props_{};
227 esp_gatt_write_type_t write_type_{};
228};
229
230template<typename... Ts> class BLEClientPasskeyReplyAction : public Action<Ts...> {
231 public:
232 BLEClientPasskeyReplyAction(BLEClient *ble_client) { parent_ = ble_client; }
233
234 void play(const Ts &...x) override {
235 uint32_t passkey;
236 if (has_simple_value_) {
237 passkey = this->value_.simple;
238 } else {
239 passkey = this->value_.template_func(x...);
240 }
241 if (passkey > 999999)
242 return;
243 esp_bd_addr_t remote_bda;
244 memcpy(remote_bda, parent_->get_remote_bda(), sizeof(esp_bd_addr_t));
245 esp_ble_passkey_reply(remote_bda, true, passkey);
246 }
247
248 void set_value_template(uint32_t (*func)(Ts...)) {
249 this->value_.template_func = func;
250 this->has_simple_value_ = false;
251 }
252
253 void set_value_simple(const uint32_t &value) {
254 this->value_.simple = value;
255 this->has_simple_value_ = true;
256 }
257
258 private:
259 BLEClient *parent_{nullptr};
260 bool has_simple_value_ = true;
261 union {
262 uint32_t simple;
263 uint32_t (*template_func)(Ts...);
264 } value_{.simple = 0};
265};
266
267template<typename... Ts> class BLEClientNumericComparisonReplyAction : public Action<Ts...> {
268 public:
269 BLEClientNumericComparisonReplyAction(BLEClient *ble_client) { parent_ = ble_client; }
270
271 void play(const Ts &...x) override {
272 esp_bd_addr_t remote_bda;
273 memcpy(remote_bda, parent_->get_remote_bda(), sizeof(esp_bd_addr_t));
274 if (has_simple_value_) {
275 esp_ble_confirm_reply(remote_bda, this->value_.simple);
276 } else {
277 esp_ble_confirm_reply(remote_bda, this->value_.template_func(x...));
278 }
279 }
280
281 void set_value_template(bool (*func)(Ts...)) {
282 this->value_.template_func = func;
283 this->has_simple_value_ = false;
284 }
285
286 void set_value_simple(const bool &value) {
287 this->value_.simple = value;
288 this->has_simple_value_ = true;
289 }
290
291 private:
292 BLEClient *parent_{nullptr};
293 bool has_simple_value_ = true;
294 union {
295 bool simple;
296 bool (*template_func)(Ts...);
297 } value_{.simple = false};
298};
299
300template<typename... Ts> class BLEClientRemoveBondAction : public Action<Ts...> {
301 public:
302 BLEClientRemoveBondAction(BLEClient *ble_client) { parent_ = ble_client; }
303
304 void play(const Ts &...x) override {
305 esp_bd_addr_t remote_bda;
306 memcpy(remote_bda, parent_->get_remote_bda(), sizeof(esp_bd_addr_t));
307 esp_ble_remove_bond_device(remote_bda);
308 }
309
310 private:
311 BLEClient *parent_{nullptr};
312};
313
314template<typename... Ts> class BLEClientConnectAction : public Action<Ts...>, public BLEClientNode {
315 public:
317 ble_client->register_ble_node(this);
318 ble_client_ = ble_client;
319 }
320 void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
321 esp_ble_gattc_cb_param_t *param) override {
322 if (this->num_running_ == 0)
323 return;
324 switch (event) {
325 case ESP_GATTC_SEARCH_CMPL_EVT:
326 this->node_state = espbt::ClientState::ESTABLISHED;
327 this->parent()->run_later([this]() { this->play_next_tuple_(this->var_); });
328 break;
329 // if the connection is closed, terminate the automation chain.
330 case ESP_GATTC_DISCONNECT_EVT:
331 this->stop_complex();
332 break;
333 default:
334 break;
335 }
336 }
337
338 // not used since we override play_complex_
339 void play(const Ts &...x) override {}
340
341 void play_complex(const Ts &...x) override {
342 // it makes no sense to have multiple instances of this running at the same time.
343 // this would occur only if the same automation was re-triggered while still
344 // running. So just cancel the second chain if this is detected.
345 if (this->num_running_ != 0) {
346 this->stop_complex();
347 return;
348 }
349 this->num_running_++;
350 if (this->node_state == espbt::ClientState::ESTABLISHED) {
351 this->play_next_(x...);
352 } else {
353 this->var_ = std::make_tuple(x...);
354 this->ble_client_->connect();
355 }
356 }
357
358 private:
359 BLEClient *ble_client_;
360 std::tuple<Ts...> var_{};
361};
362
363template<typename... Ts> class BLEClientDisconnectAction : public Action<Ts...>, public BLEClientNode {
364 public:
366 ble_client->register_ble_node(this);
367 ble_client_ = ble_client;
368 }
369 void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
370 esp_ble_gattc_cb_param_t *param) override {
371 if (this->num_running_ == 0)
372 return;
373 switch (event) {
374 case ESP_GATTC_CLOSE_EVT:
375 case ESP_GATTC_DISCONNECT_EVT:
376 this->parent()->run_later([this]() { this->play_next_tuple_(this->var_); });
377 break;
378 default:
379 break;
380 }
381 }
382
383 // not used since we override play_complex_
384 void play(const Ts &...x) override {}
385
386 void play_complex(const Ts &...x) override {
387 this->num_running_++;
388 if (this->node_state == espbt::ClientState::IDLE) {
389 this->play_next_(x...);
390 } else {
391 this->var_ = std::make_tuple(x...);
392 this->ble_client_->disconnect();
393 }
394 }
395
396 private:
397 BLEClient *ble_client_;
398 std::tuple<Ts...> var_{};
399};
400} // namespace esphome::ble_client
401
402#endif
void play_next_(const Ts &...x)
Definition automation.h:261
virtual void stop_complex()
Definition automation.h:237
void play_next_tuple_(const std::tuple< Ts... > &tuple, std::index_sequence< S... >)
Definition automation.h:269
void trigger(const Ts &...x)
Definition automation.h:204
static const char *const TAG
Definition automation.h:22
void play_complex(const Ts &...x) override
Definition automation.h:341
BLEClientConnectAction(BLEClient *ble_client)
Definition automation.h:316
void play(const Ts &...x) override
Definition automation.h:339
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
Definition automation.h:320
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
Definition automation.h:30
void play_complex(const Ts &...x) override
Definition automation.h:386
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
Definition automation.h:369
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
Definition automation.h:44
void register_ble_node(BLEClientNode *node)
Definition ble_client.h:61
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
Definition automation.h:89
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
Definition automation.h:78
void set_value_template(uint32_t(*func)(Ts...))
Definition automation.h:248
void set_value_simple(const uint32_t &value)
Definition automation.h:253
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
Definition automation.h:68
bool write(const uint8_t *data, size_t len)
Note about logging: the esph_log_X macros are used here because the CI checks complain about use of t...
Definition automation.h:153
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
Definition automation.h:174
void set_value_template(std::vector< uint8_t >(*func)(Ts...))
Definition automation.h:112
void play(const Ts &...x) override
Definition automation.h:123
void play_complex(const Ts &...x) override
Definition automation.h:125
BLEClientWriteAction(BLEClient *ble_client)
Definition automation.h:99
bool write(const std::vector< uint8_t > &value)
Definition automation.h:172
void set_value_simple(const uint8_t *data, size_t len)
Definition automation.h:118
std::string to_string() const
Definition ble_uuid.cpp:184
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
void run_later(std::function< void()> &&f)
__int64 ssize_t
Definition httplib.h:178
std::string size_t len
Definition helpers.h:533
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:331
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:735
uint16_t x
Definition tt21100.cpp:5