266 esp_ble_gattc_cb_param_t *param) {
267 if (event == ESP_GATTC_REG_EVT && this->
app_id != param->reg.app_id)
269 if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && esp_gattc_if != this->
gattc_if_)
273 event, esp_gattc_if);
276 case ESP_GATTC_REG_EVT: {
277 if (param->reg.status == ESP_GATT_OK) {
282 this->
log_error_(
"gattc app registration failed status", param->reg.status);
283 this->
status_ = param->reg.status;
288 case ESP_GATTC_OPEN_EVT: {
289 if (!this->
check_addr(param->open.remote_bda))
298 if (this->
state() == espbt::ClientState::IDLE) {
299 ESP_LOGD(TAG,
"[%d] [%s] ESP_GATTC_OPEN_EVT in IDLE state (status=%d), ignoring", this->
connection_index_,
304 if (this->
state() != espbt::ClientState::CONNECTING) {
308 ESP_LOGE(TAG,
"[%d] [%s] ESP_GATTC_OPEN_EVT in %s state (status=%d)", this->
connection_index_,
311 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
313 this->
set_state(espbt::ClientState::IDLE);
325 this->
set_state(espbt::ClientState::CONNECTED);
336 esp_ble_gattc_search_service(esp_gattc_if, param->cfg_mtu.conn_id,
nullptr);
339 case ESP_GATTC_CONNECT_EVT: {
340 if (!this->
check_addr(param->connect.remote_bda))
343 this->
conn_id_ = param->connect.conn_id;
348 auto ret = esp_ble_gattc_send_mtu_req(this->
gattc_if_, param->connect.conn_id);
354 case ESP_GATTC_DISCONNECT_EVT: {
355 if (!this->
check_addr(param->disconnect.remote_bda))
358 if (param->disconnect.reason == ESP_GATT_CONN_TERMINATE_PEER_USER &&
359 this->state() == espbt::ClientState::CONNECTED) {
363 param->disconnect.reason);
366 this->
set_state(espbt::ClientState::IDLE);
370 case ESP_GATTC_CFG_MTU_EVT: {
371 if (this->
conn_id_ != param->cfg_mtu.conn_id)
373 if (param->cfg_mtu.status != ESP_GATT_OK) {
375 param->cfg_mtu.mtu, param->cfg_mtu.status);
380 param->cfg_mtu.status, param->cfg_mtu.mtu);
381 this->
mtu_ = param->cfg_mtu.mtu;
384 case ESP_GATTC_CLOSE_EVT: {
385 if (this->
conn_id_ != param->close.conn_id)
389 this->
set_state(espbt::ClientState::IDLE);
393 case ESP_GATTC_SEARCH_RES_EVT: {
394 if (this->
conn_id_ != param->search_res.conn_id)
402#ifdef USE_ESP32_BLE_DEVICE
404 ble_service->
uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid);
405 ble_service->
start_handle = param->search_res.start_handle;
406 ble_service->
end_handle = param->search_res.end_handle;
407 ble_service->
client =
this;
412 case ESP_GATTC_SEARCH_CMPL_EVT: {
413 if (this->
conn_id_ != param->search_cmpl.conn_id)
419 this->
update_conn_params_(MEDIUM_MIN_CONN_INTERVAL, MEDIUM_MAX_CONN_INTERVAL, 0, MEDIUM_CONN_TIMEOUT,
"medium");
420 }
else if (this->
connection_type_ != espbt::ConnectionType::V3_WITH_CACHE) {
421#ifdef USE_ESP32_BLE_DEVICE
422#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
424 char uuid_buf[espbt::UUID_STR_LEN];
425 svc->uuid.to_str(uuid_buf);
427 "[%d] [%s] Service UUID: %s\n"
428 "[%d] [%s] start_handle: 0x%x end_handle: 0x%x",
430 svc->start_handle, svc->end_handle);
439 case ESP_GATTC_READ_DESCR_EVT: {
440 if (this->
conn_id_ != param->write.conn_id)
445 case ESP_GATTC_WRITE_DESCR_EVT: {
446 if (this->
conn_id_ != param->write.conn_id)
451 case ESP_GATTC_WRITE_CHAR_EVT: {
452 if (this->
conn_id_ != param->write.conn_id)
457 case ESP_GATTC_READ_CHAR_EVT: {
458 if (this->
conn_id_ != param->read.conn_id)
463 case ESP_GATTC_NOTIFY_EVT: {
464 if (this->
conn_id_ != param->notify.conn_id)
469 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
477 esp_gattc_descr_elem_t desc_result;
479 esp_gatt_status_t descr_status = esp_ble_gattc_get_descr_by_char_handle(
480 this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle, NOTIFY_DESC_UUID, &desc_result, &count);
481 if (descr_status != ESP_GATT_OK) {
485 esp_gattc_char_elem_t char_result;
486 esp_gatt_status_t char_status =
487 esp_ble_gattc_get_all_char(this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle,
488 param->reg_for_notify.handle, &char_result, &count, 0);
489 if (char_status != ESP_GATT_OK) {
498 uint16_t notify_en = char_result.properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY ? 1 : 2;
500 esp_ble_gattc_write_char_descr(this->
gattc_if_, this->
conn_id_, desc_result.handle,
sizeof(notify_en),
501 (uint8_t *) ¬ify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
502 ESP_LOGV(TAG,
"Wrote notify descriptor %d, properties=%d", notify_en, char_result.properties);
509 case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
530 case ESP_GAP_BLE_SEC_REQ_EVT:
531 if (!this->
check_addr(param->ble_security.auth_cmpl.bd_addr))
534 esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr,
true);
537 case ESP_GAP_BLE_AUTH_CMPL_EVT:
538 if (!this->
check_addr(param->ble_security.auth_cmpl.bd_addr))
540 char addr_str[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
543 if (!param->ble_security.auth_cmpl.success) {
544 this->
log_error_(
"auth fail reason", param->ble_security.auth_cmpl.fail_reason);
548 param->ble_security.auth_cmpl.addr_type, param->ble_security.auth_cmpl.auth_mode);