Skip to content

Commit

Permalink
fix(BLE): Arduino String shall not be used within std::map<> (#9875)
Browse files Browse the repository at this point in the history
* fix(BLE): std::map()

* Update libraries/BLE/src/BLERemoteService.cpp

* ci(pre-commit): Apply automatic fixes

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
SuGlider and pre-commit-ci-lite[bot] committed Jun 17, 2024
1 parent c43187a commit 32def87
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
6 changes: 3 additions & 3 deletions libraries/BLE/src/BLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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<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));
break;
} // ESP_GATTC_SEARCH_RES_EVT
Expand Down Expand Up @@ -428,7 +428,7 @@ BLERemoteService *BLEClient::getService(BLEUUID uuid) {
if (!m_haveServices) {
getServices();
}
String uuidStr = uuid.toString();
std::string uuidStr = uuid.toString().c_str();
for (auto &myPair : m_servicesMap) {
if (myPair.first == uuidStr) {
log_v("<< getService: found the service with uuid: %s", uuid.toString().c_str());
Expand All @@ -445,7 +445,7 @@ BLERemoteService *BLEClient::getService(BLEUUID uuid) {
* services and wait until we have received them all.
* @return N/A
*/
std::map<String, BLERemoteService *> *BLEClient::getServices() {
std::map<std::string, BLERemoteService *> *BLEClient::getServices() {
/*
* Design
* ------
Expand Down
4 changes: 2 additions & 2 deletions libraries/BLE/src/BLEClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BLEClient {
void disconnect(); // Disconnect from the remote BLE Server
BLEAddress getPeerAddress(); // Get the address 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(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.
Expand Down Expand Up @@ -82,7 +82,7 @@ class BLEClient {
FreeRTOS::Semaphore m_semaphoreOpenEvt = FreeRTOS::Semaphore("OpenEvt");
FreeRTOS::Semaphore m_semaphoreSearchCmplEvt = FreeRTOS::Semaphore("SearchCmplEvt");
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;
void clearServices(); // Clear any existing services.
uint16_t m_mtu = 23;
Expand Down
6 changes: 3 additions & 3 deletions libraries/BLE/src/BLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
// 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);

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++;
} // while true
Expand All @@ -308,7 +308,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
/**
* @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;
} // getDescriptors

Expand All @@ -329,7 +329,7 @@ uint16_t BLERemoteCharacteristic::getHandle() {
*/
BLERemoteDescriptor *BLERemoteCharacteristic::getDescriptor(BLEUUID uuid) {
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) {
if (myPair.first == v) {
log_v("<< getDescriptor: found");
Expand Down
4 changes: 2 additions & 2 deletions libraries/BLE/src/BLERemoteCharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BLERemoteCharacteristic {
bool canWrite();
bool canWriteNoResponse();
BLERemoteDescriptor *getDescriptor(BLEUUID uuid);
std::map<String, BLERemoteDescriptor *> *getDescriptors();
std::map<std::string, BLERemoteDescriptor *> *getDescriptors();
BLERemoteService *getRemoteService();
uint16_t getHandle();
BLEUUID getUUID();
Expand Down Expand Up @@ -82,7 +82,7 @@ class BLERemoteCharacteristic {
notify_callback m_notifyCallback;

// 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

#endif /* CONFIG_BLUEDROID_ENABLED */
Expand Down
12 changes: 7 additions & 5 deletions libraries/BLE/src/BLERemoteService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
// by this service so remember it.
m_characteristicMap.insert(std::pair<String, BLERemoteCharacteristic*>(
BLEUUID(evtParam->get_char.char_id.uuid).toString(),
m_characteristicMap.insert(std::pair<std::string, BLERemoteCharacteristic*>(
BLEUUID(evtParam->get_char.char_id.uuid).toString().c_str(),
new BLERemoteCharacteristic(evtParam->get_char.char_id, evtParam->get_char.char_prop, this) ));
Expand Down Expand Up @@ -134,7 +134,7 @@ BLERemoteCharacteristic *BLERemoteService::getCharacteristic(BLEUUID uuid) {
if (!m_haveCharacteristics) {
retrieveCharacteristics();
}
String v = uuid.toString();
std::string v = uuid.toString().c_str();
for (auto &myPair : m_characteristicMap) {
if (myPair.first == v) {
return myPair.second;
Expand Down Expand Up @@ -179,7 +179,9 @@ void BLERemoteService::retrieveCharacteristics() {
// 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);

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));
offset++; // Increment our count of number of descriptors found.
} // Loop forever (until we break inside the loop).
Expand All @@ -192,7 +194,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<String, BLERemoteCharacteristic *> *BLERemoteService::getCharacteristics() {
std::map<std::string, BLERemoteCharacteristic *> *BLERemoteService::getCharacteristics() {
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
// now. The request to retrieve the characteristics by calling "retrieveCharacteristics" is a blocking
Expand Down
4 changes: 2 additions & 2 deletions libraries/BLE/src/BLERemoteService.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BLERemoteService {
BLERemoteCharacteristic *getCharacteristic(const char *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.
std::map<String, BLERemoteCharacteristic *> *getCharacteristics();
std::map<std::string, BLERemoteCharacteristic *> *getCharacteristics();
std::map<uint16_t, BLERemoteCharacteristic *> *getCharacteristicsByHandle(); // Get the characteristics map.
void getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic *> **pCharacteristicMap);

Expand Down Expand Up @@ -66,7 +66,7 @@ class BLERemoteService {
// Properties

// 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.
std::map<uint16_t, BLERemoteCharacteristic *> m_characteristicMapByHandle;
Expand Down
9 changes: 5 additions & 4 deletions libraries/BLE/src/BLEScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void BLEScan::handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_
bool shouldDelete = true;

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;
}

Expand Down Expand Up @@ -130,7 +130,8 @@ void BLEScan::handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_
m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice);
}
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;
}
if (shouldDelete) {
Expand Down Expand Up @@ -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
void BLEScan::erase(BLEAddress address) {
log_i("erase device: %s", address.toString().c_str());
BLEAdvertisedDevice *advertisedDevice = m_scanResults.m_vectorAdvertisedDevices.find(address.toString())->second;
m_scanResults.m_vectorAdvertisedDevices.erase(address.toString());
BLEAdvertisedDevice *advertisedDevice = m_scanResults.m_vectorAdvertisedDevices.find(address.toString().c_str())->second;
m_scanResults.m_vectorAdvertisedDevices.erase(address.toString().c_str());
delete advertisedDevice;
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/BLE/src/BLEScan.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class BLEScanResults {

private:
friend BLEScan;
std::map<String, BLEAdvertisedDevice *> m_vectorAdvertisedDevices;
std::map<std::string, BLEAdvertisedDevice *> m_vectorAdvertisedDevices;
};

/**
Expand Down

0 comments on commit 32def87

Please sign in to comment.