10000 More changes for #282 · ViaMonkey/esp32-snippets@95a20eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 95a20eb

Browse files
committed
More changes for nkolban#282
1 parent bcc7f69 commit 95a20eb

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

cpp_utils/BLEAdvertisedDevice.cpp

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ std::string BLEAdvertisedDevice::getServiceData() {
115115
} //getServiceData
116116

117117

118+
/**
119+
* @brief Get the service data UUID.
120+
* @return The service data UUID.
121+
*/
122+
BLEUUID BLEAdvertisedDevice::getServiceDataUUID() {
123+
return m_serviceDataUUID;
124+
} // getServiceDataUUID
125+
126+
118127
/**
119128
* @brief Get the Service UUID.
120129
* @return The Service UUID of the advertised device.
@@ -294,11 +303,45 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
294303
break;
295304
} // ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE
296305

297-
case ESP_BLE_AD_TYPE_SERVICE_DATA: { // Adv Data Type: 0x16 (Service Data)
298-
setServiceData(std::string(reinterpret_cast<char*>(payload), length));
306+
case ESP_BLE_AD_TYPE_SERVICE_DATA: { // Adv Data Type: 0x16 (Service Data) - 2 byte UUID
307+
if (length < 2) {
308+
ESP_LOGE(LOG_TAG, "Length too small for ESP_BLE_AD_TYPE_SERVICE_DATA");
309+
break;
310+
}
311+
uint16_t uuid = *(uint16_t *)payload;
312+
setServiceDataUUID(BLEUUID(uuid));
313+
if (length > 2) {
314+
setServiceData(std::string(reinterpret_cast<char*>(payload+2), length-2));
315+
}
299316
break;
300317
} //ESP_BLE_AD_TYPE_SERVICE_DATA
301318

319+
case ESP_BLE_AD_TYPE_32SERVICE_DATA: { // Adv Data Type: 0x20 (Service Data) - 4 byte UUID
320+
if (length < 4) {
321+
ESP_LOGE(LOG_TAG, "Length too small for ESP_BLE_AD_TYPE_32SERVICE_DATA");
322+
break;
323+
}
324+
uint32_t uuid = *(uint32_t *)payload;
325+
setServiceDataUUID(BLEUUID(uuid));
326+
if (length > 4) {
327+
setServiceData(std::string(reinterpret_cast<char*>(payload+4), length-4));
328+
}
329+
break;
330+
} //ESP_BLE_AD_TYPE_32SERVICE_DATA
331+
332+
case ESP_BLE_AD_TYPE_128SERVICE_DATA: { // Adv Data Type: 0x21 (Service Data) - 16 byte UUID
333+
if (length < 16) {
334+
ESP_LOGE(LOG_TAG, "Length too small for ESP_BLE_AD_TYPE_128SERVICE_DATA");
335+
break;
336+
}
337+
338+
setServiceDataUUID(BLEUUID(payload, (size_t)16, false));
339+
if (length > 16) {
340+
setServiceData(std::string(reinterpret_cast<char*>(payload+16), length-16));
341+
}
342+
break;
343+
} //ESP_BLE_AD_TYPE_32SERVICE_DATA
344+
302345
default: {
303346
ESP_LOGD(LOG_TAG, "Unhandled type: adType: %d - 0x%.2x", ad_type, ad_type);
304347
break;
@@ -418,6 +461,16 @@ void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
418461
} //setServiceData
419462

420463

464+
/**
465+
* @brief Set the ServiceDataUUID value.
466+
* @param [in] data ServiceDataUUID value.
467+
*/
468+
void BLEAdvertisedDevice::setServiceDataUUID(BLEUUID uuid) {
469+
m_haveServiceData = true; // Set the flag that indicates we have service data.
470+
m_serviceDataUUID = uuid;
471+
} // setServiceDataUUID
472+
473+
421474
/**
422475
* @brief Set the power level for this device.
423476
* @param [in] txPower The discovered power level.
@@ -453,5 +506,8 @@ std::string BLEAdvertisedDevice::toString() {
453506
return ss.str();
454507
} // toString
455508

509+
510+
511+
456512
#endif /* CONFIG_BT_ENABLED */
457513

cpp_utils/BLEAdvertisedDevice.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class BLEAdvertisedDevice {
3636
int getRSSI();
3737
BLEScan* getScan();
3838
std::string getServiceData();
39+
BLEUUID getServiceDataUUID();
3940
BLEUUID getServiceUUID();
4041
int8_t getTXPower();
4142

@@ -63,10 +64,12 @@ class BLEAdvertisedDevice {
6364
void setName(std::string name);
6465
void setRSSI(int rssi);
6566
void setScan(BLEScan* pScan);
67+
void setServiceData(std::string data);
68+
void setServiceDataUUID(BLEUUID uuid);
6669
void setServiceUUID(const char* serviceUUID);
6770
void setServiceUUID(BLEUUID serviceUUID);
6871
void setTXPower(int8_t txPower);
69-
void setServiceData(std::string data);
72+
7073

7174
bool m_haveAppearance;
7275
bool m_haveManufacturerData;
@@ -88,6 +91,7 @@ class BLEAdvertisedDevice {
8891
std::vector<BLEUUID> m_serviceUUIDs;
8992
int8_t m_txPower;
9093
std::string m_serviceData;
94+
BLEUUID m_serviceDataUUID;
9195
};
9296

9397
/**

0 commit comments

Comments
 (0)
0