Skip to content

Commit db7cd2e

Browse files
[nrf fromlist][nrfconnect] Add checking of Wi-Fi supplicant and Interface
We need to handle the Wi-Fi supplicant events to verify whether the Wi-Fi supplicant and Interface are ready to use.
1 parent 623ef0a commit db7cd2e

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/platform/nrfconnect/wifi/WiFiManager.cpp

+41-1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,33 @@ const Map<uint32_t, WiFiManager::NetEventHandler, 5> WiFiManager::sEventHandlerM
144144
{ NET_EVENT_WIFI_DISCONNECT_COMPLETE, WiFiManager::DisconnectHandler },
145145
});
146146

147+
void WiFiManager::WiFiSupplicantEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface)
148+
{
149+
switch (mgmtEvent)
150+
{
151+
case NET_EVENT_WPA_SUPP_READY:
152+
Instance().mSupplicantReady = true;
153+
break;
154+
case NET_EVENT_WPA_SUPP_NOT_READY:
155+
Instance().mSupplicantReady = false;
156+
break;
157+
case NET_EVENT_WPA_SUPP_IFACE_ADDED:
158+
Instance().mInterfaceUp = true;
159+
break;
160+
case NET_EVENT_WPA_SUPP_IFACE_REMOVED:
161+
Instance().mInterfaceUp = false;
162+
break;
163+
default:
164+
break;
165+
}
166+
167+
if (Instance().mSupplicantReady && Instance().mInterfaceUp)
168+
{
169+
// In this case the Supplicant and Interface is fully ready.
170+
DeviceLayer::SystemLayer().CancelTimer(SupplicantInitTimeout, nullptr);
171+
}
172+
}
173+
147174
void WiFiManager::WifiMgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface)
148175
{
149176
if (iface == Instance().mNetIf)
@@ -170,11 +197,16 @@ CHIP_ERROR WiFiManager::Init()
170197

171198
net_mgmt_init_event_callback(&mWiFiMgmtClbk, WifiMgmtEventHandler, kWifiManagementEvents);
172199
net_mgmt_init_event_callback(&mIPv6MgmtClbk, IPv6MgmtEventHandler, kIPv6ManagementEvents);
200+
net_mgmt_init_event_callback(&mSuppMgmtClbk, WiFiSupplicantEventHandler, kSupplicantEvents);
173201

174202
net_mgmt_add_event_callback(&mWiFiMgmtClbk);
175203
net_mgmt_add_event_callback(&mIPv6MgmtClbk);
204+
net_mgmt_add_event_callback(&mSuppMgmtClbk);
176205

177-
ChipLogDetail(DeviceLayer, "WiFiManager has been initialized");
206+
// Set the timer and wait for the WiFi supplicant and interface ready.
207+
DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(kSupplicantReadyTimeoutMs), SupplicantInitTimeout, nullptr);
208+
209+
ChipLogDetail(DeviceLayer, "WiFiManager initialization requested");
178210

179211
return CHIP_NO_ERROR;
180212
}
@@ -588,5 +620,13 @@ CHIP_ERROR WiFiManager::SetLowPowerMode(bool onoff)
588620
return CHIP_NO_ERROR;
589621
}
590622

623+
void WiFiManager::SupplicantInitTimeout(System::Layer * layer, void * param)
624+
{
625+
ChipLogError(DeviceLayer, "Wi-Fi supplicant and interface have not been initialized!");
626+
// Wi-Fi driver must be initialized within the given timeout, when it is still not ready, do not to allow any further
627+
// operations.
628+
VerifyOrDie(Instance().mSupplicantReady && Instance().mInterfaceUp);
629+
}
630+
591631
} // namespace DeviceLayer
592632
} // namespace chip

src/platform/nrfconnect/wifi/WiFiManager.h

+11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
extern "C" {
3535
#include <src/utils/common.h>
36+
#include <supp_events.h>
3637
#include <wpa_supplicant/wpa_supplicant_i.h>
3738
}
3839

@@ -173,6 +174,7 @@ class WiFiManager
173174
static constexpr uint32_t kConnectionRecoveryMaxIntervalMs = CONFIG_CHIP_WIFI_CONNECTION_RECOVERY_MAXIMUM_INTERVAL;
174175
static constexpr uint32_t kConnectionRecoveryJitterMs = CONFIG_CHIP_WIFI_CONNECTION_RECOVERY_JITTER;
175176
static constexpr uint32_t kConnectionRecoveryMaxRetries = CONFIG_CHIP_WIFI_CONNECTION_RECOVERY_MAX_RETRIES_NUMBER;
177+
static constexpr uint32_t kSupplicantReadyTimeoutMs = 500;
176178

177179
CHIP_ERROR Init();
178180
CHIP_ERROR Scan(const ByteSpan & ssid, ScanResultCallback resultCallback, ScanDoneCallback doneCallback,
@@ -200,9 +202,13 @@ class WiFiManager
200202

201203
constexpr static uint32_t kIPv6ManagementEvents = NET_EVENT_IPV6_ADDR_ADD | NET_EVENT_IPV6_ADDR_DEL;
202204

205+
constexpr static uint32_t kSupplicantEvents = NET_EVENT_WPA_SUPP_READY | NET_EVENT_WPA_SUPP_CMD_NOT_READY |
206+
NET_EVENT_WPA_SUPP_IFACE_ADDED | NET_EVENT_WPA_SUPP_IFACE_REMOVED;
207+
203208
// Event handling
204209
static void WifiMgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface);
205210
static void IPv6MgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface);
211+
static void WiFiSupplicantEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface);
206212
static void ScanResultHandler(Platform::UniquePtr<uint8_t> data);
207213
static void ScanDoneHandler(Platform::UniquePtr<uint8_t> data);
208214
static void ConnectHandler(Platform::UniquePtr<uint8_t> data);
@@ -221,16 +227,21 @@ class WiFiManager
221227
// To avoid frequent recovery attempts when the signal to an access point is poor quality
222228
// The connection recovery interval will be cleared after the defined delay in kConnectionRecoveryDelayToReset.
223229
static void Recover(System::Layer * layer, void * param);
230+
static void SupplicantInitTimeout(System::Layer * layer, void * param);
224231
void ResetRecoveryTime();
225232
System::Clock::Milliseconds32 CalculateNextRecoveryTime();
226233

234+
bool mSupplicantReady{ false };
235+
bool mInterfaceUp{ false };
236+
bool mSupplicantInitTimeoutElapsed{ false };
227237
net_if * mNetIf{ nullptr };
228238
ConnectionParams mWiFiParams{};
229239
ConnectionHandling mHandling;
230240
wifi_iface_state mWiFiState;
231241
wifi_iface_state mCachedWiFiState;
232242
net_mgmt_event_callback mWiFiMgmtClbk{};
233243
net_mgmt_event_callback mIPv6MgmtClbk{};
244+
net_mgmt_event_callback mSuppMgmtClbk{};
234245
ScanResultCallback mScanResultCallback{ nullptr };
235246
ScanDoneCallback mScanDoneCallback{ nullptr };
236247
WiFiNetwork mWantedNetwork{};

0 commit comments

Comments
 (0)