Skip to content

Commit

Permalink
phase3
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuhammedrafi committed Dec 6, 2024
1 parent 377ee83 commit 969668b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 127 deletions.
16 changes: 12 additions & 4 deletions Tests/raspberrypi/NetworkManagerGdbusTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,18 @@ int main() {
}

case 4: {
std::string ssid, passphrase;
std::cout << "Enter SSID to add : ";
std::cin.ignore();
std::getline(std::cin, ssid);
std::cout << "Enter passphrase: ";
std::getline(std::cin, passphrase);
WPEFramework::Exchange::INetworkManager::WIFISecurityMode securityType = getSecurityType();
Exchange::INetworkManager::WiFiConnectTo ssidinfo = {
.m_ssid = "HomeNet",
.m_passphrase = "rafi@123",
.m_securityMode = WPEFramework::Exchange::INetworkManager::WIFI_SECURITY_WPA_PSK_AES,
.m_persistSSIDInfo = true
.m_ssid = ssid,
.m_passphrase = passphrase,
.m_securityMode = securityType,
.m_persistSSIDInfo = true,
};
if (nmClient->addToKnownSSIDs(ssidinfo)) {
NMLOG_INFO("SSID added to known list successfully");
Expand Down Expand Up @@ -140,6 +147,7 @@ int main() {
std::cin.ignore();
std::getline(std::cin, ssid);
if (nmClient->startWifiScan(ssid)) {
nmEvents->setwifiScanOptions(true);
NMLOG_INFO("WiFi scan started successfully");
} else {
NMLOG_ERROR("Failed to start WiFi scan");
Expand Down
46 changes: 17 additions & 29 deletions gdbus/NetworkManagerGdbusClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ namespace WPEFramework
bool NetworkManagerClient::getKnownSSIDs(std::list<std::string>& ssids)
{
std::list<std::string> paths;
if(!GnomeUtils::getConnectionPaths(m_dbus.getConnection(), paths))
if(!GnomeUtils::getConnectionPaths(m_dbus, paths))
{
NMLOG_ERROR("Connection path fetch failed");
return false;
Expand All @@ -210,11 +210,8 @@ namespace WPEFramework
bool ret = false;
deviceInfo devInfo;

if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), devInfo))
{
NMLOG_ERROR("no wifi device found");
if(!GnomeUtils::getDeviceInfoByIfname(m_dbus, GnomeUtils::getWifiIfname(), devInfo))
return false;
}

if(devInfo.path.empty() || devInfo.state < NM_DEVICE_STATE_DISCONNECTED)
{
Expand Down Expand Up @@ -256,11 +253,8 @@ namespace WPEFramework
GDBusProxy* wProxy = nullptr;
deviceInfo devProperty;

if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), devProperty))
{
NMLOG_ERROR("no wifi device found");
if(!GnomeUtils::getDeviceInfoByIfname(m_dbus, GnomeUtils::getWifiIfname(), devProperty))
return false;
}

if(devProperty.path.empty() || devProperty.state < NM_DEVICE_STATE_DISCONNECTED)
{
Expand Down Expand Up @@ -304,21 +298,18 @@ namespace WPEFramework
{
GError* error = NULL;
GDBusProxy* wProxy = NULL;
deviceInfo properties;
deviceInfo devInfo;

if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), properties))
{
NMLOG_ERROR("no wifi device found");
if(!GnomeUtils::getDeviceInfoByIfname(m_dbus, GnomeUtils::getWifiIfname(), devInfo))
return false;
}

if(properties.path.empty() || properties.state < NM_DEVICE_STATE_DISCONNECTED)
if(devInfo.path.empty() || devInfo.state < NM_DEVICE_STATE_DISCONNECTED)
{
NMLOG_WARNING("access point not active");
return false;
}

wProxy = m_dbus.getNetworkManagerWirelessProxy(properties.path.c_str());
wProxy = m_dbus.getNetworkManagerWirelessProxy(devInfo.path.c_str());
if(wProxy == NULL)
return false;

Expand Down Expand Up @@ -651,13 +642,13 @@ namespace WPEFramework
// TODO check argument length
GVariantBuilder connBuilder;
bool ret = false, reuseConnection = false;
GVariant *result = NULL;
GError* error = NULL;
GVariant *result = NULL;
GError* error = NULL;
const char *newConPath = NULL;
std::string exsistingConn;
deviceInfo deviceProp;

if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), deviceProp))
if(!GnomeUtils::getDeviceInfoByIfname(m_dbus, GnomeUtils::getWifiIfname(), deviceProp))
return false;

if(deviceProp.path.empty() || deviceProp.state == NM_DEVICE_STATE_UNKNOWN)
Expand All @@ -667,7 +658,7 @@ namespace WPEFramework
}

