From 1d3bce89c1d4c0646f3a22b9a06a312225f3d0f7 Mon Sep 17 00:00:00 2001 From: Marcin Kajor Date: Tue, 19 Mar 2024 12:11:01 +0100 Subject: [PATCH] [nrf noup] [nrfconnect] Minor Wi-Fi refinements * error code handling unification * added GetWantedNetwork getter and use it when handling network status change * minor refactoring Signed-off-by: Marcin Kajor --- .../nrfconnect/wifi/NrfWiFiDriver.cpp | 37 +++++++++++++------ src/platform/nrfconnect/wifi/NrfWiFiDriver.h | 4 +- src/platform/nrfconnect/wifi/WiFiManager.cpp | 33 +++++++++-------- src/platform/nrfconnect/wifi/WiFiManager.h | 6 ++- 4 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp b/src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp index f6a9b9f3ae..d5e64acc62 100644 --- a/src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp +++ b/src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp @@ -103,7 +103,9 @@ CHIP_ERROR NrfWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChange if (mStagingNetwork.IsConfigured()) { - WiFiManager::ConnectionHandling handling{ [](int connStatus) { Instance().OnNetworkStatusChanged(connStatus); }, + WiFiManager::ConnectionHandling handling{ [](const wifi_conn_status & connStatus) { + Instance().OnNetworkConnStatusChanged(connStatus); + }, System::Clock::Seconds32{ kWiFiConnectNetworkTimeoutSeconds } }; ReturnErrorOnFailure( WiFiManager::Instance().Connect(mStagingNetwork.GetSsidSpan(), mStagingNetwork.GetPassSpan(), handling)); @@ -112,8 +114,9 @@ CHIP_ERROR NrfWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChange return CHIP_NO_ERROR; } -void NrfWiFiDriver::OnNetworkStatusChanged(int connStatus) +void NrfWiFiDriver::OnNetworkConnStatusChanged(const wifi_conn_status & connStatus) { + // TODO: check if we can report more accurate errors Status status = connStatus ? Status::kUnknownError : Status::kSuccess; if (status == Status::kSuccess) @@ -123,14 +126,23 @@ void NrfWiFiDriver::OnNetworkStatusChanged(int connStatus) if (mpNetworkStatusChangeCallback) { + uint8_t * ssid{}; + size_t ssidLen{}; WiFiManager::WiFiInfo wifiInfo; if (CHIP_NO_ERROR == WiFiManager::Instance().GetWiFiInfo(wifiInfo)) { - mpNetworkStatusChangeCallback->OnNetworkingStatusChange(status, - MakeOptional(ByteSpan(wifiInfo.mSsid, wifiInfo.mSsidLen)), - connStatus ? MakeOptional(connStatus) : NullOptional); + ssid = wifiInfo.mSsid; + ssidLen = wifiInfo.mSsidLen; } + else + { + ssid = WiFiManager::Instance().GetWantedNetwork().ssid; + ssidLen = WiFiManager::Instance().GetWantedNetwork().ssidLen; + } + mpNetworkStatusChangeCallback->OnNetworkingStatusChange(status, MakeOptional(ByteSpan(wifiInfo.mSsid, wifiInfo.mSsidLen)), + connStatus ? MakeOptional(static_cast(connStatus)) + : NullOptional); } if (mpConnectCallback) @@ -172,7 +184,9 @@ CHIP_ERROR NrfWiFiDriver::RevertConfiguration() if (mStagingNetwork.IsConfigured()) { - WiFiManager::ConnectionHandling handling{ [](int connStatus) { Instance().OnNetworkStatusChanged(connStatus); }, + WiFiManager::ConnectionHandling handling{ [](const wifi_conn_status & connStatus) { + Instance().OnNetworkConnStatusChanged(connStatus); + }, System::Clock::Seconds32{ kWiFiConnectNetworkTimeoutSeconds } }; ReturnErrorOnFailure( WiFiManager::Instance().Connect(mStagingNetwork.GetSsidSpan(), mStagingNetwork.GetPassSpan(), handling)); @@ -225,7 +239,9 @@ Status NrfWiFiDriver::ReorderNetwork(ByteSpan networkId, uint8_t index, MutableC void NrfWiFiDriver::ConnectNetwork(ByteSpan networkId, ConnectCallback * callback) { Status status = Status::kSuccess; - WiFiManager::ConnectionHandling handling{ [](int connStatus) { Instance().OnNetworkStatusChanged(connStatus); }, + WiFiManager::ConnectionHandling handling{ [](const wifi_conn_status & connStatus) { + Instance().OnNetworkConnStatusChanged(connStatus); + }, System::Clock::Seconds32{ kWiFiConnectNetworkTimeoutSeconds } }; VerifyOrExit(mpConnectCallback == nullptr, status = Status::kUnknownError); @@ -255,11 +271,10 @@ void NrfWiFiDriver::LoadFromStorage() mStagingNetwork = network; } -void NrfWiFiDriver::OnScanWiFiNetworkDone(const wifi_status & status) +void NrfWiFiDriver::OnScanWiFiNetworkDone(const WiFiManager::ScanDoneStatus & status) { VerifyOrReturn(mScanCallback != nullptr); - mScanCallback->OnFinished(status.status ? Status::kUnknownError : Status::kSuccess, - CharSpan(), &mScanResponseIterator); + mScanCallback->OnFinished(status ? Status::kUnknownError : Status::kSuccess, CharSpan(), &mScanResponseIterator); mScanCallback = nullptr; } @@ -273,7 +288,7 @@ void NrfWiFiDriver::ScanNetworks(ByteSpan ssid, WiFiDriver::ScanCallback * callb mScanCallback = callback; CHIP_ERROR error = WiFiManager::Instance().Scan( ssid, [](const WiFiScanResponse & response) { Instance().OnScanWiFiNetworkResult(response); }, - [](const wifi_status & status) { Instance().OnScanWiFiNetworkDone(status); }); + [](const WiFiManager::ScanDoneStatus & 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 79a395e6fa..523c573ad8 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(int status); + void OnNetworkConnStatusChanged(const wifi_conn_status & connStatus); void OnScanWiFiNetworkResult(const WiFiScanResponse & result); - void OnScanWiFiNetworkDone(const wifi_status & status); + void OnScanWiFiNetworkDone(const WiFiManager::ScanDoneStatus & status); private: void LoadFromStorage(); diff --git a/src/platform/nrfconnect/wifi/WiFiManager.cpp b/src/platform/nrfconnect/wifi/WiFiManager.cpp index ce17308bf4..6e3ff8e473 100644 --- a/src/platform/nrfconnect/wifi/WiFiManager.cpp +++ b/src/platform/nrfconnect/wifi/WiFiManager.cpp @@ -210,8 +210,7 @@ CHIP_ERROR WiFiManager::Connect(const ByteSpan & ssid, const ByteSpan & credenti { ChipLogDetail(DeviceLayer, "Connecting to WiFi network: %*s", ssid.size(), ssid.data()); - mHandling.mOnConnectionDone = handling.mOnConnectionDone; - mHandling.mConnectionTimeout = handling.mConnectionTimeout; + mHandling = handling; mWiFiState = WIFI_STATE_ASSOCIATING; @@ -342,21 +341,22 @@ 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); + uint8_t * rawData = safePtr.get(); + const wifi_status * status = reinterpret_cast(rawData); + ScanDoneStatus scanDoneStatus = status->status; - if (status->status) + if (scanDoneStatus) { - ChipLogError(DeviceLayer, "Wi-Fi scan finalization failure (%d)", status->status); + ChipLogError(DeviceLayer, "Wi-Fi scan finalization failure (%d)", scanDoneStatus); } else { - ChipLogProgress(DeviceLayer, "Wi-Fi scan done (%d)", status->status); + ChipLogProgress(DeviceLayer, "Wi-Fi scan done"); } if (Instance().mScanDoneCallback && !Instance().mInternalScan) { - Instance().mScanDoneCallback(*status); + Instance().mScanDoneCallback(scanDoneStatus); // restore the connection state from before the scan request was issued Instance().mWiFiState = Instance().mCachedWiFiState; return; @@ -377,13 +377,13 @@ void WiFiManager::ScanDoneHandler(Platform::UniquePtr data) Instance().mWiFiState = WIFI_STATE_ASSOCIATING; - if (int connStatus = net_mgmt(NET_REQUEST_WIFI_CONNECT, Instance().mNetIf, &(Instance().mWiFiParams.mParams), - sizeof(wifi_connect_req_params))) + if (net_mgmt(NET_REQUEST_WIFI_CONNECT, Instance().mNetIf, &(Instance().mWiFiParams.mParams), + sizeof(wifi_connect_req_params))) { ChipLogError(DeviceLayer, "Connection request failed"); if (Instance().mHandling.mOnConnectionDone) { - Instance().mHandling.mOnConnectionDone(connStatus); + Instance().mHandling.mOnConnectionDone(WIFI_STATUS_CONN_FAIL); } Instance().mWiFiState = WIFI_STATE_DISCONNECTED; return; @@ -420,16 +420,17 @@ void WiFiManager::ConnectHandler(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); + uint8_t * rawData = safePtr.get(); + const wifi_status * status = reinterpret_cast(rawData); + wifi_conn_status connStatus = status->conn_status; - if (status->status) + if (connStatus) { ChipLogProgress(DeviceLayer, "Connection to WiFi network failed or was terminated by another request"); Instance().mWiFiState = WIFI_STATE_DISCONNECTED; if (Instance().mHandling.mOnConnectionDone) { - Instance().mHandling.mOnConnectionDone(status->status); + Instance().mHandling.mOnConnectionDone(connStatus); } } else // The connection has been established successfully. @@ -443,7 +444,7 @@ void WiFiManager::ConnectHandler(Platform::UniquePtr data) Instance().mWiFiState = WIFI_STATE_COMPLETED; if (Instance().mHandling.mOnConnectionDone) { - Instance().mHandling.mOnConnectionDone(status->status); + Instance().mHandling.mOnConnectionDone(connStatus); } Instance().PostConnectivityStatusChange(kConnectivity_Established); diff --git a/src/platform/nrfconnect/wifi/WiFiManager.h b/src/platform/nrfconnect/wifi/WiFiManager.h index 9ddb5dea27..2694b050ce 100644 --- a/src/platform/nrfconnect/wifi/WiFiManager.h +++ b/src/platform/nrfconnect/wifi/WiFiManager.h @@ -88,9 +88,10 @@ class Map class WiFiManager { public: + using ScanDoneStatus = decltype(wifi_status::status); using ScanResultCallback = void (*)(const NetworkCommissioning::WiFiScanResponse &); - using ScanDoneCallback = void (*)(const wifi_status &); - using ConnectionCallback = void (*)(int); + using ScanDoneCallback = void (*)(const ScanDoneStatus &); + using ConnectionCallback = void (*)(const wifi_conn_status &); enum class StationStatus : uint8_t { @@ -174,6 +175,7 @@ class WiFiManager CHIP_ERROR ClearStationProvisioningData(); CHIP_ERROR Disconnect(); CHIP_ERROR GetWiFiInfo(WiFiInfo & info) const; + const WiFiNetwork & GetWantedNetwork() const { return mWantedNetwork; } CHIP_ERROR GetNetworkStatistics(NetworkStatistics & stats) const; void AbortConnectionRecovery(); CHIP_ERROR SetLowPowerMode(bool onoff);