ESPHome  2023.11.6
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 
10 
11 namespace esphome {
12 namespace ble_client {
13 class BLEClientConnectTrigger : public Trigger<>, public BLEClientNode {
14  public:
16  void loop() override {}
17  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
18  esp_ble_gattc_cb_param_t *param) override {
19  if (event == ESP_GATTC_SEARCH_CMPL_EVT) {
20  this->node_state = espbt::ClientState::ESTABLISHED;
21  this->trigger();
22  }
23  }
24 };
25 
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_DISCONNECT_EVT &&
33  memcmp(param->disconnect.remote_bda, this->parent_->get_remote_bda(), 6) == 0)
34  this->trigger();
35  if (event == ESP_GATTC_SEARCH_CMPL_EVT)
36  this->node_state = espbt::ClientState::ESTABLISHED;
37  }
38 };
39 
41  public:
43  void loop() override {}
44  void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override {
45  if (event == ESP_GAP_BLE_PASSKEY_REQ_EVT &&
46  memcmp(param->ble_security.auth_cmpl.bd_addr, this->parent_->get_remote_bda(), 6) == 0) {
47  this->trigger();
48  }
49  }
50 };
51 
52 class BLEClientPasskeyNotificationTrigger : public Trigger<uint32_t>, public BLEClientNode {
53  public:
55  void loop() override {}
56  void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override {
57  if (event == ESP_GAP_BLE_PASSKEY_NOTIF_EVT &&
58  memcmp(param->ble_security.auth_cmpl.bd_addr, this->parent_->get_remote_bda(), 6) == 0) {
59  uint32_t passkey = param->ble_security.key_notif.passkey;
60  this->trigger(passkey);
61  }
62  }
63 };
64 
66  public:
68  void loop() override {}
69  void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override {
70  if (event == ESP_GAP_BLE_NC_REQ_EVT &&
71  memcmp(param->ble_security.auth_cmpl.bd_addr, this->parent_->get_remote_bda(), 6) == 0) {
72  uint32_t passkey = param->ble_security.key_notif.passkey;
73  this->trigger(passkey);
74  }
75  }
76 };
77 
79  public:
81  ble_client->register_ble_node(this);
82  ble_client_ = ble_client;
83  }
84 
85  // Attempts to write the contents of value to char_uuid_.
86  void write(const std::vector<uint8_t> &value);
87 
88  void set_service_uuid16(uint16_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
89  void set_service_uuid32(uint32_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
90  void set_service_uuid128(uint8_t *uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
91 
92  void set_char_uuid16(uint16_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
93  void set_char_uuid32(uint32_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
94  void set_char_uuid128(uint8_t *uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
95 
96  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
97  esp_ble_gattc_cb_param_t *param) override;
98 
99  private:
100  BLEClient *ble_client_;
101  int ble_char_handle_ = 0;
102  esp_gatt_char_prop_t char_props_;
103  espbt::ESPBTUUID service_uuid_;
104  espbt::ESPBTUUID char_uuid_;
105 };
106 
107 template<typename... Ts> class BLEClientWriteAction : public Action<Ts...>, public BLEWriterClientNode {
108  public:
109  BLEClientWriteAction(BLEClient *ble_client) : BLEWriterClientNode(ble_client) {}
110 
111  void play(Ts... x) override {
112  if (has_simple_value_) {
113  return write(this->value_simple_);
114  } else {
115  return write(this->value_template_(x...));
116  }
117  }
118 
119  void set_value_template(std::function<std::vector<uint8_t>(Ts...)> func) {
120  this->value_template_ = std::move(func);
121  has_simple_value_ = false;
122  }
123 
124  void set_value_simple(const std::vector<uint8_t> &value) {
125  this->value_simple_ = value;
126  has_simple_value_ = true;
127  }
128 
129  private:
130  bool has_simple_value_ = true;
131  std::vector<uint8_t> value_simple_;
132  std::function<std::vector<uint8_t>(Ts...)> value_template_{};
133 };
134 
135 template<typename... Ts> class BLEClientPasskeyReplyAction : public Action<Ts...> {
136  public:
137  BLEClientPasskeyReplyAction(BLEClient *ble_client) { parent_ = ble_client; }
138 
139  void play(Ts... x) override {
140  uint32_t passkey;
141  if (has_simple_value_) {
142  passkey = this->value_simple_;
143  } else {
144  passkey = this->value_template_(x...);
145  }
146  if (passkey > 999999)
147  return;
148  esp_bd_addr_t remote_bda;
149  memcpy(remote_bda, parent_->get_remote_bda(), sizeof(esp_bd_addr_t));
150  esp_ble_passkey_reply(remote_bda, true, passkey);
151  }
152 
153  void set_value_template(std::function<uint32_t(Ts...)> func) {
154  this->value_template_ = std::move(func);
155  has_simple_value_ = false;
156  }
157 
158  void set_value_simple(const uint32_t &value) {
159  this->value_simple_ = value;
160  has_simple_value_ = true;
161  }
162 
163  private:
164  BLEClient *parent_{nullptr};
165  bool has_simple_value_ = true;
166  uint32_t value_simple_{0};
167  std::function<uint32_t(Ts...)> value_template_{};
168 };
169 
170 template<typename... Ts> class BLEClientNumericComparisonReplyAction : public Action<Ts...> {
171  public:
173 
174  void play(Ts... x) override {
175  esp_bd_addr_t remote_bda;
176  memcpy(remote_bda, parent_->get_remote_bda(), sizeof(esp_bd_addr_t));
177  if (has_simple_value_) {
178  esp_ble_confirm_reply(remote_bda, this->value_simple_);
179  } else {
180  esp_ble_confirm_reply(remote_bda, this->value_template_(x...));
181  }
182  }
183 
184  void set_value_template(std::function<bool(Ts...)> func) {
185  this->value_template_ = std::move(func);
186  has_simple_value_ = false;
187  }
188 
189  void set_value_simple(const bool &value) {
190  this->value_simple_ = value;
191  has_simple_value_ = true;
192  }
193 
194  private:
195  BLEClient *parent_{nullptr};
196  bool has_simple_value_ = true;
197  bool value_simple_{false};
198  std::function<bool(Ts...)> value_template_{};
199 };
200 
201 template<typename... Ts> class BLEClientRemoveBondAction : public Action<Ts...> {
202  public:
203  BLEClientRemoveBondAction(BLEClient *ble_client) { parent_ = ble_client; }
204 
205  void play(Ts... x) override {
206  esp_bd_addr_t remote_bda;
207  memcpy(remote_bda, parent_->get_remote_bda(), sizeof(esp_bd_addr_t));
208  esp_ble_remove_bond_device(remote_bda);
209  }
210 
211  private:
212  BLEClient *parent_{nullptr};
213 };
214 
215 } // namespace ble_client
216 } // namespace esphome
217 
218 #endif
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
Definition: automation.h:56
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
BLEClientRemoveBondAction(BLEClient *ble_client)
Definition: automation.h:203
uint16_t x
Definition: tt21100.cpp:17
void set_value_simple(const std::vector< uint8_t > &value)
Definition: automation.h:124
void set_value_simple(const uint32_t &value)
Definition: automation.h:158
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
Definition: automation.h:69
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
void set_value_template(std::function< std::vector< uint8_t >(Ts...)> func)
Definition: automation.h:119
void set_value_template(std::function< uint32_t(Ts...)> func)
Definition: automation.h:153
static ESPBTUUID from_uint32(uint32_t uuid)
Definition: ble_uuid.cpp:22
void set_service_uuid16(uint16_t uuid)
Definition: automation.h:88
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
Definition: automation.h:44
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: ble_uuid.cpp:16
BLEWriterClientNode(BLEClient *ble_client)
Definition: automation.h:80
void register_ble_node(BLEClientNode *node)
Definition: ble_client.h:62
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
static ESPBTUUID from_raw(const uint8_t *data)
Definition: ble_uuid.cpp:28
void set_service_uuid32(uint32_t uuid)
Definition: automation.h:89
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:17
void set_service_uuid128(uint8_t *uuid)
Definition: automation.h:90
BLEClientWriteAction(BLEClient *ble_client)
Definition: automation.h:109
BLEClientPasskeyReplyAction(BLEClient *ble_client)
Definition: automation.h:137
void set_value_template(std::function< bool(Ts...)> func)
Definition: automation.h:184
espbt::ClientState node_state
Definition: ble_client.h:38