ESPHome 2025.5.0
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 <map>
6#include <vector>
7
15
17
18#include <esp_bt.h>
19#include <esp_bt_device.h>
20
21namespace esphome {
22namespace bluetooth_proxy {
23
24static const esp_err_t ESP_GATT_NOT_CONNECTED = -1;
25
26using namespace esp32_ble_client;
27
28// Legacy versions:
29// Version 1: Initial version without active connections
30// Version 2: Support for active connections
31// Version 3: New connection API
32// Version 4: Pairing support
33// Version 5: Cache clear support
34static const uint32_t LEGACY_ACTIVE_CONNECTIONS_VERSION = 5;
35static const uint32_t LEGACY_PASSIVE_ONLY_VERSION = 1;
36
46
50
52 public:
54 bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
55 bool parse_devices(esp_ble_gap_cb_param_t::ble_scan_result_evt_param *advertisements, size_t count) override;
56 void dump_config() override;
57 void setup() override;
58 void loop() override;
61
63 this->connections_.push_back(connection);
64 connection->proxy_ = this;
65 }
66
74
76 int get_bluetooth_connections_limit() { return this->connections_.size(); }
77
78 void subscribe_api_connection(api::APIConnection *api_connection, uint32_t flags);
81
82 void send_device_connection(uint64_t address, bool connected, uint16_t mtu = 0, esp_err_t error = ESP_OK);
84 void send_gatt_services_done(uint64_t address);
85 void send_gatt_error(uint64_t address, uint16_t handle, esp_err_t error);
86 void send_device_pairing(uint64_t address, bool paired, esp_err_t error = ESP_OK);
87 void send_device_unpairing(uint64_t address, bool success, esp_err_t error = ESP_OK);
88 void send_device_clear_cache(uint64_t address, bool success, esp_err_t error = ESP_OK);
89
90 void bluetooth_scanner_set_mode(bool active);
91
92 static void uint64_to_bd_addr(uint64_t address, esp_bd_addr_t bd_addr) {
93 bd_addr[0] = (address >> 40) & 0xff;
94 bd_addr[1] = (address >> 32) & 0xff;
95 bd_addr[2] = (address >> 24) & 0xff;
96 bd_addr[3] = (address >> 16) & 0xff;
97 bd_addr[4] = (address >> 8) & 0xff;
98 bd_addr[5] = (address >> 0) & 0xff;
99 }
100
101 void set_active(bool active) { this->active_ = active; }
102 bool has_active() { return this->active_; }
103
104 uint32_t get_legacy_version() const {
105 if (this->active_) {
106 return LEGACY_ACTIVE_CONNECTIONS_VERSION;
107 }
108 return LEGACY_PASSIVE_ONLY_VERSION;
109 }
110
125
127 const uint8_t *mac = esp_bt_dev_get_address();
128 return str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
129 }
130
131 protected:
134
135 BluetoothConnection *get_connection_(uint64_t address, bool reserve);
136
138
139 std::vector<BluetoothConnection *> connections_{};
142};
143
144extern BluetoothProxy *global_bluetooth_proxy; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
145
146} // namespace bluetooth_proxy
147} // namespace esphome
148
149#endif // USE_ESP32
uint8_t address
Definition bl0906.h:4
void bluetooth_gatt_read(const api::BluetoothGATTReadRequest &msg)
void bluetooth_gatt_send_services(const api::BluetoothGATTGetServicesRequest &msg)
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)
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)
BluetoothConnection * get_connection_(uint64_t address, bool reserve)
bool parse_devices(esp_ble_gap_cb_param_t::ble_scan_result_evt_param *advertisements, size_t count) override
void bluetooth_gatt_read_descriptor(const api::BluetoothGATTReadDescriptorRequest &msg)
void bluetooth_gatt_write(const api::BluetoothGATTWriteRequest &msg)
std::vector< BluetoothConnection * > connections_
void bluetooth_gatt_notify(const api::BluetoothGATTNotifyRequest &msg)
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)
void send_api_packet_(const esp32_ble_tracker::ESPBTDevice &device)
bool state
Definition fan.h:0
BluetoothProxy * global_bluetooth_proxy
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string str_snprintf(const char *fmt, size_t len,...)
Definition helpers.cpp:309