Skip to content

Commit

Permalink
Declare state in GDBusWpaSupplicant as std::atomic
Browse files Browse the repository at this point in the history
Allows concurrent access to state from multiple tasks.
  • Loading branch information
tewarid committed Sep 14, 2021
1 parent 64d8f13 commit bbfb4e2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
29 changes: 14 additions & 15 deletions src/platform/Linux/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ bool ConnectivityManagerImpl::_IsWiFiStationConnected()
bool ret = false;
const gchar * state = nullptr;

if (mWpaSupplicant.state != GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED)
if (mWpaSupplicant.state.load(std::memory_order_relaxed) != GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED)
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: _IsWiFiStationConnected: interface not connected");
return false;
Expand All @@ -426,7 +426,7 @@ bool ConnectivityManagerImpl::_IsWiFiStationProvisioned()
bool ret = false;
const gchar * bss = nullptr;

if (mWpaSupplicant.state != GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED)
if (mWpaSupplicant.state.load(std::memory_order_relaxed) != GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED)
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: _IsWiFiStationProvisioned: interface not connected");
return false;
Expand All @@ -443,7 +443,7 @@ bool ConnectivityManagerImpl::_IsWiFiStationProvisioned()

void ConnectivityManagerImpl::_ClearWiFiStationProvision()
{
if (mWpaSupplicant.state != GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED)
if (mWpaSupplicant.state.load(std::memory_order_relaxed) != GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED)
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: _ClearWiFiStationProvision: interface not connected");
return;
Expand All @@ -465,7 +465,7 @@ void ConnectivityManagerImpl::_ClearWiFiStationProvision()

bool ConnectivityManagerImpl::_CanStartWiFiScan()
{
return mWpaSupplicant.state == GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED &&
return mWpaSupplicant.state.load(std::memory_order_relaxed) == GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED &&
mWpaSupplicant.scanState == GDBusWpaSupplicant::WIFI_SCANNING_IDLE;
}

Expand Down Expand Up @@ -571,15 +571,15 @@ void ConnectivityManagerImpl::_OnWpaInterfaceProxyReady(GObject * source_object,
if (iface != nullptr && err == nullptr)
{
mWpaSupplicant.iface = iface;
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED;
mWpaSupplicant.state.store(GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED, std::memory_order_relaxed);
ChipLogProgress(DeviceLayer, "wpa_supplicant: connected to wpa_supplicant interface proxy");
}
else
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: failed to create wpa_supplicant1 interface proxy %s: %s",
mWpaSupplicant.interfacePath, err ? err->message : "unknown error");

mWpaSupplicant.state = GDBusWpaSupplicant::WPA_NOT_CONNECTED;
mWpaSupplicant.state.store(GDBusWpaSupplicant::WPA_NOT_CONNECTED, std::memory_order_relaxed);
}

