Skip to content

Commit

Permalink
Wait till NimBLE host-controller is synced
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Patil <[email protected]>
  • Loading branch information
shubhamdp committed Sep 9, 2021
1 parent 0b4b013 commit eaa3021
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/platform/ESP32/nimble/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ const ble_uuid128_t UUID_CHIPoBLEChar_TX = {
{ BLE_UUID_TYPE_128 }, { 0x12, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18 }
};

SemaphoreHandle_t semaphoreHandle = NULL;

} // unnamed namespace

BLEManagerImpl BLEManagerImpl::sInstance;
Expand Down Expand Up @@ -595,9 +597,7 @@ void BLEManagerImpl::bleprph_on_reset(int reason)

void BLEManagerImpl::bleprph_on_sync(void)
{
sInstance.mFlags.Set(Flags::kESPBLELayerInitialized);
sInstance.mFlags.Set(Flags::kGATTServiceStarted);
ESP_LOGI(TAG, "BLE host-controller synced");
xSemaphoreGive(semaphoreHandle);
}

void BLEManagerImpl::bleprph_host_task(void * param)
Expand All @@ -614,6 +614,14 @@ CHIP_ERROR BLEManagerImpl::InitESPBleLayer(void)

VerifyOrExit(!mFlags.Has(Flags::kESPBLELayerInitialized), /* */);

semaphoreHandle = xSemaphoreCreateBinary();
if (semaphoreHandle == NULL)
{
err = CHIP_ERROR_NO_MEMORY;
ESP_LOGE(TAG, "Failed to create semaphore");
ExitNow();
}

for (int i = 0; i < kMaxConnections; i++)
{
mSubscribedConIds[i] = BLE_CONNECTION_UNINITIALIZED;
Expand Down Expand Up @@ -655,6 +663,14 @@ CHIP_ERROR BLEManagerImpl::InitESPBleLayer(void)

nimble_port_freertos_init(bleprph_host_task);

xSemaphoreTake(semaphoreHandle, portMAX_DELAY);
vSemaphoreDelete(semaphoreHandle);
semaphoreHandle = NULL;

sInstance.mFlags.Set(Flags::kESPBLELayerInitialized);
sInstance.mFlags.Set(Flags::kGATTServiceStarted);
ESP_LOGI(TAG, "BLE host-controller synced");

exit:
return err;
}
Expand Down

0 comments on commit eaa3021

Please sign in to comment.