Skip to content

Commit

Permalink
Updates for #282
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolban committed Dec 21, 2017
1 parent 8329174 commit bcc7f69
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 31 deletions.
69 changes: 42 additions & 27 deletions cpp_utils/BLEAdvertisedDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ BLEAdvertisedDevice::BLEAdvertisedDevice() {
m_manufacturerData = "";
m_name = "";
m_rssi = -9999;
m_serviceData = "";
m_txPower = 0;
m_pScan = nullptr;

m_haveAppearance = false;
m_haveManufacturerData = false;
m_haveName = false;
m_haveRSSI = false;
m_haveServiceData = false;
m_haveServiceUUID = false;
m_haveTXPower = false;

Expand Down Expand Up @@ -65,7 +67,7 @@ BLEAddress BLEAdvertisedDevice::getAddress() {
*/
uint16_t BLEAdvertisedDevice::getAppearance() {
return m_appearance;
}
} // getAppearance


/**
Expand All @@ -74,7 +76,7 @@ uint16_t BLEAdvertisedDevice::getAppearance() {
*/
std::string BLEAdvertisedDevice::getManufacturerData() {
return m_manufacturerData;
}
} // getManufacturerData


/**
Expand Down Expand Up @@ -104,6 +106,15 @@ BLEScan* BLEAdvertisedDevice::getScan() {
} // getScan


/**
* @brief Get the service data.
* @return The ServiceData of the advertised device.
*/
std::string BLEAdvertisedDevice::getServiceData() {
return m_serviceData;
} //getServiceData


/**
* @brief Get the Service UUID.
* @return The Service UUID of the advertised device.
Expand Down Expand Up @@ -132,13 +143,7 @@ int8_t BLEAdvertisedDevice::getTXPower() {
return m_txPower;
} // getTXPower

/**
* @brief Get the service data.
* @return The ServiceData of the advertised device.
*/
uint8_t* BLEAdvertisedDevice::getServiceData() {
return m_serviceData;
} //getServiceData


/**
* @brief Does this advertisement have an appearance value?
Expand Down Expand Up @@ -176,6 +181,15 @@ bool BLEAdvertisedDevice::haveRSSI() {
} // haveRSSI


/**
* @brief Does this advertisement have a service data value?
* @return True if there is a service data value present.
*/
bool BLEAdvertisedDevice::haveServiceData() {
return m_haveServiceData;
} // haveServiceData


