From 4207801c86b345efd5599780e51094bdc6e61607 Mon Sep 17 00:00:00 2001 From: Rohan Sahay Date: Fri, 28 Apr 2023 22:38:26 +0530 Subject: [PATCH] [Silabs] Adds fix for LastNetworkID & LastConnectErrorValue have null value after device reset (#26237) * Adds support for NetworkStatusChangeCallback * Remove err * Moves logic to ConnectWiFiNetwork * Adds null check print * Adds UpdateNetworkingStatus API * Adds SSID length check * Refactors UpdateNetworkingStatus invocation in the ChangeWiFiStationState function * Restyled by clang-format --------- Co-authored-by: Restyled.io --- .../platform/silabs/efr32/BaseApplication.cpp | 2 +- .../silabs/ConnectivityManagerImpl_WIFI.cpp | 3 +- .../silabs/NetworkCommissioningWiFiDriver.cpp | 40 ++++++++++++++++--- .../silabs/NetworkCommissioningWiFiDriver.h | 2 + 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/examples/platform/silabs/efr32/BaseApplication.cpp b/examples/platform/silabs/efr32/BaseApplication.cpp index 6ce48e736d121e..8d088bd4de3e13 100644 --- a/examples/platform/silabs/efr32/BaseApplication.cpp +++ b/examples/platform/silabs/efr32/BaseApplication.cpp @@ -173,7 +173,7 @@ CHIP_ERROR BaseApplication::Init(Identify * identifyObj) SILABS_LOG("APP: Wait WiFi Init"); while (!wfx_hw_ready()) { - vTaskDelay(10); + vTaskDelay(pdMS_TO_TICKS(10)); } SILABS_LOG("APP: Done WiFi Init"); /* We will init server when we get IP */ diff --git a/src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp b/src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp index 6b10b294b5ad02..0c113604f625df 100644 --- a/src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp +++ b/src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp @@ -397,7 +397,7 @@ void ConnectivityManagerImpl::OnStationConnected() void ConnectivityManagerImpl::OnStationDisconnected() { - // TODO Invoke WARM to perform actions that occur when the WiFi station interface goes down. + // TODO: Invoke WARM to perform actions that occur when the WiFi station interface goes down. // Alert other components of the new state. ChipDeviceEvent event; @@ -420,6 +420,7 @@ void ConnectivityManagerImpl::ChangeWiFiStationState(WiFiStationState newState) ChipLogProgress(DeviceLayer, "WiFi station state change: %s -> %s", WiFiStationStateToStr(mWiFiStationState), WiFiStationStateToStr(newState)); mWiFiStationState = newState; + NetworkCommissioning::SlWiFiDriver::GetInstance().UpdateNetworkingStatus(); } } diff --git a/src/platform/silabs/NetworkCommissioningWiFiDriver.cpp b/src/platform/silabs/NetworkCommissioningWiFiDriver.cpp index 5f108213f193ec..5778263466ceeb 100644 --- a/src/platform/silabs/NetworkCommissioningWiFiDriver.cpp +++ b/src/platform/silabs/NetworkCommissioningWiFiDriver.cpp @@ -38,10 +38,11 @@ SlScanResponseIterator mScanResponseIter CHIP_ERROR SlWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChangeCallback) { CHIP_ERROR err; - size_t ssidLen = 0; - size_t credentialsLen = 0; - mpScanCallback = nullptr; - mpConnectCallback = nullptr; + size_t ssidLen = 0; + size_t credentialsLen = 0; + mpScanCallback = nullptr; + mpConnectCallback = nullptr; + mpStatusChangeCallback = networkStatusChangeCallback; #ifdef SL_ONNETWORK_PAIRING memcpy(&mSavedNetwork.ssid[0], SL_WIFI_SSID, sizeof(SL_WIFI_SSID)); @@ -135,11 +136,12 @@ Status SlWiFiDriver::ReorderNetwork(ByteSpan networkId, uint8_t index, MutableCh CHIP_ERROR SlWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, const char * key, uint8_t keyLen) { + int32_t status = SL_STATUS_OK; if (ConnectivityMgr().IsWiFiStationProvisioned()) { ChipLogProgress(DeviceLayer, "Disconecting for current wifi"); - int32_t status = wfx_sta_discon(); - if (status != 0) + status = wfx_sta_discon(); + if (status != SL_STATUS_OK) { return CHIP_ERROR_INTERNAL; } @@ -159,6 +161,32 @@ CHIP_ERROR SlWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, return CHIP_NO_ERROR; } +// TODO: Re-write implementation with proper driver based callback +void SlWiFiDriver::UpdateNetworkingStatus() +{ + if (mpStatusChangeCallback == nullptr) + { + ChipLogError(NetworkProvisioning, "networkStatusChangeCallback is nil"); + return; + } + + if (mStagingNetwork.ssidLen == 0) + { + ChipLogError(NetworkProvisioning, "ssidLen is 0"); + return; + } + + ByteSpan networkId = ByteSpan((const unsigned char *) mStagingNetwork.ssid, mStagingNetwork.ssidLen); + if (!wfx_is_sta_connected()) + { + mpStatusChangeCallback->OnNetworkingStatusChange(Status::kUnknownError, MakeOptional(networkId), + MakeOptional((int32_t) SL_STATUS_FAIL)); + return; + } + mpStatusChangeCallback->OnNetworkingStatusChange(Status::kSuccess, MakeOptional(networkId), + MakeOptional((int32_t) SL_STATUS_OK)); +} + void SlWiFiDriver::OnConnectWiFiNetwork() { if (mpConnectCallback) diff --git a/src/platform/silabs/NetworkCommissioningWiFiDriver.h b/src/platform/silabs/NetworkCommissioningWiFiDriver.h index 7e7dad3f3e9cdc..fd73568a6ba98b 100644 --- a/src/platform/silabs/NetworkCommissioningWiFiDriver.h +++ b/src/platform/silabs/NetworkCommissioningWiFiDriver.h @@ -123,6 +123,7 @@ class SlWiFiDriver final : public WiFiDriver chip::BitFlags ConvertSecuritytype(wfx_sec_t security); void OnConnectWiFiNetwork(); + void UpdateNetworkingStatus(); static SlWiFiDriver & GetInstance() { static SlWiFiDriver instance; @@ -138,6 +139,7 @@ class SlWiFiDriver final : public WiFiDriver WiFiNetwork mStagingNetwork = {}; ScanCallback * mpScanCallback; ConnectCallback * mpConnectCallback; + NetworkStatusChangeCallback * mpStatusChangeCallback = nullptr; }; } // namespace NetworkCommissioning