diff options
Diffstat (limited to 'src/BLERemoteService.cpp')
-rw-r--r-- | src/BLERemoteService.cpp | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/src/BLERemoteService.cpp b/src/BLERemoteService.cpp index 78ff991..c2b7d34 100644 --- a/src/BLERemoteService.cpp +++ b/src/BLERemoteService.cpp @@ -11,13 +11,16 @@ #include "BLERemoteService.h" #include "BLEUtils.h" #include "GeneralUtils.h" -#include <esp_log.h> #include <esp_err.h> -#ifdef ARDUINO_ARCH_ESP32 +#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG) #include "esp32-hal-log.h" +#define LOG_TAG "" +#else +#include "esp_log.h" +static const char* LOG_TAG = "BLERemoteService"; #endif -static const char* LOG_TAG = "BLERemoteService"; + BLERemoteService::BLERemoteService( esp_gatt_id_t srvcId, @@ -60,8 +63,8 @@ static bool compareSrvcId(esp_gatt_srvc_id_t id1, esp_gatt_srvc_id_t id2) { void BLERemoteService::gattClientEventHandler( esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, - esp_ble_gattc_cb_param_t *evtParam) { - switch(event) { + esp_ble_gattc_cb_param_t* evtParam) { + switch (event) { // // ESP_GATTC_GET_CHAR_EVT // @@ -108,13 +111,12 @@ void BLERemoteService::gattClientEventHandler( break; } // ESP_GATTC_GET_CHAR_EVT */ - default: { + default: break; - } } // switch // Send the event to each of the characteristics owned by this service. - for (auto &myPair : m_characteristicMap) { + for (auto &myPair : m_characteristicMapByHandle) { myPair.second->gattClientEventHandler(event, gattc_if, evtParam); } } // gattClientEventHandler @@ -130,7 +132,6 @@ BLERemoteCharacteristic* BLERemoteService::getCharacteristic(const char* uuid) { return getCharacteristic(BLEUUID(uuid)); } // getCharacteristic - /** * @brief Get the characteristic object for the UUID. * @param [in] uuid Characteristic uuid. @@ -153,7 +154,8 @@ BLERemoteCharacteristic* BLERemoteService::getCharacteristic(BLEUUID uuid) { return myPair.second; } } - throw new BLEUuidNotFoundException(); + // throw new BLEUuidNotFoundException(); // <-- we dont want exception here, which will cause app crash, we want to search if any characteristic can be found one after another + return nullptr; } // getCharacteristic @@ -163,15 +165,14 @@ BLERemoteCharacteristic* BLERemoteService::getCharacteristic(BLEUUID uuid) { * @return N/A */ void BLERemoteService::retrieveCharacteristics() { - ESP_LOGD(LOG_TAG, ">> getCharacteristics() for service: %s", getUUID().toString().c_str()); removeCharacteristics(); // Forget any previous characteristics. uint16_t offset = 0; esp_gattc_char_elem_t result; - while(1) { - uint16_t count = 1; + while (true) { + uint16_t count = 10; // this value is used as in parameter that allows to search max 10 chars with the same uuid esp_gatt_status_t status = ::esp_ble_gattc_get_all_char( getClient()->getGattcIf(), getClient()->getConnId(), @@ -206,7 +207,7 @@ void BLERemoteService::retrieveCharacteristics() { ); m_characteristicMap.insert(std::pair<std::string, BLERemoteCharacteristic*>(pNewRemoteCharacteristic->getUUID().toString(), pNewRemoteCharacteristic)); - + m_characteristicMapByHandle.insert(std::pair<uint16_t, BLERemoteCharacteristic*>(result.char_handle, pNewRemoteCharacteristic)); offset++; // Increment our count of number of descriptors found. } // Loop forever (until we break inside the loop). @@ -219,7 +220,7 @@ void BLERemoteService::retrieveCharacteristics() { * @brief Retrieve a map of all the characteristics of this service. * @return A map of all the characteristics of this service. */ -std::map<std::string, BLERemoteCharacteristic *> * BLERemoteService::getCharacteristics() { +std::map<std::string, BLERemoteCharacteristic*>* BLERemoteService::getCharacteristics() { ESP_LOGD(LOG_TAG, ">> getCharacteristics() for service: %s", getUUID().toString().c_str()); // If is possible that we have not read the characteristics associated with the service so do that // now. The request to retrieve the characteristics by calling "retrieveCharacteristics" is a blocking @@ -231,6 +232,13 @@ std::map<std::string, BLERemoteCharacteristic *> * BLERemoteService::getCharacte return &m_characteristicMap; } // getCharacteristics +/** + * @brief This function is designed to get characteristics map when we have multiple characteristics with the same UUID + */ +void BLERemoteService::getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic*>* pCharacteristicMap) { +#pragma GCC diagnostic ignored "-Wunused-but-set-parameter" + pCharacteristicMap = &m_characteristicMapByHandle; +} // Get the characteristics map. /** * @brief Get the client associated with this service. @@ -292,6 +300,10 @@ void BLERemoteService::removeCharacteristics() { //m_characteristicMap.erase(myPair.first); // Should be no need to delete as it will be deleted by the clear } m_characteristicMap.clear(); // Clear the map + for (auto &myPair : m_characteristicMapByHandle) { + delete myPair.second; + } + m_characteristicMapByHandle.clear(); // Clear the map } // removeCharacteristics |