Skip to content

Commit

Permalink
[QPG] Update BLE State machine to not try starting advertisement whil…
Browse files Browse the repository at this point in the history
…e previous start was still in progress (#20219)

Case existed where 2 paths restarted advertising on a FailSafe timeout.
Protection added by adding additional state.
  • Loading branch information
tima-q authored and pull[bot] committed Aug 15, 2022
1 parent 54f36a3 commit 4096259
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/platform/qpg/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
// Force the advertising state to be refreshed to reflect new provisioning state.
mFlags.Set(Flags::kAdvertisingRefreshNeeded);

DriveBLEState();
PlatformMgr().ScheduleWork(DriveBLEState, 0);

break;

Expand Down Expand Up @@ -519,7 +519,7 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
uint16_t intervalMax;

// If already advertising, stop it, before changing values
if (mFlags.Has(Flags::kAdvertising))
if (mFlags.Has(Flags::kAdvertising) || mFlags.Has(Flags::kEnablingAdvertising))
{
err = MapBLEError(qvCHIP_BleStopAdvertising());
SuccessOrExit(err);
Expand Down Expand Up @@ -550,7 +550,9 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
err = MapBLEError(qvCHIP_BleStartAdvertising());
SuccessOrExit(err);

mFlags.Set(Flags::kAdvertising);
// Flag updated asynchronously by BLE host callback
mFlags.Set(Flags::kEnablingAdvertising);

if (mFlags.Has(Flags::kFastAdvertisingEnabled))
{
StartBleAdvTimeoutTimer(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME);
Expand Down Expand Up @@ -715,6 +717,7 @@ void BLEManagerImpl::HandleDmMsg(qvCHIP_Ble_DmEvt_t * pDmEvt)
}

sInstance.mFlags.Clear(Flags::kAdvertisingRefreshNeeded);
sInstance.mFlags.Clear(Flags::kEnablingAdvertising);

// Transition to the Advertising state...
if (!sInstance.mFlags.Has(Flags::kAdvertising))
Expand All @@ -739,6 +742,7 @@ void BLEManagerImpl::HandleDmMsg(qvCHIP_Ble_DmEvt_t * pDmEvt)
ChipLogError(DeviceLayer, "QVCHIP_DM_ADV_STOP_IND error: %d", (int) pDmEvt->advSetStop.status);
return;
}

if (mFlags.Has(Flags::kRestartAdvertising))
{
BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising);
Expand Down Expand Up @@ -932,7 +936,7 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
if (BLEMgrImpl().mFlags.Has(Flags::kFastAdvertisingEnabled))
{
/* Stop advertising and defer restart for when stop confirmation is received from the stack */
ChipLogDetail(DeviceLayer, "bleAdv Timeout : Start slow advertissment");
ChipLogDetail(DeviceLayer, "bleAdv Timeout : Start slow advertisement");
sInstance.mFlags.Set(Flags::kRestartAdvertising);
sInstance.StopAdvertising();
}
Expand Down
1 change: 1 addition & 0 deletions src/platform/qpg/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
kDeviceNameSet = 0x0020, /**< The device name has been set. */
kRestartAdvertising = 0x0040, /**< The advertising will be restarted when stop advertising confirmation is received and this
flag is set*/
kEnablingAdvertising = 0x0080, /**< The BLE controller is setting up Advertisements. */
};

enum
Expand Down

0 comments on commit 4096259

Please sign in to comment.