@@ -269,14 +269,18 @@ void NimBLEServer::gattRegisterCallback(ble_gatt_register_ctxt* ctxt, void* arg)
269269 * @details Required to be called after setup of all services and characteristics / descriptors
270270 * for the NimBLE host to register them.
271271 */
272- void NimBLEServer::start () {
272+ bool NimBLEServer::start () {
273273 if (m_svcChanged && !getConnectedCount ()) {
274274 NIMBLE_LOGD (LOG_TAG, " Services have changed since last start, resetting GATT server" );
275- resetGATT () ;
275+ m_gattsStarted = false ;
276276 }
277277
278278 if (m_gattsStarted) {
279- return ; // already started
279+ return true ; // already started
280+ }
281+
282+ if (!resetGATT ()) {
283+ return false ;
280284 }
281285
282286 ble_hs_cfg.gatts_register_cb = NimBLEServer::gattRegisterCallback;
@@ -286,7 +290,7 @@ void NimBLEServer::start() {
286290 int rc = ble_gatts_start ();
287291 if (rc != 0 ) {
288292 NIMBLE_LOGE (LOG_TAG, " ble_gatts_start; rc=%d, %s" , rc, NimBLEUtils::returnCodeToString (rc));
289- return ;
293+ return false ;
290294 }
291295
292296# if MYNEWT_VAL(NIMBLE_CPP_LOG_LEVEL) >= 4
@@ -313,6 +317,7 @@ void NimBLEServer::start() {
313317 }
314318
315319 m_gattsStarted = true ;
320+ return true ;
316321} // start
317322
318323/* *
@@ -335,7 +340,7 @@ bool NimBLEServer::disconnect(uint16_t connHandle, uint8_t reason) const {
335340 * @brief Disconnect the specified client with optional reason.
336341 * @param [in] connInfo Connection of the client to disconnect.
337342 * @param [in] reason code for disconnecting.
338- * @return NimBLE host return code .
343+ * @return True if successful .
339344 */
340345bool NimBLEServer::disconnect (const NimBLEConnInfo& connInfo, uint8_t reason) const {
341346 return disconnect (connInfo.getConnHandle (), reason);
@@ -505,7 +510,7 @@ int NimBLEServer::handleGapEvent(ble_gap_event* event, void* arg) {
505510
506511 peerInfo.m_desc = event->disconnect .conn ;
507512 pServer->m_pServerCallbacks ->onDisconnect (pServer, peerInfo, event->disconnect .reason );
508- # if !MYNEWT_VAL(BLE_EXT_ADV)
513+ # if !MYNEWT_VAL(BLE_EXT_ADV) && MYNEWT_VAL(BLE_ROLE_BROADCASTER)
509514 if (pServer->m_advertiseOnDisconnect ) {
510515 pServer->startAdvertising ();
511516 }
@@ -863,8 +868,13 @@ void NimBLEServer::addService(NimBLEService* service) {
863868
864869/* *
865870 * @brief Resets the GATT server, used when services are added/removed after initialization.
871+ * @return True if successful.
872+ * @details This will reset the GATT server and re-register all services, characteristics, and
873+ * descriptors that have not been removed. Services, characteristics, and descriptors that have been
874+ * removed but not deleted will be skipped and have their handles cleared, and those that have been
875+ * deleted will be removed from the server's service vector.
866876 */
867- void NimBLEServer::resetGATT () {
877+ bool NimBLEServer::resetGATT () {
868878# if MYNEWT_VAL(BLE_ROLE_BROADCASTER)
869879 NimBLEDevice::stopAdvertising ();
870880# endif
@@ -873,8 +883,6 @@ void NimBLEServer::resetGATT() {
873883 ble_svc_gap_init ();
874884 ble_svc_gatt_init ();
875885
876- m_gattsStarted = false ;
877-
878886 for (auto svcIt = m_svcVec.begin (); svcIt != m_svcVec.end ();) {
879887 auto * pSvc = *svcIt;
880888 if (pSvc->getRemoved () == NIMBLE_ATT_REMOVE_DELETE) {
@@ -908,12 +916,17 @@ void NimBLEServer::resetGATT() {
908916 }
909917
910918 if (pSvc->getRemoved () == 0 ) {
911- pSvc->start ();
919+ if (!pSvc->start_internal ()) {
920+ NIMBLE_LOGE (LOG_TAG, " Failed to start service: %s" , pSvc->getUUID ().toString ().c_str ());
921+ return false ;
922+ }
912923 }
913924
914925 pSvc->m_handle = 0 ;
915926 ++svcIt;
916927 }
928+
929+ return true ;
917930} // resetGATT
918931
919932/* *
0 commit comments