ESPHome 2025.8.0
Loading...
Searching...
No Matches
bluetooth_connection.cpp
Go to the documentation of this file.
2
5#include "esphome/core/log.h"
6
7#ifdef USE_ESP32
8
9#include "bluetooth_proxy.h"
10
12
13static const char *const TAG = "bluetooth_proxy.connection";
14
15// This function is allocation-free and directly packs UUIDs into the output array
16// using precalculated constants for the Bluetooth base UUID
17static void fill_128bit_uuid_array(std::array<uint64_t, 2> &out, esp_bt_uuid_t uuid_source) {
18 // Bluetooth base UUID: 00000000-0000-1000-8000-00805F9B34FB
19 // out[0] = bytes 8-15 (big-endian)
20 // - For 128-bit UUIDs: use bytes 8-15 as-is
21 // - For 16/32-bit UUIDs: insert into bytes 12-15, use 0x00001000 for bytes 8-11
22 out[0] = uuid_source.len == ESP_UUID_LEN_128
23 ? (((uint64_t) uuid_source.uuid.uuid128[15] << 56) | ((uint64_t) uuid_source.uuid.uuid128[14] << 48) |
24 ((uint64_t) uuid_source.uuid.uuid128[13] << 40) | ((uint64_t) uuid_source.uuid.uuid128[12] << 32) |
25 ((uint64_t) uuid_source.uuid.uuid128[11] << 24) | ((uint64_t) uuid_source.uuid.uuid128[10] << 16) |
26 ((uint64_t) uuid_source.uuid.uuid128[9] << 8) | ((uint64_t) uuid_source.uuid.uuid128[8]))
27 : (((uint64_t) (uuid_source.len == ESP_UUID_LEN_16 ? uuid_source.uuid.uuid16 : uuid_source.uuid.uuid32)
28 << 32) |
29 0x00001000ULL); // Base UUID bytes 8-11
30 // out[1] = bytes 0-7 (big-endian)
31 // - For 128-bit UUIDs: use bytes 0-7 as-is
32 // - For 16/32-bit UUIDs: use precalculated base UUID constant
33 out[1] = uuid_source.len == ESP_UUID_LEN_128
34 ? ((uint64_t) uuid_source.uuid.uuid128[7] << 56) | ((uint64_t) uuid_source.uuid.uuid128[6] << 48) |
35 ((uint64_t) uuid_source.uuid.uuid128[5] << 40) | ((uint64_t) uuid_source.uuid.uuid128[4] << 32) |
36 ((uint64_t) uuid_source.uuid.uuid128[3] << 24) | ((uint64_t) uuid_source.uuid.uuid128[2] << 16) |
37 ((uint64_t) uuid_source.uuid.uuid128[1] << 8) | ((uint64_t) uuid_source.uuid.uuid128[0])
38 : 0x800000805F9B34FBULL; // Base UUID bytes 0-7: 80-00-00-80-5F-9B-34-FB
39}
40
41// Helper to fill UUID in the appropriate format based on client support and UUID type
42static void fill_gatt_uuid(std::array<uint64_t, 2> &uuid_128, uint32_t &short_uuid, const esp_bt_uuid_t &uuid,
43 bool use_efficient_uuids) {
44 if (!use_efficient_uuids || uuid.len == ESP_UUID_LEN_128) {
45 // Use 128-bit format for old clients or when UUID is already 128-bit
46 fill_128bit_uuid_array(uuid_128, uuid);
47 } else if (uuid.len == ESP_UUID_LEN_16) {
48 short_uuid = uuid.uuid.uuid16;
49 } else if (uuid.len == ESP_UUID_LEN_32) {
50 short_uuid = uuid.uuid.uuid32;
51 }
52}
53
54// Constants for size estimation
55static constexpr uint8_t SERVICE_OVERHEAD_LEGACY = 25; // UUID(20) + handle(4) + overhead(1)
56static constexpr uint8_t SERVICE_OVERHEAD_EFFICIENT = 10; // UUID(6) + handle(4)
57static constexpr uint8_t CHAR_SIZE_128BIT = 35; // UUID(20) + handle(4) + props(4) + overhead(7)
58static constexpr uint8_t DESC_SIZE_128BIT = 25; // UUID(20) + handle(4) + overhead(1)
59static constexpr uint8_t DESC_SIZE_16BIT = 10; // UUID(6) + handle(4)
60static constexpr uint8_t DESC_PER_CHAR = 1; // Assume 1 descriptor per characteristic
61
62// Helper to estimate service size before fetching all data
75static size_t estimate_service_size(uint16_t char_count, bool use_efficient_uuids) {
76 size_t service_overhead = use_efficient_uuids ? SERVICE_OVERHEAD_EFFICIENT : SERVICE_OVERHEAD_LEGACY;
77 // Always assume 128-bit UUIDs for characteristics to be safe
78 size_t char_size = CHAR_SIZE_128BIT;
79 // Assume one 128-bit descriptor per characteristic
80 size_t desc_size = DESC_SIZE_128BIT * DESC_PER_CHAR;
81
82 return service_overhead + (char_size + desc_size) * char_count;
83}
84
86 auto *api_conn = this->proxy_->get_api_connection();
87 return api_conn && api_conn->client_supports_api_version(1, 12);
88}
89
91 ESP_LOGCONFIG(TAG, "BLE Connection:");
93}
94
95void BluetoothConnection::update_allocated_slot_(uint64_t find_value, uint64_t set_value) {
96 auto &allocated = this->proxy_->connections_free_response_.allocated;
97 for (auto &slot : allocated) {
98 if (slot == find_value) {
99 slot = set_value;
100 return;
101 }
102 }
103}
104
106 // If we're clearing an address (disconnecting), update the pre-allocated message
107 if (address == 0 && this->address_ != 0) {
109 this->update_allocated_slot_(this->address_, 0);
110 }
111 // If we're setting a new address (connecting), update the pre-allocated message
112 else if (address != 0 && this->address_ == 0) {
114 this->update_allocated_slot_(0, address);
115 }
116
117 // Call parent implementation to actually set the address
119}
120
123
124 // Early return if no active connection
125 if (this->address_ == 0) {
126 return;
127 }
128
129 // Handle service discovery if in valid range
132 }
133
134 // Check if we should disable the loop
135 // - For V3_WITH_CACHE: Services are never sent, disable after INIT state
136 // - For V3_WITHOUT_CACHE: Disable only after service discovery is complete
137 // (send_service_ == DONE_SENDING_SERVICES, which is only set after services are sent)
138 if (this->state_ != espbt::ClientState::INIT && (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE ||
139 this->send_service_ == DONE_SENDING_SERVICES)) {
140 this->disable_loop();
141 }
142}
143
145 // Send disconnection notification
146 this->proxy_->send_device_connection(this->address_, false, 0, reason);
147
148 // Important: If we were in the middle of sending services, we do NOT send
149 // send_gatt_services_done() here. This ensures the client knows that
150 // the service discovery was interrupted and can retry. The client
151 // (aioesphomeapi) implements a 30-second timeout (DEFAULT_BLE_TIMEOUT)
152 // to detect incomplete service discovery rather than relying on us to
153 // tell them about a partial list.
154 this->set_address(0);
155 this->send_service_ = INIT_SENDING_SERVICES;
157}
158
160 if (this->send_service_ >= this->service_count_) {
161 this->send_service_ = DONE_SENDING_SERVICES;
163 this->release_services();
164 return;
165 }
166
167 // Early return if no API connection
168 auto *api_conn = this->proxy_->get_api_connection();
169 if (api_conn == nullptr) {
170 this->send_service_ = DONE_SENDING_SERVICES;
171 return;
172 }
173
174 // Check if client supports efficient UUIDs
175 bool use_efficient_uuids = this->supports_efficient_uuids_();
176
177 // Prepare response
179 resp.address = this->address_;
180
181 // Dynamic batching based on actual size
182 // Conservative MTU limit for API messages (accounts for WPA3 overhead)
183 static constexpr size_t MAX_PACKET_SIZE = 1360;
184
185 // Keep running total of actual message size
186 size_t current_size = 0;
187 api::ProtoSize size;
188 resp.calculate_size(size);
189 current_size = size.get_size();
190
192 esp_gattc_service_elem_t service_result;
193 uint16_t service_count = 1;
194 esp_gatt_status_t service_status = esp_ble_gattc_get_service(this->gattc_if_, this->conn_id_, nullptr,
195 &service_result, &service_count, this->send_service_);
196
197 if (service_status != ESP_GATT_OK || service_count == 0) {
198 ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_service %s, status=%d, service_count=%d, offset=%d",
199 this->connection_index_, this->address_str().c_str(),
200 service_status != ESP_GATT_OK ? "error" : "missing", service_status, service_count, this->send_service_);
201 this->send_service_ = DONE_SENDING_SERVICES;
202 return;
203 }
204
205 // Get the number of characteristics BEFORE adding to response
206 uint16_t total_char_count = 0;
207 esp_gatt_status_t char_count_status =
208 esp_ble_gattc_get_attr_count(this->gattc_if_, this->conn_id_, ESP_GATT_DB_CHARACTERISTIC,
209 service_result.start_handle, service_result.end_handle, 0, &total_char_count);
210
211 if (char_count_status != ESP_GATT_OK) {
212 this->log_connection_error_("esp_ble_gattc_get_attr_count", char_count_status);
213 this->send_service_ = DONE_SENDING_SERVICES;
214 return;
215 }
216
217 // If this service likely won't fit, send current batch (unless it's the first)
218 size_t estimated_size = estimate_service_size(total_char_count, use_efficient_uuids);
219 if (!resp.services.empty() && (current_size + estimated_size > MAX_PACKET_SIZE)) {
220 // This service likely won't fit, send current batch
221 break;
222 }
223
224 // Now add the service since we know it will likely fit
225 resp.services.emplace_back();
226 auto &service_resp = resp.services.back();
227
228 fill_gatt_uuid(service_resp.uuid, service_resp.short_uuid, service_result.uuid, use_efficient_uuids);
229
230 service_resp.handle = service_result.start_handle;
231
232 if (total_char_count > 0) {
233 // Reserve space and process characteristics
234 service_resp.characteristics.reserve(total_char_count);
235 uint16_t char_offset = 0;
236 esp_gattc_char_elem_t char_result;
237 while (true) { // characteristics
238 uint16_t char_count = 1;
239 esp_gatt_status_t char_status =
240 esp_ble_gattc_get_all_char(this->gattc_if_, this->conn_id_, service_result.start_handle,
241 service_result.end_handle, &char_result, &char_count, char_offset);
242 if (char_status == ESP_GATT_INVALID_OFFSET || char_status == ESP_GATT_NOT_FOUND) {
243 break;
244 }
245 if (char_status != ESP_GATT_OK) {
246 this->log_connection_error_("esp_ble_gattc_get_all_char", char_status);
247 this->send_service_ = DONE_SENDING_SERVICES;
248 return;
249 }
250 if (char_count == 0) {
251 break;
252 }
253
254 service_resp.characteristics.emplace_back();
255 auto &characteristic_resp = service_resp.characteristics.back();
256
257 fill_gatt_uuid(characteristic_resp.uuid, characteristic_resp.short_uuid, char_result.uuid, use_efficient_uuids);
258
259 characteristic_resp.handle = char_result.char_handle;
260 characteristic_resp.properties = char_result.properties;
261 char_offset++;
262
263 // Get the number of descriptors directly with one call
264 uint16_t total_desc_count = 0;
265 esp_gatt_status_t desc_count_status = esp_ble_gattc_get_attr_count(
266 this->gattc_if_, this->conn_id_, ESP_GATT_DB_DESCRIPTOR, 0, 0, char_result.char_handle, &total_desc_count);
267
268 if (desc_count_status != ESP_GATT_OK) {
269 this->log_connection_error_("esp_ble_gattc_get_attr_count", desc_count_status);
270 this->send_service_ = DONE_SENDING_SERVICES;
271 return;
272 }
273 if (total_desc_count == 0) {
274 // No descriptors, continue to next characteristic
275 continue;
276 }
277
278 // Reserve space and process descriptors
279 characteristic_resp.descriptors.reserve(total_desc_count);
280 uint16_t desc_offset = 0;
281 esp_gattc_descr_elem_t desc_result;
282 while (true) { // descriptors
283 uint16_t desc_count = 1;
284 esp_gatt_status_t desc_status = esp_ble_gattc_get_all_descr(
285 this->gattc_if_, this->conn_id_, char_result.char_handle, &desc_result, &desc_count, desc_offset);
286 if (desc_status == ESP_GATT_INVALID_OFFSET || desc_status == ESP_GATT_NOT_FOUND) {
287 break;
288 }
289 if (desc_status != ESP_GATT_OK) {
290 this->log_connection_error_("esp_ble_gattc_get_all_descr", desc_status);
291 this->send_service_ = DONE_SENDING_SERVICES;
292 return;
293 }
294 if (desc_count == 0) {
295 break; // No more descriptors
296 }
297
298 characteristic_resp.descriptors.emplace_back();
299 auto &descriptor_resp = characteristic_resp.descriptors.back();
300
301 fill_gatt_uuid(descriptor_resp.uuid, descriptor_resp.short_uuid, desc_result.uuid, use_efficient_uuids);
302
303 descriptor_resp.handle = desc_result.handle;
304 desc_offset++;
305 }
306 }
307 } // end if (total_char_count > 0)
308
309 // Calculate the actual size of just this service
310 api::ProtoSize service_sizer;
311 service_resp.calculate_size(service_sizer);
312 size_t service_size = service_sizer.get_size() + 1; // +1 for field tag
313
314 // Check if adding this service would exceed the limit
315 if (current_size + service_size > MAX_PACKET_SIZE) {
316 // We would go over - pop the last service if we have more than one
317 if (resp.services.size() > 1) {
318 resp.services.pop_back();
319 ESP_LOGD(TAG, "[%d] [%s] Service %d would exceed limit (current: %d + service: %d > %d), sending current batch",
320 this->connection_index_, this->address_str().c_str(), this->send_service_, current_size, service_size,
321 MAX_PACKET_SIZE);
322 // Don't increment send_service_ - we'll retry this service in next batch
323 } else {
324 // This single service is too large, but we have to send it anyway
325 ESP_LOGV(TAG, "[%d] [%s] Service %d is too large (%d bytes) but sending anyway", this->connection_index_,
326 this->address_str().c_str(), this->send_service_, service_size);
327 // Increment so we don't get stuck
328 this->send_service_++;
329 }
330 // Send what we have
331 break;
332 }
333
334 // Now we know we're keeping this service, add its size
335 current_size += service_size;
336 // Successfully added this service, increment counter
337 this->send_service_++;
338 }
339
340 // Send the message with dynamically batched services
341 api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE);
342}
343
344void BluetoothConnection::log_connection_error_(const char *operation, esp_gatt_status_t status) {
345 ESP_LOGE(TAG, "[%d] [%s] %s error, status=%d", this->connection_index_, this->address_str().c_str(), operation,
346 status);
347}
348
349void BluetoothConnection::log_connection_warning_(const char *operation, esp_err_t err) {
350 ESP_LOGW(TAG, "[%d] [%s] %s failed, err=%d", this->connection_index_, this->address_str().c_str(), operation, err);
351}
352
353void BluetoothConnection::log_gatt_not_connected_(const char *action, const char *type) {
354 ESP_LOGW(TAG, "[%d] [%s] Cannot %s GATT %s, not connected.", this->connection_index_, this->address_str().c_str(),
355 action, type);
356}
357
358void BluetoothConnection::log_gatt_operation_error_(const char *operation, uint16_t handle, esp_gatt_status_t status) {
359 ESP_LOGW(TAG, "[%d] [%s] Error %s for handle 0x%2X, status=%d", this->connection_index_, this->address_str().c_str(),
360 operation, handle, status);
361}
362
363esp_err_t BluetoothConnection::check_and_log_error_(const char *operation, esp_err_t err) {
364 if (err != ESP_OK) {
365 this->log_connection_warning_(operation, err);
366 return err;
367 }
368 return ESP_OK;
369}
370
371bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
372 esp_ble_gattc_cb_param_t *param) {
373 if (!BLEClientBase::gattc_event_handler(event, gattc_if, param))
374 return false;
375
376 switch (event) {
377 case ESP_GATTC_DISCONNECT_EVT: {
378 // Don't reset connection yet - wait for CLOSE_EVT to ensure controller has freed resources
379 // This prevents race condition where we mark slot as free before controller cleanup is complete
380 ESP_LOGD(TAG, "[%d] [%s] Disconnect, reason=0x%02x", this->connection_index_, this->address_str_.c_str(),
381 param->disconnect.reason);
382 // Send disconnection notification but don't free the slot yet
383 this->proxy_->send_device_connection(this->address_, false, 0, param->disconnect.reason);
384 break;
385 }
386 case ESP_GATTC_CLOSE_EVT: {
387 ESP_LOGD(TAG, "[%d] [%s] Close, reason=0x%02x, freeing slot", this->connection_index_, this->address_str_.c_str(),
388 param->close.reason);
389 // Now the GATT connection is fully closed and controller resources are freed
390 // Safe to mark the connection slot as available
391 this->reset_connection_(param->close.reason);
392 break;
393 }
394 case ESP_GATTC_OPEN_EVT: {
395 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
396 this->reset_connection_(param->open.status);
397 } else if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) {
398 this->proxy_->send_device_connection(this->address_, true, this->mtu_);
400 }
401 this->seen_mtu_or_services_ = false;
402 break;
403 }
404 case ESP_GATTC_CFG_MTU_EVT:
405 case ESP_GATTC_SEARCH_CMPL_EVT: {
406 if (!this->seen_mtu_or_services_) {
407 // We don't know if we will get the MTU or the services first, so
408 // only send the device connection true if we have already received
409 // the services.
410 this->seen_mtu_or_services_ = true;
411 break;
412 }
413 this->proxy_->send_device_connection(this->address_, true, this->mtu_);
415 break;
416 }
417 case ESP_GATTC_READ_DESCR_EVT:
418 case ESP_GATTC_READ_CHAR_EVT: {
419 if (param->read.status != ESP_GATT_OK) {
420 this->log_gatt_operation_error_("reading char/descriptor", param->read.handle, param->read.status);
421 this->proxy_->send_gatt_error(this->address_, param->read.handle, param->read.status);
422 break;
423 }
425 resp.address = this->address_;
426 resp.handle = param->read.handle;
427 resp.set_data(param->read.value, param->read.value_len);
429 break;
430 }
431 case ESP_GATTC_WRITE_CHAR_EVT:
432 case ESP_GATTC_WRITE_DESCR_EVT: {
433 if (param->write.status != ESP_GATT_OK) {
434 this->log_gatt_operation_error_("writing char/descriptor", param->write.handle, param->write.status);
435 this->proxy_->send_gatt_error(this->address_, param->write.handle, param->write.status);
436 break;
437 }
439 resp.address = this->address_;
440 resp.handle = param->write.handle;
442 break;
443 }
444 case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
445 if (param->unreg_for_notify.status != ESP_GATT_OK) {
446 this->log_gatt_operation_error_("unregistering notifications", param->unreg_for_notify.handle,
447 param->unreg_for_notify.status);
448 this->proxy_->send_gatt_error(this->address_, param->unreg_for_notify.handle, param->unreg_for_notify.status);
449 break;
450 }
452 resp.address = this->address_;
453 resp.handle = param->unreg_for_notify.handle;
455 break;
456 }
457 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
458 if (param->reg_for_notify.status != ESP_GATT_OK) {
459 this->log_gatt_operation_error_("registering notifications", param->reg_for_notify.handle,
460 param->reg_for_notify.status);
461 this->proxy_->send_gatt_error(this->address_, param->reg_for_notify.handle, param->reg_for_notify.status);
462 break;
463 }
465 resp.address = this->address_;
466 resp.handle = param->reg_for_notify.handle;
468 break;
469 }
470 case ESP_GATTC_NOTIFY_EVT: {
471 ESP_LOGV(TAG, "[%d] [%s] ESP_GATTC_NOTIFY_EVT: handle=0x%2X", this->connection_index_, this->address_str_.c_str(),
472 param->notify.handle);
474 resp.address = this->address_;
475 resp.handle = param->notify.handle;
476 resp.set_data(param->notify.value, param->notify.value_len);
478 break;
479 }
480 default:
481 break;
482 }
483 return true;
484}
485
486void BluetoothConnection::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
488
489 switch (event) {
490 case ESP_GAP_BLE_AUTH_CMPL_EVT:
491 if (memcmp(param->ble_security.auth_cmpl.bd_addr, this->remote_bda_, 6) != 0)
492 break;
493 if (param->ble_security.auth_cmpl.success) {
494 this->proxy_->send_device_pairing(this->address_, true);
495 } else {
496 this->proxy_->send_device_pairing(this->address_, false, param->ble_security.auth_cmpl.fail_reason);
497 }
498 break;
499 default:
500 break;
501 }
502}
503
505 if (!this->connected()) {
506 this->log_gatt_not_connected_("read", "characteristic");
507 return ESP_GATT_NOT_CONNECTED;
508 }
509
510 ESP_LOGV(TAG, "[%d] [%s] Reading GATT characteristic handle %d", this->connection_index_, this->address_str_.c_str(),
511 handle);
512
513 esp_err_t err = esp_ble_gattc_read_char(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
514 return this->check_and_log_error_("esp_ble_gattc_read_char", err);
515}
516
517esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const std::string &data, bool response) {
518 if (!this->connected()) {
519 this->log_gatt_not_connected_("write", "characteristic");
520 return ESP_GATT_NOT_CONNECTED;
521 }
522 ESP_LOGV(TAG, "[%d] [%s] Writing GATT characteristic handle %d", this->connection_index_, this->address_str_.c_str(),
523 handle);
524
525 esp_err_t err =
526 esp_ble_gattc_write_char(this->gattc_if_, this->conn_id_, handle, data.size(), (uint8_t *) data.data(),
527 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
528 return this->check_and_log_error_("esp_ble_gattc_write_char", err);
529}
530
531esp_err_t BluetoothConnection::read_descriptor(uint16_t handle) {
532 if (!this->connected()) {
533 this->log_gatt_not_connected_("read", "descriptor");
534 return ESP_GATT_NOT_CONNECTED;
535 }
536 ESP_LOGV(TAG, "[%d] [%s] Reading GATT descriptor handle %d", this->connection_index_, this->address_str_.c_str(),
537 handle);
538
539 esp_err_t err = esp_ble_gattc_read_char_descr(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
540 return this->check_and_log_error_("esp_ble_gattc_read_char_descr", err);
541}
542
543esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const std::string &data, bool response) {
544 if (!this->connected()) {
545 this->log_gatt_not_connected_("write", "descriptor");
546 return ESP_GATT_NOT_CONNECTED;
547 }
548 ESP_LOGV(TAG, "[%d] [%s] Writing GATT descriptor handle %d", this->connection_index_, this->address_str_.c_str(),
549 handle);
550
551 esp_err_t err = esp_ble_gattc_write_char_descr(
552 this->gattc_if_, this->conn_id_, handle, data.size(), (uint8_t *) data.data(),
553 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
554 return this->check_and_log_error_("esp_ble_gattc_write_char_descr", err);
555}
556
557esp_err_t BluetoothConnection::notify_characteristic(uint16_t handle, bool enable) {
558 if (!this->connected()) {
559 this->log_gatt_not_connected_("notify", "characteristic");
560 return ESP_GATT_NOT_CONNECTED;
561 }
562
563 if (enable) {
564 ESP_LOGV(TAG, "[%d] [%s] Registering for GATT characteristic notifications handle %d", this->connection_index_,
565 this->address_str_.c_str(), handle);
566 esp_err_t err = esp_ble_gattc_register_for_notify(this->gattc_if_, this->remote_bda_, handle);
567 return this->check_and_log_error_("esp_ble_gattc_register_for_notify", err);
568 }
569
570 ESP_LOGV(TAG, "[%d] [%s] Unregistering for GATT characteristic notifications handle %d", this->connection_index_,
571 this->address_str_.c_str(), handle);
572 esp_err_t err = esp_ble_gattc_unregister_for_notify(this->gattc_if_, this->remote_bda_, handle);
573 return this->check_and_log_error_("esp_ble_gattc_unregister_for_notify", err);
574}
575
579
580} // namespace esphome::bluetooth_proxy
581
582#endif // USE_ESP32
uint8_t address
Definition bl0906.h:4
uint8_t status
Definition bl0942.h:8
void disable_loop()
Disable this component's loop.
bool client_supports_api_version(uint16_t major, uint16_t minor) const
bool send_message(const ProtoMessage &msg, uint8_t message_type)
std::array< uint64_t, BLUETOOTH_PROXY_MAX_CONNECTIONS > allocated
Definition api_pb2.h:2086
void calculate_size(ProtoSize &size) const override
Definition api_pb2.cpp:1960
std::vector< BluetoothGATTService > services
Definition api_pb2.h:1907
void set_data(const uint8_t *data, size_t len)
Definition api_pb2.h:2052
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2132
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:1950
void set_data(const uint8_t *data, size_t len)
Definition api_pb2.h:1959
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2115
uint32_t get_size() const
Definition proto.h:387
void log_connection_error_(const char *operation, esp_gatt_status_t status)
void log_gatt_operation_error_(const char *operation, uint16_t handle, esp_gatt_status_t status)
esp32_ble_tracker::AdvertisementParserType get_advertisement_parser_type() override
esp_err_t write_descriptor(uint16_t handle, const std::string &data, bool response)
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 log_connection_warning_(const char *operation, esp_err_t err)
esp_err_t write_characteristic(uint16_t handle, const std::string &data, bool response)
esp_err_t check_and_log_error_(const char *operation, esp_err_t err)
esp_err_t notify_characteristic(uint16_t handle, bool enable)
void log_gatt_not_connected_(const char *action, const char *type)
void update_allocated_slot_(uint64_t find_value, uint64_t set_value)
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_pairing(uint64_t address, bool paired, esp_err_t error=ESP_OK)
void send_gatt_error(uint64_t address, uint16_t handle, esp_err_t error)
api::BluetoothConnectionsFreeResponse connections_free_response_
const std::string & address_str() const
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
virtual void set_address(uint64_t address)
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
uint8_t type