Skip to content

Commit 1f5605c

Browse files
adigiejukkar
authored andcommitted
[nrf toup][nrfconnect] Reconnect to network after RPU recovery
* Handle supplicant ready/not ready events. * Reconnect to previously connected network. * Restore low power mode. Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no> (cherry picked from commit 36d3ddf)
1 parent 7f1a131 commit 1f5605c

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

src/platform/nrfconnect/wifi/WiFiManager.cpp

+47-14
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,43 @@ void WiFiManager::IPv6MgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mg
180180
}
181181
}
182182

183+
void WiFiManager::SuppEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface)
184+
{
185+
if (mgmtEvent == NET_EVENT_SUPPLICANT_NOT_READY)
186+
{
187+
SystemLayer().ScheduleLambda([] {
188+
if (Instance().mWiFiState == WIFI_STATE_COMPLETED)
189+
{
190+
Instance().mReconnect = true;
191+
Instance().NotifyDisconnected(WLAN_REASON_UNSPECIFIED);
192+
}
193+
});
194+
}
195+
else if (mgmtEvent == NET_EVENT_SUPPLICANT_READY)
196+
{
197+
SystemLayer().ScheduleLambda([] {
198+
if (Instance().mWantedNetwork.IsConfigured() && Instance().mReconnect)
199+
{
200+
Instance().mReconnect = false;
201+
Instance().SetLowPowerMode(Instance().mWiFiPsEnabled);
202+
Instance().Scan(Instance().mWantedNetwork.GetSsidSpan(), nullptr, nullptr, true /* internal scan */);
203+
}
204+
});
205+
}
206+
}
207+
183208
CHIP_ERROR WiFiManager::Init()
184209
{
185210
mNetIf = InetUtils::GetWiFiInterface();
186211
VerifyOrReturnError(mNetIf != nullptr, INET_ERROR_UNKNOWN_INTERFACE);
187212

188213
net_mgmt_init_event_callback(&mWiFiMgmtClbk, WifiMgmtEventHandler, kWifiManagementEvents);
189214
net_mgmt_init_event_callback(&mIPv6MgmtClbk, IPv6MgmtEventHandler, kIPv6ManagementEvents);
215+
net_mgmt_init_event_callback(&mSuppClbk, SuppEventHandler, kSupplicantEvents);
190216

191217
net_mgmt_add_event_callback(&mWiFiMgmtClbk);
192218
net_mgmt_add_event_callback(&mIPv6MgmtClbk);
219+
net_mgmt_add_event_callback(&mSuppClbk);
193220

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

@@ -576,19 +603,7 @@ void WiFiManager::DisconnectHandler(Platform::UniquePtr<uint8_t> data, size_t le
576603
reason = WLAN_REASON_UNSPECIFIED;
577604
break;
578605
}
579-
Instance().SetLastDisconnectReason(reason);
580-
581-
ChipLogProgress(DeviceLayer, "WiFi station disconnected");
582-
Instance().mWiFiState = WIFI_STATE_DISCONNECTED;
583-
Instance().PostConnectivityStatusChange(kConnectivity_Lost);
584-
585-
WiFiDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetWiFiDiagnosticsDelegate();
586-
if (delegate)
587-
{
588-
delegate->OnConnectionStatusChanged(
589-
to_underlying(app::Clusters::WiFiNetworkDiagnostics::ConnectionStatusEnum::kNotConnected));
590-
delegate->OnDisconnectionDetected(reason);
591-
}
606+
Instance().NotifyDisconnected(reason);
592607
});
593608

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

