diff --git a/examples/platform/linux/AppMain.cpp b/examples/platform/linux/AppMain.cpp index ed7c352f43f914..784f932f4e9664 100644 --- a/examples/platform/linux/AppMain.cpp +++ b/examples/platform/linux/AppMain.cpp @@ -65,6 +65,17 @@ using namespace chip::Transport; using chip::Shell::Engine; #endif +#if CHIP_DEVICE_CONFIG_ENABLE_WPA +/* + * The device shall check every kWifiStartCheckTimeUsec whether Wi-Fi management + * has been fully initialized. If after kWifiStartCheckAttempts Wi-Fi management + * still hasn't been initialized, the device configuration is reset, and device + * needs to be paired again. + */ +static constexpr useconds_t kWifiStartCheckTimeUsec = 100 * 1000; // 100 ms +static constexpr uint8_t kWifiStartCheckAttempts = 5; +#endif + namespace { void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg) { @@ -76,6 +87,23 @@ void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg } } // namespace +#if CHIP_DEVICE_CONFIG_ENABLE_WPA +static bool EnsureWifiIsStarted() +{ + for (int cnt = 0; cnt < kWifiStartCheckAttempts; cnt++) + { + if (chip::DeviceLayer::ConnectivityMgrImpl().IsWiFiManagementStarted()) + { + return true; + } + + usleep(kWifiStartCheckTimeUsec); + } + + return chip::DeviceLayer::ConnectivityMgrImpl().IsWiFiManagementStarted(); +} +#endif + int ChipLinuxAppInit(int argc, char ** argv) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -120,6 +148,10 @@ int ChipLinuxAppInit(int argc, char ** argv) if (LinuxDeviceOptions::GetInstance().mWiFi) { chip::DeviceLayer::ConnectivityMgrImpl().StartWiFiManagement(); + if (!EnsureWifiIsStarted()) + { + ChipLogError(NotSpecified, "Wi-Fi Management taking too long to start - device configuration will be reset."); + } } #endif // CHIP_DEVICE_CONFIG_ENABLE_WPA diff --git a/src/platform/Linux/ConnectivityManagerImpl.cpp b/src/platform/Linux/ConnectivityManagerImpl.cpp index a79fda4f5c0012..1e57cfebb6f73b 100644 --- a/src/platform/Linux/ConnectivityManagerImpl.cpp +++ b/src/platform/Linux/ConnectivityManagerImpl.cpp @@ -633,6 +633,11 @@ void ConnectivityManagerImpl::StartWiFiManagement() kWpaSupplicantObjectPath, nullptr, _OnWpaProxyReady, nullptr); } +bool ConnectivityManagerImpl::IsWiFiManagementStarted() +{ + return mWpaSupplicant.state == GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED; +} + void ConnectivityManagerImpl::DriveAPState() { CHIP_ERROR err = CHIP_NO_ERROR; diff --git a/src/platform/Linux/ConnectivityManagerImpl.h b/src/platform/Linux/ConnectivityManagerImpl.h index b850ad76c254e1..e2ef99d64b0438 100644 --- a/src/platform/Linux/ConnectivityManagerImpl.h +++ b/src/platform/Linux/ConnectivityManagerImpl.h @@ -108,6 +108,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager, #if CHIP_DEVICE_CONFIG_ENABLE_WPA void StartWiFiManagement(); + bool IsWiFiManagementStarted(); #endif private: