Skip to content

Commit

Permalink
Wait for Wi-Fi management to start up (#8834) (#9322)
Browse files Browse the repository at this point in the history
* Wait for Wi-Fi management to start up (#8834)

* Restyled by clang-format

* Extract code in function EnsureWifiIsStarted

* Restyled by whitespace

* Fix unused variable warning/error

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
tewarid and restyled-commits authored Aug 31, 2021
1 parent 059ee32 commit 7b0afe0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions src/platform/Linux/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/platform/Linux/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager,

#if CHIP_DEVICE_CONFIG_ENABLE_WPA
void StartWiFiManagement();
bool IsWiFiManagementStarted();
#endif

private:
Expand Down

0 comments on commit 7b0afe0

Please sign in to comment.