From 377ee83d498c4c37db2deb479867512824e7ba66 Mon Sep 17 00:00:00 2001 From: rafi Date: Wed, 6 Nov 2024 17:19:44 +0530 Subject: [PATCH] phase 2 --- gdbus/NetworkManagerGdbusClient.cpp | 130 ++++++++-------------------- gdbus/NetworkManagerGdbusEvent.cpp | 2 +- gdbus/NetworkManagerGdbusUtils.cpp | 6 +- gdbus/NetworkManagerGdbusUtils.h | 2 +- 4 files changed, 41 insertions(+), 99 deletions(-) diff --git a/gdbus/NetworkManagerGdbusClient.cpp b/gdbus/NetworkManagerGdbusClient.cpp index 6e89e243..62813444 100644 --- a/gdbus/NetworkManagerGdbusClient.cpp +++ b/gdbus/NetworkManagerGdbusClient.cpp @@ -41,28 +41,17 @@ namespace WPEFramework NMLOG_INFO("~NetworkManagerClient"); } - static bool getSSIDFromConnection(GDBusConnection *dbusConn, const std::string connPath, std::string& ssid) + static bool getSSIDFromConnection(DbusMgr &m_dbus, const std::string connPath, std::string& ssid) { GError *error = NULL; GDBusProxy *ConnProxy = NULL; GVariant *settingsProxy= NULL, *connection= NULL, *gVarConn= NULL; bool ret = false; - ConnProxy = g_dbus_proxy_new_sync(dbusConn, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.NetworkManager", - connPath.c_str(), - "org.freedesktop.NetworkManager.Settings.Connection", - NULL, - &error); - - if (error != NULL) { - NMLOG_ERROR("Failed to create proxy: %s", error->message); - g_error_free(error); + ConnProxy = m_dbus.getNetworkManagerSettingsConnectionProxy(connPath.c_str()); + if(ConnProxy == NULL) return false; - } - + settingsProxy = g_dbus_proxy_call_sync(ConnProxy, "GetSettings", NULL, @@ -158,26 +147,15 @@ namespace WPEFramework return ret; } - static bool deleteConnection(GDBusConnection *dbusConn, const std::string& path, std::string& ssid) + static bool deleteConnection(DbusMgr m_dbus, const std::string& path, std::string& ssid) { GError *error = NULL; GDBusProxy *ConnProxy = NULL; GVariant *deleteVar= NULL; - ConnProxy = g_dbus_proxy_new_sync(dbusConn, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.NetworkManager", - path.c_str(), - "org.freedesktop.NetworkManager.Settings.Connection", - NULL, - &error); - - if (error != NULL) { - NMLOG_ERROR("Failed to create proxy: %s", error->message); - g_error_free(error); + ConnProxy = m_dbus.getNetworkManagerSettingsConnectionProxy(path.c_str()); + if(ConnProxy == NULL) return false; - } deleteVar = g_dbus_proxy_call_sync(ConnProxy, "Delete", @@ -194,9 +172,7 @@ namespace WPEFramework return false; } else - { NMLOG_INFO("connection '%s' Removed path: %s", ssid.c_str(), path.c_str()); - } if (deleteVar) g_variant_unref(deleteVar); @@ -217,7 +193,7 @@ namespace WPEFramework for (const std::string& path : paths) { // NMLOG_DEBUG("connection path %s", path.c_str()); std::string ssid; - if(getSSIDFromConnection(m_dbus.getConnection(), path, ssid) && !ssid.empty()) + if(getSSIDFromConnection(m_dbus, path, ssid) && !ssid.empty()) ssids.push_back(ssid); } @@ -234,7 +210,7 @@ namespace WPEFramework bool ret = false; deviceInfo devInfo; - if(!GnomeUtils::getDevicePropertiesByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), devInfo)) + if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), devInfo)) { NMLOG_ERROR("no wifi device found"); return false; @@ -280,7 +256,7 @@ namespace WPEFramework GDBusProxy* wProxy = nullptr; deviceInfo devProperty; - if(!GnomeUtils::getDevicePropertiesByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), devProperty)) + if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), devProperty)) { NMLOG_ERROR("no wifi device found"); return false; @@ -330,7 +306,7 @@ namespace WPEFramework GDBusProxy* wProxy = NULL; deviceInfo properties; - if(!GnomeUtils::getDevicePropertiesByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), properties)) + if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), properties)) { NMLOG_ERROR("no wifi device found"); return false; @@ -411,26 +387,15 @@ namespace WPEFramework return buf; } - bool updateConnctionAndactivate(GDBusConnection *dbusConn, GVariantBuilder& connBuilder, const char* devicePath, const char* connPath) + bool updateConnctionAndactivate(DbusMgr& m_dbus, GVariantBuilder& connBuilder, const char* devicePath, const char* connPath) { GDBusProxy* proxy = nullptr; GError* error = nullptr; GVariant* result = nullptr; - const char* service = "org.freedesktop.NetworkManager"; - const char* objectPath = connPath; - const char* interface = "org.freedesktop.NetworkManager.Settings.Connection"; - - proxy = g_dbus_proxy_new_sync(dbusConn, - G_DBUS_PROXY_FLAGS_NONE, NULL, - service, objectPath, interface, - NULL, &error); - - if (error) { - NMLOG_ERROR( "Failed to create proxy: %s", error->message); - g_clear_error(&error); + proxy = m_dbus.getNetworkManagerSettingsConnectionProxy(connPath); + if(proxy == NULL) return false; - } result = g_dbus_proxy_call_sync (proxy, "Update", g_variant_new("(a{sa{sv}})", connBuilder), @@ -447,10 +412,9 @@ namespace WPEFramework g_variant_unref(result); g_object_unref(proxy); - proxy = g_dbus_proxy_new_sync(dbusConn, - G_DBUS_PROXY_FLAGS_NONE, NULL, - service, "/org/freedesktop/NetworkManager", "org.freedesktop.NetworkManager", - NULL, &error); + proxy = m_dbus.getNetworkManagerProxy(); + if(proxy == NULL) + return false; if (error) { NMLOG_ERROR( "Failed to create proxy: %s", error->message); @@ -480,31 +444,21 @@ namespace WPEFramework if(result) g_variant_unref(result); - g_object_unref(proxy); + if(proxy) + g_object_unref(proxy); return true; } - bool addNewConnctionAndactivate(GDBusConnection *dbusConn, GVariantBuilder connBuilder, const char* devicePath, bool persist) + bool addNewConnctionAndactivate(DbusMgr& m_dbus, GVariantBuilder connBuilder, const char* devicePath, bool persist) { GDBusProxy* proxy = nullptr; GError* error = nullptr; GVariant* result = nullptr; - const char* service = "org.freedesktop.NetworkManager"; - const char* objectPath = "/org/freedesktop/NetworkManager"; - const char* interface = "org.freedesktop.NetworkManager"; - - proxy = g_dbus_proxy_new_sync(dbusConn, - G_DBUS_PROXY_FLAGS_NONE, NULL, - service, objectPath, interface, - NULL, &error); - - if (error) { - NMLOG_ERROR( "Failed to create proxy: %s", error->message); - g_clear_error(&error); + proxy = m_dbus.getNetworkManagerProxy(); + if(proxy == NULL) return false; - } const char* specific_object = "/"; @@ -703,11 +657,8 @@ namespace WPEFramework std::string exsistingConn; deviceInfo deviceProp; - if(!GnomeUtils::getDevicePropertiesByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), deviceProp)) - { - NMLOG_ERROR("no wifi device found"); + if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), deviceProp)) return false; - } if(deviceProp.path.empty() || deviceProp.state == NM_DEVICE_STATE_UNKNOWN) { @@ -721,7 +672,7 @@ namespace WPEFramework for (const std::string& path : paths) { std::string ssid; - if(getSSIDFromConnection(m_dbus.getConnection(), path, ssid) && ssid == ssidinfo.m_ssid) + if(getSSIDFromConnection(m_dbus, path, ssid) && ssid == ssidinfo.m_ssid) { exsistingConn = path; NMLOG_DEBUG("same connection exsist (%s) updating ...", exsistingConn.c_str()); @@ -763,14 +714,7 @@ namespace WPEFramework else { GDBusProxy *proxy = NULL; - proxy = g_dbus_proxy_new_sync(m_dbus.getConnection(), - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.NetworkManager", - "/org/freedesktop/NetworkManager/Settings", - "org.freedesktop.NetworkManager.Settings", - NULL, - &error); + proxy = m_dbus.getNetworkManagerSettingsProxy(); if (proxy != nullptr) { result = g_dbus_proxy_call_sync (proxy, @@ -824,8 +768,8 @@ namespace WPEFramework for (const std::string& path : paths) { // NMLOG_DEBUG("remove connection path %s", path.c_str()); std::string connSsid; - if(getSSIDFromConnection(m_dbus.getConnection(), path, connSsid) && connSsid == ssid) { - ret = deleteConnection(m_dbus.getConnection(), path, connSsid); + if(getSSIDFromConnection(m_dbus, path, connSsid) && connSsid == ssid) { + ret = deleteConnection(m_dbus, path, connSsid); } } @@ -877,7 +821,7 @@ namespace WPEFramework deviceInfo deviceProp; state = Exchange::INetworkManager::WiFiState::WIFI_STATE_UNINSTALLED; - if(!GnomeUtils::getDevicePropertiesByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), deviceProp)) + if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), deviceProp)) { NMLOG_ERROR("no wifi device found"); return false; @@ -920,15 +864,13 @@ namespace WPEFramework deviceInfo deviceProp; std::string exsistingConn; - if(!GnomeUtils::getDevicePropertiesByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), deviceProp)) - { - NMLOG_ERROR("no wifi device found"); + if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), deviceProp)) return false; - } - if(deviceProp.path.empty() || deviceProp.state == NM_DEVICE_STATE_UNKNOWN) + + if(deviceProp.path.empty() || deviceProp.state < NM_DEVICE_STATE_UNAVAILABLE) { - NMLOG_WARNING("wlan0 interface not active"); + NMLOG_ERROR("wlan0 interface not active"); return false; } @@ -937,7 +879,7 @@ namespace WPEFramework { for (const std::string& path : paths) { std::string ssid; - if(getSSIDFromConnection(m_dbus.getConnection(), path, ssid) && ssid == ssidinfo.m_ssid) + if(getSSIDFromConnection(m_dbus, path, ssid) && ssid == ssidinfo.m_ssid) { exsistingConn = path; NMLOG_WARNING("same connection exsist (%s)", exsistingConn.c_str()); @@ -956,7 +898,7 @@ namespace WPEFramework return false; } - if(updateConnctionAndactivate(m_dbus.getConnection(), connBuilder, deviceProp.path.c_str(), exsistingConn.c_str())) + if(updateConnctionAndactivate(m_dbus, connBuilder, deviceProp.path.c_str(), exsistingConn.c_str())) NMLOG_INFO("updated connection request success"); else NMLOG_ERROR("wifi connect request failed"); @@ -968,7 +910,7 @@ namespace WPEFramework NMLOG_WARNING("connection builder failed"); return false; } - if(addNewConnctionAndactivate(m_dbus.getConnection(), connBuilder, deviceProp.path.c_str(), ssidinfo.m_persistSSIDInfo)) + if(addNewConnctionAndactivate(m_dbus, connBuilder, deviceProp.path.c_str(), ssidinfo.m_persistSSIDInfo)) NMLOG_INFO("wifi connect request success"); else NMLOG_ERROR("wifi connect request failed"); @@ -983,7 +925,7 @@ namespace WPEFramework GDBusProxy* wProxy = NULL; deviceInfo devInfo; - if(!GnomeUtils::getDevicePropertiesByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), devInfo) || devInfo.path.empty()) + if(!GnomeUtils::getDeviceInfoByIfname(m_dbus.getConnection(), GnomeUtils::getWifiIfname(), devInfo) || devInfo.path.empty()) return false; if(devInfo.state <= NM_DEVICE_STATE_DISCONNECTED) diff --git a/gdbus/NetworkManagerGdbusEvent.cpp b/gdbus/NetworkManagerGdbusEvent.cpp index 6c45754e..ca8f36d7 100644 --- a/gdbus/NetworkManagerGdbusEvent.cpp +++ b/gdbus/NetworkManagerGdbusEvent.cpp @@ -692,7 +692,7 @@ namespace WPEFramework // if(_instance != nullptr) //instance->ReportIPAddressChangedEvent(iface, acqired, true, ipAddress); - NMLOG_INFO("iface:%s - ipaddress:%s - %s - isIPv6:%s", iface.c_str(), ipAddress.c_str(), acqired?"acquired":"lost", isIPv6?"true":"false"); + NMLOG_INFO("iface:%s - ipaddress:%s - %s - %s", iface.c_str(), ipAddress.c_str(), acqired?"acquired":"lost", isIPv6?"isIPv6":"isIPv4"); } void NetworkManagerEvents::onAvailableSSIDsCb(const char* wifiDevicePath) diff --git a/gdbus/NetworkManagerGdbusUtils.cpp b/gdbus/NetworkManagerGdbusUtils.cpp index 3121d78e..b04b9282 100644 --- a/gdbus/NetworkManagerGdbusUtils.cpp +++ b/gdbus/NetworkManagerGdbusUtils.cpp @@ -293,7 +293,7 @@ namespace WPEFramework return true; } - bool GnomeUtils::getDevicePropertiesByIfname(GDBusConnection *dbusConn, const char* ifaceName, deviceInfo& properties) + bool GnomeUtils::getDeviceInfoByIfname(GDBusConnection *dbusConn, const char* ifaceName, deviceInfo& properties) { GError *error = NULL; GVariant *devicesVar = NULL; @@ -620,7 +620,7 @@ namespace WPEFramework bool GnomeUtils::getDeviceState(GDBusConnection *dbusConn, const gchar *iface_name, NMDeviceState& state) { deviceInfo devProp; - if(!GnomeUtils::getDevicePropertiesByIfname(dbusConn, iface_name, devProp)) + if(!GnomeUtils::getDeviceInfoByIfname(dbusConn, iface_name, devProp)) { NMLOG_ERROR("Interface unknow to NetworkManger Deamon"); return false; @@ -633,7 +633,7 @@ namespace WPEFramework bool GnomeUtils::getDeviceStateReason(GDBusConnection *dbusConn, const gchar *iface_name, NMDeviceStateReason& StateReason) { deviceInfo devProp; - if(!GnomeUtils::getDevicePropertiesByIfname(dbusConn, iface_name, devProp)) + if(!GnomeUtils::getDeviceInfoByIfname(dbusConn, iface_name, devProp)) { NMLOG_ERROR("Interface unknow to NetworkManger Deamon"); return false; diff --git a/gdbus/NetworkManagerGdbusUtils.h b/gdbus/NetworkManagerGdbusUtils.h index 78e2b0e5..e50db756 100644 --- a/gdbus/NetworkManagerGdbusUtils.h +++ b/gdbus/NetworkManagerGdbusUtils.h @@ -67,7 +67,7 @@ namespace WPEFramework static bool getDeviceStateReason(GDBusConnection *dbusConn, const gchar *iface_name, NMDeviceStateReason& StateReason); static bool getCachedPropertyU(GDBusProxy* proxy, const char* propertiy, uint32_t *value); static bool getDevicePropertiesByPath(GDBusConnection *dbusConn, const char* devPath, deviceInfo& properties); - static bool getDevicePropertiesByIfname(GDBusConnection *dbusConn, const char* ifname, 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);