1    	/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2    	/*
3    	 * This program is free software; you can redistribute it and/or modify
4    	 * it under the terms of the GNU General Public License as published by
5    	 * the Free Software Foundation; either version 2 of the License, or
6    	 * (at your option) any later version.
7    	 *
8    	 * This program is distributed in the hope that it will be useful,
9    	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   	 * GNU General Public License for more details.
12   	 *
13   	 * You should have received a copy of the GNU General Public License along
14   	 * with this program; if not, write to the Free Software Foundation, Inc.,
15   	 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16   	 *
17   	 * (C) Copyright 2011 Red Hat, Inc.
18   	 */
19   	
20   	/*
21   	 * The example shows how to call AddConnection() D-Bus method to add
22   	 * a connection to system settings service. It uses dbus-glib and libnm-util
23   	 * libraries.
24   	 *
25   	 * Compile with:
26   	 *   gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util` add-connection-glib.c -o add-connection-glib
27   	 */
28   	
29   	#include <glib.h>
30   	#include <dbus/dbus-glib.h>
31   	
32   	#include <nm-connection.h>
33   	#include <nm-setting-connection.h>
34   	#include <nm-setting-wired.h>
35   	#include <nm-setting-ip4-config.h>
36   	#include <NetworkManager.h>
37   	#include <nm-utils.h>
38   	
39   	#define DBUS_TYPE_G_MAP_OF_VARIANT          (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE))
40   	#define DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT   (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, DBUS_TYPE_G_MAP_OF_VARIANT))
41   	
42   	static void
43   	add_connection (DBusGProxy *proxy, const char *con_name)
44   	{
45   		NMConnection *connection;
46   		NMSettingConnection *s_con;
47   		NMSettingWired *s_wired;
48   		NMSettingIP4Config *s_ip4;
49   		char *uuid, *new_con_path = NULL;
50   		GHashTable *hash;
51   		GError *error = NULL;
52   	
53   		/* Create a new connection object */
54   		connection = (NMConnection *) nm_connection_new ();
55   	
56   		/* Build up the 'connection' Setting */
57   		s_con = (NMSettingConnection *) nm_setting_connection_new ();
58   		uuid = nm_utils_uuid_generate ();
59   		g_object_set (G_OBJECT (s_con),
60   		              NM_SETTING_CONNECTION_UUID, uuid,
61   		              NM_SETTING_CONNECTION_ID, con_name,
62   		              NM_SETTING_CONNECTION_TYPE, "802-3-ethernet",
63   		              NULL);
64   		g_free (uuid);
65   		nm_connection_add_setting (connection, NM_SETTING (s_con));
66   	
67   		/* Build up the 'wired' Setting */
68   		s_wired = (NMSettingWired *) nm_setting_wired_new ();
69   		nm_connection_add_setting (connection, NM_SETTING (s_wired));
70   	
71   		/* Build up the 'ipv4' Setting */
72   		s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
73   		g_object_set (G_OBJECT (s_ip4),
74   		              NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
75   		              NULL);
76   		nm_connection_add_setting (connection, NM_SETTING (s_ip4));
77   	
78   		hash = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_ALL);
79   	
80   		/* Call AddConnection with the hash as argument */
(15) Event example_checked: Example2: "dbus_g_proxy_call(proxy, "AddConnection", &error, dbus_g_type_get_map("GHashTable", 64UL, dbus_g_type_get_map("GHashTable", 64UL, g_value_get_type())), hash, 0UL, dbus_g_object_path_get_g_type(), &new_con_path, 0UL)" has its value checked in "dbus_g_proxy_call(proxy, "AddConnection", &error, dbus_g_type_get_map("GHashTable", 64UL, dbus_g_type_get_map("GHashTable", 64UL, g_value_get_type())), hash, 0UL, dbus_g_object_path_get_g_type(), &new_con_path, 0UL)".
Also see events: [check_return][example_checked][example_checked][example_checked][example_checked][unchecked_value]
81   		if (!dbus_g_proxy_call (proxy, "AddConnection", &error,
82   		                        DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, hash,
83   		                        G_TYPE_INVALID,
84   		                        DBUS_TYPE_G_OBJECT_PATH, &new_con_path,
85   		                        G_TYPE_INVALID)) {
86   			g_print ("Error adding connection: %s %s",
87   			         dbus_g_error_get_name (error),
88   			         error->message);
89   			g_clear_error (&error);
90   		} else {
91   			g_print ("Added: %s\n", new_con_path);
92   			g_free (new_con_path);
93   		}
94   	
95   		g_hash_table_destroy (hash);
96   		g_object_unref (connection);
97   	}
98   	
99   	
100  	int main (int argc, char *argv[])
101  	{
102  		DBusGConnection *bus;
103  		DBusGProxy *proxy;
104  	
105  		/* Initialize GType system */
106  		g_type_init ();
107  	
108  		/* Get system bus */
109  		bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
110  	
111  		/* Create a D-Bus proxy; NM_DBUS_* defined in NetworkManager.h */
112  		proxy = dbus_g_proxy_new_for_name (bus,
113  		                                   NM_DBUS_SERVICE,
114  		                                   NM_DBUS_PATH_SETTINGS,
115  		                                   NM_DBUS_IFACE_SETTINGS);
116  	
117  		/* Add a connection */
118  		add_connection (proxy, "__Test connection__");
119  	
120  		g_object_unref (proxy);
121  		dbus_g_connection_unref (bus);
122  	
123  		return 0;
124  	}
125