Skip to content

Commit

Permalink
[nrf noup] Fix various Wi-Fi issues with error code handling
Browse files Browse the repository at this point in the history
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 <lukasz.duda@nordicsemi.no>
  • Loading branch information
LuDuda authored and markaj-nordic committed Mar 8, 2024
1 parent abb645f commit 1e9f9b2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 42 deletions.
22 changes: 11 additions & 11 deletions src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/platform/nrfconnect/wifi/NrfWiFiDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
33 changes: 15 additions & 18 deletions src/platform/nrfconnect/wifi/WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -343,11 +342,10 @@ void WiFiManager::ScanDoneHandler(Platform::UniquePtr<uint8_t> data)
{
CHIP_ERROR err = SystemLayer().ScheduleLambda([capturedData = data.get()] {
Platform::UniquePtr<uint8_t> safePtr(capturedData);
uint8_t * rawData = safePtr.get();
const wifi_status * status = reinterpret_cast<const wifi_status *>(rawData);
WiFiRequestStatus requestStatus = static_cast<WiFiRequestStatus>(status->status);
uint8_t * rawData = safePtr.get();
const wifi_status * status = reinterpret_cast<const wifi_status *>(rawData);

if (requestStatus == WiFiRequestStatus::FAILURE)
if (status->status)
{
ChipLogError(DeviceLayer, "Wi-Fi scan finalization failure (%d)", status->status);
}
Expand All @@ -358,7 +356,7 @@ void WiFiManager::ScanDoneHandler(Platform::UniquePtr<uint8_t> 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;
Expand All @@ -379,13 +377,13 @@ void WiFiManager::ScanDoneHandler(Platform::UniquePtr<uint8_t> 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;
Expand Down Expand Up @@ -424,15 +422,14 @@ void WiFiManager::ConnectHandler(Platform::UniquePtr<uint8_t> data)
Platform::UniquePtr<uint8_t> safePtr(capturedData);
uint8_t * rawData = safePtr.get();
const wifi_status * status = reinterpret_cast<const wifi_status *>(rawData);
WiFiRequestStatus requestStatus = static_cast<WiFiRequestStatus>(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.
Expand All @@ -444,9 +441,9 @@ void WiFiManager::ConnectHandler(Platform::UniquePtr<uint8_t> 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);

Expand Down
14 changes: 3 additions & 11 deletions src/platform/nrfconnect/wifi/WiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -120,8 +113,7 @@ class WiFiManager

struct ConnectionHandling
{
ConnectionCallback mOnConnectionSuccess{};
ConnectionCallback mOnConnectionFailed{};
ConnectionCallback mOnConnectionDone{};
System::Clock::Seconds32 mConnectionTimeout{};
};

Expand Down

0 comments on commit 1e9f9b2

Please sign in to comment.