if (err != nullptr)
Expand All @@ -594,7 +594,7 @@ void ConnectivityManagerImpl::_OnWpaInterfaceReady(GObject * source_object, GAsy
wpa_fi_w1_wpa_supplicant1_call_get_interface_finish(mWpaSupplicant.proxy, &mWpaSupplicant.interfacePath, res, &err);
if (result)
{
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_GOT_INTERFACE_PATH;
mWpaSupplicant.state.store(GDBusWpaSupplicant::WPA_GOT_INTERFACE_PATH, std::memory_order_relaxed);
ChipLogProgress(DeviceLayer, "wpa_supplicant: WiFi interface: %s", mWpaSupplicant.interfacePath);

wpa_fi_w1_wpa_supplicant1_interface_proxy_new_for_bus(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, kWpaSupplicantServiceName,
Expand All @@ -621,7 +621,7 @@ void ConnectivityManagerImpl::_OnWpaInterfaceReady(GObject * source_object, GAsy

if (result)
{
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_GOT_INTERFACE_PATH;
mWpaSupplicant.state.store(GDBusWpaSupplicant::WPA_GOT_INTERFACE_PATH, std::memory_order_relaxed);
ChipLogProgress(DeviceLayer, "wpa_supplicant: WiFi interface: %s", mWpaSupplicant.interfacePath);

wpa_fi_w1_wpa_supplicant1_interface_proxy_new_for_bus(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
Expand All @@ -633,7 +633,7 @@ void ConnectivityManagerImpl::_OnWpaInterfaceReady(GObject * source_object, GAsy
ChipLogProgress(DeviceLayer, "wpa_supplicant: failed to create interface %s: %s",
CHIP_DEVICE_CONFIG_WIFI_STATION_IF_NAME, error ? error->message : "unknown error");

mWpaSupplicant.state = GDBusWpaSupplicant::WPA_NO_INTERFACE_PATH;
mWpaSupplicant.state.store(GDBusWpaSupplicant::WPA_NO_INTERFACE_PATH, std::memory_order_relaxed);

if (mWpaSupplicant.interfacePath)
{
Expand All @@ -659,7 +659,7 @@ void ConnectivityManagerImpl::_OnWpaInterfaceAdded(WpaFiW1Wpa_supplicant1 * prox
mWpaSupplicant.interfacePath = const_cast<gchar *>(path);
if (mWpaSupplicant.interfacePath)
{
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_GOT_INTERFACE_PATH;
mWpaSupplicant.state.store(GDBusWpaSupplicant::WPA_GOT_INTERFACE_PATH, std::memory_order_relaxed);
ChipLogProgress(DeviceLayer, "wpa_supplicant: WiFi interface added: %s", mWpaSupplicant.interfacePath);

wpa_fi_w1_wpa_supplicant1_interface_proxy_new_for_bus(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, kWpaSupplicantServiceName,
Expand All @@ -678,7 +678,7 @@ void ConnectivityManagerImpl::_OnWpaInterfaceRemoved(WpaFiW1Wpa_supplicant1 * pr
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: WiFi interface removed: %s", path);

mWpaSupplicant.state = GDBusWpaSupplicant::WPA_NO_INTERFACE_PATH;
mWpaSupplicant.state.store(GDBusWpaSupplicant::WPA_NO_INTERFACE_PATH, std::memory_order_relaxed);

if (mWpaSupplicant.interfacePath)
{
Expand All @@ -703,7 +703,7 @@ void ConnectivityManagerImpl::_OnWpaProxyReady(GObject * source_object, GAsyncRe
mWpaSupplicant.proxy = wpa_fi_w1_wpa_supplicant1_proxy_new_for_bus_finish(res, &err);
if (mWpaSupplicant.proxy != nullptr && err == nullptr)
{
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_CONNECTED;
mWpaSupplicant.state.store(GDBusWpaSupplicant::WPA_CONNECTED, std::memory_order_relaxed);
ChipLogProgress(DeviceLayer, "wpa_supplicant: connected to wpa_supplicant proxy");

g_signal_connect(mWpaSupplicant.proxy, "interface-added", G_CALLBACK(_OnWpaInterfaceAdded), NULL);
Expand All @@ -717,7 +717,7 @@ void ConnectivityManagerImpl::_OnWpaProxyReady(GObject * source_object, GAsyncRe
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: failed to create wpa_supplicant proxy %s",
err ? err->message : "unknown error");
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_NOT_CONNECTED;
mWpaSupplicant.state.store(GDBusWpaSupplicant::WPA_NOT_CONNECTED, std::memory_order_relaxed);
}

if (err != nullptr)
Expand All @@ -727,7 +727,6 @@ void ConnectivityManagerImpl::_OnWpaProxyReady(GObject * source_object, GAsyncRe
void ConnectivityManagerImpl::StartWiFiManagement()
{
mConnectivityFlag.ClearAll();
mWpaSupplicant.state = GDBusWpaSupplicant::INIT;
mWpaSupplicant.scanState = GDBusWpaSupplicant::WIFI_SCANNING_IDLE;
mWpaSupplicant.proxy = nullptr;
mWpaSupplicant.iface = nullptr;
Expand All @@ -740,7 +739,7 @@ void ConnectivityManagerImpl::StartWiFiManagement()

bool ConnectivityManagerImpl::IsWiFiManagementStarted()
{
return mWpaSupplicant.state == GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED;
return mWpaSupplicant.state.load(std::memory_order_relaxed) == GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED;
}

void ConnectivityManagerImpl::DriveAPState()
Expand Down
7 changes: 5 additions & 2 deletions src/platform/Linux/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include <atomic>
#include <platform/ConnectivityManager.h>
#include <platform/internal/GenericConnectivityManagerImpl.h>
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
Expand Down Expand Up @@ -52,9 +53,10 @@ namespace chip {
namespace DeviceLayer {

#if CHIP_DEVICE_CONFIG_ENABLE_WPA

struct GDBusWpaSupplicant
{
enum
typedef enum
{
INIT,
WPA_CONNECTING,
Expand All @@ -63,7 +65,8 @@ struct GDBusWpaSupplicant
WPA_NO_INTERFACE_PATH,
WPA_GOT_INTERFACE_PATH,
WPA_INTERFACE_CONNECTED,
} state;
} WpaSupplicantState;
std::atomic<WpaSupplicantState> state {INIT};

enum
{
Expand Down

0 comments on commit bbfb4e2

Please sign in to comment.