Skip to content

Commit

Permalink
Add secondary services as an include on the primary.
Browse files Browse the repository at this point in the history
  • Loading branch information
h2zero committed Sep 13, 2021
1 parent 79f442d commit 4c8a13a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/NimBLEService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ NimBLEService::NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLE
m_pSvcDef = nullptr;
m_removed = 0;
m_secondary = false;
m_pSecSvcDef = nullptr;

} // NimBLEService

Expand All @@ -71,6 +72,13 @@ NimBLEService::~NimBLEService() {
delete(m_pSvcDef);
}

if(m_pSecSvcDef != nullptr) {
for(auto &it : m_secSvcVec) {
delete it;
}
delete m_pSecSvcDef;
}

for(auto &it : m_chrVec) {
delete it;
}
Expand Down Expand Up @@ -223,6 +231,19 @@ bool NimBLEService::start() {
// end of services must indicate to api with type = 0
svc[1].type = 0;
m_pSvcDef = svc;

if(m_secSvcVec.size() > 0){
size_t numSecSvcs = m_secSvcVec.size();
ble_gatt_svc_def** m_pSecSvcDef = new ble_gatt_svc_def*[numSecSvcs + 1];
int i = 0;
for(auto& it : m_secSvcVec) {
it->start();
m_pSecSvcDef[i] = it->m_pSvcDef;
++i;
}
m_pSecSvcDef[numSecSvcs] = nullptr;
m_pSvcDef->includes = (const ble_gatt_svc_def**)m_pSecSvcDef;
}
}

int rc = ble_gatts_count_cfg((const ble_gatt_svc_def*)m_pSvcDef);
Expand All @@ -235,13 +256,6 @@ bool NimBLEService::start() {
if (rc != 0) {
NIMBLE_LOGE(LOG_TAG, "ble_gatts_add_svcs, rc= %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
return false;

}

if(m_secSvcVec.size() > 0){
for(auto& it : m_secSvcVec) {
it->start();
}
}

NIMBLE_LOGD(LOG_TAG, "<< start()");
Expand Down
1 change: 1 addition & 0 deletions src/NimBLEService.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class NimBLEService {
ble_gatt_svc_def* m_pSvcDef;
uint8_t m_removed;
bool m_secondary;
ble_gatt_svc_def** m_pSecSvcDef;
std::vector<NimBLECharacteristic*> m_chrVec;
std::vector<NimBLEService*> m_secSvcVec;

Expand Down

0 comments on commit 4c8a13a

Please sign in to comment.