Skip to content

Commit

Permalink
addknow ssid
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuhammedrafi committed Oct 9, 2024
1 parent 906c900 commit aeecc91
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 25 deletions.
179 changes: 154 additions & 25 deletions GnomeProxy/NetworkManagerGnomeClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,32 +392,70 @@ namespace WPEFramework
return buf;
}

bool NetworkManagerClient::wifiConnect(std::string ssid, std::string password)
bool activateConnection(GDBusConnection *dbusConn, GVariantBuilder connBuilder, const char* devicePath, bool persist)
{
GDBusProxy *proxy;
GError *error = NULL;
GVariantBuilder connBuilder;
GVariantBuilder settingsBuilder;
char *uuid = NULL;
const char *newConPath = NULL;
GVariant *ret = NULL;
GDBusProxy* proxy = nullptr;
GError* error = nullptr;
GVariant* result = nullptr;

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);
// 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 (!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 1;
if (error) {
std::cerr << "Failed to create proxy: " << error->message << std::endl;
g_clear_error(&error);
return false;
}

const char* specific_object = "/";

GVariantBuilder optionsBuilder;
g_variant_builder_init (&optionsBuilder, G_VARIANT_TYPE ("a{sv}"));
if(persist)
g_variant_builder_add(&optionsBuilder, "{sv}", "persist", g_variant_new_string("disk"));
else
g_variant_builder_add(&optionsBuilder, "{sv}", "persist", g_variant_new_string("volatile"));

// Call AddAndActivateConnection2
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) {
std::cerr << "Failed to call AddAndActivateConnection2: " << error->message << std::endl;
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_TRACE("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(std::string ssid, std::string password, GVariantBuilder& connBuilder)
{
char *uuid = NULL;
GVariantBuilder settingsBuilder;
/* Build up the complete connection */
g_variant_builder_init (&connBuilder, G_VARIANT_TYPE ("a{sa{sv}}"));

Expand Down Expand Up @@ -497,16 +535,75 @@ namespace WPEFramework
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;

if(ssidinfo.m_ssid.empty())
{
NMLOG_WARNING("ssid name is missing");
return false;
}

ret = g_dbus_proxy_call_sync (proxy,
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;
}

ret = gVariantConnectionBuilder(ssid, password, connBuilder);
break;
}
case case Exchange::INetworkManager::WIFI_SECURITY_NONE:
NMLOG_INFO("open wifi network configuration");
ret= gVariantConnectionBuilder(ssid, password, connBuilder);
break;
default:
{
NMLOG_WARNING("connection wifi securtity type not supported");
return false;
}
}

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 (ret) {
g_variant_get (ret, "(&o)", &newConPath);

if (result) {
g_variant_get (result, "(&o)", &newConPath);
NMLOG_INFO("Added: %s", newConPath);
g_variant_unref (ret);
g_variant_unref (result);
} else {
g_dbus_error_strip_remote_error (error);
NMLOG_ERROR("Error adding connection: %s", error->message);
Expand All @@ -516,5 +613,37 @@ namespace WPEFramework
return true;
}

bool NetworkManagerClient::wifiConnect(std::string ssid, std::string password)
{
GDBusProxy *proxy;
GError *error = NULL;
GVariantBuilder connBuilder;
const char *newConPath = NULL;



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;
}

gVariantConnectionBuilder(ssid, password, connBuilder);

std::string wifiDevicePath;
if(!GnomeUtils::getDeviceByIpIface(dbusConnection.getConnection(),"wlan0", wifiDevicePath)) {
NMLOG_ERROR("no wifi device found");
return false;
}

if(activateConnection(dbusConnection.getConnection(), connBuilder, wifiDevicePath.c_str(), true))
NMLOG_INFO("wifi connect request success");
else
NMLOG_ERROR("wifi connect request Failed");

return true;
}

} // WPEFramework
} // Plugin
2 changes: 2 additions & 0 deletions GnomeProxy/NetworkManagerGnomeClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "NetworkManagerLogger.h"
#include "NetworkManagerDbusMgr.h"
#include "INetworkManager.h"

namespace WPEFramework
{
Expand All @@ -45,6 +46,7 @@ namespace WPEFramework
bool getKnownSSIDs(std::list<std::string>& ssids);
bool getAvailableSSIDs(std::list<std::string>& ssids);
bool getConnectedSSID();
bool addToKnownSSIDs(const Exchange::INetworkManager::WiFiConnectTo& ssidinfo);
bool startWifiScanning(const std::string ssid = "");
bool wifiConnect(std::string ssid, std::string password = "");

Expand Down

0 comments on commit aeecc91

Please sign in to comment.