ESPHome  2024.12.2
ble_client_base.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 
7 
8 #include "ble_service.h"
9 
10 #include <array>
11 #include <string>
12 #include <vector>
13 
14 #include <esp_bt_defs.h>
15 #include <esp_gap_ble_api.h>
16 #include <esp_gatt_common_api.h>
17 #include <esp_gattc_api.h>
18 
19 namespace esphome {
20 namespace esp32_ble_client {
21 
23 
24 class BLEClientBase : public espbt::ESPBTClient, public Component {
25  public:
26  void setup() override;
27  void loop() override;
28  float get_setup_priority() const override;
29 
30  void run_later(std::function<void()> &&f); // NOLINT
31  bool parse_device(const espbt::ESPBTDevice &device) override;
32  void on_scan_end() override {}
33  bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
34  esp_ble_gattc_cb_param_t *param) override;
35  void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
36  void connect() override;
37  esp_err_t pair();
38  void disconnect() override;
39  void release_services();
40 
41  bool connected() { return this->state_ == espbt::ClientState::ESTABLISHED; }
42 
43  void set_auto_connect(bool auto_connect) { this->auto_connect_ = auto_connect; }
44 
45  void set_address(uint64_t address) {
46  this->address_ = address;
47  this->remote_bda_[0] = (address >> 40) & 0xFF;
48  this->remote_bda_[1] = (address >> 32) & 0xFF;
49  this->remote_bda_[2] = (address >> 24) & 0xFF;
50  this->remote_bda_[3] = (address >> 16) & 0xFF;
51  this->remote_bda_[4] = (address >> 8) & 0xFF;
52  this->remote_bda_[5] = (address >> 0) & 0xFF;
53  if (address == 0) {
54  this->address_str_ = "";
55  } else {
56  this->address_str_ =
57  str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, (uint8_t) (this->address_ >> 40) & 0xff,
58  (uint8_t) (this->address_ >> 32) & 0xff, (uint8_t) (this->address_ >> 24) & 0xff,
59  (uint8_t) (this->address_ >> 16) & 0xff, (uint8_t) (this->address_ >> 8) & 0xff,
60  (uint8_t) (this->address_ >> 0) & 0xff);
61  }
62  }
63  std::string address_str() const { return this->address_str_; }
64 
66  BLEService *get_service(uint16_t uuid);
68  BLECharacteristic *get_characteristic(uint16_t service, uint16_t chr);
69  BLECharacteristic *get_characteristic(uint16_t handle);
71  BLEDescriptor *get_descriptor(uint16_t service, uint16_t chr, uint16_t descr);
72  BLEDescriptor *get_descriptor(uint16_t handle);
73  // Get the configuration descriptor for the given characteristic handle.
74  BLEDescriptor *get_config_descriptor(uint16_t handle);
75 
76  float parse_char_value(uint8_t *value, uint16_t length);
77 
78  int get_gattc_if() const { return this->gattc_if_; }
79  uint8_t *get_remote_bda() { return this->remote_bda_; }
80  esp_ble_addr_type_t get_remote_addr_type() const { return this->remote_addr_type_; }
81  void set_remote_addr_type(esp_ble_addr_type_t address_type) { this->remote_addr_type_ = address_type; }
82  uint16_t get_conn_id() const { return this->conn_id_; }
83  uint64_t get_address() const { return this->address_; }
84  bool is_paired() const { return this->paired_; }
85 
86  uint8_t get_connection_index() const { return this->connection_index_; }
87 
89 
90  bool check_addr(esp_bd_addr_t &addr) { return memcmp(addr, this->remote_bda_, sizeof(esp_bd_addr_t)) == 0; }
91 
92  protected:
93  int gattc_if_;
94  esp_bd_addr_t remote_bda_;
95  esp_ble_addr_type_t remote_addr_type_{BLE_ADDR_TYPE_PUBLIC};
96  uint16_t conn_id_{0xFFFF};
97  uint64_t address_{0};
98  bool auto_connect_{false};
99  std::string address_str_{};
101  int16_t service_count_{0};
102  uint16_t mtu_{23};
103  bool paired_{false};
104  espbt::ConnectionType connection_type_{espbt::ConnectionType::V1};
105  std::vector<BLEService *> services_;
106 
107  void log_event_(const char *name);
108 };
109 
110 } // namespace esp32_ble_client
111 } // namespace esphome
112 
113 #endif // USE_ESP32
BLEDescriptor * get_descriptor(espbt::ESPBTUUID service, espbt::ESPBTUUID chr, espbt::ESPBTUUID descr)
const char * name
Definition: stm32flash.h:78
BLEService * get_service(espbt::ESPBTUUID uuid)
std::vector< BLEService * > services_
bool check_addr(esp_bd_addr_t &addr)
void run_later(std::function< void()> &&f)
BLEDescriptor * get_config_descriptor(uint16_t handle)
void set_remote_addr_type(esp_ble_addr_type_t address_type)
virtual void set_connection_type(espbt::ConnectionType ct)
void set_auto_connect(bool auto_connect)
esp_ble_addr_type_t get_remote_addr_type() const
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool parse_device(const espbt::ESPBTDevice &device) override
uint8_t address
Definition: bl0906.h:211
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
float parse_char_value(uint8_t *value, uint16_t length)
std::string str_snprintf(const char *fmt, size_t len,...)
Definition: helpers.cpp:306