2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLEAdvertising.cpp
|
|
|
|
*
|
|
|
|
* Created: on March 3, 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2020-03-30 01:44:20 +02:00
|
|
|
* Originally:
|
|
|
|
*
|
|
|
|
* BLEAdvertising.cpp
|
|
|
|
*
|
|
|
|
* This class encapsulates advertising a BLE Server.
|
|
|
|
* Created on: Jun 21, 2017
|
|
|
|
* Author: kolban
|
|
|
|
*
|
|
|
|
*/
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
2022-04-10 18:21:45 +02:00
|
|
|
#if (defined(CONFIG_BT_ENABLED) && \
|
|
|
|
defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && \
|
|
|
|
!CONFIG_BT_NIMBLE_EXT_ADV) || defined(_DOXYGEN_)
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_NIMBLE_CPP_IDF)
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "services/gap/ble_svc_gap.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#else
|
|
|
|
#include "nimble/nimble/host/services/gap/include/services/gap/ble_svc_gap.h"
|
|
|
|
#endif
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLEAdvertising.h"
|
|
|
|
#include "NimBLEDevice.h"
|
|
|
|
#include "NimBLEServer.h"
|
|
|
|
#include "NimBLEUtils.h"
|
|
|
|
#include "NimBLELog.h"
|
|
|
|
|
|
|
|
static const char* LOG_TAG = "NimBLEAdvertising";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Construct a default advertising object.
|
|
|
|
*/
|
2021-02-08 04:33:57 +01:00
|
|
|
NimBLEAdvertising::NimBLEAdvertising() {
|
|
|
|
reset();
|
|
|
|
} // NimBLEAdvertising
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Stops the current advertising and resets the advertising data to the default values.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::reset() {
|
2021-02-08 06:26:02 +01:00
|
|
|
if(NimBLEDevice::getInitialized() && isAdvertising()) {
|
|
|
|
stop();
|
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
memset(&m_advData, 0, sizeof m_advData);
|
2020-05-14 06:03:56 +02:00
|
|
|
memset(&m_scanData, 0, sizeof m_scanData);
|
|
|
|
memset(&m_advParams, 0, sizeof m_advParams);
|
2021-02-08 04:33:57 +01:00
|
|
|
memset(&m_slaveItvl, 0, sizeof m_slaveItvl);
|
2020-03-30 01:44:20 +02:00
|
|
|
const char *name = ble_svc_gap_device_name();
|
2020-04-14 03:13:51 +02:00
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
m_advData.name = (uint8_t *)name;
|
2020-03-30 01:44:20 +02:00
|
|
|
m_advData.name_len = strlen(name);
|
|
|
|
m_advData.name_is_complete = 1;
|
2020-10-24 15:55:37 +02:00
|
|
|
m_advData.tx_pwr_lvl = NimBLEDevice::getPower();
|
2020-03-30 01:44:20 +02:00
|
|
|
m_advData.flags = (BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-11-06 23:44:35 +01:00
|
|
|
#if !defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
|
|
|
m_advParams.conn_mode = BLE_GAP_CONN_MODE_NON;
|
|
|
|
#else
|
2020-05-14 06:03:56 +02:00
|
|
|
m_advParams.conn_mode = BLE_GAP_CONN_MODE_UND;
|
2020-11-06 23:44:35 +01:00
|
|
|
#endif
|
2020-03-30 01:44:20 +02:00
|
|
|
m_advParams.disc_mode = BLE_GAP_DISC_MODE_GEN;
|
2020-09-14 04:36:59 +02:00
|
|
|
m_customAdvData = false;
|
|
|
|
m_customScanResponseData = false;
|
|
|
|
m_scanResp = true;
|
|
|
|
m_advDataSet = false;
|
2021-01-14 02:21:23 +01:00
|
|
|
// Set this to non-zero to prevent auto start if host reset before started by app.
|
|
|
|
m_duration = BLE_HS_FOREVER;
|
2021-12-29 15:51:57 +01:00
|
|
|
m_advCompCB = nullptr;
|
2021-02-08 04:33:57 +01:00
|
|
|
} // reset
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add a service uuid to exposed list of services.
|
|
|
|
* @param [in] serviceUUID The UUID of the service to expose.
|
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
void NimBLEAdvertising::addServiceUUID(const NimBLEUUID &serviceUUID) {
|
2020-05-14 06:03:56 +02:00
|
|
|
m_serviceUUIDs.push_back(serviceUUID);
|
2020-07-14 05:24:07 +02:00
|
|
|
m_advDataSet = false;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // addServiceUUID
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add a service uuid to exposed list of services.
|
|
|
|
* @param [in] serviceUUID The string representation of the service to expose.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::addServiceUUID(const char* serviceUUID) {
|
2020-05-14 06:03:56 +02:00
|
|
|
addServiceUUID(NimBLEUUID(serviceUUID));
|
2021-02-08 04:33:57 +01:00
|
|
|
m_advDataSet = false;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // addServiceUUID
|
|
|
|
|
|
|
|
|
2020-07-14 05:24:07 +02:00
|
|
|
/**
|
2024-06-04 02:27:25 +02:00
|
|
|
* @brief Remove a service UUID from the advertisment.
|
|
|
|
* @param [in] serviceUUID The UUID of the service to remove.
|
2020-07-14 05:24:07 +02:00
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::removeServiceUUID(const NimBLEUUID &serviceUUID) {
|
|
|
|
for(auto it = m_serviceUUIDs.begin(); it != m_serviceUUIDs.end(); ++it) {
|
|
|
|
if((*it) == serviceUUID) {
|
|
|
|
m_serviceUUIDs.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_advDataSet = false;
|
|
|
|
} // addServiceUUID
|
|
|
|
|
|
|
|
|
2024-06-04 02:27:25 +02:00
|
|
|
/**
|
|
|
|
* @brief Remove all service UUIDs from the advertisment.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::removeServices() {
|
|
|
|
std::vector<NimBLEUUID>().swap(m_serviceUUIDs);
|
|
|
|
m_advDataSet = false;
|
|
|
|
} // removeServices
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Set the device appearance in the advertising data.
|
|
|
|
* @param [in] appearance The appearance of the device in the advertising data.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::setAppearance(uint16_t appearance) {
|
2020-05-14 06:03:56 +02:00
|
|
|
m_advData.appearance = appearance;
|
|
|
|
m_advData.appearance_is_present = 1;
|
2021-02-08 04:33:57 +01:00
|
|
|
m_advDataSet = false;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setAppearance
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
2021-02-08 04:33:57 +01:00
|
|
|
/**
|
|
|
|
* @brief Add the transmission power level to the advertisement packet.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::addTxPower() {
|
|
|
|
m_advData.tx_pwr_lvl_is_present = 1;
|
|
|
|
m_advDataSet = false;
|
|
|
|
} // addTxPower
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the advertised name of the device.
|
|
|
|
* @param [in] name The name to advertise.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::setName(const std::string &name) {
|
2024-06-04 02:27:25 +02:00
|
|
|
std::vector<uint8_t>(name.begin(), name.end()).swap(m_name);
|
2021-02-08 04:33:57 +01:00
|
|
|
m_advData.name = &m_name[0];
|
|
|
|
m_advData.name_len = m_name.size();
|
|
|
|
m_advDataSet = false;
|
|
|
|
} // setName
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the advertised manufacturer data.
|
2021-02-08 19:46:11 +01:00
|
|
|
* @param [in] data The data to advertise.
|
2021-02-08 04:33:57 +01:00
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::setManufacturerData(const std::string &data) {
|
2024-06-04 02:27:25 +02:00
|
|
|
std::vector<uint8_t>(data.begin(), data.end()).swap(m_mfgData);
|
2021-02-08 04:33:57 +01:00
|
|
|
m_advData.mfg_data = &m_mfgData[0];
|
|
|
|
m_advData.mfg_data_len = m_mfgData.size();
|
|
|
|
m_advDataSet = false;
|
|
|
|
} // setManufacturerData
|
|
|
|
|
|
|
|
|
2023-05-29 00:05:31 +02:00
|
|
|
/**
|
|
|
|
* @brief Set the advertised manufacturer data.
|
|
|
|
* @param [in] data The data to advertise.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::setManufacturerData(const std::vector<uint8_t> &data) {
|
|
|
|
m_mfgData = data;
|
|
|
|
m_advData.mfg_data = &m_mfgData[0];
|
|
|
|
m_advData.mfg_data_len = m_mfgData.size();
|
|
|
|
m_advDataSet = false;
|
|
|
|
} // setManufacturerData
|
|
|
|
|
|
|
|
|
2021-02-08 04:33:57 +01:00
|
|
|
/**
|
|
|
|
* @brief Set the advertised URI.
|
2021-02-08 19:46:11 +01:00
|
|
|
* @param [in] uri The URI to advertise.
|
2021-02-08 04:33:57 +01:00
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::setURI(const std::string &uri) {
|
2024-06-04 02:27:25 +02:00
|
|
|
std::vector<uint8_t>(uri.begin(), uri.end()).swap(m_uri);
|
2021-02-08 04:33:57 +01:00
|
|
|
m_advData.uri = &m_uri[0];
|
|
|
|
m_advData.uri_len = m_uri.size();
|
|
|
|
m_advDataSet = false;
|
|
|
|
} // setURI
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the service data advertised for the UUID.
|
|
|
|
* @param [in] uuid The UUID the service data belongs to.
|
|
|
|
* @param [in] data The data to advertise.
|
|
|
|
* @note If data length is 0 the service data will not be advertised.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::setServiceData(const NimBLEUUID &uuid, const std::string &data) {
|
|
|
|
switch (uuid.bitSize()) {
|
|
|
|
case 16: {
|
2024-06-04 02:27:25 +02:00
|
|
|
std::vector<uint8_t>((uint8_t*)&uuid.getNative()->u16.value,
|
|
|
|
(uint8_t*)&uuid.getNative()->u16.value + 2).swap(m_svcData16);
|
2021-02-08 04:33:57 +01:00
|
|
|
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: {
|
2024-06-04 02:27:25 +02:00
|
|
|
std::vector<uint8_t>((uint8_t*)&uuid.getNative()->u32.value,
|
|
|
|
(uint8_t*)&uuid.getNative()->u32.value + 4).swap(m_svcData32);
|
2021-02-08 04:33:57 +01:00
|
|
|
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: {
|
2024-06-04 02:27:25 +02:00
|
|
|
std::vector<uint8_t>(uuid.getNative()->u128.value,
|
|
|
|
uuid.getNative()->u128.value + 16).swap(m_svcData128);
|
2021-02-08 04:33:57 +01:00
|
|
|
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;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_advDataSet = false;
|
|
|
|
} // setServiceData
|
|
|
|
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
|
|
|
* @brief Set the type of advertisment to use.
|
|
|
|
* @param [in] adv_type:
|
2020-11-06 23:44:35 +01:00
|
|
|
* * BLE_GAP_CONN_MODE_NON (0) - not connectable advertising
|
|
|
|
* * BLE_GAP_CONN_MODE_DIR (1) - directed connectable advertising
|
|
|
|
* * BLE_GAP_CONN_MODE_UND (2) - undirected connectable advertising
|
2020-07-09 03:27:26 +02:00
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
void NimBLEAdvertising::setAdvertisementType(uint8_t adv_type){
|
2020-05-14 06:03:56 +02:00
|
|
|
m_advParams.conn_mode = adv_type;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setAdvertisementType
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the minimum advertising interval.
|
|
|
|
* @param [in] mininterval Minimum value for advertising interval in 0.625ms units, 0 = use default.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
void NimBLEAdvertising::setMinInterval(uint16_t mininterval) {
|
2020-05-14 06:03:56 +02:00
|
|
|
m_advParams.itvl_min = mininterval;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setMinInterval
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the maximum advertising interval.
|
|
|
|
* @param [in] maxinterval Maximum value for advertising interval in 0.625ms units, 0 = use default.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
void NimBLEAdvertising::setMaxInterval(uint16_t maxinterval) {
|
2020-05-14 06:03:56 +02:00
|
|
|
m_advParams.itvl_max = maxinterval;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setMaxInterval
|
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
|
2020-12-27 23:25:38 +01:00
|
|
|
/**
|
|
|
|
* @brief Set the advertised min connection interval preferred by this device.
|
|
|
|
* @param [in] mininterval the max interval value. Range = 0x0006 to 0x0C80.
|
|
|
|
* @details Values not within the range will cancel advertising of this data.\n
|
|
|
|
* Consumes 6 bytes of advertising space (combined with max interval).
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::setMinPreferred(uint16_t mininterval) {
|
|
|
|
// invalid paramters, set the slave interval to null
|
|
|
|
if(mininterval < 0x0006 || mininterval > 0x0C80) {
|
|
|
|
m_advData.slave_itvl_range = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_advData.slave_itvl_range == nullptr) {
|
|
|
|
m_advData.slave_itvl_range = m_slaveItvl;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_slaveItvl[0] = mininterval;
|
|
|
|
m_slaveItvl[1] = mininterval >> 8;
|
|
|
|
|
|
|
|
uint16_t maxinterval = *(uint16_t*)(m_advData.slave_itvl_range+2);
|
|
|
|
|
|
|
|
// If mininterval is higher than the maxinterval make them the same
|
|
|
|
if(mininterval > maxinterval) {
|
|
|
|
m_slaveItvl[2] = m_slaveItvl[0];
|
|
|
|
m_slaveItvl[3] = m_slaveItvl[1];
|
|
|
|
}
|
2021-02-08 04:33:57 +01:00
|
|
|
|
|
|
|
m_advDataSet = false;
|
2020-12-27 23:25:38 +01:00
|
|
|
} // setMinPreferred
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the advertised max connection interval preferred by this device.
|
|
|
|
* @param [in] maxinterval the max interval value. Range = 0x0006 to 0x0C80.
|
|
|
|
* @details Values not within the range will cancel advertising of this data.\n
|
|
|
|
* Consumes 6 bytes of advertising space (combined with min interval).
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::setMaxPreferred(uint16_t maxinterval) {
|
|
|
|
// invalid paramters, set the slave interval to null
|
|
|
|
if(maxinterval < 0x0006 || maxinterval > 0x0C80) {
|
|
|
|
m_advData.slave_itvl_range = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(m_advData.slave_itvl_range == nullptr) {
|
|
|
|
m_advData.slave_itvl_range = m_slaveItvl;
|
|
|
|
}
|
|
|
|
m_slaveItvl[2] = maxinterval;
|
|
|
|
m_slaveItvl[3] = maxinterval >> 8;
|
|
|
|
|
|
|
|
uint16_t mininterval = *(uint16_t*)(m_advData.slave_itvl_range);
|
|
|
|
|
|
|
|
// If mininterval is higher than the maxinterval make them the same
|
|
|
|
if(mininterval > maxinterval) {
|
|
|
|
m_slaveItvl[0] = m_slaveItvl[2];
|
|
|
|
m_slaveItvl[1] = m_slaveItvl[3];
|
|
|
|
}
|
2021-02-08 04:33:57 +01:00
|
|
|
|
|
|
|
m_advDataSet = false;
|
2020-12-27 23:25:38 +01:00
|
|
|
} // setMaxPreferred
|
|
|
|
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
|
|
|
* @brief Set if scan response is available.
|
|
|
|
* @param [in] set true = scan response available.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
void NimBLEAdvertising::setScanResponse(bool set) {
|
2020-05-14 06:03:56 +02:00
|
|
|
m_scanResp = set;
|
2021-02-08 04:33:57 +01:00
|
|
|
m_advDataSet = false;
|
2020-07-09 03:27:26 +02:00
|
|
|
} // setScanResponse
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the filtering for the scan filter.
|
|
|
|
* @param [in] scanRequestWhitelistOnly If true, only allow scan requests from those on the white list.
|
|
|
|
* @param [in] connectWhitelistOnly If true, only allow connections from those on the white list.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly) {
|
2021-01-14 03:48:38 +01:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> setScanFilter: scanRequestWhitelistOnly: %d, connectWhitelistOnly: %d",
|
|
|
|
scanRequestWhitelistOnly, connectWhitelistOnly);
|
2020-05-14 06:03:56 +02:00
|
|
|
if (!scanRequestWhitelistOnly && !connectWhitelistOnly) {
|
|
|
|
m_advParams.filter_policy = BLE_HCI_ADV_FILT_NONE;
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< setScanFilter");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (scanRequestWhitelistOnly && !connectWhitelistOnly) {
|
|
|
|
m_advParams.filter_policy = BLE_HCI_ADV_FILT_SCAN;
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< setScanFilter");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!scanRequestWhitelistOnly && connectWhitelistOnly) {
|
|
|
|
m_advParams.filter_policy = BLE_HCI_ADV_FILT_CONN;
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< setScanFilter");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (scanRequestWhitelistOnly && connectWhitelistOnly) {
|
|
|
|
m_advParams.filter_policy = BLE_HCI_ADV_FILT_BOTH;
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< setScanFilter");
|
|
|
|
return;
|
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setScanFilter
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Set the advertisement data that is to be published in a regular advertisement.
|
|
|
|
* @param [in] advertisementData The data to be advertised.
|
2020-09-14 04:02:15 +02:00
|
|
|
* @details The use of this function will replace any data set with addServiceUUID\n
|
|
|
|
* or setAppearance. If you wish for these to be advertised you must include them\n
|
|
|
|
* in the advertisementData parameter sent.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
void NimBLEAdvertising::setAdvertisementData(NimBLEAdvertisementData& advertisementData) {
|
2020-05-14 06:03:56 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> setAdvertisementData");
|
|
|
|
int rc = ble_gap_adv_set_data(
|
|
|
|
(uint8_t*)advertisementData.getPayload().data(),
|
|
|
|
advertisementData.getPayload().length());
|
|
|
|
if (rc != 0) {
|
2021-01-14 03:48:38 +01:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "ble_gap_adv_set_data: %d %s",
|
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
|
|
|
m_customAdvData = true; // Set the flag that indicates we are using custom advertising data.
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< setAdvertisementData");
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setAdvertisementData
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the advertisement data that is to be published in a scan response.
|
|
|
|
* @param [in] advertisementData The data to be advertised.
|
2020-09-14 04:02:15 +02:00
|
|
|
* @details Calling this without also using setAdvertisementData will have no effect.\n
|
|
|
|
* When using custom scan response data you must also use custom advertisement data.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::setScanResponseData(NimBLEAdvertisementData& advertisementData) {
|
2020-05-14 06:03:56 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> setScanResponseData");
|
|
|
|
int rc = ble_gap_adv_rsp_set_data(
|
|
|
|
(uint8_t*)advertisementData.getPayload().data(),
|
|
|
|
advertisementData.getPayload().length());
|
|
|
|
if (rc != 0) {
|
2021-01-14 03:48:38 +01:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "ble_gap_adv_rsp_set_data: %d %s",
|
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
|
|
|
m_customScanResponseData = true; // Set the flag that indicates we are using custom scan response data.
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< setScanResponseData");
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setScanResponseData
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Start advertising.
|
2022-08-27 03:32:01 +02:00
|
|
|
* @param [in] duration The duration, in milliseconds, to advertise, 0 == advertise forever.
|
2020-09-14 04:36:59 +02:00
|
|
|
* @param [in] advCompleteCB A pointer to a callback to be invoked when advertising ends.
|
2023-04-06 20:04:40 +02:00
|
|
|
* @param [in] dirAddr The address of a peer to directly advertise to.
|
2021-08-03 03:51:04 +02:00
|
|
|
* @return True if advertising started successfully.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2024-06-04 15:12:20 +02:00
|
|
|
bool NimBLEAdvertising::start(uint32_t duration, advCompleteCB_t advCompleteCB, NimBLEAddress* dirAddr) {
|
2021-01-14 03:48:38 +01:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> Advertising start: customAdvData: %d, customScanResponseData: %d",
|
|
|
|
m_customAdvData, m_customScanResponseData);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
// If Host is not synced we cannot start advertising.
|
|
|
|
if(!NimBLEDevice::m_synced) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Host reset, wait for sync.");
|
2021-01-14 03:48:38 +01:00
|
|
|
return false;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
2020-05-14 20:59:35 +02:00
|
|
|
NimBLEServer* pServer = NimBLEDevice::getServer();
|
|
|
|
if(pServer != nullptr) {
|
|
|
|
if(!pServer->m_gattsStarted){
|
|
|
|
pServer->start();
|
|
|
|
} else if(pServer->getConnectedCount() >= NIMBLE_MAX_CONNECTIONS) {
|
2021-01-14 03:48:38 +01:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "Max connections reached - not advertising");
|
|
|
|
return false;
|
2020-05-14 20:59:35 +02:00
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
// If already advertising just return
|
|
|
|
if(ble_gap_adv_active()) {
|
2021-01-14 03:48:38 +01:00
|
|
|
NIMBLE_LOGW(LOG_TAG, "Advertising already active");
|
2022-04-10 18:21:45 +02:00
|
|
|
return true;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-13 04:42:19 +01:00
|
|
|
// Save the duration incase of host reset so we can restart with the same params
|
|
|
|
m_duration = duration;
|
|
|
|
|
2020-09-14 04:36:59 +02:00
|
|
|
if(duration == 0){
|
|
|
|
duration = BLE_HS_FOREVER;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_advCompCB = advCompleteCB;
|
|
|
|
|
2020-11-06 23:44:35 +01:00
|
|
|
m_advParams.disc_mode = BLE_GAP_DISC_MODE_GEN;
|
|
|
|
m_advData.flags = (BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP);
|
|
|
|
if(m_advParams.conn_mode == BLE_GAP_CONN_MODE_NON) {
|
|
|
|
if(!m_scanResp) {
|
|
|
|
m_advParams.disc_mode = BLE_GAP_DISC_MODE_NON;
|
2022-07-10 15:15:46 +02:00
|
|
|
// non-connectable advertising does not require AD flags.
|
|
|
|
m_advData.flags = 0;
|
2020-11-06 23:44:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (!m_customAdvData && !m_advDataSet) {
|
2022-07-10 15:15:46 +02:00
|
|
|
//start with 3 bytes for the flags data if required
|
|
|
|
uint8_t payloadLen = (m_advData.flags > 0) ? (2 + 1) : 0;
|
2021-02-08 04:33:57 +01:00
|
|
|
if(m_advData.mfg_data_len > 0)
|
|
|
|
payloadLen += (2 + m_advData.mfg_data_len);
|
|
|
|
|
|
|
|
if(m_advData.svc_data_uuid16_len > 0)
|
|
|
|
payloadLen += (2 + m_advData.svc_data_uuid16_len);
|
|
|
|
|
|
|
|
if(m_advData.svc_data_uuid32_len > 0)
|
|
|
|
payloadLen += (2 + m_advData.svc_data_uuid32_len);
|
|
|
|
|
|
|
|
if(m_advData.svc_data_uuid128_len > 0)
|
|
|
|
payloadLen += (2 + m_advData.svc_data_uuid128_len);
|
|
|
|
|
|
|
|
if(m_advData.uri_len > 0)
|
|
|
|
payloadLen += (2 + m_advData.uri_len);
|
|
|
|
|
2020-10-24 15:55:37 +02:00
|
|
|
if(m_advData.appearance_is_present)
|
|
|
|
payloadLen += (2 + BLE_HS_ADV_APPEARANCE_LEN);
|
2021-02-08 04:33:57 +01:00
|
|
|
|
2020-10-24 15:55:37 +02:00
|
|
|
if(m_advData.tx_pwr_lvl_is_present)
|
2021-02-08 04:33:57 +01:00
|
|
|
payloadLen += (2 + BLE_HS_ADV_TX_PWR_LVL_LEN);
|
|
|
|
|
2020-12-27 23:25:38 +01:00
|
|
|
if(m_advData.slave_itvl_range != nullptr)
|
2021-02-08 04:33:57 +01:00
|
|
|
payloadLen += (2 + BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN);
|
2020-06-08 02:42:28 +02:00
|
|
|
|
|
|
|
for(auto &it : m_serviceUUIDs) {
|
|
|
|
if(it.getNative()->u.type == BLE_UUID_TYPE_16) {
|
2020-05-14 06:03:56 +02:00
|
|
|
int add = (m_advData.num_uuids16 > 0) ? 2 : 4;
|
2020-10-24 15:55:37 +02:00
|
|
|
if((payloadLen + add) > BLE_HS_ADV_MAX_SZ){
|
2020-05-14 06:03:56 +02:00
|
|
|
m_advData.uuids16_is_complete = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
payloadLen += add;
|
|
|
|
|
2021-02-10 18:54:53 +01:00
|
|
|
if(nullptr == (m_advData.uuids16 = (ble_uuid16_t*)realloc((void*)m_advData.uuids16,
|
2020-06-08 02:42:28 +02:00
|
|
|
(m_advData.num_uuids16 + 1) * sizeof(ble_uuid16_t))))
|
2020-03-30 01:44:20 +02:00
|
|
|
{
|
2021-01-14 03:48:38 +01:00
|
|
|
NIMBLE_LOGC(LOG_TAG, "Error, no mem");
|
2020-03-30 01:44:20 +02:00
|
|
|
abort();
|
|
|
|
}
|
2021-02-10 18:54:53 +01:00
|
|
|
memcpy((void*)&m_advData.uuids16[m_advData.num_uuids16],
|
|
|
|
&it.getNative()->u16, sizeof(ble_uuid16_t));
|
2020-03-30 01:44:20 +02:00
|
|
|
m_advData.uuids16_is_complete = 1;
|
|
|
|
m_advData.num_uuids16++;
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
2020-06-08 02:42:28 +02:00
|
|
|
if(it.getNative()->u.type == BLE_UUID_TYPE_32) {
|
2020-05-14 06:03:56 +02:00
|
|
|
int add = (m_advData.num_uuids32 > 0) ? 4 : 6;
|
2020-10-24 15:55:37 +02:00
|
|
|
if((payloadLen + add) > BLE_HS_ADV_MAX_SZ){
|
2020-05-14 06:03:56 +02:00
|
|
|
m_advData.uuids32_is_complete = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
payloadLen += add;
|
|
|
|
|
2021-02-10 18:54:53 +01:00
|
|
|
if(nullptr == (m_advData.uuids32 = (ble_uuid32_t*)realloc((void*)m_advData.uuids32,
|
2020-06-08 02:42:28 +02:00
|
|
|
(m_advData.num_uuids32 + 1) * sizeof(ble_uuid32_t))))
|
2020-03-30 01:44:20 +02:00
|
|
|
{
|
2021-01-14 03:48:38 +01:00
|
|
|
NIMBLE_LOGC(LOG_TAG, "Error, no mem");
|
2020-03-30 01:44:20 +02:00
|
|
|
abort();
|
|
|
|
}
|
2021-02-10 18:54:53 +01:00
|
|
|
memcpy((void*)&m_advData.uuids32[m_advData.num_uuids32],
|
|
|
|
&it.getNative()->u32, sizeof(ble_uuid32_t));
|
2020-03-30 01:44:20 +02:00
|
|
|
m_advData.uuids32_is_complete = 1;
|
|
|
|
m_advData.num_uuids32++;
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
2020-06-08 02:42:28 +02:00
|
|
|
if(it.getNative()->u.type == BLE_UUID_TYPE_128){
|
2020-05-14 06:03:56 +02:00
|
|
|
int add = (m_advData.num_uuids128 > 0) ? 16 : 18;
|
2020-10-24 15:55:37 +02:00
|
|
|
if((payloadLen + add) > BLE_HS_ADV_MAX_SZ){
|
2020-05-14 06:03:56 +02:00
|
|
|
m_advData.uuids128_is_complete = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
payloadLen += add;
|
|
|
|
|
2021-02-10 18:54:53 +01:00
|
|
|
if(nullptr == (m_advData.uuids128 = (ble_uuid128_t*)realloc((void*)m_advData.uuids128,
|
2020-06-08 02:42:28 +02:00
|
|
|
(m_advData.num_uuids128 + 1) * sizeof(ble_uuid128_t))))
|
|
|
|
{
|
2021-01-14 03:48:38 +01:00
|
|
|
NIMBLE_LOGC(LOG_TAG, "Error, no mem");
|
2020-03-30 01:44:20 +02:00
|
|
|
abort();
|
|
|
|
}
|
2021-02-10 18:54:53 +01:00
|
|
|
memcpy((void*)&m_advData.uuids128[m_advData.num_uuids128],
|
|
|
|
&it.getNative()->u128, sizeof(ble_uuid128_t));
|
2020-03-30 01:44:20 +02:00
|
|
|
m_advData.uuids128_is_complete = 1;
|
|
|
|
m_advData.num_uuids128++;
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if there is room for the name, if not put it in scan data
|
2020-10-24 15:55:37 +02:00
|
|
|
if((payloadLen + (2 + m_advData.name_len)) > BLE_HS_ADV_MAX_SZ) {
|
2021-02-06 22:29:03 +01:00
|
|
|
if(m_scanResp && !m_customScanResponseData){
|
2020-05-14 06:03:56 +02:00
|
|
|
m_scanData.name = m_advData.name;
|
|
|
|
m_scanData.name_len = m_advData.name_len;
|
2020-10-24 15:55:37 +02:00
|
|
|
if(m_scanData.name_len > BLE_HS_ADV_MAX_SZ - 2) {
|
|
|
|
m_scanData.name_len = BLE_HS_ADV_MAX_SZ - 2;
|
|
|
|
m_scanData.name_is_complete = 0;
|
|
|
|
} else {
|
2020-11-06 23:44:35 +01:00
|
|
|
m_scanData.name_is_complete = 1;
|
2020-10-24 15:55:37 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
m_advData.name = nullptr;
|
|
|
|
m_advData.name_len = 0;
|
2020-10-24 15:55:37 +02:00
|
|
|
m_advData.name_is_complete = 0;
|
2020-05-14 06:03:56 +02:00
|
|
|
} else {
|
2020-10-24 15:55:37 +02:00
|
|
|
if(m_advData.tx_pwr_lvl_is_present) {
|
|
|
|
m_advData.tx_pwr_lvl_is_present = 0;
|
|
|
|
payloadLen -= (2 + 1);
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
// if not using scan response just cut the name down
|
|
|
|
// leaving 2 bytes for the data specifier.
|
2020-10-24 15:55:37 +02:00
|
|
|
if(m_advData.name_len > (BLE_HS_ADV_MAX_SZ - payloadLen - 2)) {
|
|
|
|
m_advData.name_len = (BLE_HS_ADV_MAX_SZ - payloadLen - 2);
|
|
|
|
m_advData.name_is_complete = 0;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 22:29:03 +01:00
|
|
|
if(m_scanResp && !m_customScanResponseData) {
|
2020-05-14 06:03:56 +02:00
|
|
|
rc = ble_gap_adv_rsp_set_fields(&m_scanData);
|
2021-01-14 03:48:38 +01:00
|
|
|
switch(rc) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_HS_EBUSY:
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Already advertising");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_HS_EMSGSIZE:
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Scan data too long");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Error setting scan response data; rc=%d, %s",
|
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
|
|
|
break;
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 03:48:38 +01:00
|
|
|
if(rc == 0) {
|
|
|
|
rc = ble_gap_adv_set_fields(&m_advData);
|
|
|
|
switch(rc) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_HS_EBUSY:
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Already advertising");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_HS_EMSGSIZE:
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Advertisement data too long");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Error setting advertisement data; rc=%d, %s",
|
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
|
|
|
break;
|
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
if(m_advData.num_uuids128 > 0) {
|
2021-02-10 18:54:53 +01:00
|
|
|
free((void*)m_advData.uuids128);
|
2020-03-30 01:44:20 +02:00
|
|
|
m_advData.uuids128 = nullptr;
|
|
|
|
m_advData.num_uuids128 = 0;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
if(m_advData.num_uuids32 > 0) {
|
2021-02-10 18:54:53 +01:00
|
|
|
free((void*)m_advData.uuids32);
|
2020-03-30 01:44:20 +02:00
|
|
|
m_advData.uuids32 = nullptr;
|
|
|
|
m_advData.num_uuids32 = 0;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
if(m_advData.num_uuids16 > 0) {
|
2021-02-10 18:54:53 +01:00
|
|
|
free((void*)m_advData.uuids16);
|
2020-03-30 01:44:20 +02:00
|
|
|
m_advData.uuids16 = nullptr;
|
|
|
|
m_advData.num_uuids16 = 0;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-14 03:48:38 +01:00
|
|
|
if(rc !=0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
m_advDataSet = true;
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2023-04-06 20:04:40 +02:00
|
|
|
ble_addr_t peerAddr;
|
|
|
|
if (dirAddr != nullptr) {
|
|
|
|
memcpy(&peerAddr.val, dirAddr->getNative(), 6);
|
|
|
|
peerAddr.type = dirAddr->getType();
|
|
|
|
}
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
2023-04-06 20:04:40 +02:00
|
|
|
rc = ble_gap_adv_start(NimBLEDevice::m_own_addr_type,
|
|
|
|
(dirAddr != nullptr) ? &peerAddr : NULL,
|
|
|
|
duration,
|
2020-05-14 20:59:35 +02:00
|
|
|
&m_advParams,
|
2021-01-14 03:48:38 +01:00
|
|
|
(pServer != nullptr) ? NimBLEServer::handleGapEvent :
|
|
|
|
NimBLEAdvertising::handleGapEvent,
|
2022-04-10 18:21:45 +02:00
|
|
|
(void*)this);
|
2020-05-14 06:03:56 +02:00
|
|
|
#else
|
2023-04-06 20:04:40 +02:00
|
|
|
rc = ble_gap_adv_start(NimBLEDevice::m_own_addr_type,
|
|
|
|
(dirAddr != nullptr) ? &peerAddr : NULL,
|
|
|
|
duration,
|
|
|
|
&m_advParams,
|
|
|
|
NimBLEAdvertising::handleGapEvent,
|
|
|
|
(void*)this);
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif
|
2021-01-14 03:48:38 +01:00
|
|
|
switch(rc) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
|
2022-04-10 18:21:45 +02:00
|
|
|
case BLE_HS_EALREADY:
|
|
|
|
NIMBLE_LOGI(LOG_TAG, "Advertisement Already active");
|
|
|
|
break;
|
|
|
|
|
2021-01-14 03:48:38 +01:00
|
|
|
case BLE_HS_EINVAL:
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Unable to advertise - Duration too long");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_HS_EPREEMPTED:
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Unable to advertise - busy");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_HS_ETIMEOUT_HCI:
|
|
|
|
case BLE_HS_EOS:
|
|
|
|
case BLE_HS_ECONTROLLER:
|
|
|
|
case BLE_HS_ENOTSYNCED:
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Unable to advertise - Host Reset");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Error enabling advertising; rc=%d, %s",
|
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< Advertising start");
|
2022-04-10 18:21:45 +02:00
|
|
|
return (rc == 0 || rc == BLE_HS_EALREADY);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // start
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Stop advertising.
|
2022-04-10 18:21:45 +02:00
|
|
|
* @return True if advertising stopped successfully.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2022-04-10 18:21:45 +02:00
|
|
|
bool NimBLEAdvertising::stop() {
|
2020-05-14 06:03:56 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> stop");
|
2021-01-13 04:42:19 +01:00
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
int rc = ble_gap_adv_stop();
|
|
|
|
if (rc != 0 && rc != BLE_HS_EALREADY) {
|
2021-01-14 03:48:38 +01:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "ble_gap_adv_stop rc=%d %s",
|
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
2022-04-10 18:21:45 +02:00
|
|
|
return false;
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< stop");
|
2022-04-10 18:21:45 +02:00
|
|
|
return true;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // stop
|
|
|
|
|
2020-04-14 03:13:51 +02:00
|
|
|
|
2020-09-14 04:36:59 +02:00
|
|
|
/**
|
|
|
|
* @brief Handles the callback when advertising stops.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertising::advCompleteCB() {
|
|
|
|
if(m_advCompCB != nullptr) {
|
|
|
|
m_advCompCB(this);
|
|
|
|
}
|
2021-02-08 04:33:57 +01:00
|
|
|
} // advCompleteCB
|
2020-09-14 04:36:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if currently advertising.
|
|
|
|
* @return true if advertising is active.
|
|
|
|
*/
|
|
|
|
bool NimBLEAdvertising::isAdvertising() {
|
|
|
|
return ble_gap_adv_active();
|
2021-02-08 04:33:57 +01:00
|
|
|
} // isAdvertising
|
2020-09-14 04:36:59 +02:00
|
|
|
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/*
|
2020-05-14 06:03:56 +02:00
|
|
|
* Host reset seems to clear advertising data,
|
2020-04-14 03:13:51 +02:00
|
|
|
* we need clear the flag so it reloads it.
|
2020-05-14 06:03:56 +02:00
|
|
|
*/
|
2021-01-13 04:42:19 +01:00
|
|
|
void NimBLEAdvertising::onHostSync() {
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "Host re-synced");
|
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
m_advDataSet = false;
|
2021-01-13 04:42:19 +01:00
|
|
|
// If we were advertising forever, restart it now
|
|
|
|
if(m_duration == 0) {
|
|
|
|
start(m_duration, m_advCompCB);
|
2021-01-14 02:34:49 +01:00
|
|
|
} else {
|
|
|
|
// Otherwise we should tell the app that advertising stopped.
|
|
|
|
advCompleteCB();
|
2021-01-13 04:42:19 +01:00
|
|
|
}
|
2021-02-08 04:33:57 +01:00
|
|
|
} // onHostSync
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-09-14 04:36:59 +02:00
|
|
|
/**
|
|
|
|
* @brief Handler for gap events when not using peripheral role.
|
|
|
|
* @param [in] event the event data.
|
|
|
|
* @param [in] arg pointer to the advertising instance.
|
|
|
|
*/
|
|
|
|
/*STATIC*/
|
|
|
|
int NimBLEAdvertising::handleGapEvent(struct ble_gap_event *event, void *arg) {
|
|
|
|
NimBLEAdvertising *pAdv = (NimBLEAdvertising*)arg;
|
|
|
|
|
|
|
|
if(event->type == BLE_GAP_EVENT_ADV_COMPLETE) {
|
2021-01-13 04:42:19 +01:00
|
|
|
switch(event->adv_complete.reason) {
|
|
|
|
// Don't call the callback if host reset, we want to
|
|
|
|
// preserve the active flag until re-sync to restart advertising.
|
|
|
|
case BLE_HS_ETIMEOUT_HCI:
|
|
|
|
case BLE_HS_EOS:
|
|
|
|
case BLE_HS_ECONTROLLER:
|
|
|
|
case BLE_HS_ENOTSYNCED:
|
|
|
|
NIMBLE_LOGC(LOG_TAG, "host reset, rc=%d", event->adv_complete.reason);
|
|
|
|
NimBLEDevice::onReset(event->adv_complete.reason);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-09-14 04:36:59 +02:00
|
|
|
pAdv->advCompleteCB();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Add data to the payload to be advertised.
|
|
|
|
* @param [in] data The data to be added to the payload.
|
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
void NimBLEAdvertisementData::addData(const std::string &data) {
|
2020-05-14 06:03:56 +02:00
|
|
|
if ((m_payload.length() + data.length()) > BLE_HS_ADV_MAX_SZ) {
|
2022-07-31 19:00:12 +02:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "Advertisement data length exceeded");
|
2020-05-14 06:03:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_payload.append(data);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // addData
|
|
|
|
|
|
|
|
|
2020-07-28 05:38:22 +02:00
|
|
|
/**
|
|
|
|
* @brief Add data to the payload to be advertised.
|
|
|
|
* @param [in] data The data to be added to the payload.
|
|
|
|
* @param [in] length The size of data to be added to the payload.
|
|
|
|
*/
|
2021-02-06 21:27:12 +01:00
|
|
|
void NimBLEAdvertisementData::addData(char * data, size_t length) {
|
|
|
|
addData(std::string(data, length));
|
2020-07-28 05:38:22 +02:00
|
|
|
} // addData
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Set the appearance.
|
|
|
|
* @param [in] appearance The appearance code value.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::setAppearance(uint16_t appearance) {
|
2020-05-14 06:03:56 +02:00
|
|
|
char cdata[2];
|
|
|
|
cdata[0] = 3;
|
|
|
|
cdata[1] = BLE_HS_ADV_TYPE_APPEARANCE; // 0x19
|
|
|
|
addData(std::string(cdata, 2) + std::string((char*) &appearance, 2));
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setAppearance
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the advertisement flags.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] flag The flags to be set in the advertisement.
|
2020-03-30 01:44:20 +02:00
|
|
|
* * BLE_HS_ADV_F_DISC_LTD
|
|
|
|
* * BLE_HS_ADV_F_DISC_GEN
|
|
|
|
* * BLE_HS_ADV_F_BREDR_UNSUP - must always use with NimBLE
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::setFlags(uint8_t flag) {
|
2020-05-14 06:03:56 +02:00
|
|
|
char cdata[3];
|
|
|
|
cdata[0] = 2;
|
|
|
|
cdata[1] = BLE_HS_ADV_TYPE_FLAGS; // 0x01
|
|
|
|
cdata[2] = flag | BLE_HS_ADV_F_BREDR_UNSUP;
|
|
|
|
addData(std::string(cdata, 3));
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setFlag
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set manufacturer specific data.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] data The manufacturer data to advertise.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
void NimBLEAdvertisementData::setManufacturerData(const std::string &data) {
|
2020-05-14 06:03:56 +02:00
|
|
|
char cdata[2];
|
|
|
|
cdata[0] = data.length() + 1;
|
|
|
|
cdata[1] = BLE_HS_ADV_TYPE_MFG_DATA ; // 0xff
|
|
|
|
addData(std::string(cdata, 2) + data);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setManufacturerData
|
|
|
|
|
|
|
|
|
2023-05-29 00:05:31 +02:00
|
|
|
/**
|
|
|
|
* @brief Set manufacturer specific data.
|
|
|
|
* @param [in] data The manufacturer data to advertise.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::setManufacturerData(const std::vector<uint8_t> &data) {
|
|
|
|
char cdata[2];
|
|
|
|
cdata[0] = data.size() + 1;
|
|
|
|
cdata[1] = BLE_HS_ADV_TYPE_MFG_DATA ; // 0xff
|
|
|
|
addData(std::string(cdata, 2) + std::string((char*)&data[0], data.size()));
|
|
|
|
} // setManufacturerData
|
|
|
|
|
|
|
|
|
2021-02-08 04:33:57 +01:00
|
|
|
/**
|
|
|
|
* @brief Set the URI to advertise.
|
|
|
|
* @param [in] uri The uri to advertise.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::setURI(const std::string &uri) {
|
|
|
|
char cdata[2];
|
|
|
|
cdata[0] = uri.length() + 1;
|
|
|
|
cdata[1] = BLE_HS_ADV_TYPE_URI;
|
|
|
|
addData(std::string(cdata, 2) + uri);
|
|
|
|
} // setURI
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Set the complete name of this device.
|
|
|
|
* @param [in] name The name to advertise.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
void NimBLEAdvertisementData::setName(const std::string &name) {
|
2020-05-14 06:03:56 +02:00
|
|
|
char cdata[2];
|
|
|
|
cdata[0] = name.length() + 1;
|
|
|
|
cdata[1] = BLE_HS_ADV_TYPE_COMP_NAME; // 0x09
|
|
|
|
addData(std::string(cdata, 2) + name);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setName
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-06 21:27:12 +01:00
|
|
|
* @brief Set a single service to advertise as a complete list of services.
|
|
|
|
* @param [in] uuid The service to advertise.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::setCompleteServices(const NimBLEUUID &uuid) {
|
|
|
|
setServices(true, uuid.bitSize(), {uuid});
|
|
|
|
} // setCompleteServices
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the complete list of 16 bit services to advertise.
|
|
|
|
* @param [in] v_uuid A vector of 16 bit UUID's to advertise.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::setCompleteServices16(const std::vector<NimBLEUUID>& v_uuid) {
|
|
|
|
setServices(true, 16, v_uuid);
|
2021-02-08 04:33:57 +01:00
|
|
|
} // setCompleteServices16
|
2021-02-06 21:27:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the complete list of 32 bit services to advertise.
|
|
|
|
* @param [in] v_uuid A vector of 32 bit UUID's to advertise.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::setCompleteServices32(const std::vector<NimBLEUUID>& v_uuid) {
|
|
|
|
setServices(true, 32, v_uuid);
|
2021-02-08 04:33:57 +01:00
|
|
|
} // setCompleteServices32
|
2021-02-06 21:27:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set a single service to advertise as a partial list of services.
|
|
|
|
* @param [in] uuid The service to advertise.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
void NimBLEAdvertisementData::setPartialServices(const NimBLEUUID &uuid) {
|
2021-02-06 21:27:12 +01:00
|
|
|
setServices(false, uuid.bitSize(), {uuid});
|
|
|
|
} // setPartialServices
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the partial list of services to advertise.
|
|
|
|
* @param [in] v_uuid A vector of 16 bit UUID's to advertise.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::setPartialServices16(const std::vector<NimBLEUUID>& v_uuid) {
|
|
|
|
setServices(false, 16, v_uuid);
|
2021-02-08 04:33:57 +01:00
|
|
|
} // setPartialServices16
|
2021-02-06 21:27:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the partial list of services to advertise.
|
|
|
|
* @param [in] v_uuid A vector of 32 bit UUID's to advertise.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::setPartialServices32(const std::vector<NimBLEUUID>& v_uuid) {
|
|
|
|
setServices(false, 32, v_uuid);
|
2021-02-08 04:33:57 +01:00
|
|
|
} // setPartialServices32
|
2021-02-06 21:27:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Utility function to create the list of service UUID's from a vector.
|
|
|
|
* @param [in] complete If true the vector is the complete set of services.
|
|
|
|
* @param [in] size The bit size of the UUID's in the vector. (16, 32, or 128).
|
|
|
|
* @param [in] v_uuid The vector of service UUID's to advertise.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::setServices(const bool complete, const uint8_t size,
|
|
|
|
const std::vector<NimBLEUUID> &v_uuid)
|
|
|
|
{
|
2020-05-14 06:03:56 +02:00
|
|
|
char cdata[2];
|
2021-02-06 21:27:12 +01:00
|
|
|
cdata[0] = (size / 8) * v_uuid.size() + 1;
|
|
|
|
switch(size) {
|
|
|
|
case 16:
|
|
|
|
cdata[1] = complete ? BLE_HS_ADV_TYPE_COMP_UUIDS16 : BLE_HS_ADV_TYPE_INCOMP_UUIDS16;
|
2020-05-14 06:03:56 +02:00
|
|
|
break;
|
2021-02-06 21:27:12 +01:00
|
|
|
case 32:
|
|
|
|
cdata[1] = complete ? BLE_HS_ADV_TYPE_COMP_UUIDS32 : BLE_HS_ADV_TYPE_INCOMP_UUIDS32;
|
2020-05-14 06:03:56 +02:00
|
|
|
break;
|
2021-02-06 21:27:12 +01:00
|
|
|
case 128:
|
|
|
|
cdata[1] = complete ? BLE_HS_ADV_TYPE_COMP_UUIDS128 : BLE_HS_ADV_TYPE_INCOMP_UUIDS128;
|
2020-05-14 06:03:56 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2021-02-06 21:27:12 +01:00
|
|
|
|
|
|
|
std::string uuids;
|
|
|
|
|
|
|
|
for(auto &it : v_uuid){
|
|
|
|
if(it.bitSize() != size) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Service UUID(%d) invalid", size);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
switch(size) {
|
|
|
|
case 16:
|
|
|
|
uuids += std::string((char*)&it.getNative()->u16.value, 2);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
uuids += std::string((char*)&it.getNative()->u32.value, 4);
|
|
|
|
break;
|
|
|
|
case 128:
|
|
|
|
uuids += std::string((char*)&it.getNative()->u128.value, 16);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addData(std::string(cdata, 2) + uuids);
|
2021-02-08 04:33:57 +01:00
|
|
|
} // setServices
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the service data (UUID + data)
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] uuid The UUID to set with the service data.
|
|
|
|
* @param [in] data The data to be associated with the service data advertised.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
void NimBLEAdvertisementData::setServiceData(const NimBLEUUID &uuid, const std::string &data) {
|
2020-05-14 06:03:56 +02:00
|
|
|
char cdata[2];
|
|
|
|
switch (uuid.bitSize()) {
|
|
|
|
case 16: {
|
|
|
|
// [Len] [0x16] [UUID16] data
|
|
|
|
cdata[0] = data.length() + 3;
|
|
|
|
cdata[1] = BLE_HS_ADV_TYPE_SVC_DATA_UUID16; // 0x16
|
|
|
|
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->u16.value, 2) + data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 32: {
|
|
|
|
// [Len] [0x20] [UUID32] data
|
|
|
|
cdata[0] = data.length() + 5;
|
|
|
|
cdata[1] = BLE_HS_ADV_TYPE_SVC_DATA_UUID32; // 0x20
|
|
|
|
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->u32.value, 4) + data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 128: {
|
|
|
|
// [Len] [0x21] [UUID128] data
|
|
|
|
cdata[0] = data.length() + 17;
|
|
|
|
cdata[1] = BLE_HS_ADV_TYPE_SVC_DATA_UUID128; // 0x21
|
|
|
|
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->u128.value, 16) + data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setServiceData
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the short name.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] name The short name of the device.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
void NimBLEAdvertisementData::setShortName(const std::string &name) {
|
2020-05-14 06:03:56 +02:00
|
|
|
char cdata[2];
|
|
|
|
cdata[0] = name.length() + 1;
|
|
|
|
cdata[1] = BLE_HS_ADV_TYPE_INCOMP_NAME; // 0x08
|
|
|
|
addData(std::string(cdata, 2) + name);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setShortName
|
|
|
|
|
|
|
|
|
2021-02-08 04:33:57 +01:00
|
|
|
/**
|
|
|
|
* @brief Adds Tx power level to the advertisement data.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::addTxPower() {
|
|
|
|
char cdata[3];
|
|
|
|
cdata[0] = BLE_HS_ADV_TX_PWR_LVL_LEN + 1;
|
|
|
|
cdata[1] = BLE_HS_ADV_TYPE_TX_PWR_LVL;
|
|
|
|
cdata[2] = NimBLEDevice::getPower();
|
|
|
|
addData(cdata, 3);
|
|
|
|
} // addTxPower
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the preferred connection interval parameters.
|
|
|
|
* @param [in] min The minimum interval desired.
|
|
|
|
* @param [in] max The maximum interval desired.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::setPreferredParams(uint16_t min, uint16_t max) {
|
|
|
|
char cdata[6];
|
|
|
|
cdata[0] = BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN + 1;
|
|
|
|
cdata[1] = BLE_HS_ADV_TYPE_SLAVE_ITVL_RANGE;
|
|
|
|
cdata[2] = min;
|
|
|
|
cdata[3] = min >> 8;
|
|
|
|
cdata[4] = max;
|
|
|
|
cdata[5] = max >> 8;
|
|
|
|
addData(cdata, 6);
|
|
|
|
} // setPreferredParams
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Retrieve the payload that is to be advertised.
|
|
|
|
* @return The payload that is to be advertised.
|
|
|
|
*/
|
|
|
|
std::string NimBLEAdvertisementData::getPayload() {
|
2020-05-14 06:03:56 +02:00
|
|
|
return m_payload;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // getPayload
|
|
|
|
|
2024-06-13 23:02:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Clear the advertisement data for reuse.
|
|
|
|
*/
|
|
|
|
void NimBLEAdvertisementData::clearData() {
|
|
|
|
m_payload.clear();
|
|
|
|
}
|
|
|
|
|
2022-04-10 18:21:45 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && !CONFIG_BT_NIMBLE_EXT_ADV */
|