67 std::string state_name;
68 switch (this->
state()) {
69 case espbt::ClientState::INIT:
72 case espbt::ClientState::DISCONNECTING:
73 state_name =
"DISCONNECTING";
75 case espbt::ClientState::IDLE:
78 case espbt::ClientState::SEARCHING:
79 state_name =
"SEARCHING";
81 case espbt::ClientState::DISCOVERED:
82 state_name =
"DISCOVERED";
84 case espbt::ClientState::READY_TO_CONNECT:
85 state_name =
"READY_TO_CONNECT";
87 case espbt::ClientState::CONNECTING:
88 state_name =
"CONNECTING";
90 case espbt::ClientState::CONNECTED:
91 state_name =
"CONNECTED";
93 case espbt::ClientState::ESTABLISHED:
94 state_name =
"ESTABLISHED";
97 state_name =
"UNKNOWN_STATE";
100 ESP_LOGCONFIG(TAG,
" State: %s", state_name.c_str());
101 if (this->
status_ == ESP_GATT_NO_RESOURCES) {
102 ESP_LOGE(TAG,
" Failed due to no resources. Try to reduce number of BLE clients in config.");
103 }
else if (this->
status_ != ESP_GATT_OK) {
104 ESP_LOGW(TAG,
" Failed due to error code %d", this->
status_);
212 esp_ble_gattc_cb_param_t *param) {
213 if (event == ESP_GATTC_REG_EVT && this->
app_id != param->reg.app_id)
215 if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && esp_gattc_if != this->
gattc_if_)
218 ESP_LOGV(TAG,
"[%d] [%s] gattc_event_handler: event=%d gattc_if=%d", this->
connection_index_,
222 case ESP_GATTC_REG_EVT: {
223 if (param->reg.status == ESP_GATT_OK) {
228 ESP_LOGE(TAG,
"[%d] [%s] gattc app registration failed id=%d code=%d", this->
connection_index_,
229 this->
address_str_.c_str(), param->reg.app_id, param->reg.status);
230 this->
status_ = param->reg.status;
235 case ESP_GATTC_OPEN_EVT: {
236 if (!this->
check_addr(param->open.remote_bda))
239 this->
conn_id_ = param->open.conn_id;
241 if (this->
state_ != espbt::ClientState::CONNECTING) {
245 if (this->
state_ == espbt::ClientState::CONNECTED) {
246 ESP_LOGE(TAG,
"[%d] [%s] Got ESP_GATTC_OPEN_EVT while already connected, status=%d", this->
connection_index_,
248 }
else if (this->
state_ == espbt::ClientState::ESTABLISHED) {
249 ESP_LOGE(TAG,
"[%d] [%s] Got ESP_GATTC_OPEN_EVT while already established, status=%d",
251 }
else if (this->
state_ == espbt::ClientState::DISCONNECTING) {
252 ESP_LOGE(TAG,
"[%d] [%s] Got ESP_GATTC_OPEN_EVT while disconnecting, status=%d", this->
connection_index_,
255 ESP_LOGE(TAG,
"[%d] [%s] Got ESP_GATTC_OPEN_EVT while not in connecting state, status=%d",
259 if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
262 this->
set_state(espbt::ClientState::IDLE);
273 auto ret = esp_ble_gattc_send_mtu_req(this->
gattc_if_, param->open.conn_id);
275 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_send_mtu_req failed, status=%x", this->
connection_index_,
278 this->
set_state(espbt::ClientState::CONNECTED);
282 this->
state_ = espbt::ClientState::ESTABLISHED;
285 esp_ble_gattc_search_service(esp_gattc_if, param->cfg_mtu.conn_id,
nullptr);
288 case ESP_GATTC_CONNECT_EVT: {
289 if (!this->
check_addr(param->connect.remote_bda))
294 case ESP_GATTC_DISCONNECT_EVT: {
295 if (!this->
check_addr(param->disconnect.remote_bda))
297 ESP_LOGD(TAG,
"[%d] [%s] ESP_GATTC_DISCONNECT_EVT, reason %d", this->
connection_index_,
300 this->
set_state(espbt::ClientState::IDLE);
304 case ESP_GATTC_CFG_MTU_EVT: {
305 if (this->
conn_id_ != param->cfg_mtu.conn_id)
307 if (param->cfg_mtu.status != ESP_GATT_OK) {
308 ESP_LOGW(TAG,
"[%d] [%s] cfg_mtu failed, mtu %d, status %d", this->
connection_index_,
309 this->
address_str_.c_str(), param->cfg_mtu.mtu, param->cfg_mtu.status);
314 param->cfg_mtu.status, param->cfg_mtu.mtu);
315 this->
mtu_ = param->cfg_mtu.mtu;
318 case ESP_GATTC_CLOSE_EVT: {
319 if (this->
conn_id_ != param->close.conn_id)
323 this->
set_state(espbt::ClientState::IDLE);
327 case ESP_GATTC_SEARCH_RES_EVT: {
328 if (this->
conn_id_ != param->search_res.conn_id)
337 ble_service->
uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid);
338 ble_service->
start_handle = param->search_res.start_handle;
339 ble_service->
end_handle = param->search_res.end_handle;
340 ble_service->
client =
this;
344 case ESP_GATTC_SEARCH_CMPL_EVT: {
345 if (this->
conn_id_ != param->search_cmpl.conn_id)
347 this->
log_event_(
"ESP_GATTC_SEARCH_CMPL_EVT");
350 svc->uuid.to_string().c_str());
351 ESP_LOGV(TAG,
"[%d] [%s] start_handle: 0x%x end_handle: 0x%x", this->
connection_index_,
352 this->
address_str_.c_str(), svc->start_handle, svc->end_handle);
355 this->
state_ = espbt::ClientState::ESTABLISHED;
358 case ESP_GATTC_READ_DESCR_EVT: {
359 if (this->
conn_id_ != param->write.conn_id)
364 case ESP_GATTC_WRITE_DESCR_EVT: {
365 if (this->
conn_id_ != param->write.conn_id)
367 this->
log_event_(
"ESP_GATTC_WRITE_DESCR_EVT");
370 case ESP_GATTC_WRITE_CHAR_EVT: {
371 if (this->
conn_id_ != param->write.conn_id)
376 case ESP_GATTC_READ_CHAR_EVT: {
377 if (this->
conn_id_ != param->read.conn_id)
382 case ESP_GATTC_NOTIFY_EVT: {
383 if (this->
conn_id_ != param->notify.conn_id)
388 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
389 this->
log_event_(
"ESP_GATTC_REG_FOR_NOTIFY_EVT");
396 esp_gattc_descr_elem_t desc_result;
398 esp_gatt_status_t descr_status = esp_ble_gattc_get_descr_by_char_handle(
399 this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle, NOTIFY_DESC_UUID, &desc_result, &count);
400 if (descr_status != ESP_GATT_OK) {
401 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_get_descr_by_char_handle error, status=%d", this->
connection_index_,
405 esp_gattc_char_elem_t char_result;
406 esp_gatt_status_t char_status =
407 esp_ble_gattc_get_all_char(this->
gattc_if_, this->
conn_id_, param->reg_for_notify.handle,
408 param->reg_for_notify.handle, &char_result, &count, 0);
409 if (char_status != ESP_GATT_OK) {
410 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", this->
connection_index_,
419 uint16_t notify_en = char_result.properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY ? 1 : 2;
421 esp_ble_gattc_write_char_descr(this->
gattc_if_, this->
conn_id_, desc_result.handle,
sizeof(notify_en),
422 (uint8_t *) ¬ify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
423 ESP_LOGD(TAG,
"Wrote notify descriptor %d, properties=%d", notify_en, char_result.properties);
425 ESP_LOGW(TAG,
"[%d] [%s] esp_ble_gattc_write_char_descr error, status=%d", this->
connection_index_,
447 case ESP_GAP_BLE_SEC_REQ_EVT:
448 if (!this->
check_addr(param->ble_security.auth_cmpl.bd_addr))
451 esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr,
true);
454 case ESP_GAP_BLE_AUTH_CMPL_EVT:
455 if (!this->
check_addr(param->ble_security.auth_cmpl.bd_addr))
457 esp_bd_addr_t bd_addr;
458 memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr,
sizeof(esp_bd_addr_t));
461 if (!param->ble_security.auth_cmpl.success) {
463 param->ble_security.auth_cmpl.fail_reason);
466 ESP_LOGD(TAG,
"[%d] [%s] auth success. address type = %d auth mode = %d", this->
connection_index_,
467 this->
address_str_.c_str(), param->ble_security.auth_cmpl.addr_type,
468 param->ble_security.auth_cmpl.auth_mode);