ESPHome  2022.8.0
ble_client.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/helpers.h"
6 
7 #ifdef USE_ESP32
8 
9 #include <string>
10 #include <array>
11 #include <esp_gap_ble_api.h>
12 #include <esp_gattc_api.h>
13 #include <esp_bt_defs.h>
14 #include <esp_gatt_common_api.h>
15 
16 namespace esphome {
17 namespace ble_client {
18 
20 
21 class BLEClient;
22 class BLEService;
23 class BLECharacteristic;
24 
26  public:
27  virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
28  esp_ble_gattc_cb_param_t *param) = 0;
29  virtual void loop(){};
30  void set_address(uint64_t address) { address_ = address; }
32  // This should be transitioned to Established once the node no longer needs
33  // the services/descriptors/characteristics of the parent client. This will
34  // allow some memory to be freed.
36 
37  BLEClient *parent() { return this->parent_; }
39 
40  protected:
42  uint64_t address_;
43 };
44 
46  public:
48  uint16_t handle;
49 
51 };
52 
54  public:
57  uint16_t handle;
58  esp_gatt_char_prop_t properties;
59  std::vector<BLEDescriptor *> descriptors;
60  void parse_descriptors();
61  BLEDescriptor *get_descriptor(espbt::ESPBTUUID uuid);
62  BLEDescriptor *get_descriptor(uint16_t uuid);
63  void write_value(uint8_t *new_val, int16_t new_val_size);
64  void write_value(uint8_t *new_val, int16_t new_val_size, esp_gatt_write_type_t write_type);
66 };
67 
68 class BLEService {
69  public:
70  ~BLEService();
72  uint16_t start_handle;
73  uint16_t end_handle;
74  std::vector<BLECharacteristic *> characteristics;
76  void parse_characteristics();
77  BLECharacteristic *get_characteristic(espbt::ESPBTUUID uuid);
78  BLECharacteristic *get_characteristic(uint16_t uuid);
79 };
80 
81 class BLEClient : public espbt::ESPBTClient, public Component {
82  public:
83  void setup() override;
84  void dump_config() override;
85  void loop() override;
86  float get_setup_priority() const override;
87 
88  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
89  esp_ble_gattc_cb_param_t *param) override;
90  void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
91  bool parse_device(const espbt::ESPBTDevice &device) override;
92  void on_scan_end() override {}
93  void connect() override;
94 
95  void set_address(uint64_t address) { this->address = address; }
96 
97  void set_enabled(bool enabled);
98 
100  node->client = this;
101  node->set_ble_client_parent(this);
102  this->nodes_.push_back(node);
103  }
104 
105  BLEService *get_service(espbt::ESPBTUUID uuid);
106  BLEService *get_service(uint16_t uuid);
107  BLECharacteristic *get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr);
108  BLECharacteristic *get_characteristic(uint16_t service, uint16_t chr);
109  BLEDescriptor *get_descriptor(espbt::ESPBTUUID service, espbt::ESPBTUUID chr, espbt::ESPBTUUID descr);
110  BLEDescriptor *get_descriptor(uint16_t service, uint16_t chr, uint16_t descr);
111  // Get the configuration descriptor for the given characteristic handle.
112  BLEDescriptor *get_config_descriptor(uint16_t handle);
113 
114  float parse_char_value(uint8_t *value, uint16_t length);
115 
116  int gattc_if;
117  esp_bd_addr_t remote_bda;
118  esp_ble_addr_type_t remote_addr_type;
119  uint16_t conn_id;
120  uint64_t address;
121  bool enabled;
122  std::string address_str() const;
123 
124  protected:
126  this->set_state(st);
127  for (auto &node : nodes_)
128  node->node_state = st;
129  }
131  if (this->state() != espbt::ClientState::ESTABLISHED)
132  return false;
133  for (auto &node : nodes_) {
134  if (node->node_state != espbt::ClientState::ESTABLISHED)
135  return false;
136  }
137  return true;
138  }
139 
140  std::vector<BLEClientNode *> nodes_;
141  std::vector<BLEService *> services_;
142 };
143 
144 } // namespace ble_client
145 } // namespace esphome
146 
147 #endif
void setup()
std::vector< BLEDescriptor * > descriptors
Definition: ble_client.h:59
void set_address(uint64_t address)
Definition: ble_client.h:30
BLECharacteristic * characteristic
Definition: ble_client.h:50
void set_states_(espbt::ClientState st)
Definition: ble_client.h:125
virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)=0
std::vector< BLEClientNode * > nodes_
Definition: ble_client.h:140
std::vector< BLEService * > services_
Definition: ble_client.h:141
espbt::ESPBTClient * client
Definition: ble_client.h:31
std::vector< BLECharacteristic * > characteristics
Definition: ble_client.h:74
void set_address(uint64_t address)
Definition: ble_client.h:95
void set_ble_client_parent(BLEClient *parent)
Definition: ble_client.h:38
void register_ble_node(BLEClientNode *node)
Definition: ble_client.h:99
Definition: a4988.cpp:4
void on_scan_end() override
Definition: ble_client.h:92
esp_ble_addr_type_t remote_addr_type
Definition: ble_client.h:118
bool state
Definition: fan.h:34
espbt::ClientState node_state
Definition: ble_client.h:35