Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock concurrent access to mWpaSupplicant #9704

Merged
merged 2 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/platform/Linux/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
BitFlags<Internal::GenericConnectivityManagerImpl_WiFi<ConnectivityManagerImpl>::ConnectivityFlags>
ConnectivityManagerImpl::mConnectivityFlag;
struct GDBusWpaSupplicant ConnectivityManagerImpl::mWpaSupplicant;
std::mutex ConnectivityManagerImpl::mWpaSupplicantMutex;

bool ConnectivityManagerImpl::_HaveIPv4InternetConnectivity()
{
Expand Down Expand Up @@ -399,6 +400,8 @@ bool ConnectivityManagerImpl::_IsWiFiStationConnected()
bool ret = false;
const gchar * state = nullptr;

std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

if (mWpaSupplicant.state != GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED)
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: _IsWiFiStationConnected: interface not connected");
Expand Down Expand Up @@ -426,6 +429,8 @@ bool ConnectivityManagerImpl::_IsWiFiStationProvisioned()
bool ret = false;
const gchar * bss = nullptr;

std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);
mspang marked this conversation as resolved.
Show resolved Hide resolved

if (mWpaSupplicant.state != GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED)
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: _IsWiFiStationProvisioned: interface not connected");
Expand All @@ -443,6 +448,8 @@ bool ConnectivityManagerImpl::_IsWiFiStationProvisioned()

void ConnectivityManagerImpl::_ClearWiFiStationProvision()
{
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

if (mWpaSupplicant.state != GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED)
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: _ClearWiFiStationProvision: interface not connected");
Expand All @@ -465,8 +472,12 @@ void ConnectivityManagerImpl::_ClearWiFiStationProvision()

bool ConnectivityManagerImpl::_CanStartWiFiScan()
{
return mWpaSupplicant.state == GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED &&
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

bool ret = mWpaSupplicant.state == GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED &&
mWpaSupplicant.scanState == GDBusWpaSupplicant::WIFI_SCANNING_IDLE;

return ret;
}

CHIP_ERROR ConnectivityManagerImpl::_SetWiFiAPMode(WiFiAPMode val)
Expand Down Expand Up @@ -559,7 +570,10 @@ CHIP_ERROR ConnectivityManagerImpl::_GetEthOverrunCount(uint64_t & overrunCount)

void ConnectivityManagerImpl::_OnWpaInterfaceProxyReady(GObject * source_object, GAsyncResult * res, gpointer user_data)
{
GError * err = nullptr;
GError * err = nullptr;

std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

WpaFiW1Wpa_supplicant1Interface * iface = wpa_fi_w1_wpa_supplicant1_interface_proxy_new_for_bus_finish(res, &err);

if (mWpaSupplicant.iface)
Expand Down Expand Up @@ -590,6 +604,8 @@ void ConnectivityManagerImpl::_OnWpaInterfaceReady(GObject * source_object, GAsy
{
GError * err = nullptr;

std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

gboolean result =
wpa_fi_w1_wpa_supplicant1_call_get_interface_finish(mWpaSupplicant.proxy, &mWpaSupplicant.interfacePath, res, &err);
if (result)
Expand Down Expand Up @@ -653,8 +669,12 @@ void ConnectivityManagerImpl::_OnWpaInterfaceReady(GObject * source_object, GAsy
void ConnectivityManagerImpl::_OnWpaInterfaceAdded(WpaFiW1Wpa_supplicant1 * proxy, const gchar * path, GVariant * properties,
gpointer user_data)
{
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

if (mWpaSupplicant.interfacePath)
{
return;
}

mWpaSupplicant.interfacePath = const_cast<gchar *>(path);
if (mWpaSupplicant.interfacePath)
Expand All @@ -671,8 +691,12 @@ void ConnectivityManagerImpl::_OnWpaInterfaceAdded(WpaFiW1Wpa_supplicant1 * prox
void ConnectivityManagerImpl::_OnWpaInterfaceRemoved(WpaFiW1Wpa_supplicant1 * proxy, const gchar * path, GVariant * properties,
gpointer user_data)
{
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

if (mWpaSupplicant.interfacePath == nullptr)
{
return;
}

if (g_strcmp0(mWpaSupplicant.interfacePath, path) == 0)
{
Expand Down Expand Up @@ -700,6 +724,8 @@ void ConnectivityManagerImpl::_OnWpaProxyReady(GObject * source_object, GAsyncRe
{
GError * err = nullptr;

std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

mWpaSupplicant.proxy = wpa_fi_w1_wpa_supplicant1_proxy_new_for_bus_finish(res, &err);
if (mWpaSupplicant.proxy != nullptr && err == nullptr)
{
Expand Down Expand Up @@ -740,7 +766,11 @@ void ConnectivityManagerImpl::StartWiFiManagement()

bool ConnectivityManagerImpl::IsWiFiManagementStarted()
{
return mWpaSupplicant.state == GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED;
std::lock_guard<std::mutex> lock(mWpaSupplicantMutex);

bool ret = mWpaSupplicant.state == GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED;

return ret;
}

void ConnectivityManagerImpl::DriveAPState()
Expand Down
2 changes: 2 additions & 0 deletions src/platform/Linux/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ class ConnectivityManagerImpl final : public ConnectivityManager,

static BitFlags<ConnectivityFlags> mConnectivityFlag;
static struct GDBusWpaSupplicant mWpaSupplicant;
static std::mutex mWpaSupplicantMutex;

#endif

// ==================== ConnectivityManager Private Methods ====================
Expand Down