std::list<std::string> paths;
if(GnomeUtils::getwifiConnectionPaths(m_dbus.getConnection(), deviceProp.path.c_str(), paths))
if(GnomeUtils::getwifiConnectionPaths(m_dbus, deviceProp.path.c_str(), paths))
{
for (const std::string& path : paths)
{
Expand Down Expand Up @@ -759,7 +750,7 @@ namespace WPEFramework
}

std::list<std::string> paths;
if(!GnomeUtils::getConnectionPaths(m_dbus.getConnection(), paths))
if(!GnomeUtils::getConnectionPaths(m_dbus, paths))
{
NMLOG_ERROR("remove connection path fetch failed");
return false;
Expand Down Expand Up @@ -821,11 +812,9 @@ namespace WPEFramework
deviceInfo deviceProp;
state = Exchange::INetworkManager::WiFiState::WIFI_STATE_UNINSTALLED;

if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), deviceProp))
{
NMLOG_ERROR("no wifi device found");
if(!GnomeUtils::getDeviceInfoByIfname(m_dbus, GnomeUtils::getWifiIfname(), deviceProp))
return false;
}

// Todo check NMDeviceStateReason for more information
switch(deviceProp.state)
{
Expand Down Expand Up @@ -864,18 +853,17 @@ namespace WPEFramework
deviceInfo deviceProp;
std::string exsistingConn;

if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), deviceProp))
if(!GnomeUtils::getDeviceInfoByIfname(m_dbus, GnomeUtils::getWifiIfname(), deviceProp))
return false;


if(deviceProp.path.empty() || deviceProp.state < NM_DEVICE_STATE_UNAVAILABLE)
{
NMLOG_ERROR("wlan0 interface not active");
return false;
}

std::list<std::string> paths;
if(GnomeUtils::getwifiConnectionPaths(m_dbus.getConnection(), deviceProp.path.c_str(), paths))
if(GnomeUtils::getwifiConnectionPaths(m_dbus, deviceProp.path.c_str(), paths))
{
for (const std::string& path : paths) {
std::string ssid;
Expand Down Expand Up @@ -925,7 +913,7 @@ namespace WPEFramework
GDBusProxy* wProxy = NULL;
deviceInfo devInfo;

if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), devInfo) || devInfo.path.empty())
if(!GnomeUtils::getDeviceInfoByIfname(m_dbus, GnomeUtils::getWifiIfname(), devInfo) || devInfo.path.empty())
return false;

if(devInfo.state <= NM_DEVICE_STATE_DISCONNECTED)
Expand Down
102 changes: 14 additions & 88 deletions gdbus/NetworkManagerGdbusUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,34 +236,18 @@ namespace WPEFramework
return true;
}

bool GnomeUtils::getDevicePropertiesByPath(GDBusConnection *dbusConn, const char* devicePath, deviceInfo& properties)
bool GnomeUtils::getDevicePropertiesByPath(DbusMgr& m_dbus, const char* devicePath, deviceInfo& properties)
{
GError *error = NULL;
GVariant *devicesVar = NULL;
GDBusProxy* nmProxy = NULL;
u_int32_t value;

nmProxy = g_dbus_proxy_new_sync(dbusConn,
G_DBUS_PROXY_FLAGS_NONE,
NULL, // GDBusInterfaceInfo
"org.freedesktop.NetworkManager", // Bus name
devicePath, // Object path
"org.freedesktop.NetworkManager.Device", // Interface name
NULL, // GCancellable
&error);


if (error) {
NMLOG_ERROR("Error calling NetworkManager.Device: %s", error->message);
g_error_free(error);
nmProxy = m_dbus.getNetworkManagerDeviceProxy(devicePath);
if(nmProxy == NULL)
return false;
}


if(GnomeUtils::getCachedPropertyU(nmProxy, "DeviceType", &value))
{
properties.deviceType = static_cast<NMDeviceType>(value);
}
else
NMLOG_ERROR("'DeviceType' property failed");

Expand Down Expand Up @@ -293,20 +277,15 @@ namespace WPEFramework
return true;
}

