Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Silabs] [EFR32] Refactor 917 NCP and 917 SoC Bluetooth "DriveBLEState" function and bugfix RS9116 Stop Advertising #26450

Merged
Merged
4 changes: 0 additions & 4 deletions src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,6 @@ void ConnectivityManagerImpl::OnStationConnected()
(void) PlatformMgr().PostEvent(&event);
// Setting the rs911x in the power save mode
#if (CHIP_DEVICE_CONFIG_ENABLE_SED && RS911X_WIFI)
// TODO: Remove stop advertising after BLEManagerImpl is fixed
#if RSI_BLE_ENABLE
chip::DeviceLayer::Internal::BLEManagerImpl().StopAdvertising();
#endif /* RSI_BLE_ENABLE */
sl_status_t err = wfx_power_save();
if (err != SL_STATUS_OK)
{
Expand Down
74 changes: 21 additions & 53 deletions src/platform/silabs/rs911x/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ using namespace ::chip::DeviceLayer::Internal;

void sl_ble_init()
{
SILABS_LOG("%s starting", __func__);

// registering the GAP callback functions
rsi_ble_gap_register_callbacks(NULL, NULL, rsi_ble_on_disconnect_event, NULL, NULL, NULL, rsi_ble_on_enhance_conn_status_event,
Expand All @@ -88,25 +87,18 @@ void sl_ble_init()
rsi_ble_on_mtu_event, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
rsi_ble_on_event_indication_confirmation, NULL);

SILABS_LOG("registering rsi_ble_add_service");

// Exchange of GATT info with BLE stack
rsi_ble_add_matter_service();

// initializing the application events map
rsi_ble_app_init_events();

SILABS_LOG("StartAdvertising");
chip::DeviceLayer::Internal::BLEManagerImpl().StartAdvertising(); // TODO:: Called on after init of module
SILABS_LOG("%s Ended", __func__);
chip::DeviceLayer::Internal::BLEMgrImpl().HandleBootEvent();
}

void sl_ble_event_handling_task(void)
{
int32_t event_id;

SILABS_LOG("%s starting", __func__);

//! This semaphore is waiting for wifi module initialization.
rsi_semaphore_wait(&sl_rs_ble_init_sem, 0);

Expand Down Expand Up @@ -166,8 +158,6 @@ void sl_ble_event_handling_task(void)
break;
}
}

SILABS_LOG("%s Ended", __func__);
}

namespace chip {
Expand Down Expand Up @@ -235,14 +225,13 @@ CHIP_ERROR BLEManagerImpl::_Init()
CHIP_ERROR err;
rsi_semaphore_create(&sl_rs_ble_init_sem, 0);
rsi_semaphore_create(&sl_ble_event_sem, 0);
ChipLogProgress(DeviceLayer, "%s Start ", __func__);

wfx_rsi.ble_task = xTaskCreateStatic((TaskFunction_t) sl_ble_event_handling_task, "rsi_ble", WFX_RSI_TASK_SZ, NULL, 1,
wfxBLETaskStack, &rsiBLETaskStruct);

if (wfx_rsi.ble_task == NULL)
{
SILABS_LOG("%s: error: failed to create ble task.", __func__);
ChipLogError(DeviceLayer, "%s: error: failed to create ble task.", __func__);
}

// Initialize the CHIP BleLayer.
Expand All @@ -263,12 +252,9 @@ CHIP_ERROR BLEManagerImpl::_Init()

mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
#ifdef SIWX_917
PlatformMgr().ScheduleWork(DriveBLEState, 0);
#endif

exit:
ChipLogProgress(DeviceLayer, "%s END ", __func__);
return err;
}

Expand All @@ -295,9 +281,7 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val)
if (mFlags.Has(Flags::kAdvertisingEnabled) != val)
{
mFlags.Set(Flags::kAdvertisingEnabled, val);
#ifdef SIWX_917
PlatformMgr().ScheduleWork(DriveBLEState, 0);
#endif
}

exit:
Expand All @@ -318,9 +302,7 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode)
return CHIP_ERROR_INVALID_ARGUMENT;
}
mFlags.Set(Flags::kRestartAdvertising);
#ifdef SIWX_917
PlatformMgr().ScheduleWork(DriveBLEState, 0);
#endif
return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -358,9 +340,7 @@ CHIP_ERROR BLEManagerImpl::_SetDeviceName(const char * deviceName)
{
mDeviceName[0] = 0;
}
#ifdef SIWX_917
PlatformMgr().ScheduleWork(DriveBLEState, 0);
#endif
ChipLogProgress(DeviceLayer, "_SetDeviceName Ended");
return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -446,12 +426,11 @@ bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUU
PacketBufferHandle data)
{
int32_t status = 0;
SILABS_LOG("In send indication");
status = rsi_ble_indicate_value(event_msg.resp_enh_conn.dev_addr, event_msg.rsi_ble_measurement_hndl, (data->DataLength()),
data->Start());
if (status != RSI_SUCCESS)
{
SILABS_LOG("indication %d failed with error code %lx ", status);
ChipLogProgress(DeviceLayer, "indication failed with error code %lx ", status);
return false;
}

Expand Down Expand Up @@ -506,23 +485,20 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)

