diff options
author | Neil Kolban <kolban1@kolban.com> | 2017-11-21 09:51:03 -0600 |
---|---|---|
committer | Neil Kolban <kolban1@kolban.com> | 2017-11-21 09:51:03 -0600 |
commit | 4a781e01832176d61af010669b8c362f5a777768 (patch) | |
tree | 6a7a16309d61c4728512f5eb632d3ad565288c36 /src/BLEClient.cpp | |
parent | Fixes for #121 (diff) | |
download | thermostat-4a781e01832176d61af010669b8c362f5a777768.tar.gz thermostat-4a781e01832176d61af010669b8c362f5a777768.tar.bz2 thermostat-4a781e01832176d61af010669b8c362f5a777768.zip |
Updates 2017-11-21 0950
Diffstat (limited to 'src/BLEClient.cpp')
-rw-r--r-- | src/BLEClient.cpp | 106 |
1 files changed, 96 insertions, 10 deletions
diff --git a/src/BLEClient.cpp b/src/BLEClient.cpp index 93f703a..83e778d 100644 --- a/src/BLEClient.cpp +++ b/src/BLEClient.cpp @@ -49,6 +49,7 @@ BLEClient::BLEClient() { m_conn_id = 0; m_gattc_if = 0; m_haveServices = false; + m_isConnected = false; // Initially, we are flagged as not connected. } // BLEClient @@ -76,7 +77,8 @@ bool BLEClient::connect(BLEAddress address) { // We need the connection handle that we get from registering the application. We register the app // and then block on its completion. When the event has arrived, we will have the handle. m_semaphoreRegEvt.take("connect"); - esp_err_t errRc = esp_ble_gattc_app_register(0); + + esp_err_t errRc = ::esp_ble_gattc_app_register(0); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "esp_ble_gattc_app_register: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); return false; @@ -115,6 +117,7 @@ void BLEClient::disconnect() { ESP_LOGE(LOG_TAG, "esp_ble_gattc_close: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); return; } + esp_ble_gattc_app_unregister(getGattcIf()); m_peerAddress = BLEAddress("00:00:00:00:00:00"); ESP_LOGD(LOG_TAG, "<< disconnect()"); } // disconnect @@ -130,6 +133,22 @@ void BLEClient::gattClientEventHandler( // Execute handler code based on the type of event received. switch(event) { + + // + // 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; + break; + } // ESP_GATTC_DISCONNECT_EVT + + // // ESP_GATTC_OPEN_EVT // @@ -144,7 +163,10 @@ void BLEClient::gattClientEventHandler( if (m_pClientCallbacks != nullptr) { m_pClientCallbacks->onConnect(this); } - m_semaphoreOpenEvt.give(); + 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 @@ -211,6 +233,17 @@ void BLEClient::gattClientEventHandler( } // gattClientEventHandler +uint16_t BLEClient::getConnId() { + return m_conn_id; +} // getConnId + + + +esp_gatt_if_t BLEClient::getGattcIf() { + return m_gattc_if; +} // getGattcIf + + /** * @brief Retrieve the address of the peer. * @@ -221,14 +254,29 @@ BLEAddress BLEClient::getPeerAddress() { } // getAddress -uint16_t BLEClient::getConnId() { - return m_conn_id; -} // getConnId - - -esp_gatt_if_t BLEClient::getGattcIf() { - return m_gattc_if; -} // getGattcIf +/** + * @brief Ask the BLE server for the RSSI value. + * @return The RSSI value. + */ +int BLEClient::getRssi() { + ESP_LOGD(LOG_TAG, ">> getRssi()"); + if (!isConnected()) { + ESP_LOGD(LOG_TAG, "<< getRssi(): Not connected"); + return 0; + } + // We make the API call to read the RSSI value which is an asynchronous operation. We expect to receive + // an ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT to indicate completion. + // + m_semaphoreRssiCmplEvt.take("getRssi"); + esp_err_t rc = ::esp_ble_gap_read_rssi(*getPeerAddress().getNative()); + if (rc != ESP_OK) { + ESP_LOGE(LOG_TAG, "<< getRssi: esp_ble_gap_read_rssi: rc=%d %s", rc, GeneralUtils::errorToString(rc)); + return 0; + } + int rssiValue = m_semaphoreRssiCmplEvt.wait("getRssi"); + ESP_LOGD(LOG_TAG, "<< getRssi(): %d", rssiValue); + return rssiValue; +} // getRssi /** @@ -301,6 +349,44 @@ std::map<std::string, BLERemoteService*>* BLEClient::getServices() { return &m_servicesMap; } // getServices +/** + * @brief Handle a received GAP event. + * + * @param [in] event + * @param [in] param + */ +void BLEClient::handleGAPEvent( + esp_gap_ble_cb_event_t event, + esp_ble_gap_cb_param_t* param) { + ESP_LOGD(LOG_TAG, "BLEClient ... handling GAP event!"); + switch(event) { + // + // ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT + // + // read_rssi_cmpl + // - esp_bt_status_t status + // - int8_t rssi + // - esp_bd_addr_t remote_addr + // + case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT: { + m_semaphoreRssiCmplEvt.give((uint32_t)param->read_rssi_cmpl.rssi); + break; + } // ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT + + default: + break; + } +} // handleGAPEvent + + +/** + * @brief Are we connected to a partner? + * @return True if we are connected and false if we are not connected. + */ +bool BLEClient::isConnected() { + return m_isConnected; +} // isConnected + /** * @brief Set the callbacks that will be invoked. |