Unverified Commit 32def87c authored by Rodrigo Garcia's avatar Rodrigo Garcia Committed by GitHub

fix(BLE): Arduino String shall not be used within std::map<> (#9875)

* fix(BLE): std::map()

* Update libraries/BLE/src/BLERemoteService.cpp

* ci(pre-commit): Apply automatic fixes

---------
Co-authored-by: default avatarpre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
parent c43187a3
...@@ -343,7 +343,7 @@ void BLEClient::gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t ...@@ -343,7 +343,7 @@ void BLEClient::gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t
BLEUUID uuid = BLEUUID(evtParam->search_res.srvc_id); BLEUUID uuid = BLEUUID(evtParam->search_res.srvc_id);
BLERemoteService *pRemoteService = BLERemoteService *pRemoteService =
new BLERemoteService(evtParam->search_res.srvc_id, this, evtParam->search_res.start_handle, evtParam->search_res.end_handle); new BLERemoteService(evtParam->search_res.srvc_id, this, evtParam->search_res.start_handle, evtParam->search_res.end_handle);
m_servicesMap.insert(std::pair<String, BLERemoteService *>(uuid.toString(), pRemoteService)); m_servicesMap.insert(std::pair<std::string, BLERemoteService *>(uuid.toString().c_str(), pRemoteService));
m_servicesMapByInstID.insert(std::pair<BLERemoteService *, uint16_t>(pRemoteService, evtParam->search_res.srvc_id.inst_id)); m_servicesMapByInstID.insert(std::pair<BLERemoteService *, uint16_t>(pRemoteService, evtParam->search_res.srvc_id.inst_id));
break; break;
} // ESP_GATTC_SEARCH_RES_EVT } // ESP_GATTC_SEARCH_RES_EVT
...@@ -428,7 +428,7 @@ BLERemoteService *BLEClient::getService(BLEUUID uuid) { ...@@ -428,7 +428,7 @@ BLERemoteService *BLEClient::getService(BLEUUID uuid) {
if (!m_haveServices) { if (!m_haveServices) {
getServices(); getServices();
} }
String uuidStr = uuid.toString(); std::string uuidStr = uuid.toString().c_str();
for (auto &myPair : m_servicesMap) { for (auto &myPair : m_servicesMap) {
if (myPair.first == uuidStr) { if (myPair.first == uuidStr) {
log_v("<< getService: found the service with uuid: %s", uuid.toString().c_str()); log_v("<< getService: found the service with uuid: %s", uuid.toString().c_str());
...@@ -445,7 +445,7 @@ BLERemoteService *BLEClient::getService(BLEUUID uuid) { ...@@ -445,7 +445,7 @@ BLERemoteService *BLEClient::getService(BLEUUID uuid) {
* services and wait until we have received them all. * services and wait until we have received them all.
* @return N/A * @return N/A
*/ */
std::map<String, BLERemoteService *> *BLEClient::getServices() { std::map<std::string, BLERemoteService *> *BLEClient::getServices() {
/* /*
* Design * Design
* ------ * ------
......
...@@ -42,7 +42,7 @@ public: ...@@ -42,7 +42,7 @@ public:
void disconnect(); // Disconnect from the remote BLE Server void disconnect(); // Disconnect from the remote BLE Server
BLEAddress getPeerAddress(); // Get the address of the remote BLE Server BLEAddress getPeerAddress(); // Get the address of the remote BLE Server
int getRssi(); // Get the RSSI of the remote BLE Server int getRssi(); // Get the RSSI of the remote BLE Server
std::map<String, BLERemoteService *> *getServices(); // Get a map of the services offered by the remote BLE Server std::map<std::string, BLERemoteService *> *getServices(); // Get a map of the services offered by the remote BLE Server
BLERemoteService *getService(const char *uuid); // Get a reference to a specified service offered by the remote BLE server. BLERemoteService *getService(const char *uuid); // Get a reference to a specified service offered by the remote BLE server.
BLERemoteService *getService(BLEUUID uuid); // Get a reference to a specified service offered by the remote BLE server. BLERemoteService *getService(BLEUUID uuid); // Get a reference to a specified service offered by the remote BLE server.
String getValue(BLEUUID serviceUUID, BLEUUID characteristicUUID); // Get the value of a given characteristic at a given service. String getValue(BLEUUID serviceUUID, BLEUUID characteristicUUID); // Get the value of a given characteristic at a given service.
...@@ -82,7 +82,7 @@ private: ...@@ -82,7 +82,7 @@ private:
FreeRTOS::Semaphore m_semaphoreOpenEvt = FreeRTOS::Semaphore("OpenEvt"); FreeRTOS::Semaphore m_semaphoreOpenEvt = FreeRTOS::Semaphore("OpenEvt");
FreeRTOS::Semaphore m_semaphoreSearchCmplEvt = FreeRTOS::Semaphore("SearchCmplEvt"); FreeRTOS::Semaphore m_semaphoreSearchCmplEvt = FreeRTOS::Semaphore("SearchCmplEvt");
FreeRTOS::Semaphore m_semaphoreRssiCmplEvt = FreeRTOS::Semaphore("RssiCmplEvt"); FreeRTOS::Semaphore m_semaphoreRssiCmplEvt = FreeRTOS::Semaphore("RssiCmplEvt");
std::map<String, BLERemoteService *> m_servicesMap; std::map<std::string, BLERemoteService *> m_servicesMap;
std::map<BLERemoteService *, uint16_t> m_servicesMapByInstID; std::map<BLERemoteService *, uint16_t> m_servicesMapByInstID;
void clearServices(); // Clear any existing services. void clearServices(); // Clear any existing services.
uint16_t m_mtu = 23; uint16_t m_mtu = 23;
......
...@@ -297,7 +297,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() { ...@@ -297,7 +297,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
// We now have a new characteristic ... let us add that to our set of known characteristics // We now have a new characteristic ... let us add that to our set of known characteristics
BLERemoteDescriptor *pNewRemoteDescriptor = new BLERemoteDescriptor(result.handle, BLEUUID(result.uuid), this); BLERemoteDescriptor *pNewRemoteDescriptor = new BLERemoteDescriptor(result.handle, BLEUUID(result.uuid), this);
m_descriptorMap.insert(std::pair<String, BLERemoteDescriptor *>(pNewRemoteDescriptor->getUUID().toString(), pNewRemoteDescriptor)); m_descriptorMap.insert(std::pair<std::string, BLERemoteDescriptor *>(pNewRemoteDescriptor->getUUID().toString().c_str(), pNewRemoteDescriptor));
offset++; offset++;
} // while true } // while true
...@@ -308,7 +308,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() { ...@@ -308,7 +308,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
/** /**
* @brief Retrieve the map of descriptors keyed by UUID. * @brief Retrieve the map of descriptors keyed by UUID.
*/ */
std::map<String, BLERemoteDescriptor *> *BLERemoteCharacteristic::getDescriptors() { std::map<std::string, BLERemoteDescriptor *> *BLERemoteCharacteristic::getDescriptors() {
return &m_descriptorMap; return &m_descriptorMap;
} // getDescriptors } // getDescriptors
...@@ -329,7 +329,7 @@ uint16_t BLERemoteCharacteristic::getHandle() { ...@@ -329,7 +329,7 @@ uint16_t BLERemoteCharacteristic::getHandle() {
*/ */
BLERemoteDescriptor *BLERemoteCharacteristic::getDescriptor(BLEUUID uuid) { BLERemoteDescriptor *BLERemoteCharacteristic::getDescriptor(BLEUUID uuid) {
log_v(">> getDescriptor: uuid: %s", uuid.toString().c_str()); log_v(">> getDescriptor: uuid: %s", uuid.toString().c_str());
String v = uuid.toString(); std::string v = uuid.toString().c_str();
for (auto &myPair : m_descriptorMap) { for (auto &myPair : m_descriptorMap) {
if (myPair.first == v) { if (myPair.first == v) {
log_v("<< getDescriptor: found"); log_v("<< getDescriptor: found");
......
...@@ -39,7 +39,7 @@ public: ...@@ -39,7 +39,7 @@ public:
bool canWrite(); bool canWrite();
bool canWriteNoResponse(); bool canWriteNoResponse();
BLERemoteDescriptor *getDescriptor(BLEUUID uuid); BLERemoteDescriptor *getDescriptor(BLEUUID uuid);
std::map<String, BLERemoteDescriptor *> *getDescriptors(); std::map<std::string, BLERemoteDescriptor *> *getDescriptors();
BLERemoteService *getRemoteService(); BLERemoteService *getRemoteService();
uint16_t getHandle(); uint16_t getHandle();
BLEUUID getUUID(); BLEUUID getUUID();
...@@ -82,7 +82,7 @@ private: ...@@ -82,7 +82,7 @@ private:
notify_callback m_notifyCallback; notify_callback m_notifyCallback;
// We maintain a map of descriptors owned by this characteristic keyed by a string representation of the UUID. // We maintain a map of descriptors owned by this characteristic keyed by a string representation of the UUID.
std::map<String, BLERemoteDescriptor *> m_descriptorMap; std::map<std::string, BLERemoteDescriptor *> m_descriptorMap;
}; // BLERemoteCharacteristic }; // BLERemoteCharacteristic
#endif /* CONFIG_BLUEDROID_ENABLED */ #endif /* CONFIG_BLUEDROID_ENABLED */
......
...@@ -79,8 +79,8 @@ void BLERemoteService::gattClientEventHandler(esp_gattc_cb_event_t event, esp_ga ...@@ -79,8 +79,8 @@ void BLERemoteService::gattClientEventHandler(esp_gattc_cb_event_t event, esp_ga
// This is an indication that we now have the characteristic details for a characteristic owned // This is an indication that we now have the characteristic details for a characteristic owned
// by this service so remember it. // by this service so remember it.
m_characteristicMap.insert(std::pair<String, BLERemoteCharacteristic*>( m_characteristicMap.insert(std::pair<std::string, BLERemoteCharacteristic*>(
BLEUUID(evtParam->get_char.char_id.uuid).toString(), BLEUUID(evtParam->get_char.char_id.uuid).toString().c_str(),
new BLERemoteCharacteristic(evtParam->get_char.char_id, evtParam->get_char.char_prop, this) )); new BLERemoteCharacteristic(evtParam->get_char.char_id, evtParam->get_char.char_prop, this) ));
...@@ -134,7 +134,7 @@ BLERemoteCharacteristic *BLERemoteService::getCharacteristic(BLEUUID uuid) { ...@@ -134,7 +134,7 @@ BLERemoteCharacteristic *BLERemoteService::getCharacteristic(BLEUUID uuid) {
if (!m_haveCharacteristics) { if (!m_haveCharacteristics) {
retrieveCharacteristics(); retrieveCharacteristics();
} }
String v = uuid.toString(); std::string v = uuid.toString().c_str();
for (auto &myPair : m_characteristicMap) { for (auto &myPair : m_characteristicMap) {
if (myPair.first == v) { if (myPair.first == v) {
return myPair.second; return myPair.second;
...@@ -179,7 +179,9 @@ void BLERemoteService::retrieveCharacteristics() { ...@@ -179,7 +179,9 @@ void BLERemoteService::retrieveCharacteristics() {
// We now have a new characteristic ... let us add that to our set of known characteristics // We now have a new characteristic ... let us add that to our set of known characteristics
BLERemoteCharacteristic *pNewRemoteCharacteristic = new BLERemoteCharacteristic(result.char_handle, BLEUUID(result.uuid), result.properties, this); BLERemoteCharacteristic *pNewRemoteCharacteristic = new BLERemoteCharacteristic(result.char_handle, BLEUUID(result.uuid), result.properties, this);
m_characteristicMap.insert(std::pair<String, BLERemoteCharacteristic *>(pNewRemoteCharacteristic->getUUID().toString(), pNewRemoteCharacteristic)); m_characteristicMap.insert(
std::pair<std::string, BLERemoteCharacteristic *>(pNewRemoteCharacteristic->getUUID().toString().c_str(), pNewRemoteCharacteristic)
);
m_characteristicMapByHandle.insert(std::pair<uint16_t, BLERemoteCharacteristic *>(result.char_handle, pNewRemoteCharacteristic)); m_characteristicMapByHandle.insert(std::pair<uint16_t, BLERemoteCharacteristic *>(result.char_handle, pNewRemoteCharacteristic));
offset++; // Increment our count of number of descriptors found. offset++; // Increment our count of number of descriptors found.
} // Loop forever (until we break inside the loop). } // Loop forever (until we break inside the loop).
...@@ -192,7 +194,7 @@ void BLERemoteService::retrieveCharacteristics() { ...@@ -192,7 +194,7 @@ void BLERemoteService::retrieveCharacteristics() {
* @brief Retrieve a map of all the characteristics of this service. * @brief Retrieve a map of all the characteristics of this service.
* @return A map of all the characteristics of this service. * @return A map of all the characteristics of this service.
*/ */
std::map<String, BLERemoteCharacteristic *> *BLERemoteService::getCharacteristics() { std::map<std::string, BLERemoteCharacteristic *> *BLERemoteService::getCharacteristics() {
log_v(">> getCharacteristics() for service: %s", getUUID().toString().c_str()); log_v(">> 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 // 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 // now. The request to retrieve the characteristics by calling "retrieveCharacteristics" is a blocking
......
...@@ -34,7 +34,7 @@ public: ...@@ -34,7 +34,7 @@ public:
BLERemoteCharacteristic *getCharacteristic(const char *uuid); // Get the specified characteristic reference. BLERemoteCharacteristic *getCharacteristic(const char *uuid); // Get the specified characteristic reference.
BLERemoteCharacteristic *getCharacteristic(BLEUUID uuid); // Get the specified characteristic reference. BLERemoteCharacteristic *getCharacteristic(BLEUUID uuid); // Get the specified characteristic reference.
BLERemoteCharacteristic *getCharacteristic(uint16_t uuid); // Get the specified characteristic reference. BLERemoteCharacteristic *getCharacteristic(uint16_t uuid); // Get the specified characteristic reference.
std::map<String, BLERemoteCharacteristic *> *getCharacteristics(); std::map<std::string, BLERemoteCharacteristic *> *getCharacteristics();
std::map<uint16_t, BLERemoteCharacteristic *> *getCharacteristicsByHandle(); // Get the characteristics map. std::map<uint16_t, BLERemoteCharacteristic *> *getCharacteristicsByHandle(); // Get the characteristics map.
void getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic *> **pCharacteristicMap); void getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic *> **pCharacteristicMap);
...@@ -66,7 +66,7 @@ private: ...@@ -66,7 +66,7 @@ private:
// Properties // Properties
// We maintain a map of characteristics owned by this service keyed by a string representation of the UUID. // We maintain a map of characteristics owned by this service keyed by a string representation of the UUID.
std::map<String, BLERemoteCharacteristic *> m_characteristicMap; std::map<std::string, BLERemoteCharacteristic *> m_characteristicMap;
// We maintain a map of characteristics owned by this service keyed by a handle. // We maintain a map of characteristics owned by this service keyed by a handle.
std::map<uint16_t, BLERemoteCharacteristic *> m_characteristicMapByHandle; std::map<uint16_t, BLERemoteCharacteristic *> m_characteristicMapByHandle;
......
...@@ -99,7 +99,7 @@ void BLEScan::handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_ ...@@ -99,7 +99,7 @@ void BLEScan::handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_
bool shouldDelete = true; bool shouldDelete = true;
if (!m_wantDuplicates) { if (!m_wantDuplicates) {
if (m_scanResults.m_vectorAdvertisedDevices.count(advertisedAddress.toString()) != 0) { if (m_scanResults.m_vectorAdvertisedDevices.count(advertisedAddress.toString().c_str()) != 0) {
found = true; found = true;
} }
...@@ -130,7 +130,8 @@ void BLEScan::handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_ ...@@ -130,7 +130,8 @@ void BLEScan::handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_
m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice); m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice);
} }
if (!m_wantDuplicates && !found) { // if no callback and not want duplicate, and not already in vector, record it if (!m_wantDuplicates && !found) { // if no callback and not want duplicate, and not already in vector, record it
m_scanResults.m_vectorAdvertisedDevices.insert(std::pair<String, BLEAdvertisedDevice *>(advertisedAddress.toString(), advertisedDevice)); m_scanResults.m_vectorAdvertisedDevices.insert(std::pair<std::string, BLEAdvertisedDevice *>(advertisedAddress.toString().c_str(), advertisedDevice)
);
shouldDelete = false; shouldDelete = false;
} }
if (shouldDelete) { if (shouldDelete) {
...@@ -443,8 +444,8 @@ void BLEScan::stop() { ...@@ -443,8 +444,8 @@ void BLEScan::stop() {
// delete peer device from cache after disconnecting, it is required in case we are connecting to devices with not public address // delete peer device from cache after disconnecting, it is required in case we are connecting to devices with not public address
void BLEScan::erase(BLEAddress address) { void BLEScan::erase(BLEAddress address) {
log_i("erase device: %s", address.toString().c_str()); log_i("erase device: %s", address.toString().c_str());
BLEAdvertisedDevice *advertisedDevice = m_scanResults.m_vectorAdvertisedDevices.find(address.toString())->second; BLEAdvertisedDevice *advertisedDevice = m_scanResults.m_vectorAdvertisedDevices.find(address.toString().c_str())->second;
m_scanResults.m_vectorAdvertisedDevices.erase(address.toString()); m_scanResults.m_vectorAdvertisedDevices.erase(address.toString().c_str());
delete advertisedDevice; delete advertisedDevice;
} }
......
...@@ -53,7 +53,7 @@ public: ...@@ -53,7 +53,7 @@ public:
private: private:
friend BLEScan; friend BLEScan;
std::map<String, BLEAdvertisedDevice *> m_vectorAdvertisedDevices; std::map<std::string, BLEAdvertisedDevice *> m_vectorAdvertisedDevices;
}; };
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment