/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * (C) Copyright 2011 Red Hat, Inc. */ /* * The example shows how to call AddConnection() D-Bus method to add * a connection to system settings service. It uses dbus-glib and libnm-util * libraries. * * To compile the project use cmake * $> mkdir buildTree; cd buildTree; cmake ..; make */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct ConnectionHelpers { GMainLoop *loop; NMDevice *wifi_device; NMRemoteConnection *remote; }; static void remove_cb( NMRemoteConnection * /*connection*/, GError * /*error*/, gpointer user_data ) { g_print( "\tSettings removed.\n"); ConnectionHelpers *helpers = (ConnectionHelpers *)user_data; /* Tell the mainloop we're done and we can quit now */ g_main_loop_quit (helpers->loop); } static void disconnect_cb( NMDevice * /*device*/, GError * /*error*/, gpointer user_data) { g_print( "\tConnection disconnected.\n"); ConnectionHelpers *helpers = (ConnectionHelpers *)user_data; NMRemoteConnection *remote = helpers->remote; nm_remote_connection_delete( remote, remove_cb, user_data ); } static void added_cb (NMRemoteSettings * /*settings*/, NMRemoteConnection *remote, GError *error, gpointer user_data) { g_print( "\tConnection created!\n" ); ConnectionHelpers *helpers = (ConnectionHelpers *)user_data; GMainLoop *loop = helpers->loop;//(GMainLoop *)user_data; helpers->remote = remote; /* NM responded to our request; either handle the resulting error or * print out the object path of the connection we just added. */ if (error) { g_print ("Error adding connection: %s", error->message); } else { g_print ("Added: %s\n", nm_connection_get_path (NM_CONNECTION (remote))); } DBusGConnection *bus; DBusGProxy *proxy2; /* Get system bus */ bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL); /* Create a D-Bus proxy to get the object properties from the NM Manager * object. NM_DBUS_* defines are from NetworkManager.h. */ proxy2 = dbus_g_proxy_new_for_name (bus, NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE); /* Get NMClient object */ NMClient *client; client = nm_client_new (); if (!client) { dbus_g_connection_unref (bus); g_message ("Error: Could not create NMClient."); exit(-1); } /* Get all devices managed by NetworkManager */ const GPtrArray *devices = nm_client_get_devices (client); NMDevice *wifi_device=NULL; /* Go through the array and process Wi-Fi devices */ for (unsigned int i = 0; devices && (i < devices->len); i++) { NMDevice *device = (NMDevice *)g_ptr_array_index (devices, i); if (NM_IS_DEVICE_WIFI (device)) { wifi_device = device; break; } } helpers->wifi_device = wifi_device; error = NULL; GByteArray *new_active_path=NULL; /** Needs to know how to use dbus */ if(!dbus_g_proxy_call ( proxy2, "ActivateConnection", &error, DBUS_TYPE_G_OBJECT_PATH, nm_connection_get_path (NM_CONNECTION (remote)), DBUS_TYPE_G_OBJECT_PATH, nm_object_get_path((NMObject *)wifi_device), DBUS_TYPE_G_OBJECT_PATH, "/", G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH, &new_active_path, G_TYPE_INVALID)) { g_print ("Error activating connection: %s %s", dbus_g_error_get_name (error), error->message); g_clear_error (&error); } else{ g_print( "\tConnection activated!\n" ); } g_free (new_active_path); /* Tell the mainloop we're done and we can quit now */ g_main_loop_quit (loop); } static gboolean add_connection (NMRemoteSettings *settings, ConnectionHelpers *helpers, const char *con_name) { NMConnection *connection; NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingIP4Config *s_ip4; char *uuid; GByteArray *ssid; const unsigned char ssid_data[] = { 'T', 'e', 's', 't', ' ', 'o', 'f', ' ', 'm', 'y' };//Test of my own wifi network gboolean success; /* Create a new connection object */ connection = nm_connection_new (); /* Build up the 'connection' Setting */ { s_con = (NMSettingConnection *) nm_setting_connection_new (); uuid = nm_utils_uuid_generate (); g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_ID, con_name, NM_SETTING_CONNECTION_TYPE, "802-11-wireless", NULL); g_free (uuid); nm_connection_add_setting (connection, NM_SETTING (s_con)); /* Build up the 'wireless' Setting */ s_wireless = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wireless)); ssid = g_byte_array_sized_new (sizeof (ssid_data)); g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); g_object_set(s_wireless, NM_SETTING_WIRELESS_SSID, ssid, NULL); g_object_set(s_wireless, NM_SETTING_WIRELESS_MODE, "adhoc", NULL); g_byte_array_free (ssid, TRUE); /* Build up the 'ipv4' Setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); } /* Ask the settings service to add the new connection; we'll quit the * mainloop and exit when the callback is called. */ success = nm_remote_settings_add_connection (settings, connection, added_cb, helpers); if (!success) { g_print ("Error adding connection\n"); } g_object_unref (connection); return success; } int main (int /*argc*/, char * /*argv*/[]) { DBusGConnection *bus; NMRemoteSettings *settings; GMainLoop *loop; ConnectionHelpers *helpers; g_print("Test of adhoc network handling through network-manager:\n" "create, activate, disconnect, delete.\n"); /* Initialize GType system */ g_type_init (); loop = g_main_loop_new (NULL, FALSE); helpers = new ConnectionHelpers; helpers->loop = loop; /* Get system bus */ bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL); /* Create our proxy for NetworkManager's settings service */ settings = nm_remote_settings_new (bus); /* Ask the settings service to add the new connection */ if (add_connection (settings, helpers, "__Test wireless adhoc network creation__")) { /* Wait for the connection to be added */ g_main_loop_run (loop); } else { g_print ("Error adding connection to NetworkManager\n"); } {//Waiting using namespace boost::asio; io_service io_s; deadline_timer timer(io_s); // Set an expiry time relative to now. timer.expires_from_now(boost::posix_time::seconds(3)); // Wait for the timer to expire. timer.wait(); } /* Disconnect the device then remove the settings in the callback function */ nm_device_disconnect( helpers->wifi_device, disconnect_cb, helpers ); g_main_loop_run (loop); /* Clean up */ delete helpers; g_object_unref (settings); dbus_g_connection_unref (bus); return 0; }