261 esp_ble_gattc_cb_param_t *param) {
262 if (event == ESP_GATTC_REG_EVT && this->
app_id != param->reg.app_id)
264 if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && esp_gattc_if != this->
gattc_if_)
267 ESP_LOGV(TAG,
"[%d] [%s] gattc_event_handler: event=%d gattc_if=%d", this->
connection_index_,
271 case ESP_GATTC_REG_EVT: {
272 if (param->reg.status == ESP_GATT_OK) {
277 this->
log_error_(
"gattc app registration failed status", param->reg.status);
278 this->
status_ = param->reg.status;
283 case ESP_GATTC_OPEN_EVT: {
284 if (!this->
check_addr(param->open.remote_bda))
293 if (this->
state_ == espbt::ClientState::IDLE) {
294 ESP_LOGD(TAG,
"[%d] [%s] ESP_GATTC_OPEN_EVT in IDLE state (status=%d), ignoring", this->
connection_index_,
299 if (this->
state_ != espbt::ClientState::CONNECTING) {
303 ESP_LOGE(TAG,
"[%d] [%s] ESP_GATTC_OPEN_EVT in %s state (status=%d)", this->
connection_index_,
306 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
308 this->
set_state(espbt::ClientState::IDLE);
320 this->
set_state(espbt::ClientState::CONNECTED);
325 this->
state_ = espbt::ClientState::ESTABLISHED;
331 esp_ble_gattc_search_service(esp_gattc_if, param->cfg_mtu.conn_id,
nullptr);
334 case ESP_GATTC_CONNECT_EVT: {
335 if (!this->
check_addr(param->connect.remote_bda))
338 this->
conn_id_ = param->connect.conn_id;
343 auto ret = esp_ble_gattc_send_mtu_req(this->
gattc_if_, param->connect.conn_id);
349 case ESP_GATTC_DISCONNECT_EVT: {
350 if (!this->
check_addr(param->disconnect.remote_bda))
353 if (param->disconnect.reason == ESP_GATT_CONN_TERMINATE_PEER_USER &&
354 this->state_ == espbt::ClientState::CONNECTED) {
357 ESP_LOGD(TAG,
"[%d] [%s] ESP_GATTC_DISCONNECT_EVT, reason 0x%02x", this->
connection_index_,
361 this->
set_state(espbt::ClientState::IDLE);
365 case ESP_GATTC_CFG_MTU_EVT: {
366 if (this->
conn_id_ != param->cfg_mtu.conn_id)
368 if (param->cfg_mtu.status != ESP_GATT_OK) {
369 ESP_LOGW(TAG,
"[%d] [%s] cfg_mtu failed, mtu %d, status %d", this->
connection_index_,
370 this->
address_str_.c_str(), param->cfg_mtu.mtu, param->cfg_mtu.status);
375 param->cfg_mtu.status, param->cfg_mtu.mtu);
376 this->
mtu_ = param->cfg_mtu.mtu;
379 case ESP_GATTC_CLOSE_EVT: {
380 if (this->
conn_id_ != param->close.conn_id)
384 this->
set_state(espbt::ClientState::IDLE);
388 case ESP_GATTC_SEARCH_RES_EVT: {
389 if (this->
conn_id_ != param->search_res.conn_id)
397#ifdef USE_ESP32_BLE_DEVICE
399 ble_service->
uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid);
400 ble_service->
start_handle = param->search_res.start_handle;
401 ble_service->
end_handle = param->search_res.end_handle;
402 ble_service->
client =
this;
407 case ESP_GATTC_SEARCH_CMPL_EVT: {
408 if (this->
conn_id_ != param->search_cmpl.conn_id)
414 this->
update_conn_params_(MEDIUM_MIN_CONN_INTERVAL, MEDIUM_MAX_CONN_INTERVAL, 0, MEDIUM_CONN_TIMEOUT,
"medium");
415 }
else if (this->
connection_type_ != espbt::ConnectionType::V3_WITH_CACHE) {
416#ifdef USE_ESP32_BLE_DEVICE
419 svc->uuid.to_string().c_str());
420 ESP_LOGV(TAG,
"[%d] [%s] start_handle: 0x%x end_handle: 0x%x", this->
connection_index_,
421 this->
address_str_.c_str(), svc->start_handle, svc->end_handle);
426 this->
state_ = espbt::ClientState::ESTABLISHED;
429 case ESP_GATTC_READ_DESCR_EVT: {
430 if (this->
conn_id_ != param->write.conn_id)
435 case ESP_GATTC_WRITE_DESCR_EVT: {
436 if (this->
conn_id_ != param->write.conn_id)
441 case ESP_GATTC_WRITE_CHAR_EVT: {
442 if (this->
conn_id_ != param->write.conn_id)
447 case ESP_GATTC_READ_CHAR_EVT: {
448 if (this->
conn_id_ != param->read.conn_id)
453 case ESP_GATTC_NOTIFY_EVT: {
454 if (this->
conn_id_ != param->notify.conn_id)
459 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
467 esp_gattc_descr_elem_t desc_result;
469 esp_gatt_status_t descr_status = esp_ble_gattc_get_descr_by_char_handle(
470 this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle, NOTIFY_DESC_UUID, &desc_result, &count);
471 if (descr_status != ESP_GATT_OK) {
475 esp_gattc_char_elem_t char_result;
476 esp_gatt_status_t char_status =
477 esp_ble_gattc_get_all_char(this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle,
478 param->reg_for_notify.handle, &char_result, &count, 0);
479 if (char_status != ESP_GATT_OK) {
488 uint16_t notify_en = char_result.properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY ? 1 : 2;
490 esp_ble_gattc_write_char_descr(this->
gattc_if_, this->
conn_id_, desc_result.handle,
sizeof(notify_en),
491 (uint8_t *) ¬ify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
492 ESP_LOGD(TAG,
"Wrote notify descriptor %d, properties=%d", notify_en, char_result.properties);
499 case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {