Skip to content

Commit 45fcc07

Browse files
committed
2 parents ba964d5 + c8dea9e commit 45fcc07

26 files changed

+813
-610
lines changed

cpp_utils/BLEAdvertisedDevice.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
233233
uint8_t ad_type;
234234
uint8_t sizeConsumed = 0;
235235
bool finished = false;
236-
setPayload(payload);
236+
m_payload = payload;
237+
m_payloadLength = total_len;
237238

238239
while(!finished) {
239240
length = *payload; // Retrieve the length of the record.
@@ -351,9 +352,9 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
351352
} // Length <> 0
352353

353354

354-
if (sizeConsumed >= total_len || length == 0) {
355+
if (sizeConsumed >= total_len)
355356
finished = true;
356-
}
357+
357358
} // !finished
358359
} // parseAdvertisement
359360

@@ -510,10 +511,6 @@ uint8_t* BLEAdvertisedDevice::getPayload() {
510511
return m_payload;
511512
}
512513

513-
void BLEAdvertisedDevice::setPayload(uint8_t* payload) {
514-
m_payload = payload;
515-
}
516-
517514
esp_ble_addr_type_t BLEAdvertisedDevice::getAddressType() {
518515
return m_addressType;
519516
}
@@ -522,6 +519,9 @@ void BLEAdvertisedDevice::setAddressType(esp_ble_addr_type_t type) {
522519
m_addressType = type;
523520
}
524521

522+
size_t BLEAdvertisedDevice::getPayloadLength() {
523+
return m_payloadLength;
524+
}
525525

526526
#endif /* CONFIG_BT_ENABLED */
527527

