mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-11-24 14:10:55 +01:00
[NimBLEAdvertisementData] Add setServices methods for multiple UUID's
Adds: - setPartialServices16 - setPartialServices32 - setCompleteServices16 - setCompleteServices32 Each takes an input parameter of std::vector<NimBLEUUID> to allow for advertising multiple services with a simple interface.
This commit is contained in:
parent
ae2ff3a638
commit
b290ca9077
2 changed files with 103 additions and 73 deletions
|
@ -646,6 +646,7 @@ int NimBLEAdvertising::handleGapEvent(struct ble_gap_event *event, void *arg) {
|
||||||
*/
|
*/
|
||||||
void NimBLEAdvertisementData::addData(const std::string &data) {
|
void NimBLEAdvertisementData::addData(const std::string &data) {
|
||||||
if ((m_payload.length() + data.length()) > BLE_HS_ADV_MAX_SZ) {
|
if ((m_payload.length() + data.length()) > BLE_HS_ADV_MAX_SZ) {
|
||||||
|
NIMBLE_LOGE(LOG_TAG, "Advertisement data length exceded");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_payload.append(data);
|
m_payload.append(data);
|
||||||
|
@ -658,10 +659,7 @@ void NimBLEAdvertisementData::addData(const std::string &data) {
|
||||||
* @param [in] length The size of data to be added to the payload.
|
* @param [in] length The size of data to be added to the payload.
|
||||||
*/
|
*/
|
||||||
void NimBLEAdvertisementData::addData(char * data, size_t length) {
|
void NimBLEAdvertisementData::addData(char * data, size_t length) {
|
||||||
if ((m_payload.length() + length) > BLE_HS_ADV_MAX_SZ) {
|
addData(std::string(data, length));
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_payload.append(data,length);
|
|
||||||
} // addData
|
} // addData
|
||||||
|
|
||||||
|
|
||||||
|
@ -680,43 +678,6 @@ void NimBLEAdvertisementData::setAppearance(uint16_t appearance) {
|
||||||
} // setAppearance
|
} // setAppearance
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Set the complete services to advertise.
|
|
||||||
* @param [in] uuid The UUID of the service.
|
|
||||||
*/
|
|
||||||
void NimBLEAdvertisementData::setCompleteServices(const NimBLEUUID &uuid) {
|
|
||||||
char cdata[2];
|
|
||||||
switch (uuid.bitSize()) {
|
|
||||||
case 16: {
|
|
||||||
// [Len] [0x02] [LL] [HH]
|
|
||||||
cdata[0] = 3;
|
|
||||||
cdata[1] = BLE_HS_ADV_TYPE_COMP_UUIDS16; // 0x03
|
|
||||||
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->u16.value, 2));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 32: {
|
|
||||||
// [Len] [0x04] [LL] [LL] [HH] [HH]
|
|
||||||
cdata[0] = 5;
|
|
||||||
cdata[1] = BLE_HS_ADV_TYPE_COMP_UUIDS32; // 0x05
|
|
||||||
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->u32.value, 4));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 128: {
|
|
||||||
// [Len] [0x04] [0] [1] ... [15]
|
|
||||||
cdata[0] = 17;
|
|
||||||
cdata[1] = BLE_HS_ADV_TYPE_COMP_UUIDS128; // 0x07
|
|
||||||
addData(std::string(cdata, 2) + std::string((char*) uuid.getNative()->u128.value, 16));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} // setCompleteServices
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set the advertisement flags.
|
* @brief Set the advertisement flags.
|
||||||
* @param [in] flag The flags to be set in the advertisement.
|
* @param [in] flag The flags to be set in the advertisement.
|
||||||
|
@ -738,12 +699,10 @@ void NimBLEAdvertisementData::setFlags(uint8_t flag) {
|
||||||
* @param [in] data The manufacturer data to advertise.
|
* @param [in] data The manufacturer data to advertise.
|
||||||
*/
|
*/
|
||||||
void NimBLEAdvertisementData::setManufacturerData(const std::string &data) {
|
void NimBLEAdvertisementData::setManufacturerData(const std::string &data) {
|
||||||
NIMBLE_LOGD("NimBLEAdvertisementData", ">> setManufacturerData");
|
|
||||||
char cdata[2];
|
char cdata[2];
|
||||||
cdata[0] = data.length() + 1;
|
cdata[0] = data.length() + 1;
|
||||||
cdata[1] = BLE_HS_ADV_TYPE_MFG_DATA ; // 0xff
|
cdata[1] = BLE_HS_ADV_TYPE_MFG_DATA ; // 0xff
|
||||||
addData(std::string(cdata, 2) + data);
|
addData(std::string(cdata, 2) + data);
|
||||||
NIMBLE_LOGD("NimBLEAdvertisementData", "<< setManufacturerData");
|
|
||||||
} // setManufacturerData
|
} // setManufacturerData
|
||||||
|
|
||||||
|
|
||||||
|
@ -752,50 +711,117 @@ void NimBLEAdvertisementData::setManufacturerData(const std::string &data) {
|
||||||
* @param [in] name The name to advertise.
|
* @param [in] name The name to advertise.
|
||||||
*/
|
*/
|
||||||
void NimBLEAdvertisementData::setName(const std::string &name) {
|
void NimBLEAdvertisementData::setName(const std::string &name) {
|
||||||
NIMBLE_LOGD("NimBLEAdvertisementData", ">> setName: %s", name.c_str());
|
|
||||||
char cdata[2];
|
char cdata[2];
|
||||||
cdata[0] = name.length() + 1;
|
cdata[0] = name.length() + 1;
|
||||||
cdata[1] = BLE_HS_ADV_TYPE_COMP_NAME; // 0x09
|
cdata[1] = BLE_HS_ADV_TYPE_COMP_NAME; // 0x09
|
||||||
addData(std::string(cdata, 2) + name);
|
addData(std::string(cdata, 2) + name);
|
||||||
NIMBLE_LOGD("NimBLEAdvertisementData", "<< setName");
|
|
||||||
} // setName
|
} // setName
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set the partial services to advertise.
|
* @brief Set a single service to advertise as a complete list of services.
|
||||||
* @param [in] uuid The single service to advertise.
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set a single service to advertise as a partial list of services.
|
||||||
|
* @param [in] uuid The service to advertise.
|
||||||
*/
|
*/
|
||||||
void NimBLEAdvertisementData::setPartialServices(const NimBLEUUID &uuid) {
|
void NimBLEAdvertisementData::setPartialServices(const NimBLEUUID &uuid) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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)
|
||||||
|
{
|
||||||
char cdata[2];
|
char cdata[2];
|
||||||
switch (uuid.bitSize()) {
|
cdata[0] = (size / 8) * v_uuid.size() + 1;
|
||||||
case 16: {
|
switch(size) {
|
||||||
// [Len] [0x02] [LL] [HH]
|
case 16:
|
||||||
cdata[0] = 3;
|
cdata[1] = complete ? BLE_HS_ADV_TYPE_COMP_UUIDS16 : BLE_HS_ADV_TYPE_INCOMP_UUIDS16;
|
||||||
cdata[1] = BLE_HS_ADV_TYPE_INCOMP_UUIDS16; // 0x02
|
|
||||||
addData(std::string(cdata, 2) + std::string((char *) &uuid.getNative()->u16.value, 2));
|
|
||||||
break;
|
break;
|
||||||
}
|
case 32:
|
||||||
|
cdata[1] = complete ? BLE_HS_ADV_TYPE_COMP_UUIDS32 : BLE_HS_ADV_TYPE_INCOMP_UUIDS32;
|
||||||
case 32: {
|
|
||||||
// [Len] [0x04] [LL] [LL] [HH] [HH]
|
|
||||||
cdata[0] = 5;
|
|
||||||
cdata[1] = BLE_HS_ADV_TYPE_INCOMP_UUIDS32; // 0x04
|
|
||||||
addData(std::string(cdata, 2) + std::string((char *) &uuid.getNative()->u32.value, 4));
|
|
||||||
break;
|
break;
|
||||||
}
|
case 128:
|
||||||
|
cdata[1] = complete ? BLE_HS_ADV_TYPE_COMP_UUIDS128 : BLE_HS_ADV_TYPE_INCOMP_UUIDS128;
|
||||||
case 128: {
|
|
||||||
// [Len] [0x04] [0] [1] ... [15]
|
|
||||||
cdata[0] = 17;
|
|
||||||
cdata[1] = BLE_HS_ADV_TYPE_INCOMP_UUIDS128; // 0x06
|
|
||||||
addData(std::string(cdata, 2) + std::string((char *) &uuid.getNative()->u128.value, 16));
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} // setPartialServices
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -841,12 +867,10 @@ void NimBLEAdvertisementData::setServiceData(const NimBLEUUID &uuid, const std::
|
||||||
* @param [in] name The short name of the device.
|
* @param [in] name The short name of the device.
|
||||||
*/
|
*/
|
||||||
void NimBLEAdvertisementData::setShortName(const std::string &name) {
|
void NimBLEAdvertisementData::setShortName(const std::string &name) {
|
||||||
NIMBLE_LOGD("NimBLEAdvertisementData", ">> setShortName: %s", name.c_str());
|
|
||||||
char cdata[2];
|
char cdata[2];
|
||||||
cdata[0] = name.length() + 1;
|
cdata[0] = name.length() + 1;
|
||||||
cdata[1] = BLE_HS_ADV_TYPE_INCOMP_NAME; // 0x08
|
cdata[1] = BLE_HS_ADV_TYPE_INCOMP_NAME; // 0x08
|
||||||
addData(std::string(cdata, 2) + name);
|
addData(std::string(cdata, 2) + name);
|
||||||
NIMBLE_LOGD("NimBLEAdvertisementData", "<< setShortName");
|
|
||||||
} // setShortName
|
} // setShortName
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -50,10 +50,14 @@ class NimBLEAdvertisementData {
|
||||||
public:
|
public:
|
||||||
void setAppearance(uint16_t appearance);
|
void setAppearance(uint16_t appearance);
|
||||||
void setCompleteServices(const NimBLEUUID &uuid);
|
void setCompleteServices(const NimBLEUUID &uuid);
|
||||||
|
void setCompleteServices16(const std::vector<NimBLEUUID> &v_uuid);
|
||||||
|
void setCompleteServices32(const std::vector<NimBLEUUID> &v_uuid);
|
||||||
void setFlags(uint8_t);
|
void setFlags(uint8_t);
|
||||||
void setManufacturerData(const std::string &data);
|
void setManufacturerData(const std::string &data);
|
||||||
void setName(const std::string &name);
|
void setName(const std::string &name);
|
||||||
void setPartialServices(const NimBLEUUID &uuid);
|
void setPartialServices(const NimBLEUUID &uuid);
|
||||||
|
void setPartialServices16(const std::vector<NimBLEUUID> &v_uuid);
|
||||||
|
void setPartialServices32(const std::vector<NimBLEUUID> &v_uuid);
|
||||||
void setServiceData(const NimBLEUUID &uuid, const std::string &data);
|
void setServiceData(const NimBLEUUID &uuid, const std::string &data);
|
||||||
void setShortName(const std::string &name);
|
void setShortName(const std::string &name);
|
||||||
void addData(const std::string &data); // Add data to the payload.
|
void addData(const std::string &data); // Add data to the payload.
|
||||||
|
@ -62,6 +66,8 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class NimBLEAdvertising;
|
friend class NimBLEAdvertising;
|
||||||
|
void setServices(const bool complete, const uint8_t size,
|
||||||
|
const std::vector<NimBLEUUID> &v_uuid);
|
||||||
std::string m_payload; // The payload of the advertisement.
|
std::string m_payload; // The payload of the advertisement.
|
||||||
}; // NimBLEAdvertisementData
|
}; // NimBLEAdvertisementData
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue