Refactor BLEAdvertisedDevice (#4739)

fixes #4596

* Prevent possible undefined behaviour by get methods not taking an index as parameter
* Add methods to get the count of service data UUIDs and service UUIDs
* Various code improvements
This commit is contained in:
Markus Frey 2021-02-16 10:02:58 +01:00 committed by GitHub
parent 8134a42162
commit 7cdfb8bc7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 19 deletions

View File

@ -25,6 +25,7 @@ BLEAdvertisedDevice::BLEAdvertisedDevice() {
m_manufacturerData = ""; m_manufacturerData = "";
m_name = ""; m_name = "";
m_rssi = -9999; m_rssi = -9999;
m_serviceUUIDs = {};
m_serviceData = {}; m_serviceData = {};
m_serviceDataUUIDs = {}; m_serviceDataUUIDs = {};
m_txPower = 0; m_txPower = 0;
@ -34,8 +35,6 @@ BLEAdvertisedDevice::BLEAdvertisedDevice() {
m_haveManufacturerData = false; m_haveManufacturerData = false;
m_haveName = false; m_haveName = false;
m_haveRSSI = false; m_haveRSSI = false;
m_haveServiceData = false;
m_haveServiceUUID = false;
m_haveTXPower = false; m_haveTXPower = false;
} // BLEAdvertisedDevice } // BLEAdvertisedDevice
@ -107,11 +106,7 @@ BLEScan* BLEAdvertisedDevice::getScan() {
* @return Number of service data discovered. * @return Number of service data discovered.
*/ */
int BLEAdvertisedDevice::getServiceDataCount() { int BLEAdvertisedDevice::getServiceDataCount() {
if (m_haveServiceData)
return m_serviceData.size(); return m_serviceData.size();
else
return 0;
} //getServiceDataCount } //getServiceDataCount
/** /**
@ -119,7 +114,7 @@ int BLEAdvertisedDevice::getServiceDataCount() {
* @return The ServiceData of the advertised device. * @return The ServiceData of the advertised device.
*/ */
std::string BLEAdvertisedDevice::getServiceData() { std::string BLEAdvertisedDevice::getServiceData() {
return m_serviceData[0]; return m_serviceData.empty() ? std::string() : m_serviceData.front();
} //getServiceData } //getServiceData
/** /**
@ -130,12 +125,20 @@ std::string BLEAdvertisedDevice::getServiceData(int i) {
return m_serviceData[i]; return m_serviceData[i];
} //getServiceData } //getServiceData
/**
* @brief Get the number of service data UUIDs.
* @return Number of service data UUIDs discovered.
*/
int BLEAdvertisedDevice::getServiceDataUUIDCount() {
return m_serviceDataUUIDs.size();
} //getServiceDataUUIDCount
/** /**
* @brief Get the service data UUID. * @brief Get the service data UUID.
* @return The service data UUID. * @return The service data UUID.
*/ */
BLEUUID BLEAdvertisedDevice::getServiceDataUUID() { BLEUUID BLEAdvertisedDevice::getServiceDataUUID() {
return m_serviceDataUUIDs[0]; return m_serviceDataUUIDs.empty() ? BLEUUID() : m_serviceDataUUIDs.front();
} // getServiceDataUUID } // getServiceDataUUID
/** /**
@ -146,12 +149,20 @@ BLEUUID BLEAdvertisedDevice::getServiceDataUUID(int i) {
return m_serviceDataUUIDs[i]; return m_serviceDataUUIDs[i];
} // getServiceDataUUID } // getServiceDataUUID
/**
* @brief Get the number of service UUIDs.
* @return Number of service UUIDs discovered.
*/
int BLEAdvertisedDevice::getServiceUUIDCount() {
return m_serviceUUIDs.size();
} //getServiceUUIDCount
/** /**
* @brief Get the Service UUID. * @brief Get the Service UUID.
* @return The Service UUID of the advertised device. * @return The Service UUID of the advertised device.
*/ */
BLEUUID BLEAdvertisedDevice::getServiceUUID() { BLEUUID BLEAdvertisedDevice::getServiceUUID() {
return m_serviceUUIDs[0]; return m_serviceUUIDs.empty() ? BLEUUID() : m_serviceUUIDs.front();
} // getServiceUUID } // getServiceUUID
/** /**
@ -167,7 +178,7 @@ BLEUUID BLEAdvertisedDevice::getServiceUUID(int i) {
* @return Return true if service is advertised * @return Return true if service is advertised
*/ */
bool BLEAdvertisedDevice::isAdvertisingService(BLEUUID uuid){ bool BLEAdvertisedDevice::isAdvertisingService(BLEUUID uuid){
for (int i = 0; i < m_serviceUUIDs.size(); i++) { for (int i = 0; i < getServiceUUIDCount(); i++) {
if (m_serviceUUIDs[i].equals(uuid)) return true; if (m_serviceUUIDs[i].equals(uuid)) return true;
} }
return false; return false;
@ -224,7 +235,7 @@ bool BLEAdvertisedDevice::haveRSSI() {
* @return True if there is a service data value present. * @return True if there is a service data value present.
*/ */
bool BLEAdvertisedDevice::haveServiceData() { bool BLEAdvertisedDevice::haveServiceData() {
return m_haveServiceData; return !m_serviceData.empty();
} // haveServiceData } // haveServiceData
@ -233,7 +244,7 @@ bool BLEAdvertisedDevice::haveServiceData() {
* @return True if there is a service UUID value present. * @return True if there is a service UUID value present.
*/ */
bool BLEAdvertisedDevice::haveServiceUUID() { bool BLEAdvertisedDevice::haveServiceUUID() {
return m_haveServiceUUID; return !m_serviceUUIDs.empty();
} // haveServiceUUID } // haveServiceUUID
@ -486,7 +497,6 @@ void BLEAdvertisedDevice::setServiceUUID(const char* serviceUUID) {
*/ */
void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) { void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
m_serviceUUIDs.push_back(serviceUUID); m_serviceUUIDs.push_back(serviceUUID);
m_haveServiceUUID = true;
log_d("- addServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str()); log_d("- addServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str());
} // setServiceUUID } // setServiceUUID
@ -496,7 +506,6 @@ void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
* @param [in] data ServiceData value. * @param [in] data ServiceData value.
*/ */
void BLEAdvertisedDevice::setServiceData(std::string serviceData) { void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
m_haveServiceData = true; // Set the flag that indicates we have service data.
m_serviceData.push_back(serviceData); // Save the service data that we received. m_serviceData.push_back(serviceData); // Save the service data that we received.
} //setServiceData } //setServiceData
@ -506,7 +515,6 @@ void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
* @param [in] data ServiceDataUUID value. * @param [in] data ServiceDataUUID value.
*/ */
void BLEAdvertisedDevice::setServiceDataUUID(BLEUUID uuid) { void BLEAdvertisedDevice::setServiceDataUUID(BLEUUID uuid) {
m_haveServiceData = true; // Set the flag that indicates we have service data.
m_serviceDataUUIDs.push_back(uuid); m_serviceDataUUIDs.push_back(uuid);
log_d("- addServiceDataUUID(): serviceDataUUID: %s", uuid.toString().c_str()); log_d("- addServiceDataUUID(): serviceDataUUID: %s", uuid.toString().c_str());
} // setServiceDataUUID } // setServiceDataUUID
@ -542,7 +550,7 @@ std::string BLEAdvertisedDevice::toString() {
free(pHex); free(pHex);
} }
if (haveServiceUUID()) { if (haveServiceUUID()) {
for (int i=0; i < m_serviceUUIDs.size(); i++) { for (int i=0; i < getServiceUUIDCount(); i++) {
res += ", serviceUUID: " + getServiceUUID(i).toString(); res += ", serviceUUID: " + getServiceUUID(i).toString();
} }
} }

View File

@ -42,6 +42,8 @@ public:
BLEUUID getServiceUUID(); BLEUUID getServiceUUID();
BLEUUID getServiceUUID(int i); BLEUUID getServiceUUID(int i);
int getServiceDataCount(); int getServiceDataCount();
int getServiceDataUUIDCount();
int getServiceUUIDCount();
int8_t getTXPower(); int8_t getTXPower();
uint8_t* getPayload(); uint8_t* getPayload();
size_t getPayloadLength(); size_t getPayloadLength();
@ -83,8 +85,6 @@ private:
bool m_haveManufacturerData; bool m_haveManufacturerData;
bool m_haveName; bool m_haveName;
bool m_haveRSSI; bool m_haveRSSI;
bool m_haveServiceData;
bool m_haveServiceUUID;
bool m_haveTXPower; bool m_haveTXPower;