diff --git a/GnomeProxy/NetworkManagerGnomeClient.cpp b/GnomeProxy/NetworkManagerGnomeClient.cpp index 62b893e8..39967b4d 100644 --- a/GnomeProxy/NetworkManagerGnomeClient.cpp +++ b/GnomeProxy/NetworkManagerGnomeClient.cpp @@ -21,8 +21,8 @@ #include #include #include -#include - +#include +#include #include "NetworkManagerLogger.h" #include "NetworkManagerGnomeClient.h" #include "NetworkManagerGnomeUtils.h" @@ -160,7 +160,7 @@ namespace WPEFramework } for (const std::string& path : paths) { - NMLOG_VERBOSE("connection path %s", path.c_str()); + // NMLOG_VERBOSE("connection path %s", path.c_str()); fetchSSIDFromConnection(dbusConnection.getConnection(), path, ssids); } @@ -193,7 +193,6 @@ namespace WPEFramework return false; } - //TODO check active connection path and return properties wProxy = g_dbus_proxy_new_sync(dbusConnection.getConnection(), G_DBUS_PROXY_FLAGS_NONE, @@ -302,7 +301,7 @@ namespace WPEFramework return true; } - bool NetworkManagerClient::startWifiScanning(const std::string& ssid) + bool NetworkManagerClient::startWifiScanning(const std::string ssid) { GError* error = NULL; GDBusProxy* wProxy = NULL; @@ -328,21 +327,46 @@ namespace WPEFramework 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_TRACE("Last scan timestamp: %lld", timestamp); + } else { + g_warning("Unexpected variant type: %s", g_variant_get_type_string (timestampVariant)); + } + GVariant *options = NULL; - if (!ssid.empty()) { + if (!ssid.empty()) + { + // TODO fix specific ssid scanning NMLOG_INFO("staring wifi scanning .. %s", ssid.c_str()); - 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(&builder); + 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); } - - g_dbus_proxy_call_sync(wProxy, "RequestScan", g_variant_new("(a{sv})", 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); @@ -357,5 +381,140 @@ namespace WPEFramework 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 NetworkManagerClient::wifiConnect(std::string ssid, std::string password) + { + GDBusProxy *proxy; + GError *error = NULL; + GVariantBuilder connBuilder; + GVariantBuilder settingsBuilder; + char *uuid = NULL; + const char *newConPath = NULL; + GVariant *ret = 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) { + 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; + } + + /* 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 (ssid.c_str())); + 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 *)ssid.c_str(), 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(!password.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(password.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); + + ret = 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); + NMLOG_INFO("Added: %s", newConPath); + g_variant_unref (ret); + } else { + g_dbus_error_strip_remote_error (error); + NMLOG_ERROR("Error adding connection: %s", error->message); + g_clear_error (&error); + } + + return true; + } + } // WPEFramework } // Plugin diff --git a/GnomeProxy/NetworkManagerGnomeClient.h b/GnomeProxy/NetworkManagerGnomeClient.h index 40c1bdab..426fe093 100644 --- a/GnomeProxy/NetworkManagerGnomeClient.h +++ b/GnomeProxy/NetworkManagerGnomeClient.h @@ -45,7 +45,8 @@ namespace WPEFramework bool getKnownSSIDs(std::list& ssids); bool getAvailableSSIDs(std::list& ssids); bool getConnectedSSID(); - bool startWifiScanning(const std::string& ssid); + bool startWifiScanning(const std::string ssid = ""); + bool wifiConnect(std::string ssid, std::string password = ""); private: diff --git a/GnomeProxy/NetworkManagerGnomeUtils.cpp b/GnomeProxy/NetworkManagerGnomeUtils.cpp index 71f48202..8a6b1c16 100644 --- a/GnomeProxy/NetworkManagerGnomeUtils.cpp +++ b/GnomeProxy/NetworkManagerGnomeUtils.cpp @@ -81,13 +81,15 @@ namespace WPEFramework return ret; } - g_variant_get(result, "(o)", &device_path); - if(g_strdup(device_path) != NULL) + if (g_variant_is_of_type (result, (const GVariantType *) "(o)")) { - path = std::string(g_strdup(device_path)); - ret = true; + g_variant_get(result, "(o)", &device_path); + if(g_strdup(device_path) != NULL) + { + path = std::string(g_strdup(device_path)); + ret = true; + } } - //NMLOG_TRACE("%s device path %s", iface_name, path.c_str()); g_variant_unref(result); return ret; @@ -102,8 +104,10 @@ namespace WPEFramework g_object_unref(proxy); return false; } - g_variant_get(result, "u", &value); - NMLOG_TRACE("%s: %d", propertiy, value); + if (g_variant_is_of_type (result, G_VARIANT_TYPE_UINT32)) { + value = g_variant_get_uint32 (result); + NMLOG_TRACE("%s: %d", propertiy, value); + } g_variant_unref(result); return true; } @@ -234,7 +238,7 @@ namespace WPEFramework } for (int i = 0; paths[i]; i++) { - NMLOG_VERBOSE("Connection path: %s", paths[i]); + //NMLOG_VERBOSE("Connection path: %s", paths[i]); pathsList.push_back(paths[i]); } diff --git a/Tests/raspberrypi/CMakeLists.txt b/Tests/raspberrypi/CMakeLists.txt index 77552b55..e766ec4b 100644 --- a/Tests/raspberrypi/CMakeLists.txt +++ b/Tests/raspberrypi/CMakeLists.txt @@ -30,4 +30,5 @@ target_link_libraries( NmDbusClient ${GLIB_LIBRARIES} ${GIO_LIBRARIES} + uuid # TODO remove ) \ No newline at end of file diff --git a/Tests/raspberrypi/rpiUnitTest.cpp b/Tests/raspberrypi/rpiUnitTest.cpp index 5e91768c..2ee5f646 100644 --- a/Tests/raspberrypi/rpiUnitTest.cpp +++ b/Tests/raspberrypi/rpiUnitTest.cpp @@ -24,8 +24,11 @@ int main() { std::this_thread::sleep_for(std::chrono::milliseconds(1)); nmClient->getConnectedSSID(); - //std::string ssid = "Mi11Lite"; - //nmClient->startWifiScanning(ssid); + nmClient->getAvailableSSIDs(ssids); + std::string ssid = "Smrithi"; + std::string psk ="12345678"; + nmClient->startWifiScanning(); + nmClient->wifiConnect(ssid); } NMLOG_INFO("Program completed successfully"); return 0;