116 if (this->
state() == espbt::ClientState::CONNECTING || this->
state() == espbt::ClientState::CONNECTED ||
117 this->
state() == espbt::ClientState::ESTABLISHED) {
121 }
else if (this->
state() == espbt::ClientState::DISCONNECTING) {
122 ESP_LOGW(TAG,
"[%d] [%s] Cannot connect, still waiting for CLOSE_EVT to complete disconnect",
131 this->
set_state(espbt::ClientState::CONNECTING);
136 this->
set_conn_params_(FAST_MIN_CONN_INTERVAL, FAST_MAX_CONN_INTERVAL, 0, FAST_CONN_TIMEOUT,
"fast");
137 }
else if (this->
connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) {
139 this->
set_conn_params_(MEDIUM_MIN_CONN_INTERVAL, MEDIUM_MAX_CONN_INTERVAL, 0, MEDIUM_CONN_TIMEOUT,
"medium");
283 esp_ble_gattc_cb_param_t *param) {
284 if (event == ESP_GATTC_REG_EVT && this->
app_id != param->reg.app_id)
286 if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && esp_gattc_if != this->
gattc_if_)
290 event, esp_gattc_if);
293 case ESP_GATTC_REG_EVT: {
294 if (param->reg.status == ESP_GATT_OK) {
299 this->
log_error_(
"gattc app registration failed status", param->reg.status);
300 this->
status_ = param->reg.status;
305 case ESP_GATTC_OPEN_EVT: {
306 if (!this->
check_addr(param->open.remote_bda))
315 if (this->
state() == espbt::ClientState::IDLE) {
316 ESP_LOGD(TAG,
"[%d] [%s] ESP_GATTC_OPEN_EVT in IDLE state (status=%d), ignoring", this->
connection_index_,
321 if (this->
state() != espbt::ClientState::CONNECTING) {
325 ESP_LOGE(TAG,
"[%d] [%s] ESP_GATTC_OPEN_EVT in %s state (status=%d)", this->
connection_index_,
328 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
343 this->
set_state(espbt::ClientState::CONNECTED);
354 esp_ble_gattc_search_service(esp_gattc_if, param->open.conn_id,
nullptr);
357 case ESP_GATTC_CONNECT_EVT: {
358 if (!this->
check_addr(param->connect.remote_bda))
361 this->
conn_id_ = param->connect.conn_id;
366 auto ret = esp_ble_gattc_send_mtu_req(this->
gattc_if_, param->connect.conn_id);
372 case ESP_GATTC_DISCONNECT_EVT: {
373 if (!this->
check_addr(param->disconnect.remote_bda))
376 if (param->disconnect.reason == ESP_GATT_CONN_TERMINATE_PEER_USER &&
377 this->state() == espbt::ClientState::CONNECTED) {
381 param->disconnect.reason);
386 if (this->
state() == espbt::ClientState::IDLE) {
387 this->
log_event_(
"DISCONNECT_EVT after CLOSE_EVT, already IDLE");
402 case ESP_GATTC_CFG_MTU_EVT: {
403 if (this->
conn_id_ != param->cfg_mtu.conn_id)
405 if (param->cfg_mtu.status != ESP_GATT_OK) {
407 param->cfg_mtu.mtu, param->cfg_mtu.status);
412 param->cfg_mtu.status, param->cfg_mtu.mtu);
413 this->
mtu_ = param->cfg_mtu.mtu;
416 case ESP_GATTC_CLOSE_EVT: {
417 if (this->
conn_id_ != param->close.conn_id)
425 case ESP_GATTC_SEARCH_RES_EVT: {
426 if (this->
conn_id_ != param->search_res.conn_id)
434#ifdef USE_ESP32_BLE_DEVICE
436 ble_service->
uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid);
437 ble_service->
start_handle = param->search_res.start_handle;
438 ble_service->
end_handle = param->search_res.end_handle;
439 ble_service->
client =
this;
444 case ESP_GATTC_SEARCH_CMPL_EVT: {
445 if (this->
conn_id_ != param->search_cmpl.conn_id)
451 this->
update_conn_params_(MEDIUM_MIN_CONN_INTERVAL, MEDIUM_MAX_CONN_INTERVAL, 0, MEDIUM_CONN_TIMEOUT,
"medium");
452 }
else if (this->
connection_type_ != espbt::ConnectionType::V3_WITH_CACHE) {
453#ifdef USE_ESP32_BLE_DEVICE
454#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
456 char uuid_buf[espbt::UUID_STR_LEN];
457 svc->uuid.to_str(uuid_buf);
460 svc->start_handle, svc->end_handle);
469 case ESP_GATTC_READ_DESCR_EVT: {
470 if (this->
conn_id_ != param->write.conn_id)
475 case ESP_GATTC_WRITE_DESCR_EVT: {
476 if (this->
conn_id_ != param->write.conn_id)
481 case ESP_GATTC_WRITE_CHAR_EVT: {
482 if (this->
conn_id_ != param->write.conn_id)
487 case ESP_GATTC_READ_CHAR_EVT: {
488 if (this->
conn_id_ != param->read.conn_id)
493 case ESP_GATTC_NOTIFY_EVT: {
494 if (this->
conn_id_ != param->notify.conn_id)
499 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
507 esp_gattc_descr_elem_t desc_result;
509 esp_gatt_status_t descr_status = esp_ble_gattc_get_descr_by_char_handle(
510 this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle, NOTIFY_DESC_UUID, &desc_result, &count);
511 if (descr_status != ESP_GATT_OK) {
515 esp_gattc_char_elem_t char_result;
516 esp_gatt_status_t char_status =
517 esp_ble_gattc_get_all_char(this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle,
518 param->reg_for_notify.handle, &char_result, &count, 0);
519 if (char_status != ESP_GATT_OK) {
528 uint16_t notify_en = char_result.properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY ? 1 : 2;
530 esp_ble_gattc_write_char_descr(this->
gattc_if_, this->
conn_id_, desc_result.handle,
sizeof(notify_en),
531 (uint8_t *) ¬ify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
532 ESP_LOGV(TAG,
"Wrote notify descriptor %d, properties=%d", notify_en, char_result.properties);
539 case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
560 case ESP_GAP_BLE_SEC_REQ_EVT:
561 if (!this->
check_addr(param->ble_security.auth_cmpl.bd_addr))
564 esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr,
true);
567 case ESP_GAP_BLE_AUTH_CMPL_EVT:
568 if (!this->
check_addr(param->ble_security.auth_cmpl.bd_addr))
570 char addr_str[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
573 if (!param->ble_security.auth_cmpl.success) {
574 this->
log_error_(
"auth fail reason", param->ble_security.auth_cmpl.fail_reason);
578 param->ble_security.auth_cmpl.addr_type, param->ble_security.auth_cmpl.auth_mode);