Skip to content

Commit 8eb8f8e

Browse files
committed
Add secondary service capability.
1 parent 933c55c commit 8eb8f8e

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/NimBLEService.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ NimBLEService::NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLE
5252
m_numHandles = numHandles;
5353
m_pSvcDef = nullptr;
5454
m_removed = 0;
55+
m_secondary = false;
5556

5657
} // NimBLEService
5758

@@ -130,7 +131,7 @@ bool NimBLEService::start() {
130131
ble_gatt_chr_def* pChr_a = nullptr;
131132
ble_gatt_dsc_def* pDsc_a = nullptr;
132133

133-
svc[0].type = BLE_GATT_SVC_TYPE_PRIMARY;
134+
svc[0].type = m_secondary ? BLE_GATT_SVC_TYPE_SECONDARY : BLE_GATT_SVC_TYPE_PRIMARY;
134135
svc[0].uuid = &m_uuid.getNative()->u;
135136
svc[0].includes = NULL;
136137

@@ -237,6 +238,12 @@ bool NimBLEService::start() {
237238

238239
}
239240

241+
if(m_secSvcVec.size() > 0){
242+
for(auto& it : m_secSvcVec) {
243+
it->start();
244+
}
245+
}
246+
240247
NIMBLE_LOGD(LOG_TAG, "<< start()");
241248
return true;
242249
} // start
@@ -251,6 +258,44 @@ uint16_t NimBLEService::getHandle() {
251258
} // getHandle
252259

253260

261+
/**
262+
* @brief Creates a BLE service as a secondary service to the service this was called from.
263+
* @param [in] uuid The UUID of the secondary service.
264+
* @return A reference to the new secondary service object.
265+
*/
266+
NimBLEService* NimBLEService::createService(const NimBLEUUID &uuid) {
267+
NIMBLE_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str());
268+
269+
NimBLEServer* pServer = getServer();
270+
NimBLEService* pService = new NimBLEService(uuid, 0, pServer);
271+
m_secSvcVec.push_back(pService);
272+
pService->m_secondary = true;
273+
pServer->serviceChanged();
274+
275+
NIMBLE_LOGD(LOG_TAG, "<< createService");
276+
return pService;
277+
}
278+
279+
280+
/**
281+
* @brief Adds a secondary service to this service which was either already created but removed from availability,\n
282+
* or created and later added.
283+
* @param [in] service The secondary service object to add.
284+
*/
285+
void NimBLEService::addService(NimBLEService* service) {
286+
// If adding a service that was not removed add it and return.
287+
// Else reset GATT and send service changed notification.
288+
if(service->m_removed == 0) {
289+
m_secSvcVec.push_back(service);
290+
return;
291+
}
292+
293+
service->m_secondary = true;
294+
service->m_removed = 0;
295+
getServer()->serviceChanged();
296+
}
297+
298+
254299
/**
255300
* @brief Create a new BLE Characteristic associated with this service.
256301
* @param [in] uuid - The UUID of the characteristic.

src/NimBLEService.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class NimBLEService {
6767
std::vector<NimBLECharacteristic*> getCharacteristics(const char* uuid);
6868
std::vector<NimBLECharacteristic*> getCharacteristics(const NimBLEUUID &uuid);
6969

70+
void addService(NimBLEService* service);
71+
NimBLEService* createService(const NimBLEUUID &uuid);
72+
7073

7174
private:
7275

@@ -79,7 +82,9 @@ class NimBLEService {
7982
uint16_t m_numHandles;
8083
ble_gatt_svc_def* m_pSvcDef;
8184
uint8_t m_removed;
85+
bool m_secondary;
8286
std::vector<NimBLECharacteristic*> m_chrVec;
87+
std::vector<NimBLEService*> m_secSvcVec;
8388

8489
}; // NimBLEService
8590

0 commit comments

Comments
 (0)