Skip to content

Commit 4a781e0

Browse files
committed
Updates 2017-11-21 0950
1 parent cb1ab4e commit 4a781e0

27 files changed

+751
-243
lines changed

src/BLEAdvertisedDevice.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,21 @@ BLEScan* BLEAdvertisedDevice::getScan() {
108108
* @brief Get the Service UUID.
109109
* @return The Service UUID of the advertised device.
110110
*/
111-
BLEUUID BLEAdvertisedDevice::getServiceUUID() {
112-
return m_serviceUUID;
111+
BLEUUID BLEAdvertisedDevice::getServiceUUID() { //TODO Remove it eventually, is no longer useful
112+
return m_serviceUUIDs[0];
113113
} // getServiceUUID
114114

115+
/**
116+
* @brief Check advertised serviced for existence required UUID
117+
* @return Return true if service is advertised
118+
*/
119+
bool BLEAdvertisedDevice::isAdvertisingService(BLEUUID uuid){
120+
for (int i = 0; i < m_serviceUUIDs.size(); ++i) {
121+
if(m_serviceUUIDs[i].equals(uuid))
122+
return true;
123+
}
124+
return false;
125+
}
115126

116127
/**
117128
* @brief Get the TX Power.
@@ -198,7 +209,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
198209
payload++; // Skip to type
199210
sizeConsumed += 1 + length; // increase the size consumed.
200211

201-
if (length != 0) { // A length of 0 indicate that we have reached the end.
212+
if (length != 0) { // A length of 0 indicates that we have reached the end.
202213
ad_type = *payload;
203214
payload++;
204215
length--;
@@ -231,23 +242,19 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
231242
break;
232243
} // ESP_BLE_AD_TYPE_FLAG
233244

234-
case ESP_BLE_AD_TYPE_16SRV_CMPL: { // Adv Data Type: 0x03
235-
setServiceUUID(BLEUUID(*reinterpret_cast<uint16_t*>(payload)));
236-
break;
237-
} // ESP_BLE_AD_TYPE_16SRV_CMPL
238-
245+
case ESP_BLE_AD_TYPE_16SRV_CMPL:
239246
case ESP_BLE_AD_TYPE_16SRV_PART: { // Adv Data Type: 0x02
240-
setServiceUUID(BLEUUID(*reinterpret_cast<uint16_t*>(payload)));
247+
for (int var = 0; var < length/2; ++var) {
248+
setServiceUUID(BLEUUID(*reinterpret_cast<uint16_t*>(payload+var*2)));
249+
}
241250
break;
242251
} // ESP_BLE_AD_TYPE_16SRV_PART
243252

244-
case ESP_BLE_AD_TYPE_32SRV_CMPL: { // Adv Data Type: 0x05
245-
setServiceUUID(BLEUUID(*reinterpret_cast<uint32_t*>(payload)));
246-
break;
247-
} // ESP_BLE_AD_TYPE_32SRV_CMPL
248-
253+
case ESP_BLE_AD_TYPE_32SRV_CMPL:
249254
case ESP_BLE_AD_TYPE_32SRV_PART: { // Adv Data Type: 0x04
250-
setServiceUUID(BLEUUID(*reinterpret_cast<uint32_t*>(payload)));
255+
for (int var = 0; var < length/4; ++var) {
256+
setServiceUUID(BLEUUID(*reinterpret_cast<uint32_t*>(payload+var*4)));
257+
}
251258
break;
252259
} // ESP_BLE_AD_TYPE_32SRV_PART
253260

@@ -355,23 +362,25 @@ void BLEAdvertisedDevice::setScan(BLEScan* pScan) {
355362
m_pScan = pScan;
356363
} // setScan
357364

365+
358366
/**
359367
* @brief Set the Service UUID for this device.
360368
* @param [in] serviceUUID The discovered serviceUUID
361369
*/
362370
void BLEAdvertisedDevice::setServiceUUID(const char* serviceUUID) {
363371
return setServiceUUID(BLEUUID(serviceUUID));
364-
} // setRSSI
372+
} // setServiceUUID
373+
365374

366375
/**
367376
* @brief Set the Service UUID for this device.
368377
* @param [in] serviceUUID The discovered serviceUUID
369378
*/
370379
void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
371-
m_serviceUUID = serviceUUID;
380+
m_serviceUUIDs.push_back(serviceUUID);
372381
m_haveServiceUUID = true;
373-
ESP_LOGD(LOG_TAG, "- setServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str());
374-
} // setRSSI
382+
ESP_LOGD(LOG_TAG, "- addServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str());
383+
} // setServiceUUID
375384

376385

377386
/**

src/BLEAdvertisedDevice.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class BLEAdvertisedDevice {
3838
BLEUUID getServiceUUID();
3939
int8_t getTXPower();
4040

41+
bool isAdvertisingService(BLEUUID uuid);
4142
bool haveAppearance();
4243
bool haveManufacturerData();
4344
bool haveName();
@@ -79,7 +80,7 @@ class BLEAdvertisedDevice {
7980
std::string m_name;
8081
BLEScan* m_pScan;
8182
int m_rssi;
82-
BLEUUID m_serviceUUID;
83+
std::vector<BLEUUID> m_serviceUUIDs;
8384
int8_t m_txPower;
8485
};
8586

src/BLECharacteristic.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ void BLECharacteristic::handleGATTServerEvent(
244244
// - esp_bt_uuid_t char_uuid
245245
case ESP_GATTS_ADD_CHAR_EVT: {
246246
if (getUUID().equals(BLEUUID(param->add_char.char_uuid)) &&
247+
getHandle() == param->add_char.attr_handle &&
247248
getService()->getHandle()==param->add_char.service_handle) {
248249
m_semaphoreCreateEvt.give();
249250
}
@@ -418,11 +419,8 @@ void BLECharacteristic::handleGATTServerEvent(
418419

419420
// Give each of the descriptors associated with this characteristic the opportunity to handle the
420421
// event.
421-
BLEDescriptor *pDescriptor = m_descriptorMap.getFirst();
422-
while(pDescriptor != nullptr) {
423-
pDescriptor->handleGATTServerEvent(event, gatts_if, param);
424-
pDescriptor = m_descriptorMap.getNext();
425-
}
422+
423+
m_descriptorMap.handleGATTServerEvent(event, gatts_if, param);
426424

427425
} // handleGATTServerEvent
428426

@@ -556,7 +554,9 @@ void BLECharacteristic::setBroadcastProperty(bool value) {
556554
* @param [in] pCallbacks An instance of a callbacks structure used to define any callbacks for the characteristic.
557555
*/
558556
void BLECharacteristic::setCallbacks(BLECharacteristicCallbacks* pCallbacks) {
557+
ESP_LOGD(LOG_TAG, ">> setCallbacks: 0x%x", (uint32_t)pCallbacks);
559558
m_pCallbacks = pCallbacks;
559+
ESP_LOGD(LOG_TAG, "<< setCallbacks");
560560
} // setCallbacks
561561

562562

@@ -695,4 +695,25 @@ std::string BLECharacteristic::toString() {
695695
return stringstream.str();
696696
} // toString
697697

698+
BLECharacteristicCallbacks::~BLECharacteristicCallbacks() {}
699+
700+
/**
701+
* @brief Callback function to support a read request.
702+
* @param [in] pCharacteristic The characteristic that is the source of the event.
703+
*/
704+
void BLECharacteristicCallbacks::onRead(BLECharacteristic *pCharacteristic) {
705+
ESP_LOGD("BLECharacteristicCallbacks", ">> onRead: default");
706+
ESP_LOGD("BLECharacteristicCallbacks", "<< onRead");
707+
} // onRead
708+
709+
710+
/**
711+
* @brief Callback function to support a write request.
712+
* @param [in] pCharacteristic The characteristic that is the source of the event.
713+
*/
714+
void BLECharacteristicCallbacks::onWrite(BLECharacteristic *pCharacteristic) {
715+
ESP_LOGD("BLECharacteristicCallbacks", ">> onWrite: default");
716+
ESP_LOGD("BLECharacteristicCallbacks", "<< onWrite");
717+
} // onWrite
718+
698719
#endif /* CONFIG_BT_ENABLED */

src/BLECharacteristic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class BLECharacteristic {
7777
void setWriteProperty(bool value);
7878
void setWriteNoResponseProperty(bool value);
7979
std::string toString();
80+
uint16_t getHandle();
8081

8182

8283
static const uint32_t PROPERTY_READ = 1<<0;
@@ -106,7 +107,6 @@ class BLECharacteristic {
106107
esp_ble_gatts_cb_param_t* param);
107108

108109
void executeCreate(BLEService* pService);
109-
uint16_t getHandle();
110110
esp_gatt_char_prop_t getProperties();
111111
BLEService* getService();
112112
void setHandle(uint16_t handle);

src/BLECharacteristicMap.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ BLECharacteristic* BLECharacteristicMap::getByUUID(const char* uuid) {
4141
*/
4242
BLECharacteristic* BLECharacteristicMap::getByUUID(BLEUUID uuid) {
4343
for (auto &myPair : m_uuidMap) {
44-
if (myPair.second->getUUID().equals(uuid)) {
45-
return myPair.second;
44+
if (myPair.first->getUUID().equals(uuid)) {
45+
return myPair.first;
4646
}
4747
}
4848
//return m_uuidMap.at(uuid.toString());
@@ -59,7 +59,7 @@ BLECharacteristic* BLECharacteristicMap::getFirst() {
5959
if (m_iterator == m_uuidMap.end()) {
6060
return nullptr;
6161
}
62-
BLECharacteristic* pRet = m_iterator->second;
62+
BLECharacteristic* pRet = m_iterator->first;
6363
m_iterator++;
6464
return pRet;
6565
} // getFirst
@@ -73,7 +73,7 @@ BLECharacteristic* BLECharacteristicMap::getNext() {
7373
if (m_iterator == m_uuidMap.end()) {
7474
return nullptr;
7575
}
76-
BLECharacteristic* pRet = m_iterator->second;
76+
BLECharacteristic* pRet = m_iterator->first;
7777
m_iterator++;
7878
return pRet;
7979
} // getNext
@@ -91,7 +91,7 @@ void BLECharacteristicMap::handleGATTServerEvent(
9191
esp_ble_gatts_cb_param_t* param) {
9292
// Invoke the handler for every Service we have.
9393
for (auto &myPair : m_uuidMap) {
94-
myPair.second->handleGATTServerEvent(event, gatts_if, param);
94+
myPair.first->handleGATTServerEvent(event, gatts_if, param);
9595
}
9696
} // handleGATTServerEvent
9797

@@ -115,9 +115,9 @@ void BLECharacteristicMap::setByHandle(uint16_t handle,
115115
* @return N/A.
116116
*/
117117
void BLECharacteristicMap::setByUUID(
118-
BLEUUID uuid,
119-
BLECharacteristic *pCharacteristic) {
120-
m_uuidMap.insert(std::pair<std::string, BLECharacteristic *>(uuid.toString(), pCharacteristic));
118+
BLECharacteristic *pCharacteristic,
119+
BLEUUID uuid) {
120+
m_uuidMap.insert(std::pair<BLECharacteristic *, std::string>(pCharacteristic, uuid.toString()));
121121
} // setByUUID
122122

123123

@@ -134,7 +134,7 @@ std::string BLECharacteristicMap::toString() {
134134
stringStream << "\n";
135135
}
136136
count++;
137-
stringStream << "handle: 0x" << std::setw(2) << myPair.second->getHandle() << ", uuid: " + myPair.second->getUUID().toString();
137+
stringStream << "handle: 0x" << std::setw(2) << myPair.first->getHandle() << ", uuid: " + myPair.first->getUUID().toString();
138138
}
139139
return stringStream.str();
140140
} // toString

0 commit comments

Comments
 (0)