diff options
Diffstat (limited to 'examples/C/glib')
| -rw-r--r-- | examples/C/glib/Makefile.am | 27 | ||||
| -rw-r--r-- | examples/C/glib/Makefile.in | 72 | ||||
| -rw-r--r-- | examples/C/glib/list-connections-libnm-glib.c | 2 | ||||
| -rw-r--r-- | examples/C/glib/monitor-nm-running-GDBus.c | 92 | ||||
| -rw-r--r-- | examples/C/glib/monitor-nm-running-dbus-glib.c | 125 | ||||
| -rw-r--r-- | examples/C/glib/monitor-nm-state-GDBus.c | 138 |
6 files changed, 447 insertions, 9 deletions
diff --git a/examples/C/glib/Makefile.am b/examples/C/glib/Makefile.am index 1063d520..428f6adc 100644 --- a/examples/C/glib/Makefile.am +++ b/examples/C/glib/Makefile.am @@ -1,6 +1,8 @@ INCLUDES = -I${top_srcdir}/libnm-util \ + -I${top_builddir}/libnm-util \ -I${top_srcdir}/libnm-glib \ - -I${top_srcdir}/include + -I${top_srcdir}/include \ + -I${top_builddir}/include AM_CPPFLAGS = \ $(DBUS_CFLAGS) \ @@ -12,7 +14,10 @@ noinst_PROGRAMS = \ get-active-connections-dbus-glib \ list-connections-dbus-glib \ list-connections-libnm-glib \ - get-ap-info-libnm-glib + get-ap-info-libnm-glib \ + monitor-nm-running-dbus-glib \ + monitor-nm-running-GDBus \ + monitor-nm-state-GDBus add_connection_dbus_glib_SOURCES = add-connection-dbus-glib.c add_connection_dbus_glib_LDADD = \ @@ -53,11 +58,27 @@ get_ap_info_libnm_glib_LDADD = \ $(DBUS_LIBS) \ $(GLIB_LIBS) +monitor_nm_running_dbus_glib_SOURCES = monitor-nm-running-dbus-glib.c +monitor_nm_running_dbus_glib_LDADD = \ + $(DBUS_LIBS) \ + $(GLIB_LIBS) + +monitor_nm_running_GDBus_SOURCES = monitor-nm-running-GDBus.c +monitor_nm_running_GDBus_LDADD = \ + $(GIO_LIBS) + +monitor_nm_state_GDBus_SOURCES = monitor-nm-state-GDBus.c +monitor_nm_state_GDBus_LDADD = \ + $(GIO_LIBS) + EXTRA_DIST = \ add-connection-dbus-glib.c \ add-connection-libnm-glib.c \ get-active-connections-dbus-glib.c \ list-connections-dbus-glib.c \ list-connections-libnm-glib.c \ - get-ap-info-libnm-glib.c + get-ap-info-libnm-glib.c \ + monitor-nm-running-dbus-glib.c \ + monitor-nm-running-GDBus.c \ + monitor-nm-state-GDBus.c diff --git a/examples/C/glib/Makefile.in b/examples/C/glib/Makefile.in index 1a220537..871da9c9 100644 --- a/examples/C/glib/Makefile.in +++ b/examples/C/glib/Makefile.in @@ -39,7 +39,10 @@ noinst_PROGRAMS = add-connection-dbus-glib$(EXEEXT) \ get-active-connections-dbus-glib$(EXEEXT) \ list-connections-dbus-glib$(EXEEXT) \ list-connections-libnm-glib$(EXEEXT) \ - get-ap-info-libnm-glib$(EXEEXT) + get-ap-info-libnm-glib$(EXEEXT) \ + monitor-nm-running-dbus-glib$(EXEEXT) \ + monitor-nm-running-GDBus$(EXEEXT) \ + monitor-nm-state-GDBus$(EXEEXT) subdir = examples/C/glib DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 @@ -108,6 +111,20 @@ list_connections_libnm_glib_DEPENDENCIES = \ $(top_builddir)/libnm-util/libnm-util.la \ $(top_builddir)/libnm-glib/libnm-glib.la $(am__DEPENDENCIES_1) \ $(am__DEPENDENCIES_1) +am_monitor_nm_running_GDBus_OBJECTS = \ + monitor-nm-running-GDBus.$(OBJEXT) +monitor_nm_running_GDBus_OBJECTS = \ + $(am_monitor_nm_running_GDBus_OBJECTS) +monitor_nm_running_GDBus_DEPENDENCIES = $(am__DEPENDENCIES_1) +am_monitor_nm_running_dbus_glib_OBJECTS = \ + monitor-nm-running-dbus-glib.$(OBJEXT) +monitor_nm_running_dbus_glib_OBJECTS = \ + $(am_monitor_nm_running_dbus_glib_OBJECTS) +monitor_nm_running_dbus_glib_DEPENDENCIES = $(am__DEPENDENCIES_1) \ + $(am__DEPENDENCIES_1) +am_monitor_nm_state_GDBus_OBJECTS = monitor-nm-state-GDBus.$(OBJEXT) +monitor_nm_state_GDBus_OBJECTS = $(am_monitor_nm_state_GDBus_OBJECTS) +monitor_nm_state_GDBus_DEPENDENCIES = $(am__DEPENDENCIES_1) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) depcomp = $(SHELL) $(top_srcdir)/build-aux/depcomp am__depfiles_maybe = depfiles @@ -139,13 +156,19 @@ SOURCES = $(add_connection_dbus_glib_SOURCES) \ $(get_active_connections_dbus_glib_SOURCES) \ $(get_ap_info_libnm_glib_SOURCES) \ $(list_connections_dbus_glib_SOURCES) \ - $(list_connections_libnm_glib_SOURCES) + $(list_connections_libnm_glib_SOURCES) \ + $(monitor_nm_running_GDBus_SOURCES) \ + $(monitor_nm_running_dbus_glib_SOURCES) \ + $(monitor_nm_state_GDBus_SOURCES) DIST_SOURCES = $(add_connection_dbus_glib_SOURCES) \ $(add_connection_libnm_glib_SOURCES) \ $(get_active_connections_dbus_glib_SOURCES) \ $(get_ap_info_libnm_glib_SOURCES) \ $(list_connections_dbus_glib_SOURCES) \ - $(list_connections_libnm_glib_SOURCES) + $(list_connections_libnm_glib_SOURCES) \ + $(monitor_nm_running_GDBus_SOURCES) \ + $(monitor_nm_running_dbus_glib_SOURCES) \ + $(monitor_nm_state_GDBus_SOURCES) ETAGS = etags CTAGS = ctags DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) @@ -194,6 +217,8 @@ GIO_LIBS = @GIO_LIBS@ GLIB_CFLAGS = @GLIB_CFLAGS@ GLIB_GENMARSHAL = @GLIB_GENMARSHAL@ GLIB_LIBS = @GLIB_LIBS@ +GLIB_MAKEFILE = @GLIB_MAKEFILE@ +GLIB_MKENUMS = @GLIB_MKENUMS@ GMODULE_CFLAGS = @GMODULE_CFLAGS@ GMODULE_LIBS = @GMODULE_LIBS@ GMSGFMT = @GMSGFMT@ @@ -248,11 +273,15 @@ LIBNL2_LIBS = @LIBNL2_LIBS@ LIBNL3_CFLAGS = @LIBNL3_CFLAGS@ LIBNL3_LIBS = @LIBNL3_LIBS@ LIBNL_CFLAGS = @LIBNL_CFLAGS@ +LIBNL_GENL3_CFLAGS = @LIBNL_GENL3_CFLAGS@ +LIBNL_GENL3_LIBS = @LIBNL_GENL3_LIBS@ LIBNL_LIBS = @LIBNL_LIBS@ LIBNL_ROUTE3_CFLAGS = @LIBNL_ROUTE3_CFLAGS@ LIBNL_ROUTE3_LIBS = @LIBNL_ROUTE3_LIBS@ LIBOBJS = @LIBOBJS@ LIBS = @LIBS@ +LIBSOUP_CFLAGS = @LIBSOUP_CFLAGS@ +LIBSOUP_LIBS = @LIBSOUP_LIBS@ LIBTOOL = @LIBTOOL@ LIPO = @LIPO@ LN_S = @LN_S@ @@ -263,6 +292,7 @@ MAINT = @MAINT@ MAKEINFO = @MAKEINFO@ MANIFEST_TOOL = @MANIFEST_TOOL@ MKDIR_P = @MKDIR_P@ +MOC = @MOC@ MSGFMT = @MSGFMT@ MSGFMT_015 = @MSGFMT_015@ MSGMERGE = @MSGMERGE@ @@ -302,6 +332,8 @@ SED = @SED@ SET_MAKE = @SET_MAKE@ SHELL = @SHELL@ STRIP = @STRIP@ +SYSTEMD_CFLAGS = @SYSTEMD_CFLAGS@ +SYSTEMD_LIBS = @SYSTEMD_LIBS@ SYSTEM_CA_PATH = @SYSTEM_CA_PATH@ UDEV_BASE_DIR = @UDEV_BASE_DIR@ USE_NLS = @USE_NLS@ @@ -366,8 +398,10 @@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ INCLUDES = -I${top_srcdir}/libnm-util \ + -I${top_builddir}/libnm-util \ -I${top_srcdir}/libnm-glib \ - -I${top_srcdir}/include + -I${top_srcdir}/include \ + -I${top_builddir}/include AM_CPPFLAGS = \ $(DBUS_CFLAGS) \ @@ -412,13 +446,29 @@ get_ap_info_libnm_glib_LDADD = \ $(DBUS_LIBS) \ $(GLIB_LIBS) +monitor_nm_running_dbus_glib_SOURCES = monitor-nm-running-dbus-glib.c +monitor_nm_running_dbus_glib_LDADD = \ + $(DBUS_LIBS) \ + $(GLIB_LIBS) + +monitor_nm_running_GDBus_SOURCES = monitor-nm-running-GDBus.c +monitor_nm_running_GDBus_LDADD = \ + $(GIO_LIBS) + +monitor_nm_state_GDBus_SOURCES = monitor-nm-state-GDBus.c +monitor_nm_state_GDBus_LDADD = \ + $(GIO_LIBS) + EXTRA_DIST = \ add-connection-dbus-glib.c \ add-connection-libnm-glib.c \ get-active-connections-dbus-glib.c \ list-connections-dbus-glib.c \ list-connections-libnm-glib.c \ - get-ap-info-libnm-glib.c + get-ap-info-libnm-glib.c \ + monitor-nm-running-dbus-glib.c \ + monitor-nm-running-GDBus.c \ + monitor-nm-state-GDBus.c all: all-am @@ -481,6 +531,15 @@ list-connections-dbus-glib$(EXEEXT): $(list_connections_dbus_glib_OBJECTS) $(lis list-connections-libnm-glib$(EXEEXT): $(list_connections_libnm_glib_OBJECTS) $(list_connections_libnm_glib_DEPENDENCIES) @rm -f list-connections-libnm-glib$(EXEEXT) $(AM_V_CCLD)$(LINK) $(list_connections_libnm_glib_OBJECTS) $(list_connections_libnm_glib_LDADD) $(LIBS) +monitor-nm-running-GDBus$(EXEEXT): $(monitor_nm_running_GDBus_OBJECTS) $(monitor_nm_running_GDBus_DEPENDENCIES) + @rm -f monitor-nm-running-GDBus$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(monitor_nm_running_GDBus_OBJECTS) $(monitor_nm_running_GDBus_LDADD) $(LIBS) +monitor-nm-running-dbus-glib$(EXEEXT): $(monitor_nm_running_dbus_glib_OBJECTS) $(monitor_nm_running_dbus_glib_DEPENDENCIES) + @rm -f monitor-nm-running-dbus-glib$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(monitor_nm_running_dbus_glib_OBJECTS) $(monitor_nm_running_dbus_glib_LDADD) $(LIBS) +monitor-nm-state-GDBus$(EXEEXT): $(monitor_nm_state_GDBus_OBJECTS) $(monitor_nm_state_GDBus_DEPENDENCIES) + @rm -f monitor-nm-state-GDBus$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(monitor_nm_state_GDBus_OBJECTS) $(monitor_nm_state_GDBus_LDADD) $(LIBS) mostlyclean-compile: -rm -f *.$(OBJEXT) @@ -494,6 +553,9 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/get-ap-info-libnm-glib.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/list-connections-dbus-glib.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/list-connections-libnm-glib.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/monitor-nm-running-GDBus.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/monitor-nm-running-dbus-glib.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/monitor-nm-state-GDBus.Po@am__quote@ .c.o: @am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.o$$||'`;\ diff --git a/examples/C/glib/list-connections-libnm-glib.c b/examples/C/glib/list-connections-libnm-glib.c index edb5ccc3..01a9f254 100644 --- a/examples/C/glib/list-connections-libnm-glib.c +++ b/examples/C/glib/list-connections-libnm-glib.c @@ -76,7 +76,7 @@ show_connection (NMConnection *data, gpointer user_data) char timestamp_real_str[64]; const char *val1, *val2, *val3, *val4, *val5; - s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION); + s_con = nm_connection_get_setting_connection (connection); if (s_con) { /* Get various info from NMSettingConnection and show it */ timestamp = nm_setting_connection_get_timestamp (s_con); diff --git a/examples/C/glib/monitor-nm-running-GDBus.c b/examples/C/glib/monitor-nm-running-GDBus.c new file mode 100644 index 00000000..42f9b758 --- /dev/null +++ b/examples/C/glib/monitor-nm-running-GDBus.c @@ -0,0 +1,92 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* vim: set ft=c ts=4 sts=4 sw=4 noexpandtab smartindent: */ +/* + * 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 2012 Red Hat, Inc. + */ + +/* + * This example monitors whether NM is running by checking if + * "org.freedesktop.NetworkManager" is owned by a process on D-Bus. + * It uses g_bus_watch_name(). + * + * See also http://developer.gnome.org/gio/stable/gio-Watching-Bus-Names.html + * + * Standalone compilation: + * gcc -Wall `pkg-config --libs --cflags glib-2.0 gio-2.0` monitor-nm-running-GDBus.c -o monitor-nm-running-GDBus + */ + +#include <gio/gio.h> + +#if GLIB_CHECK_VERSION(2,26,0) +static void +on_name_appeared (GDBusConnection *connection, + const gchar *name, + const gchar *name_owner, + gpointer user_data) +{ + g_print ("Name '%s' on the system bus is owned by %s => NM is running\n", + name, name_owner); +} + +static void +on_name_vanished (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + g_print ("Name '%s' does not exist on the system bus => NM is not running\n", name); +} +#endif + + +int +main (int argc, char *argv[]) +{ +#if GLIB_CHECK_VERSION(2,26,0) + guint watcher_id; + GMainLoop *loop; + GBusNameWatcherFlags flags; + + /* Initialize GType system */ + g_type_init (); + + g_print ("Monitor 'org.freedesktop.NetworkManager' D-Bus name\n"); + g_print ("===================================================\n"); + + flags = G_BUS_NAME_WATCHER_FLAGS_NONE; + + /* Start to watch "org.freedesktop.NetworkManager" bus name */ + watcher_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM, + "org.freedesktop.NetworkManager", + flags, + on_name_appeared, + on_name_vanished, + NULL, + NULL); + + /* Run main loop */ + loop = g_main_loop_new (NULL, FALSE); + g_main_loop_run (loop); + + /* Stop watching the name */ + g_bus_unwatch_name (watcher_id); +#else + g_print ("Sorry, you need at least GLib 2.26 for GDBus.\n"); +#endif + + return 0; +} + diff --git a/examples/C/glib/monitor-nm-running-dbus-glib.c b/examples/C/glib/monitor-nm-running-dbus-glib.c new file mode 100644 index 00000000..3290fb1a --- /dev/null +++ b/examples/C/glib/monitor-nm-running-dbus-glib.c @@ -0,0 +1,125 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* vim: set ft=c ts=4 sts=4 sw=4 noexpandtab smartindent: */ +/* + * 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 2012 Red Hat, Inc. + */ + +/* + * This example monitors whether NM is running by checking D-Bus + * NameOwnerChanged signal. + * It uses dbus-glib library. + * + * Standalone compilation: + * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1` monitor-nm-running.c -o monitor-nm-running + */ + +#include <glib.h> +#include <dbus/dbus-glib.h> +#include <dbus/dbus-glib-bindings.h> +#include <string.h> + +#define NM_DBUS_SERVICE "org.freedesktop.NetworkManager" + +static void +proxy_name_owner_changed (DBusGProxy *proxy, + const char *name, + const char *old_owner, + const char *new_owner, + gpointer user_data) +{ + gboolean *nm_running = (gboolean *) user_data; + gboolean old_good = (old_owner && strlen (old_owner)); + gboolean new_good = (new_owner && strlen (new_owner)); + gboolean new_running = FALSE; + + /* We are only interested in NetworkManager */ + if (!name || strcmp (name, NM_DBUS_SERVICE) != 0) + return; + + if (!old_good && new_good) + new_running = TRUE; + else if (old_good && !new_good) + new_running = FALSE; + + *nm_running = new_running; + + g_print ("name: '%s', old_owner: '%s', new_owner: '%s'", name, old_owner, new_owner); + g_print (" => NM is %s\n", *nm_running ? "running" : "not running"); +} + + +int +main (int argc, char *argv[]) +{ + DBusGConnection *bus; + DBusGProxy *bus_proxy; + GMainLoop *loop = NULL; + GError *err = NULL; + gboolean nm_running; + + /* Initialize GType system */ + g_type_init (); + + g_print ("Monitor 'org.freedesktop.NetworkManager' D-Bus name\n"); + g_print ("===================================================\n"); + + /* Get system bus */ + bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL); + + /* Create a D-Bus proxy to D-Bus daemon */ + bus_proxy = dbus_g_proxy_new_for_name (bus, + "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus"); + + if (!bus_proxy) { + g_message ("Error: Couldn't create D-Bus object proxy for org.freedesktop.DBus."); + dbus_g_connection_unref (bus); + return -1; + } + + /* Call NameHasOwner method to find out if NM is running. When NM runs it claims + * 'org.freedesktop.NetworkManager' service name on D-Bus */ + if (!org_freedesktop_DBus_name_has_owner (bus_proxy, NM_DBUS_SERVICE, &nm_running, &err)) { + g_message ("Error: NameHasOwner request failed: %s", + (err && err->message) ? err->message : "(unknown)"); + g_clear_error (&err); + g_object_unref (bus_proxy); + dbus_g_connection_unref (bus); + return -1; + } + g_print ("NM is %s\n", nm_running ? "running" : "not running"); + + + /* Connect to NameOwnerChanged signal to monitor NM running state */ + dbus_g_proxy_add_signal (bus_proxy, "NameOwnerChanged", + G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, + G_TYPE_INVALID); + dbus_g_proxy_connect_signal (bus_proxy, + "NameOwnerChanged", + G_CALLBACK (proxy_name_owner_changed), + &nm_running, NULL); + + loop = g_main_loop_new (NULL, FALSE); /* Create main loop */ + g_main_loop_run (loop); /* Run main loop */ + + g_object_unref (bus_proxy); + dbus_g_connection_unref (bus); + + return 0; +} + diff --git a/examples/C/glib/monitor-nm-state-GDBus.c b/examples/C/glib/monitor-nm-state-GDBus.c new file mode 100644 index 00000000..22ff65b7 --- /dev/null +++ b/examples/C/glib/monitor-nm-state-GDBus.c @@ -0,0 +1,138 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* vim: set ft=c ts=4 sts=4 sw=4 noexpandtab smartindent: */ +/* + * 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 2012 Red Hat, Inc. + */ + +/* + * This example monitors NM state via D-Bus "StateChanged" signal on + * "org.freedesktop.NetworkManager" object. + * It uses GDBus. + + * You don't need to have NetworkManager devel package installed. You can just + * grab NetworkManager.h and put it in the path. + * + * Standalone compilation: + * gcc -Wall `pkg-config --libs --cflags glib-2.0 gio-2.0 NetworkManager` monitor-nm-state-GDBus.c -o monitor-nm-state-GDBus + */ + +#include <gio/gio.h> +#include <string.h> +#include <NetworkManager.h> + +#if GLIB_CHECK_VERSION(2,26,0) +static const char * +nm_state_to_string (NMState state) +{ + switch (state) { + case NM_STATE_ASLEEP: + return "asleep"; + case NM_STATE_CONNECTING: + return "connecting"; + case NM_STATE_CONNECTED_LOCAL: + return "connected (local only)"; + case NM_STATE_CONNECTED_SITE: + return "connected (site only)"; + case NM_STATE_CONNECTED_GLOBAL: + return "connected"; + case NM_STATE_DISCONNECTING: + return "disconnecting"; + case NM_STATE_DISCONNECTED: + return "disconnected"; + case NM_STATE_UNKNOWN: + default: + return "unknown"; + } +} + +static void +on_signal (GDBusProxy *proxy, + gchar *sender_name, + gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + guint32 new_state; + + /* Print all signals */ + //gchar *parameters_str; + //parameters_str = g_variant_print (parameters, TRUE); + //g_print (" *** Received Signal: %s: %s\n", signal_name, parameters_str); + //g_free (parameters_str); + + /* We are only interested in "StateChanged" signal */ + if (strcmp (signal_name, "StateChanged") == 0) { + GVariant *tmp = g_variant_get_child_value (parameters, 0); + new_state = g_variant_get_uint32 (tmp); + g_variant_unref (tmp); + g_print ("NetworkManager state is: (%d) %s\n", new_state, nm_state_to_string ((NMState) new_state)); + } +} +#endif + + +int +main (int argc, char *argv[]) +{ +#if GLIB_CHECK_VERSION(2,26,0) + GMainLoop *loop; + GError *error = NULL; + GDBusProxyFlags flags; + GDBusProxy *proxy; + + /* Initialize GType system */ + g_type_init (); + + /* Monitor 'StateChanged' signal on 'org.freedesktop.NetworkManager' interface */ + g_print ("Monitor NetworkManager's state\n"); + g_print ("==============================\n"); + + flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START; + proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + flags, + NULL, /* GDBusInterfaceInfo */ + "org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager", + "org.freedesktop.NetworkManager", + NULL, /* GCancellable */ + &error); + + if (proxy == NULL) + { + g_printerr ("Error creating D-Bus proxy: %s\n", error->message); + g_error_free (error); + return -1; + } + + /* Connect to g-signal to receive signals from proxy (remote object) */ + g_signal_connect (proxy, + "g-signal", + G_CALLBACK (on_signal), + NULL); + + /* Run main loop */ + loop = g_main_loop_new (NULL, FALSE); + g_main_loop_run (loop); + + g_object_unref (proxy); +#else + g_print ("Sorry, you need at least GLib 2.26 for GDBus.\n"); +#endif + + return 0; +} + |