cpp_utils/BLEAdvertisedDevice.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class BLEAdvertisedDevice {
4040
BLEUUID getServiceUUID();
4141
int8_t getTXPower();
4242
uint8_t* getPayload();
43+
size_t getPayloadLength();
4344
esp_ble_addr_type_t getAddressType();
4445
void setAddressType(esp_ble_addr_type_t type);
4546

@@ -72,7 +73,6 @@ class BLEAdvertisedDevice {
7273
void setServiceUUID(const char* serviceUUID);
7374
void setServiceUUID(BLEUUID serviceUUID);
7475
void setTXPower(int8_t txPower);
75-
void setPayload(uint8_t* payload);
7676

7777
bool m_haveAppearance;
7878
bool m_haveManufacturerData;
@@ -96,6 +96,7 @@ class BLEAdvertisedDevice {
9696
std::string m_serviceData;
9797
BLEUUID m_serviceDataUUID;
9898
uint8_t* m_payload;
99+
size_t m_payloadLength = 0;
99100
esp_ble_addr_type_t m_addressType;
100101
};
101102

cpp_utils/BLEAdvertising.cpp

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ BLEAdvertising::BLEAdvertising() {
5656
m_advParams.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
5757
m_advParams.channel_map = ADV_CHNL_ALL;
5858
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
59+
m_advParams.peer_addr_type = BLE_ADDR_TYPE_PUBLIC;
5960

6061
m_customAdvData = false; // No custom advertising data
6162
m_customScanResponseData = false; // No custom scan response data
@@ -92,15 +93,24 @@ void BLEAdvertising::setAppearance(uint16_t appearance) {
9293
} // setAppearance
9394

9495
void BLEAdvertising::setMinInterval(uint16_t mininterval) {
95-
m_advData.min_interval = mininterval;
9696
m_advParams.adv_int_min = mininterval;
9797
} // setMinInterval
9898

9999
void BLEAdvertising::setMaxInterval(uint16_t maxinterval) {
100-
m_advData.max_interval = maxinterval;
101100
m_advParams.adv_int_max = maxinterval;
102101
} // setMaxInterval
103102

103+
void BLEAdvertising::setMinPreferred(uint16_t mininterval) {
104+
m_advData.min_interval = mininterval;
105+
} //
106+
107+
void BLEAdvertising::setMaxPreferred(uint16_t maxinterval) {
108+
m_advData.max_interval = maxinterval;
109+
} //
110+
111+
void BLEAdvertising::setScanResponse(bool set) {
112+
m_scanResp = set;
113+
}
104114

105115
/**
106116
* @brief Set the filtering for the scan filter.
@@ -198,15 +208,19 @@ void BLEAdvertising::start() {
198208
if (!m_customAdvData) {
199209
// Set the configuration for advertising.
200210
m_advData.set_scan_rsp = false;
211+
m_advData.include_name = !m_scanResp;
212+
m_advData.include_txpower = !m_scanResp;
201213
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
202214
if (errRc != ESP_OK) {
203215
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
204216
return;
205217
}
206218
}
207219

208-
if (!m_customScanResponseData) {
220+
if (!m_customScanResponseData && m_scanResp) {
209221
m_advData.set_scan_rsp = true;
222+
m_advData.include_name = m_scanResp;
223+
m_advData.include_txpower = m_scanResp;
210224
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
211225
if (errRc != ESP_OK) {
212226
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
@@ -456,5 +470,34 @@ std::string BLEAdvertisementData::getPayload() {
456470
return m_payload;
457471
} // getPayload
458472

473+
void BLEAdvertising::handleGAPEvent(
474+
esp_gap_ble_cb_event_t event,
475+
esp_ble_gap_cb_param_t* param) {
476+
477+
ESP_LOGD(LOG_TAG, "handleGAPEvent [event no: %d]", (int)event);
478+
479+
switch(event) {
480+
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: {
481+
// m_semaphoreSetAdv.give();
482+
break;
483+
}
484+
case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT: {
485+
// m_semaphoreSetAdv.give();
486+
break;
487+
}
488+
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: {
489+
// m_semaphoreSetAdv.give();
490+
break;
491+
}
492+
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: {
493+
ESP_LOGI(LOG_TAG, "STOP advertising");
494+
start();
495+
break;
496+
}
497+
default:
498+
break;
499+
}
500+
}
501+
459502

460503
#endif /* CONFIG_BT_ENABLED */

cpp_utils/BLEAdvertising.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
#include <esp_gap_ble_api.h>
1313
#include "BLEUUID.h"
1414
#include <vector>
15+
#include "FreeRTOS.h"
1516

1617
/**
1718
* @brief Advertisement data set by the programmer to be published by the %BLE server.
1819
*/
1920
class BLEAdvertisementData {
2021
// Only a subset of the possible BLE architected advertisement fields are currently exposed. Others will
2122
// be exposed on demand/request or as time permits.
23+
//
2224
public:
2325
void setAppearance(uint16_t appearance);
2426
void setCompleteServices(BLEUUID uuid);
@@ -55,13 +57,22 @@ class BLEAdvertising {
5557
void setAdvertisementData(BLEAdvertisementData& advertisementData);
5658
void setScanFilter(bool scanRequertWhitelistOnly, bool connectWhitelistOnly);
5759
void setScanResponseData(BLEAdvertisementData& advertisementData);
60+
void setPrivateAddress(esp_ble_addr_type_t type = BLE_ADDR_TYPE_RANDOM);
61+
62+
void handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t* param);
63+
void setMinPreferred(uint16_t);
64+
void setMaxPreferred(uint16_t);
65+
void setScanResponse(bool);
5866

5967
private:
6068
esp_ble_adv_data_t m_advData;
6169
esp_ble_adv_params_t m_advParams;
6270
std::vector<BLEUUID> m_serviceUUIDs;
63-
bool m_customAdvData; // Are we using custom advertising data?
64-
bool m_customScanResponseData; // Are we using custom scan response data?
71+
bool m_customAdvData = false; // Are we using custom advertising data?
72+
bool m_customScanResponseData = false; // Are we using custom scan response data?
73+
FreeRTOS::Semaphore m_semaphoreSetAdv = FreeRTOS::Semaphore("startAdvert");
74+
bool m_scanResp = true;
75+
6576
};
6677
#endif /* CONFIG_BT_ENABLED */
67-
#endif /* COMPONENTS_CPP_UTILS_BLEADVERTISING_H_ */
78+
#endif /* COMPONENTS_CPP_UTILS_BLEADVERTISING_H_ */

0 commit comments

Comments
 (0)