Skip to content

Fix for issue 3974 m_connectedCount incorrectly decremented when no connection exists #4035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions libraries/BLE/src/BLEServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,19 @@ void BLEServer::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t
// If we receive a disconnect event then invoke the callback for disconnects (if one is present).
// we also want to start advertising again.
case ESP_GATTS_DISCONNECT_EVT: {
m_connectedCount--; // Decrement the number of connected devices count.
if (m_pServerCallbacks != nullptr) { // If we have callbacks, call now.
m_pServerCallbacks->onDisconnect(this);
}
startAdvertising(); //- do this with some delay from the loop()
removePeerDevice(param->disconnect.conn_id, false);
break;
if(m_connId == ESP_GATT_IF_NONE) {
return;
}

// only decrement if connection is found in map and removed
// sometimes this event triggers w/o a valid connection
if(removePeerDevice(param->disconnect.conn_id, false)) {
m_connectedCount--; // Decrement the number of connected devices count.
}
break;
} // ESP_GATTS_DISCONNECT_EVT


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

void BLEServer::removePeerDevice(uint16_t conn_id, bool _client) {
m_connectedServersMap.erase(conn_id);
bool BLEServer::removePeerDevice(uint16_t conn_id, bool _client) {
return m_connectedServersMap.erase(conn_id) > 0;
}
/* multi connect support */

Expand Down
2 changes: 1 addition & 1 deletion libraries/BLE/src/BLEServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class BLEServer {
/* multi connection support */
std::map<uint16_t, conn_status_t> getPeerDevices(bool client);
void addPeerDevice(void* peer, bool is_client, uint16_t conn_id);
void removePeerDevice(uint16_t conn_id, bool client);
bool removePeerDevice(uint16_t conn_id, bool client);
BLEServer* getServerByConnId(uint16_t conn_id);
void updatePeerMTU(uint16_t connId, uint16_t mtu);
uint16_t getPeerMTU(uint16_t conn_id);
Expand Down