ESPHome 2026.3.0
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 <vector>
14
15#include <esp_bt_defs.h>
16#include <esp_gap_ble_api.h>
17#include <esp_gatt_common_api.h>
18#include <esp_gattc_api.h>
19
21
23
24static const int UNSET_CONN_ID = 0xFFFF;
25
27 public:
28 void setup() override;
29 void loop() override;
30 float get_setup_priority() const override;
31 void dump_config() override;
32
33 void run_later(std::function<void()> &&f); // NOLINT
34#ifdef USE_ESP32_BLE_DEVICE
35 bool parse_device(const espbt::ESPBTDevice &device) override;
36#endif
37 void on_scan_end() override {}
38 bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
39 esp_ble_gattc_cb_param_t *param) override;
40 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
41 void connect() override;
42 esp_err_t pair();
43 void disconnect() override;
45 void release_services();
46
47 bool connected() { return this->state() == espbt::ClientState::ESTABLISHED; }
48
49 void set_auto_connect(bool auto_connect) { this->auto_connect_ = auto_connect; }
50
51 virtual void set_address(uint64_t address) {
52 this->address_ = address;
53 this->remote_bda_[0] = (address >> 40) & 0xFF;
54 this->remote_bda_[1] = (address >> 32) & 0xFF;
55 this->remote_bda_[2] = (address >> 24) & 0xFF;
56 this->remote_bda_[3] = (address >> 16) & 0xFF;
57 this->remote_bda_[4] = (address >> 8) & 0xFF;
58 this->remote_bda_[5] = (address >> 0) & 0xFF;
59 if (address == 0) {
60 this->address_str_[0] = '\0';
61 } else {
63 }
64 }
65 const char *address_str() const { return this->address_str_; }
66
67#ifdef USE_ESP32_BLE_DEVICE
69 BLEService *get_service(uint16_t uuid);
71 BLECharacteristic *get_characteristic(uint16_t service, uint16_t chr);
72 BLECharacteristic *get_characteristic(uint16_t handle);
74 BLEDescriptor *get_descriptor(uint16_t service, uint16_t chr, uint16_t descr);
75 BLEDescriptor *get_descriptor(uint16_t handle);
76 // Get the configuration descriptor for the given characteristic handle.
77 BLEDescriptor *get_config_descriptor(uint16_t handle);
78#endif
79
80 float parse_char_value(uint8_t *value, uint16_t length);
81
82 int get_gattc_if() const { return this->gattc_if_; }
83 uint8_t *get_remote_bda() { return this->remote_bda_; }
84 esp_ble_addr_type_t get_remote_addr_type() const { return this->remote_addr_type_; }
85 void set_remote_addr_type(esp_ble_addr_type_t address_type) { this->remote_addr_type_ = address_type; }
86 uint16_t get_conn_id() const { return this->conn_id_; }
87 uint64_t get_address() const { return this->address_; }
88 bool is_paired() const { return this->paired_; }
89
90 uint8_t get_connection_index() const { return this->connection_index_; }
91
93
94 bool check_addr(esp_bd_addr_t &addr) { return memcmp(addr, this->remote_bda_, sizeof(esp_bd_addr_t)) == 0; }
95
96 void set_state(espbt::ClientState st) override;
97
98 protected:
99 // Memory optimized layout for 32-bit systems
100 // Group 1: 8-byte types
101 uint64_t address_{0};
102
103 // Group 2: Container types (grouped for memory optimization)
104#ifdef USE_ESP32_BLE_DEVICE
105 std::vector<BLEService *> services_;
106#endif
107
108 // Group 3: 4-byte types
110 esp_gatt_status_t status_{ESP_GATT_OK};
111
112 // Group 4: Arrays
113 char address_str_[MAC_ADDRESS_PRETTY_BUFFER_SIZE]{};
114 esp_bd_addr_t remote_bda_; // 6 bytes
115
116 // Group 5: 4-byte types
118
119 // Group 6: 2-byte types
120 uint16_t conn_id_{UNSET_CONN_ID};
121 uint16_t mtu_{23};
122
123 // Group 7: 1-byte types and small enums
124 esp_ble_addr_type_t remote_addr_type_{BLE_ADDR_TYPE_PUBLIC};
125 espbt::ConnectionType connection_type_{espbt::ConnectionType::V1};
127 uint8_t service_count_{0}; // ESP32 has max handles < 255, typical devices have < 50 services
128 bool auto_connect_{false};
129 bool paired_{false};
130 // 6 bytes used, 2 bytes padding
131
132 void log_event_(const char *name);
133 void log_gattc_lifecycle_event_(const char *name);
134 void log_gattc_data_event_(const char *name);
135 esp_err_t update_conn_params_(uint16_t min_interval, uint16_t max_interval, uint16_t latency, uint16_t timeout,
136 const char *param_type);
137 void set_conn_params_(uint16_t min_interval, uint16_t max_interval, uint16_t latency, uint16_t timeout,
138 const char *param_type);
139 void log_gattc_warning_(const char *operation, esp_gatt_status_t status);
140 void log_gattc_warning_(const char *operation, esp_err_t err);
141 void log_connection_params_(const char *param_type);
142 void handle_connection_result_(esp_err_t ret);
144 void set_idle_() {
145 this->set_state(espbt::ClientState::IDLE);
146 this->conn_id_ = UNSET_CONN_ID;
147 }
151 this->set_state(espbt::ClientState::DISCONNECTING);
152 }
153 // Compact error logging helpers to reduce flash usage
154 void log_error_(const char *message);
155 void log_error_(const char *message, int code);
156 void log_warning_(const char *message);
157};
158
159} // namespace esphome::esp32_ble_client
160
161#endif // USE_ESP32
uint8_t address
Definition bl0906.h:4
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
Definition component.h:395
std::vector< BLEService * > services_
char address_str_[MAC_ADDRESS_PRETTY_BUFFER_SIZE]
void log_gattc_warning_(const char *operation, esp_gatt_status_t status)
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
esp_err_t update_conn_params_(uint16_t min_interval, uint16_t max_interval, uint16_t latency, uint16_t timeout, const char *param_type)
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 set_idle_()
Transition to IDLE and reset conn_id — call when the connection is fully dead.
void run_later(std::function< void()> &&f)
BLEService * get_service(espbt::ESPBTUUID uuid)
void set_disconnecting_()
Transition to DISCONNECTING and start the safety timeout.
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_conn_params_(uint16_t min_interval, uint16_t max_interval, uint16_t latency, uint16_t timeout, const char *param_type)
void set_remote_addr_type(esp_ble_addr_type_t address_type)
Base class for BLE GATT clients that connect to remote devices.
const char * message
Definition component.cpp:38
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
Definition helpers.h:1267
static void uint32_t
uint16_t length
Definition tt21100.cpp:0