-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[linux] Add WiFi support in ConnectivityManager. #1975
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,10 +34,21 @@ | |
#include <platform/internal/GenericConnectivityManagerImpl_Thread.ipp> | ||
#endif | ||
|
||
#if CHIP_DEVICE_CONFIG_ENABLE_WPA | ||
#include <platform/internal/GenericConnectivityManagerImpl_WiFi.ipp> | ||
#endif | ||
|
||
using namespace ::chip; | ||
using namespace ::chip::TLV; | ||
using namespace ::chip::DeviceLayer::Internal; | ||
|
||
#if CHIP_DEVICE_CONFIG_ENABLE_WPA | ||
namespace { | ||
const char kWpaSupplicantServiceName[] = "fi.w1.wpa_supplicant1"; | ||
const char kWpaSupplicantObjectPath[] = "/fi/w1/wpa_supplicant1"; | ||
} // namespace | ||
#endif | ||
|
||
namespace chip { | ||
namespace DeviceLayer { | ||
|
||
|
@@ -52,6 +63,19 @@ CHIP_ERROR ConnectivityManagerImpl::_Init() | |
GenericConnectivityManagerImpl_Thread<ConnectivityManagerImpl>::_Init(); | ||
#endif | ||
|
||
#if CHIP_DEVICE_CONFIG_ENABLE_WPA | ||
mConnectivityFlag = 0; | ||
mWpaSupplicant.state = GDBusWpaSupplicant::INIT; | ||
mWpaSupplicant.scanState = GDBusWpaSupplicant::WIFI_SCANNING_IDLE; | ||
mWpaSupplicant.proxy = NULL; | ||
mWpaSupplicant.iface = NULL; | ||
mWpaSupplicant.interfacePath = NULL; | ||
mWpaSupplicant.networkPath = NULL; | ||
|
||
wpa_fi_w1_wpa_supplicant1_proxy_new_for_bus(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, kWpaSupplicantServiceName, | ||
kWpaSupplicantObjectPath, NULL, _OnWpaProxyReady, NULL); | ||
#endif | ||
|
||
SuccessOrExit(err); | ||
|
||
exit: | ||
|
@@ -66,5 +90,186 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) | |
#endif | ||
} | ||
|
||
#if CHIP_DEVICE_CONFIG_ENABLE_WPA | ||
|
||
uint16_t ConnectivityManagerImpl::mConnectivityFlag; | ||
struct GDBusWpaSupplicant ConnectivityManagerImpl::mWpaSupplicant; | ||
|
||
bool ConnectivityManagerImpl::_HaveIPv4InternetConnectivity(void) | ||
{ | ||
return ((mConnectivityFlag & kFlag_HaveIPv4InternetConnectivity) != 0); | ||
} | ||
|
||
bool ConnectivityManagerImpl::_HaveIPv6InternetConnectivity(void) | ||
{ | ||
return ((mConnectivityFlag & kFlag_HaveIPv6InternetConnectivity) != 0); | ||
} | ||
|
||
bool ConnectivityManagerImpl::_IsWiFiStationConnected(void) | ||
{ | ||
bool ret = false; | ||
const gchar * state = NULL; | ||
|
||
if (mWpaSupplicant.state != GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED) | ||
{ | ||
ChipLogProgress(DeviceLayer, "wpa_supplicant: _IsWiFiStationConnected: interface not connected"); | ||
return false; | ||
} | ||
|
||
state = wpa_fi_w1_wpa_supplicant1_interface_get_state(mWpaSupplicant.iface); | ||
ChipLogProgress(DeviceLayer, "wpa_supplicant: wpa_fi_w1_wpa_supplicant1_interface_get_state: %s", state); | ||
|
||
if (g_strcmp0(state, "completed") == 0) | ||
{ | ||
ChipLogProgress(DeviceLayer, "wpa_supplicant: WiFi already connected"); | ||
SetFlag(mConnectivityFlag, kFlag_HaveIPv4InternetConnectivity, true); | ||
SetFlag(mConnectivityFlag, kFlag_HaveIPv6InternetConnectivity, true); | ||
ret = true; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
bool ConnectivityManagerImpl::_CanStartWiFiScan() | ||
{ | ||
return mWpaSupplicant.state == GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED && | ||
mWpaSupplicant.scanState == GDBusWpaSupplicant::WIFI_SCANNING_IDLE; | ||
} | ||
|
||
void ConnectivityManagerImpl::_OnWpaInterfaceProxyReady(GObject * source_object, GAsyncResult * res, gpointer user_data) | ||
{ | ||
GError * err = NULL; | ||
WpaFiW1Wpa_supplicant1Interface * iface = wpa_fi_w1_wpa_supplicant1_interface_proxy_new_for_bus_finish(res, &err); | ||
|
||
if (mWpaSupplicant.iface) | ||
{ | ||
g_object_unref(mWpaSupplicant.iface); | ||
mWpaSupplicant.iface = NULL; | ||
} | ||
|
||
if (iface != NULL && err == NULL) | ||
{ | ||
mWpaSupplicant.iface = iface; | ||
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_INTERFACE_CONNECTED; | ||
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; | ||
} | ||
|
||
if (err != NULL) | ||
g_error_free(err); | ||
} | ||
|
||
void ConnectivityManagerImpl::_OnWpaInterfaceReady(GObject * source_object, GAsyncResult * res, gpointer user_data) | ||
{ | ||
GError * err = NULL; | ||
|
||
gboolean result = | ||
wpa_fi_w1_wpa_supplicant1_call_get_interface_finish(mWpaSupplicant.proxy, &mWpaSupplicant.interfacePath, res, &err); | ||
if (result) | ||
{ | ||
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_GOT_INTERFACE_PATH; | ||
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, | ||
mWpaSupplicant.interfacePath, NULL, _OnWpaInterfaceProxyReady, NULL); | ||
} | ||
else | ||
{ | ||
ChipLogProgress(DeviceLayer, "wpa_supplicant: can't find interface %s: %s", CHIP_DEVICE_CONFIG_WIFI_STATION_IF_NAME, | ||
err ? err->message : "unknown error"); | ||
|
||
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_NO_INTERFACE_PATH; | ||
|
||
if (mWpaSupplicant.interfacePath) | ||
{ | ||
g_free(mWpaSupplicant.interfacePath); | ||
mWpaSupplicant.interfacePath = NULL; | ||
} | ||
} | ||
|
||
if (err != NULL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need this branch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see comments above |
||
g_error_free(err); | ||
} | ||
|
||
void ConnectivityManagerImpl::_OnWpaInterfaceAdded(WpaFiW1Wpa_supplicant1 * proxy, const gchar * path, GVariant * properties, | ||
gpointer user_data) | ||
{ | ||
if (mWpaSupplicant.interfacePath) | ||
return; | ||
|
||
mWpaSupplicant.interfacePath = const_cast<gchar *>(path); | ||
if (mWpaSupplicant.interfacePath) | ||
{ | ||
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_GOT_INTERFACE_PATH; | ||
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, | ||
mWpaSupplicant.interfacePath, NULL, _OnWpaInterfaceProxyReady, NULL); | ||
} | ||
} | ||
|
||
void ConnectivityManagerImpl::_OnWpaInterfaceRemoved(WpaFiW1Wpa_supplicant1 * proxy, const gchar * path, GVariant * properties, | ||
gpointer user_data) | ||
{ | ||
if (mWpaSupplicant.interfacePath == NULL) | ||
return; | ||
|
||
if (g_strcmp0(mWpaSupplicant.interfacePath, path) == 0) | ||
{ | ||
ChipLogProgress(DeviceLayer, "wpa_supplicant: WiFi interface removed: %s", path); | ||
|
||
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_NO_INTERFACE_PATH; | ||
|
||
if (mWpaSupplicant.interfacePath) | ||
{ | ||
g_free(mWpaSupplicant.interfacePath); | ||
mWpaSupplicant.interfacePath = NULL; | ||
} | ||
|
||
if (mWpaSupplicant.iface) | ||
{ | ||
g_object_unref(mWpaSupplicant.iface); | ||
mWpaSupplicant.iface = NULL; | ||
} | ||
|
||
mWpaSupplicant.scanState = GDBusWpaSupplicant::WIFI_SCANNING_IDLE; | ||
} | ||
} | ||
|
||
void ConnectivityManagerImpl::_OnWpaProxyReady(GObject * source_object, GAsyncResult * res, gpointer user_data) | ||
{ | ||
GError * err = NULL; | ||
|
||
mWpaSupplicant.proxy = wpa_fi_w1_wpa_supplicant1_proxy_new_for_bus_finish(res, &err); | ||
if (mWpaSupplicant.proxy != NULL && err == NULL) | ||
{ | ||
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_CONNECTED; | ||
ChipLogProgress(DeviceLayer, "wpa_supplicant: connected to wpa_supplicant proxy"); | ||
|
||
g_signal_connect(mWpaSupplicant.proxy, "interface-added", G_CALLBACK(_OnWpaInterfaceAdded), NULL); | ||
|
||
g_signal_connect(mWpaSupplicant.proxy, "interface-removed", G_CALLBACK(_OnWpaInterfaceRemoved), NULL); | ||
|
||
wpa_fi_w1_wpa_supplicant1_call_get_interface(mWpaSupplicant.proxy, CHIP_DEVICE_CONFIG_WIFI_STATION_IF_NAME, NULL, | ||
_OnWpaInterfaceReady, NULL); | ||
} | ||
else | ||
{ | ||
ChipLogProgress(DeviceLayer, "wpa_supplicant: failed to create wpa_supplicant proxy %s", | ||
err ? err->message : "unknown error"); | ||
mWpaSupplicant.state = GDBusWpaSupplicant::WPA_NOT_CONNECTED; | ||
} | ||
|
||
if (err != NULL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need this branch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above |
||
g_error_free(err); | ||
} | ||
#endif // CHIP_DEVICE_CONFIG_ENABLE_WPA | ||
|
||
} // namespace DeviceLayer | ||
} // namespace chip |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need to check for NULL here as g_error_free() already does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer always check NULL before calling all g_error_free() even it handles NULL case, this pattern is used throughout gio source code. Suppose there are 0.001 chance of error, then we can prevent 99.99% unnecessary function call.