8000 Features, bugfixes and changes by chegewara · Pull Request #729 · nkolban/esp32-snippets · GitHub
[go: up one dir, main page]

Skip to content

Features, bugfixes and changes #729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions cpp_utils/BLEAdvertisedDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
uint8_t ad_type;
uint8_t sizeConsumed = 0;
bool finished = false;
setPayload(payload);
m_payload = payload;
m_payloadLength = total_len;

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


if (sizeConsumed >= total_len || length == 0) {
if (sizeConsumed >= total_len)
finished = true;
}

} // !finished
} // parseAdvertisement

Expand Down Expand Up @@ -510,10 +511,6 @@ uint8_t* BLEAdvertisedDevice::getPayload() {
return m_payload;
}

void BLEAdvertisedDevice::setPayload(uint8_t* payload) {
m_payload = payload;
}

esp_ble_addr_type_t BLEAdvertisedDevice::getAddressType() {
return m_addressType;
}
Expand All @@ -522,6 +519,9 @@ void BLEAdvertisedDevice::setAddressType(esp_ble_addr_type_t type) {
m_addressType = type;
}

size_t BLEAdvertisedDevice::getPayloadLength() {
return m_payloadLength;
}

#endif /* CONFIG_BT_ENABLED */

3 changes: 2 additions & 1 deletion cpp_utils/BLEAdvertisedDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BLEAdvertisedDevice {
BLEUUID getServiceUUID();
int8_t getTXPower();
uint8_t* getPayload();
size_t getPayloadLength();
esp_ble_addr_type_t getAddressType();
void setAddressType(esp_ble_addr_type_t type);

Expand Down Expand Up @@ -72,7 +73,6 @@ class BLEAdvertisedDevice {
void setServiceUUID(const char* serviceUUID);
void setServiceUUID(BLEUUID serviceUUID);
void setTXPower(int8_t txPower);
void setPayload(uint8_t* payload);

bool m_haveAppearance;
bool m_haveManufacturerData;
Expand All @@ -96,6 +96,7 @@ class BLEAdvertisedDevice {
std::string m_serviceData;
BLEUUID m_serviceDataUUID;
uint8_t* m_payload;
size_t m_payloadLength = 0;
esp_ble_addr_type_t m_addressType;
};

Expand Down
49 changes: 46 additions & 3 deletions cpp_utils/BLEAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ BLEAdvertising::BLEAdvertising() {
m_advParams.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
m_advParams.channel_map = ADV_CHNL_ALL;
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
m_advParams.peer_addr_type = BLE_ADDR_TYPE_PUBLIC;

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

void BLEAdvertising::setMinInterval(uint16_t mininterval) {
m_advData.min_interval = mininterval;
m_advParams.adv_int_min = mininterval;
} // setMinInterval

void BLEAdvertising::setMaxInterval(uint16_t maxinterval) {
m_advData.max_interval = maxinterval;
m_advParams.adv_int_max = maxinterval;
} // setMaxInterval

void BLEAdvertising::setMinPreferred(uint16_t mininterval) {
m_advData.min_interval = mininterval;
} //

void BLEAdvertising::setMaxPreferred(uint16_t maxinterval) {
m_advData.max_interval = maxinterval;
} //

void BLEAdvertising::setScanResponse(bool set) {
m_scanResp = set;
}

/**
* @brief Set the filtering for the scan filter.
Expand Down Expand Up @@ -198,15 +208,19 @@ void BLEAdvertising::start() {
if (!m_customAdvData) {
// Set the configuration for advertising.
m_advData.set_scan_rsp = false;
m_advData.include_name = !m_scanResp;
m_advData.include_txpower = !m_scanResp;
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
}

if (!m_customScanResponseData) {
if (!m_customScanResponseData && m_scanResp) {
m_advData.set_scan_rsp = true;
m_advData.include_name = m_scanResp;
m_advData.include_txpower = m_scanResp;
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
Expand Down Expand Up @@ -456,5 +470,34 @@ std::string BLEAdvertisementData::getPayload() {
return m_payload;
} // getPayload

void BLEAdvertising::handleGAPEvent(
esp_gap_ble_cb_event_t event,
esp_ble_gap_cb_param_t* param) {

ESP_LOGD(LOG_TAG, "handleGAPEvent [event no: %d]", (int)event);

switch(event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: {
// m_semaphoreSetAdv.give();
break;
}
case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT: {
// m_semaphoreSetAdv.give();
break;
}
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: {
// m_semaphoreSetAdv.give();
break;
}
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: {
ESP_LOGI(LOG_TAG, "STOP advertising");
start();
break;
}
default:
break;
}
}


#endif /* CONFIG_BT_ENABLED */
17 changes: 14 additions & 3 deletions cpp_utils/BLEAdvertising.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
#include <esp_gap_ble_api.h>
#include "BLEUUID.h"
#include <vector>
#include "FreeRTOS.h"

/**
* @brief Advertisement data set by the programmer to be published by the %BLE server.
*/
class BLEAdvertisementData {
// Only a subset of the possible BLE architected advertisement fields are currently exposed. Others will
// be exposed on demand/request or as time permits.
//
public:
void setAppearance(uint16_t appearance);
void setCompleteServices(BLEUUID uuid);
Expand Down Expand Up @@ -55,13 +57,22 @@ class BLEAdvertising {
void setAdvertisementData(BLEAdvertisementData& advertisementData);
void setScanFilter(bool scanRequertWhitelistOnly, bool connectWhitelistOnly);
void setScanResponseData(BLEAdvertisementData& advertisementData);
void setPrivateAddress(esp_ble_addr_type_t type = BLE_ADDR_TYPE_RANDOM);

void handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t* param);
void setMinPreferred(uint16_t);
void setMaxPreferred(uint16_t);
void setScanResponse(bool);

private:
esp_ble_adv_data_t m_advData;
esp_ble_adv_params_t m_advParams;
std::vector<BLEUUID> m_serviceUUIDs;
bool m_customAdvData; // Are we using custom advertising data?
bool m_customScanResponseData; // Are we using custom scan response data?
bool m_customAdvData = false; // Are we using custom advertising data?
bool m_customScanResponseData = false; // Are we using custom scan response data?
FreeRTOS::Semaphore m_semaphoreSetAdv = FreeRTOS::Semaphore("startAdvert");
bool m_scanResp = true;

};
#endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_CPP_UTILS_BLEADVERTISING_H_ */
#endif /* COMPONENTS_CPP_UTILS_BLEADVERTISING_H_ */
Loading
0