From 1e9f9b28141f663c3be1fdfa0c128cd774c35c89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Duda?= Date: Fri, 8 Mar 2024 00:15:33 +0100 Subject: [PATCH] [nrf noup] Fix various Wi-Fi issues with error code handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit handles a few issues with Wi-Fi connection or scanning: - Use wifi_status structure instead of incompatible WiFiRequestStatus - On connect error value > 2 do not report success - On scan error value > 1 do not report success - Provide value of mandatory LastConnectErrorValue attribute Signed-off-by: Ɓukasz Duda --- .../nrfconnect/wifi/NrfWiFiDriver.cpp | 22 ++++++------- src/platform/nrfconnect/wifi/NrfWiFiDriver.h | 4 +-- src/platform/nrfconnect/wifi/WiFiManager.cpp | 33 +++++++++---------- src/platform/nrfconnect/wifi/WiFiManager.h | 14 ++------ 4 files changed, 31 insertions(+), 42 deletions(-) diff --git a/src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp b/src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp index 341bd6b377..f6a9b9f3ae 100644 --- a/src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp +++ b/src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp @@ -103,8 +103,7 @@ CHIP_ERROR NrfWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChange if (mStagingNetwork.IsConfigured()) { - WiFiManager::ConnectionHandling handling{ [] { Instance().OnNetworkStatusChanged(Status::kSuccess); }, - [] { Instance().OnNetworkStatusChanged(Status::kUnknownError); }, + WiFiManager::ConnectionHandling handling{ [](int connStatus) { Instance().OnNetworkStatusChanged(connStatus); }, System::Clock::Seconds32{ kWiFiConnectNetworkTimeoutSeconds } }; ReturnErrorOnFailure( WiFiManager::Instance().Connect(mStagingNetwork.GetSsidSpan(), mStagingNetwork.GetPassSpan(), handling)); @@ -113,8 +112,10 @@ CHIP_ERROR NrfWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChange return CHIP_NO_ERROR; } -void NrfWiFiDriver::OnNetworkStatusChanged(Status status) +void NrfWiFiDriver::OnNetworkStatusChanged(int connStatus) { + Status status = connStatus ? Status::kUnknownError : Status::kSuccess; + if (status == Status::kSuccess) { ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Enabled); @@ -127,7 +128,8 @@ void NrfWiFiDriver::OnNetworkStatusChanged(Status status) if (CHIP_NO_ERROR == WiFiManager::Instance().GetWiFiInfo(wifiInfo)) { mpNetworkStatusChangeCallback->OnNetworkingStatusChange(status, - MakeOptional(ByteSpan(wifiInfo.mSsid, wifiInfo.mSsidLen)), NullOptional); + MakeOptional(ByteSpan(wifiInfo.mSsid, wifiInfo.mSsidLen)), + connStatus ? MakeOptional(connStatus) : NullOptional); } } @@ -170,8 +172,7 @@ CHIP_ERROR NrfWiFiDriver::RevertConfiguration() if (mStagingNetwork.IsConfigured()) { - WiFiManager::ConnectionHandling handling{ [] { Instance().OnNetworkStatusChanged(Status::kSuccess); }, - [] { Instance().OnNetworkStatusChanged(Status::kUnknownError); }, + WiFiManager::ConnectionHandling handling{ [](int connStatus) { Instance().OnNetworkStatusChanged(connStatus); }, System::Clock::Seconds32{ kWiFiConnectNetworkTimeoutSeconds } }; ReturnErrorOnFailure( WiFiManager::Instance().Connect(mStagingNetwork.GetSsidSpan(), mStagingNetwork.GetPassSpan(), handling)); @@ -224,8 +225,7 @@ Status NrfWiFiDriver::ReorderNetwork(ByteSpan networkId, uint8_t index, MutableC void NrfWiFiDriver::ConnectNetwork(ByteSpan networkId, ConnectCallback * callback) { Status status = Status::kSuccess; - WiFiManager::ConnectionHandling handling{ [] { Instance().OnNetworkStatusChanged(Status::kSuccess); }, - [] { Instance().OnNetworkStatusChanged(Status::kUnknownError); }, + WiFiManager::ConnectionHandling handling{ [](int connStatus) { Instance().OnNetworkStatusChanged(connStatus); }, System::Clock::Seconds32{ kWiFiConnectNetworkTimeoutSeconds } }; VerifyOrExit(mpConnectCallback == nullptr, status = Status::kUnknownError); @@ -255,10 +255,10 @@ void NrfWiFiDriver::LoadFromStorage() mStagingNetwork = network; } -void NrfWiFiDriver::OnScanWiFiNetworkDone(WiFiManager::WiFiRequestStatus status) +void NrfWiFiDriver::OnScanWiFiNetworkDone(const wifi_status & status) { VerifyOrReturn(mScanCallback != nullptr); - mScanCallback->OnFinished(status == WiFiManager::WiFiRequestStatus::SUCCESS ? Status::kSuccess : Status::kUnknownError, + mScanCallback->OnFinished(status.status ? Status::kUnknownError : Status::kSuccess, CharSpan(), &mScanResponseIterator); mScanCallback = nullptr; } @@ -273,7 +273,7 @@ void NrfWiFiDriver::ScanNetworks(ByteSpan ssid, WiFiDriver::ScanCallback * callb mScanCallback = callback; CHIP_ERROR error = WiFiManager::Instance().Scan( ssid, [](const WiFiScanResponse & response) { Instance().OnScanWiFiNetworkResult(response); }, - [](WiFiManager::WiFiRequestStatus status) { Instance().OnScanWiFiNetworkDone(status); }); + [](const wifi_status & status) { Instance().OnScanWiFiNetworkDone(status); }); if (error != CHIP_NO_ERROR) { diff --git a/src/platform/nrfconnect/wifi/NrfWiFiDriver.h b/src/platform/nrfconnect/wifi/NrfWiFiDriver.h index d1fc765a72..79a395e6fa 100644 --- a/src/platform/nrfconnect/wifi/NrfWiFiDriver.h +++ b/src/platform/nrfconnect/wifi/NrfWiFiDriver.h @@ -93,9 +93,9 @@ class NrfWiFiDriver final : public WiFiDriver return sInstance; } - void OnNetworkStatusChanged(Status status); + void OnNetworkStatusChanged(int status); void OnScanWiFiNetworkResult(const WiFiScanResponse & result); - void OnScanWiFiNetworkDone(WiFiManager::WiFiRequestStatus status); + void OnScanWiFiNetworkDone(const wifi_status & status); private: void LoadFromStorage(); diff --git a/src/platform/nrfconnect/wifi/WiFiManager.cpp b/src/platform/nrfconnect/wifi/WiFiManager.cpp index 1f22b366b3..ce17308bf4 100644 --- a/src/platform/nrfconnect/wifi/WiFiManager.cpp +++ b/src/platform/nrfconnect/wifi/WiFiManager.cpp @@ -210,9 +210,8 @@ CHIP_ERROR WiFiManager::Connect(const ByteSpan & ssid, const ByteSpan & credenti { ChipLogDetail(DeviceLayer, "Connecting to WiFi network: %*s", ssid.size(), ssid.data()); - mHandling.mOnConnectionSuccess = handling.mOnConnectionSuccess; - mHandling.mOnConnectionFailed = handling.mOnConnectionFailed; - mHandling.mConnectionTimeout = handling.mConnectionTimeout; + mHandling.mOnConnectionDone = handling.mOnConnectionDone; + mHandling.mConnectionTimeout = handling.mConnectionTimeout; mWiFiState = WIFI_STATE_ASSOCIATING; @@ -343,11 +342,10 @@ void WiFiManager::ScanDoneHandler(Platform::UniquePtr data) { CHIP_ERROR err = SystemLayer().ScheduleLambda([capturedData = data.get()] { Platform::UniquePtr safePtr(capturedData); - uint8_t * rawData = safePtr.get(); - const wifi_status * status = reinterpret_cast(rawData); - WiFiRequestStatus requestStatus = static_cast(status->status); + uint8_t * rawData = safePtr.get(); + const wifi_status * status = reinterpret_cast(rawData); - if (requestStatus == WiFiRequestStatus::FAILURE) + if (status->status) { ChipLogError(DeviceLayer, "Wi-Fi scan finalization failure (%d)", status->status); } @@ -358,7 +356,7 @@ void WiFiManager::ScanDoneHandler(Platform::UniquePtr data) if (Instance().mScanDoneCallback && !Instance().mInternalScan) { - Instance().mScanDoneCallback(requestStatus); + Instance().mScanDoneCallback(*status); // restore the connection state from before the scan request was issued Instance().mWiFiState = Instance().mCachedWiFiState; return; @@ -379,13 +377,13 @@ void WiFiManager::ScanDoneHandler(Platform::UniquePtr data) Instance().mWiFiState = WIFI_STATE_ASSOCIATING; - if (net_mgmt(NET_REQUEST_WIFI_CONNECT, Instance().mNetIf, &(Instance().mWiFiParams.mParams), - sizeof(wifi_connect_req_params))) + if (int connStatus = net_mgmt(NET_REQUEST_WIFI_CONNECT, Instance().mNetIf, &(Instance().mWiFiParams.mParams), + sizeof(wifi_connect_req_params))) { ChipLogError(DeviceLayer, "Connection request failed"); - if (Instance().mHandling.mOnConnectionFailed) + if (Instance().mHandling.mOnConnectionDone) { - Instance().mHandling.mOnConnectionFailed(); + Instance().mHandling.mOnConnectionDone(connStatus); } Instance().mWiFiState = WIFI_STATE_DISCONNECTED; return; @@ -424,15 +422,14 @@ void WiFiManager::ConnectHandler(Platform::UniquePtr data) Platform::UniquePtr safePtr(capturedData); uint8_t * rawData = safePtr.get(); const wifi_status * status = reinterpret_cast(rawData); - WiFiRequestStatus requestStatus = static_cast(status->status); - if (requestStatus == WiFiRequestStatus::FAILURE || requestStatus == WiFiRequestStatus::TERMINATED) + if (status->status) { ChipLogProgress(DeviceLayer, "Connection to WiFi network failed or was terminated by another request"); Instance().mWiFiState = WIFI_STATE_DISCONNECTED; - if (Instance().mHandling.mOnConnectionFailed) + if (Instance().mHandling.mOnConnectionDone) { - Instance().mHandling.mOnConnectionFailed(); + Instance().mHandling.mOnConnectionDone(status->status); } } else // The connection has been established successfully. @@ -444,9 +441,9 @@ void WiFiManager::ConnectHandler(Platform::UniquePtr data) ChipLogProgress(DeviceLayer, "Connected to WiFi network"); Instance().mWiFiState = WIFI_STATE_COMPLETED; - if (Instance().mHandling.mOnConnectionSuccess) + if (Instance().mHandling.mOnConnectionDone) { - Instance().mHandling.mOnConnectionSuccess(); + Instance().mHandling.mOnConnectionDone(status->status); } Instance().PostConnectivityStatusChange(kConnectivity_Established); diff --git a/src/platform/nrfconnect/wifi/WiFiManager.h b/src/platform/nrfconnect/wifi/WiFiManager.h index 5e910a19ca..9ddb5dea27 100644 --- a/src/platform/nrfconnect/wifi/WiFiManager.h +++ b/src/platform/nrfconnect/wifi/WiFiManager.h @@ -88,16 +88,9 @@ class Map class WiFiManager { public: - enum WiFiRequestStatus : int - { - SUCCESS = 0, - FAILURE = 1, - TERMINATED = 2 - }; - using ScanResultCallback = void (*)(const NetworkCommissioning::WiFiScanResponse &); - using ScanDoneCallback = void (*)(WiFiRequestStatus); - using ConnectionCallback = void (*)(); + using ScanDoneCallback = void (*)(const wifi_status &); + using ConnectionCallback = void (*)(int); enum class StationStatus : uint8_t { @@ -120,8 +113,7 @@ class WiFiManager struct ConnectionHandling { - ConnectionCallback mOnConnectionSuccess{}; - ConnectionCallback mOnConnectionFailed{}; + ConnectionCallback mOnConnectionDone{}; System::Clock::Seconds32 mConnectionTimeout{}; };