Skip to content

Commit

Permalink
[nrf noup] [nrfconnect] Minor Wi-Fi refinements
Browse files Browse the repository at this point in the history
* error code handling unification
* added GetWantedNetwork getter
  and use it when handling network status change
* minor refactoring

Signed-off-by: Marcin Kajor <marcin.kajor@nordicsemi.no>
  • Loading branch information
markaj-nordic committed Mar 19, 2024
1 parent 7653e6c commit 1d3bce8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
37 changes: 26 additions & 11 deletions src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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)
Expand All @@ -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<int32_t>(connStatus))
: NullOptional);
}

if (mpConnectCallback)
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

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

Expand Down Expand Up @@ -342,21 +341,22 @@ 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);
uint8_t * rawData = safePtr.get();
const wifi_status * status = reinterpret_cast<const wifi_status *>(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;
Expand All @@ -377,13 +377,13 @@ void WiFiManager::ScanDoneHandler(Platform::UniquePtr<uint8_t> 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;
Expand Down Expand Up @@ -420,16 +420,17 @@ void WiFiManager::ConnectHandler(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);
uint8_t * rawData = safePtr.get();
const wifi_status * status = reinterpret_cast<const wifi_status *>(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.
Expand All @@ -443,7 +444,7 @@ void WiFiManager::ConnectHandler(Platform::UniquePtr<uint8_t> data)
Instance().mWiFiState = WIFI_STATE_COMPLETED;
if (Instance().mHandling.mOnConnectionDone)
{
Instance().mHandling.mOnConnectionDone(status->status);
Instance().mHandling.mOnConnectionDone(connStatus);
}
Instance().PostConnectivityStatusChange(kConnectivity_Established);

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

0 comments on commit 1d3bce8

Please sign in to comment.