/** * @brief Handle GATT client events. * * Handler for the GATT client events. * * @param [in] event * @param [in] gattc_if * @param [in] param */ /* STATIC */ void BLEDevice::gattClientEventHandler( esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* param) { ESP_LOGD(LOG_TAG, "gattClientEventHandler [esp_gatt_if: %d] ... %s", gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str()); BLEUtils::dumpGattClientEvent(event, gattc_if, param); switch(event) { case ESP_GATTC_CONNECT_EVT: { if(BLEDevice::getMTU() != 23){ esp_err_t errRc = esp_ble_gattc_send_mtu_req(gattc_if, param->connect.conn_id); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "esp_ble_gattc_send_mtu_req: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); } } if(BLEDevice::m_securityLevel){ esp_ble_set_encryption(param->connect.remote_bda, BLEDevice::m_securityLevel); } break; } // ESP_GATTC_CONNECT_EVT default: { break; } } // switch // If we have a client registered, call it. if (BLEDevice::m_pClient != nullptr) { BLEDevice::m_pClient->gattClientEventHandler(event, gattc_if, param); } } // gattClientEventHandler
/** * @brief Handle GATT Client events */ void BLEClient::gattClientEventHandler( esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam) { ESP_LOGD(LOG_TAG, "gattClientEventHandler [esp_gatt_if: %d] ... %s", gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str()); // Execute handler code based on the type of event received. switch(event) { case ESP_GATTC_SRVC_CHG_EVT: ESP_LOGI(LOG_TAG, "SERVICE CHANGED"); break; case ESP_GATTC_CLOSE_EVT: { // esp_ble_gattc_app_unregister(m_appId); // BLEDevice::removePeerDevice(m_gattc_if, true); break; } // // ESP_GATTC_DISCONNECT_EVT // // disconnect: // - esp_gatt_status_t status // - uint16_t conn_id // - esp_bd_addr_t remote_bda case ESP_GATTC_DISCONNECT_EVT: { // If we receive a disconnect event, set the class flag that indicates that we are // no longer connected. m_isConnected = false; if (m_pClientCallbacks != nullptr) { m_pClientCallbacks->onDisconnect(this); } BLEDevice::removePeerDevice(m_appId, true); esp_ble_gattc_app_unregister(m_gattc_if); m_semaphoreRssiCmplEvt.give(); m_semaphoreSearchCmplEvt.give(1); break; } // ESP_GATTC_DISCONNECT_EVT // // ESP_GATTC_OPEN_EVT // // open: // - esp_gatt_status_t status // - uint16_t conn_id // - esp_bd_addr_t remote_bda // case ESP_GATTC_OPEN_EVT: { m_conn_id = evtParam->open.conn_id; if (m_pClientCallbacks != nullptr) { m_pClientCallbacks->onConnect(this); } if (evtParam->open.status == ESP_GATT_OK) { m_isConnected = true; // Flag us as connected. } m_semaphoreOpenEvt.give(evtParam->open.status); break; } // ESP_GATTC_OPEN_EVT // // ESP_GATTC_REG_EVT // // reg: // esp_gatt_status_t status // uint16_t app_id // case ESP_GATTC_REG_EVT: { m_gattc_if = gattc_if; m_semaphoreRegEvt.give(); break; } // ESP_GATTC_REG_EVT case ESP_GATTC_CFG_MTU_EVT: if(evtParam->cfg_mtu.status != ESP_GATT_OK) { ESP_LOGE(LOG_TAG,"Config mtu failed"); } m_mtu = evtParam->cfg_mtu.mtu; break; case ESP_GATTC_CONNECT_EVT: { BLEDevice::updatePeerDevice(this, true, m_gattc_if); esp_err_t errRc = esp_ble_gattc_send_mtu_req(gattc_if, evtParam->connect.conn_id); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "esp_ble_gattc_send_mtu_req: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); } #ifdef CONFIG_BLE_SMP_ENABLE // Check that BLE SMP (security) is configured in make menuconfig if(BLEDevice::m_securityLevel){ esp_ble_set_encryption(evtParam->connect.remote_bda, BLEDevice::m_securityLevel); } #endif // CONFIG_BLE_SMP_ENABLE break; } // ESP_GATTC_CONNECT_EVT // // ESP_GATTC_SEARCH_CMPL_EVT // // search_cmpl: // - esp_gatt_status_t status // - uint16_t conn_id // case ESP_GATTC_SEARCH_CMPL_EVT: { esp_ble_gattc_cb_param_t* p_data = (esp_ble_gattc_cb_param_t*)evtParam; if (p_data->search_cmpl.status != ESP_GATT_OK){ ESP_LOGE(LOG_TAG, "search service failed, error status = %x", p_data->search_cmpl.status); break; } #ifndef ARDUINO_ARCH_ESP32 // commented out just for now to keep backward compatibility // if(p_data->search_cmpl.searched_service_source == ESP_GATT_SERVICE_FROM_REMOTE_DEVICE) { // ESP_LOGI(LOG_TAG, "Get service information from remote device"); // } else if (p_data->search_cmpl.searched_service_source == ESP_GATT_SERVICE_FROM_NVS_FLASH) { // ESP_LOGI(LOG_TAG, "Get service information from flash"); // } else { // ESP_LOGI(LOG_TAG, "unknown service source"); // } #endif m_semaphoreSearchCmplEvt.give(0); break; } // ESP_GATTC_SEARCH_CMPL_EVT // // ESP_GATTC_SEARCH_RES_EVT // // search_res: // - uint16_t conn_id // - uint16_t start_handle // - uint16_t end_handle // - esp_gatt_id_t srvc_id // case ESP_GATTC_SEARCH_RES_EVT: { BLEUUID uuid = BLEUUID(evtParam->search_res.srvc_id); BLERemoteService* pRemoteService = new BLERemoteService( evtParam->search_res.srvc_id, this, evtParam->search_res.start_handle, evtParam->search_res.end_handle ); m_servicesMap.insert(std::pair<std::string, BLERemoteService*>(uuid.toString(), pRemoteService)); m_servicesMapByInstID.insert(std::pair<BLERemoteService *, uint16_t>(pRemoteService, evtParam->search_res.srvc_id.inst_id)); break; } // ESP_GATTC_SEARCH_RES_EVT default: { break; } } // Switch // Pass the request on to all services. for (auto &myPair : m_servicesMap) { myPair.second->gattClientEventHandler(event, gattc_if, evtParam); } } // gattClientEventHandler