ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
bluetooth_proxy.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
5#include <array>
6#include <map>
7#include <vector>
8
16
18
19#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
20#include <esp_bt.h>
21#endif
22#include <esp_bt_device.h>
23
25
26static constexpr esp_err_t ESP_GATT_NOT_CONNECTED = -1;
27static constexpr int DONE_SENDING_SERVICES = -2;
28static constexpr int INIT_SENDING_SERVICES = -3;
29
30using namespace esp32_ble_client;
31
32// Legacy versions:
33// Version 1: Initial version without active connections
34// Version 2: Support for active connections
35// Version 3: New connection API
36// Version 4: Pairing support
37// Version 5: Cache clear support
38static constexpr uint32_t LEGACY_ACTIVE_CONNECTIONS_VERSION = 5;
39static constexpr uint32_t LEGACY_PASSIVE_ONLY_VERSION = 1;
40
51
55
58 public Component {
59 friend class BluetoothConnection; // Allow connection to update connections_free_response_
60 public:
62#ifdef USE_ESP32_BLE_DEVICE
63 bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
64#endif
65 bool parse_devices(const esp32_ble::BLEScanResult *scan_results, size_t count) override;
66 void dump_config() override;
67 void setup() override;
69
71 if (this->connection_count_ < BLUETOOTH_PROXY_MAX_CONNECTIONS) {
72 this->connections_[this->connection_count_++] = connection;
73 connection->proxy_ = this;
74 }
75 }
76
85
89
90 void send_device_connection(uint64_t address, bool connected, uint16_t mtu = 0, esp_err_t error = ESP_OK);
92 void send_connections_free(api::APIConnection *api_connection);
93 void send_gatt_services_done(uint64_t address);
94 void send_gatt_error(uint64_t address, uint16_t handle, esp_err_t error);
95 void send_device_pairing(uint64_t address, bool paired, esp_err_t error = ESP_OK);
96 void send_device_unpairing(uint64_t address, bool success, esp_err_t error = ESP_OK);
97 void send_device_clear_cache(uint64_t address, bool success, esp_err_t error = ESP_OK);
98
99 void bluetooth_scanner_set_mode(bool active);
100
101 static void uint64_to_bd_addr(uint64_t address, esp_bd_addr_t bd_addr) {
102 bd_addr[0] = (address >> 40) & 0xff;
103 bd_addr[1] = (address >> 32) & 0xff;
104 bd_addr[2] = (address >> 24) & 0xff;
105 bd_addr[3] = (address >> 16) & 0xff;
106 bd_addr[4] = (address >> 8) & 0xff;
107 bd_addr[5] = (address >> 0) & 0xff;
108 }
109
110 void set_active(bool active) { this->active_ = active; }
111 bool has_active() { return this->active_; }
112
115
117 if (this->active_) {
118 return LEGACY_ACTIVE_CONNECTIONS_VERSION;
119 }
120 return LEGACY_PASSIVE_ONLY_VERSION;
121 }
122
138
139 void get_bluetooth_mac_address_pretty(std::span<char, 18> output) {
140 const uint8_t *mac = esp_bt_dev_get_address();
141 if (mac != nullptr) {
142 format_mac_addr_upper(mac, output.data());
143 } else {
144 output[0] = '\0';
145 }
146 }
147
148 protected:
150
153 if (this->response_.advertisements_len == 0)
154 return;
156#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
158#endif
160 }
162
163 BluetoothConnection *get_connection_(uint64_t address, bool reserve);
165 void log_connection_info_(BluetoothConnection *connection, const char *message);
166 void log_not_connected_gatt_(const char *action, const char *type);
167 void handle_gatt_not_connected_(uint64_t address, uint16_t handle, const char *action, const char *type);
168
169 // Memory optimized layout for 32-bit systems
170 // Group 1: Pointers (4 bytes each, naturally aligned)
172
173 // Group 2: Fixed-size array of connection pointers
174 std::array<BluetoothConnection *, BLUETOOTH_PROXY_MAX_CONNECTIONS> connections_{};
175
176 // BLE advertisement batching
178
179 // Pre-allocated response message - always ready to send
181
182 // Group 4: 1-byte types grouped together
185 bool configured_scan_active_{false}; // Configured scan mode from YAML
186 // 3 bytes used, 1 byte padding
187};
188
189extern BluetoothProxy *global_bluetooth_proxy; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
190
191} // namespace esphome::bluetooth_proxy
192
193#endif // USE_ESP32
uint8_t address
Definition bl0906.h:4
bool send_message(const T &msg)
void bluetooth_gatt_read(const api::BluetoothGATTReadRequest &msg)
void bluetooth_gatt_send_services(const api::BluetoothGATTGetServicesRequest &msg)
void handle_gatt_not_connected_(uint64_t address, uint16_t handle, const char *action, const char *type)
esp32_ble_tracker::AdvertisementParserType get_advertisement_parser_type() override
void send_device_connection(uint64_t address, bool connected, uint16_t mtu=0, esp_err_t error=ESP_OK)
void send_device_unpairing(uint64_t address, bool success, esp_err_t error=ESP_OK)
void send_device_pairing(uint64_t address, bool paired, esp_err_t error=ESP_OK)
bool parse_devices(const esp32_ble::BLEScanResult *scan_results, size_t count) override
void log_not_connected_gatt_(const char *action, const char *type)
void bluetooth_device_request(const api::BluetoothDeviceRequest &msg)
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
void bluetooth_gatt_write_descriptor(const api::BluetoothGATTWriteDescriptorRequest &msg)
void send_bluetooth_scanner_state_(esp32_ble_tracker::ScannerState state)
void subscribe_api_connection(api::APIConnection *api_connection, uint32_t flags)
void send_gatt_error(uint64_t address, uint16_t handle, esp_err_t error)
void unsubscribe_api_connection(api::APIConnection *api_connection)
void register_connection(BluetoothConnection *connection)
api::BluetoothLERawAdvertisementsResponse response_
void log_connection_info_(BluetoothConnection *connection, const char *message)
void get_bluetooth_mac_address_pretty(std::span< char, 18 > output)
BluetoothConnection * get_connection_(uint64_t address, bool reserve)
void bluetooth_set_connection_params(const api::BluetoothSetConnectionParamsRequest &msg)
void bluetooth_gatt_read_descriptor(const api::BluetoothGATTReadDescriptorRequest &msg)
void bluetooth_gatt_write(const api::BluetoothGATTWriteRequest &msg)
void bluetooth_gatt_notify(const api::BluetoothGATTNotifyRequest &msg)
void on_scanner_state(esp32_ble_tracker::ScannerState state) override
BLEScannerStateListener interface.
void flush_pending_advertisements_()
Caller must ensure api_connection_ is non-null and API server is connected.
static void uint64_to_bd_addr(uint64_t address, esp_bd_addr_t bd_addr)
void send_device_clear_cache(uint64_t address, bool success, esp_err_t error=ESP_OK)
std::array< BluetoothConnection *, BLUETOOTH_PROXY_MAX_CONNECTIONS > connections_
api::BluetoothConnectionsFreeResponse connections_free_response_
void log_connection_request_ignored_(BluetoothConnection *connection, espbt::ClientState state)
Listener interface for BLE scanner state changes.
const char * message
Definition component.cpp:35
uint16_t type
uint16_t flags
bool state
Definition fan.h:2
BluetoothProxy * global_bluetooth_proxy
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:1435
static void uint32_t