From 456ef65041d5e5ec5b2a0f886b53cd698159f94c Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Mon, 11 Sep 2023 20:31:23 +0530 Subject: [PATCH 1/3] [ESP32] Fix the threading issue in nimble Send ble connection error than executing in nimble thread context --- src/platform/ESP32/nimble/BLEManagerImpl.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/platform/ESP32/nimble/BLEManagerImpl.cpp b/src/platform/ESP32/nimble/BLEManagerImpl.cpp index 66a4d29df768d4..fe15b00c6273dd 100644 --- a/src/platform/ESP32/nimble/BLEManagerImpl.cpp +++ b/src/platform/ESP32/nimble/BLEManagerImpl.cpp @@ -1288,7 +1288,12 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(struct ble_gap_event * gapEvent) disconReason = BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT; break; } - HandleConnectionError(gapEvent->disconnect.conn.conn_handle, disconReason); + + ChipDeviceEvent connectionErrorEvent; + connectionErrorEvent.Type = DeviceEventType::kCHIPoBLEConnectionError; + connectionErrorEvent.CHIPoBLEConnectionError.ConId = gapEvent->disconnect.conn.conn_handle; + connectionErrorEvent.CHIPoBLEConnectionError.Reason = disconReason; + ReturnErrorOnFailure(PlatformMgr().PostEvent(&connectionErrorEvent)); } ChipDeviceEvent disconnectEvent; From 4690942e4ab35110c76878f77e416b08b5929d0c Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Wed, 18 Oct 2023 17:36:05 +0530 Subject: [PATCH 2/3] comment explaining why we are posting connection error event --- src/platform/ESP32/nimble/BLEManagerImpl.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/platform/ESP32/nimble/BLEManagerImpl.cpp b/src/platform/ESP32/nimble/BLEManagerImpl.cpp index fe15b00c6273dd..877f49e5fb006c 100644 --- a/src/platform/ESP32/nimble/BLEManagerImpl.cpp +++ b/src/platform/ESP32/nimble/BLEManagerImpl.cpp @@ -1273,6 +1273,8 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(struct ble_gap_event * gapEvent) peer_delete(gapEvent->disconnect.conn.conn_handle); #endif + // There can be a case where the BLE central disconnects without unsubscribing from the BLE characteristic. + // In such situations, it is necessary to clear the subscription and post a connection error event. if (UnsetSubscribed(gapEvent->disconnect.conn.conn_handle)) { CHIP_ERROR disconReason; From 24537812d90480d2fc61934299894de65303aacd Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Thu, 2 Nov 2023 18:25:25 +0530 Subject: [PATCH 3/3] Adding a comment for kCHIPoBLEConnectionError --- src/include/platform/CHIPDeviceEvent.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/include/platform/CHIPDeviceEvent.h b/src/include/platform/CHIPDeviceEvent.h index bf4c55ca438377..561cd5f539367b 100644 --- a/src/include/platform/CHIPDeviceEvent.h +++ b/src/include/platform/CHIPDeviceEvent.h @@ -258,6 +258,12 @@ enum InternalEventTypes kCHIPoBLEUnsubscribe, kCHIPoBLEWriteReceived, kCHIPoBLEIndicateConfirm, + + /** + * Post this event in case of a BLE connection error. This event should be posted + * if the BLE central disconnects without unsubscribing from the BLE characteristic. + * This event should populate CHIPoBLEConnectionError structure. + */ kCHIPoBLEConnectionError, kCHIPoBLENotifyConfirm };