From 4c8a13afe4965b37118d2f11e030917514e48e85 Mon Sep 17 00:00:00 2001 From: h2zero Date: Mon, 13 Sep 2021 08:39:59 -0600 Subject: [PATCH] Add secondary services as an include on the primary. --- src/NimBLEService.cpp | 28 +++++++++++++++++++++------- src/NimBLEService.h | 1 + 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/NimBLEService.cpp b/src/NimBLEService.cpp index 987b3e4..6231771 100644 --- a/src/NimBLEService.cpp +++ b/src/NimBLEService.cpp @@ -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 @@ -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; } @@ -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); @@ -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()"); diff --git a/src/NimBLEService.h b/src/NimBLEService.h index 8c7f4e3..1bcf935 100644 --- a/src/NimBLEService.h +++ b/src/NimBLEService.h @@ -83,6 +83,7 @@ private: ble_gatt_svc_def* m_pSvcDef; uint8_t m_removed; bool m_secondary; + ble_gatt_svc_def** m_pSecSvcDef; std::vector m_chrVec; std::vector m_secSvcVec;