void BLEManagerImpl::DriveBLEState(void)
{
ChipLogProgress(DeviceLayer, "DriveBLEState starting");
CHIP_ERROR err = CHIP_NO_ERROR;

// Check if BLE stack is initialized ( TODO )
// VerifyOrExit(mFlags.Has(Flags::kEFRBLEStackInitialized),);
VerifyOrExit(mFlags.Has(Flags::kEFRBLEStackInitialized), /* */);

ChipLogProgress(DeviceLayer, "Start advertising if needed...");
// Start advertising if needed...
if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && mFlags.Has(Flags::kAdvertisingEnabled) &&
NumConnections() < kMaxConnections)
{

ChipLogProgress(DeviceLayer, "Start/re-start advertising if not already started, or if there is a pending change");
// Start/re-start advertising if not already started, or if there is a pending change
// to the advertising configuration.
if (!mFlags.Has(Flags::kAdvertising) || mFlags.Has(Flags::kRestartAdvertising))
{
ChipLogProgress(DeviceLayer, "Start Advertising");
err = StartAdvertising();
SuccessOrExit(err);
}
Expand All @@ -531,12 +507,12 @@ void BLEManagerImpl::DriveBLEState(void)
// Otherwise, stop advertising if it is enabled.
else if (mFlags.Has(Flags::kAdvertising))
{
ChipLogProgress(DeviceLayer, "Stop Advertising");
err = StopAdvertising();
SuccessOrExit(err);
}

exit:
ChipLogProgress(DeviceLayer, "DriveBLEState End");
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Disabling CHIPoBLE service due to error: %s", ErrorStr(err));
Expand Down Expand Up @@ -677,19 +653,23 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising(void)
{
CHIP_ERROR err = CHIP_NO_ERROR;
int32_t status = 0;
// TODO: add the below code in a condition if (mFlags.Has(Flags::kAdvertising))
// Since DriveBLEState is not called the device is still advertising
mFlags.Clear(Flags::kAdvertising).Clear(Flags::kRestartAdvertising);
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
status = rsi_ble_stop_advertising();
if (status != RSI_SUCCESS)

if (mFlags.Has(Flags::kAdvertising))
{
ChipLogProgress(DeviceLayer, "advertising failed to stop, with status = 0x%lx", status);
// Since DriveBLEState is not called the device is still advertising
status = rsi_ble_stop_advertising();
if (status != RSI_SUCCESS)
{
mFlags.Clear(Flags::kAdvertising).Clear(Flags::kRestartAdvertising);
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
advertising_set_handle = 0xff;
CancelBleAdvTimeoutTimer();
}
else
{
ChipLogProgress(DeviceLayer, "advertising failed to stop, with status = 0x%lx", status);
}
}
advertising_set_handle = 0xff;
CancelBleAdvTimeoutTimer();

// exit:
return err;
}

Expand Down Expand Up @@ -719,27 +699,20 @@ void BLEManagerImpl::UpdateMtu(rsi_ble_event_mtu_t evt)
void BLEManagerImpl::HandleBootEvent(void)
{
mFlags.Set(Flags::kEFRBLEStackInitialized);
#ifdef SIWX_917
PlatformMgr().ScheduleWork(DriveBLEState, 0);
#endif
}

void BLEManagerImpl::HandleConnectEvent(void)
{
ChipLogProgress(DeviceLayer, "Connect Event for handle : %d", event_msg.connectionHandle);
AddConnection(event_msg.connectionHandle, event_msg.bondingHandle);
#ifdef SIWX_917
PlatformMgr().ScheduleWork(DriveBLEState, 0);
#endif
}

// TODO:: Implementation need to be done.
void BLEManagerImpl::HandleConnectionCloseEvent(uint16_t reason)
{
uint8_t connHandle = 1;

ChipLogProgress(DeviceLayer, "Disconnect Event for handle : %d", connHandle);

if (RemoveConnection(connHandle))
{
ChipDeviceEvent event;
Expand Down Expand Up @@ -772,15 +745,10 @@ void BLEManagerImpl::HandleConnectionCloseEvent(uint16_t reason)

void BLEManagerImpl::HandleWriteEvent(rsi_ble_event_write_t evt)
{
// RSI_BLE_WRITE_REQUEST_EVENT
ChipLogProgress(DeviceLayer, "Char Write Req, packet type %d", evt.pkt_type);
// uint8_t attribute = (uint8_t) event_msg.rsi_ble_measurement_hndl;

SILABS_LOG("event_msg.rsi_ble_gatt_server_client_config_hndl = %d", event_msg.rsi_ble_gatt_server_client_config_hndl);

if (evt.handle[0] == (uint8_t) event_msg.rsi_ble_gatt_server_client_config_hndl) // TODO:: compare the handle exactly
{
SILABS_LOG("Inside HandleTXCharCCCDWrite ");
HandleTXCharCCCDWrite(&evt);
}
else
Expand Down