ESPHome  2022.12.8
ble_client.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "esphome/core/helpers.h"
7 
8 #ifdef USE_ESP32
9 
10 #include <esp_bt_defs.h>
11 #include <esp_gap_ble_api.h>
12 #include <esp_gatt_common_api.h>
13 #include <esp_gattc_api.h>
14 #include <array>
15 #include <string>
16 #include <vector>
17 
18 namespace esphome {
19 namespace ble_client {
20 
22 
23 using namespace esp32_ble_client;
24 
25 class BLEClient;
26 
28  public:
29  virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
30  esp_ble_gattc_cb_param_t *param) = 0;
31  virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {}
32  virtual void loop() {}
33  void set_address(uint64_t address) { address_ = address; }
35  // This should be transitioned to Established once the node no longer needs
36  // the services/descriptors/characteristics of the parent client. This will
37  // allow some memory to be freed.
39 
40  BLEClient *parent() { return this->parent_; }
41  void set_ble_client_parent(BLEClient *parent) { this->parent_ = parent; }
42 
43  protected:
45  uint64_t address_;
46 };
47 
48 class BLEClient : public BLEClientBase {
49  public:
50  void setup() override;
51  void dump_config() override;
52  void loop() override;
53 
54  bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
55  esp_ble_gattc_cb_param_t *param) override;
56 
57  void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
58  bool parse_device(const espbt::ESPBTDevice &device) override;
59 
60  void set_enabled(bool enabled);
61 
63  node->client = this;
64  node->set_ble_client_parent(this);
65  this->nodes_.push_back(node);
66  }
67 
68  bool enabled;
69 
70  void set_state(espbt::ClientState state) override;
71 
72  protected:
73  bool all_nodes_established_();
74 
75  std::vector<BLEClientNode *> nodes_;
76 };
77 
78 } // namespace ble_client
79 } // namespace esphome
80 
81 #endif
void setup()
void loop()
void set_address(uint64_t address)
Definition: ble_client.h:33
virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
Definition: ble_client.h:31
std::vector< BLEClientNode * > nodes_
Definition: ble_client.h:75
espbt::ESPBTClient * client
Definition: ble_client.h:34
void set_ble_client_parent(BLEClient *parent)
Definition: ble_client.h:41
void register_ble_node(BLEClientNode *node)
Definition: ble_client.h:62
Definition: a4988.cpp:4
bool state
Definition: fan.h:34
espbt::ClientState node_state
Definition: ble_client.h:38