195 esp_ble_gattc_cb_param_t *param) {
196 if (event == ESP_GATTC_REG_EVT && this->
app_id != param->reg.app_id)
198 if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && esp_gattc_if != this->
gattc_if_)
201 ESP_LOGV(TAG,
"[%d] [%s] gattc_event_handler: event=%d gattc_if=%d", this->
connection_index_,
205 case ESP_GATTC_REG_EVT: {
206 if (param->reg.status == ESP_GATT_OK) {
211 ESP_LOGE(TAG,
"[%d] [%s] gattc app registration failed id=%d code=%d", this->
connection_index_,
212 this->
address_str_.c_str(), param->reg.app_id, param->reg.status);
213 this->
status_ = param->reg.status;
218 case ESP_GATTC_OPEN_EVT: {
219 if (!this->
check_addr(param->open.remote_bda))
222 this->
conn_id_ = param->open.conn_id;
224 if (this->
state_ != espbt::ClientState::CONNECTING) {
228 if (this->
state_ == espbt::ClientState::CONNECTED) {
229 ESP_LOGE(TAG,
"[%d] [%s] Got ESP_GATTC_OPEN_EVT while already connected, status=%d", this->
connection_index_,
231 }
else if (this->
state_ == espbt::ClientState::ESTABLISHED) {
232 ESP_LOGE(TAG,
"[%d] [%s] Got ESP_GATTC_OPEN_EVT while already established, status=%d",
234 }
else if (this->
state_ == espbt::ClientState::DISCONNECTING) {
235 ESP_LOGE(TAG,
"[%d] [%s] Got ESP_GATTC_OPEN_EVT while disconnecting, status=%d", this->
connection_index_,
238 ESP_LOGE(TAG,
"[%d] [%s] Got ESP_GATTC_OPEN_EVT while not in connecting state, status=%d",
242 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
245 this->
set_state(espbt::ClientState::IDLE);
256 auto ret = esp_ble_gattc_send_mtu_req(this->
gattc_if_, param->open.conn_id);
258 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_send_mtu_req failed, status=%x", this->
connection_index_,
261 this->
set_state(espbt::ClientState::CONNECTED);
265 this->
state_ = espbt::ClientState::ESTABLISHED;
268 esp_ble_gattc_search_service(esp_gattc_if, param->cfg_mtu.conn_id,
nullptr);
271 case ESP_GATTC_CONNECT_EVT: {
272 if (!this->
check_addr(param->connect.remote_bda))
277 case ESP_GATTC_DISCONNECT_EVT: {
278 if (!this->
check_addr(param->disconnect.remote_bda))
280 ESP_LOGD(TAG,
"[%d] [%s] ESP_GATTC_DISCONNECT_EVT, reason %d", this->
connection_index_,
283 this->
set_state(espbt::ClientState::IDLE);
287 case ESP_GATTC_CFG_MTU_EVT: {
288 if (this->
conn_id_ != param->cfg_mtu.conn_id)
290 if (param->cfg_mtu.status != ESP_GATT_OK) {
291 ESP_LOGW(TAG,
"[%d] [%s] cfg_mtu failed, mtu %d, status %d", this->
connection_index_,
292 this->
address_str_.c_str(), param->cfg_mtu.mtu, param->cfg_mtu.status);
297 param->cfg_mtu.status, param->cfg_mtu.mtu);
298 this->
mtu_ = param->cfg_mtu.mtu;
301 case ESP_GATTC_CLOSE_EVT: {
302 if (this->
conn_id_ != param->close.conn_id)
306 this->
set_state(espbt::ClientState::IDLE);
310 case ESP_GATTC_SEARCH_RES_EVT: {
311 if (this->
conn_id_ != param->search_res.conn_id)
320 ble_service->
uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid);
321 ble_service->
start_handle = param->search_res.start_handle;
322 ble_service->
end_handle = param->search_res.end_handle;
323 ble_service->
client =
this;
327 case ESP_GATTC_SEARCH_CMPL_EVT: {
328 if (this->
conn_id_ != param->search_cmpl.conn_id)
330 this->
log_event_(
"ESP_GATTC_SEARCH_CMPL_EVT");
333 svc->uuid.to_string().c_str());
334 ESP_LOGV(TAG,
"[%d] [%s] start_handle: 0x%x end_handle: 0x%x", this->
connection_index_,
335 this->
address_str_.c_str(), svc->start_handle, svc->end_handle);
338 this->
state_ = espbt::ClientState::ESTABLISHED;
341 case ESP_GATTC_READ_DESCR_EVT: {
342 if (this->
conn_id_ != param->write.conn_id)
347 case ESP_GATTC_WRITE_DESCR_EVT: {
348 if (this->
conn_id_ != param->write.conn_id)
350 this->
log_event_(
"ESP_GATTC_WRITE_DESCR_EVT");
353 case ESP_GATTC_WRITE_CHAR_EVT: {
354 if (this->
conn_id_ != param->write.conn_id)
359 case ESP_GATTC_READ_CHAR_EVT: {
360 if (this->
conn_id_ != param->read.conn_id)
365 case ESP_GATTC_NOTIFY_EVT: {
366 if (this->
conn_id_ != param->notify.conn_id)
371 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
372 this->
log_event_(
"ESP_GATTC_REG_FOR_NOTIFY_EVT");
379 esp_gattc_descr_elem_t desc_result;
381 esp_gatt_status_t descr_status = esp_ble_gattc_get_descr_by_char_handle(
382 this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle, NOTIFY_DESC_UUID, &desc_result, &count);
383 if (descr_status != ESP_GATT_OK) {
384 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_get_descr_by_char_handle error, status=%d", this->
connection_index_,
388 esp_gattc_char_elem_t char_result;
389 esp_gatt_status_t char_status =
390 esp_ble_gattc_get_all_char(this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle,
391 param->reg_for_notify.handle, &char_result, &count, 0);
392 if (char_status != ESP_GATT_OK) {
393 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", this->
connection_index_,
402 uint16_t notify_en = char_result.properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY ? 1 : 2;
404 esp_ble_gattc_write_char_descr(this->
gattc_if_, this->
conn_id_, desc_result.handle,
sizeof(notify_en),
405 (uint8_t *) ¬ify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
406 ESP_LOGD(TAG,
"Wrote notify descriptor %d, properties=%d", notify_en, char_result.properties);
408 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_write_char_descr error, status=%d", this->
connection_index_,
430 case ESP_GAP_BLE_SEC_REQ_EVT:
431 if (!this->
check_addr(param->ble_security.auth_cmpl.bd_addr))
434 esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr,
true);
437 case ESP_GAP_BLE_AUTH_CMPL_EVT:
438 if (!this->
check_addr(param->ble_security.auth_cmpl.bd_addr))
440 esp_bd_addr_t bd_addr;
441 memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr,
sizeof(esp_bd_addr_t));
444 if (!param->ble_security.auth_cmpl.success) {
446 param->ble_security.auth_cmpl.fail_reason);
449 ESP_LOGD(TAG,
"[%d] [%s] auth success. address type = %d auth mode = %d", this->
connection_index_,
450 this->
address_str_.c_str(), param->ble_security.auth_cmpl.addr_type,
451 param->ble_security.auth_cmpl.auth_mode);