ESPHome 2025.12.0-dev
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 // Initialize FixedVector with exact count and process characteristics
234 service_resp.characteristics.init(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 fill_gatt_uuid(characteristic_resp.uuid, characteristic_resp.short_uuid, char_result.uuid, use_efficient_uuids);
257 characteristic_resp.handle = char_result.char_handle;
258 characteristic_resp.properties = char_result.properties;
259 char_offset++;
260
261 // Get the number of descriptors directly with one call
262 uint16_t total_desc_count = 0;
263 esp_gatt_status_t desc_count_status = esp_ble_gattc_get_attr_count(
264 this->gattc_if_, this->conn_id_, ESP_GATT_DB_DESCRIPTOR, 0, 0, char_result.char_handle, &total_desc_count);
265
266 if (desc_count_status != ESP_GATT_OK) {
267 this->log_connection_error_("esp_ble_gattc_get_attr_count", desc_count_status);
268 this->send_service_ = DONE_SENDING_SERVICES;
269 return;
270 }
271 if (total_desc_count == 0) {
272 continue;
273 }
274
275 // Initialize FixedVector with exact count and process descriptors
276 characteristic_resp.descriptors.init(total_desc_count);
277 uint16_t desc_offset = 0;
278 esp_gattc_descr_elem_t desc_result;
279 while (true) { // descriptors
280 uint16_t desc_count = 1;
281 esp_gatt_status_t desc_status = esp_ble_gattc_get_all_descr(
282 this->gattc_if_, this->conn_id_, char_result.char_handle, &desc_result, &desc_count, desc_offset);
283 if (desc_status == ESP_GATT_INVALID_OFFSET || desc_status == ESP_GATT_NOT_FOUND) {
284 break;
285 }
286 if (desc_status != ESP_GATT_OK) {
287 this->log_connection_error_("esp_ble_gattc_get_all_descr", desc_status);
288 this->send_service_ = DONE_SENDING_SERVICES;
289 return;
290 }
291 if (desc_count == 0) {
292 break; // No more descriptors
293 }
294
295 characteristic_resp.descriptors.emplace_back();
296 auto &descriptor_resp = characteristic_resp.descriptors.back();
297 fill_gatt_uuid(descriptor_resp.uuid, descriptor_resp.short_uuid, desc_result.uuid, use_efficient_uuids);
298 descriptor_resp.handle = desc_result.handle;
299 desc_offset++;
300 }
301 }
302 } // end if (total_char_count > 0)
303
304 // Calculate the actual size of just this service
305 api::ProtoSize service_sizer;
306 service_resp.calculate_size(service_sizer);
307 size_t service_size = service_sizer.get_size() + 1; // +1 for field tag
308
309 // Check if adding this service would exceed the limit
310 if (current_size + service_size > MAX_PACKET_SIZE) {
311 // We would go over - pop the last service if we have more than one
312 if (resp.services.size() > 1) {
313 resp.services.pop_back();
314 ESP_LOGD(TAG, "[%d] [%s] Service %d would exceed limit (current: %d + service: %d > %d), sending current batch",
315 this->connection_index_, this->address_str().c_str(), this->send_service_, current_size, service_size,
316 MAX_PACKET_SIZE);
317 // Don't increment send_service_ - we'll retry this service in next batch
318 } else {
319 // This single service is too large, but we have to send it anyway
320 ESP_LOGV(TAG, "[%d] [%s] Service %d is too large (%d bytes) but sending anyway", this->connection_index_,
321 this->address_str().c_str(), this->send_service_, service_size);
322 // Increment so we don't get stuck
323 this->send_service_++;
324 }
325 // Send what we have
326 break;
327 }
328
329 // Now we know we're keeping this service, add its size
330 current_size += service_size;
331 // Successfully added this service, increment counter
332 this->send_service_++;
333 }
334
335 // Send the message with dynamically batched services
336 api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE);
337}
338
339void BluetoothConnection::log_connection_error_(const char *operation, esp_gatt_status_t status) {
340 ESP_LOGE(TAG, "[%d] [%s] %s error, status=%d", this->connection_index_, this->address_str().c_str(), operation,
341 status);
342}
343
344void BluetoothConnection::log_connection_warning_(const char *operation, esp_err_t err) {
345 ESP_LOGW(TAG, "[%d] [%s] %s failed, err=%d", this->connection_index_, this->address_str().c_str(), operation, err);
346}
347
348void BluetoothConnection::log_gatt_not_connected_(const char *action, const char *type) {
349 ESP_LOGW(TAG, "[%d] [%s] Cannot %s GATT %s, not connected.", this->connection_index_, this->address_str().c_str(),
350 action, type);
351}
352
353void BluetoothConnection::log_gatt_operation_error_(const char *operation, uint16_t handle, esp_gatt_status_t status) {
354 ESP_LOGW(TAG, "[%d] [%s] Error %s for handle 0x%2X, status=%d", this->connection_index_, this->address_str().c_str(),
355 operation, handle, status);
356}
357
358esp_err_t BluetoothConnection::check_and_log_error_(const char *operation, esp_err_t err) {
359 if (err != ESP_OK) {
360 this->log_connection_warning_(operation, err);
361 return err;
362 }
363 return ESP_OK;
364}
365
366bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
367 esp_ble_gattc_cb_param_t *param) {
368 if (!BLEClientBase::gattc_event_handler(event, gattc_if, param))
369 return false;
370
371 switch (event) {
372 case ESP_GATTC_DISCONNECT_EVT: {
373 // Don't reset connection yet - wait for CLOSE_EVT to ensure controller has freed resources
374 // This prevents race condition where we mark slot as free before controller cleanup is complete
375 ESP_LOGD(TAG, "[%d] [%s] Disconnect, reason=0x%02x", this->connection_index_, this->address_str_.c_str(),
376 param->disconnect.reason);
377 // Send disconnection notification but don't free the slot yet
378 this->proxy_->send_device_connection(this->address_, false, 0, param->disconnect.reason);
379 break;
380 }
381 case ESP_GATTC_CLOSE_EVT: {
382 ESP_LOGD(TAG, "[%d] [%s] Close, reason=0x%02x, freeing slot", this->connection_index_, this->address_str_.c_str(),
383 param->close.reason);
384 // Now the GATT connection is fully closed and controller resources are freed
385 // Safe to mark the connection slot as available
386 this->reset_connection_(param->close.reason);
387 break;
388 }
389 case ESP_GATTC_OPEN_EVT: {
390 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
391 this->reset_connection_(param->open.status);
392 } else if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) {
393 this->proxy_->send_device_connection(this->address_, true, this->mtu_);
395 }
396 this->seen_mtu_or_services_ = false;
397 break;
398 }
399 case ESP_GATTC_CFG_MTU_EVT:
400 case ESP_GATTC_SEARCH_CMPL_EVT: {
401 if (!this->seen_mtu_or_services_) {
402 // We don't know if we will get the MTU or the services first, so
403 // only send the device connection true if we have already received
404 // the services.
405 this->seen_mtu_or_services_ = true;
406 break;
407 }
408 this->proxy_->send_device_connection(this->address_, true, this->mtu_);
410 break;
411 }
412 case ESP_GATTC_READ_DESCR_EVT:
413 case ESP_GATTC_READ_CHAR_EVT: {
414 if (param->read.status != ESP_GATT_OK) {
415 this->log_gatt_operation_error_("reading char/descriptor", param->read.handle, param->read.status);
416 this->proxy_->send_gatt_error(this->address_, param->read.handle, param->read.status);
417 break;
418 }
420 resp.address = this->address_;
421 resp.handle = param->read.handle;
422 resp.set_data(param->read.value, param->read.value_len);
424 break;
425 }
426 case ESP_GATTC_WRITE_CHAR_EVT:
427 case ESP_GATTC_WRITE_DESCR_EVT: {
428 if (param->write.status != ESP_GATT_OK) {
429 this->log_gatt_operation_error_("writing char/descriptor", param->write.handle, param->write.status);
430 this->proxy_->send_gatt_error(this->address_, param->write.handle, param->write.status);
431 break;
432 }
434 resp.address = this->address_;
435 resp.handle = param->write.handle;
437 break;
438 }
439 case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
440 if (param->unreg_for_notify.status != ESP_GATT_OK) {
441 this->log_gatt_operation_error_("unregistering notifications", param->unreg_for_notify.handle,
442 param->unreg_for_notify.status);
443 this->proxy_->send_gatt_error(this->address_, param->unreg_for_notify.handle, param->unreg_for_notify.status);
444 break;
445 }
447 resp.address = this->address_;
448 resp.handle = param->unreg_for_notify.handle;
450 break;
451 }
452 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
453 if (param->reg_for_notify.status != ESP_GATT_OK) {
454 this->log_gatt_operation_error_("registering notifications", param->reg_for_notify.handle,
455 param->reg_for_notify.status);
456 this->proxy_->send_gatt_error(this->address_, param->reg_for_notify.handle, param->reg_for_notify.status);
457 break;
458 }
460 resp.address = this->address_;
461 resp.handle = param->reg_for_notify.handle;
463 break;
464 }
465 case ESP_GATTC_NOTIFY_EVT: {
466 ESP_LOGV(TAG, "[%d] [%s] ESP_GATTC_NOTIFY_EVT: handle=0x%2X", this->connection_index_, this->address_str_.c_str(),
467 param->notify.handle);
469 resp.address = this->address_;
470 resp.handle = param->notify.handle;
471 resp.set_data(param->notify.value, param->notify.value_len);
473 break;
474 }
475 default:
476 break;
477 }
478 return true;
479}
480
481void BluetoothConnection::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
483
484 switch (event) {
485 case ESP_GAP_BLE_AUTH_CMPL_EVT:
486 if (memcmp(param->ble_security.auth_cmpl.bd_addr, this->remote_bda_, 6) != 0)
487 break;
488 if (param->ble_security.auth_cmpl.success) {
489 this->proxy_->send_device_pairing(this->address_, true);
490 } else {
491 this->proxy_->send_device_pairing(this->address_, false, param->ble_security.auth_cmpl.fail_reason);
492 }
493 break;
494 default:
495 break;
496 }
497}
498
500 if (!this->connected()) {
501 this->log_gatt_not_connected_("read", "characteristic");
502 return ESP_GATT_NOT_CONNECTED;
503 }
504
505 ESP_LOGV(TAG, "[%d] [%s] Reading GATT characteristic handle %d", this->connection_index_, this->address_str_.c_str(),
506 handle);
507
508 esp_err_t err = esp_ble_gattc_read_char(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
509 return this->check_and_log_error_("esp_ble_gattc_read_char", err);
510}
511
512esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const uint8_t *data, size_t length,
513 bool response) {
514 if (!this->connected()) {
515 this->log_gatt_not_connected_("write", "characteristic");
516 return ESP_GATT_NOT_CONNECTED;
517 }
518 ESP_LOGV(TAG, "[%d] [%s] Writing GATT characteristic handle %d", this->connection_index_, this->address_str_.c_str(),
519 handle);
520
521 // ESP-IDF's API requires a non-const uint8_t* but it doesn't modify the data
522 // The BTC layer immediately copies the data to its own buffer (see btc_gattc.c)
523 // const_cast is safe here and was previously hidden by a C-style cast
524 esp_err_t err =
525 esp_ble_gattc_write_char(this->gattc_if_, this->conn_id_, handle, length, const_cast<uint8_t *>(data),
526 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
527 return this->check_and_log_error_("esp_ble_gattc_write_char", err);
528}
529
530esp_err_t BluetoothConnection::read_descriptor(uint16_t handle) {
531 if (!this->connected()) {
532 this->log_gatt_not_connected_("read", "descriptor");
533 return ESP_GATT_NOT_CONNECTED;
534 }
535 ESP_LOGV(TAG, "[%d] [%s] Reading GATT descriptor handle %d", this->connection_index_, this->address_str_.c_str(),
536 handle);
537
538 esp_err_t err = esp_ble_gattc_read_char_descr(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
539 return this->check_and_log_error_("esp_ble_gattc_read_char_descr", err);
540}
541
542esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const uint8_t *data, size_t length, bool response) {
543 if (!this->connected()) {
544 this->log_gatt_not_connected_("write", "descriptor");
545 return ESP_GATT_NOT_CONNECTED;
546 }
547 ESP_LOGV(TAG, "[%d] [%s] Writing GATT descriptor handle %d", this->connection_index_, this->address_str_.c_str(),
548 handle);
549
550 // ESP-IDF's API requires a non-const uint8_t* but it doesn't modify the data
551 // The BTC layer immediately copies the data to its own buffer (see btc_gattc.c)
552 // const_cast is safe here and was previously hidden by a C-style cast
553 esp_err_t err = esp_ble_gattc_write_char_descr(
554 this->gattc_if_, this->conn_id_, handle, length, const_cast<uint8_t *>(data),
555 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
556 return this->check_and_log_error_("esp_ble_gattc_write_char_descr", err);
557}
558
559esp_err_t BluetoothConnection::notify_characteristic(uint16_t handle, bool enable) {
560 if (!this->connected()) {
561 this->log_gatt_not_connected_("notify", "characteristic");
562 return ESP_GATT_NOT_CONNECTED;
563 }
564
565 if (enable) {
566 ESP_LOGV(TAG, "[%d] [%s] Registering for GATT characteristic notifications handle %d", this->connection_index_,
567 this->address_str_.c_str(), handle);
568 esp_err_t err = esp_ble_gattc_register_for_notify(this->gattc_if_, this->remote_bda_, handle);
569 return this->check_and_log_error_("esp_ble_gattc_register_for_notify", err);
570 }
571
572 ESP_LOGV(TAG, "[%d] [%s] Unregistering for GATT characteristic notifications handle %d", this->connection_index_,
573 this->address_str_.c_str(), handle);
574 esp_err_t err = esp_ble_gattc_unregister_for_notify(this->gattc_if_, this->remote_bda_, handle);
575 return this->check_and_log_error_("esp_ble_gattc_unregister_for_notify", err);
576}
577
581
582} // namespace esphome::bluetooth_proxy
583
584#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:2142
void calculate_size(ProtoSize &size) const override
Definition api_pb2.cpp:2060
std::vector< BluetoothGATTService > services
Definition api_pb2.h:1961
void set_data(const uint8_t *data, size_t len)
Definition api_pb2.h:2108
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2188
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2004
void set_data(const uint8_t *data, size_t len)
Definition api_pb2.h:2013
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2171
uint32_t get_size() const
Definition proto.h:409
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_characteristic(uint16_t handle, const uint8_t *data, size_t length, 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 check_and_log_error_(const char *operation, esp_err_t err)
esp_err_t write_descriptor(uint16_t handle, const uint8_t *data, size_t length, bool response)
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
uint16_t type
uint16_t length
Definition tt21100.cpp:0