Skip to content

Commit

Permalink
[Breaking] Refactor NimBLEUUID.
Browse files Browse the repository at this point in the history
* msbFirst parameter has been removed from constructor as it was unnecessary,
caller should reverse the data first or call the new `reverseByteOrder` method after.
* `getNative` method replaced with `getBase` which returns a read-only pointer to the UUID size underlying.
* Added `reverseByteOrder` method, this will reverse the bytes of the UUID, which can be useful for advertising/logging.
* Added `getValue` method, which returns a read-only `uint8_t` pointer to the UUID value.
* Removed `m_valueSet` member variable, `bitSize()` can be used as a replacement.
* General code cleanup.
  • Loading branch information
h2zero committed Jul 22, 2024
1 parent b713cbf commit 8f75994
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 280 deletions.
6 changes: 3 additions & 3 deletions src/NimBLEAdvertisedDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ std::string NimBLEAdvertisedDevice::getServiceData(const NimBLEUUID& uuid) const

while (data_loc < pl_size) {
const ble_hs_adv_field* field = reinterpret_cast<const ble_hs_adv_field*>(&m_payload[data_loc]);
if (bytes == uuid_bytes && NimBLEUUID(field->value, bytes, false) == uuid) {
if (bytes == uuid_bytes && NimBLEUUID(field->value, bytes) == uuid) {
const char* field_data = reinterpret_cast<const char*>(field->value + bytes);
return std::string(field_data, field->length - bytes - 1);
}
Expand All @@ -344,7 +344,7 @@ NimBLEUUID NimBLEAdvertisedDevice::getServiceDataUUID(uint8_t index) const {
if (data_loc != ULONG_MAX) {
const ble_hs_adv_field* field = reinterpret_cast<const ble_hs_adv_field*>(&m_payload[data_loc]);
if (field->length >= bytes) {
return NimBLEUUID(field->value, bytes, false);
return NimBLEUUID(field->value, bytes);
}
}

Expand Down Expand Up @@ -436,7 +436,7 @@ NimBLEUUID NimBLEAdvertisedDevice::getServiceUUID(uint8_t index) const {
}

if (field->length > uuid_bytes * index) {
return NimBLEUUID(field->value + uuid_bytes * (index - 1), uuid_bytes, false);
return NimBLEUUID(field->value + uuid_bytes * (index - 1), uuid_bytes);
}
}

Expand Down
57 changes: 20 additions & 37 deletions src/NimBLEAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,26 +196,23 @@ void NimBLEAdvertising::setURI(const std::string &uri) {
void NimBLEAdvertising::setServiceData(const NimBLEUUID &uuid, const std::string &data) {
switch (uuid.bitSize()) {
case 16: {
std::vector<uint8_t>((uint8_t*)&uuid.getNative()->u16.value,
(uint8_t*)&uuid.getNative()->u16.value + 2).swap(m_svcData16);
std::vector<uint8_t>(uuid.getValue(), uuid.getValue() + 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: {
std::vector<uint8_t>((uint8_t*)&uuid.getNative()->u32.value,
(uint8_t*)&uuid.getNative()->u32.value + 4).swap(m_svcData32);
std::vector<uint8_t>(uuid.getValue(), uuid.getValue() + 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: {
std::vector<uint8_t>(uuid.getNative()->u128.value,
uuid.getNative()->u128.value + 16).swap(m_svcData128);
std::vector<uint8_t>(uuid.getValue(), uuid.getValue() + 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 Expand Up @@ -489,60 +486,58 @@ bool NimBLEAdvertising::start(uint32_t duration, advCompleteCB_t advCompleteCB,
payloadLen += (2 + BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN);

for(auto &it : m_serviceUUIDs) {
if(it.getNative()->u.type == BLE_UUID_TYPE_16) {
if(it.bitSize() == BLE_UUID_TYPE_16) {
int add = (m_advData.num_uuids16 > 0) ? 2 : 4;
if((payloadLen + add) > BLE_HS_ADV_MAX_SZ){
m_advData.uuids16_is_complete = 0;
continue;
}
payloadLen += add;

if(nullptr == (m_advData.uuids16 = (ble_uuid16_t*)realloc((void*)m_advData.uuids16,
(m_advData.num_uuids16 + 1) * sizeof(ble_uuid16_t))))
if(nullptr == (m_advData.uuids16 = reinterpret_cast<ble_uuid16_t*>(realloc((void*)m_advData.uuids16,
(m_advData.num_uuids16 + 1) * sizeof(ble_uuid16_t)))))
{
NIMBLE_LOGE(LOG_TAG, "Error, no mem");
return false;
}
memcpy((void*)&m_advData.uuids16[m_advData.num_uuids16],
&it.getNative()->u16, sizeof(ble_uuid16_t));
reinterpret_cast<ble_uuid16_t*>(&it), sizeof(ble_uuid16_t));
m_advData.uuids16_is_complete = 1;
m_advData.num_uuids16++;
}
if(it.getNative()->u.type == BLE_UUID_TYPE_32) {
} else if(it.bitSize() == BLE_UUID_TYPE_32) {
int add = (m_advData.num_uuids32 > 0) ? 4 : 6;
if((payloadLen + add) > BLE_HS_ADV_MAX_SZ){
m_advData.uuids32_is_complete = 0;
continue;
}
payloadLen += add;

if(nullptr == (m_advData.uuids32 = (ble_uuid32_t*)realloc((void*)m_advData.uuids32,
(m_advData.num_uuids32 + 1) * sizeof(ble_uuid32_t))))
if(nullptr == (m_advData.uuids32 = reinterpret_cast<ble_uuid32_t*>(realloc((void*)m_advData.uuids32,
(m_advData.num_uuids32 + 1) * sizeof(ble_uuid32_t)))))
{
NIMBLE_LOGE(LOG_TAG, "Error, no mem");
return false;
}
memcpy((void*)&m_advData.uuids32[m_advData.num_uuids32],
&it.getNative()->u32, sizeof(ble_uuid32_t));
reinterpret_cast<ble_uuid32_t*>(&it), sizeof(ble_uuid32_t));
m_advData.uuids32_is_complete = 1;
m_advData.num_uuids32++;
}
if(it.getNative()->u.type == BLE_UUID_TYPE_128){
} else if(it.bitSize() == BLE_UUID_TYPE_128){
int add = (m_advData.num_uuids128 > 0) ? 16 : 18;
if((payloadLen + add) > BLE_HS_ADV_MAX_SZ){
m_advData.uuids128_is_complete = 0;
continue;
}
payloadLen += add;

if(nullptr == (m_advData.uuids128 = (ble_uuid128_t*)realloc((void*)m_advData.uuids128,
(m_advData.num_uuids128 + 1) * sizeof(ble_uuid128_t))))
if(nullptr == (m_advData.uuids128 = reinterpret_cast<ble_uuid128_t*>(realloc((void*)m_advData.uuids128,
(m_advData.num_uuids128 + 1) * sizeof(ble_uuid128_t)))))
{
NIMBLE_LOGE(LOG_TAG, "Error, no mem");
return false;
}
memcpy((void*)&m_advData.uuids128[m_advData.num_uuids128],
&it.getNative()->u128, sizeof(ble_uuid128_t));
reinterpret_cast<ble_uuid128_t*>(&it), sizeof(ble_uuid128_t));
m_advData.uuids128_is_complete = 1;
m_advData.num_uuids128++;
}
Expand Down Expand Up @@ -961,21 +956,9 @@ void NimBLEAdvertisementData::setServices(const bool complete, const uint8_t siz
for(auto &it : v_uuid){
if(it.bitSize() != size) {
NIMBLE_LOGE(LOG_TAG, "Service UUID(%d) invalid", size);
return;
continue;
} 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;
}
uuids += std::string(reinterpret_cast<const char*>(it.getValue()), size / 8);
}
}

Expand All @@ -995,23 +978,23 @@ void NimBLEAdvertisementData::setServiceData(const NimBLEUUID &uuid, const std::
// [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);
addData(std::string(cdata, 2) + std::string(reinterpret_cast<const char*>(uuid.getValue()), 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);
addData(std::string(cdata, 2) + std::string(reinterpret_cast<const char*>(uuid.getValue()), 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);
addData(std::string(cdata, 2) + std::string(reinterpret_cast<const char*>(uuid.getValue()), 16) + data);
break;
}

Expand Down
6 changes: 2 additions & 4 deletions src/NimBLEBeacon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ uint16_t NimBLEBeacon::getMinor() {
* @return The UUID advertised.
*/
NimBLEUUID NimBLEBeacon::getProximityUUID() {
return NimBLEUUID(m_beaconData.proximityUUID, 16, true);
return NimBLEUUID(m_beaconData.proximityUUID, 16).reverseByteOrder();
}


Expand Down Expand Up @@ -140,9 +140,7 @@ void NimBLEBeacon::setMinor(uint16_t minor) {
void NimBLEBeacon::setProximityUUID(const NimBLEUUID &uuid) {
NimBLEUUID temp_uuid = uuid;
temp_uuid.to128();
std::reverse_copy(temp_uuid.getNative()->u128.value,
temp_uuid.getNative()->u128.value + 16,
m_beaconData.proximityUUID);
std::reverse_copy(temp_uuid.getValue(), temp_uuid.getValue() + 16, m_beaconData.proximityUUID);
} // setProximityUUID


Expand Down
2 changes: 1 addition & 1 deletion src/NimBLECharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_han
ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR ? "Read" : "Write");

uuid = ctxt->chr->uuid;
if(ble_uuid_cmp(uuid, &pCharacteristic->getUUID().getNative()->u) == 0){
if(ble_uuid_cmp(uuid, pCharacteristic->getUUID().getBase()) == 0){
switch(ctxt->op) {
case BLE_GATT_ACCESS_OP_READ_CHR: {
ble_gap_conn_find(conn_handle, &peerInfo.m_desc);
Expand Down
2 changes: 1 addition & 1 deletion src/NimBLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) {
if(uuid_filter == nullptr) {
rc = ble_gattc_disc_all_svcs(m_conn_id, NimBLEClient::serviceDiscoveredCB, &taskData);
} else {
rc = ble_gattc_disc_svc_by_uuid(m_conn_id, &uuid_filter->getNative()->u,
rc = ble_gattc_disc_svc_by_uuid(m_conn_id, uuid_filter->getBase(),
NimBLEClient::serviceDiscoveredCB, &taskData);
}

Expand Down
2 changes: 1 addition & 1 deletion src/NimBLEDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ int NimBLEDescriptor::handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC ? "Read" : "Write");

uuid = ctxt->chr->uuid;
if(ble_uuid_cmp(uuid, &pDescriptor->getUUID().getNative()->u) == 0){
if(ble_uuid_cmp(uuid, pDescriptor->getUUID().getBase()) == 0){
switch(ctxt->op) {
case BLE_GATT_ACCESS_OP_READ_DSC: {
ble_gap_conn_find(conn_handle, &peerInfo.m_desc);
Expand Down
6 changes: 5 additions & 1 deletion src/NimBLEEddystoneTLM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ void NimBLEEddystoneTLM::setData(const std::string &data) {
* @param [in] l_uuid The UUID.
*/
void NimBLEEddystoneTLM::setUUID(const NimBLEUUID &l_uuid) {
beaconUUID = l_uuid.getNative()->u16.value;
if (l_uuid.bitSize() != 16) {
NIMBLE_LOGE(LOG_TAG, "UUID must be 16 bits");
return;
}
beaconUUID = *reinterpret_cast<const uint16_t*>(l_uuid.getValue());
} // setUUID


Expand Down
6 changes: 5 additions & 1 deletion src/NimBLEEddystoneURL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ void NimBLEEddystoneURL::setData(const std::string &data) {
* @param [in] l_uuid The UUID.
*/
void NimBLEEddystoneURL::setUUID(const NimBLEUUID &l_uuid) {
beaconUUID = l_uuid.getNative()->u16.value;
if (l_uuid.bitSize() != 16) {
NIMBLE_LOGE(LOG_TAG, "UUID must be 16 bits");
return;
}
beaconUUID = *reinterpret_cast<const uint16_t*>(l_uuid.getValue());
} // setUUID


Expand Down
22 changes: 5 additions & 17 deletions src/NimBLEExtAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,21 +761,9 @@ void NimBLEExtAdvertisement::setServices(const bool complete, const uint8_t size
for(auto &it : v_uuid){
if(it.bitSize() != size) {
NIMBLE_LOGE(LOG_TAG, "Service UUID(%d) invalid", size);
return;
continue;
} 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;
}
uuids += std::string(reinterpret_cast<const char*>(it.getValue()), size / 8);
}
}

Expand All @@ -795,23 +783,23 @@ void NimBLEExtAdvertisement::setServiceData(const NimBLEUUID &uuid, const std::s
// [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);
addData(std::string(cdata, 2) + std::string(reinterpret_cast<const char*>(uuid.getValue()), 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);
addData(std::string(cdata, 2) + std::string(reinterpret_cast<const char*>(uuid.getValue()), 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);
addData(std::string(cdata, 2) + std::string(reinterpret_cast<const char*>(uuid.getValue()), 16) + data);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/NimBLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ int NimBLERemoteCharacteristic::descriptorDiscCB(uint16_t conn_handle,
switch (rc) {
case 0: {
if (uuid_filter != nullptr) {
if (ble_uuid_cmp(&uuid_filter->getNative()->u, &dsc->uuid.u) != 0) {
if (ble_uuid_cmp(uuid_filter->getBase(), &dsc->uuid.u) != 0) {
return 0;
} else {
rc = BLE_HS_EDONE;
Expand Down
2 changes: 1 addition & 1 deletion src/NimBLERemoteService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID *uuid_filter)
rc = ble_gattc_disc_chrs_by_uuid(m_pClient->getConnId(),
m_startHandle,
m_endHandle,
&uuid_filter->getNative()->u,
uuid_filter->getBase(),
NimBLERemoteService::characteristicDiscCB,
&taskData);
}
Expand Down
4 changes: 2 additions & 2 deletions src/NimBLEServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void NimBLEServer::start() {
// with Notify / Indicate capabilities for event handling
for(auto &svc : m_svcVec) {
if(svc->m_removed == 0) {
rc = ble_gatts_find_svc(&svc->getUUID().getNative()->u, &svc->m_handle);
rc = ble_gatts_find_svc(svc->getUUID().getBase(), &svc->m_handle);
if(rc != 0) {
NIMBLE_LOGW(LOG_TAG, "GATT Server started without service: %s, Service %s",
svc->getUUID().toString().c_str(), svc->isStarted() ? "missing" : "not started");
Expand Down Expand Up @@ -412,7 +412,7 @@ std::string NimBLEServer::getPeerNameInternal(uint16_t conn_handle, TaskHandle_t
int rc = ble_gattc_read_by_uuid(conn_handle,
1,
0xffff,
((ble_uuid_t*)&uuid),
&uuid.u,
NimBLEServer::peerNameCB,
taskData);
if (rc != 0) {
Expand Down
8 changes: 4 additions & 4 deletions src/NimBLEService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ bool NimBLEService::start() {
ble_gatt_dsc_def* pDsc_a = nullptr;

svc[0].type = BLE_GATT_SVC_TYPE_PRIMARY;
svc[0].uuid = &m_uuid.getNative()->u;
svc[0].uuid = m_uuid.getBase();
svc[0].includes = NULL;

int removedCount = 0;
Expand Down Expand Up @@ -194,7 +194,7 @@ bool NimBLEService::start() {
if((*dsc_it)->m_removed > 0) {
continue;
}
pDsc_a[d].uuid = &(*dsc_it)->m_uuid.getNative()->u;
pDsc_a[d].uuid = (*dsc_it)->m_uuid.getBase();
pDsc_a[d].att_flags = (*dsc_it)->m_properties;
pDsc_a[d].min_key_size = 0;
pDsc_a[d].access_cb = NimBLEDescriptor::handleGapEvent;
Expand All @@ -206,7 +206,7 @@ bool NimBLEService::start() {
pChr_a[i].descriptors = pDsc_a;
}

pChr_a[i].uuid = &(*chr_it)->m_uuid.getNative()->u;
pChr_a[i].uuid = (*chr_it)->m_uuid.getBase();
pChr_a[i].access_cb = NimBLECharacteristic::handleGapEvent;
pChr_a[i].arg = (*chr_it);
pChr_a[i].flags = (*chr_it)->m_properties;
Expand Down Expand Up @@ -248,7 +248,7 @@ bool NimBLEService::start() {
*/
uint16_t NimBLEService::getHandle() {
if (m_handle == NULL_HANDLE) {
ble_gatts_find_svc(&getUUID().getNative()->u, &m_handle);
ble_gatts_find_svc(getUUID().getBase(), &m_handle);
}
return m_handle;
} // getHandle
Expand Down
Loading

0 comments on commit 8f75994

Please sign in to comment.