bool GnomeUtils::getDeviceInfoByIfname(GDBusConnection *dbusConn, const char* ifaceName, deviceInfo& properties)
bool GnomeUtils::getDeviceInfoByIfname(DbusMgr& m_dbus, const char* ifaceName, deviceInfo& properties)
{
GError *error = NULL;
GVariant *devicesVar = NULL;
bool ret = false;

GDBusProxy* nmProxy = g_dbus_proxy_new_sync(dbusConn,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager",
"org.freedesktop.NetworkManager",
NULL,
&error);
GDBusProxy* nmProxy = m_dbus.getNetworkManagerProxy();
if(nmProxy == NULL)
return false;

devicesVar = g_dbus_proxy_call_sync(nmProxy, "GetDevices", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
if (error) {
Expand All @@ -323,7 +302,7 @@ namespace WPEFramework
{
if(devicePath != NULL )
{
if(getDevicePropertiesByPath(dbusConn, devicePath, properties) && properties.interface == ifaceName)
if(getDevicePropertiesByPath(m_dbus, devicePath, properties) && properties.interface == ifaceName)
{
properties.path = devicePath;
ret = true;
Expand Down Expand Up @@ -504,26 +483,14 @@ namespace WPEFramework
return result;
}

bool GnomeUtils::getwifiConnectionPaths(GDBusConnection *dbusConn, const char* devicePath, std::list<std::string>& paths)
bool GnomeUtils::getwifiConnectionPaths(DbusMgr& m_dbus, const char* devicePath, std::list<std::string>& paths)
{
GError *error = NULL;
GDBusProxy* nmProxy = NULL;
GVariant* result = NULL;
bool ret = false;
nmProxy = g_dbus_proxy_new_sync(dbusConn,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.freedesktop.NetworkManager",
devicePath,
"org.freedesktop.NetworkManager.Device",
NULL,
&error);

if (error) {
NMLOG_ERROR("Error calling DBus.Properties method: %s", error->message);
g_error_free(error);
return ret;
}
nmProxy = m_dbus.getNetworkManagerDeviceProxy(devicePath);
if(nmProxy == NULL)
return false;

result = g_dbus_proxy_get_cached_property(nmProxy, "AvailableConnections");
if (!result) {
Expand All @@ -550,30 +517,16 @@ namespace WPEFramework
return ret;
}

bool GnomeUtils::getConnectionPaths(GDBusConnection *dbusConn, std::list<std::string>& pathsList)
bool GnomeUtils::getConnectionPaths(DbusMgr& m_dbus, std::list<std::string>& pathsList)
{
GDBusProxy *sProxy= NULL;
GError *error= NULL;
GVariant *listProxy = NULL;
char **paths = NULL;

sProxy = g_dbus_proxy_new_sync(dbusConn,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager/Settings",
"org.freedesktop.NetworkManager.Settings",
NULL, // GCancellable
&error);

sProxy = m_dbus.getNetworkManagerSettingsProxy();
if(sProxy == NULL)
{
if (error != NULL) {
NMLOG_ERROR("Failed to create proxy: %s", error->message);
g_error_free(error);
}
return false;
}

listProxy = g_dbus_proxy_call_sync(sProxy,
"ListConnections",
Expand Down Expand Up @@ -617,33 +570,6 @@ namespace WPEFramework
return true;
}

bool GnomeUtils::getDeviceState(GDBusConnection *dbusConn, const gchar *iface_name, NMDeviceState& state)
{
deviceInfo devProp;
if(!GnomeUtils::getDeviceInfoByIfname(dbusConn, iface_name, devProp))
{
NMLOG_ERROR("Interface unknow to NetworkManger Deamon");
return false;
}

state = devProp.state;
return true;
}

bool GnomeUtils::getDeviceStateReason(GDBusConnection *dbusConn, const gchar *iface_name, NMDeviceStateReason& StateReason)
{
deviceInfo devProp;
if(!GnomeUtils::getDeviceInfoByIfname(dbusConn, iface_name, devProp))
{
NMLOG_ERROR("Interface unknow to NetworkManger Deamon");
return false;
}

StateReason = devProp.stateReason;
NMLOG_WARNING("Device state: %u (reason: %u)", devProp.state, devProp.stateReason);
return true;
}

} // Plugin
} // WPEFramework

11 changes: 5 additions & 6 deletions gdbus/NetworkManagerGdbusUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// #include <NetworkManager.h>
#include <libnm/nm-dbus-interface.h>
#include "INetworkManager.h"
#include "NetworkManagerGdbusMgr.h"

using namespace std;

Expand Down Expand Up @@ -61,13 +62,11 @@ namespace WPEFramework
static void printKeyVariant(const char *setting_name, GVariant *setting);
static bool getDeviceByIpIface(GDBusConnection *dbusConn, const gchar *iface_name, std::string& path);
static bool getApDetails(GDBusConnection *connection, const char* apPath, Exchange::INetworkManager::WiFiSSIDInfo& wifiInfo);
static bool getConnectionPaths(GDBusConnection *dbusConn, std::list<std::string>& pathsList);
static bool getwifiConnectionPaths(GDBusConnection *dbusConn,const char* devicePath, std::list<std::string>& paths);
static bool getDeviceState(GDBusConnection *dbusConn, const gchar *iface_name, NMDeviceState& state);
static bool getDeviceStateReason(GDBusConnection *dbusConn, const gchar *iface_name, NMDeviceStateReason& StateReason);
static bool getConnectionPaths(DbusMgr& m_dbus, std::list<std::string>& pathsList);
static bool getwifiConnectionPaths(DbusMgr& m_dbus, const char* devicePath, std::list<std::string>& paths);
static bool getDevicePropertiesByPath(DbusMgr& m_dbus, const char* devPath, deviceInfo& properties);
static bool getDeviceInfoByIfname(DbusMgr& m_dbus, const char* ifname, deviceInfo& properties);
static bool getCachedPropertyU(GDBusProxy* proxy, const char* propertiy, uint32_t *value);
static bool getDevicePropertiesByPath(GDBusConnection *dbusConn, const char* devPath, deviceInfo& properties);
static bool getDeviceInfoByIfname(GDBusConnection *dbusConn, const char* ifname, deviceInfo& properties);
static bool getIpv4AddrFromIP4ConfigProxy( GDBusProxy *ipProxy, std::string& ipAddr, uint32_t& prifix);
static bool getIpv6AddrFromIP6ConfigProxy( GDBusProxy *ipProxy, std::string& ipAddr, uint32_t& prifix);

Expand Down

0 comments on commit 969668b

Please sign in to comment.