Skip to content

Commit

Permalink
[ESP32] Fix the threading issue in nimble
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamdp committed Sep 12, 2023
1 parent ba45f9c commit d71e172
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/platform/ESP32/nimble/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#define CHIP_ADV_DATA_TYPE_FLAGS 0x01
#define CHIP_ADV_DATA_FLAGS 0x06
#define CHIP_ADV_DATA_TYPE_SERVICE_DATA 0x16
#define NIMBLE_CONN_HANDLE_INVALID 0xFFFF

using namespace ::chip;
using namespace ::chip::Ble;
Expand Down Expand Up @@ -376,6 +377,13 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
DriveBLEState();
break;

case DeviceEventType::kCHIPoBLEConnectionClosed:
if (event->CHIPoBLEConnectionError.ConId != NIMBLE_CONN_HANDLE_INVALID)
{
HandleConnectionError(event->CHIPoBLEConnectionError.ConId, event->CHIPoBLEConnectionError.Reason);
}
break;

default:
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
HandlePlatformSpecificBLEEvent(event);
Expand Down Expand Up @@ -819,7 +827,7 @@ void BLEManagerImpl::DriveBLEState(void)

void BLEManagerImpl::bleprph_on_reset(int reason)
{
ESP_LOGE(TAG, "Resetting state; reason=%d\n", reason);
ESP_LOGE(TAG, "Resetting state; reason=%d", reason);
}

CHIP_ERROR BLEManagerImpl::bleprph_set_random_addr(void)
Expand Down Expand Up @@ -1273,6 +1281,13 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(struct ble_gap_event * gapEvent)
peer_delete(gapEvent->disconnect.conn.conn_handle);
#endif

ChipDeviceEvent disconnectEvent;

// Since there is no event data struct available for connection close,
// reusing the connection error struct.
disconnectEvent.CHIPoBLEConnectionError.ConId = NIMBLE_CONN_HANDLE_INVALID;
disconnectEvent.Type = DeviceEventType::kCHIPoBLEConnectionClosed;

if (UnsetSubscribed(gapEvent->disconnect.conn.conn_handle))
{
CHIP_ERROR disconReason;
Expand All @@ -1288,11 +1303,13 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(struct ble_gap_event * gapEvent)
disconReason = BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT;
break;
}
HandleConnectionError(gapEvent->disconnect.conn.conn_handle, disconReason);

// Set the connection handle if unsubscribe is successful. This connection handle is then used in
// _OnPlatformEvent() and HandleConnectionError() is called if its valid.
disconnectEvent.CHIPoBLEConnectionError.ConId = gapEvent->disconnect.conn.conn_handle;
disconnectEvent.CHIPoBLEConnectionError.Reason = disconReason;
}

ChipDeviceEvent disconnectEvent;
disconnectEvent.Type = DeviceEventType::kCHIPoBLEConnectionClosed;
ReturnErrorOnFailure(PlatformMgr().PostEvent(&disconnectEvent));

// Force a reconfiguration of advertising in case we switched to non-connectable mode when
Expand Down Expand Up @@ -1666,7 +1683,6 @@ void BLEManagerImpl::OnDeviceScanned(const struct ble_hs_adv_fields & fields, co
{
if (!mBLEScanConfig.mDiscriminator.MatchesLongDiscriminator(info.GetDeviceDiscriminator()))
{
printf("Discriminator didi not match \n");
return;
}
ChipLogProgress(Ble, "Device Discriminator match. Attempting to connect");
Expand Down

0 comments on commit d71e172

Please sign in to comment.