3#if defined(USE_ESP32_BLE) && defined(USE_BLE_GATT_CLIENT)
7#ifdef USE_BLUETOOTH_PROXY_CONNECTIONS
20#include <esp_idf_version.h>
23#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 5) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(6, 0, 0)) || \
24 ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 1)
25#define BLUEDROID_HAS_CANCEL_OPEN
32using ble_device_base::FAST_CONN_TIMEOUT;
33using ble_device_base::FAST_MAX_CONN_INTERVAL;
34using ble_device_base::FAST_MIN_CONN_INTERVAL;
35using ble_device_base::MEDIUM_CONN_TIMEOUT;
36using ble_device_base::MEDIUM_MAX_CONN_INTERVAL;
37using ble_device_base::MEDIUM_MIN_CONN_INTERVAL;
53 static uint8_t connection_index = 0;
62 auto st = this->
state();
63 if (st == ClientState::INIT) {
66 auto ret = esp_ble_gattc_app_register(this->
app_id);
68 ESP_LOGE(TAG,
"gattc app register failed: app_id=%d code=%d", this->
app_id,
ret);
70 LOG_STR(
"Registration failed; if the error was ESP_GATT_NO_RESOURCES, reduce the connection slots"));
80 }
else if (st == ClientState::CONNECTING) {
84 ESP_LOGE(TAG,
"[%d] Timeout waiting for teardown, forcing IDLE", this->
connection_index_);
94 if (this->
state() == ClientState::IDLE) {
103 auto st = this->
state();
104 if (st != ClientState::IDLE && st != ClientState::INIT) {
121 if (this->
gattc_if_ == ESP_GATT_IF_NONE) {
123 ESP_LOGW(TAG,
"[%d] Connect rejected, GATT app not registered", this->
connection_index_);
124 return ble_device_base::GATT_ERR_NOT_CONNECTED;
128 if (this->
state() != ClientState::IDLE) {
130 return ESP_GATT_BUSY;
136 this->
set_state(ClientState::DISCOVERED);
141 auto st = this->
state();
142 if (st == ClientState::CONNECTING || st == ClientState::CONNECTED || st == ClientState::ESTABLISHED) {
146 if (st == ClientState::DISCONNECTING) {
147 ESP_LOGW(TAG,
"[%d] Cannot connect, still waiting for CLOSE_EVT", this->
connection_index_);
159 this->
set_state(ClientState::CONNECTING);
160 uint16_t min_interval = MEDIUM_MIN_CONN_INTERVAL;
161 uint16_t max_interval = MEDIUM_MAX_CONN_INTERVAL;
162 uint16_t timeout = MEDIUM_CONN_TIMEOUT;
164#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
172 min_interval = FAST_MIN_CONN_INTERVAL;
173 max_interval = FAST_MAX_CONN_INTERVAL;
174 timeout = FAST_CONN_TIMEOUT;
178 "esp_ble_gap_set_prefer_conn_params",
179 esp_ble_gap_set_prefer_conn_params(this->
remote_bda_, min_interval, max_interval, 0, timeout));
191 auto st = this->
state();
192 if (st == ClientState::DISCONNECTING) {
197 if (st == ClientState::IDLE) {
198 return ble_device_base::GATT_ERR_NOT_CONNECTED;
200 if (st == ClientState::DISCOVERED) {
203 return ble_device_base::GATT_ERR_NOT_CONNECTED;
222#ifdef BLUEDROID_HAS_CANCEL_OPEN
223 esp_ble_gattc_cancel_open_params_t params{};
225 memcpy(params.remote_bda, this->remote_bda_,
sizeof(esp_bd_addr_t));
227 if (this->
check_and_log_error_(
"esp_ble_gattc_cancel_open", esp_ble_gattc_cancel_open(¶ms)) == ESP_OK) {
246 ESP_LOGE(TAG,
"[%d] esp_ble_gattc_close error: %d", this->
connection_index_, err);
268 return ble_device_base::GATT_ERR_NOT_CONNECTED;
298 return ble_device_base::GATT_ERR_NOT_CONNECTED;
301 handle, ESP_GATT_AUTH_REQ_NONE));
306 return ble_device_base::GATT_ERR_NOT_CONNECTED;
310 "esp_ble_gattc_write_char",
311 esp_ble_gattc_write_char(this->
gattc_if_, this->
conn_id_, handle,
len,
const_cast<uint8_t *
>(data),
312 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP,
313 ESP_GATT_AUTH_REQ_NONE));
318 return ble_device_base::GATT_ERR_NOT_CONNECTED;
321 "esp_ble_gattc_read_char_descr",
322 esp_ble_gattc_read_char_descr(this->
gattc_if_, this->
conn_id_, handle, ESP_GATT_AUTH_REQ_NONE));
327 return ble_device_base::GATT_ERR_NOT_CONNECTED;
330 "esp_ble_gattc_write_char_descr",
331 esp_ble_gattc_write_char_descr(this->
gattc_if_, this->
conn_id_, handle,
len,
const_cast<uint8_t *
>(data),
332 ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE));
337 return ble_device_base::GATT_ERR_NOT_CONNECTED;
350 return ble_device_base::GATT_ERR_NOT_CONNECTED;
352 return esp_ble_set_encryption(this->
remote_bda_, ESP_BLE_SEC_ENCRYPT);
364#ifndef CONFIG_BT_GATTC_CACHE_NVS_FLASH
377 return memcmp(addr, this->
remote_bda_,
sizeof(esp_bd_addr_t)) == 0;
389#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
400 this->
set_state(ClientState::DISCONNECTING);
406 uint16_t timeout,
const char *param_type) {
407 esp_ble_conn_update_params_t conn_params = {};
408 memcpy(conn_params.bda, this->remote_bda_,
sizeof(esp_bd_addr_t));
409 conn_params.min_int = min_interval;
410 conn_params.max_int = max_interval;
411 conn_params.latency = latency;
412 conn_params.timeout = timeout;
414 return this->
check_and_log_error_(
"esp_ble_gap_update_conn_params", esp_ble_gap_update_conn_params(&conn_params));
425 ESP_LOGW(TAG,
"[%d] %s failed, status=%d", this->
connection_index_, operation, code);
433 this->
update_conn_params_(MEDIUM_MIN_CONN_INTERVAL, MEDIUM_MAX_CONN_INTERVAL, 0, MEDIUM_CONN_TIMEOUT,
"medium");
434 if (
status != ESP_GATT_OK) {
440 uint16_t primary = 0;
441 uint16_t secondary = 0;
442 auto primary_status = esp_ble_gattc_get_attr_count(this->
gattc_if_, this->
conn_id_, ESP_GATT_DB_PRIMARY_SERVICE,
443 0x0001, 0xFFFF, 0, &primary);
444 auto secondary_status = esp_ble_gattc_get_attr_count(this->
gattc_if_, this->
conn_id_, ESP_GATT_DB_SECONDARY_SERVICE,
445 0x0001, 0xFFFF, 0, &secondary);
446 if (primary_status != ESP_GATT_OK || secondary_status != ESP_GATT_OK) {
448 auto count_status = primary_status != ESP_GATT_OK ? primary_status : secondary_status;
465#ifdef USE_BLUETOOTH_PROXY_CONNECTIONS
490 if (api_conn ==
nullptr) {
503 esp_gattc_service_elem_t service_result;
504 uint16_t svc_count = 1;
505 esp_gatt_status_t svc_status = esp_ble_gattc_get_service(this->
gattc_if_, this->
conn_id_,
nullptr, &service_result,
507 if (svc_status != ESP_GATT_OK || svc_count == 0) {
508 ESP_LOGE(TAG,
"[%d] [%s] Service walk failed (service %d), aborting stream", conn.
connection_index_,
513 uint16_t total_char_count = 0;
514 auto char_count_status =
515 esp_ble_gattc_get_attr_count(this->
gattc_if_, this->
conn_id_, ESP_GATT_DB_CHARACTERISTIC,
516 service_result.start_handle, service_result.end_handle, 0, &total_char_count);
517 if (char_count_status != ESP_GATT_OK) {
525 if (!resp.
services.empty() && current_size + estimated_size > MAX_PACKET_SIZE) {
530 auto &service_resp = resp.
services.back();
533 service_resp.handle = service_result.start_handle;
535 if (total_char_count > 0) {
536 service_resp.characteristics.init(total_char_count);
537 uint16_t char_offset = 0;
538 esp_gattc_char_elem_t char_result;
541 while (char_offset < total_char_count) {
543 auto char_status = esp_ble_gattc_get_all_char(this->
gattc_if_, this->
conn_id_, service_result.start_handle,
544 service_result.end_handle, &char_result, &cc, char_offset);
545 if (char_status != ESP_GATT_OK || cc == 0) {
552 service_resp.characteristics.emplace_back();
553 auto &characteristic_resp = service_resp.characteristics.back();
554 fill_gatt_uuid(characteristic_resp.uuid, characteristic_resp.short_uuid,
556 characteristic_resp.handle = char_result.char_handle;
557 characteristic_resp.properties = char_result.properties;
559 uint16_t total_desc_count = 0;
560 auto desc_count_status = esp_ble_gattc_get_attr_count(this->
gattc_if_, this->
conn_id_, ESP_GATT_DB_DESCRIPTOR,
561 0, 0, char_result.char_handle, &total_desc_count);
562 if (desc_count_status != ESP_GATT_OK) {
569 if (total_desc_count > 0) {
570 characteristic_resp.descriptors.init(total_desc_count);
571 uint16_t desc_offset = 0;
572 esp_gattc_descr_elem_t desc_result;
573 while (desc_offset < total_desc_count) {
575 auto desc_status = esp_ble_gattc_get_all_descr(this->
gattc_if_, this->
conn_id_, char_result.char_handle,
576 &desc_result, &dc, desc_offset);
577 if (desc_status != ESP_GATT_OK || dc == 0) {
582 characteristic_resp.descriptors.emplace_back();
583 auto &descriptor_resp = characteristic_resp.descriptors.back();
586 descriptor_resp.handle = desc_result.handle;
602 if (!api_conn->send_message(resp)) {
614 auto st = this->
state();
615 if (st == ClientState::IDLE) {
618 ESP_LOGD(TAG,
"[%d] OPEN_EVT in IDLE state (status=%d)", this->
connection_index_, param->open.status);
619 if (param->open.status == ESP_GATT_OK || param->open.status == ESP_GATT_ALREADY_OPEN) {
625 if (st != ClientState::CONNECTING) {
628 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
636 this->
conn_id_ = param->open.conn_id;
648 this->
set_state(ClientState::ESTABLISHED);
659 esp_ble_gattc_search_service(this->
gattc_if_, param->open.conn_id,
nullptr)) == 0) {
673 if (param->disconnect.reason == ESP_GATT_CONN_TERMINATE_PEER_USER && this->state() == ClientState::CONNECTED) {
676 ESP_LOGD(TAG,
"[%d] DISCONNECT_EVT reason=0x%02x", this->
connection_index_, param->disconnect.reason);
679 if (this->
state() == ClientState::CONNECTING && param->disconnect.reason == ESP_GATT_CONN_FAIL_ESTABLISH)
681 if (this->
state() == ClientState::IDLE) {
699 esp_ble_gattc_cb_param_t *param) {
700 if (event == ESP_GATTC_REG_EVT && this->
app_id != param->reg.app_id)
702 if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && esp_gattc_if != this->
gattc_if_)
706 case ESP_GATTC_REG_EVT: {
707 if (param->reg.status == ESP_GATT_OK) {
710 ESP_LOGE(TAG,
"[%d] gattc app registration failed, status=%d", this->
connection_index_, param->reg.status);
712 LOG_STR(
"Registration failed; if the error was ESP_GATT_NO_RESOURCES, reduce the connection slots"));
716 case ESP_GATTC_CONNECT_EVT: {
721 if (this->
state() != ClientState::CONNECTING)
723 this->
conn_id_ = param->connect.conn_id;
725 auto ret = esp_ble_gattc_send_mtu_req(this->
gattc_if_, param->connect.conn_id);
733 case ESP_GATTC_OPEN_EVT: {
736 if (param->open.status == ESP_GATT_OK || param->open.status == ESP_GATT_ALREADY_OPEN) {
737 ESP_LOGW(TAG,
"[%d] Closing link left by an abandoned open", this->
connection_index_);
746 case ESP_GATTC_CFG_MTU_EVT: {
747 if (this->
conn_id_ != param->cfg_mtu.conn_id)
749 if (param->cfg_mtu.status != ESP_GATT_OK) {
759 true, param->cfg_mtu.status == ESP_GATT_OK ? param->cfg_mtu.mtu : ble_device_base::DEFAULT_ATT_MTU, 0);
766 case ESP_GATTC_DISCONNECT_EVT: {
767 if (!this->
check_addr_(param->disconnect.remote_bda))
772 case ESP_GATTC_CLOSE_EVT: {
773 if (this->
conn_id_ != param->close.conn_id)
782 case ESP_GATTC_SEARCH_CMPL_EVT: {
783 if (this->
conn_id_ != param->search_cmpl.conn_id)
786 if (this->
state() == ClientState::DISCONNECTING) {
794 this->
set_state(ClientState::ESTABLISHED);
798 case ESP_GATTC_READ_CHAR_EVT:
799 case ESP_GATTC_READ_DESCR_EVT: {
800 if (this->
conn_id_ != param->read.conn_id)
802 bool ok = param->read.status == ESP_GATT_OK;
804 ok ? param->read.value_len : 0, ok ? 0 : param->read.status);
807 case ESP_GATTC_WRITE_CHAR_EVT:
808 case ESP_GATTC_WRITE_DESCR_EVT: {
809 if (this->
conn_id_ != param->write.conn_id)
812 param->write.status == ESP_GATT_OK ? 0 : param->write.status);
815 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
817 param->reg_for_notify.status == ESP_GATT_OK ? 0 : param->reg_for_notify.status);
820 case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
822 param->unreg_for_notify.handle,
false,
823 param->unreg_for_notify.status == ESP_GATT_OK ? 0 : param->unreg_for_notify.status);
826 case ESP_GATTC_NOTIFY_EVT: {
827 if (this->
conn_id_ != param->notify.conn_id)
829 ESP_LOGV(TAG,
"[%d] NOTIFY_EVT handle=0x%2X", this->
connection_index_, param->notify.handle);
833#ifdef BLUEDROID_HAS_CANCEL_OPEN
834 case ESP_GATTC_CANCEL_OPEN_EVT: {
835 if (!this->
check_addr_(param->cancel_open.remote_bda))
837 if (param->cancel_open.status != ESP_GATT_OK) {
859 case ESP_GAP_BLE_SEC_REQ_EVT: {
860 if (!this->
check_addr_(param->ble_security.auth_cmpl.bd_addr))
865 esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr,
true));
871 case ESP_GAP_BLE_AUTH_CMPL_EVT: {
872 if (!this->
check_addr_(param->ble_security.auth_cmpl.bd_addr))
875 param->ble_security.auth_cmpl.success ? 0 : param->ble_security.auth_cmpl.fail_reason);
void mark_failed()
Mark this component as failed.
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
uint32_t calculate_size() const
std::vector< BluetoothGATTService > services
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Source compatibility with the historical esp32_ble API (esp32 builds only).
virtual void on_notify_state(uint16_t handle, bool enabled, int error)
virtual void on_notify_data(uint16_t handle, const uint8_t *data, uint16_t len)
virtual void on_write_result(uint16_t handle, int error)
virtual void on_service_discovery_done(int error)
virtual void on_pairing_result(int status)
virtual void on_connection_state(bool connected, uint16_t mtu, int error)
virtual void on_read_result(uint16_t handle, const uint8_t *data, uint16_t len, int error)
virtual void on_link_open(bool established)
The link came up (true) or failed to establish with HCI 0x3e (false).
void cancel_pending_open_()
bool check_addr_(const esp_bd_addr_t &addr) const
void disconnect() override
uint32_t disconnecting_started_
int handle_search_cmpl_(esp_gatt_status_t status)
void release_coex_priority_()
esp_err_t update_conn_params_(uint16_t min_interval, uint16_t max_interval, uint16_t latency, uint16_t timeout, const char *param_type)
int read_descriptor(uint16_t handle)
SearchState search_state_
esp_bd_addr_t remote_bda_
void ble_before_disabled_event_handler() override
ble_device_base::GattClientListener * listener_
void handle_open_evt_(esp_ble_gattc_cb_param_t *param)
int write_descriptor(uint16_t handle, const uint8_t *data, uint16_t len)
uint8_t remote_addr_type_
int write_characteristic(uint16_t handle, const uint8_t *data, uint16_t len, bool response)
void handle_disconnect_evt_(esp_ble_gattc_cb_param_t *param)
bool cancel_gatt_disconnect()
static constexpr uint16_t UNSET_CONN_ID
esp32_ble_tracker::ConnectionType connection_type_
uint8_t connection_index_
void unconditional_disconnect_()
int update_connection_params(uint16_t min_interval, uint16_t max_interval, uint16_t latency, uint16_t timeout)
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
void dump_config() override
void deliver_pending_search_()
void stream_service_batch(BluetoothConnection &conn)
In-place service streamer (the proxy wrapper detects and prefers it): builds one api response batch d...
int read_characteristic(uint16_t handle)
int check_and_log_error_(const char *operation, esp_err_t err)
int notify_characteristic(uint16_t handle, bool enable)
void set_disconnecting_()
void log_gattc_warning_(const char *operation, int code)
void abort_service_stream(conn_err_t err)
Streamer abort: latch the GATT cause, park the cursor, tear down.
bool batch_stalled_
Set while a refused batch is retrying, so only the first one warns.
void send_services_done_()
Send services-done and settle the cursor: DONE when it lands (or no subscriber), SERVICES_DONE_PENDIN...
void park_service_stream_()
Park the stream without services-done and free any held table: an interrupted stream must never be de...
bluetooth_proxy::BluetoothProxy * proxy_
uint8_t connection_index_
void note_batch_stalled_()
Warn on the stall's leading edge only.
char address_str_[MAC_ADDRESS_PRETTY_BUFFER_SIZE]
api::APIConnection * get_api_connection()
bool client_supports_efficient_uuids() const
Whether the subscribed API client understands 16/32-bit UUID fields.
ClientState state() const
virtual void set_state(ClientState st)
Set the client state with IDLE handling (clears want_disconnect_).
bool disconnect_pending() const
void uint64_to_mac_msb_first(uint64_t address, uint8_t out[6])
Unpack a uint64 BLE address into printable (MSB-first) byte order — the order bd_addr_t / esp_bd_addr...
size_t estimate_service_size(uint16_t char_count, bool use_efficient_uuids)
Estimate the wire size of a service (service overhead + its characteristics, assuming 128-bit UUIDs a...
ESPHOME_LOG_TAG(TAG, "bluetooth_connection")
BatchClose close_service_batch(api::BluetoothGATTGetServicesResponse &resp, size_t ¤t_size, int16_t &send_service, uint8_t connection_index, const char *address_str)
Close out the service just packed into resp (account its actual wire size, advance the cursor) and de...
void fill_gatt_uuid(std::array< uint64_t, 2 > &uuid_128, uint32_t &short_uuid, const ble_device_base::ESPBTUUID &uuid, bool use_efficient_uuids)
Fill the UUID in the appropriate wire format based on client support and UUID type (128-bit array for...
void coex_ble_priority_release()
void coex_ble_priority_acquire()
Give BLE priority over WiFi on the shared radio while a GATT client discovers services.
uint32_t IRAM_ATTR HOT millis()
spi_device_handle_t handle