Skip to content

Commit 35d9759

Browse files
authored
Fix for issue 3974 m_connectedCount incorrectly decremented when no connection exists
There is no need to decrement if nothing was removed from removePeerDevice Reference issue: #3974
1 parent 09bff50 commit 35d9759

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

Diff for: libraries/BLE/src/BLEServer.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,19 @@ void BLEServer::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t
202202
// If we receive a disconnect event then invoke the callback for disconnects (if one is present).
203203
// we also want to start advertising again.
204204
case ESP_GATTS_DISCONNECT_EVT: {
205-
m_connectedCount--; // Decrement the number of connected devices count.
206205
if (m_pServerCallbacks != nullptr) { // If we have callbacks, call now.
207206
m_pServerCallbacks->onDisconnect(this);
208207
}
209-
startAdvertising(); //- do this with some delay from the loop()
210-
removePeerDevice(param->disconnect.conn_id, false);
211-
break;
208+
if(m_connId == ESP_GATT_IF_NONE) {
209+
return;
210+
}
211+
212+
// only decrement if connection is found in map and removed
213+
// sometimes this event triggers w/o a valid connection
214+
if(removePeerDevice(param->disconnect.conn_id, false)) {
215+
m_connectedCount--; // Decrement the number of connected devices count.
216+
}
217+
break;
212218
} // ESP_GATTS_DISCONNECT_EVT
213219

214220

@@ -395,8 +401,8 @@ void BLEServer::addPeerDevice(void* peer, bool _client, uint16_t conn_id) {
395401
m_connectedServersMap.insert(std::pair<uint16_t, conn_status_t>(conn_id, status));
396402
}
397403

398-
void BLEServer::removePeerDevice(uint16_t conn_id, bool _client) {
399-
m_connectedServersMap.erase(conn_id);
404+
bool BLEServer::removePeerDevice(uint16_t conn_id, bool _client) {
405+
return m_connectedServersMap.erase(conn_id) > 0;
400406
}
401407
/* multi connect support */
402408

Diff for: libraries/BLE/src/BLEServer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class BLEServer {
7979
/* multi connection support */
8080
std::map<uint16_t, conn_status_t> getPeerDevices(bool client);
8181
void addPeerDevice(void* peer, bool is_client, uint16_t conn_id);
82-
void removePeerDevice(uint16_t conn_id, bool client);
82+
bool removePeerDevice(uint16_t conn_id, bool client);
8383
BLEServer* getServerByConnId(uint16_t conn_id);
8484
void updatePeerMTU(uint16_t connId, uint16_t mtu);
8585
uint16_t getPeerMTU(uint16_t conn_id);

0 commit comments

Comments
 (0)