Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
h2zero committed Jul 16, 2024
1 parent 21e1217 commit 75dda86
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 237 deletions.
45 changes: 14 additions & 31 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,7 +486,7 @@ 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;
Expand All @@ -504,11 +501,10 @@ bool NimBLEAdvertising::start(uint32_t duration, advCompleteCB_t advCompleteCB,
return false;
}
memcpy((void*)&m_advData.uuids16[m_advData.num_uuids16],
&it.getNative()->u16, sizeof(ble_uuid16_t));
(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;
Expand All @@ -523,11 +519,10 @@ bool NimBLEAdvertising::start(uint32_t duration, advCompleteCB_t advCompleteCB,
return false;
}
memcpy((void*)&m_advData.uuids32[m_advData.num_uuids32],
&it.getNative()->u32, sizeof(ble_uuid32_t));
(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;
Expand All @@ -542,7 +537,7 @@ bool NimBLEAdvertising::start(uint32_t duration, advCompleteCB_t advCompleteCB,
return false;
}
memcpy((void*)&m_advData.uuids128[m_advData.num_uuids128],
&it.getNative()->u128, sizeof(ble_uuid128_t));
(ble_uuid128_t*)&it, sizeof(ble_uuid128_t));
m_advData.uuids128_is_complete = 1;
m_advData.num_uuids128++;
}
Expand Down Expand Up @@ -967,21 +962,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((char*)it.getValue(), size / 8);
}
}

Expand All @@ -1001,23 +984,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((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((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((char*)uuid.getValue(), 16) + data);
break;
}

Expand Down
4 changes: 1 addition & 3 deletions src/NimBLEBeacon.cpp
Original file line number Diff line number Diff line change
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 @@ -816,7 +816,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 = *(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 = *(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 @@ -768,21 +768,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((char*)it.getValue(), size / 8);
}
}

Expand All @@ -802,23 +790,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((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((char*)(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((char*)(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
2 changes: 1 addition & 1 deletion src/NimBLEServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,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
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 75dda86

Please sign in to comment.