111 this->remote_addr_type_);
117 uint16_t min_interval, max_interval, timeout;
118 const char *param_type;
122 min_interval = FAST_MIN_CONN_INTERVAL;
123 max_interval = FAST_MAX_CONN_INTERVAL;
124 timeout = FAST_CONN_TIMEOUT;
127 min_interval = MEDIUM_MIN_CONN_INTERVAL;
128 max_interval = MEDIUM_MAX_CONN_INTERVAL;
129 timeout = MEDIUM_CONN_TIMEOUT;
130 param_type =
"medium";
133 auto param_ret = esp_ble_gap_set_prefer_conn_params(this->
remote_bda_, min_interval, max_interval,
136 if (param_ret != ESP_OK) {
137 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gap_set_prefer_conn_params failed: %d", this->
connection_index_,
147 this->
set_state(espbt::ClientState::IDLE);
149 this->
set_state(espbt::ClientState::CONNECTING);
251 esp_ble_gattc_cb_param_t *param) {
252 if (event == ESP_GATTC_REG_EVT && this->
app_id != param->reg.app_id)
254 if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && esp_gattc_if != this->
gattc_if_)
257 ESP_LOGV(TAG,
"[%d] [%s] gattc_event_handler: event=%d gattc_if=%d", this->
connection_index_,
261 case ESP_GATTC_REG_EVT: {
262 if (param->reg.status == ESP_GATT_OK) {
267 ESP_LOGE(TAG,
"[%d] [%s] gattc app registration failed id=%d code=%d", this->
connection_index_,
268 this->
address_str_.c_str(), param->reg.app_id, param->reg.status);
269 this->
status_ = param->reg.status;
274 case ESP_GATTC_OPEN_EVT: {
275 if (!this->
check_addr(param->open.remote_bda))
280 if (this->
state_ != espbt::ClientState::CONNECTING) {
284 ESP_LOGE(TAG,
"[%d] [%s] Got ESP_GATTC_OPEN_EVT while in %s state, status=%d", this->
connection_index_,
287 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
289 this->
set_state(espbt::ClientState::IDLE);
301 this->
set_state(espbt::ClientState::CONNECTED);
307 this->
state_ = espbt::ClientState::ESTABLISHED;
311 esp_ble_gattc_search_service(esp_gattc_if, param->cfg_mtu.conn_id,
nullptr);
314 case ESP_GATTC_CONNECT_EVT: {
315 if (!this->
check_addr(param->connect.remote_bda))
318 this->
conn_id_ = param->connect.conn_id;
323 auto ret = esp_ble_gattc_send_mtu_req(this->
gattc_if_, param->connect.conn_id);
329 case ESP_GATTC_DISCONNECT_EVT: {
330 if (!this->
check_addr(param->disconnect.remote_bda))
333 if (param->disconnect.reason == ESP_GATT_CONN_TERMINATE_PEER_USER &&
334 this->state_ == espbt::ClientState::CONNECTED) {
335 ESP_LOGW(TAG,
"[%d] [%s] Disconnected by remote during service discovery", this->
connection_index_,
338 ESP_LOGD(TAG,
"[%d] [%s] ESP_GATTC_DISCONNECT_EVT, reason 0x%02x", this->
connection_index_,
342 this->
set_state(espbt::ClientState::IDLE);
346 case ESP_GATTC_CFG_MTU_EVT: {
347 if (this->
conn_id_ != param->cfg_mtu.conn_id)
349 if (param->cfg_mtu.status != ESP_GATT_OK) {
350 ESP_LOGW(TAG,
"[%d] [%s] cfg_mtu failed, mtu %d, status %d", this->
connection_index_,
351 this->
address_str_.c_str(), param->cfg_mtu.mtu, param->cfg_mtu.status);
356 param->cfg_mtu.status, param->cfg_mtu.mtu);
357 this->
mtu_ = param->cfg_mtu.mtu;
360 case ESP_GATTC_CLOSE_EVT: {
361 if (this->
conn_id_ != param->close.conn_id)
365 this->
set_state(espbt::ClientState::IDLE);
369 case ESP_GATTC_SEARCH_RES_EVT: {
370 if (this->
conn_id_ != param->search_res.conn_id)
378#ifdef USE_ESP32_BLE_DEVICE
380 ble_service->
uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid);
381 ble_service->
start_handle = param->search_res.start_handle;
382 ble_service->
end_handle = param->search_res.end_handle;
383 ble_service->
client =
this;
388 case ESP_GATTC_SEARCH_CMPL_EVT: {
389 if (this->
conn_id_ != param->search_cmpl.conn_id)
398#ifdef USE_ESP32_BLE_DEVICE
401 svc->uuid.to_string().c_str());
402 ESP_LOGV(TAG,
"[%d] [%s] start_handle: 0x%x end_handle: 0x%x", this->
connection_index_,
403 this->
address_str_.c_str(), svc->start_handle, svc->end_handle);
408 this->
state_ = espbt::ClientState::ESTABLISHED;
411 case ESP_GATTC_READ_DESCR_EVT: {
412 if (this->
conn_id_ != param->write.conn_id)
417 case ESP_GATTC_WRITE_DESCR_EVT: {
418 if (this->
conn_id_ != param->write.conn_id)
423 case ESP_GATTC_WRITE_CHAR_EVT: {
424 if (this->
conn_id_ != param->write.conn_id)
429 case ESP_GATTC_READ_CHAR_EVT: {
430 if (this->
conn_id_ != param->read.conn_id)
435 case ESP_GATTC_NOTIFY_EVT: {
436 if (this->
conn_id_ != param->notify.conn_id)
441 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
449 esp_gattc_descr_elem_t desc_result;
451 esp_gatt_status_t descr_status = esp_ble_gattc_get_descr_by_char_handle(
452 this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle, NOTIFY_DESC_UUID, &desc_result, &count);
453 if (descr_status != ESP_GATT_OK) {
457 esp_gattc_char_elem_t char_result;
458 esp_gatt_status_t char_status =
459 esp_ble_gattc_get_all_char(this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle,
460 param->reg_for_notify.handle, &char_result, &count, 0);
461 if (char_status != ESP_GATT_OK) {
470 uint16_t notify_en = char_result.properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY ? 1 : 2;
472 esp_ble_gattc_write_char_descr(this->
gattc_if_, this->
conn_id_, desc_result.handle,
sizeof(notify_en),
473 (uint8_t *) ¬ify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
474 ESP_LOGD(TAG,
"Wrote notify descriptor %d, properties=%d", notify_en, char_result.properties);
497 case ESP_GAP_BLE_SEC_REQ_EVT:
498 if (!this->
check_addr(param->ble_security.auth_cmpl.bd_addr))
501 esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr,
true);
504 case ESP_GAP_BLE_AUTH_CMPL_EVT:
505 if (!this->
check_addr(param->ble_security.auth_cmpl.bd_addr))
507 esp_bd_addr_t bd_addr;
508 memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr,
sizeof(esp_bd_addr_t));
511 if (!param->ble_security.auth_cmpl.success) {
513 param->ble_security.auth_cmpl.fail_reason);
516 ESP_LOGD(TAG,
"[%d] [%s] auth success. address type = %d auth mode = %d", this->
connection_index_,
517 this->
address_str_.c_str(), param->ble_security.auth_cmpl.addr_type,
518 param->ble_security.auth_cmpl.auth_mode);