Skip to content

Commit

Permalink
Add adv change event to silabs platforms (#32987)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkardous-silabs authored Apr 15, 2024
1 parent ef44226 commit 8d47f40
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
44 changes: 30 additions & 14 deletions src/platform/silabs/efr32/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,12 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)

CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
{
CHIP_ERROR err;
sl_status_t ret;
uint32_t interval_min;
uint32_t interval_max;
CHIP_ERROR err = CHIP_NO_ERROR;
sl_status_t ret = SL_STATUS_OK;
uint32_t interval_min = 0;
uint32_t interval_max = 0;
uint16_t numConnectionss = NumConnections();
bool postAdvChangeEvent = false;
uint8_t connectableAdv =
(numConnectionss < kMaxConnections) ? sl_bt_advertiser_connectable_scannable : sl_bt_advertiser_scannable_non_connectable;

Expand All @@ -548,6 +549,7 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
else
{
ChipLogDetail(DeviceLayer, "Start BLE advertisement");
postAdvChangeEvent = true;
}

err = ConfigureAdvertisingData();
Expand Down Expand Up @@ -586,18 +588,26 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
SuccessOrExit(err);

sl_bt_advertiser_configure(advertising_set_handle, 1);

ret = sl_bt_legacy_advertiser_start(advertising_set_handle, connectableAdv);
err = MapBLEError(ret);
SuccessOrExit(err);

if (SL_STATUS_OK == ret)
if (mFlags.Has(Flags::kFastAdvertisingEnabled))
{
if (mFlags.Has(Flags::kFastAdvertisingEnabled))
{
StartBleAdvTimeoutTimer(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME);
}
mFlags.Set(Flags::kAdvertising);
StartBleAdvTimeoutTimer(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME);
}
mFlags.Set(Flags::kAdvertising);

err = MapBLEError(ret);
if (postAdvChangeEvent)
{
// Post CHIPoBLEAdvertisingChange event.
ChipDeviceEvent advChange;
advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange;
advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Started;

ReturnErrorOnFailure(PlatformMgr().PostEvent(&advChange));
}

exit:
return err;
Expand All @@ -606,23 +616,29 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
CHIP_ERROR BLEManagerImpl::StopAdvertising(void)
{
CHIP_ERROR err = CHIP_NO_ERROR;
sl_status_t ret;

if (mFlags.Has(Flags::kAdvertising))
{
sl_status_t ret = SL_STATUS_OK;

mFlags.Clear(Flags::kAdvertising).Clear(Flags::kRestartAdvertising);
mFlags.Set(Flags::kFastAdvertisingEnabled, true);

ret = sl_bt_advertiser_stop(advertising_set_handle);
sl_bt_advertiser_delete_set(advertising_set_handle);
advertising_set_handle = 0xff;
err = MapBLEError(ret);
SuccessOrExit(err);
VerifyOrReturnError(err == CHIP_NO_ERROR, err);

CancelBleAdvTimeoutTimer();

// Post CHIPoBLEAdvertisingChange event.
ChipDeviceEvent advChange;
advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange;
advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Stopped;
err = PlatformMgr().PostEvent(&advChange);
}

exit:
return err;
}

Expand Down
32 changes: 24 additions & 8 deletions src/platform/silabs/rs911x/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,9 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)

CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
{
CHIP_ERROR err = CHIP_NO_ERROR;
int32_t status = 0;
CHIP_ERROR err = CHIP_NO_ERROR;
int32_t status = 0;
bool postAdvChangeEvent = false;

ChipLogProgress(DeviceLayer, "StartAdvertising start");

Expand All @@ -695,6 +696,7 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
else
{
ChipLogDetail(DeviceLayer, "Start BLE advertisement");
postAdvChangeEvent = true;
}

if (!(mFlags.Has(Flags::kAdvertising)))
Expand All @@ -719,15 +721,26 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
StartBleAdvTimeoutTimer(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME);
}
mFlags.Set(Flags::kAdvertising);

if (postAdvChangeEvent)
{
// Post CHIPoBLEAdvertisingChange event.
ChipDeviceEvent advChange;
advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange;
advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Started;

ReturnErrorOnFailure(PlatformMgr().PostEvent(&advChange));
}
}
else
{
ChipLogProgress(DeviceLayer, "rsi_ble_start_advertising Failed with status: %lx", status);
}

exit:
// TODO: Add MapBLEError to return the correct error code
ChipLogError(DeviceLayer, "StartAdvertising() End error: %s", ErrorStr(err));
return CHIP_NO_ERROR; // err;
return err;
}

int32_t BLEManagerImpl::SendBLEAdvertisementCommand(void)
Expand Down Expand Up @@ -756,7 +769,6 @@ int32_t BLEManagerImpl::SendBLEAdvertisementCommand(void)
return rsi_ble_start_advertising_with_values(&ble_adv);
}

// TODO:: Implementation need to be done.
CHIP_ERROR BLEManagerImpl::StopAdvertising(void)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -772,12 +784,16 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising(void)
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
advertising_set_handle = 0xff;
CancelBleAdvTimeoutTimer();
}
else
{
ChipLogProgress(DeviceLayer, "advertising failed to stop, with status = 0x%lx", status);

// Post CHIPoBLEAdvertisingChange event.
ChipDeviceEvent advChange;
advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange;
advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Stopped;
err = PlatformMgr().PostEvent(&advChange);
}
}

// TODO: Add MapBLEError to return the correct error code
return err;
}

Expand Down

0 comments on commit 8d47f40

Please sign in to comment.