From 1fc81dfd9893ae287ae235dfe92bd532613c04cd Mon Sep 17 00:00:00 2001 From: rafi Date: Wed, 16 Oct 2024 13:22:45 +0530 Subject: [PATCH] other api include --- GnomeProxy/NetworkManagerGnomeClient.cpp | 1975 ++++++++++++---------- GnomeProxy/NetworkManagerGnomeClient.h | 127 +- GnomeProxy/NetworkManagerGnomeUtils.cpp | 905 +++++----- Tests/raspberrypi/rpiUnitTest.cpp | 15 +- 4 files changed, 1586 insertions(+), 1436 deletions(-) diff --git a/GnomeProxy/NetworkManagerGnomeClient.cpp b/GnomeProxy/NetworkManagerGnomeClient.cpp index cd95f2c9..222a98cb 100644 --- a/GnomeProxy/NetworkManagerGnomeClient.cpp +++ b/GnomeProxy/NetworkManagerGnomeClient.cpp @@ -1,917 +1,1058 @@ -/** -* If not stated otherwise in this file or this component's LICENSE -* file the following copyright and licenses apply: -* -* Copyright 2022 RDK Management -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -**/ - -#include -#include -#include -#include -#include -#include -#include -#include "NetworkManagerLogger.h" -#include "NetworkManagerGnomeClient.h" -#include "NetworkManagerGnomeUtils.h" - -namespace WPEFramework -{ - namespace Plugin - { - - NetworkManagerClient::NetworkManagerClient() { - NMLOG_INFO("NetworkManagerClient created"); - if (!dbusConnection.InitializeBusConnection("org.freedesktop.NetworkManager")) { - NMLOG_ERROR("Failed to initialize D-Bus connection for NetworkManager"); - } - } - - NetworkManagerClient::~NetworkManagerClient() { - NMLOG_INFO("NetworkManagerClient destroyed"); - } - - static bool getSSIDFromConnection(GDBusConnection *dbusConn, 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); - return false; - } - - settingsProxy = g_dbus_proxy_call_sync(ConnProxy, - "GetSettings", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - if (!settingsProxy) { - g_dbus_error_strip_remote_error(error); - NMLOG_ERROR("Failed to get connection settings: %s", error->message); - g_error_free(error); - g_object_unref(ConnProxy); - return false; - } - - g_variant_get(settingsProxy, "(@a{sa{sv}})", &connection); - gVarConn = g_variant_lookup_value(connection, "connection", NULL); - - if(gVarConn == NULL) { - NMLOG_ERROR("connection Gvarient Error"); - g_variant_unref(settingsProxy); - g_object_unref(ConnProxy); - return false; - } - - if(gVarConn) - { - const char *connTyp= NULL; - G_VARIANT_LOOKUP(gVarConn, "type", "&s", &connTyp); - if(strcmp(connTyp, "802-11-wireless") != 0) - { - NMLOG_ERROR("connection is not 802-11-wireless type"); - ret = false; - } - else - { - // 802-11-wireless.ssid: - GVariant *setting = NULL; - setting = g_variant_lookup_value(connection, "802-11-wireless", NULL); - if(setting) - { - GVariantIter iter; - GVariant *value = NULL; - const char *propertyName; - g_variant_iter_init(&iter, setting); - while (g_variant_iter_next(&iter, "{&sv}", &propertyName, &value)) { - if (strcmp(propertyName, "ssid") == 0) { - // Decode SSID from GVariant of type "ay" - gsize ssid_length = 0; - const guchar *ssid_data = static_cast(g_variant_get_fixed_array(value, &ssid_length, sizeof(guchar))); - if (ssid_data && ssid_length > 0 && ssid_length <= 32) { - ssid.assign(reinterpret_cast(ssid_data), ssid_length); - NMLOG_DEBUG("SSID: %s", ssid.c_str()); - } else { - NMLOG_ERROR("Invalid SSID length: %zu (maximum is 32)", ssid_length); - ssid.empty(); - } - } - } - if(value) - g_variant_unref(value); - g_variant_unref(setting); - setting = NULL; - } - - if(!ssid.empty()) - { - // 802-11-wireless-security.key-mgmt: wpa-psk - const char* keyMgmt = NULL; - setting = g_variant_lookup_value(connection, "802-11-wireless-security", NULL); - G_VARIANT_LOOKUP(setting, "key-mgmt", "&s", &keyMgmt); - NMLOG_DEBUG("802-11-wireless-security.key-mgmt: %s", keyMgmt); - g_variant_unref(setting); - setting = NULL; - ret = true; - } - } - } - - if (gVarConn) - g_variant_unref(gVarConn); - if (connection) - g_variant_unref(connection); - if (settingsProxy) - g_variant_unref(settingsProxy); - g_object_unref(ConnProxy); - - return ret; - } - - static bool deleteConnection(GDBusConnection *dbusConn, 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); - return false; - } - - deleteVar = g_dbus_proxy_call_sync(ConnProxy, - "Delete", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - if (!deleteVar) { - g_dbus_error_strip_remote_error(error); - NMLOG_ERROR("Failed to get connection settings: %s", error->message); - g_error_free(error); - g_object_unref(ConnProxy); - return false; - } - else - { - NMLOG_INFO("connection '%s' Removed path: %s", ssid.c_str(), path.c_str()); - } - - if (deleteVar) - g_variant_unref(deleteVar); - g_object_unref(ConnProxy); - - return true; - } - - bool NetworkManagerClient::getKnownSSIDs(std::list& ssids) - { - std::list paths; - if(!GnomeUtils::getConnectionPaths(dbusConnection.getConnection(), paths)) - { - NMLOG_ERROR("Connection path fetch failed"); - return false; - } - - for (const std::string& path : paths) { - // NMLOG_DEBUG("connection path %s", path.c_str()); - std::string ssid; - if(getSSIDFromConnection(dbusConnection.getConnection(), path, ssid) && !ssid.empty()) - ssids.push_back(ssid); - } - - if(ssids.empty()) - return false; - return true; - } - - bool NetworkManagerClient::getConnectedSSID(const Exchange::INetworkManager::WiFiSSIDInfo &ssidinfo) - { - GError* error = NULL; - GDBusProxy* wProxy = NULL; - GVariant* result = NULL; - gchar *activeApPath = NULL; - bool ret = false; - deviceProperties properties; - - if(!GnomeUtils::getDevicePropertiesByIfname(dbusConnection.getConnection(), GnomeUtils::getWifiIfname(), properties)) - { - NMLOG_ERROR("no wifi device found"); - return false; - } - - if(properties.path.empty() || properties.state <= NM_DEVICE_STATE_DISCONNECTED) - { - NMLOG_WARNING("access point not active"); - return false; - } - - wProxy = g_dbus_proxy_new_sync(dbusConnection.getConnection(), - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.NetworkManager", - properties.path.c_str(), - "org.freedesktop.NetworkManager.Device.Wireless", - NULL, // GCancellable - &error); - if(wProxy == NULL) - { - if (error) { - NMLOG_ERROR("Error creating proxy: %s", error->message); - g_error_free(error); - } - return false; - } - - result = g_dbus_proxy_get_cached_property(wProxy, "ActiveAccessPoint"); - if (!result) { - NMLOG_ERROR("Failed to get ActiveAccessPoint property."); - g_object_unref(wProxy); - return false; - } - - g_variant_get(result, "o", &activeApPath); - if(g_strdup(activeApPath) != NULL && g_strcmp0(activeApPath, "/") != 0) - { - NMLOG_DEBUG("ActiveAccessPoint property path %s", activeApPath); - apProperties apDetails; - if(GnomeUtils::getApDetails(dbusConnection.getConnection(), g_strdup(activeApPath), apDetails)) - { - NMLOG_INFO("getApDetails success"); - ret = true; - } - } - else - NMLOG_ERROR("active access point not found"); - - g_variant_unref(result); - g_object_unref(wProxy); - return ret; - } - - bool NetworkManagerClient::getAvailableSSIDs(std::list& ssids) - { - GError* error = nullptr; - GDBusProxy* wProxy = nullptr; - deviceProperties properties; - - if(!GnomeUtils::getDevicePropertiesByIfname(dbusConnection.getConnection(), GnomeUtils::getWifiIfname(), properties)) - { - NMLOG_ERROR("no wifi device found"); - return false; - } - - if(properties.path.empty() || properties.state < NM_DEVICE_STATE_DISCONNECTED) - { - NMLOG_WARNING("access point not active"); - return false; - } - - wProxy = g_dbus_proxy_new_sync(dbusConnection.getConnection(), - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.NetworkManager", - properties.path.c_str(), - "org.freedesktop.NetworkManager.Device.Wireless", - NULL, - &error); - - if (error) { - NMLOG_ERROR("Error creating proxy: %s", error->message); - g_error_free(error); - return false; - } - - GVariant* result = g_dbus_proxy_call_sync(wProxy, "GetAccessPoints", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); - // TODO change to GetAllAccessPoints to include hidden ssid - if (error) { - NMLOG_ERROR("Error creating proxy: %s", error->message); - g_error_free(error); - g_object_unref(wProxy); - return false; - } - - GVariantIter* iter; - const gchar* apPath; - g_variant_get(result, "(ao)", &iter); - - while (g_variant_iter_loop(iter, "o", &apPath)) { - apProperties apDetails; - NMLOG_DEBUG("Access Point Path: %s", apPath); - if(!GnomeUtils::getApDetails(dbusConnection.getConnection(), apPath, apDetails)) - { - NMLOG_WARNING("getApDetails failed"); - } - } - - g_variant_iter_free(iter); - g_variant_unref(result); - g_object_unref(wProxy); - - return true; - } - - bool NetworkManagerClient::startWifiScanning(const std::string ssid) - { - GError* error = NULL; - GDBusProxy* wProxy = NULL; - deviceProperties properties; - - if(!GnomeUtils::getDevicePropertiesByIfname(dbusConnection.getConnection(), GnomeUtils::getWifiIfname(), properties)) - { - NMLOG_ERROR("no wifi device found"); - return false; - } - - if(properties.path.empty() || properties.state < NM_DEVICE_STATE_DISCONNECTED) - { - NMLOG_WARNING("access point not active"); - return false; - } - - wProxy = g_dbus_proxy_new_sync(dbusConnection.getConnection(), - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.NetworkManager", - properties.path.c_str(), - "org.freedesktop.NetworkManager.Device.Wireless", - NULL, - &error); - - if (error) { - NMLOG_ERROR("Error creating proxy: %s", error->message); - g_error_free(error); - return false; - } - - GVariant* timestampVariant = NULL; - timestampVariant = g_dbus_proxy_get_cached_property(wProxy, "LastScan"); - if (!timestampVariant) { - NMLOG_ERROR("Failed to get LastScan properties"); - g_object_unref(wProxy); - return false; - } - - if (g_variant_is_of_type (timestampVariant, G_VARIANT_TYPE_INT64)) { - gint64 timestamp; - timestamp = g_variant_get_int64 (timestampVariant); - NMLOG_DEBUG("Last scan timestamp: %lld", static_cast(timestamp)); - } else { - g_warning("Unexpected variant type: %s", g_variant_get_type_string (timestampVariant)); - } - - GVariant *options = NULL; - if (!ssid.empty()) - { - // TODO fix specific ssid scanning - NMLOG_INFO("staring wifi scanning .. %s", ssid.c_str()); - GVariantBuilder settingsBuilder; - g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); - GVariant *ssid_array = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, (const guint8 *)ssid.c_str(), ssid.length(), 1); - g_variant_builder_add (&settingsBuilder, "{sv}", "ssids", ssid_array); - - // GVariantBuilder builder, array_builder; - // g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT); - // g_variant_builder_init(&array_builder, G_VARIANT_TYPE("aay")); - // g_variant_builder_add(&array_builder, "@ay", - // g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, (const guint8 *) ssid.c_str(), ssid.length(), 1) - // ); - // g_variant_builder_add(&builder, "{sv}", "ssids", g_variant_builder_end(&array_builder)); - // g_variant_builder_add(&builder, "{sv}", "hidden", g_variant_new_boolean(TRUE)); - - options = g_variant_builder_end(&settingsBuilder); - g_dbus_proxy_call_sync(wProxy, "RequestScan", options, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); - } - else - g_dbus_proxy_call_sync(wProxy, "RequestScan", g_variant_new("(a{sv})", options), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); - if (error) { - NMLOG_ERROR("Error RequestScan: %s", error->message); - g_error_free(error); - g_object_unref(wProxy); - return false; - } - - if(options) - g_variant_unref(options); - g_object_unref(wProxy); - - return true; - } - - static char * nm_utils_uuid_generate (void) - { - uuid_t uuid; - char *buf; - - buf = static_cast(g_malloc0(37)); - uuid_generate_random (uuid); - uuid_unparse_lower (uuid, &buf[0]); - return buf; - } - - bool updateConnctionAndactivate(GDBusConnection *dbusConn, 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); - return false; - } - - result = g_dbus_proxy_call_sync (proxy, "Update", - g_variant_new("(a{sa{sv}})", connBuilder), - G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error); - - if (error) { - NMLOG_ERROR("Failed to call Update : %s", error->message); - g_clear_error(&error); - g_object_unref(proxy); - return false; - } - - if(result) - 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); - - if (error) { - NMLOG_ERROR( "Failed to create proxy: %s", error->message); - g_clear_error(&error); - return false; - } - const char* specificObject = "/"; - - result = g_dbus_proxy_call_sync (proxy, "ActivateConnection", - g_variant_new("(ooo)", connPath, devicePath, specificObject), - G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error); - - if (error) { - NMLOG_ERROR("Failed to call ActivateConnection: %s", error->message); - g_clear_error(&error); - g_object_unref(proxy); - return false; - } - - if(result) { - GVariant* activeConnVariant; - g_variant_get(result, "(@o)", &activeConnVariant); - const char* activeConnPath = g_variant_get_string(activeConnVariant, nullptr); - NMLOG_DEBUG("connPath %s; activeconn %s", connPath, activeConnPath); - g_variant_unref(activeConnVariant); - } - - if(result) - g_variant_unref(result); - g_object_unref(proxy); - - return true; - } - - bool addNewConnctionAndactivate(GDBusConnection *dbusConn, GVariantBuilder connBuilder, const char* devicePath, bool persist) - { - GDBusProxy* proxy = nullptr; - GError* error = nullptr; - GVariant* result = nullptr; - - // Define the D-Bus service, object path, and interface - 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); - return false; - } - - const char* specific_object = "/"; - - GVariantBuilder optionsBuilder; - g_variant_builder_init (&optionsBuilder, G_VARIANT_TYPE ("a{sv}")); - if(!persist) // by default it will be in disk mode - { - NMLOG_ERROR("wifi connection will not persist to the disk"); - g_variant_builder_add(&optionsBuilder, "{sv}", "persist", g_variant_new_string("volatile")); - } - //else - //g_variant_builder_add(&optionsBuilder, "{sv}", "persist", g_variant_new_string("disk")); - - result = g_dbus_proxy_call_sync (proxy, "AddAndActivateConnection2", - g_variant_new("(a{sa{sv}}ooa{sv})", connBuilder, devicePath, specific_object, optionsBuilder), - G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error); - - if (error) { - NMLOG_ERROR("Failed to call AddAndActivateConnection2: %s", error->message); - g_clear_error(&error); - g_object_unref(proxy); - return false; - } - - GVariant* pathVariant; - GVariant* activeConnVariant; - GVariant* resultDict; - - g_variant_get(result, "(@o@o@a{sv})", &pathVariant, &activeConnVariant, &resultDict); - - // Extract connection and active connection paths - const char* newConnPath = g_variant_get_string(pathVariant, nullptr); - const char* activeConnPath = g_variant_get_string(activeConnVariant, nullptr); - - NMLOG_DEBUG("newconn %s; activeconn %s",newConnPath, activeConnPath); - g_variant_unref(pathVariant); - g_variant_unref(activeConnVariant); - g_variant_unref(resultDict); - g_variant_unref(result); - g_object_unref(proxy); - - return true; - } - - static bool gVariantConnectionBuilder(const Exchange::INetworkManager::WiFiConnectTo& ssidinfo, GVariantBuilder& connBuilder) - { - char *uuid = NULL; - GVariantBuilder settingsBuilder; - - if(ssidinfo.m_ssid.empty() || ssidinfo.m_ssid.length() > 32) - { - NMLOG_WARNING("ssid name is missing or invalied"); - return false; - } - - switch(ssidinfo.m_securityMode) - { - case Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_PSK_AES: - case Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_WPA2_PSK: - case Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_PSK_TKIP: - case Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA2_PSK_AES: - case Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA2_PSK_TKIP: - case Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA3_SAE: - { - if(ssidinfo.m_passphrase.empty()) - { - NMLOG_WARNING("wifi securtity type need password"); - return false; - } - break; - } - case Exchange::INetworkManager::WIFI_SECURITY_NONE: - NMLOG_WARNING("open wifi network configuration"); - break; - default: - { - NMLOG_WARNING("connection wifi securtity type not supported"); - return false; - } - } - - /* Build up the complete connection */ - g_variant_builder_init (&connBuilder, G_VARIANT_TYPE ("a{sa{sv}}")); - - /* Build up the 'connection' Setting */ - g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); - - uuid = nm_utils_uuid_generate(); - g_variant_builder_add (&settingsBuilder, "{sv}", - NM_SETTING_CONNECTION_UUID, - g_variant_new_string (uuid)); - g_free (uuid); - - g_variant_builder_add (&settingsBuilder, "{sv}", - NM_SETTING_CONNECTION_ID, - g_variant_new_string (ssidinfo.m_ssid.c_str())); - - g_variant_builder_add (&settingsBuilder, "{sv}", - NM_SETTING_CONNECTION_INTERFACE_NAME, - g_variant_new_string (GnomeUtils::getWifiIfname())); - - g_variant_builder_add (&settingsBuilder, "{sv}", - NM_SETTING_CONNECTION_TYPE, - g_variant_new_string (NM_SETTING_WIRELESS_SETTING_NAME)); - - g_variant_builder_add (&connBuilder, "{sa{sv}}", - NM_SETTING_CONNECTION_SETTING_NAME, - &settingsBuilder); - - /* Add the '802-11-wireless' Setting */ - g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); - GVariant *ssid_array = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, (const guint8 *)ssidinfo.m_ssid.c_str(), ssidinfo.m_ssid.length(), 1); - g_variant_builder_add (&settingsBuilder, "{sv}", - NM_SETTING_WIRELESS_SSID, - ssid_array); - - g_variant_builder_add (&settingsBuilder, "{sv}", - NM_SETTING_WIRELESS_MODE, - g_variant_new_string(NM_SETTING_WIRELESS_MODE_INFRA)); - - g_variant_builder_add (&settingsBuilder, "{sv}", - NM_SETTING_WIRELESS_HIDDEN, - g_variant_new_boolean(true)); // set hidden: yes - - g_variant_builder_add (&connBuilder, "{sa{sv}}", - NM_SETTING_WIRELESS_SETTING_NAME, - &settingsBuilder); - - if(!ssidinfo.m_passphrase.empty()) - { - /* Build up the '802-11-wireless-security' settings */ - g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); - - g_variant_builder_add (&settingsBuilder, "{sv}", - NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, - g_variant_new_string("wpa-psk")); - - g_variant_builder_add (&settingsBuilder, "{sv}", - NM_SETTING_WIRELESS_SECURITY_PSK, - g_variant_new_string(ssidinfo.m_passphrase.c_str())); - - g_variant_builder_add (&connBuilder, "{sa{sv}}", - NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, - &settingsBuilder); - } - - /* Build up the 'ipv4' Setting */ - g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); - g_variant_builder_add (&settingsBuilder, "{sv}", - NM_SETTING_IP_CONFIG_METHOD, - g_variant_new_string (NM_SETTING_IP4_CONFIG_METHOD_AUTO)); - - g_variant_builder_add (&connBuilder, "{sa{sv}}", - NM_SETTING_IP4_CONFIG_SETTING_NAME, - &settingsBuilder); - - /* Build up the 'ipv6' Setting */ - g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); - g_variant_builder_add (&settingsBuilder, "{sv}", - NM_SETTING_IP_CONFIG_METHOD, - g_variant_new_string (NM_SETTING_IP6_CONFIG_METHOD_AUTO)); - - g_variant_builder_add (&connBuilder, "{sa{sv}}", - NM_SETTING_IP6_CONFIG_SETTING_NAME, - &settingsBuilder); - return true; - } - - bool NetworkManagerClient::addToKnownSSIDs(const Exchange::INetworkManager::WiFiConnectTo& ssidinfo) - { - GVariantBuilder connBuilder; - bool ret = false; - GVariant *result = NULL; - GError* error = nullptr; - const char *newConPath = NULL; - - // TODO check same connection found - - ret = gVariantConnectionBuilder(ssidinfo, connBuilder); - if(!ret) { - NMLOG_WARNING("connection builder failed"); - return false; - } - - GDBusProxy *proxy = NULL; - proxy = g_dbus_proxy_new_sync(dbusConnection.getConnection(), - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.NetworkManager", - "/org/freedesktop/NetworkManager/Settings", - "org.freedesktop.NetworkManager.Settings", - NULL, - &error); - - result = g_dbus_proxy_call_sync (proxy, - "AddConnection", - g_variant_new ("(a{sa{sv}})", &connBuilder), - G_DBUS_CALL_FLAGS_NONE, -1, - NULL, &error); - - if (!proxy) { - g_dbus_error_strip_remote_error (error); - NMLOG_ERROR ("Could not create NetworkManager D-Bus proxy: %s", error->message); - g_error_free (error); - return false; - } - - if (result) { - g_variant_get (result, "(&o)", &newConPath); - NMLOG_INFO("Added: %s", newConPath); - g_variant_unref (result); - } else { - g_dbus_error_strip_remote_error (error); - NMLOG_ERROR("Error adding connection: %s", error->message); - g_clear_error (&error); - } - - return true; - } - - bool NetworkManagerClient::removeKnownSSIDs(const std::string& ssid) - { - bool ret = false; - - if(ssid.empty()) - { - NMLOG_ERROR("ssid name is empty"); - return false; - } - - std::list paths; - if(!GnomeUtils::getConnectionPaths(dbusConnection.getConnection(), paths)) - { - NMLOG_ERROR("remove connection path fetch failed"); - return false; - } - - for (const std::string& path : paths) { - // NMLOG_DEBUG("remove connection path %s", path.c_str()); - std::string connSsid; - if(getSSIDFromConnection(dbusConnection.getConnection(), path, connSsid) && connSsid == ssid) { - ret = deleteConnection(dbusConnection.getConnection(), path, connSsid); - } - } - - if(!ret) - NMLOG_ERROR("ssid '%s' Connection remove failed", ssid.c_str()); - - return ret; - } - - bool NetworkManagerClient::wifiConnect(const Exchange::INetworkManager::WiFiConnectTo& ssidinfo) - { - GVariantBuilder connBuilder; - bool ret = false, reuseConnection = false; - deviceProperties deviceProp; - std::string exsistingConn; - - if(!GnomeUtils::getDevicePropertiesByIfname(dbusConnection.getConnection(), GnomeUtils::getWifiIfname(), deviceProp)) - { - NMLOG_ERROR("no wifi device found"); - return false; - } - - if(deviceProp.path.empty() || deviceProp.state == NM_DEVICE_STATE_UNKNOWN) - { - NMLOG_WARNING("wlan0 interface not active"); - return false; - } - - /* - * - * 1. check interface state - * 2. check available connection check ssid and password security matches - * 3. if matches use the same connection to activate - * 4. else remove connection if same conn name - * 5. then activate new connection - */ - - std::list paths; - if(GnomeUtils::getwifiConnectionPaths(dbusConnection.getConnection(), deviceProp.path.c_str(), paths)) - { - for (const std::string& path : paths) { - std::string ssid, passKeyTyp = "wpa-psk"; - if(getSSIDFromConnection(dbusConnection.getConnection(), path, ssid) && ssid == ssidinfo.m_ssid) - { - exsistingConn = path; - NMLOG_WARNING("same connection exsist (%s)", exsistingConn.c_str()); - reuseConnection = true; - break; // only one connection will be activate (same property only) - } - } - } - - if(reuseConnection) - { - NMLOG_INFO("activating connection..."); - ret = gVariantConnectionBuilder(ssidinfo, connBuilder); - if(!ret) { - NMLOG_WARNING("connection builder failed"); - return false; - } - - if(updateConnctionAndactivate(dbusConnection.getConnection(), connBuilder, deviceProp.path.c_str(), exsistingConn.c_str())) - NMLOG_INFO("updated connection request success"); - else - NMLOG_ERROR("wifi connect request failed"); - } - else - { - ret = gVariantConnectionBuilder(ssidinfo, connBuilder); - if(!ret) { - NMLOG_WARNING("connection builder failed"); - return false; - } - if(addNewConnctionAndactivate(dbusConnection.getConnection(), connBuilder, deviceProp.path.c_str(), ssidinfo.m_persistSSIDInfo)) - NMLOG_INFO("wifi connect request success"); - else - NMLOG_ERROR("wifi connect request failed"); - } - - return true; - } - - bool NetworkManagerClient::wifiDisconnect() - { - GError* error = NULL; - GDBusProxy* wProxy = NULL; - deviceProperties properties; - - if(!GnomeUtils::getDevicePropertiesByIfname(dbusConnection.getConnection(), GnomeUtils::getWifiIfname(), properties) || properties.path.empty()) - { - NMLOG_ERROR("no wifi device found"); - return false; - } - - if(properties.state <= NM_DEVICE_STATE_DISCONNECTED) - { - NMLOG_WARNING("access point alreafy disconnected state"); - return true; - } - - wProxy = g_dbus_proxy_new_sync(dbusConnection.getConnection(), - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.NetworkManager", - properties.path.c_str(), - "org.freedesktop.NetworkManager.Device", - NULL, - &error); - - if (error) { - NMLOG_ERROR("Error creating proxy: %s", error->message); - g_error_free(error); - return false; - } - - g_dbus_proxy_call_sync(wProxy, "Disconnect", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); - if (error) { - NMLOG_ERROR("Error calling Disconnect method: %s", error->message); - g_error_free(error); - g_object_unref(wProxy); - return false; - } - else - NMLOG_INFO("wifi disconnected success"); - - return true; - } - - } // WPEFramework -} // Plugin +/** +* If not stated otherwise in this file or this component's LICENSE +* file the following copyright and licenses apply: +* +* Copyright 2022 RDK Management +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +**/ + +#include +#include +#include +#include +#include +#include +#include +#include "NetworkManagerLogger.h" +#include "NetworkManagerGnomeClient.h" +#include "NetworkManagerGnomeUtils.h" + +namespace WPEFramework +{ + namespace Plugin + { + + NetworkManagerClient::NetworkManagerClient() { + NMLOG_INFO("NetworkManagerClient created"); + if (!dbusConnection.InitializeBusConnection("org.freedesktop.NetworkManager")) { + NMLOG_ERROR("Failed to initialize D-Bus connection for NetworkManager"); + } + } + + NetworkManagerClient::~NetworkManagerClient() { + NMLOG_INFO("NetworkManagerClient destroyed"); + } + + static bool getSSIDFromConnection(GDBusConnection *dbusConn, 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); + return false; + } + + settingsProxy = g_dbus_proxy_call_sync(ConnProxy, + "GetSettings", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (!settingsProxy) { + g_dbus_error_strip_remote_error(error); + NMLOG_ERROR("Failed to get connection settings: %s", error->message); + g_error_free(error); + g_object_unref(ConnProxy); + return false; + } + + g_variant_get(settingsProxy, "(@a{sa{sv}})", &connection); + gVarConn = g_variant_lookup_value(connection, "connection", NULL); + + if(gVarConn == NULL) { + NMLOG_ERROR("connection Gvarient Error"); + g_variant_unref(settingsProxy); + g_object_unref(ConnProxy); + return false; + } + + if(gVarConn) + { + const char *connTyp= NULL; + G_VARIANT_LOOKUP(gVarConn, "type", "&s", &connTyp); + if(strcmp(connTyp, "802-11-wireless") != 0) + { + NMLOG_ERROR("connection is not 802-11-wireless type"); + ret = false; + } + else + { + // 802-11-wireless.ssid: + GVariant *setting = NULL; + setting = g_variant_lookup_value(connection, "802-11-wireless", NULL); + if(setting) + { + GVariantIter iter; + GVariant *value = NULL; + const char *propertyName; + g_variant_iter_init(&iter, setting); + while (g_variant_iter_next(&iter, "{&sv}", &propertyName, &value)) { + if (strcmp(propertyName, "ssid") == 0) { + // Decode SSID from GVariant of type "ay" + gsize ssid_length = 0; + const guchar *ssid_data = static_cast(g_variant_get_fixed_array(value, &ssid_length, sizeof(guchar))); + if (ssid_data && ssid_length > 0 && ssid_length <= 32) { + ssid.assign(reinterpret_cast(ssid_data), ssid_length); + NMLOG_DEBUG("SSID: %s", ssid.c_str()); + } else { + NMLOG_ERROR("Invalid SSID length: %zu (maximum is 32)", ssid_length); + ssid.empty(); + } + } + } + if(value) + g_variant_unref(value); + g_variant_unref(setting); + setting = NULL; + } + + if(!ssid.empty()) + { + // 802-11-wireless-security.key-mgmt: wpa-psk + const char* keyMgmt = NULL; + setting = g_variant_lookup_value(connection, "802-11-wireless-security", NULL); + G_VARIANT_LOOKUP(setting, "key-mgmt", "&s", &keyMgmt); + NMLOG_DEBUG("802-11-wireless-security.key-mgmt: %s", keyMgmt); + g_variant_unref(setting); + setting = NULL; + ret = true; + } + } + } + + if (gVarConn) + g_variant_unref(gVarConn); + if (connection) + g_variant_unref(connection); + if (settingsProxy) + g_variant_unref(settingsProxy); + g_object_unref(ConnProxy); + + return ret; + } + + static bool deleteConnection(GDBusConnection *dbusConn, 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); + return false; + } + + deleteVar = g_dbus_proxy_call_sync(ConnProxy, + "Delete", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (!deleteVar) { + g_dbus_error_strip_remote_error(error); + NMLOG_ERROR("Failed to get connection settings: %s", error->message); + g_error_free(error); + g_object_unref(ConnProxy); + return false; + } + else + { + NMLOG_INFO("connection '%s' Removed path: %s", ssid.c_str(), path.c_str()); + } + + if (deleteVar) + g_variant_unref(deleteVar); + g_object_unref(ConnProxy); + + return true; + } + + bool NetworkManagerClient::getKnownSSIDs(std::list& ssids) + { + std::list paths; + if(!GnomeUtils::getConnectionPaths(dbusConnection.getConnection(), paths)) + { + NMLOG_ERROR("Connection path fetch failed"); + return false; + } + + for (const std::string& path : paths) { + // NMLOG_DEBUG("connection path %s", path.c_str()); + std::string ssid; + if(getSSIDFromConnection(dbusConnection.getConnection(), path, ssid) && !ssid.empty()) + ssids.push_back(ssid); + } + + if(ssids.empty()) + return false; + return true; + } + + bool NetworkManagerClient::getConnectedSSID(const Exchange::INetworkManager::WiFiSSIDInfo &ssidinfo) + { + GError* error = NULL; + GDBusProxy* wProxy = NULL; + GVariant* result = NULL; + gchar *activeApPath = NULL; + bool ret = false; + deviceProperties properties; + + if(!GnomeUtils::getDevicePropertiesByIfname(dbusConnection.getConnection(), GnomeUtils::getWifiIfname(), properties)) + { + NMLOG_ERROR("no wifi device found"); + return false; + } + + if(properties.path.empty() || properties.state <= NM_DEVICE_STATE_DISCONNECTED) + { + NMLOG_WARNING("access point not active"); + return false; + } + + wProxy = g_dbus_proxy_new_sync(dbusConnection.getConnection(), + G_DBUS_PROXY_FLAGS_NONE, + NULL, + "org.freedesktop.NetworkManager", + properties.path.c_str(), + "org.freedesktop.NetworkManager.Device.Wireless", + NULL, // GCancellable + &error); + if(wProxy == NULL) + { + if (error) { + NMLOG_ERROR("Error creating proxy: %s", error->message); + g_error_free(error); + } + return false; + } + + result = g_dbus_proxy_get_cached_property(wProxy, "ActiveAccessPoint"); + if (!result) { + NMLOG_ERROR("Failed to get ActiveAccessPoint property."); + g_object_unref(wProxy); + return false; + } + + g_variant_get(result, "o", &activeApPath); + if(g_strdup(activeApPath) != NULL && g_strcmp0(activeApPath, "/") != 0) + { + //NMLOG_DEBUG("ActiveAccessPoint property path %s", activeApPath); + apProperties apDetails; + if(GnomeUtils::getApDetails(dbusConnection.getConnection(), g_strdup(activeApPath), apDetails)) + { + NMLOG_INFO("getApDetails success"); + ret = true; + } + } + else + NMLOG_ERROR("active access point not found"); + + g_variant_unref(result); + g_object_unref(wProxy); + return ret; + } + + bool NetworkManagerClient::getAvailableSSIDs(std::list& ssids) + { + GError* error = nullptr; + GDBusProxy* wProxy = nullptr; + deviceProperties properties; + + if(!GnomeUtils::getDevicePropertiesByIfname(dbusConnection.getConnection(), GnomeUtils::getWifiIfname(), properties)) + { + NMLOG_ERROR("no wifi device found"); + return false; + } + + if(properties.path.empty() || properties.state < NM_DEVICE_STATE_DISCONNECTED) + { + NMLOG_WARNING("access point not active"); + return false; + } + + wProxy = g_dbus_proxy_new_sync(dbusConnection.getConnection(), + G_DBUS_PROXY_FLAGS_NONE, + NULL, + "org.freedesktop.NetworkManager", + properties.path.c_str(), + "org.freedesktop.NetworkManager.Device.Wireless", + NULL, + &error); + + if (error) { + NMLOG_ERROR("Error creating proxy: %s", error->message); + g_error_free(error); + return false; + } + + GVariant* result = g_dbus_proxy_call_sync(wProxy, "GetAllAccessPoints", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (error) { + NMLOG_ERROR("Error creating proxy: %s", error->message); + g_error_free(error); + g_object_unref(wProxy); + return false; + } + + GVariantIter* iter; + const gchar* apPath; + g_variant_get(result, "(ao)", &iter); + + while (g_variant_iter_loop(iter, "o", &apPath)) { + apProperties apDetails; + NMLOG_DEBUG("Access Point Path: %s", apPath); + if(!GnomeUtils::getApDetails(dbusConnection.getConnection(), apPath, apDetails)) + { + NMLOG_WARNING("getApDetails failed"); + } + } + + g_variant_iter_free(iter); + g_variant_unref(result); + g_object_unref(wProxy); + + return true; + } + + bool NetworkManagerClient::startWifiScan(const std::string ssid) + { + GError* error = NULL; + GDBusProxy* wProxy = NULL; + deviceProperties properties; + + if(!GnomeUtils::getDevicePropertiesByIfname(dbusConnection.getConnection(), GnomeUtils::getWifiIfname(), properties)) + { + NMLOG_ERROR("no wifi device found"); + return false; + } + + if(properties.path.empty() || properties.state < NM_DEVICE_STATE_DISCONNECTED) + { + NMLOG_WARNING("access point not active"); + return false; + } + + wProxy = g_dbus_proxy_new_sync(dbusConnection.getConnection(), + G_DBUS_PROXY_FLAGS_NONE, + NULL, + "org.freedesktop.NetworkManager", + properties.path.c_str(), + "org.freedesktop.NetworkManager.Device.Wireless", + NULL, + &error); + + if (error) { + NMLOG_ERROR("Error creating proxy: %s", error->message); + g_error_free(error); + return false; + } + + GVariant* timestampVariant = NULL; + timestampVariant = g_dbus_proxy_get_cached_property(wProxy, "LastScan"); + if (!timestampVariant) { + NMLOG_ERROR("Failed to get LastScan properties"); + g_object_unref(wProxy); + return false; + } + + if (g_variant_is_of_type (timestampVariant, G_VARIANT_TYPE_INT64)) { + gint64 timestamp; + timestamp = g_variant_get_int64 (timestampVariant); + NMLOG_DEBUG("Last scan timestamp: %lld", static_cast(timestamp)); + } else { + g_warning("Unexpected variant type: %s", g_variant_get_type_string (timestampVariant)); + } + + GVariant *options = NULL; + if (!ssid.empty()) + { + // TODO fix specific ssid scanning + NMLOG_INFO("staring wifi scanning .. %s", ssid.c_str()); + GVariantBuilder settingsBuilder; + g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); + GVariant *ssid_array = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, (const guint8 *)ssid.c_str(), ssid.length(), 1); + g_variant_builder_add (&settingsBuilder, "{sv}", "ssids", ssid_array); + + // GVariantBuilder builder, array_builder; + // g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT); + // g_variant_builder_init(&array_builder, G_VARIANT_TYPE("aay")); + // g_variant_builder_add(&array_builder, "@ay", + // g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, (const guint8 *) ssid.c_str(), ssid.length(), 1) + // ); + // g_variant_builder_add(&builder, "{sv}", "ssids", g_variant_builder_end(&array_builder)); + // g_variant_builder_add(&builder, "{sv}", "hidden", g_variant_new_boolean(TRUE)); + + options = g_variant_builder_end(&settingsBuilder); + g_dbus_proxy_call_sync(wProxy, "RequestScan", options, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + } + else + g_dbus_proxy_call_sync(wProxy, "RequestScan", g_variant_new("(a{sv})", options), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (error) { + NMLOG_ERROR("Error RequestScan: %s", error->message); + g_error_free(error); + g_object_unref(wProxy); + return false; + } + + if(options) + g_variant_unref(options); + g_object_unref(wProxy); + + return true; + } + + static char * nm_utils_uuid_generate (void) + { + uuid_t uuid; + char *buf; + + buf = static_cast(g_malloc0(37)); + uuid_generate_random (uuid); + uuid_unparse_lower (uuid, &buf[0]); + return buf; + } + + bool updateConnctionAndactivate(GDBusConnection *dbusConn, 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); + return false; + } + + result = g_dbus_proxy_call_sync (proxy, "Update", + g_variant_new("(a{sa{sv}})", connBuilder), + G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error); + + if (error) { + NMLOG_ERROR("Failed to call Update : %s", error->message); + g_clear_error(&error); + g_object_unref(proxy); + return false; + } + + if(result) + 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); + + if (error) { + NMLOG_ERROR( "Failed to create proxy: %s", error->message); + g_clear_error(&error); + return false; + } + const char* specificObject = "/"; + + result = g_dbus_proxy_call_sync (proxy, "ActivateConnection", + g_variant_new("(ooo)", connPath, devicePath, specificObject), + G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error); + + if (error) { + NMLOG_ERROR("Failed to call ActivateConnection: %s", error->message); + g_clear_error(&error); + g_object_unref(proxy); + return false; + } + + if(result) { + GVariant* activeConnVariant; + g_variant_get(result, "(@o)", &activeConnVariant); + const char* activeConnPath = g_variant_get_string(activeConnVariant, nullptr); + NMLOG_DEBUG("connPath %s; activeconn %s", connPath, activeConnPath); + g_variant_unref(activeConnVariant); + } + + if(result) + g_variant_unref(result); + g_object_unref(proxy); + + return true; + } + + bool addNewConnctionAndactivate(GDBusConnection *dbusConn, 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); + return false; + } + + const char* specific_object = "/"; + + GVariantBuilder optionsBuilder; + g_variant_builder_init (&optionsBuilder, G_VARIANT_TYPE ("a{sv}")); + if(!persist) // by default it will be in disk mode + { + NMLOG_ERROR("wifi connection will not persist to the disk"); + g_variant_builder_add(&optionsBuilder, "{sv}", "persist", g_variant_new_string("volatile")); + } + //else + //g_variant_builder_add(&optionsBuilder, "{sv}", "persist", g_variant_new_string("disk")); + + result = g_dbus_proxy_call_sync (proxy, "AddAndActivateConnection2", + g_variant_new("(a{sa{sv}}ooa{sv})", connBuilder, devicePath, specific_object, optionsBuilder), + G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error); + + if (error) { + NMLOG_ERROR("Failed to call AddAndActivateConnection2: %s", error->message); + g_clear_error(&error); + g_object_unref(proxy); + return false; + } + + GVariant* pathVariant; + GVariant* activeConnVariant; + GVariant* resultDict; + + g_variant_get(result, "(@o@o@a{sv})", &pathVariant, &activeConnVariant, &resultDict); + + // Extract connection and active connection paths + const char* newConnPath = g_variant_get_string(pathVariant, nullptr); + const char* activeConnPath = g_variant_get_string(activeConnVariant, nullptr); + + NMLOG_DEBUG("newconn %s; activeconn %s",newConnPath, activeConnPath); + g_variant_unref(pathVariant); + g_variant_unref(activeConnVariant); + g_variant_unref(resultDict); + g_variant_unref(result); + g_object_unref(proxy); + + return true; + } + + static bool gVariantConnectionBuilder(const Exchange::INetworkManager::WiFiConnectTo& ssidinfo, GVariantBuilder& connBuilder) + { + char *uuid = NULL; + GVariantBuilder settingsBuilder; + + if(ssidinfo.m_ssid.empty() || ssidinfo.m_ssid.length() > 32) + { + NMLOG_WARNING("ssid name is missing or invalied"); + return false; + } + + switch(ssidinfo.m_securityMode) + { + case Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_PSK_AES: + case Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_WPA2_PSK: + case Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_PSK_TKIP: + case Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA2_PSK_AES: + case Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA2_PSK_TKIP: + case Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA3_SAE: + { + if(ssidinfo.m_passphrase.empty()) + { + NMLOG_WARNING("wifi securtity type need password"); + return false; + } + break; + } + case Exchange::INetworkManager::WIFI_SECURITY_NONE: + NMLOG_WARNING("open wifi network configuration"); + break; + default: + { + NMLOG_WARNING("connection wifi securtity type not supported"); + return false; + } + } + + /* Build up the complete connection */ + g_variant_builder_init (&connBuilder, G_VARIANT_TYPE ("a{sa{sv}}")); + + /* Build up the 'connection' Setting */ + g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); + + uuid = nm_utils_uuid_generate(); + g_variant_builder_add (&settingsBuilder, "{sv}", + NM_SETTING_CONNECTION_UUID, + g_variant_new_string (uuid)); + g_free (uuid); + + g_variant_builder_add (&settingsBuilder, "{sv}", + NM_SETTING_CONNECTION_ID, + g_variant_new_string (ssidinfo.m_ssid.c_str())); + + g_variant_builder_add (&settingsBuilder, "{sv}", + NM_SETTING_CONNECTION_INTERFACE_NAME, + g_variant_new_string (GnomeUtils::getWifiIfname())); + + g_variant_builder_add (&settingsBuilder, "{sv}", + NM_SETTING_CONNECTION_TYPE, + g_variant_new_string (NM_SETTING_WIRELESS_SETTING_NAME)); + + g_variant_builder_add (&connBuilder, "{sa{sv}}", + NM_SETTING_CONNECTION_SETTING_NAME, + &settingsBuilder); + + /* Add the '802-11-wireless' Setting */ + g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); + GVariant *ssid_array = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, (const guint8 *)ssidinfo.m_ssid.c_str(), ssidinfo.m_ssid.length(), 1); + g_variant_builder_add (&settingsBuilder, "{sv}", + NM_SETTING_WIRELESS_SSID, + ssid_array); + + g_variant_builder_add (&settingsBuilder, "{sv}", + NM_SETTING_WIRELESS_MODE, + g_variant_new_string(NM_SETTING_WIRELESS_MODE_INFRA)); + + g_variant_builder_add (&settingsBuilder, "{sv}", + NM_SETTING_WIRELESS_HIDDEN, + g_variant_new_boolean(true)); // set hidden: yes + + g_variant_builder_add (&connBuilder, "{sa{sv}}", + NM_SETTING_WIRELESS_SETTING_NAME, + &settingsBuilder); + + if(!ssidinfo.m_passphrase.empty()) + { + /* Build up the '802-11-wireless-security' settings */ + g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); + + g_variant_builder_add (&settingsBuilder, "{sv}", + NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, + g_variant_new_string("wpa-psk")); + + g_variant_builder_add (&settingsBuilder, "{sv}", + NM_SETTING_WIRELESS_SECURITY_PSK, + g_variant_new_string(ssidinfo.m_passphrase.c_str())); + + g_variant_builder_add (&connBuilder, "{sa{sv}}", + NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, + &settingsBuilder); + } + + /* Build up the 'ipv4' Setting */ + g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); + g_variant_builder_add (&settingsBuilder, "{sv}", + NM_SETTING_IP_CONFIG_METHOD, + g_variant_new_string (NM_SETTING_IP4_CONFIG_METHOD_AUTO)); + + g_variant_builder_add (&connBuilder, "{sa{sv}}", + NM_SETTING_IP4_CONFIG_SETTING_NAME, + &settingsBuilder); + + /* Build up the 'ipv6' Setting */ + g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); + g_variant_builder_add (&settingsBuilder, "{sv}", + NM_SETTING_IP_CONFIG_METHOD, + g_variant_new_string (NM_SETTING_IP6_CONFIG_METHOD_AUTO)); + + g_variant_builder_add (&connBuilder, "{sa{sv}}", + NM_SETTING_IP6_CONFIG_SETTING_NAME, + &settingsBuilder); + return true; + } + + bool NetworkManagerClient::addToKnownSSIDs(const Exchange::INetworkManager::WiFiConnectTo& ssidinfo) + { + // TODO check argument length + GVariantBuilder connBuilder; + bool ret = false, reuseConnection = false; + GVariant *result = NULL; + GError* error = NULL; + const char *newConPath = NULL; + std::string exsistingConn; + deviceProperties deviceProp; + + if(!GnomeUtils::getDevicePropertiesByIfname(dbusConnection.getConnection(), GnomeUtils::getWifiIfname(), deviceProp)) + { + NMLOG_ERROR("no wifi device found"); + return false; + } + + if(deviceProp.path.empty() || deviceProp.state == NM_DEVICE_STATE_UNKNOWN) + { + NMLOG_WARNING("wlan0 interface not active"); + return false; + } + + std::list paths; + if(GnomeUtils::getwifiConnectionPaths(dbusConnection.getConnection(), deviceProp.path.c_str(), paths)) + { + for (const std::string& path : paths) + { + std::string ssid; + if(getSSIDFromConnection(dbusConnection.getConnection(), path, ssid) && ssid == ssidinfo.m_ssid) + { + exsistingConn = path; + NMLOG_DEBUG("same connection exsist (%s) updating ...", exsistingConn.c_str()); + reuseConnection = true; + break; // only one connection will be activate (same property only) + } + } + } + + if(!gVariantConnectionBuilder(ssidinfo, connBuilder)) + { + NMLOG_WARNING("connection builder failed"); + return false; + } + + if(reuseConnection) + { + GDBusProxy* proxy = g_dbus_proxy_new_sync(dbusConnection.getConnection(), + G_DBUS_PROXY_FLAGS_NONE, NULL, + "org.freedesktop.NetworkManager", exsistingConn.c_str(), + "org.freedesktop.NetworkManager.Settings.Connection", + NULL, &error); + + if (error) { + NMLOG_ERROR( "Failed to create proxy: %s", error->message); + g_clear_error(&error); + return false; + } + + if(proxy != nullptr) + { + result = g_dbus_proxy_call_sync (proxy, "Update", + g_variant_new("(a{sa{sv}})", connBuilder), + G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error); + + if (error == nullptr) { + NMLOG_DEBUG("same connection updated success : %s", exsistingConn.c_str()); + ret = true; + } + else { + NMLOG_ERROR("Failed to call Update : %s", error->message); + g_clear_error(&error); + } + if(result) + g_variant_unref (result); + g_object_unref(proxy); + } + } + else + { + GDBusProxy *proxy = NULL; + proxy = g_dbus_proxy_new_sync(dbusConnection.getConnection(), + G_DBUS_PROXY_FLAGS_NONE, + NULL, + "org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager/Settings", + "org.freedesktop.NetworkManager.Settings", + NULL, + &error); + if (proxy != nullptr) + { + result = g_dbus_proxy_call_sync (proxy, + "AddConnection", + g_variant_new ("(a{sa{sv}})", &connBuilder), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + + if (error != nullptr) { + g_dbus_error_strip_remote_error (error); + NMLOG_ERROR ("Could not create NetworkManager/Settings proxy: %s", error->message); + g_error_free (error); + g_object_unref(proxy); + return false; + } + + if (result) { + g_variant_get (result, "(&o)", &newConPath); + NMLOG_INFO("Added a new connection: %s", newConPath); + ret = true; + g_variant_unref (result); + } else { + g_dbus_error_strip_remote_error (error); + NMLOG_ERROR("Error adding connection: %s", error->message); + g_clear_error (&error); + } + g_object_unref(proxy); + } + } + + return ret; + } + + bool NetworkManagerClient::removeKnownSSIDs(const std::string& ssid) + { + bool ret = false; + + if(ssid.empty()) + { + NMLOG_ERROR("ssid name is empty"); + return false; + } + + std::list paths; + if(!GnomeUtils::getConnectionPaths(dbusConnection.getConnection(), paths)) + { + NMLOG_ERROR("remove connection path fetch failed"); + return false; + } + + for (const std::string& path : paths) { + // NMLOG_DEBUG("remove connection path %s", path.c_str()); + std::string connSsid; + if(getSSIDFromConnection(dbusConnection.getConnection(), path, connSsid) && connSsid == ssid) { + ret = deleteConnection(dbusConnection.getConnection(), path, connSsid); + } + } + + if(!ret) + NMLOG_ERROR("ssid '%s' Connection remove failed", ssid.c_str()); + + return ret; + } + + bool NetworkManagerClient::getWiFiSignalStrength(string& ssid, string& signalStrength, Exchange::INetworkManager::WiFiSignalQuality& quality) + { + Exchange::INetworkManager::WiFiSSIDInfo ssidInfo; + int signalStrengthInt= 0; + + if(getConnectedSSID(ssidInfo)) + { + NMLOG_ERROR("no wifi connected"); + return false; + } + else + { + ssid = ssidInfo.m_ssid; + signalStrength = ssidInfo.m_signalStrength; + + if(!signalStrength.empty()) + signalStrengthInt = std::stoi(signalStrength.c_str()); + + if (signalStrengthInt == 0) + quality = Exchange::INetworkManager::WiFiSignalQuality::WIFI_SIGNAL_DISCONNECTED; + else if (signalStrengthInt > 0 && signalStrengthInt <= 25) + quality = Exchange::INetworkManager::WiFiSignalQuality::WIFI_SIGNAL_WEAK; + else if (signalStrengthInt > 25 && signalStrengthInt <= 50) + quality = Exchange::INetworkManager::WiFiSignalQuality::WIFI_SIGNAL_FAIR; + else if (signalStrengthInt > 50 && signalStrengthInt <= 75) + quality = Exchange::INetworkManager::WiFiSignalQuality::WIFI_SIGNAL_GOOD; + else // signalStrengthInt > 75 + quality = Exchange::INetworkManager::WiFiSignalQuality::WIFI_SIGNAL_EXCELLENT; + + NMLOG_INFO ("wifi signal strength success %s", signalStrength.c_str()); + } + return true; + } + + bool NetworkManagerClient::getWifiState(Exchange::INetworkManager::WiFiState &state) + { + deviceProperties deviceProp; + state = Exchange::INetworkManager::WiFiState::WIFI_STATE_UNINSTALLED; + + if(!GnomeUtils::getDevicePropertiesByIfname(dbusConnection.getConnection(), GnomeUtils::getWifiIfname(), deviceProp)) + { + NMLOG_ERROR("no wifi device found"); + return false; + } + // Todo check NMDeviceStateReason for more information + switch(deviceProp.state) + { + case NM_DEVICE_STATE_ACTIVATED: + state = Exchange::INetworkManager::WiFiState::WIFI_STATE_CONNECTED; + break; + case NM_DEVICE_STATE_PREPARE: + case NM_DEVICE_STATE_CONFIG: + state = Exchange::INetworkManager::WiFiState::WIFI_STATE_PAIRING; + break; + case NM_DEVICE_STATE_NEED_AUTH: + case NM_DEVICE_STATE_IP_CONFIG: + case NM_DEVICE_STATE_IP_CHECK: + case NM_DEVICE_STATE_SECONDARIES: + state = Exchange::INetworkManager::WiFiState::WIFI_STATE_CONNECTING; + break; + case NM_DEVICE_STATE_DEACTIVATING: + case NM_DEVICE_STATE_FAILED: + case NM_DEVICE_STATE_DISCONNECTED: + state = Exchange::INetworkManager::WiFiState::WIFI_STATE_DISCONNECTED; + break; + default: + state = Exchange::INetworkManager::WiFiState::WIFI_STATE_DISABLED; + } + NMLOG_DEBUG("nm Wifi sate %d mapped state %d ", (int)deviceProp.state, (int)state); + return true; + } + + bool NetworkManagerClient::wifiConnect(const Exchange::INetworkManager::WiFiConnectTo& ssidinfo) + { + // TODO check argument length password min 8 and 32 + GVariantBuilder connBuilder; + bool ret = false, reuseConnection = false; + deviceProperties deviceProp; + std::string exsistingConn; + + if(!GnomeUtils::getDevicePropertiesByIfname(dbusConnection.getConnection(), GnomeUtils::getWifiIfname(), deviceProp)) + { + NMLOG_ERROR("no wifi device found"); + return false; + } + + if(deviceProp.path.empty() || deviceProp.state == NM_DEVICE_STATE_UNKNOWN) + { + NMLOG_WARNING("wlan0 interface not active"); + return false; + } + + /* + * + * 1. check interface state + * 2. check available connection check ssid and password security matches + * 3. if matches use the same connection to activate + * 4. else remove connection if same conn name + * 5. then activate new connection + */ + + std::list paths; + if(GnomeUtils::getwifiConnectionPaths(dbusConnection.getConnection(), deviceProp.path.c_str(), paths)) + { + for (const std::string& path : paths) { + std::string ssid, passKeyTyp = "wpa-psk"; + if(getSSIDFromConnection(dbusConnection.getConnection(), path, ssid) && ssid == ssidinfo.m_ssid) + { + exsistingConn = path; + NMLOG_WARNING("same connection exsist (%s)", exsistingConn.c_str()); + reuseConnection = true; + break; // only one connection will be activate (same property only) + } + } + } + + if(reuseConnection) + { + NMLOG_INFO("activating connection..."); + ret = gVariantConnectionBuilder(ssidinfo, connBuilder); + if(!ret) { + NMLOG_WARNING("connection builder failed"); + return false; + } + + if(updateConnctionAndactivate(dbusConnection.getConnection(), connBuilder, deviceProp.path.c_str(), exsistingConn.c_str())) + NMLOG_INFO("updated connection request success"); + else + NMLOG_ERROR("wifi connect request failed"); + } + else + { + ret = gVariantConnectionBuilder(ssidinfo, connBuilder); + if(!ret) { + NMLOG_WARNING("connection builder failed"); + return false; + } + if(addNewConnctionAndactivate(dbusConnection.getConnection(), connBuilder, deviceProp.path.c_str(), ssidinfo.m_persistSSIDInfo)) + NMLOG_INFO("wifi connect request success"); + else + NMLOG_ERROR("wifi connect request failed"); + } + + return true; + } + + bool NetworkManagerClient::wifiDisconnect() + { + GError* error = NULL; + GDBusProxy* wProxy = NULL; + deviceProperties properties; + + if(!GnomeUtils::getDevicePropertiesByIfname(dbusConnection.getConnection(), GnomeUtils::getWifiIfname(), properties) || properties.path.empty()) + { + NMLOG_ERROR("no wifi device found"); + return false; + } + + if(properties.state <= NM_DEVICE_STATE_DISCONNECTED) + { + NMLOG_WARNING("access point alreafy disconnected state"); + return true; + } + + wProxy = g_dbus_proxy_new_sync(dbusConnection.getConnection(), + G_DBUS_PROXY_FLAGS_NONE, + NULL, + "org.freedesktop.NetworkManager", + properties.path.c_str(), + "org.freedesktop.NetworkManager.Device", + NULL, + &error); + + if (error) { + NMLOG_ERROR("Error creating proxy: %s", error->message); + g_error_free(error); + return false; + } + + g_dbus_proxy_call_sync(wProxy, "Disconnect", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (error) { + NMLOG_ERROR("Error calling Disconnect method: %s", error->message); + g_error_free(error); + g_object_unref(wProxy); + return false; + } + else + NMLOG_INFO("wifi disconnected success"); + + return true; + } + + } // WPEFramework +} // Plugin diff --git a/GnomeProxy/NetworkManagerGnomeClient.h b/GnomeProxy/NetworkManagerGnomeClient.h index 866cda39..7873d639 100644 --- a/GnomeProxy/NetworkManagerGnomeClient.h +++ b/GnomeProxy/NetworkManagerGnomeClient.h @@ -1,62 +1,67 @@ -/* - * If not stated otherwise in this file or this component's LICENSE file the - * following copyright and licenses apply: - * - * Copyright 2020 RDK Management - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once -#include -#include -#include -#include - -#include "NetworkManagerLogger.h" -#include "NetworkManagerDbusMgr.h" -#include "INetworkManager.h" - -namespace WPEFramework -{ - namespace Plugin - { - class NetworkManagerClient - { - public: - static NetworkManagerClient* getInstance() - { - static NetworkManagerClient instance; - return &instance; - } - - NetworkManagerClient(const NetworkManagerClient&) = delete; - NetworkManagerClient& operator=(const NetworkManagerClient&) = delete; - - bool getKnownSSIDs(std::list& ssids); - bool getAvailableSSIDs(std::list& ssids); - bool getConnectedSSID(const Exchange::INetworkManager::WiFiSSIDInfo& ssidinfo); - bool addToKnownSSIDs(const Exchange::INetworkManager::WiFiConnectTo& ssidinfo); - bool removeKnownSSIDs(const std::string& ssid); - bool startWifiScanning(const std::string ssid = ""); - bool wifiConnect(const Exchange::INetworkManager::WiFiConnectTo& ssidinfo); - bool wifiDisconnect(); - - private: - NetworkManagerClient(); - ~NetworkManagerClient(); - - DbusConnectionManager dbusConnection; - }; - } // Plugin +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2020 RDK Management + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include +#include +#include +#include + +#include "NetworkManagerLogger.h" +#include "NetworkManagerDbusMgr.h" +#include "INetworkManager.h" + +namespace WPEFramework +{ + namespace Plugin + { + class NetworkManagerClient + { + public: + static NetworkManagerClient* getInstance() + { + static NetworkManagerClient instance; + return &instance; + } + + NetworkManagerClient(const NetworkManagerClient&) = delete; + NetworkManagerClient& operator=(const NetworkManagerClient&) = delete; + + bool getKnownSSIDs(std::list& ssids); + bool getAvailableSSIDs(std::list& ssids); + bool getConnectedSSID(const Exchange::INetworkManager::WiFiSSIDInfo& ssidinfo); + bool addToKnownSSIDs(const Exchange::INetworkManager::WiFiConnectTo& ssidinfo); + bool removeKnownSSIDs(const std::string& ssid); + bool startWifiScan(const std::string ssid = ""); + bool wifiConnect(const Exchange::INetworkManager::WiFiConnectTo& ssidinfo); + bool wifiDisconnect(); + bool getWifiState(Exchange::INetworkManager::WiFiState &state); + bool getWiFiSignalStrength(std::string& ssid, std::string& signalStrength, Exchange::INetworkManager::WiFiSignalQuality& quality); + bool startWPS(const Exchange::INetworkManager::WiFiWPS& method , const string& wps_pin ) {return false;} + bool stopWPS() {return false;} + bool stopWiFiScan() {return false;} + + private: + NetworkManagerClient(); + ~NetworkManagerClient(); + + DbusConnectionManager dbusConnection; + }; + } // Plugin } // WPEFramework \ No newline at end of file diff --git a/GnomeProxy/NetworkManagerGnomeUtils.cpp b/GnomeProxy/NetworkManagerGnomeUtils.cpp index fc6515d6..6146f94e 100644 --- a/GnomeProxy/NetworkManagerGnomeUtils.cpp +++ b/GnomeProxy/NetworkManagerGnomeUtils.cpp @@ -1,452 +1,453 @@ -/** -* If not stated otherwise in this file or this component's LICENSE -* file the following copyright and licenses apply: -* -* Copyright 2022 RDK Management -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -**/ - -#include -#include -#include -#include - -#include "NetworkManagerLogger.h" -#include "NetworkManagerGnomeUtils.h" -#include "NetworkManagerDbusMgr.h" - -namespace WPEFramework -{ - namespace Plugin - { - static const char* ifnameEth = "eth0"; - static const char* ifnameWlan = "wlan0"; - - std::string GnomeUtils::getCommaSeparatedSSIDs(const std::list& ssids) { - std::string result; - for (auto it = ssids.begin(); it != ssids.end(); ++it) { - result += *it; - if (std::next(it) != ssids.end()) { - result += ", "; - } - } - return result; - } - - void GnomeUtils::printKeyVariant(const char *setting_name, GVariant *setting) - { - GVariantIter iter; - const char *property_name; - GVariant *value; - char *printed_value; - - NMLOG_DEBUG(" %s:", setting_name); - g_variant_iter_init(&iter, setting); - while (g_variant_iter_next(&iter, "{&sv}", &property_name, &value)) { - printed_value = g_variant_print(value, FALSE); - if (strcmp(printed_value, "[]") != 0) - NMLOG_DEBUG(" %s: %s", property_name, printed_value); - g_free(printed_value); - g_variant_unref(value); - } - } - - // TODO change this function - const char* GnomeUtils::getWifiIfname() { return ifnameWlan; } - const char* GnomeUtils::getEthIfname() { return ifnameEth; } - - bool GnomeUtils::getCachedPropertyU(GDBusProxy* proxy, const char* propertiy, uint32_t *value) - { - GVariant* result = NULL; - result = g_dbus_proxy_get_cached_property(proxy, propertiy); - if (!result) { - NMLOG_ERROR("Failed to get '%s' properties", propertiy); - g_object_unref(proxy); - return false; - } - - if (g_variant_is_of_type (result, G_VARIANT_TYPE_UINT32)) { - *value = g_variant_get_uint32(result); - //NMLOG_DEBUG("%s: %d", propertiy, *value); - } - else - NMLOG_WARNING("Unexpected type returned property: %s", g_variant_get_type_string(result)); - g_variant_unref(result); - return true; - } - - bool GnomeUtils::getDevicePropertiesByPath(GDBusConnection *dbusConn, const char* devicePath, deviceProperties& 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 DBus.Properties method: %s", error->message); - g_error_free(error); - return false; - } - - - if(GnomeUtils::getCachedPropertyU(nmProxy, "DeviceType", &value)) - { - properties.deviceType = static_cast(value); - } - else - NMLOG_ERROR("'DeviceType' property failed"); - - devicesVar = g_dbus_proxy_get_cached_property(nmProxy, "Interface"); - if (devicesVar) { - const gchar *iface = g_variant_get_string(devicesVar, NULL); - if(iface != NULL) - properties.interface = iface; - //NMLOG_DEBUG("Interface: %s", iface); - g_variant_unref(devicesVar); - } - else - NMLOG_ERROR("'Interface' property failed"); - - guint32 devState, stateReason; - devicesVar = g_dbus_proxy_get_cached_property(nmProxy, "StateReason"); - if (devicesVar) { - g_variant_get(devicesVar, "(uu)", &devState, &stateReason); - properties.state = static_cast(devState); - properties.stateReason = static_cast(stateReason); - g_variant_unref(devicesVar); - } - else - NMLOG_ERROR("'StateReason' property failed"); - - g_object_unref(nmProxy); - return true; - } - - bool GnomeUtils::getDevicePropertiesByIfname(GDBusConnection *dbusConn, const char* ifaceName, deviceProperties& 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); - - devicesVar = g_dbus_proxy_call_sync(nmProxy, "GetDevices", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); - if (error) { - NMLOG_ERROR("Error calling GetDevices method: %s", error->message); - g_error_free(error); - g_object_unref(nmProxy); - return false; - } - - GVariantIter* iter; - const gchar* devicePath; - g_variant_get(devicesVar, "(ao)", &iter); - while (g_variant_iter_loop(iter, "o", &devicePath)) - { - if(devicePath != NULL ) - { - if(getDevicePropertiesByPath(dbusConn, devicePath, properties) && properties.interface == ifaceName) - { - properties.path = devicePath; - ret = true; - break; - } - } - } - g_variant_iter_free(iter); - - if(!ret) - NMLOG_ERROR("'%s' interface not found", ifaceName); - - if(devicesVar) - g_variant_unref(devicesVar); - if(nmProxy) - g_object_unref(nmProxy); - return ret; - } - - bool GnomeUtils::getDeviceByIpIface(GDBusConnection *dbusConn, const gchar *iface_name, std::string& path) - { - // TODO Fix Error calling method: - // GDBus.Error:org.freedesktop.NetworkManager.UnknownDevice: No device found for the requested iface - // in wsl linux - GError *error = NULL; - GVariant *result; - gchar *device_path = NULL; - bool ret = false; - - result = g_dbus_connection_call_sync( - dbusConn, - "org.freedesktop.NetworkManager", // D-Bus name - "/org/freedesktop/NetworkManager", // Object path - "org.freedesktop.NetworkManager", // Interface - "GetDeviceByIpIface", // Method name - g_variant_new("(s)", iface_name), // Input parameter (the interface name) - G_VARIANT_TYPE("(o)"), // Expected return type (object path) - G_DBUS_CALL_FLAGS_NONE, - -1, // Default timeout - NULL, - &error - ); - - if (error != NULL) { - NMLOG_ERROR("calling GetDeviceByIpIface: %s", error->message); - g_error_free(error); - return ret; - } - - if (g_variant_is_of_type (result, (const GVariantType *) "(o)")) - { - g_variant_get(result, "(o)", &device_path); - if(g_strdup(device_path) != NULL) - { - path = std::string(g_strdup(device_path)); - ret = true; - } - } - //NMLOG_DEBUG("%s device path %s", iface_name, path.c_str()); - g_variant_unref(result); - return ret; - } - - bool GnomeUtils::getApDetails(GDBusConnection *connection, const char* apPath, apProperties& apDetails) - { - char *bssid = NULL; - uint8_t strength = 0; - GError* error = NULL; - GVariant* result = NULL; - - GDBusProxy* proxy = g_dbus_proxy_new_sync(connection, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.NetworkManager", - apPath, - "org.freedesktop.NetworkManager.AccessPoint", - NULL, - &error); - - if (error) { - NMLOG_ERROR("creating proxy: %s", error->message); - g_error_free(error); - return false; - } - - gsize ssid_length = 0; - result = g_dbus_proxy_get_cached_property(proxy,"Ssid"); - if (!result) { - NMLOG_ERROR("Failed to get AP properties."); - g_object_unref(proxy); - return false; - } - const guchar *ssid_data = static_cast(g_variant_get_fixed_array(result, &ssid_length, sizeof(guchar))); - if (ssid_data && ssid_length > 0 && ssid_length <= 32) { - apDetails.ssid.assign(reinterpret_cast(ssid_data), ssid_length); - NMLOG_DEBUG("SSID: %s", apDetails.ssid.c_str()); - } else { - NMLOG_ERROR("Invalid SSID length: %zu (maximum is 32)", ssid_length); - apDetails.ssid="---"; - } - g_variant_unref(result); - - result = g_dbus_proxy_get_cached_property(proxy,"HwAddress"); - if (!result) { - NMLOG_ERROR("Failed to get AP properties."); - g_object_unref(proxy); - return false; - } - g_variant_get(result, "s", &bssid); - apDetails.bssid.assign(bssid); - NMLOG_DEBUG("bssid %s", apDetails.bssid.c_str()); - g_variant_unref(result); - - result = g_dbus_proxy_get_cached_property(proxy,"Strength"); - if (!result) { - NMLOG_ERROR("Failed to get AP properties."); - g_object_unref(proxy); - return false; - } - g_variant_get(result, "y", &strength); - NMLOG_DEBUG("strength %d", strength); - g_variant_unref(result); - - GnomeUtils::getCachedPropertyU(proxy, "Flags", &apDetails.flags); - GnomeUtils::getCachedPropertyU(proxy, "WpaFlags", &apDetails.wpaFlags); - GnomeUtils::getCachedPropertyU(proxy, "RsnFlags", &apDetails.rsnFlags); - GnomeUtils::getCachedPropertyU(proxy, "Mode", &apDetails.mode); - GnomeUtils::getCachedPropertyU(proxy, "Frequency", &apDetails.frequency); - - g_object_unref(proxy); - - return true; - } - - bool GnomeUtils::getwifiConnectionPaths(GDBusConnection *dbusConn, const char* devicePath, std::list& 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; - } - - result = g_dbus_proxy_get_cached_property(nmProxy, "AvailableConnections"); - if (!result) { - NMLOG_ERROR("Failed to get AvailableConnections property."); - g_object_unref(nmProxy); - return ret; - } - - GVariantIter* iter; - const gchar* connPath = NULL; - g_variant_get(result, "ao", &iter); - while (g_variant_iter_loop(iter, "o", &connPath)) { - if(g_strdup(connPath) != NULL && g_strcmp0(connPath, "/") != 0) - { - paths.push_back(connPath); - // NMLOG_DEBUG("AvailableConnections Path: %s", connPath); - ret = true; - } - } - - g_variant_iter_free(iter); - g_variant_unref(result); - g_object_unref(nmProxy); - return ret; - } - - bool GnomeUtils::getConnectionPaths(GDBusConnection *dbusConn, std::list& 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); - - 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", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - if(listProxy == NULL) - { - if (!error) { - NMLOG_ERROR("ListConnections failed: %s", error->message); - g_error_free(error); - g_object_unref(sProxy); - return false; - } - else - NMLOG_ERROR("ListConnections proxy failed no error message"); - } - - g_variant_get(listProxy, "(^ao)", &paths); - g_variant_unref(listProxy); - - if(paths == NULL) - { - NMLOG_WARNING("no connection path available"); - g_object_unref(sProxy); - return false; - } - - for (int i = 0; paths[i]; i++) { - //NMLOG_DEBUG("Connection path: %s", paths[i]); - pathsList.push_back(paths[i]); - } - - g_object_unref(sProxy); - if(pathsList.empty()) - return false; - - return true; - } - - bool GnomeUtils::getDeviceState(GDBusConnection *dbusConn, const gchar *iface_name, NMDeviceState& state) - { - deviceProperties devProp; - if(!GnomeUtils::getDevicePropertiesByIfname(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) - { - deviceProperties devProp; - if(!GnomeUtils::getDevicePropertiesByIfname(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 - +/** +* If not stated otherwise in this file or this component's LICENSE +* file the following copyright and licenses apply: +* +* Copyright 2022 RDK Management +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +**/ + +#include +#include +#include +#include + +#include "NetworkManagerLogger.h" +#include "NetworkManagerGnomeUtils.h" +#include "NetworkManagerDbusMgr.h" + +namespace WPEFramework +{ + namespace Plugin + { + static const char* ifnameEth = "eth0"; + static const char* ifnameWlan = "wlan0"; + + std::string GnomeUtils::getCommaSeparatedSSIDs(const std::list& ssids) { + std::string result; + for (auto it = ssids.begin(); it != ssids.end(); ++it) { + result += *it; + if (std::next(it) != ssids.end()) { + result += ", "; + } + } + return result; + } + + void GnomeUtils::printKeyVariant(const char *setting_name, GVariant *setting) + { + GVariantIter iter; + const char *property_name; + GVariant *value; + char *printed_value; + + NMLOG_DEBUG(" %s:", setting_name); + g_variant_iter_init(&iter, setting); + while (g_variant_iter_next(&iter, "{&sv}", &property_name, &value)) { + printed_value = g_variant_print(value, FALSE); + if (strcmp(printed_value, "[]") != 0) + NMLOG_DEBUG(" %s: %s", property_name, printed_value); + g_free(printed_value); + g_variant_unref(value); + } + } + + // TODO change this function + const char* GnomeUtils::getWifiIfname() { return ifnameWlan; } + const char* GnomeUtils::getEthIfname() { return ifnameEth; } + + bool GnomeUtils::getCachedPropertyU(GDBusProxy* proxy, const char* propertiy, uint32_t *value) + { + GVariant* result = NULL; + result = g_dbus_proxy_get_cached_property(proxy, propertiy); + if (!result) { + NMLOG_ERROR("Failed to get '%s' properties", propertiy); + return false; + } + + if (result != NULL && g_variant_is_of_type (result, G_VARIANT_TYPE_UINT32)) { + *value = g_variant_get_uint32(result); + //NMLOG_DEBUG("%s: %d", propertiy, *value); + } + else + NMLOG_WARNING("Unexpected type returned property: %s", g_variant_get_type_string(result)); + g_variant_unref(result); + return true; + } + + bool GnomeUtils::getDevicePropertiesByPath(GDBusConnection *dbusConn, const char* devicePath, deviceProperties& 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 DBus.Properties method: %s", error->message); + g_error_free(error); + return false; + } + + + if(GnomeUtils::getCachedPropertyU(nmProxy, "DeviceType", &value)) + { + properties.deviceType = static_cast(value); + } + else + NMLOG_ERROR("'DeviceType' property failed"); + + devicesVar = g_dbus_proxy_get_cached_property(nmProxy, "Interface"); + if (devicesVar) { + const gchar *iface = g_variant_get_string(devicesVar, NULL); + if(iface != NULL) + properties.interface = iface; + //NMLOG_DEBUG("Interface: %s", iface); + g_variant_unref(devicesVar); + } + else + NMLOG_ERROR("'Interface' property failed"); + + guint32 devState, stateReason; + devicesVar = g_dbus_proxy_get_cached_property(nmProxy, "StateReason"); + if (devicesVar) { + g_variant_get(devicesVar, "(uu)", &devState, &stateReason); + properties.state = static_cast(devState); + properties.stateReason = static_cast(stateReason); + g_variant_unref(devicesVar); + } + else + NMLOG_ERROR("'StateReason' property failed"); + + g_object_unref(nmProxy); + return true; + } + + bool GnomeUtils::getDevicePropertiesByIfname(GDBusConnection *dbusConn, const char* ifaceName, deviceProperties& 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); + + devicesVar = g_dbus_proxy_call_sync(nmProxy, "GetDevices", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (error) { + NMLOG_ERROR("Error calling GetDevices method: %s", error->message); + g_error_free(error); + g_object_unref(nmProxy); + return false; + } + + GVariantIter* iter; + const gchar* devicePath; + g_variant_get(devicesVar, "(ao)", &iter); + while (g_variant_iter_loop(iter, "o", &devicePath)) + { + if(devicePath != NULL ) + { + if(getDevicePropertiesByPath(dbusConn, devicePath, properties) && properties.interface == ifaceName) + { + properties.path = devicePath; + ret = true; + break; + } + } + } + g_variant_iter_free(iter); + + if(!ret) + NMLOG_ERROR("'%s' interface not found", ifaceName); + + if(devicesVar) + g_variant_unref(devicesVar); + if(nmProxy) + g_object_unref(nmProxy); + return ret; + } + + bool GnomeUtils::getDeviceByIpIface(GDBusConnection *dbusConn, const gchar *iface_name, std::string& path) + { + // TODO Fix Error calling method: + // GDBus.Error:org.freedesktop.NetworkManager.UnknownDevice: No device found for the requested iface + // in wsl linux + GError *error = NULL; + GVariant *result; + gchar *device_path = NULL; + bool ret = false; + + result = g_dbus_connection_call_sync( + dbusConn, + "org.freedesktop.NetworkManager", // D-Bus name + "/org/freedesktop/NetworkManager", // Object path + "org.freedesktop.NetworkManager", // Interface + "GetDeviceByIpIface", // Method name + g_variant_new("(s)", iface_name), // Input parameter (the interface name) + G_VARIANT_TYPE("(o)"), // Expected return type (object path) + G_DBUS_CALL_FLAGS_NONE, + -1, // Default timeout + NULL, + &error + ); + + if (error != NULL) { + NMLOG_ERROR("calling GetDeviceByIpIface: %s", error->message); + g_error_free(error); + return ret; + } + + if (g_variant_is_of_type (result, (const GVariantType *) "(o)")) + { + g_variant_get(result, "(o)", &device_path); + if(g_strdup(device_path) != NULL) + { + path = std::string(g_strdup(device_path)); + ret = true; + } + } + //NMLOG_DEBUG("%s device path %s", iface_name, path.c_str()); + g_variant_unref(result); + return ret; + } + + bool GnomeUtils::getApDetails(GDBusConnection *connection, const char* apPath, apProperties& apDetails) + { + char *bssid = NULL; + uint8_t strength = 0; + GError* error = NULL; + GVariant* result = NULL; + + GDBusProxy* proxy = g_dbus_proxy_new_sync(connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + "org.freedesktop.NetworkManager", + apPath, + "org.freedesktop.NetworkManager.AccessPoint", + NULL, + &error); + + if (error) { + NMLOG_ERROR("creating proxy: %s", error->message); + g_error_free(error); + return false; + } + + gsize ssid_length = 0; + result = g_dbus_proxy_get_cached_property(proxy,"Ssid"); + if (!result) { + NMLOG_ERROR("Failed to get AP properties."); + g_object_unref(proxy); + return false; + } + + const guchar *ssid_data = static_cast(g_variant_get_fixed_array(result, &ssid_length, sizeof(guchar))); + if (ssid_data && ssid_length > 0 && ssid_length <= 32) + { + apDetails.ssid.assign(reinterpret_cast(ssid_data), ssid_length); + NMLOG_DEBUG("SSID: %s", apDetails.ssid.c_str()); + g_variant_unref(result); + + result = g_dbus_proxy_get_cached_property(proxy,"HwAddress"); + if (!result) { + NMLOG_ERROR("Failed to get AP properties."); + g_object_unref(proxy); + return false; + } + g_variant_get(result, "s", &bssid); + apDetails.bssid.assign(bssid); + NMLOG_DEBUG("bssid %s", apDetails.bssid.c_str()); + g_variant_unref(result); + + result = g_dbus_proxy_get_cached_property(proxy,"Strength"); + if (!result) { + NMLOG_ERROR("Failed to get AP properties."); + g_object_unref(proxy); + return false; + } + g_variant_get(result, "y", &strength); + NMLOG_DEBUG("strength %d", strength); + g_variant_unref(result); + + GnomeUtils::getCachedPropertyU(proxy, "Flags", &apDetails.flags); + GnomeUtils::getCachedPropertyU(proxy, "WpaFlags", &apDetails.wpaFlags); + GnomeUtils::getCachedPropertyU(proxy, "RsnFlags", &apDetails.rsnFlags); + GnomeUtils::getCachedPropertyU(proxy, "Mode", &apDetails.mode); + GnomeUtils::getCachedPropertyU(proxy, "Frequency", &apDetails.frequency); + } + else { + NMLOG_ERROR("Invalid SSID length: %zu (maximum is 32)", ssid_length); + } + + g_object_unref(proxy); + + return true; + } + + bool GnomeUtils::getwifiConnectionPaths(GDBusConnection *dbusConn, const char* devicePath, std::list& 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; + } + + result = g_dbus_proxy_get_cached_property(nmProxy, "AvailableConnections"); + if (!result) { + NMLOG_ERROR("Failed to get AvailableConnections property."); + g_object_unref(nmProxy); + return ret; + } + + GVariantIter* iter; + const gchar* connPath = NULL; + g_variant_get(result, "ao", &iter); + while (g_variant_iter_loop(iter, "o", &connPath)) { + if(g_strdup(connPath) != NULL && g_strcmp0(connPath, "/") != 0) + { + paths.push_back(connPath); + // NMLOG_DEBUG("AvailableConnections Path: %s", connPath); + ret = true; + } + } + + g_variant_iter_free(iter); + g_variant_unref(result); + g_object_unref(nmProxy); + return ret; + } + + bool GnomeUtils::getConnectionPaths(GDBusConnection *dbusConn, std::list& 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); + + 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", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if(listProxy == NULL) + { + if (!error) { + NMLOG_ERROR("ListConnections failed: %s", error->message); + g_error_free(error); + g_object_unref(sProxy); + return false; + } + else + NMLOG_ERROR("ListConnections proxy failed no error message"); + } + + g_variant_get(listProxy, "(^ao)", &paths); + g_variant_unref(listProxy); + + if(paths == NULL) + { + NMLOG_WARNING("no connection path available"); + g_object_unref(sProxy); + return false; + } + + for (int i = 0; paths[i]; i++) { + //NMLOG_DEBUG("Connection path: %s", paths[i]); + pathsList.push_back(paths[i]); + } + + g_object_unref(sProxy); + if(pathsList.empty()) + return false; + + return true; + } + + bool GnomeUtils::getDeviceState(GDBusConnection *dbusConn, const gchar *iface_name, NMDeviceState& state) + { + deviceProperties devProp; + if(!GnomeUtils::getDevicePropertiesByIfname(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) + { + deviceProperties devProp; + if(!GnomeUtils::getDevicePropertiesByIfname(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 + diff --git a/Tests/raspberrypi/rpiUnitTest.cpp b/Tests/raspberrypi/rpiUnitTest.cpp index 986535f0..a2bc3c00 100644 --- a/Tests/raspberrypi/rpiUnitTest.cpp +++ b/Tests/raspberrypi/rpiUnitTest.cpp @@ -35,15 +35,18 @@ int main() { // std::string psk ="123454321"; // nmClient->startWifiScanning(); - Exchange::INetworkManager::WiFiConnectTo ssidinfo = {.m_ssid = "Mi12", - .m_passphrase = "1234567890", + Exchange::INetworkManager::WiFiConnectTo ssidinfo = {.m_ssid = "HomeNet", + .m_passphrase = "rafi@123", .m_securityMode = WPEFramework::Exchange::INetworkManager::WIFI_SECURITY_WPA_PSK_AES, .m_persistSSIDInfo = true }; - //nmClient->removeKnownSSIDs(ssidinfo.m_ssid); - nmClient->wifiConnect(ssidinfo); - sleep(30); - nmClient->wifiDisconnect(); + // nmClient->wifiConnect(ssidinfo); + // sleep(10); + Exchange::INetworkManager::WiFiState state; + nmClient->getWifiState(state); + std::string ssid, signalStrength; + Exchange::INetworkManager::WiFiSignalQuality quality; + nmClient->getWiFiSignalStrength(ssid, signalStrength, quality); } NMLOG_INFO("Program completed successfully"); return 0;