258 esp_ble_gattc_cb_param_t *param) {
259 if (event == ESP_GATTC_REG_EVT && this->
app_id != param->reg.app_id)
261 if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && esp_gattc_if != this->
gattc_if_)
265 event, esp_gattc_if);
268 case ESP_GATTC_REG_EVT: {
269 if (param->reg.status == ESP_GATT_OK) {
274 this->
log_error_(
"gattc app registration failed status", param->reg.status);
275 this->
status_ = param->reg.status;
280 case ESP_GATTC_OPEN_EVT: {
281 if (!this->
check_addr(param->open.remote_bda))
290 if (this->
state_ == espbt::ClientState::IDLE) {
291 ESP_LOGD(TAG,
"[%d] [%s] ESP_GATTC_OPEN_EVT in IDLE state (status=%d), ignoring", this->
connection_index_,
296 if (this->
state_ != espbt::ClientState::CONNECTING) {
300 ESP_LOGE(TAG,
"[%d] [%s] ESP_GATTC_OPEN_EVT in %s state (status=%d)", this->
connection_index_,
303 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
305 this->
set_state(espbt::ClientState::IDLE);
317 this->
set_state(espbt::ClientState::CONNECTED);
322 this->
state_ = espbt::ClientState::ESTABLISHED;
328 esp_ble_gattc_search_service(esp_gattc_if, param->cfg_mtu.conn_id,
nullptr);
331 case ESP_GATTC_CONNECT_EVT: {
332 if (!this->
check_addr(param->connect.remote_bda))
335 this->
conn_id_ = param->connect.conn_id;
340 auto ret = esp_ble_gattc_send_mtu_req(this->
gattc_if_, param->connect.conn_id);
346 case ESP_GATTC_DISCONNECT_EVT: {
347 if (!this->
check_addr(param->disconnect.remote_bda))
350 if (param->disconnect.reason == ESP_GATT_CONN_TERMINATE_PEER_USER &&
351 this->state_ == espbt::ClientState::CONNECTED) {
355 param->disconnect.reason);
358 this->
set_state(espbt::ClientState::IDLE);
362 case ESP_GATTC_CFG_MTU_EVT: {
363 if (this->
conn_id_ != param->cfg_mtu.conn_id)
365 if (param->cfg_mtu.status != ESP_GATT_OK) {
367 param->cfg_mtu.mtu, param->cfg_mtu.status);
372 param->cfg_mtu.status, param->cfg_mtu.mtu);
373 this->
mtu_ = param->cfg_mtu.mtu;
376 case ESP_GATTC_CLOSE_EVT: {
377 if (this->
conn_id_ != param->close.conn_id)
381 this->
set_state(espbt::ClientState::IDLE);
385 case ESP_GATTC_SEARCH_RES_EVT: {
386 if (this->
conn_id_ != param->search_res.conn_id)
394#ifdef USE_ESP32_BLE_DEVICE
396 ble_service->
uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid);
397 ble_service->
start_handle = param->search_res.start_handle;
398 ble_service->
end_handle = param->search_res.end_handle;
399 ble_service->
client =
this;
404 case ESP_GATTC_SEARCH_CMPL_EVT: {
405 if (this->
conn_id_ != param->search_cmpl.conn_id)
411 this->
update_conn_params_(MEDIUM_MIN_CONN_INTERVAL, MEDIUM_MAX_CONN_INTERVAL, 0, MEDIUM_CONN_TIMEOUT,
"medium");
412 }
else if (this->
connection_type_ != espbt::ConnectionType::V3_WITH_CACHE) {
413#ifdef USE_ESP32_BLE_DEVICE
416 svc->uuid.to_string().c_str());
418 svc->start_handle, svc->end_handle);
423 this->
state_ = espbt::ClientState::ESTABLISHED;
426 case ESP_GATTC_READ_DESCR_EVT: {
427 if (this->
conn_id_ != param->write.conn_id)
432 case ESP_GATTC_WRITE_DESCR_EVT: {
433 if (this->
conn_id_ != param->write.conn_id)
438 case ESP_GATTC_WRITE_CHAR_EVT: {
439 if (this->
conn_id_ != param->write.conn_id)
444 case ESP_GATTC_READ_CHAR_EVT: {
445 if (this->
conn_id_ != param->read.conn_id)
450 case ESP_GATTC_NOTIFY_EVT: {
451 if (this->
conn_id_ != param->notify.conn_id)
456 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
464 esp_gattc_descr_elem_t desc_result;
466 esp_gatt_status_t descr_status = esp_ble_gattc_get_descr_by_char_handle(
467 this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle, NOTIFY_DESC_UUID, &desc_result, &count);
468 if (descr_status != ESP_GATT_OK) {
472 esp_gattc_char_elem_t char_result;
473 esp_gatt_status_t char_status =
474 esp_ble_gattc_get_all_char(this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle,
475 param->reg_for_notify.handle, &char_result, &count, 0);
476 if (char_status != ESP_GATT_OK) {
485 uint16_t notify_en = char_result.properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY ? 1 : 2;
487 esp_ble_gattc_write_char_descr(this->
gattc_if_, this->
conn_id_, desc_result.handle,
sizeof(notify_en),
488 (uint8_t *) ¬ify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
489 ESP_LOGD(TAG,
"Wrote notify descriptor %d, properties=%d", notify_en, char_result.properties);
496 case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
517 case ESP_GAP_BLE_SEC_REQ_EVT:
518 if (!this->
check_addr(param->ble_security.auth_cmpl.bd_addr))
521 esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr,
true);
524 case ESP_GAP_BLE_AUTH_CMPL_EVT:
525 if (!this->
check_addr(param->ble_security.auth_cmpl.bd_addr))
527 esp_bd_addr_t bd_addr;
528 memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr,
sizeof(esp_bd_addr_t));
531 if (!param->ble_security.auth_cmpl.success) {
532 this->
log_error_(
"auth fail reason", param->ble_security.auth_cmpl.fail_reason);
536 param->ble_security.auth_cmpl.addr_type, param->ble_security.auth_cmpl.auth_mode);