Skip to content

Commit

Permalink
Reset advertisment vectors when changing their data.
Browse files Browse the repository at this point in the history
* Add method to erase all service UUIDS.
  • Loading branch information
h2zero committed Jun 15, 2024
1 parent 6934e87 commit 86b538b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/NimBLEAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void NimBLEAdvertising::addServiceUUID(const char* serviceUUID) {


/**
* @brief Add a service uuid to exposed list of services.
* @param [in] serviceUUID The UUID of the service to expose.
* @brief Remove a service UUID from the advertisment.
* @param [in] serviceUUID The UUID of the service to remove.
*/
void NimBLEAdvertising::removeServiceUUID(const NimBLEUUID &serviceUUID) {
for(auto it = m_serviceUUIDs.begin(); it != m_serviceUUIDs.end(); ++it) {
Expand All @@ -110,6 +110,15 @@ void NimBLEAdvertising::removeServiceUUID(const NimBLEUUID &serviceUUID) {
} // addServiceUUID


/**
* @brief Remove all service UUIDs from the advertisment.
*/
void NimBLEAdvertising::removeServices() {
std::vector<NimBLEUUID>().swap(m_serviceUUIDs);
m_advDataSet = false;
} // removeServices


/**
* @brief Set the device appearance in the advertising data.
* @param [in] appearance The appearance of the device in the advertising data.
Expand All @@ -135,7 +144,7 @@ void NimBLEAdvertising::addTxPower() {
* @param [in] name The name to advertise.
*/
void NimBLEAdvertising::setName(const std::string &name) {
m_name.assign(name.begin(), name.end());
std::vector<uint8_t>(name.begin(), name.end()).swap(m_name);
m_advData.name = &m_name[0];
m_advData.name_len = m_name.size();
m_advDataSet = false;
Expand All @@ -147,7 +156,7 @@ void NimBLEAdvertising::setName(const std::string &name) {
* @param [in] data The data to advertise.
*/
void NimBLEAdvertising::setManufacturerData(const std::string &data) {
m_mfgData.assign(data.begin(), data.end());
std::vector<uint8_t>(data.begin(), data.end()).swap(m_mfgData);
m_advData.mfg_data = &m_mfgData[0];
m_advData.mfg_data_len = m_mfgData.size();
m_advDataSet = false;
Expand All @@ -171,7 +180,7 @@ void NimBLEAdvertising::setManufacturerData(const std::vector<uint8_t> &data) {
* @param [in] uri The URI to advertise.
*/
void NimBLEAdvertising::setURI(const std::string &uri) {
m_uri.assign(uri.begin(), uri.end());
std::vector<uint8_t>(uri.begin(), uri.end()).swap(m_uri);
m_advData.uri = &m_uri[0];
m_advData.uri_len = m_uri.size();
m_advDataSet = false;
Expand All @@ -187,23 +196,26 @@ void NimBLEAdvertising::setURI(const std::string &uri) {
void NimBLEAdvertising::setServiceData(const NimBLEUUID &uuid, const std::string &data) {
switch (uuid.bitSize()) {
case 16: {
m_svcData16.assign((uint8_t*)&uuid.getNative()->u16.value, (uint8_t*)&uuid.getNative()->u16.value + 2);
std::vector<uint8_t>((uint8_t*)&uuid.getNative()->u16.value,
(uint8_t*)&uuid.getNative()->u16.value + 2).swap(m_svcData16);
m_svcData16.insert(m_svcData16.end(), data.begin(), data.end());
m_advData.svc_data_uuid16 = (uint8_t*)&m_svcData16[0];
m_advData.svc_data_uuid16_len = (data.length() > 0) ? m_svcData16.size() : 0;
break;
}

case 32: {
m_svcData32.assign((uint8_t*)&uuid.getNative()->u32.value, (uint8_t*)&uuid.getNative()->u32.value + 4);
std::vector<uint8_t>((uint8_t*)&uuid.getNative()->u32.value,
(uint8_t*)&uuid.getNative()->u32.value + 4).swap(m_svcData32);
m_svcData32.insert(m_svcData32.end(), data.begin(), data.end());
m_advData.svc_data_uuid32 = (uint8_t*)&m_svcData32[0];
m_advData.svc_data_uuid32_len = (data.length() > 0) ? m_svcData32.size() : 0;
break;
}

case 128: {
m_svcData128.assign(uuid.getNative()->u128.value, uuid.getNative()->u128.value + 16);
std::vector<uint8_t>(uuid.getNative()->u128.value,
uuid.getNative()->u128.value + 16).swap(m_svcData128);
m_svcData128.insert(m_svcData128.end(), data.begin(), data.end());
m_advData.svc_data_uuid128 = (uint8_t*)&m_svcData128[0];
m_advData.svc_data_uuid128_len = (data.length() > 0) ? m_svcData128.size() : 0;
Expand Down
1 change: 1 addition & 0 deletions src/NimBLEAdvertising.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class NimBLEAdvertising {
void addServiceUUID(const char* serviceUUID);
void removeServiceUUID(const NimBLEUUID &serviceUUID);
bool start(uint32_t duration = 0, advCompleteCB_t advCompleteCB = nullptr, NimBLEAddress* dirAddr = nullptr);
void removeServices();
bool stop();
void setAppearance(uint16_t appearance);
void setName(const std::string &name);
Expand Down

0 comments on commit 86b538b

Please sign in to comment.