ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
ble_client_base.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
7
8#ifdef USE_ESP32_BLE_DEVICE
9#include "ble_service.h"
10#endif
11
12#include <array>
13#include <string>
14#include <vector>
15
16#include <esp_bt_defs.h>
17#include <esp_gap_ble_api.h>
18#include <esp_gatt_common_api.h>
19#include <esp_gattc_api.h>
20
22
24
25static const int UNSET_CONN_ID = 0xFFFF;
26
28 public:
29 void setup() override;
30 void loop() override;
31 float get_setup_priority() const override;
32 void dump_config() override;
33
34 void run_later(std::function<void()> &&f); // NOLINT
35#ifdef USE_ESP32_BLE_DEVICE
36 bool parse_device(const espbt::ESPBTDevice &device) override;
37#endif
38 void on_scan_end() override {}
39 bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
40 esp_ble_gattc_cb_param_t *param) override;
41 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
42 void connect() override;
43 esp_err_t pair();
44 void disconnect() override;
46 void release_services();
47
48 bool connected() { return this->state_ == espbt::ClientState::ESTABLISHED; }
49
50 void set_auto_connect(bool auto_connect) { this->auto_connect_ = auto_connect; }
51
52 virtual void set_address(uint64_t address) {
53 this->address_ = address;
54 this->remote_bda_[0] = (address >> 40) & 0xFF;
55 this->remote_bda_[1] = (address >> 32) & 0xFF;
56 this->remote_bda_[2] = (address >> 24) & 0xFF;
57 this->remote_bda_[3] = (address >> 16) & 0xFF;
58 this->remote_bda_[4] = (address >> 8) & 0xFF;
59 this->remote_bda_[5] = (address >> 0) & 0xFF;
60 if (address == 0) {
61 this->address_str_ = "";
62 } else {
63 this->address_str_ =
64 str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, (uint8_t) (this->address_ >> 40) & 0xff,
65 (uint8_t) (this->address_ >> 32) & 0xff, (uint8_t) (this->address_ >> 24) & 0xff,
66 (uint8_t) (this->address_ >> 16) & 0xff, (uint8_t) (this->address_ >> 8) & 0xff,
67 (uint8_t) (this->address_ >> 0) & 0xff);
68 }
69 }
70 const std::string &address_str() const { return this->address_str_; }
71
72#ifdef USE_ESP32_BLE_DEVICE
74 BLEService *get_service(uint16_t uuid);
76 BLECharacteristic *get_characteristic(uint16_t service, uint16_t chr);
77 BLECharacteristic *get_characteristic(uint16_t handle);
79 BLEDescriptor *get_descriptor(uint16_t service, uint16_t chr, uint16_t descr);
80 BLEDescriptor *get_descriptor(uint16_t handle);
81 // Get the configuration descriptor for the given characteristic handle.
82 BLEDescriptor *get_config_descriptor(uint16_t handle);
83#endif
84
85 float parse_char_value(uint8_t *value, uint16_t length);
86
87 int get_gattc_if() const { return this->gattc_if_; }
88 uint8_t *get_remote_bda() { return this->remote_bda_; }
89 esp_ble_addr_type_t get_remote_addr_type() const { return this->remote_addr_type_; }
90 void set_remote_addr_type(esp_ble_addr_type_t address_type) { this->remote_addr_type_ = address_type; }
91 uint16_t get_conn_id() const { return this->conn_id_; }
92 uint64_t get_address() const { return this->address_; }
93 bool is_paired() const { return this->paired_; }
94
95 uint8_t get_connection_index() const { return this->connection_index_; }
96
98
99 bool check_addr(esp_bd_addr_t &addr) { return memcmp(addr, this->remote_bda_, sizeof(esp_bd_addr_t)) == 0; }
100
101 void set_state(espbt::ClientState st) override;
102
103 protected:
104 // Memory optimized layout for 32-bit systems
105 // Group 1: 8-byte types
106 uint64_t address_{0};
107
108 // Group 2: Container types (grouped for memory optimization)
109 std::string address_str_{};
110#ifdef USE_ESP32_BLE_DEVICE
111 std::vector<BLEService *> services_;
112#endif
113
114 // Group 3: 4-byte types
116 esp_gatt_status_t status_{ESP_GATT_OK};
117
118 // Group 4: Arrays (6 bytes)
119 esp_bd_addr_t remote_bda_;
120
121 // Group 5: 2-byte types
122 uint16_t conn_id_{UNSET_CONN_ID};
123 uint16_t mtu_{23};
124
125 // Group 6: 1-byte types and small enums
126 esp_ble_addr_type_t remote_addr_type_{BLE_ADDR_TYPE_PUBLIC};
127 espbt::ConnectionType connection_type_{espbt::ConnectionType::V1};
129 uint8_t service_count_{0}; // ESP32 has max handles < 255, typical devices have < 50 services
130 bool auto_connect_{false};
131 bool paired_{false};
132 // 6 bytes used, 2 bytes padding
133
134 void log_event_(const char *name);
135 void log_gattc_event_(const char *name);
137 void log_gattc_warning_(const char *operation, esp_gatt_status_t status);
138 void log_gattc_warning_(const char *operation, esp_err_t err);
139 void log_connection_params_(const char *param_type);
140};
141
142} // namespace esphome::esp32_ble_client
143
144#endif // USE_ESP32
uint8_t address
Definition bl0906.h:4
std::vector< BLEService * > services_
void log_gattc_warning_(const char *operation, esp_gatt_status_t status)
const std::string & address_str() const
void log_connection_params_(const char *param_type)
BLEDescriptor * get_descriptor(espbt::ESPBTUUID service, espbt::ESPBTUUID chr, espbt::ESPBTUUID descr)
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
void set_state(espbt::ClientState st) override
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
virtual void set_address(uint64_t address)
esp_ble_addr_type_t get_remote_addr_type() const
void run_later(std::function< void()> &&f)
BLEService * get_service(espbt::ESPBTUUID uuid)
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
bool parse_device(const espbt::ESPBTDevice &device) override
virtual void set_connection_type(espbt::ConnectionType ct)
float parse_char_value(uint8_t *value, uint16_t length)
BLEDescriptor * get_config_descriptor(uint16_t handle)
void set_remote_addr_type(esp_ble_addr_type_t address_type)
std::string str_snprintf(const char *fmt, size_t len,...)
Definition helpers.cpp:194
uint16_t length
Definition tt21100.cpp:0