Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport v2.8-branch][nrf toup][nrfconnect] Reconnect to network after RPU recovery #532

Open
wants to merge 1 commit into
base: v2.8-branch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 47 additions & 14 deletions src/platform/nrfconnect/wifi/WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,43 @@ void WiFiManager::IPv6MgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mg
}
}

void WiFiManager::SuppEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface)
{
if (mgmtEvent == NET_EVENT_SUPPLICANT_NOT_READY)
{
SystemLayer().ScheduleLambda([] {
if (Instance().mWiFiState == WIFI_STATE_COMPLETED)
{
Instance().mReconnect = true;
Instance().NotifyDisconnected(WLAN_REASON_UNSPECIFIED);
}
});
}
else if (mgmtEvent == NET_EVENT_SUPPLICANT_READY)
{
SystemLayer().ScheduleLambda([] {
if (Instance().mWantedNetwork.IsConfigured() && Instance().mReconnect)
{
Instance().mReconnect = false;
Instance().SetLowPowerMode(Instance().mWiFiPsEnabled);
Instance().Scan(Instance().mWantedNetwork.GetSsidSpan(), nullptr, nullptr, true /* internal scan */);
}
});
}
}

CHIP_ERROR WiFiManager::Init()
{
mNetIf = InetUtils::GetWiFiInterface();
VerifyOrReturnError(mNetIf != nullptr, INET_ERROR_UNKNOWN_INTERFACE);

net_mgmt_init_event_callback(&mWiFiMgmtClbk, WifiMgmtEventHandler, kWifiManagementEvents);
net_mgmt_init_event_callback(&mIPv6MgmtClbk, IPv6MgmtEventHandler, kIPv6ManagementEvents);
net_mgmt_init_event_callback(&mSuppClbk, SuppEventHandler, kSupplicantEvents);

net_mgmt_add_event_callback(&mWiFiMgmtClbk);
net_mgmt_add_event_callback(&mIPv6MgmtClbk);
net_mgmt_add_event_callback(&mSuppClbk);

ChipLogDetail(DeviceLayer, "WiFiManager has been initialized");

Expand Down Expand Up @@ -576,19 +603,7 @@ void WiFiManager::DisconnectHandler(Platform::UniquePtr<uint8_t> data, size_t le
reason = WLAN_REASON_UNSPECIFIED;
break;
}
Instance().SetLastDisconnectReason(reason);

ChipLogProgress(DeviceLayer, "WiFi station disconnected");
Instance().mWiFiState = WIFI_STATE_DISCONNECTED;
Instance().PostConnectivityStatusChange(kConnectivity_Lost);

WiFiDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetWiFiDiagnosticsDelegate();
if (delegate)
{
delegate->OnConnectionStatusChanged(
to_underlying(app::Clusters::WiFiNetworkDiagnostics::ConnectionStatusEnum::kNotConnected));
delegate->OnDisconnectionDetected(reason);
}
Instance().NotifyDisconnected(reason);
});

if (CHIP_NO_ERROR == err)
Expand All @@ -598,6 +613,23 @@ void WiFiManager::DisconnectHandler(Platform::UniquePtr<uint8_t> data, size_t le
}
}

void WiFiManager::NotifyDisconnected(uint16_t reason)
{
SetLastDisconnectReason(reason);

ChipLogProgress(DeviceLayer, "WiFi station disconnected");
mWiFiState = WIFI_STATE_DISCONNECTED;
PostConnectivityStatusChange(kConnectivity_Lost);

WiFiDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetWiFiDiagnosticsDelegate();
if (delegate)
{
delegate->OnConnectionStatusChanged(
to_underlying(app::Clusters::WiFiNetworkDiagnostics::ConnectionStatusEnum::kNotConnected));
delegate->OnDisconnectionDetected(reason);
}
}

void WiFiManager::IPv6AddressChangeHandler(const void * data)
{
const in6_addr * addr = reinterpret_cast<const in6_addr *>(data);
Expand Down Expand Up @@ -692,7 +724,8 @@ CHIP_ERROR WiFiManager::SetLowPowerMode(bool onoff)
if ((currentConfig.ps_params.enabled == WIFI_PS_ENABLED && onoff == false) ||
(currentConfig.ps_params.enabled == WIFI_PS_DISABLED && onoff == true))
{
wifi_ps_params params{ .enabled = onoff ? WIFI_PS_ENABLED : WIFI_PS_DISABLED };
mWiFiPsEnabled = onoff;
wifi_ps_params params{ .enabled = mWiFiPsEnabled ? WIFI_PS_ENABLED : WIFI_PS_DISABLED };
if (net_mgmt(NET_REQUEST_WIFI_PS, mNetIf, &params, sizeof(params)))
{
ChipLogError(DeviceLayer, "Set low power mode request failed");
Expand Down
9 changes: 9 additions & 0 deletions src/platform/nrfconnect/wifi/WiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <lib/support/Span.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/NetworkCommissioning.h>
#include <supp_events.h>
#include <system/SystemLayer.h>

#include <zephyr/net/net_if.h>
Expand Down Expand Up @@ -196,9 +197,12 @@ class WiFiManager

constexpr static uint32_t kIPv6ManagementEvents = NET_EVENT_IPV6_ADDR_ADD | NET_EVENT_IPV6_ADDR_DEL;

constexpr static uint32_t kSupplicantEvents = NET_EVENT_SUPPLICANT_READY | NET_EVENT_SUPPLICANT_NOT_READY;

// Event handling
static void WifiMgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface);
static void IPv6MgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface);
static void SuppEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface);
static void ScanResultHandler(Platform::UniquePtr<uint8_t> data, size_t length);
static void ScanDoneHandler(Platform::UniquePtr<uint8_t> data, size_t length);
static void ConnectHandler(Platform::UniquePtr<uint8_t> data, size_t length);
Expand All @@ -220,6 +224,8 @@ class WiFiManager
void ResetRecoveryTime();
System::Clock::Milliseconds32 CalculateNextRecoveryTime();

void NotifyDisconnected(uint16_t reason);

net_if * mNetIf{ nullptr };
ConnectionParams mWiFiParams{};
ConnectionHandling mHandling{};
Expand All @@ -229,6 +235,7 @@ class WiFiManager
wifi_iface_state mCachedWiFiState;
net_mgmt_event_callback mWiFiMgmtClbk{};
net_mgmt_event_callback mIPv6MgmtClbk{};
net_mgmt_event_callback mSuppClbk{};
ScanResultCallback mScanResultCallback{ nullptr };
ScanDoneCallback mScanDoneCallback{ nullptr };
WiFiNetwork mWantedNetwork{};
Expand All @@ -239,6 +246,8 @@ class WiFiManager
uint32_t mConnectionRecoveryTimeMs{ kConnectionRecoveryMinIntervalMs };
bool mApplicationDisconnectRequested{ false };
uint16_t mLastDisconnectedReason = WLAN_REASON_UNSPECIFIED;
bool mReconnect{ false };
bool mWiFiPsEnabled{ true };

static const Map<wifi_iface_state, StationStatus, 10> sStatusMap;
static const Map<uint32_t, NetEventHandler, 5> sEventHandlerMap;
Expand Down
Loading