diff --git a/src/NimBLECharacteristic.cpp b/src/NimBLECharacteristic.cpp index f3c5cebf..f4406347 100644 --- a/src/NimBLECharacteristic.cpp +++ b/src/NimBLECharacteristic.cpp @@ -132,7 +132,7 @@ void NimBLECharacteristic::addDescriptor(NimBLEDescriptor* pDescriptor) { } pDescriptor->setCharacteristic(this); - NimBLEDevice::getServer()->serviceChanged(); + NimBLEDevice::getServer()->setServiceChanged(); } /** @@ -159,7 +159,7 @@ void NimBLECharacteristic::removeDescriptor(NimBLEDescriptor* pDescriptor, bool } pDescriptor->setRemoved(deleteDsc ? NIMBLE_ATT_REMOVE_DELETE : NIMBLE_ATT_REMOVE_HIDE); - NimBLEDevice::getServer()->serviceChanged(); + NimBLEDevice::getServer()->setServiceChanged(); } // removeDescriptor /** diff --git a/src/NimBLEServer.cpp b/src/NimBLEServer.cpp index bcbec9a3..6e39ab1c 100644 --- a/src/NimBLEServer.cpp +++ b/src/NimBLEServer.cpp @@ -98,8 +98,7 @@ NimBLEService* NimBLEServer::createService(const char* uuid) { NimBLEService* NimBLEServer::createService(const NimBLEUUID& uuid) { NimBLEService* pService = new NimBLEService(uuid); m_svcVec.push_back(pService); - serviceChanged(); - + setServiceChanged(); return pService; } // createService @@ -187,12 +186,20 @@ NimBLEAdvertising* NimBLEServer::getAdvertising() const { * @brief Called when the services are added/removed and sets a flag to indicate they should be reloaded. * @details This has no effect if the GATT server was not already started. */ -void NimBLEServer::serviceChanged() { +void NimBLEServer::setServiceChanged() { if (m_gattsStarted) { m_svcChanged = true; } } // serviceChanged +/** + * @brief Send a service changed indication to all clients. + * @details This should be called when services are added, removed or modified after the server has been started. + */ +void NimBLEServer::sendServiceChangedIndication() const { + ble_svc_gatt_changed(0x0001, 0xffff); +} + /** * @brief Callback for GATT registration events, * used to obtain the assigned handles for services, characteristics, and descriptors. @@ -313,7 +320,7 @@ bool NimBLEServer::start() { // If the services have changed indicate it now if (m_svcChanged) { m_svcChanged = false; - ble_svc_gatt_changed(0x0001, 0xffff); + sendServiceChangedIndication(); } m_gattsStarted = true; @@ -836,7 +843,7 @@ void NimBLEServer::removeService(NimBLEService* service, bool deleteSvc) { } service->setRemoved(deleteSvc ? NIMBLE_ATT_REMOVE_DELETE : NIMBLE_ATT_REMOVE_HIDE); - serviceChanged(); + setServiceChanged(); # if !MYNEWT_VAL(BLE_EXT_ADV) && MYNEWT_VAL(BLE_ROLE_BROADCASTER) NimBLEDevice::getAdvertising()->removeServiceUUID(service->getUUID()); # endif @@ -863,7 +870,7 @@ void NimBLEServer::addService(NimBLEService* service) { } service->setRemoved(0); - serviceChanged(); + setServiceChanged(); } // addService /** diff --git a/src/NimBLEServer.h b/src/NimBLEServer.h index 8e2504d0..a5171d2e 100644 --- a/src/NimBLEServer.h +++ b/src/NimBLEServer.h @@ -84,7 +84,7 @@ class NimBLEServer { void setDataLen(uint16_t connHandle, uint16_t tx_octets) const; bool updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions); bool getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy); - void serviceChanged(); + void sendServiceChangedIndication() const; # if MYNEWT_VAL(BLE_ROLE_CENTRAL) NimBLEClient* getClient(uint16_t connHandle); @@ -123,6 +123,7 @@ class NimBLEServer { static int handleGapEvent(struct ble_gap_event* event, void* arg); static int handleGattEvent(uint16_t connHandle, uint16_t attrHandle, ble_gatt_access_ctxt* ctxt, void* arg); static void gattRegisterCallback(struct ble_gatt_register_ctxt* ctxt, void* arg); + void setServiceChanged(); bool resetGATT(); bool m_gattsStarted : 1; diff --git a/src/NimBLEService.cpp b/src/NimBLEService.cpp index aebe59fd..c266f164 100644 --- a/src/NimBLEService.cpp +++ b/src/NimBLEService.cpp @@ -245,7 +245,7 @@ void NimBLEService::addCharacteristic(NimBLECharacteristic* pChar) { } pChar->setService(this); - getServer()->serviceChanged(); + getServer()->setServiceChanged(); } // addCharacteristic /** @@ -272,7 +272,7 @@ void NimBLEService::removeCharacteristic(NimBLECharacteristic* pChar, bool delet } pChar->setRemoved(deleteChr ? NIMBLE_ATT_REMOVE_DELETE : NIMBLE_ATT_REMOVE_HIDE); - getServer()->serviceChanged(); + getServer()->setServiceChanged(); } // removeCharacteristic /**