Skip to content

Commit 41acb1a

Browse files
committed
Improve error handling
1 parent 4da6670 commit 41acb1a

3 files changed

Lines changed: 27 additions & 13 deletions

File tree

src/NimBLEAdvertising.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ bool NimBLEAdvertising::start(uint32_t duration, const NimBLEAddress* dirAddr) {
197197

198198
# if MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
199199
NimBLEServer* pServer = NimBLEDevice::getServer();
200-
if (pServer != nullptr) {
201-
pServer->start(); // make sure the GATT server is ready before advertising
200+
if (pServer != nullptr && !pServer->start()) { // make sure the GATT server is ready before advertising
201+
NIMBLE_LOGE(LOG_TAG, "Failed to start GATT server");
202+
return false;
202203
}
203204
# endif
204205

src/NimBLEServer.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,19 @@ 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");
275275
m_gattsStarted = false;
276276
}
277277

278278
if (m_gattsStarted) {
279-
return; // already started
279+
return true; // already started
280280
}
281281

282-
resetGATT();
282+
if (!resetGATT()) {
283+
return false;
284+
}
283285

284286
ble_hs_cfg.gatts_register_cb = NimBLEServer::gattRegisterCallback;
285287
gattRegisterCallbackArgs args{};
@@ -288,7 +290,7 @@ void NimBLEServer::start() {
288290
int rc = ble_gatts_start();
289291
if (rc != 0) {
290292
NIMBLE_LOGE(LOG_TAG, "ble_gatts_start; rc=%d, %s", rc, NimBLEUtils::returnCodeToString(rc));
291-
return;
293+
return false;
292294
}
293295

294296
# if MYNEWT_VAL(NIMBLE_CPP_LOG_LEVEL) >= 4
@@ -315,6 +317,7 @@ void NimBLEServer::start() {
315317
}
316318

317319
m_gattsStarted = true;
320+
return true;
318321
} // start
319322

320323
/**
@@ -337,7 +340,7 @@ bool NimBLEServer::disconnect(uint16_t connHandle, uint8_t reason) const {
337340
* @brief Disconnect the specified client with optional reason.
338341
* @param [in] connInfo Connection of the client to disconnect.
339342
* @param [in] reason code for disconnecting.
340-
* @return NimBLE host return code.
343+
* @return True if successful.
341344
*/
342345
bool NimBLEServer::disconnect(const NimBLEConnInfo& connInfo, uint8_t reason) const {
343346
return disconnect(connInfo.getConnHandle(), reason);
@@ -507,7 +510,7 @@ int NimBLEServer::handleGapEvent(ble_gap_event* event, void* arg) {
507510

508511
peerInfo.m_desc = event->disconnect.conn;
509512
pServer->m_pServerCallbacks->onDisconnect(pServer, peerInfo, event->disconnect.reason);
510-
# if !MYNEWT_VAL(BLE_EXT_ADV)
513+
# if !MYNEWT_VAL(BLE_EXT_ADV) && MYNEWT_VAL(BLE_ROLE_BROADCASTER)
511514
if (pServer->m_advertiseOnDisconnect) {
512515
pServer->startAdvertising();
513516
}
@@ -865,8 +868,13 @@ void NimBLEServer::addService(NimBLEService* service) {
865868

866869
/**
867870
* @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.
868876
*/
869-
void NimBLEServer::resetGATT() {
877+
bool NimBLEServer::resetGATT() {
870878
# if MYNEWT_VAL(BLE_ROLE_BROADCASTER)
871879
NimBLEDevice::stopAdvertising();
872880
# endif
@@ -908,12 +916,17 @@ void NimBLEServer::resetGATT() {
908916
}
909917

910918
if (pSvc->getRemoved() == 0) {
911-
pSvc->start_internal();
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
/**

src/NimBLEServer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class NimBLEClient;
6161
*/
6262
class NimBLEServer {
6363
public:
64-
void start();
64+
bool start();
6565
uint8_t getConnectedCount() const;
6666
bool disconnect(uint16_t connHandle, uint8_t reason = BLE_ERR_REM_USER_CONN_TERM) const;
6767
bool disconnect(const NimBLEConnInfo& connInfo, uint8_t reason = BLE_ERR_REM_USER_CONN_TERM) const;
@@ -123,12 +123,12 @@ class NimBLEServer {
123123
static int handleGattEvent(uint16_t connHandle, uint16_t attrHandle, ble_gatt_access_ctxt* ctxt, void* arg);
124124
static void gattRegisterCallback(struct ble_gatt_register_ctxt* ctxt, void* arg);
125125
void serviceChanged();
126-
void resetGATT();
126+
bool resetGATT();
127127

128128
bool m_gattsStarted : 1;
129129
bool m_svcChanged : 1;
130130
bool m_deleteCallbacks : 1;
131-
# if !MYNEWT_VAL(BLE_EXT_ADV)
131+
# if !MYNEWT_VAL(BLE_EXT_ADV) && MYNEWT_VAL(BLE_ROLE_BROADCASTER)
132132
bool m_advertiseOnDisconnect : 1;
133133
# endif
134134
NimBLEServerCallbacks* m_pServerCallbacks;

0 commit comments

Comments
 (0)