Skip to content

Commit 39b752d

Browse files
committed
Add secondary service capability.
1 parent e238a18 commit 39b752d

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
@@ -55,6 +55,7 @@ NimBLEService::NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLE
5555
m_numHandles = numHandles;
5656
m_pSvcDef = nullptr;
5757
m_removed = 0;
58+
m_secondary = false;
5859

5960
} // NimBLEService
6061

@@ -133,7 +134,7 @@ bool NimBLEService::start() {
133134
ble_gatt_chr_def* pChr_a = nullptr;
134135
ble_gatt_dsc_def* pDsc_a = nullptr;
135136

136-
svc[0].type = BLE_GATT_SVC_TYPE_PRIMARY;
137+
svc[0].type = m_secondary ? BLE_GATT_SVC_TYPE_SECONDARY : BLE_GATT_SVC_TYPE_PRIMARY;
137138
svc[0].uuid = &m_uuid.getNative()->u;
138139
svc[0].includes = NULL;
139140

@@ -240,6 +241,12 @@ bool NimBLEService::start() {
240241

241242
}
242243

244+
if(m_secSvcVec.size() > 0){
245+
for(auto& it : m_secSvcVec) {
246+
it->start();
247+
}
248+
}
249+
243250
NIMBLE_LOGD(LOG_TAG, "<< start()");
244251
return true;
245252
} // start
@@ -254,6 +261,44 @@ uint16_t NimBLEService::getHandle() {
254261
} // getHandle
255262

256263

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

72+
void addService(NimBLEService* service);
73+
NimBLEService* createService(const NimBLEUUID &uuid);
74+
7275

7376
private:
7477

@@ -81,7 +84,9 @@ class NimBLEService {
8184
uint16_t m_numHandles;
8285
ble_gatt_svc_def* m_pSvcDef;
8386
uint8_t m_removed;
87+
bool m_secondary;
8488
std::vector<NimBLECharacteristic*> m_chrVec;
89+
std::vector<NimBLEService*> m_secSvcVec;
8590

8691
}; // NimBLEService
8792

0 commit comments

Comments
 (0)