/**
* @brief Does this advertisement have a service UUID value?
* @return True if there is a service UUID value present.
Expand Down Expand Up @@ -213,8 +227,8 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
bool finished = false;

while(!finished) {
length = *payload; // Retrieve the length of the record.
payload++; // Skip to type
length = *payload; // Retrieve the length of the record.
payload++; // Skip to type
sizeConsumed += 1 + length; // increase the size consumed.

if (length != 0) { // A length of 0 indicates that we have reached the end.
Expand All @@ -227,15 +241,13 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
ad_type, BLEUtils::advTypeToString(ad_type), length, pHex);
free(pHex);



switch(ad_type) {
case ESP_BLE_AD_TYPE_NAME_CMPL: { // Adv Data Type: 0x09
case ESP_BLE_AD_TYPE_NAME_CMPL: { // Adv Data Type: 0x09
setName(std::string(reinterpret_cast<char*>(payload), length));
break;
} // ESP_BLE_AD_TYPE_NAME_CMPL

case ESP_BLE_AD_TYPE_TX_PWR: { // Adv Data Type: 0x0A
case ESP_BLE_AD_TYPE_TX_PWR: { // Adv Data Type: 0x0A
setTXPower(*payload);
break;
} // ESP_BLE_AD_TYPE_TX_PWR
Expand All @@ -245,21 +257,21 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
break;
} // ESP_BLE_AD_TYPE_APPEARANCE

case ESP_BLE_AD_TYPE_FLAG: { // Adv Data Type: 0x01
case ESP_BLE_AD_TYPE_FLAG: { // Adv Data Type: 0x01
setAdFlag(*payload);
break;
} // ESP_BLE_AD_TYPE_FLAG

case ESP_BLE_AD_TYPE_16SRV_CMPL:
case ESP_BLE_AD_TYPE_16SRV_PART: { // Adv Data Type: 0x02
case ESP_BLE_AD_TYPE_16SRV_PART: { // Adv Data Type: 0x02
for (int var = 0; var < length/2; ++var) {
setServiceUUID(BLEUUID(*reinterpret_cast<uint16_t*>(payload+var*2)));
}
break;
} // ESP_BLE_AD_TYPE_16SRV_PART

case ESP_BLE_AD_TYPE_32SRV_CMPL:
case ESP_BLE_AD_TYPE_32SRV_PART: { // Adv Data Type: 0x04
case ESP_BLE_AD_TYPE_32SRV_PART: { // Adv Data Type: 0x04
for (int var = 0; var < length/4; ++var) {
setServiceUUID(BLEUUID(*reinterpret_cast<uint32_t*>(payload+var*4)));
}
Expand All @@ -282,8 +294,8 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
break;
} // ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE

case ESP_BLE_AD_TYPE_SERVICE_DATA: {
setServiceData(payload);
case ESP_BLE_AD_TYPE_SERVICE_DATA: { // Adv Data Type: 0x16 (Service Data)
setServiceData(std::string(reinterpret_cast<char*>(payload), length));
break;
} //ESP_BLE_AD_TYPE_SERVICE_DATA

Expand Down Expand Up @@ -396,6 +408,16 @@ void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
} // setServiceUUID


/**
* @brief Set the ServiceData value.
* @param [in] data ServiceData value.
*/
void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
m_haveServiceData = true; // Set the flag that indicates we have service data.
m_serviceData = serviceData; // Save the service data that we received.
} //setServiceData


/**
* @brief Set the power level for this device.
* @param [in] txPower The discovered power level.
Expand All @@ -406,13 +428,6 @@ void BLEAdvertisedDevice::setTXPower(int8_t txPower) {
ESP_LOGD(LOG_TAG, "- txPower: %d", m_txPower);
} // setTXPower

/**
* @brief Set the ServiceData value.
* @param [in] data ServiceData value.
*/
void BLEAdvertisedDevice::setServiceData(uint8_t* data) {
m_serviceData = data;
} //setServiceData

/**
* @brief Create a string representation of this device.
Expand Down
11 changes: 7 additions & 4 deletions cpp_utils/BLEAdvertisedDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ class BLEAdvertisedDevice {
std::string getName();
int getRSSI();
BLEScan* getScan();
std::string getServiceData();
BLEUUID getServiceUUID();
int8_t getTXPower();
uint8_t* getServiceData();

bool isAdvertisingService(BLEUUID uuid);

bool isAdvertisingService(BLEUUID uuid);
bool haveAppearance();
bool haveManufacturerData();
bool haveName();
bool haveRSSI();
bool haveServiceData();
bool haveServiceUUID();
bool haveTXPower();

Expand All @@ -64,12 +66,13 @@ class BLEAdvertisedDevice {
void setServiceUUID(const char* serviceUUID);
void setServiceUUID(BLEUUID serviceUUID);
void setTXPower(int8_t txPower);
void setServiceData(uint8_t* data);
void setServiceData(std::string data);

bool m_haveAppearance;
bool m_haveManufacturerData;
bool m_haveName;
bool m_haveRSSI;
bool m_haveServiceData;
bool m_haveServiceUUID;
bool m_haveTXPower;

Expand All @@ -84,7 +87,7 @@ class BLEAdvertisedDevice {
int m_rssi;
std::vector<BLEUUID> m_serviceUUIDs;
int8_t m_txPower;
uint8_t* m_serviceData;
std::string m_serviceData;
};

/**
Expand Down

0 comments on commit bcc7f69

Please sign in to comment.