ESPHome 2025.12.2
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(), service_status != ESP_GATT_OK ? "error" : "missing",
200 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(), 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(), 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(), operation, status);
341}
342
343void BluetoothConnection::log_connection_warning_(const char *operation, esp_err_t err) {
344 ESP_LOGW(TAG, "[%d] [%s] %s failed, err=%d", this->connection_index_, this->address_str(), operation, err);
345}
346
347void BluetoothConnection::log_gatt_not_connected_(const char *action, const char *type) {
348 ESP_LOGW(TAG, "[%d] [%s] Cannot %s GATT %s, not connected.", this->connection_index_, this->address_str(), action,
349 type);
350}
351
352void BluetoothConnection::log_gatt_operation_error_(const char *operation, uint16_t handle, esp_gatt_status_t status) {
353 ESP_LOGW(TAG, "[%d] [%s] Error %s for handle 0x%2X, status=%d", this->connection_index_, this->address_str(),
354 operation, handle, status);
355}
356
357esp_err_t BluetoothConnection::check_and_log_error_(const char *operation, esp_err_t err) {
358 if (err != ESP_OK) {
359 this->log_connection_warning_(operation, err);
360 return err;
361 }
362 return ESP_OK;
363}
364
365bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
366 esp_ble_gattc_cb_param_t *param) {
367 if (!BLEClientBase::gattc_event_handler(event, gattc_if, param))
368 return false;
369
370 switch (event) {
371 case ESP_GATTC_DISCONNECT_EVT: {
372 // Don't reset connection yet - wait for CLOSE_EVT to ensure controller has freed resources
373 // This prevents race condition where we mark slot as free before controller cleanup is complete
374 ESP_LOGD(TAG, "[%d] [%s] Disconnect, reason=0x%02x", this->connection_index_, this->address_str_,
375 param->disconnect.reason);
376 // Send disconnection notification but don't free the slot yet
377 this->proxy_->send_device_connection(this->address_, false, 0, param->disconnect.reason);
378 break;
379 }
380 case ESP_GATTC_CLOSE_EVT: {
381 ESP_LOGD(TAG, "[%d] [%s] Close, reason=0x%02x, freeing slot", this->connection_index_, this->address_str_,
382 param->close.reason);
383 // Now the GATT connection is fully closed and controller resources are freed
384 // Safe to mark the connection slot as available
385 this->reset_connection_(param->close.reason);
386 break;
387 }
388 case ESP_GATTC_OPEN_EVT: {
389 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
390 this->reset_connection_(param->open.status);
391 } else if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) {
392 this->proxy_->send_device_connection(this->address_, true, this->mtu_);
394 }
395 this->seen_mtu_or_services_ = false;
396 break;
397 }
398 case ESP_GATTC_CFG_MTU_EVT:
399 case ESP_GATTC_SEARCH_CMPL_EVT: {
400 if (!this->seen_mtu_or_services_) {
401 // We don't know if we will get the MTU or the services first, so
402 // only send the device connection true if we have already received
403 // the services.
404 this->seen_mtu_or_services_ = true;
405 break;
406 }
407 this->proxy_->send_device_connection(this->address_, true, this->mtu_);
409 break;
410 }
411 case ESP_GATTC_READ_DESCR_EVT:
412 case ESP_GATTC_READ_CHAR_EVT: {
413 if (param->read.status != ESP_GATT_OK) {
414 this->log_gatt_operation_error_("reading char/descriptor", param->read.handle, param->read.status);
415 this->proxy_->send_gatt_error(this->address_, param->read.handle, param->read.status);
416 break;
417 }
419 resp.address = this->address_;
420 resp.handle = param->read.handle;
421 resp.set_data(param->read.value, param->read.value_len);
423 break;
424 }
425 case ESP_GATTC_WRITE_CHAR_EVT:
426 case ESP_GATTC_WRITE_DESCR_EVT: {
427 if (param->write.status != ESP_GATT_OK) {
428 this->log_gatt_operation_error_("writing char/descriptor", param->write.handle, param->write.status);
429 this->proxy_->send_gatt_error(this->address_, param->write.handle, param->write.status);
430 break;
431 }
433 resp.address = this->address_;
434 resp.handle = param->write.handle;
436 break;
437 }
438 case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
439 if (param->unreg_for_notify.status != ESP_GATT_OK) {
440 this->log_gatt_operation_error_("unregistering notifications", param->unreg_for_notify.handle,
441 param->unreg_for_notify.status);
442 this->proxy_->send_gatt_error(this->address_, param->unreg_for_notify.handle, param->unreg_for_notify.status);
443 break;
444 }
446 resp.address = this->address_;
447 resp.handle = param->unreg_for_notify.handle;
449 break;
450 }
451 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
452 if (param->reg_for_notify.status != ESP_GATT_OK) {
453 this->log_gatt_operation_error_("registering notifications", param->reg_for_notify.handle,
454 param->reg_for_notify.status);
455 this->proxy_->send_gatt_error(this->address_, param->reg_for_notify.handle, param->reg_for_notify.status);
456 break;
457 }
459 resp.address = this->address_;
460 resp.handle = param->reg_for_notify.handle;
462 break;
463 }
464 case ESP_GATTC_NOTIFY_EVT: {
465 ESP_LOGV(TAG, "[%d] [%s] ESP_GATTC_NOTIFY_EVT: handle=0x%2X", this->connection_index_, this->address_str_,
466 param->notify.handle);
468 resp.address = this->address_;
469 resp.handle = param->notify.handle;
470 resp.set_data(param->notify.value, param->notify.value_len);
472 break;
473 }
474 default:
475 break;
476 }
477 return true;
478}
479
480void BluetoothConnection::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
482
483 switch (event) {
484 case ESP_GAP_BLE_AUTH_CMPL_EVT:
485 if (memcmp(param->ble_security.auth_cmpl.bd_addr, this->remote_bda_, 6) != 0)
486 break;
487 if (param->ble_security.auth_cmpl.success) {
488 this->proxy_->send_device_pairing(this->address_, true);
489 } else {
490 this->proxy_->send_device_pairing(this->address_, false, param->ble_security.auth_cmpl.fail_reason);
491 }
492 break;
493 default:
494 break;
495 }
496}
497
499 if (!this->connected()) {
500 this->log_gatt_not_connected_("read", "characteristic");
501 return ESP_GATT_NOT_CONNECTED;
502 }
503
504 ESP_LOGV(TAG, "[%d] [%s] Reading GATT characteristic handle %d", this->connection_index_, this->address_str_, handle);
505
506 esp_err_t err = esp_ble_gattc_read_char(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
507 return this->check_and_log_error_("esp_ble_gattc_read_char", err);
508}
509
510esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const uint8_t *data, size_t length,
511 bool response) {
512 if (!this->connected()) {
513 this->log_gatt_not_connected_("write", "characteristic");
514 return ESP_GATT_NOT_CONNECTED;
515 }
516 ESP_LOGV(TAG, "[%d] [%s] Writing GATT characteristic handle %d", this->connection_index_, this->address_str_, handle);
517
518 // ESP-IDF's API requires a non-const uint8_t* but it doesn't modify the data
519 // The BTC layer immediately copies the data to its own buffer (see btc_gattc.c)
520 // const_cast is safe here and was previously hidden by a C-style cast
521 esp_err_t err =
522 esp_ble_gattc_write_char(this->gattc_if_, this->conn_id_, handle, length, const_cast<uint8_t *>(data),
523 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
524 return this->check_and_log_error_("esp_ble_gattc_write_char", err);
525}
526
527esp_err_t BluetoothConnection::read_descriptor(uint16_t handle) {
528 if (!this->connected()) {
529 this->log_gatt_not_connected_("read", "descriptor");
530 return ESP_GATT_NOT_CONNECTED;
531 }
532 ESP_LOGV(TAG, "[%d] [%s] Reading GATT descriptor handle %d", this->connection_index_, this->address_str_, handle);
533
534 esp_err_t err = esp_ble_gattc_read_char_descr(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
535 return this->check_and_log_error_("esp_ble_gattc_read_char_descr", err);
536}
537
538esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const uint8_t *data, size_t length, bool response) {
539 if (!this->connected()) {
540 this->log_gatt_not_connected_("write", "descriptor");
541 return ESP_GATT_NOT_CONNECTED;
542 }
543 ESP_LOGV(TAG, "[%d] [%s] Writing GATT descriptor handle %d", this->connection_index_, this->address_str_, handle);
544
545 // ESP-IDF's API requires a non-const uint8_t* but it doesn't modify the data
546 // The BTC layer immediately copies the data to its own buffer (see btc_gattc.c)
547 // const_cast is safe here and was previously hidden by a C-style cast
548 esp_err_t err = esp_ble_gattc_write_char_descr(
549 this->gattc_if_, this->conn_id_, handle, length, const_cast<uint8_t *>(data),
550 response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
551 return this->check_and_log_error_("esp_ble_gattc_write_char_descr", err);
552}
553
554esp_err_t BluetoothConnection::notify_characteristic(uint16_t handle, bool enable) {
555 if (!this->connected()) {
556 this->log_gatt_not_connected_("notify", "characteristic");
557 return ESP_GATT_NOT_CONNECTED;
558 }
559
560 if (enable) {
561 ESP_LOGV(TAG, "[%d] [%s] Registering for GATT characteristic notifications handle %d", this->connection_index_,
562 this->address_str_, handle);
563 esp_err_t err = esp_ble_gattc_register_for_notify(this->gattc_if_, this->remote_bda_, handle);
564 return this->check_and_log_error_("esp_ble_gattc_register_for_notify", err);
565 }
566
567 ESP_LOGV(TAG, "[%d] [%s] Unregistering for GATT characteristic notifications handle %d", this->connection_index_,
568 this->address_str_, handle);
569 esp_err_t err = esp_ble_gattc_unregister_for_notify(this->gattc_if_, this->remote_bda_, handle);
570 return this->check_and_log_error_("esp_ble_gattc_unregister_for_notify", err);
571}
572
576
577} // namespace esphome::bluetooth_proxy
578
579#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:2184
void calculate_size(ProtoSize &size) const override
Definition api_pb2.cpp:2103
std::vector< BluetoothGATTService > services
Definition api_pb2.h:2003
void set_data(const uint8_t *data, size_t len)
Definition api_pb2.h:2150
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2230
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2046
void set_data(const uint8_t *data, size_t len)
Definition api_pb2.h:2055
static constexpr uint8_t MESSAGE_TYPE
Definition api_pb2.h:2213
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_
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