ESPHome  2022.12.8
automation.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 #include <vector>
5 
8 
9 #ifdef USE_ESP32
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  ble_client->register_ble_node(this);
44  ble_client_ = ble_client;
45  }
46 
47  // Attempts to write the contents of value to char_uuid_.
48  void write(const std::vector<uint8_t> &value);
49 
50  void set_service_uuid16(uint16_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
51  void set_service_uuid32(uint32_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
52  void set_service_uuid128(uint8_t *uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
53 
54  void set_char_uuid16(uint16_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
55  void set_char_uuid32(uint32_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
56  void set_char_uuid128(uint8_t *uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
57 
58  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
59  esp_ble_gattc_cb_param_t *param) override;
60 
61  private:
62  BLEClient *ble_client_;
63  int ble_char_handle_ = 0;
64  esp_gatt_char_prop_t char_props_;
65  espbt::ESPBTUUID service_uuid_;
66  espbt::ESPBTUUID char_uuid_;
67 };
68 
69 template<typename... Ts> class BLEClientWriteAction : public Action<Ts...>, public BLEWriterClientNode {
70  public:
71  BLEClientWriteAction(BLEClient *ble_client) : BLEWriterClientNode(ble_client) {}
72 
73  void play(Ts... x) override {
74  if (has_simple_value_) {
75  return write(this->value_simple_);
76  } else {
77  return write(this->value_template_(x...));
78  }
79  }
80 
81  void set_value_template(std::function<std::vector<uint8_t>(Ts...)> func) {
82  this->value_template_ = std::move(func);
83  has_simple_value_ = false;
84  }
85 
86  void set_value_simple(const std::vector<uint8_t> &value) {
87  this->value_simple_ = value;
88  has_simple_value_ = true;
89  }
90 
91  private:
92  bool has_simple_value_ = true;
93  std::vector<uint8_t> value_simple_;
94  std::function<std::vector<uint8_t>(Ts...)> value_template_{};
95 };
96 
97 } // namespace ble_client
98 } // namespace esphome
99 
100 #endif
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 set_value_simple(const std::vector< uint8_t > &value)
Definition: automation.h:86
static ESPBTUUID from_raw(const uint8_t *data)
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:81
void set_service_uuid16(uint16_t uuid)
Definition: automation.h:50
BLEWriterClientNode(BLEClient *ble_client)
Definition: automation.h:42
void register_ble_node(BLEClientNode *node)
Definition: ble_client.h:62
static ESPBTUUID from_uint16(uint16_t uuid)
static ESPBTUUID from_uint32(uint32_t uuid)
Definition: a4988.cpp:4
void set_service_uuid32(uint32_t uuid)
Definition: automation.h:51
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:52
BLEClientWriteAction(BLEClient *ble_client)
Definition: automation.h:71
espbt::ClientState node_state
Definition: ble_client.h:38