616+
void WiFiManager::NotifyDisconnected(uint16_t reason)
617+
{
618+
SetLastDisconnectReason(reason);
619+
620+
ChipLogProgress(DeviceLayer, "WiFi station disconnected");
621+
mWiFiState = WIFI_STATE_DISCONNECTED;
622+
PostConnectivityStatusChange(kConnectivity_Lost);
623+
624+
WiFiDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetWiFiDiagnosticsDelegate();
625+
if (delegate)
626+
{
627+
delegate->OnConnectionStatusChanged(
628+
to_underlying(app::Clusters::WiFiNetworkDiagnostics::ConnectionStatusEnum::kNotConnected));
629+
delegate->OnDisconnectionDetected(reason);
630+
}
631+
}
632+
601633
void WiFiManager::IPv6AddressChangeHandler(const void * data)
602634
{
603635
const in6_addr * addr = reinterpret_cast<const in6_addr *>(data);
@@ -692,7 +724,8 @@ CHIP_ERROR WiFiManager::SetLowPowerMode(bool onoff)
692724
if ((currentConfig.ps_params.enabled == WIFI_PS_ENABLED && onoff == false) ||
693725
(currentConfig.ps_params.enabled == WIFI_PS_DISABLED && onoff == true))
694726
{
695-
wifi_ps_params params{ .enabled = onoff ? WIFI_PS_ENABLED : WIFI_PS_DISABLED };
727+
mWiFiPsEnabled = onoff;
728+
wifi_ps_params params{ .enabled = mWiFiPsEnabled ? WIFI_PS_ENABLED : WIFI_PS_DISABLED };
696729
if (net_mgmt(NET_REQUEST_WIFI_PS, mNetIf, &params, sizeof(params)))
697730
{
698731
ChipLogError(DeviceLayer, "Set low power mode request failed");

src/platform/nrfconnect/wifi/WiFiManager.h

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <lib/support/Span.h>
2727
#include <platform/CHIPDeviceLayer.h>
2828
#include <platform/NetworkCommissioning.h>
29+
#include <supp_events.h>
2930
#include <system/SystemLayer.h>
3031

3132
#include <zephyr/net/net_if.h>
@@ -205,9 +206,12 @@ class WiFiManager
205206

206207
constexpr static uint32_t kIPv6ManagementEvents = NET_EVENT_IPV6_ADDR_ADD | NET_EVENT_IPV6_ADDR_DEL;
207208

209+
constexpr static uint32_t kSupplicantEvents = NET_EVENT_SUPPLICANT_READY | NET_EVENT_SUPPLICANT_NOT_READY;
210+
208211
// Event handling
209212
static void WifiMgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface);
210213
static void IPv6MgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface);
214+
static void SuppEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface);
211215
static void ScanResultHandler(Platform::UniquePtr<uint8_t> data, size_t length);
212216
static void ScanDoneHandler(Platform::UniquePtr<uint8_t> data, size_t length);
213217
static void ConnectHandler(Platform::UniquePtr<uint8_t> data, size_t length);
@@ -229,6 +233,8 @@ class WiFiManager
229233
void ResetRecoveryTime();
230234
System::Clock::Milliseconds32 CalculateNextRecoveryTime();
231235

236+
void NotifyDisconnected(uint16_t reason);
237+
232238
net_if * mNetIf{ nullptr };
233239
ConnectionParams mWiFiParams{};
234240
ConnectionHandling mHandling{};
@@ -238,6 +244,7 @@ class WiFiManager
238244
wifi_iface_state mCachedWiFiState;
239245
net_mgmt_event_callback mWiFiMgmtClbk{};
240246
net_mgmt_event_callback mIPv6MgmtClbk{};
247+
net_mgmt_event_callback mSuppClbk{};
241248
ScanResultCallback mScanResultCallback{ nullptr };
242249
ScanDoneCallback mScanDoneCallback{ nullptr };
243250
WiFiNetwork mWantedNetwork{};
@@ -248,6 +255,8 @@ class WiFiManager
248255
uint32_t mConnectionRecoveryTimeMs{ kConnectionRecoveryMinIntervalMs };
249256
bool mApplicationDisconnectRequested{ false };
250257
uint16_t mLastDisconnectedReason = WLAN_REASON_UNSPECIFIED;
258+
bool mReconnect{ false };
259+
bool mWiFiPsEnabled{ true };
251260

252261
static const Map<wifi_iface_state, StationStatus, 10> sStatusMap;
253262
static const Map<uint32_t, NetEventHandler, 5> sEventHandlerMap;

0 commit comments

Comments
 (0)