Skip to content

Commit 9abb7dd

Browse files
committed
Add NimBLEServer::sendServiceChangedIndication
This reverts changing NimBLEServer::serviceChanged from private to public and adds a new function that is more user firendly to indicate to clients that the services should be re-discovered. * Renames serviceChanged to setServiceChanged to better indicate its function.
1 parent 9b2cadc commit 9abb7dd

4 files changed

Lines changed: 19 additions & 11 deletions

File tree

src/NimBLECharacteristic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void NimBLECharacteristic::addDescriptor(NimBLEDescriptor* pDescriptor) {
132132
}
133133

134134
pDescriptor->setCharacteristic(this);
135-
NimBLEDevice::getServer()->serviceChanged();
135+
NimBLEDevice::getServer()->setServiceChanged();
136136
}
137137

138138
/**
@@ -159,7 +159,7 @@ void NimBLECharacteristic::removeDescriptor(NimBLEDescriptor* pDescriptor, bool
159159
}
160160

161161
pDescriptor->setRemoved(deleteDsc ? NIMBLE_ATT_REMOVE_DELETE : NIMBLE_ATT_REMOVE_HIDE);
162-
NimBLEDevice::getServer()->serviceChanged();
162+
NimBLEDevice::getServer()->setServiceChanged();
163163
} // removeDescriptor
164164

165165
/**

src/NimBLEServer.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ NimBLEService* NimBLEServer::createService(const char* uuid) {
9898
NimBLEService* NimBLEServer::createService(const NimBLEUUID& uuid) {
9999
NimBLEService* pService = new NimBLEService(uuid);
100100
m_svcVec.push_back(pService);
101-
serviceChanged();
102-
101+
setServiceChanged();
103102
return pService;
104103
} // createService
105104

@@ -187,12 +186,20 @@ NimBLEAdvertising* NimBLEServer::getAdvertising() const {
187186
* @brief Called when the services are added/removed and sets a flag to indicate they should be reloaded.
188187
* @details This has no effect if the GATT server was not already started.
189188
*/
190-
void NimBLEServer::serviceChanged() {
189+
void NimBLEServer::setServiceChanged() {
191190
if (m_gattsStarted) {
192191
m_svcChanged = true;
193192
}
194193
} // serviceChanged
195194

195+
/**
196+
* @brief Send a service changed indication to all clients.
197+
* @details This should be called when services are added, removed or modified after the server has been started.
198+
*/
199+
void NimBLEServer::sendServiceChangedIndication() const {
200+
ble_svc_gatt_changed(0x0001, 0xffff);
201+
}
202+
196203
/**
197204
* @brief Callback for GATT registration events,
198205
* used to obtain the assigned handles for services, characteristics, and descriptors.
@@ -313,7 +320,7 @@ bool NimBLEServer::start() {
313320
// If the services have changed indicate it now
314321
if (m_svcChanged) {
315322
m_svcChanged = false;
316-
ble_svc_gatt_changed(0x0001, 0xffff);
323+
sendServiceChangedIndication();
317324
}
318325

319326
m_gattsStarted = true;
@@ -836,7 +843,7 @@ void NimBLEServer::removeService(NimBLEService* service, bool deleteSvc) {
836843
}
837844

838845
service->setRemoved(deleteSvc ? NIMBLE_ATT_REMOVE_DELETE : NIMBLE_ATT_REMOVE_HIDE);
839-
serviceChanged();
846+
setServiceChanged();
840847
# if !MYNEWT_VAL(BLE_EXT_ADV) && MYNEWT_VAL(BLE_ROLE_BROADCASTER)
841848
NimBLEDevice::getAdvertising()->removeServiceUUID(service->getUUID());
842849
# endif
@@ -863,7 +870,7 @@ void NimBLEServer::addService(NimBLEService* service) {
863870
}
864871

865872
service->setRemoved(0);
866-
serviceChanged();
873+
setServiceChanged();
867874
} // addService
868875

869876
/**

src/NimBLEServer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class NimBLEServer {
8484
void setDataLen(uint16_t connHandle, uint16_t tx_octets) const;
8585
bool updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions);
8686
bool getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy);
87+
void sendServiceChangedIndication() const;
8788

8889
# if MYNEWT_VAL(BLE_ROLE_CENTRAL)
8990
NimBLEClient* getClient(uint16_t connHandle);
@@ -122,7 +123,7 @@ class NimBLEServer {
122123
static int handleGapEvent(struct ble_gap_event* event, void* arg);
123124
static int handleGattEvent(uint16_t connHandle, uint16_t attrHandle, ble_gatt_access_ctxt* ctxt, void* arg);
124125
static void gattRegisterCallback(struct ble_gatt_register_ctxt* ctxt, void* arg);
125-
void serviceChanged();
126+
void setServiceChanged();
126127
bool resetGATT();
127128

128129
bool m_gattsStarted : 1;

src/NimBLEService.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ void NimBLEService::addCharacteristic(NimBLECharacteristic* pChar) {
245245
}
246246

247247
pChar->setService(this);
248-
getServer()->serviceChanged();
248+
getServer()->setServiceChanged();
249249
} // addCharacteristic
250250

251251
/**
@@ -272,7 +272,7 @@ void NimBLEService::removeCharacteristic(NimBLECharacteristic* pChar, bool delet
272272
}
273273

274274
pChar->setRemoved(deleteChr ? NIMBLE_ATT_REMOVE_DELETE : NIMBLE_ATT_REMOVE_HIDE);
275-
getServer()->serviceChanged();
275+
getServer()->setServiceChanged();
276276
} // removeCharacteristic
277277

278278
/**

0 commit comments

Comments
 (0)