From 2c032d8f1c6292c1338a615e6ec40252889ba85c Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Thu, 22 Jan 2015 00:29:39 +0100 Subject: Imported Upstream version 1.0.0 --- examples/C/Makefile.in | 6 +- examples/C/glib/Makefile.am | 96 +++---- examples/C/glib/Makefile.in | 282 ++++++++------------- examples/C/glib/add-connection-dbus-glib.c | 126 --------- examples/C/glib/add-connection-gdbus.c | 150 +++++++++++ examples/C/glib/add-connection-libnm-glib.c | 138 ---------- examples/C/glib/add-connection-libnm.c | 134 ++++++++++ examples/C/glib/get-active-connections-dbus-glib.c | 223 ---------------- examples/C/glib/get-active-connections-gdbus.c | 274 ++++++++++++++++++++ examples/C/glib/get-ap-info-libnm-glib.c | 233 ----------------- examples/C/glib/get-ap-info-libnm.c | 233 +++++++++++++++++ examples/C/glib/list-connections-dbus-glib.c | 91 ------- examples/C/glib/list-connections-gdbus.c | 90 +++++++ examples/C/glib/list-connections-libnm-glib.c | 178 ------------- examples/C/glib/list-connections-libnm.c | 101 ++++++++ examples/C/glib/monitor-nm-running-GDBus.c | 88 ------- examples/C/glib/monitor-nm-running-dbus-glib.c | 127 ---------- examples/C/glib/monitor-nm-running-gdbus.c | 89 +++++++ examples/C/glib/monitor-nm-state-GDBus.c | 134 ---------- examples/C/glib/monitor-nm-state-gdbus.c | 136 ++++++++++ examples/C/qt/Makefile.am | 4 +- examples/C/qt/Makefile.in | 10 +- examples/C/qt/add-connection-wired.cpp | 4 +- examples/C/qt/change-ipv4-addresses.cpp | 12 +- examples/C/qt/list-connections.cpp | 4 +- 25 files changed, 1375 insertions(+), 1588 deletions(-) delete mode 100644 examples/C/glib/add-connection-dbus-glib.c create mode 100644 examples/C/glib/add-connection-gdbus.c delete mode 100644 examples/C/glib/add-connection-libnm-glib.c create mode 100644 examples/C/glib/add-connection-libnm.c delete mode 100644 examples/C/glib/get-active-connections-dbus-glib.c create mode 100644 examples/C/glib/get-active-connections-gdbus.c delete mode 100644 examples/C/glib/get-ap-info-libnm-glib.c create mode 100644 examples/C/glib/get-ap-info-libnm.c delete mode 100644 examples/C/glib/list-connections-dbus-glib.c create mode 100644 examples/C/glib/list-connections-gdbus.c delete mode 100644 examples/C/glib/list-connections-libnm-glib.c create mode 100644 examples/C/glib/list-connections-libnm.c delete mode 100644 examples/C/glib/monitor-nm-running-GDBus.c delete mode 100644 examples/C/glib/monitor-nm-running-dbus-glib.c create mode 100644 examples/C/glib/monitor-nm-running-gdbus.c delete mode 100644 examples/C/glib/monitor-nm-state-GDBus.c create mode 100644 examples/C/glib/monitor-nm-state-gdbus.c (limited to 'examples/C') diff --git a/examples/C/Makefile.in b/examples/C/Makefile.in index 95754494..c3bdf7e6 100644 --- a/examples/C/Makefile.in +++ b/examples/C/Makefile.in @@ -190,6 +190,8 @@ AUTOCONF = @AUTOCONF@ AUTOHEADER = @AUTOHEADER@ AUTOMAKE = @AUTOMAKE@ AWK = @AWK@ +BLUEZ5_CFLAGS = @BLUEZ5_CFLAGS@ +BLUEZ5_LIBS = @BLUEZ5_LIBS@ CC = @CC@ CCDEPMODE = @CCDEPMODE@ CFLAGS = @CFLAGS@ @@ -313,6 +315,7 @@ NEWT_CFLAGS = @NEWT_CFLAGS@ NEWT_LIBS = @NEWT_LIBS@ NM = @NM@ NMEDIT = @NMEDIT@ +NM_CONFIG_DEFAULT_AUTH_POLKIT_TEXT = @NM_CONFIG_DEFAULT_AUTH_POLKIT_TEXT@ NM_MAJOR_VERSION = @NM_MAJOR_VERSION@ NM_MICRO_VERSION = @NM_MICRO_VERSION@ NM_MINOR_VERSION = @NM_MINOR_VERSION@ @@ -332,6 +335,7 @@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_URL = @PACKAGE_URL@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PERL = @PERL@ PKG_CONFIG = @PKG_CONFIG@ PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@ PKG_CONFIG_PATH = @PKG_CONFIG_PATH@ @@ -358,7 +362,7 @@ SYSTEMD_INHIBIT_LIBS = @SYSTEMD_INHIBIT_LIBS@ SYSTEMD_LOGIN_CFLAGS = @SYSTEMD_LOGIN_CFLAGS@ SYSTEMD_LOGIN_LIBS = @SYSTEMD_LOGIN_LIBS@ SYSTEM_CA_PATH = @SYSTEM_CA_PATH@ -UDEV_BASE_DIR = @UDEV_BASE_DIR@ +UDEV_DIR = @UDEV_DIR@ USE_NLS = @USE_NLS@ UUID_CFLAGS = @UUID_CFLAGS@ UUID_LIBS = @UUID_LIBS@ diff --git a/examples/C/glib/Makefile.am b/examples/C/glib/Makefile.am index 1f4785da..d8e5d285 100644 --- a/examples/C/glib/Makefile.am +++ b/examples/C/glib/Makefile.am @@ -1,83 +1,53 @@ AM_CPPFLAGS = \ - -I${top_srcdir}/libnm-util \ - -I${top_builddir}/libnm-util \ - -I${top_srcdir}/libnm-glib \ -I${top_srcdir}/include \ - -I${top_builddir}/include \ - $(DBUS_CFLAGS) \ + -I${top_srcdir}/libnm-core \ + -I${top_builddir}/libnm-core \ + -I${top_srcdir}/libnm \ + -I${top_builddir}/libnm \ $(GLIB_CFLAGS) noinst_PROGRAMS = \ - add-connection-dbus-glib \ - add-connection-libnm-glib \ - get-active-connections-dbus-glib \ - list-connections-dbus-glib \ - list-connections-libnm-glib \ - get-ap-info-libnm-glib \ - monitor-nm-running-dbus-glib \ - monitor-nm-running-GDBus \ - monitor-nm-state-GDBus + add-connection-gdbus \ + add-connection-libnm \ + get-active-connections-gdbus \ + get-ap-info-libnm \ + list-connections-gdbus \ + list-connections-libnm \ + monitor-nm-running-gdbus \ + monitor-nm-state-gdbus -add_connection_dbus_glib_SOURCES = add-connection-dbus-glib.c -add_connection_dbus_glib_LDADD = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(DBUS_LIBS) \ - $(GLIB_LIBS) - -add_connection_libnm_glib_SOURCES = add-connection-libnm-glib.c -add_connection_libnm_glib_LDADD = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(top_builddir)/libnm-glib/libnm-glib.la \ - $(DBUS_LIBS) \ - $(GLIB_LIBS) +add_connection_gdbus_SOURCES = add-connection-gdbus.c +add_connection_gdbus_LDADD = \ + $(GLIB_LIBS) \ + $(UUID_LIBS) -get_active_connections_dbus_glib_SOURCES = get-active-connections-dbus-glib.c -get_active_connections_dbus_glib_LDADD = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(DBUS_LIBS) \ +add_connection_libnm_SOURCES = add-connection-libnm.c +add_connection_libnm_LDADD = \ + $(top_builddir)/libnm/libnm.la \ $(GLIB_LIBS) -list_connections_dbus_glib_SOURCES = list-connections-dbus-glib.c -list_connections_dbus_glib_LDADD = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(DBUS_LIBS) \ +get_active_connections_gdbus_SOURCES = get-active-connections-gdbus.c +get_active_connections_gdbus_LDADD = \ $(GLIB_LIBS) -list_connections_libnm_glib_SOURCES = list-connections-libnm-glib.c -list_connections_libnm_glib_LDADD = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(top_builddir)/libnm-glib/libnm-glib.la \ - $(DBUS_LIBS) \ +get_ap_info_libnm_SOURCES = get-ap-info-libnm.c +get_ap_info_libnm_LDADD = \ + $(top_builddir)/libnm/libnm.la \ $(GLIB_LIBS) -get_ap_info_libnm_glib_SOURCES = get-ap-info-libnm-glib.c -get_ap_info_libnm_glib_LDADD = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(top_builddir)/libnm-glib/libnm-glib.la \ - $(DBUS_LIBS) \ +list_connections_gdbus_SOURCES = list-connections-gdbus.c +list_connections_gdbus_LDADD = \ $(GLIB_LIBS) -monitor_nm_running_dbus_glib_SOURCES = monitor-nm-running-dbus-glib.c -monitor_nm_running_dbus_glib_LDADD = \ - $(DBUS_LIBS) \ +list_connections_libnm_SOURCES = list-connections-libnm.c +list_connections_libnm_LDADD = \ + $(top_builddir)/libnm/libnm.la \ $(GLIB_LIBS) -monitor_nm_running_GDBus_SOURCES = monitor-nm-running-GDBus.c -monitor_nm_running_GDBus_LDADD = \ +monitor_nm_running_gdbus_SOURCES = monitor-nm-running-gdbus.c +monitor_nm_running_gdbus_LDADD = \ $(GLIB_LIBS) -monitor_nm_state_GDBus_SOURCES = monitor-nm-state-GDBus.c -monitor_nm_state_GDBus_LDADD = \ +monitor_nm_state_gdbus_SOURCES = monitor-nm-state-gdbus.c +monitor_nm_state_gdbus_LDADD = \ $(GLIB_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 \ - 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 e58e66d0..d2790a7a 100644 --- a/examples/C/glib/Makefile.in +++ b/examples/C/glib/Makefile.in @@ -78,15 +78,13 @@ PRE_UNINSTALL = : POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ -noinst_PROGRAMS = add-connection-dbus-glib$(EXEEXT) \ - add-connection-libnm-glib$(EXEEXT) \ - get-active-connections-dbus-glib$(EXEEXT) \ - list-connections-dbus-glib$(EXEEXT) \ - list-connections-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) +noinst_PROGRAMS = add-connection-gdbus$(EXEEXT) \ + add-connection-libnm$(EXEEXT) \ + get-active-connections-gdbus$(EXEEXT) \ + get-ap-info-libnm$(EXEEXT) list-connections-gdbus$(EXEEXT) \ + list-connections-libnm$(EXEEXT) \ + monitor-nm-running-gdbus$(EXEEXT) \ + monitor-nm-state-gdbus$(EXEEXT) subdir = examples/C/glib DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/Makefile.am \ $(top_srcdir)/build-aux/depcomp @@ -111,68 +109,43 @@ CONFIG_HEADER = $(top_builddir)/config.h CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = PROGRAMS = $(noinst_PROGRAMS) -am_add_connection_dbus_glib_OBJECTS = \ - add-connection-dbus-glib.$(OBJEXT) -add_connection_dbus_glib_OBJECTS = \ - $(am_add_connection_dbus_glib_OBJECTS) +am_add_connection_gdbus_OBJECTS = add-connection-gdbus.$(OBJEXT) +add_connection_gdbus_OBJECTS = $(am_add_connection_gdbus_OBJECTS) am__DEPENDENCIES_1 = -add_connection_dbus_glib_DEPENDENCIES = \ - $(top_builddir)/libnm-util/libnm-util.la $(am__DEPENDENCIES_1) \ +add_connection_gdbus_DEPENDENCIES = $(am__DEPENDENCIES_1) \ $(am__DEPENDENCIES_1) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) am__v_lt_0 = --silent am__v_lt_1 = -am_add_connection_libnm_glib_OBJECTS = \ - add-connection-libnm-glib.$(OBJEXT) -add_connection_libnm_glib_OBJECTS = \ - $(am_add_connection_libnm_glib_OBJECTS) -add_connection_libnm_glib_DEPENDENCIES = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(top_builddir)/libnm-glib/libnm-glib.la $(am__DEPENDENCIES_1) \ +am_add_connection_libnm_OBJECTS = add-connection-libnm.$(OBJEXT) +add_connection_libnm_OBJECTS = $(am_add_connection_libnm_OBJECTS) +add_connection_libnm_DEPENDENCIES = $(top_builddir)/libnm/libnm.la \ $(am__DEPENDENCIES_1) -am_get_active_connections_dbus_glib_OBJECTS = \ - get-active-connections-dbus-glib.$(OBJEXT) -get_active_connections_dbus_glib_OBJECTS = \ - $(am_get_active_connections_dbus_glib_OBJECTS) -get_active_connections_dbus_glib_DEPENDENCIES = \ - $(top_builddir)/libnm-util/libnm-util.la $(am__DEPENDENCIES_1) \ +am_get_active_connections_gdbus_OBJECTS = \ + get-active-connections-gdbus.$(OBJEXT) +get_active_connections_gdbus_OBJECTS = \ + $(am_get_active_connections_gdbus_OBJECTS) +get_active_connections_gdbus_DEPENDENCIES = $(am__DEPENDENCIES_1) +am_get_ap_info_libnm_OBJECTS = get-ap-info-libnm.$(OBJEXT) +get_ap_info_libnm_OBJECTS = $(am_get_ap_info_libnm_OBJECTS) +get_ap_info_libnm_DEPENDENCIES = $(top_builddir)/libnm/libnm.la \ $(am__DEPENDENCIES_1) -am_get_ap_info_libnm_glib_OBJECTS = get-ap-info-libnm-glib.$(OBJEXT) -get_ap_info_libnm_glib_OBJECTS = $(am_get_ap_info_libnm_glib_OBJECTS) -get_ap_info_libnm_glib_DEPENDENCIES = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(top_builddir)/libnm-glib/libnm-glib.la $(am__DEPENDENCIES_1) \ +am_list_connections_gdbus_OBJECTS = list-connections-gdbus.$(OBJEXT) +list_connections_gdbus_OBJECTS = $(am_list_connections_gdbus_OBJECTS) +list_connections_gdbus_DEPENDENCIES = $(am__DEPENDENCIES_1) +am_list_connections_libnm_OBJECTS = list-connections-libnm.$(OBJEXT) +list_connections_libnm_OBJECTS = $(am_list_connections_libnm_OBJECTS) +list_connections_libnm_DEPENDENCIES = $(top_builddir)/libnm/libnm.la \ $(am__DEPENDENCIES_1) -am_list_connections_dbus_glib_OBJECTS = \ - list-connections-dbus-glib.$(OBJEXT) -list_connections_dbus_glib_OBJECTS = \ - $(am_list_connections_dbus_glib_OBJECTS) -list_connections_dbus_glib_DEPENDENCIES = \ - $(top_builddir)/libnm-util/libnm-util.la $(am__DEPENDENCIES_1) \ - $(am__DEPENDENCIES_1) -am_list_connections_libnm_glib_OBJECTS = \ - list-connections-libnm-glib.$(OBJEXT) -list_connections_libnm_glib_OBJECTS = \ - $(am_list_connections_libnm_glib_OBJECTS) -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) +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_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) AM_V_P = $(am__v_P_@AM_V@) am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) am__v_P_0 = false @@ -207,24 +180,20 @@ AM_V_CCLD = $(am__v_CCLD_@AM_V@) am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) am__v_CCLD_0 = @echo " CCLD " $@; am__v_CCLD_1 = -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) \ - $(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) \ - $(monitor_nm_running_GDBus_SOURCES) \ - $(monitor_nm_running_dbus_glib_SOURCES) \ - $(monitor_nm_state_GDBus_SOURCES) +SOURCES = $(add_connection_gdbus_SOURCES) \ + $(add_connection_libnm_SOURCES) \ + $(get_active_connections_gdbus_SOURCES) \ + $(get_ap_info_libnm_SOURCES) $(list_connections_gdbus_SOURCES) \ + $(list_connections_libnm_SOURCES) \ + $(monitor_nm_running_gdbus_SOURCES) \ + $(monitor_nm_state_gdbus_SOURCES) +DIST_SOURCES = $(add_connection_gdbus_SOURCES) \ + $(add_connection_libnm_SOURCES) \ + $(get_active_connections_gdbus_SOURCES) \ + $(get_ap_info_libnm_SOURCES) $(list_connections_gdbus_SOURCES) \ + $(list_connections_libnm_SOURCES) \ + $(monitor_nm_running_gdbus_SOURCES) \ + $(monitor_nm_state_gdbus_SOURCES) am__can_run_installinfo = \ case $$AM_UPDATE_INFO_DIR in \ n|no|NO) false;; \ @@ -259,6 +228,8 @@ AUTOCONF = @AUTOCONF@ AUTOHEADER = @AUTOHEADER@ AUTOMAKE = @AUTOMAKE@ AWK = @AWK@ +BLUEZ5_CFLAGS = @BLUEZ5_CFLAGS@ +BLUEZ5_LIBS = @BLUEZ5_LIBS@ CC = @CC@ CCDEPMODE = @CCDEPMODE@ CFLAGS = @CFLAGS@ @@ -382,6 +353,7 @@ NEWT_CFLAGS = @NEWT_CFLAGS@ NEWT_LIBS = @NEWT_LIBS@ NM = @NM@ NMEDIT = @NMEDIT@ +NM_CONFIG_DEFAULT_AUTH_POLKIT_TEXT = @NM_CONFIG_DEFAULT_AUTH_POLKIT_TEXT@ NM_MAJOR_VERSION = @NM_MAJOR_VERSION@ NM_MICRO_VERSION = @NM_MICRO_VERSION@ NM_MINOR_VERSION = @NM_MINOR_VERSION@ @@ -401,6 +373,7 @@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_URL = @PACKAGE_URL@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PERL = @PERL@ PKG_CONFIG = @PKG_CONFIG@ PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@ PKG_CONFIG_PATH = @PKG_CONFIG_PATH@ @@ -427,7 +400,7 @@ SYSTEMD_INHIBIT_LIBS = @SYSTEMD_INHIBIT_LIBS@ SYSTEMD_LOGIN_CFLAGS = @SYSTEMD_LOGIN_CFLAGS@ SYSTEMD_LOGIN_LIBS = @SYSTEMD_LOGIN_LIBS@ SYSTEM_CA_PATH = @SYSTEM_CA_PATH@ -UDEV_BASE_DIR = @UDEV_BASE_DIR@ +UDEV_DIR = @UDEV_DIR@ USE_NLS = @USE_NLS@ UUID_CFLAGS = @UUID_CFLAGS@ UUID_LIBS = @UUID_LIBS@ @@ -507,77 +480,49 @@ with_netconfig = @with_netconfig@ with_resolvconf = @with_resolvconf@ with_valgrind = @with_valgrind@ AM_CPPFLAGS = \ - -I${top_srcdir}/libnm-util \ - -I${top_builddir}/libnm-util \ - -I${top_srcdir}/libnm-glib \ -I${top_srcdir}/include \ - -I${top_builddir}/include \ - $(DBUS_CFLAGS) \ + -I${top_srcdir}/libnm-core \ + -I${top_builddir}/libnm-core \ + -I${top_srcdir}/libnm \ + -I${top_builddir}/libnm \ $(GLIB_CFLAGS) -add_connection_dbus_glib_SOURCES = add-connection-dbus-glib.c -add_connection_dbus_glib_LDADD = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(DBUS_LIBS) \ - $(GLIB_LIBS) - -add_connection_libnm_glib_SOURCES = add-connection-libnm-glib.c -add_connection_libnm_glib_LDADD = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(top_builddir)/libnm-glib/libnm-glib.la \ - $(DBUS_LIBS) \ - $(GLIB_LIBS) +add_connection_gdbus_SOURCES = add-connection-gdbus.c +add_connection_gdbus_LDADD = \ + $(GLIB_LIBS) \ + $(UUID_LIBS) -get_active_connections_dbus_glib_SOURCES = get-active-connections-dbus-glib.c -get_active_connections_dbus_glib_LDADD = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(DBUS_LIBS) \ +add_connection_libnm_SOURCES = add-connection-libnm.c +add_connection_libnm_LDADD = \ + $(top_builddir)/libnm/libnm.la \ $(GLIB_LIBS) -list_connections_dbus_glib_SOURCES = list-connections-dbus-glib.c -list_connections_dbus_glib_LDADD = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(DBUS_LIBS) \ +get_active_connections_gdbus_SOURCES = get-active-connections-gdbus.c +get_active_connections_gdbus_LDADD = \ $(GLIB_LIBS) -list_connections_libnm_glib_SOURCES = list-connections-libnm-glib.c -list_connections_libnm_glib_LDADD = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(top_builddir)/libnm-glib/libnm-glib.la \ - $(DBUS_LIBS) \ +get_ap_info_libnm_SOURCES = get-ap-info-libnm.c +get_ap_info_libnm_LDADD = \ + $(top_builddir)/libnm/libnm.la \ $(GLIB_LIBS) -get_ap_info_libnm_glib_SOURCES = get-ap-info-libnm-glib.c -get_ap_info_libnm_glib_LDADD = \ - $(top_builddir)/libnm-util/libnm-util.la \ - $(top_builddir)/libnm-glib/libnm-glib.la \ - $(DBUS_LIBS) \ +list_connections_gdbus_SOURCES = list-connections-gdbus.c +list_connections_gdbus_LDADD = \ $(GLIB_LIBS) -monitor_nm_running_dbus_glib_SOURCES = monitor-nm-running-dbus-glib.c -monitor_nm_running_dbus_glib_LDADD = \ - $(DBUS_LIBS) \ +list_connections_libnm_SOURCES = list-connections-libnm.c +list_connections_libnm_LDADD = \ + $(top_builddir)/libnm/libnm.la \ $(GLIB_LIBS) -monitor_nm_running_GDBus_SOURCES = monitor-nm-running-GDBus.c -monitor_nm_running_GDBus_LDADD = \ +monitor_nm_running_gdbus_SOURCES = monitor-nm-running-gdbus.c +monitor_nm_running_gdbus_LDADD = \ $(GLIB_LIBS) -monitor_nm_state_GDBus_SOURCES = monitor-nm-state-GDBus.c -monitor_nm_state_GDBus_LDADD = \ +monitor_nm_state_gdbus_SOURCES = monitor-nm-state-gdbus.c +monitor_nm_state_gdbus_LDADD = \ $(GLIB_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 \ - monitor-nm-running-dbus-glib.c \ - monitor-nm-running-GDBus.c \ - monitor-nm-state-GDBus.c - all: all-am .SUFFIXES: @@ -622,41 +567,37 @@ clean-noinstPROGRAMS: echo " rm -f" $$list; \ rm -f $$list -add-connection-dbus-glib$(EXEEXT): $(add_connection_dbus_glib_OBJECTS) $(add_connection_dbus_glib_DEPENDENCIES) $(EXTRA_add_connection_dbus_glib_DEPENDENCIES) - @rm -f add-connection-dbus-glib$(EXEEXT) - $(AM_V_CCLD)$(LINK) $(add_connection_dbus_glib_OBJECTS) $(add_connection_dbus_glib_LDADD) $(LIBS) - -add-connection-libnm-glib$(EXEEXT): $(add_connection_libnm_glib_OBJECTS) $(add_connection_libnm_glib_DEPENDENCIES) $(EXTRA_add_connection_libnm_glib_DEPENDENCIES) - @rm -f add-connection-libnm-glib$(EXEEXT) - $(AM_V_CCLD)$(LINK) $(add_connection_libnm_glib_OBJECTS) $(add_connection_libnm_glib_LDADD) $(LIBS) +add-connection-gdbus$(EXEEXT): $(add_connection_gdbus_OBJECTS) $(add_connection_gdbus_DEPENDENCIES) $(EXTRA_add_connection_gdbus_DEPENDENCIES) + @rm -f add-connection-gdbus$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(add_connection_gdbus_OBJECTS) $(add_connection_gdbus_LDADD) $(LIBS) -get-active-connections-dbus-glib$(EXEEXT): $(get_active_connections_dbus_glib_OBJECTS) $(get_active_connections_dbus_glib_DEPENDENCIES) $(EXTRA_get_active_connections_dbus_glib_DEPENDENCIES) - @rm -f get-active-connections-dbus-glib$(EXEEXT) - $(AM_V_CCLD)$(LINK) $(get_active_connections_dbus_glib_OBJECTS) $(get_active_connections_dbus_glib_LDADD) $(LIBS) +add-connection-libnm$(EXEEXT): $(add_connection_libnm_OBJECTS) $(add_connection_libnm_DEPENDENCIES) $(EXTRA_add_connection_libnm_DEPENDENCIES) + @rm -f add-connection-libnm$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(add_connection_libnm_OBJECTS) $(add_connection_libnm_LDADD) $(LIBS) -get-ap-info-libnm-glib$(EXEEXT): $(get_ap_info_libnm_glib_OBJECTS) $(get_ap_info_libnm_glib_DEPENDENCIES) $(EXTRA_get_ap_info_libnm_glib_DEPENDENCIES) - @rm -f get-ap-info-libnm-glib$(EXEEXT) - $(AM_V_CCLD)$(LINK) $(get_ap_info_libnm_glib_OBJECTS) $(get_ap_info_libnm_glib_LDADD) $(LIBS) +get-active-connections-gdbus$(EXEEXT): $(get_active_connections_gdbus_OBJECTS) $(get_active_connections_gdbus_DEPENDENCIES) $(EXTRA_get_active_connections_gdbus_DEPENDENCIES) + @rm -f get-active-connections-gdbus$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(get_active_connections_gdbus_OBJECTS) $(get_active_connections_gdbus_LDADD) $(LIBS) -list-connections-dbus-glib$(EXEEXT): $(list_connections_dbus_glib_OBJECTS) $(list_connections_dbus_glib_DEPENDENCIES) $(EXTRA_list_connections_dbus_glib_DEPENDENCIES) - @rm -f list-connections-dbus-glib$(EXEEXT) - $(AM_V_CCLD)$(LINK) $(list_connections_dbus_glib_OBJECTS) $(list_connections_dbus_glib_LDADD) $(LIBS) +get-ap-info-libnm$(EXEEXT): $(get_ap_info_libnm_OBJECTS) $(get_ap_info_libnm_DEPENDENCIES) $(EXTRA_get_ap_info_libnm_DEPENDENCIES) + @rm -f get-ap-info-libnm$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(get_ap_info_libnm_OBJECTS) $(get_ap_info_libnm_LDADD) $(LIBS) -list-connections-libnm-glib$(EXEEXT): $(list_connections_libnm_glib_OBJECTS) $(list_connections_libnm_glib_DEPENDENCIES) $(EXTRA_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) +list-connections-gdbus$(EXEEXT): $(list_connections_gdbus_OBJECTS) $(list_connections_gdbus_DEPENDENCIES) $(EXTRA_list_connections_gdbus_DEPENDENCIES) + @rm -f list-connections-gdbus$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(list_connections_gdbus_OBJECTS) $(list_connections_gdbus_LDADD) $(LIBS) -monitor-nm-running-GDBus$(EXEEXT): $(monitor_nm_running_GDBus_OBJECTS) $(monitor_nm_running_GDBus_DEPENDENCIES) $(EXTRA_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) +list-connections-libnm$(EXEEXT): $(list_connections_libnm_OBJECTS) $(list_connections_libnm_DEPENDENCIES) $(EXTRA_list_connections_libnm_DEPENDENCIES) + @rm -f list-connections-libnm$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(list_connections_libnm_OBJECTS) $(list_connections_libnm_LDADD) $(LIBS) -monitor-nm-running-dbus-glib$(EXEEXT): $(monitor_nm_running_dbus_glib_OBJECTS) $(monitor_nm_running_dbus_glib_DEPENDENCIES) $(EXTRA_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-running-gdbus$(EXEEXT): $(monitor_nm_running_gdbus_OBJECTS) $(monitor_nm_running_gdbus_DEPENDENCIES) $(EXTRA_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-state-GDBus$(EXEEXT): $(monitor_nm_state_GDBus_OBJECTS) $(monitor_nm_state_GDBus_DEPENDENCIES) $(EXTRA_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) +monitor-nm-state-gdbus$(EXEEXT): $(monitor_nm_state_gdbus_OBJECTS) $(monitor_nm_state_gdbus_DEPENDENCIES) $(EXTRA_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) @@ -664,15 +605,14 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/add-connection-dbus-glib.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/add-connection-libnm-glib.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/get-active-connections-dbus-glib.Po@am__quote@ -@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@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/add-connection-gdbus.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/add-connection-libnm.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/get-active-connections-gdbus.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/get-ap-info-libnm.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/list-connections-gdbus.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/list-connections-libnm.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-state-gdbus.Po@am__quote@ .c.o: @am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/examples/C/glib/add-connection-dbus-glib.c b/examples/C/glib/add-connection-dbus-glib.c deleted file mode 100644 index 354dcb75..00000000 --- a/examples/C/glib/add-connection-dbus-glib.c +++ /dev/null @@ -1,126 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* - * 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. - * - * Compile with: - * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util` add-connection-glib.c -o add-connection-glib - */ - -#include -#include - -#include -#include -#include -#include -#include -#include - -#define DBUS_TYPE_G_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)) -#define DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, DBUS_TYPE_G_MAP_OF_VARIANT)) - -static void -add_connection (DBusGProxy *proxy, const char *con_name) -{ - NMConnection *connection; - NMSettingConnection *s_con; - NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - char *uuid, *new_con_path = NULL; - GHashTable *hash; - GError *error = NULL; - - /* Create a new connection object */ - connection = (NMConnection *) 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-3-ethernet", - NULL); - g_free (uuid); - nm_connection_add_setting (connection, NM_SETTING (s_con)); - - /* Build up the 'wired' Setting */ - s_wired = (NMSettingWired *) nm_setting_wired_new (); - nm_connection_add_setting (connection, NM_SETTING (s_wired)); - - /* 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)); - - hash = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_ALL); - - /* Call AddConnection with the hash as argument */ - if (!dbus_g_proxy_call (proxy, "AddConnection", &error, - DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, hash, - G_TYPE_INVALID, - DBUS_TYPE_G_OBJECT_PATH, &new_con_path, - G_TYPE_INVALID)) { - g_print ("Error adding connection: %s %s", - dbus_g_error_get_name (error), - error->message); - g_clear_error (&error); - } else { - g_print ("Added: %s\n", new_con_path); - g_free (new_con_path); - } - - g_hash_table_destroy (hash); - g_object_unref (connection); -} - - -int main (int argc, char *argv[]) -{ - DBusGConnection *bus; - DBusGProxy *proxy; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - /* Get system bus */ - bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL); - - /* Create a D-Bus proxy; NM_DBUS_* defined in NetworkManager.h */ - proxy = dbus_g_proxy_new_for_name (bus, - NM_DBUS_SERVICE, - NM_DBUS_PATH_SETTINGS, - NM_DBUS_IFACE_SETTINGS); - - /* Add a connection */ - add_connection (proxy, "__Test connection__"); - - g_object_unref (proxy); - dbus_g_connection_unref (bus); - - return 0; -} diff --git a/examples/C/glib/add-connection-gdbus.c b/examples/C/glib/add-connection-gdbus.c new file mode 100644 index 00000000..d3903273 --- /dev/null +++ b/examples/C/glib/add-connection-gdbus.c @@ -0,0 +1,150 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * 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. + * + * Copyright 2011, 2014 Red Hat, Inc. + */ + +/* + * The example shows how to call the AddConnection() D-Bus method to add a + * connection to the system settings service. It uses GDBus, plus a few defines + * from the NetworkManager headers. Contrast add-connection-libnm, which is + * higher level because it uses libnm. + * + * Compile with: + * gcc -Wall `pkg-config --cflags libnm` `pkg-config --cflags --libs gio-2.0` -luuid add-connection-gdbus.c -o add-connection-gdbus + */ + +#include "config.h" + +#include +#include +#include + +/* copied from libnm-core/nm-utils.c */ +char * +nm_utils_uuid_generate (void) +{ + uuid_t uuid; + char *buf; + + buf = g_malloc0 (37); + uuid_generate_random (uuid); + uuid_unparse_lower (uuid, &buf[0]); + return buf; +} + +static void +add_connection (GDBusProxy *proxy, const char *con_name) +{ + GVariantBuilder connection_builder; + GVariantBuilder setting_builder; + char *uuid; + const char *new_con_path; + GVariant *ret; + GError *error = NULL; + + /* Initialize connection GVariantBuilder */ + g_variant_builder_init (&connection_builder, G_VARIANT_TYPE ("a{sa{sv}}")); + + /* Build up the 'connection' Setting */ + g_variant_builder_init (&setting_builder, G_VARIANT_TYPE ("a{sv}")); + + uuid = nm_utils_uuid_generate (); + g_variant_builder_add (&setting_builder, "{sv}", + NM_SETTING_CONNECTION_UUID, + g_variant_new_string (uuid)); + g_free (uuid); + + g_variant_builder_add (&setting_builder, "{sv}", + NM_SETTING_CONNECTION_ID, + g_variant_new_string (con_name)); + g_variant_builder_add (&setting_builder, "{sv}", + NM_SETTING_CONNECTION_TYPE, + g_variant_new_string (NM_SETTING_WIRED_SETTING_NAME)); + + g_variant_builder_add (&connection_builder, "{sa{sv}}", + NM_SETTING_CONNECTION_SETTING_NAME, + &setting_builder); + + /* Add the (empty) 'wired' Setting */ + g_variant_builder_init (&setting_builder, G_VARIANT_TYPE ("a{sv}")); + g_variant_builder_add (&connection_builder, "{sa{sv}}", + NM_SETTING_WIRED_SETTING_NAME, + &setting_builder); + + /* Build up the 'ipv4' Setting */ + g_variant_builder_init (&setting_builder, G_VARIANT_TYPE ("a{sv}")); + g_variant_builder_add (&setting_builder, "{sv}", + NM_SETTING_IP_CONFIG_METHOD, + g_variant_new_string (NM_SETTING_IP4_CONFIG_METHOD_AUTO)); + g_variant_builder_add (&connection_builder, "{sa{sv}}", + NM_SETTING_IP4_CONFIG_SETTING_NAME, + &setting_builder); + + /* Call AddConnection with the connection dictionary as argument. + * (g_variant_new() will consume the floating GVariant returned from + * &connection_builder, and g_dbus_proxy_call_sync() will consume the + * floating variant returned from g_variant_new(), so no cleanup is needed. + */ + ret = g_dbus_proxy_call_sync (proxy, + "AddConnection", + g_variant_new ("(a{sa{sv}})", &connection_builder), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + if (ret) { + g_variant_get (ret, "(&o)", &new_con_path); + g_print ("Added: %s\n", new_con_path); + g_variant_unref (ret); + } else { + g_dbus_error_strip_remote_error (error); + g_print ("Error adding connection: %s\n", error->message); + g_clear_error (&error); + } +} + +int +main (int argc, char *argv[]) +{ + GDBusProxy *proxy; + GError *error = NULL; + +#if !GLIB_CHECK_VERSION (2, 35, 0) + /* Initialize GType system */ + g_type_init (); +#endif + + /* Create a D-Bus proxy; NM_DBUS_* defined in nm-dbus-interface.h */ + proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + NM_DBUS_SERVICE, + NM_DBUS_PATH_SETTINGS, + NM_DBUS_INTERFACE_SETTINGS, + NULL, &error); + if (!proxy) { + g_dbus_error_strip_remote_error (error); + g_print ("Could not create NetworkManager D-Bus proxy: %s\n", error->message); + g_error_free (error); + return 1; + } + + /* Add a connection */ + add_connection (proxy, "__Test connection__"); + + g_object_unref (proxy); + + return 0; +} diff --git a/examples/C/glib/add-connection-libnm-glib.c b/examples/C/glib/add-connection-libnm-glib.c deleted file mode 100644 index 28eadf8d..00000000 --- a/examples/C/glib/add-connection-libnm-glib.c +++ /dev/null @@ -1,138 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* - * 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 add a new connection using libnm-glib and libnm-util. - * Contrast this example with add-connection-dbus-glib.c, which is a bit lower - * level and talks directly to NM using dbus-glib. This example is simpler - * because libnm-glib handles much of the low-level stuff for you. - * - * Compile with: - * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util` add-connection-libnm-glib.c -o add-connection-libnm-glib - */ - -#include -#include - -#include -#include -#include -#include -#include - -static void -added_cb (NMRemoteSettings *settings, - NMRemoteConnection *remote, - GError *error, - gpointer user_data) -{ - GMainLoop *loop = user_data; - - /* 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))); - - /* Tell the mainloop we're done and we can quit now */ - g_main_loop_quit (loop); -} - -static gboolean -add_connection (NMRemoteSettings *settings, GMainLoop *loop, const char *con_name) -{ - NMConnection *connection; - NMSettingConnection *s_con; - NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - char *uuid; - 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-3-ethernet", - NULL); - g_free (uuid); - nm_connection_add_setting (connection, NM_SETTING (s_con)); - - /* Build up the 'wired' Setting */ - s_wired = (NMSettingWired *) nm_setting_wired_new (); - nm_connection_add_setting (connection, NM_SETTING (s_wired)); - - /* 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, loop); - 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; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - loop = g_main_loop_new (NULL, FALSE); - - /* 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, loop, "__Test connection__")) { - /* Wait for the connection to be added */ - g_main_loop_run (loop); - } else - g_print ("Error adding connection to NetworkManager\n"); - - /* Clean up */ - g_object_unref (settings); - dbus_g_connection_unref (bus); - - return 0; -} diff --git a/examples/C/glib/add-connection-libnm.c b/examples/C/glib/add-connection-libnm.c new file mode 100644 index 00000000..75048bd8 --- /dev/null +++ b/examples/C/glib/add-connection-libnm.c @@ -0,0 +1,134 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * 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. + * + * Copyright 2011 Red Hat, Inc. + */ + +/* + * The example shows how to add a new connection using libnm. Contrast this + * example with add-connection-gdbus.c, which is a bit lower level and talks + * directly to NM using GDBus. This example is simpler because libnm handles + * much of the low-level stuff for you. + * + * Compile with: + * gcc -Wall `pkg-config --libs --cflags glib-2.0 libnm` add-connection-libnm.c -o add-connection-libnm + */ + +#include "config.h" + +#include +#include + +static void +added_cb (GObject *client, + GAsyncResult *result, + gpointer user_data) +{ + GMainLoop *loop = user_data; + NMRemoteConnection *remote; + GError *error; + + /* NM responded to our request; either handle the resulting error or + * print out the object path of the connection we just added. + */ + remote = nm_client_add_connection_finish (NM_CLIENT (client), result, &error); + + if (error) { + g_print ("Error adding connection: %s", error->message); + g_error_free (error); + } else { + g_print ("Added: %s\n", nm_connection_get_path (NM_CONNECTION (remote))); + g_object_unref (remote); + } + + /* Tell the mainloop we're done and we can quit now */ + g_main_loop_quit (loop); +} + +static void +add_connection (NMClient *client, GMainLoop *loop, const char *con_name) +{ + NMConnection *connection; + NMSettingConnection *s_con; + NMSettingWired *s_wired; + NMSettingIP4Config *s_ip4; + char *uuid; + + /* Create a new connection object */ + connection = nm_simple_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-3-ethernet", + NULL); + g_free (uuid); + nm_connection_add_setting (connection, NM_SETTING (s_con)); + + /* Build up the 'wired' Setting */ + s_wired = (NMSettingWired *) nm_setting_wired_new (); + nm_connection_add_setting (connection, NM_SETTING (s_wired)); + + /* Build up the 'ipv4' Setting */ + s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + g_object_set (G_OBJECT (s_ip4), + NM_SETTING_IP_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. + */ + nm_client_add_connection_async (client, connection, TRUE, NULL, added_cb, loop); + g_object_unref (connection); +} + + +int +main (int argc, char *argv[]) +{ + NMClient *client; + GMainLoop *loop; + GError *error = NULL; + +#if !GLIB_CHECK_VERSION (2, 35, 0) + /* Initialize GType system */ + g_type_init (); +#endif + + loop = g_main_loop_new (NULL, FALSE); + + /* Connect to NetworkManager */ + client = nm_client_new (NULL, &error); + if (!client) { + g_message ("Error: Could not connect to NetworkManager: %s.", error->message); + g_error_free (error); + return 1; + } + + /* Ask NM to add the new connection */ + add_connection (client, loop, "__Test connection__"); + /* Wait for the connection to be added */ + g_main_loop_run (loop); + + /* Clean up */ + g_object_unref (client); + + return 0; +} diff --git a/examples/C/glib/get-active-connections-dbus-glib.c b/examples/C/glib/get-active-connections-dbus-glib.c deleted file mode 100644 index 186feae2..00000000 --- a/examples/C/glib/get-active-connections-dbus-glib.c +++ /dev/null @@ -1,223 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* - * 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 2010 -2014 Red Hat, Inc. - */ - -/* - * The example shows how to call the D-Bus properties interface to get the - * list of currently active connections known to NetworkManager. It uses - * dbus-glib and libnm-util libraries. - * - * Compile with: - * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util` get-active-connections-dbus-glib.c -o get-active-connections-dbus-glib - */ - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#define DBUS_TYPE_G_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)) -#define DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, DBUS_TYPE_G_MAP_OF_VARIANT)) -#define DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH)) - -static void -print_connection (DBusGConnection *bus, const char *path) -{ - DBusGProxy *proxy; - GError *error = NULL; - GHashTable *hash = NULL; - NMConnection *connection = NULL; - - /* This function asks NetworkManager for the details of the connection */ - - /* Create the D-Bus proxy so we can ask it for the connection configuration details. */ - proxy = dbus_g_proxy_new_for_name (bus, - NM_DBUS_SERVICE, - path, - NM_DBUS_IFACE_SETTINGS_CONNECTION); - g_assert (proxy); - - /* Request the all the configuration of the Connection */ - if (!dbus_g_proxy_call (proxy, "GetSettings", &error, - G_TYPE_INVALID, - DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, &hash, - G_TYPE_INVALID)) { - g_warning ("Failed to get active connection Connection property: %s", - error->message); - g_error_free (error); - goto out; - } - - /* Using the raw configuration, create an NMConnection object for it. This - * step also verifies that the data we got from NetworkManager are valid. */ - connection = nm_connection_new_from_hash (hash, &error); - if (!connection) { - g_warning ("Received invalid connection data: %s", error->message); - g_error_free (error); - goto out; - } - - /* And finally dump all the configuration to stdout */ - printf ("%s <=> %s\n", nm_connection_get_id (connection), path); - nm_connection_dump (connection); - -out: - if (connection) - g_object_unref (connection); - if (hash) - g_hash_table_destroy (hash); - g_object_unref (proxy); -} - -static void -get_active_connection_details (DBusGConnection *bus, const char *obj_path) -{ - DBusGProxy *props_proxy; - GValue path_value = G_VALUE_INIT; - GError *error = NULL; - const char *path = NULL; - - /* This function gets the backing Connection object that describes the - * network configuration that the ActiveConnection object is actually using. - * The ActiveConnection object contains the mapping between the configuration - * and the actual network interfaces that are using that configuration. - */ - - /* Create a D-Bus object proxy for the active connection object's properties */ - props_proxy = dbus_g_proxy_new_for_name (bus, - NM_DBUS_SERVICE, - obj_path, - DBUS_INTERFACE_PROPERTIES); - g_assert (props_proxy); - - /* Get the object path of the Connection details */ - if (!dbus_g_proxy_call (props_proxy, "Get", &error, - G_TYPE_STRING, NM_DBUS_INTERFACE_ACTIVE_CONNECTION, - G_TYPE_STRING, "Connection", - G_TYPE_INVALID, - G_TYPE_VALUE, &path_value, - G_TYPE_INVALID)) { - g_warning ("Failed to get active connection Connection property: %s", - error->message); - g_error_free (error); - goto out; - } - - if (!G_VALUE_HOLDS (&path_value, DBUS_TYPE_G_OBJECT_PATH)) { - g_warning ("Unexpected type returned getting Connection property: %s", - G_VALUE_TYPE_NAME (&path_value)); - goto out; - } - - path = g_value_get_boxed (&path_value); - if (!path) { - g_warning ("Missing connection path!"); - goto out; - } - - /* Print out the actual connection details */ - print_connection (bus, path); - -out: - g_value_unset (&path_value); - g_object_unref (props_proxy); -} - -static void -get_active_connections (DBusGConnection *bus, DBusGProxy *proxy) -{ - GError *error = NULL; - GValue value = G_VALUE_INIT; - GPtrArray *paths = NULL; - const char *a_path; - int i; - - /* Get the ActiveConnections property from the NM Manager object */ - if (!dbus_g_proxy_call (proxy, "Get", &error, - G_TYPE_STRING, NM_DBUS_INTERFACE, - G_TYPE_STRING, "ActiveConnections", - G_TYPE_INVALID, - G_TYPE_VALUE, &value, - G_TYPE_INVALID)) { - g_warning ("Failed to get ActiveConnections property: %s", error->message); - g_error_free (error); - return; - } - - /* Make sure the ActiveConnections property is the type we expect it to be */ - if (!G_VALUE_HOLDS (&value, DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH)) { - g_warning ("Unexpected type returned getting ActiveConnections: %s", - G_VALUE_TYPE_NAME (&value)); - goto out; - } - - /* Extract the active connections array from the GValue */ - paths = g_value_get_boxed (&value); - if (!paths) { - g_warning ("Could not retrieve active connections property"); - goto out; - } - - /* And print out the details for each active connection */ - for (i = 0; i < paths->len; i++) { - a_path = g_ptr_array_index (paths, i); - printf ("Active connection path: %s\n", a_path); - get_active_connection_details (bus, a_path); - } - -out: - g_value_unset (&value); -} - - -int main (int argc, char *argv[]) -{ - DBusGConnection *bus; - DBusGProxy *props_proxy; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - /* 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. - */ - props_proxy = dbus_g_proxy_new_for_name (bus, - NM_DBUS_SERVICE, - NM_DBUS_PATH, - DBUS_INTERFACE_PROPERTIES); - g_assert (props_proxy); - - /* Get active connections */ - get_active_connections (bus, props_proxy); - - g_object_unref (props_proxy); - dbus_g_connection_unref (bus); - - return 0; -} diff --git a/examples/C/glib/get-active-connections-gdbus.c b/examples/C/glib/get-active-connections-gdbus.c new file mode 100644 index 00000000..9b34f6b2 --- /dev/null +++ b/examples/C/glib/get-active-connections-gdbus.c @@ -0,0 +1,274 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * 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 2010 -2014 Red Hat, Inc. + */ + +/* + * The example shows how to call the D-Bus properties interface to get the list + * of currently active connections known to NetworkManager. It uses GDBus, plus + * a few defines from the NetworkManager headers. + * + * Compile with: + * gcc -Wall `pkg-config --cflags libnm` `pkg-config --cflags --libs gio-2.0` get-active-connections-gdbus.c -o get-active-connections-gdbus + */ + +#include "config.h" + +#include +#include +#include + +static void +print_setting (const char *setting_name, GVariant *setting) +{ + GVariantIter iter; + const char *property_name; + GVariant *value; + char *printed_value; + + g_print (" %s:\n", setting_name); + g_variant_iter_init (&iter, setting); + while (g_variant_iter_next (&iter, "{&sv}", &property_name, &value)) { + printed_value = g_variant_print (value, FALSE); + if (strcmp (printed_value, "[]") != 0) + g_print (" %s: %s\n", property_name, printed_value); + g_free (printed_value); + g_variant_unref (value); + } +} + +static void +print_connection (const char *path) +{ + GDBusProxy *proxy; + GError *error = NULL; + GVariant *ret, *connection = NULL, *s_con = NULL; + const char *id, *type; + gboolean found; + GVariantIter iter; + const char *setting_name; + GVariant *setting; + + /* This function asks NetworkManager for the details of the connection */ + + /* Create the D-Bus proxy so we can ask it for the connection configuration details. */ + proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + NM_DBUS_SERVICE, + path, + NM_DBUS_INTERFACE_SETTINGS_CONNECTION, + NULL, NULL); + g_assert (proxy); + + /* Request the all the configuration of the Connection */ + ret = g_dbus_proxy_call_sync (proxy, + "GetSettings", + NULL, + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + if (!ret) { + g_dbus_error_strip_remote_error (error); + g_warning ("Failed to get connection settings: %s\n", error->message); + g_error_free (error); + goto out; + } + + g_variant_get (ret, "(@a{sa{sv}})", &connection); + + s_con = g_variant_lookup_value (connection, NM_SETTING_CONNECTION_SETTING_NAME, NULL); + g_assert (s_con != NULL); + found = g_variant_lookup (s_con, NM_SETTING_CONNECTION_ID, "&s", &id); + g_assert (found); + found = g_variant_lookup (s_con, NM_SETTING_CONNECTION_TYPE, "&s", &type); + g_assert (found); + + /* Dump the configuration to stdout */ + g_print ("%s <=> %s\n", id, path); + + /* Connection setting first */ + print_setting (NM_SETTING_CONNECTION_SETTING_NAME, s_con); + + /* Then the type-specific setting */ + setting = g_variant_lookup_value (connection, type, NULL); + if (setting) { + print_setting (type, setting); + g_variant_unref (setting); + } + + g_variant_iter_init (&iter, connection); + while (g_variant_iter_next (&iter, "{&s@a{sv}}", &setting_name, &setting)) { + if ( strcmp (setting_name, NM_SETTING_CONNECTION_SETTING_NAME) != 0 + && strcmp (setting_name, type) != 0) + print_setting (setting_name, setting); + g_variant_unref (setting); + } + g_print ("\n"); + +out: + if (s_con) + g_variant_unref (s_con); + if (connection) + g_variant_unref (connection); + if (ret) + g_variant_unref (ret); + g_object_unref (proxy); +} + +static void +get_active_connection_details (const char *obj_path) +{ + GDBusProxy *props_proxy; + GVariant *ret = NULL, *path_value = NULL; + const char *path = NULL; + GError *error = NULL; + + /* This function gets the backing Connection object that describes the + * network configuration that the ActiveConnection object is actually using. + * The ActiveConnection object contains the mapping between the configuration + * and the actual network interfaces that are using that configuration. + */ + + /* Create a D-Bus object proxy for the active connection object's properties */ + props_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + NM_DBUS_SERVICE, + obj_path, + "org.freedesktop.DBus.Properties", + NULL, NULL); + g_assert (props_proxy); + + /* Get the object path of the Connection details */ + ret = g_dbus_proxy_call_sync (props_proxy, + "Get", + g_variant_new ("(ss)", + NM_DBUS_INTERFACE_ACTIVE_CONNECTION, + "Connection"), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + if (!ret) { + g_dbus_error_strip_remote_error (error); + g_warning ("Failed to get active connection Connection property: %s\n", + error->message); + g_error_free (error); + goto out; + } + + g_variant_get (ret, "(v)", &path_value); + if (!g_variant_is_of_type (path_value, G_VARIANT_TYPE_OBJECT_PATH)) { + g_warning ("Unexpected type returned getting Connection property: %s", + g_variant_get_type_string (path_value)); + goto out; + } + + path = g_variant_get_string (path_value, NULL); + + /* Print out the actual connection details */ + print_connection (path); + +out: + if (path_value) + g_variant_unref (path_value); + if (ret) + g_variant_unref (ret); + g_object_unref (props_proxy); +} + +static void +get_active_connections (GDBusProxy *proxy) +{ + GError *error = NULL; + GVariant *ret = NULL, *value = NULL; + char **paths; + int i; + + /* Get the ActiveConnections property from the NM Manager object */ + ret = g_dbus_proxy_call_sync (proxy, + "Get", + g_variant_new ("(ss)", + NM_DBUS_INTERFACE, + "ActiveConnections"), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + if (!ret) { + g_dbus_error_strip_remote_error (error); + g_warning ("Failed to get ActiveConnections property: %s\n", error->message); + g_error_free (error); + return; + } + + g_variant_get (ret, "(v)", &value); + + /* Make sure the ActiveConnections property is the type we expect it to be */ + if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("ao"))) { + g_warning ("Unexpected type returned getting ActiveConnections: %s", + g_variant_get_type_string (value)); + goto out; + } + + /* Extract the active connections array from the GValue */ + paths = g_variant_dup_objv (value, NULL); + if (!paths) { + g_warning ("Could not retrieve active connections property"); + goto out; + } + + /* And print out the details for each active connection */ + for (i = 0; paths[i]; i++) { + g_print ("Active connection path: %s\n", paths[i]); + get_active_connection_details (paths[i]); + } + g_strfreev (paths); + +out: + if (value) + g_variant_unref (value); + if (ret) + g_variant_unref (ret); +} + + +int +main (int argc, char *argv[]) +{ + GDBusProxy *props_proxy; + +#if !GLIB_CHECK_VERSION (2, 35, 0) + /* Initialize GType system */ + g_type_init (); +#endif + + /* Create a D-Bus proxy to get the object properties from the NM Manager + * object. NM_DBUS_* defines are from nm-dbus-interface.h. + */ + props_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + NM_DBUS_SERVICE, + NM_DBUS_PATH, + "org.freedesktop.DBus.Properties", + NULL, NULL); + g_assert (props_proxy); + + /* Get active connections */ + get_active_connections (props_proxy); + + g_object_unref (props_proxy); + + return 0; +} diff --git a/examples/C/glib/get-ap-info-libnm-glib.c b/examples/C/glib/get-ap-info-libnm-glib.c deleted file mode 100644 index 1ad58124..00000000 --- a/examples/C/glib/get-ap-info-libnm-glib.c +++ /dev/null @@ -1,233 +0,0 @@ -/* - * 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 2010 Red Hat, Inc. - */ - -/* - * The example shows how to get info about APs visible by Wi-Fi devices - * using libnm-glib (that wraps direct D-Bus calls). - * The example uses dbus-glib, libnm-util and libnm-glib libraries. - * - * Compile with: - * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util libnm-glib` get-ap-info-libnm-glib.c -o get-ap-info-libnm-glib - */ - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -/* Convert flags to string */ -static char * -ap_wpa_rsn_flags_to_string (guint32 flags) -{ - char *flags_str[16]; /* Enough space for flags and terminating NULL */ - char *ret_str; - int i = 0; - - if (flags & NM_802_11_AP_SEC_PAIR_WEP40) - flags_str[i++] = g_strdup ("pair_wpe40"); - if (flags & NM_802_11_AP_SEC_PAIR_WEP104) - flags_str[i++] = g_strdup ("pair_wpe104"); - if (flags & NM_802_11_AP_SEC_PAIR_TKIP) - flags_str[i++] = g_strdup ("pair_tkip"); - if (flags & NM_802_11_AP_SEC_PAIR_CCMP) - flags_str[i++] = g_strdup ("pair_ccmp"); - if (flags & NM_802_11_AP_SEC_GROUP_WEP40) - flags_str[i++] = g_strdup ("group_wpe40"); - if (flags & NM_802_11_AP_SEC_GROUP_WEP104) - flags_str[i++] = g_strdup ("group_wpe104"); - if (flags & NM_802_11_AP_SEC_GROUP_TKIP) - flags_str[i++] = g_strdup ("group_tkip"); - if (flags & NM_802_11_AP_SEC_GROUP_CCMP) - flags_str[i++] = g_strdup ("group_ccmp"); - if (flags & NM_802_11_AP_SEC_KEY_MGMT_PSK) - flags_str[i++] = g_strdup ("psk"); - if (flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X) - flags_str[i++] = g_strdup ("802.1X"); - - if (i == 0) - flags_str[i++] = g_strdup ("none"); - - flags_str[i] = NULL; - - ret_str = g_strjoinv (" ", flags_str); - - i = 0; - while (flags_str[i]) - g_free (flags_str[i++]); - - return ret_str; -} - -static void -show_access_point_info (NMAccessPoint *ap) -{ - guint32 flags, wpa_flags, rsn_flags, freq, bitrate; - guint8 strength; - const GByteArray *ssid; - const char *hwaddr; - NM80211Mode mode; - char *freq_str, *ssid_str, *bitrate_str, *strength_str, *wpa_flags_str, *rsn_flags_str; - GString *security_str; - - /* Get AP properties */ - flags = nm_access_point_get_flags (ap); - wpa_flags = nm_access_point_get_wpa_flags (ap); - rsn_flags = nm_access_point_get_rsn_flags (ap); - ssid = nm_access_point_get_ssid (ap); - hwaddr = nm_access_point_get_bssid (ap); - freq = nm_access_point_get_frequency (ap); - mode = nm_access_point_get_mode (ap); - bitrate = nm_access_point_get_max_bitrate (ap); - strength = nm_access_point_get_strength (ap); - - /* Convert to strings */ - ssid_str = nm_utils_ssid_to_utf8 (ssid); - freq_str = g_strdup_printf ("%u MHz", freq); - bitrate_str = g_strdup_printf ("%u Mbit/s", bitrate/1000); - strength_str = g_strdup_printf ("%u", strength); - wpa_flags_str = ap_wpa_rsn_flags_to_string (wpa_flags); - rsn_flags_str = ap_wpa_rsn_flags_to_string (rsn_flags); - - security_str = g_string_new (NULL); - if ( !(flags & NM_802_11_AP_FLAGS_PRIVACY) - && (wpa_flags != NM_802_11_AP_SEC_NONE) - && (rsn_flags != NM_802_11_AP_SEC_NONE)) - g_string_append (security_str, "Encrypted: "); - - if ( (flags & NM_802_11_AP_FLAGS_PRIVACY) - && (wpa_flags == NM_802_11_AP_SEC_NONE) - && (rsn_flags == NM_802_11_AP_SEC_NONE)) - g_string_append (security_str, "WEP "); - if (wpa_flags != NM_802_11_AP_SEC_NONE) - g_string_append (security_str, "WPA "); - if (rsn_flags != NM_802_11_AP_SEC_NONE) - g_string_append (security_str, "WPA2 "); - if ( (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X) - || (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)) - g_string_append (security_str, "Enterprise "); - - if (security_str->len > 0) - g_string_truncate (security_str, security_str->len-1); /* Chop off last space */ - - printf ("SSID: %s\n", ssid_str); - printf ("BSSID: %s\n", hwaddr); - printf ("Mode: %s\n", mode == NM_802_11_MODE_ADHOC ? "Ad-Hoc" - : mode == NM_802_11_MODE_INFRA ? "Infrastructure" - : "Unknown"); - printf ("Freq: %s\n", freq_str); - printf ("Bitrate: %s\n", bitrate_str); - printf ("Strength: %s\n", strength_str); - printf ("Security: %s\n", security_str->str); - printf ("WPA flags: %s\n", wpa_flags_str); - printf ("RSN flags: %s\n", rsn_flags_str); - printf ("D-Bus path: %s\n\n", nm_object_get_path (NM_OBJECT (ap))); - - g_free (ssid_str); - g_free (freq_str); - g_free (bitrate_str); - g_free (strength_str); - g_free (wpa_flags_str); - g_free (rsn_flags_str); - g_string_free (security_str, TRUE); -} - -static void -show_wifi_device_info (NMDevice *device) -{ - NMAccessPoint *active_ap = NULL; - const GPtrArray *aps; - const char *iface; - const char *driver; - guint32 speed; - const GByteArray *active_ssid; - char *active_ssid_str = NULL; - int i; - - /* Get active AP */ - if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) { - if ((active_ap = nm_device_wifi_get_active_access_point (NM_DEVICE_WIFI (device)))) { - active_ssid = nm_access_point_get_ssid (active_ap); - active_ssid_str = nm_utils_ssid_to_utf8 (active_ssid); - } - } - - iface = nm_device_get_iface (device); - driver = nm_device_get_driver (device); - speed = nm_device_wifi_get_bitrate (NM_DEVICE_WIFI (device)); - speed /= 1000; - - printf ("Device: %s ---- Driver: %s ---- Speed: %d Mbit/s ---- Active AP: %s\n", - iface, driver, speed, active_ssid_str ? active_ssid_str : "none"); - printf ("=================================================================================\n"); - g_free (active_ssid_str); - - /* Get all APs of the Wi-Fi device */ - aps = nm_device_wifi_get_access_points (NM_DEVICE_WIFI (device)); - - /* Print AP details */ - for (i = 0; aps && (i < aps->len); i++) { - NMAccessPoint *ap = g_ptr_array_index (aps, i); - show_access_point_info (ap); - } -} - -int main (int argc, char *argv[]) -{ - DBusGConnection *bus; - NMClient *client; - const GPtrArray *devices; - int i; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - /* Get system bus */ - bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL); - - /* Get NMClient object */ - client = nm_client_new (); - if (!client) { - dbus_g_connection_unref (bus); - g_message ("Error: Could not create NMClient."); - return EXIT_FAILURE; - } - - /* Get all devices managed by NetworkManager */ - devices = nm_client_get_devices (client); - - /* Go through the array and process Wi-Fi devices */ - for (i = 0; devices && (i < devices->len); i++) { - NMDevice *device = g_ptr_array_index (devices, i); - if (NM_IS_DEVICE_WIFI (device)) - show_wifi_device_info (device); - } - - g_object_unref (client); - dbus_g_connection_unref (bus); - - return EXIT_SUCCESS; -} diff --git a/examples/C/glib/get-ap-info-libnm.c b/examples/C/glib/get-ap-info-libnm.c new file mode 100644 index 00000000..90bc2d5e --- /dev/null +++ b/examples/C/glib/get-ap-info-libnm.c @@ -0,0 +1,233 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * 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. + * + * Copyright 2010 Red Hat, Inc. + */ + +/* + * The example shows how to get info about APs visible by Wi-Fi devices using + * libnm. + * + * Compile with: + * gcc -Wall `pkg-config --libs --cflags glib-2.0 libnm` get-ap-info-libnm.c -o get-ap-info-libnm + */ + +#include "config.h" + +#include +#include +#include + +#include + +/* Convert flags to string */ +static char * +ap_wpa_rsn_flags_to_string (guint32 flags) +{ + char *flags_str[16]; /* Enough space for flags and terminating NULL */ + char *ret_str; + int i = 0; + + if (flags & NM_802_11_AP_SEC_PAIR_WEP40) + flags_str[i++] = g_strdup ("pair_wpe40"); + if (flags & NM_802_11_AP_SEC_PAIR_WEP104) + flags_str[i++] = g_strdup ("pair_wpe104"); + if (flags & NM_802_11_AP_SEC_PAIR_TKIP) + flags_str[i++] = g_strdup ("pair_tkip"); + if (flags & NM_802_11_AP_SEC_PAIR_CCMP) + flags_str[i++] = g_strdup ("pair_ccmp"); + if (flags & NM_802_11_AP_SEC_GROUP_WEP40) + flags_str[i++] = g_strdup ("group_wpe40"); + if (flags & NM_802_11_AP_SEC_GROUP_WEP104) + flags_str[i++] = g_strdup ("group_wpe104"); + if (flags & NM_802_11_AP_SEC_GROUP_TKIP) + flags_str[i++] = g_strdup ("group_tkip"); + if (flags & NM_802_11_AP_SEC_GROUP_CCMP) + flags_str[i++] = g_strdup ("group_ccmp"); + if (flags & NM_802_11_AP_SEC_KEY_MGMT_PSK) + flags_str[i++] = g_strdup ("psk"); + if (flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X) + flags_str[i++] = g_strdup ("802.1X"); + + if (i == 0) + flags_str[i++] = g_strdup ("none"); + + flags_str[i] = NULL; + + ret_str = g_strjoinv (" ", flags_str); + + i = 0; + while (flags_str[i]) + g_free (flags_str[i++]); + + return ret_str; +} + +static void +show_access_point_info (NMAccessPoint *ap) +{ + guint32 flags, wpa_flags, rsn_flags, freq, bitrate; + guint8 strength; + GBytes *ssid; + const char *hwaddr; + NM80211Mode mode; + char *freq_str, *ssid_str, *bitrate_str, *strength_str, *wpa_flags_str, *rsn_flags_str; + GString *security_str; + + /* Get AP properties */ + flags = nm_access_point_get_flags (ap); + wpa_flags = nm_access_point_get_wpa_flags (ap); + rsn_flags = nm_access_point_get_rsn_flags (ap); + ssid = nm_access_point_get_ssid (ap); + hwaddr = nm_access_point_get_bssid (ap); + freq = nm_access_point_get_frequency (ap); + mode = nm_access_point_get_mode (ap); + bitrate = nm_access_point_get_max_bitrate (ap); + strength = nm_access_point_get_strength (ap); + + /* Convert to strings */ + if (ssid) + ssid_str = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL), g_bytes_get_size (ssid)); + else + ssid_str = g_strdup ("--"); + freq_str = g_strdup_printf ("%u MHz", freq); + bitrate_str = g_strdup_printf ("%u Mbit/s", bitrate/1000); + strength_str = g_strdup_printf ("%u", strength); + wpa_flags_str = ap_wpa_rsn_flags_to_string (wpa_flags); + rsn_flags_str = ap_wpa_rsn_flags_to_string (rsn_flags); + + security_str = g_string_new (NULL); + if ( !(flags & NM_802_11_AP_FLAGS_PRIVACY) + && (wpa_flags != NM_802_11_AP_SEC_NONE) + && (rsn_flags != NM_802_11_AP_SEC_NONE)) + g_string_append (security_str, "Encrypted: "); + + if ( (flags & NM_802_11_AP_FLAGS_PRIVACY) + && (wpa_flags == NM_802_11_AP_SEC_NONE) + && (rsn_flags == NM_802_11_AP_SEC_NONE)) + g_string_append (security_str, "WEP "); + if (wpa_flags != NM_802_11_AP_SEC_NONE) + g_string_append (security_str, "WPA "); + if (rsn_flags != NM_802_11_AP_SEC_NONE) + g_string_append (security_str, "WPA2 "); + if ( (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X) + || (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)) + g_string_append (security_str, "Enterprise "); + + if (security_str->len > 0) + g_string_truncate (security_str, security_str->len-1); /* Chop off last space */ + + printf ("SSID: %s\n", ssid_str); + printf ("BSSID: %s\n", hwaddr); + printf ("Mode: %s\n", mode == NM_802_11_MODE_ADHOC ? "Ad-Hoc" + : mode == NM_802_11_MODE_INFRA ? "Infrastructure" + : "Unknown"); + printf ("Freq: %s\n", freq_str); + printf ("Bitrate: %s\n", bitrate_str); + printf ("Strength: %s\n", strength_str); + printf ("Security: %s\n", security_str->str); + printf ("WPA flags: %s\n", wpa_flags_str); + printf ("RSN flags: %s\n", rsn_flags_str); + printf ("D-Bus path: %s\n\n", nm_object_get_path (NM_OBJECT (ap))); + + g_free (ssid_str); + g_free (freq_str); + g_free (bitrate_str); + g_free (strength_str); + g_free (wpa_flags_str); + g_free (rsn_flags_str); + g_string_free (security_str, TRUE); +} + +static void +show_wifi_device_info (NMDevice *device) +{ + NMAccessPoint *active_ap = NULL; + const GPtrArray *aps; + const char *iface; + const char *driver; + guint32 speed; + GBytes *active_ssid; + char *active_ssid_str = NULL; + int i; + + /* Get active AP */ + if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) { + if ((active_ap = nm_device_wifi_get_active_access_point (NM_DEVICE_WIFI (device)))) { + active_ssid = nm_access_point_get_ssid (active_ap); + if (active_ssid) + active_ssid_str = nm_utils_ssid_to_utf8 (g_bytes_get_data (active_ssid, NULL), + g_bytes_get_size (active_ssid)); + else + active_ssid_str = g_strdup ("--"); + } + } + + iface = nm_device_get_iface (device); + driver = nm_device_get_driver (device); + speed = nm_device_wifi_get_bitrate (NM_DEVICE_WIFI (device)); + speed /= 1000; + + printf ("Device: %s ---- Driver: %s ---- Speed: %d Mbit/s ---- Active AP: %s\n", + iface, driver, speed, active_ssid_str ? active_ssid_str : "none"); + printf ("=================================================================================\n"); + g_free (active_ssid_str); + + /* Get all APs of the Wi-Fi device */ + aps = nm_device_wifi_get_access_points (NM_DEVICE_WIFI (device)); + + /* Print AP details */ + for (i = 0; i < aps->len; i++) { + NMAccessPoint *ap = g_ptr_array_index (aps, i); + show_access_point_info (ap); + } +} + +int +main (int argc, char *argv[]) +{ + NMClient *client; + const GPtrArray *devices; + int i; + GError *error = NULL; + +#if !GLIB_CHECK_VERSION (2, 35, 0) + /* Initialize GType system */ + g_type_init (); +#endif + + /* Get NMClient object */ + client = nm_client_new (NULL, &error); + if (!client) { + g_message ("Error: Could not create NMClient: %s.", error->message); + g_error_free (error); + return EXIT_FAILURE; + } + + /* Get all devices managed by NetworkManager */ + devices = nm_client_get_devices (client); + + /* Go through the array and process Wi-Fi devices */ + for (i = 0; i < devices->len; i++) { + NMDevice *device = g_ptr_array_index (devices, i); + if (NM_IS_DEVICE_WIFI (device)) + show_wifi_device_info (device); + } + + g_object_unref (client); + + return EXIT_SUCCESS; +} diff --git a/examples/C/glib/list-connections-dbus-glib.c b/examples/C/glib/list-connections-dbus-glib.c deleted file mode 100644 index e17b8dc7..00000000 --- a/examples/C/glib/list-connections-dbus-glib.c +++ /dev/null @@ -1,91 +0,0 @@ -/* - * 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 list connections from System Settings service using direct - * D-Bus call of ListConnections method. - * The example uses dbus-glib, libnm-util libraries. - * - * Compile with: - * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util` list-connections-dbus.c -o list-connections-dbus - */ - -#include -#include -#include - -#include -#include - -#define DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH)) - -static void -list_connections (DBusGProxy *proxy) -{ - int i; - GError *error = NULL; - GPtrArray *con_array; - gboolean success; - - /* Call ListConnections D-Bus method */ - success = dbus_g_proxy_call (proxy, "ListConnections", &error, - /* No input arguments */ - G_TYPE_INVALID, - DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH, &con_array, /* Return values */ - G_TYPE_INVALID); - if (!success) { - printf ("ListConnections failed: %s", error->message); - g_error_free (error); - return; - } - - for (i = 0; con_array && i < con_array->len; i++) { - char *connection_path = g_ptr_array_index (con_array, i); - printf ("%s\n", connection_path); - g_free (connection_path); - } - g_ptr_array_free (con_array, TRUE); -} - -int main (int argc, char *argv[]) -{ - DBusGConnection *bus; - DBusGProxy *proxy; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - /* Get system bus */ - bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL); - - /* Create a D-Bus proxy; NM_DBUS_* defined in NetworkManager.h */ - proxy = dbus_g_proxy_new_for_name (bus, - NM_DBUS_SERVICE, - NM_DBUS_PATH_SETTINGS, - NM_DBUS_IFACE_SETTINGS); - - /* List connections of system settings service */ - list_connections (proxy); - - g_object_unref (proxy); - dbus_g_connection_unref (bus); - - return 0; -} diff --git a/examples/C/glib/list-connections-gdbus.c b/examples/C/glib/list-connections-gdbus.c new file mode 100644 index 00000000..634bd90c --- /dev/null +++ b/examples/C/glib/list-connections-gdbus.c @@ -0,0 +1,90 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * 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. + * + * Copyright 2011, 2014 Red Hat, Inc. + */ + +/* + * The example shows how to list connections from the System Settings service + * using direct D-Bus calls. The example uses GDBus, plus a few defines from + * the NetworkManager headers. Contrast list-connections-libnm, which is higher + * level because it uses libnm. + * + * Compile with: + * gcc -Wall `pkg-config --cflags libnm` `pkg-config --cflags --libs gio-2.0` list-connections-gdbus.c -o list-connections-gdbus + */ + +#include "config.h" + +#include +#include + +static void +list_connections (GDBusProxy *proxy) +{ + int i; + GError *error = NULL; + GVariant *ret; + char **paths; + + /* Call ListConnections D-Bus method */ + ret = g_dbus_proxy_call_sync (proxy, + "ListConnections", + NULL, + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + if (!ret) { + g_dbus_error_strip_remote_error (error); + g_print ("ListConnections failed: %s\n", error->message); + g_error_free (error); + return; + } + + g_variant_get (ret, "(^ao)", &paths); + g_variant_unref (ret); + + for (i = 0; paths[i]; i++) + g_print ("%s\n", paths[i]); + g_strfreev (paths); +} + +int +main (int argc, char *argv[]) +{ + GDBusProxy *proxy; + +#if !GLIB_CHECK_VERSION (2, 35, 0) + /* Initialize GType system */ + g_type_init (); +#endif + + /* Create a D-Bus proxy; NM_DBUS_* defined in nm-dbus-interface.h */ + proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + NM_DBUS_SERVICE, + NM_DBUS_PATH_SETTINGS, + NM_DBUS_INTERFACE_SETTINGS, + NULL, NULL); + g_assert (proxy != NULL); + + /* List connections of system settings service */ + list_connections (proxy); + + g_object_unref (proxy); + + return 0; +} diff --git a/examples/C/glib/list-connections-libnm-glib.c b/examples/C/glib/list-connections-libnm-glib.c deleted file mode 100644 index 29f105c6..00000000 --- a/examples/C/glib/list-connections-libnm-glib.c +++ /dev/null @@ -1,178 +0,0 @@ -/* - * 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 list connections from System Settings service using libnm-glib - * (that wraps direct D-Bus calls). - * The example uses dbus-glib, libnm-util and libnm-glib libraries. - * - * Compile with: - * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util libnm-glib` list-connections-libnm-glib.c -o list-connections-libnm-glib - */ - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - - -/* Global variables */ -GMainLoop *loop = NULL; /* Main loop variable - needed for waiting for signal */ -int result = EXIT_SUCCESS; - -static void -signal_handler (int signo) -{ - if (signo == SIGINT || signo == SIGTERM) { - g_message ("Caught signal %d, shutting down...", signo); - g_main_loop_quit (loop); - } -} - -static void -setup_signals (void) -{ - struct sigaction action; - sigset_t mask; - - sigemptyset (&mask); - action.sa_handler = signal_handler; - action.sa_mask = mask; - action.sa_flags = 0; - sigaction (SIGTERM, &action, NULL); - sigaction (SIGINT, &action, NULL); -} - -/* Print details of connection */ -static void -show_connection (gpointer data, gpointer user_data) -{ - NMConnection *connection = (NMConnection *) data; - NMSettingConnection *s_con; - guint64 timestamp; - char *timestamp_str; - char timestamp_real_str[64]; - const char *val1, *val2, *val3, *val4, *val5; - - 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); - timestamp_str = g_strdup_printf ("%" G_GUINT64_FORMAT, timestamp); - strftime (timestamp_real_str, sizeof (timestamp_real_str), "%c", localtime ((time_t *) ×tamp)); - - val1 = nm_setting_connection_get_id (s_con); - val2 = nm_setting_connection_get_uuid (s_con); - val3 = nm_setting_connection_get_connection_type (s_con); - val4 = nm_connection_get_path (connection); - val5 = timestamp ? timestamp_real_str : "never"; - - printf ("%-25s | %s | %-15s | %-43s | %s\n", val1, val2, val3, val4, val5); - - g_free (timestamp_str); - } -} - -/* This callback is called when connections from the settings service are ready. - * Now the connections can be listed. - */ -static void -get_connections_cb (NMRemoteSettings *settings, gpointer user_data) -{ - GSList *connections; - - connections = nm_remote_settings_list_connections (settings); - - printf ("Connections:\n===================\n"); - - g_slist_foreach (connections, show_connection, NULL); - - g_slist_free (connections); - g_object_unref (settings); - - /* We are done, exit main loop */ - g_main_loop_quit (loop); -} - -/* Get system settings and then connect to connections-read signal */ -static gboolean -list_connections (gpointer data) -{ - DBusGConnection *bus = (DBusGConnection *) data; - NMRemoteSettings *settings; - gboolean settings_running; - - /* Get system settings */ - if (!(settings = nm_remote_settings_new (bus))) { - g_message ("Error: Could not get system settings."); - result = EXIT_FAILURE; - g_main_loop_quit (loop); - return FALSE; - } - - /* Find out whether setting service is running */ - g_object_get (settings, NM_REMOTE_SETTINGS_SERVICE_RUNNING, &settings_running, NULL); - - if (!settings_running) { - g_message ("Error: Can't obtain connections: settings service is not running."); - result = EXIT_FAILURE; - g_main_loop_quit (loop); - return FALSE; - } - - /* Connect to signal "connections-read" - emitted when connections are fetched and ready */ - g_signal_connect (settings, NM_REMOTE_SETTINGS_CONNECTIONS_READ, - G_CALLBACK (get_connections_cb), NULL); - - return FALSE; -} - -int main (int argc, char *argv[]) -{ - DBusGConnection *bus; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - /* Get system bus */ - bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL); - - /* Run list_connections from main loop, because we need to wait for "connections-read" - * signal to have connections ready. The execution will be finished in get_connections_cb() - * callback on the signal. - */ - g_idle_add (list_connections, bus); - - loop = g_main_loop_new (NULL, FALSE); /* Create main loop */ - setup_signals (); /* Setup UNIX signals */ - g_main_loop_run (loop); /* Run main loop */ - - g_main_loop_unref (loop); - dbus_g_connection_unref (bus); - - return result; -} diff --git a/examples/C/glib/list-connections-libnm.c b/examples/C/glib/list-connections-libnm.c new file mode 100644 index 00000000..d5cdf361 --- /dev/null +++ b/examples/C/glib/list-connections-libnm.c @@ -0,0 +1,101 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * 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. + * + * Copyright 2011 Red Hat, Inc. + */ + +/* + * The example shows how to list connections. Contrast this example with + * list-connections-gdbus.c, which is a bit lower level and talks directly to NM + * using GDBus. + * + * Compile with: + * gcc -Wall `pkg-config --libs --cflags glib-2.0 libnm` list-connections-libnm.c -o list-connections-libnm + */ + +#include "config.h" + +#include +#include +#include + +#include + +/* Print details of connection */ +static void +show_connection (NMConnection *connection) +{ + NMSettingConnection *s_con; + guint64 timestamp; + char *timestamp_str; + char timestamp_real_str[64]; + const char *val1, *val2, *val3, *val4, *val5; + + 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); + timestamp_str = g_strdup_printf ("%" G_GUINT64_FORMAT, timestamp); + strftime (timestamp_real_str, sizeof (timestamp_real_str), "%c", localtime ((time_t *) ×tamp)); + + val1 = nm_setting_connection_get_id (s_con); + val2 = nm_setting_connection_get_uuid (s_con); + val3 = nm_setting_connection_get_connection_type (s_con); + val4 = nm_connection_get_path (connection); + val5 = timestamp ? timestamp_real_str : "never"; + + printf ("%-25s | %s | %-15s | %-43s | %s\n", val1, val2, val3, val4, val5); + + g_free (timestamp_str); + } +} + +int +main (int argc, char *argv[]) +{ + NMClient *client; + GError *error = NULL; + const GPtrArray *connections; + int i; + +#if !GLIB_CHECK_VERSION (2, 35, 0) + /* Initialize GType system */ + g_type_init (); +#endif + + if (!(client = nm_client_new (NULL, &error))) { + g_message ("Error: Could not connect to NetworkManager: %s.", error->message); + g_error_free (error); + return EXIT_FAILURE; + } + + if (!nm_client_get_nm_running (client)) { + g_message ("Error: Can't obtain connections: NetworkManager is not running."); + return EXIT_FAILURE; + } + + /* Now the connections can be listed. */ + connections = nm_client_get_connections (client); + + printf ("Connections:\n===================\n"); + + for (i = 0; i < connections->len; i++) + show_connection (connections->pdata[i]); + + g_object_unref (client); + + return EXIT_SUCCESS; +} diff --git a/examples/C/glib/monitor-nm-running-GDBus.c b/examples/C/glib/monitor-nm-running-GDBus.c deleted file mode 100644 index 9e38d4c7..00000000 --- a/examples/C/glib/monitor-nm-running-GDBus.c +++ /dev/null @@ -1,88 +0,0 @@ -/* -*- 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 - -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); -} - - -int -main (int argc, char *argv[]) -{ - guint watcher_id; - GMainLoop *loop; - GBusNameWatcherFlags flags; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - 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); - - return 0; -} - diff --git a/examples/C/glib/monitor-nm-running-dbus-glib.c b/examples/C/glib/monitor-nm-running-dbus-glib.c deleted file mode 100644 index 0b01a57f..00000000 --- a/examples/C/glib/monitor-nm-running-dbus-glib.c +++ /dev/null @@ -1,127 +0,0 @@ -/* -*- 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 -#include -#include -#include - -#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; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - 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-running-gdbus.c b/examples/C/glib/monitor-nm-running-gdbus.c new file mode 100644 index 00000000..e4b3bbbf --- /dev/null +++ b/examples/C/glib/monitor-nm-running-gdbus.c @@ -0,0 +1,89 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * 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. + * + * 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 "config.h" + +#include + +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); +} + + +int +main (int argc, char *argv[]) +{ + guint watcher_id; + GMainLoop *loop; + GBusNameWatcherFlags flags; + +#if !GLIB_CHECK_VERSION (2, 35, 0) + /* Initialize GType system */ + g_type_init (); +#endif + + 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); + + return 0; +} + diff --git a/examples/C/glib/monitor-nm-state-GDBus.c b/examples/C/glib/monitor-nm-state-GDBus.c deleted file mode 100644 index f4019725..00000000 --- a/examples/C/glib/monitor-nm-state-GDBus.c +++ /dev/null @@ -1,134 +0,0 @@ -/* -*- 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 -#include -#include - -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)); - } -} - - -int -main (int argc, char *argv[]) -{ - GMainLoop *loop; - GError *error = NULL; - GDBusProxyFlags flags; - GDBusProxy *proxy; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - /* 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); - - 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..aeabcfe1 --- /dev/null +++ b/examples/C/glib/monitor-nm-state-gdbus.c @@ -0,0 +1,136 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * 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. + * + * Copyright 2012 Red Hat, Inc. + */ + +/* + * This example monitors NM state via D-Bus "StateChanged" signal on + * "org.freedesktop.NetworkManager" object. + * It uses GDBus and the libnm headers. + + * You don't need to have the NetworkManager devel packages installed. You can just + * grab nm-dbus-interface.h and put it in the path. + * + * Standalone compilation: + * gcc -Wall `pkg-config --libs --cflags glib-2.0 gio-2.0` `pkg-config --cflags libnm` monitor-nm-state-gdbus.c -o monitor-nm-state-gdbus + */ + +#include "config.h" + +#include +#include +#include + +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)); + } +} + + +int +main (int argc, char *argv[]) +{ + GMainLoop *loop; + GError *error = NULL; + GDBusProxyFlags flags; + GDBusProxy *proxy; + +#if !GLIB_CHECK_VERSION (2, 35, 0) + /* Initialize GType system */ + g_type_init (); +#endif + + /* 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_dbus_error_strip_remote_error (error); + 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); + + return 0; +} + diff --git a/examples/C/qt/Makefile.am b/examples/C/qt/Makefile.am index 15b6bf45..aff4c48e 100644 --- a/examples/C/qt/Makefile.am +++ b/examples/C/qt/Makefile.am @@ -1,6 +1,6 @@ AM_CPPFLAGS = \ - -I${top_srcdir}/include \ - -I${top_builddir}/include \ + -I${top_srcdir}/libnm-core \ + -I${top_builddir}/libnm-core \ $(DBUS_CFLAGS) \ $(QT_CFLAGS) diff --git a/examples/C/qt/Makefile.in b/examples/C/qt/Makefile.in index 6cce9883..f6bb151c 100644 --- a/examples/C/qt/Makefile.in +++ b/examples/C/qt/Makefile.in @@ -200,6 +200,8 @@ AUTOCONF = @AUTOCONF@ AUTOHEADER = @AUTOHEADER@ AUTOMAKE = @AUTOMAKE@ AWK = @AWK@ +BLUEZ5_CFLAGS = @BLUEZ5_CFLAGS@ +BLUEZ5_LIBS = @BLUEZ5_LIBS@ CC = @CC@ CCDEPMODE = @CCDEPMODE@ CFLAGS = @CFLAGS@ @@ -323,6 +325,7 @@ NEWT_CFLAGS = @NEWT_CFLAGS@ NEWT_LIBS = @NEWT_LIBS@ NM = @NM@ NMEDIT = @NMEDIT@ +NM_CONFIG_DEFAULT_AUTH_POLKIT_TEXT = @NM_CONFIG_DEFAULT_AUTH_POLKIT_TEXT@ NM_MAJOR_VERSION = @NM_MAJOR_VERSION@ NM_MICRO_VERSION = @NM_MICRO_VERSION@ NM_MINOR_VERSION = @NM_MINOR_VERSION@ @@ -342,6 +345,7 @@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_URL = @PACKAGE_URL@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PERL = @PERL@ PKG_CONFIG = @PKG_CONFIG@ PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@ PKG_CONFIG_PATH = @PKG_CONFIG_PATH@ @@ -368,7 +372,7 @@ SYSTEMD_INHIBIT_LIBS = @SYSTEMD_INHIBIT_LIBS@ SYSTEMD_LOGIN_CFLAGS = @SYSTEMD_LOGIN_CFLAGS@ SYSTEMD_LOGIN_LIBS = @SYSTEMD_LOGIN_LIBS@ SYSTEM_CA_PATH = @SYSTEM_CA_PATH@ -UDEV_BASE_DIR = @UDEV_BASE_DIR@ +UDEV_DIR = @UDEV_DIR@ USE_NLS = @USE_NLS@ UUID_CFLAGS = @UUID_CFLAGS@ UUID_LIBS = @UUID_LIBS@ @@ -448,8 +452,8 @@ with_netconfig = @with_netconfig@ with_resolvconf = @with_resolvconf@ with_valgrind = @with_valgrind@ AM_CPPFLAGS = \ - -I${top_srcdir}/include \ - -I${top_builddir}/include \ + -I${top_srcdir}/libnm-core \ + -I${top_builddir}/libnm-core \ $(DBUS_CFLAGS) \ $(QT_CFLAGS) diff --git a/examples/C/qt/add-connection-wired.cpp b/examples/C/qt/add-connection-wired.cpp index 475eafff..fe790ecd 100644 --- a/examples/C/qt/add-connection-wired.cpp +++ b/examples/C/qt/add-connection-wired.cpp @@ -31,7 +31,7 @@ #include -#include "NetworkManager.h" +#include "nm-dbus-interface.h" typedef QMap > Connection; Q_DECLARE_METATYPE(Connection) @@ -68,7 +68,7 @@ int main() { QDBusInterface interface( NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, - NM_DBUS_IFACE_SETTINGS, + NM_DBUS_INTERFACE_SETTINGS, QDBusConnection::systemBus()); addConnection(interface, "__Test connection__"); diff --git a/examples/C/qt/change-ipv4-addresses.cpp b/examples/C/qt/change-ipv4-addresses.cpp index ca340f7f..8ca3dde2 100644 --- a/examples/C/qt/change-ipv4-addresses.cpp +++ b/examples/C/qt/change-ipv4-addresses.cpp @@ -22,10 +22,10 @@ * It uses Qt and D-Bus libraries to do that. * * Standalone compilation: - * g++ -Wall `pkg-config --libs --cflags NetworkManager QtCore QtDBus QtNetwork` change-ipv4-addresses.cpp -o change-ipv4-addresses + * g++ -Wall `pkg-config --libs --cflags QtCore QtDBus QtNetwork` `pkg-config --cflags libnm` change-ipv4-addresses.cpp -o change-ipv4-addresses * * You don't need to have NetworkManager devel package installed; you can just - * grab NetworkManager.h and put it in the path + * grab nm-dbus-interface.h and put it in the path */ #include @@ -40,7 +40,7 @@ #include "arpa/inet.h" -#include "NetworkManager.h" +#include "nm-dbus-interface.h" typedef QMap > Connection; Q_DECLARE_METATYPE(Connection) @@ -61,7 +61,7 @@ const QString getConnection(const QString& connectionUuid, Connection *found_con QDBusInterface interface( NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, - NM_DBUS_IFACE_SETTINGS, + NM_DBUS_INTERFACE_SETTINGS, QDBusConnection::systemBus()); // Get connection list and find the connection with 'connectionUuid' @@ -71,7 +71,7 @@ const QString getConnection(const QString& connectionUuid, Connection *found_con ifaceForSettings = new QDBusInterface( NM_DBUS_SERVICE, connection.path(), - NM_DBUS_IFACE_SETTINGS_CONNECTION, + NM_DBUS_INTERFACE_SETTINGS_CONNECTION, QDBusConnection::systemBus()); QDBusReply result2 = ifaceForSettings->call("GetSettings"); delete ifaceForSettings; @@ -120,7 +120,7 @@ void changeConnection(const QString& uuid) QDBusInterface interface( NM_DBUS_SERVICE, conPath, - NM_DBUS_IFACE_SETTINGS_CONNECTION, + NM_DBUS_INTERFACE_SETTINGS_CONNECTION, QDBusConnection::systemBus()); // Call Update() D-Bus method to update connection diff --git a/examples/C/qt/list-connections.cpp b/examples/C/qt/list-connections.cpp index fb981664..66f309c8 100644 --- a/examples/C/qt/list-connections.cpp +++ b/examples/C/qt/list-connections.cpp @@ -28,7 +28,7 @@ #include -#include "NetworkManager.h" +#include "nm-dbus-interface.h" void listConnections(QDBusInterface& interface) { @@ -44,7 +44,7 @@ int main() { QDBusInterface interface( NM_DBUS_SERVICE, NM_DBUS_PATH_SETTINGS, - NM_DBUS_IFACE_SETTINGS, + NM_DBUS_INTERFACE_SETTINGS, QDBusConnection::systemBus()); listConnections(interface); -- cgit 1.3.0-6-gf8a5