Skip to content

Commit

Permalink
[nrf toup][nrfconnect] Reconnect to network after RPU recovery
Browse files Browse the repository at this point in the history
* Handle supplicant ready/not ready events.
* Reconnect to previously connected network.
* Restore low power mode.

Signed-off-by: Adrian Gielniewski <[email protected]>
  • Loading branch information
adigie committed Jan 16, 2025
1 parent b237efc commit aaf2598
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
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_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 */);
}
});
}
if (mgmtEvent == NET_EVENT_SUPPLICANT_NOT_READY)
{
SystemLayer().ScheduleLambda([] {
if (Instance().mWiFiState == WIFI_STATE_COMPLETED)
{
Instance().mReconnect = true;
Instance().NotifyDisconnected(WLAN_REASON_UNSPECIFIED);
}
});
}
}

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 @@ -205,9 +206,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 @@ -229,6 +233,8 @@ class WiFiManager
void ResetRecoveryTime();
System::Clock::Milliseconds32 CalculateNextRecoveryTime();

void NotifyDisconnected(uint16_t reason);

net_if * mNetIf{ nullptr };
ConnectionParams mWiFiParams{};
ConnectionHandling mHandling{};
Expand All @@ -238,6 +244,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 @@ -248,6 +255,8 @@ class WiFiManager
uint32_t mConnectionRecoveryTimeMs{ kConnectionRecoveryMinIntervalMs };
bool mApplicationDisconnectRequested{ false };
uint16_t mLastDisconnectedReason = WLAN_REASON_UNSPECIFIED;
bool mReconnect{ false };
bool mWiFiPsEnabled{};

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

0 comments on commit aaf2598

Please sign in to comment.