From 11907237d3d557884b03910c7f4410117dccacc0 Mon Sep 17 00:00:00 2001 From: shgutte <102281713+shgutte@users.noreply.github.com> Date: Thu, 11 May 2023 00:06:47 +0530 Subject: [PATCH] [Silabs] [EFR32] Refactor 917 NCP and 917 SoC Bluetooth "DriveBLEState" function and bugfix RS9116 Stop Advertising (#26450) * Refactor 917 NCP and 917 SoC bluetooth to use the same implementation file * Restyled by gn * Added changes for coding standard * Adds fix for the BLE stop advertising * Removing Stop BLE adversting for Sleepy Apps * Restyled by clang-format * Refactor 917 NCP and 917 SoC bluetooth to use the same implementation file * Added changes for coding standard * Adds fix for the BLE stop advertising * Removing Stop BLE adversting for Sleepy Apps * Restyled by clang-format * Removed the additional comments * Added build fox log error --------- Co-authored-by: Restyled.io --- .../silabs/ConnectivityManagerImpl_WIFI.cpp | 4 - src/platform/silabs/rs911x/BLEManagerImpl.cpp | 74 ++++++------------- 2 files changed, 21 insertions(+), 57 deletions(-) diff --git a/src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp b/src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp index 0c113604f625df..215cfddb6d37cb 100644 --- a/src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp +++ b/src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp @@ -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) { diff --git a/src/platform/silabs/rs911x/BLEManagerImpl.cpp b/src/platform/silabs/rs911x/BLEManagerImpl.cpp index ecedc1cd9dfb24..b1dc9f77e2cb2c 100644 --- a/src/platform/silabs/rs911x/BLEManagerImpl.cpp +++ b/src/platform/silabs/rs911x/BLEManagerImpl.cpp @@ -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, @@ -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); @@ -166,8 +158,6 @@ void sl_ble_event_handling_task(void) break; } } - - SILABS_LOG("%s Ended", __func__); } namespace chip { @@ -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. @@ -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; } @@ -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: @@ -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; } @@ -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; } @@ -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; } @@ -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); } @@ -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)); @@ -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; } @@ -719,18 +699,13 @@ 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. @@ -738,8 +713,6 @@ void BLEManagerImpl::HandleConnectionCloseEvent(uint16_t reason) { uint8_t connHandle = 1; - ChipLogProgress(DeviceLayer, "Disconnect Event for handle : %d", connHandle); - if (RemoveConnection(connHandle)) { ChipDeviceEvent event; @@ -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