[PATCH v2 2/4] nmcompat: add plugin

Kalle Valo kalle.valo at canonical.com
Fri Jan 28 00:05:45 PST 2011


Add plugin which provides network-manager state interface to applications.
This is a direct copy of what was in src/manager.c, just copied all into
a plugin.
---
 Makefile.plugins   |   12 +++
 configure.ac       |    6 ++
 plugins/nmcompat.c |  189 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 207 insertions(+), 0 deletions(-)
 create mode 100644 plugins/nmcompat.c

diff --git a/Makefile.plugins b/Makefile.plugins
index e87f10b..eb317f3 100644
--- a/Makefile.plugins
+++ b/Makefile.plugins
@@ -272,6 +272,18 @@ plugins_ntpd_la_LDFLAGS = $(plugin_ldflags)
 endif
 endif
 
+if NMCOMPAT
+if NMCOMPAT_BUILTIN
+builtin_modules += nmcompat
+builtin_sources += plugins/nmcompat.c
+else
+plugin_LTLIBRARIES += plugins/nmcompat.la
+plugin_objects += $(plugins_nmcompat_la_OBJECTS)
+plugins_nmcompat_la_CFLAGS = $(plugin_cflags)
+plugins_nmcompat_la_LDFLAGS = $(plugin_ldflags)
+endif
+endif
+
 EXTRA_DIST += plugins/polkit.policy
 
 plugins/net.connman.policy: plugins/polkit.policy
diff --git a/configure.ac b/configure.ac
index 951b7a3..f2d401f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -274,6 +274,12 @@ fi
 AM_CONDITIONAL(NTPD, test "${enable_ntpd}" != "no")
 AM_CONDITIONAL(NTPD_BUILTIN, test "${enable_ntpd}" = "builtin")
 
+AC_ARG_ENABLE(nmcompat,
+	AC_HELP_STRING([--enable-nmcompat], [enable nmcompat support]),
+			[enable_nmcompat=${enableval}], [enable_nmcompat="no"])
+AM_CONDITIONAL(NMCOMPAT, test "${enable_nmcompat}" != "no")
+AM_CONDITIONAL(NMCOMPAT_BUILTIN, test "${enable_nmcompat}" = "builtin")
+
 AC_ARG_WITH(stats-max-file-size, AC_HELP_STRING([--with-stats-max-file-size=SIZE],
 			[Maximal size of a statistics round robin file]),
 			[stats_max_file_size=${withval}])
diff --git a/plugins/nmcompat.c b/plugins/nmcompat.c
new file mode 100644
index 0000000..0135d03
--- /dev/null
+++ b/plugins/nmcompat.c
@@ -0,0 +1,189 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <gdbus.h>
+
+#define CONNMAN_API_SUBJECT_TO_CHANGE
+#include <connman/plugin.h>
+#include <connman/log.h>
+#include <connman/notifier.h>
+#include <connman/dbus.h>
+
+enum {
+	NM_STATE_UNKNOWN = 0,
+	NM_STATE_ASLEEP,
+	NM_STATE_CONNECTING,
+	NM_STATE_CONNECTED,
+	NM_STATE_DISCONNECTED
+};
+
+#define NM_SERVICE    "org.freedesktop.NetworkManager"
+#define NM_PATH       "/org/freedesktop/NetworkManager"
+#define NM_INTERFACE  NM_SERVICE
+
+static DBusConnection *connection = NULL;
+static dbus_uint32_t state = NM_STATE_UNKNOWN;
+
+
+static void nm_send_signal(const char *name, dbus_uint32_t state)
+{
+	DBusMessage *signal;
+
+	signal = dbus_message_new_signal(NM_PATH, NM_INTERFACE, name);
+	if (signal == NULL)
+		return;
+
+	dbus_message_append_args(signal, DBUS_TYPE_UINT32, &state,
+				DBUS_TYPE_INVALID);
+
+	g_dbus_send_message(connection, signal);
+}
+
+static void default_changed(struct connman_service *service)
+{
+	if (service != NULL)
+		state = NM_STATE_CONNECTED;
+	else
+		state = NM_STATE_DISCONNECTED;
+
+	DBG("%p %d", service, state);
+
+	/* older deprecated signal, in case applications still use this */
+	nm_send_signal("StateChange", state);
+
+	/* the preferred current signal */
+	nm_send_signal("StateChanged", state);
+}
+
+static struct connman_notifier notifier = {
+	.name		= "nmcompat",
+	.priority	= CONNMAN_NOTIFIER_PRIORITY_DEFAULT,
+	.default_changed= default_changed,
+};
+
+static DBusMessage *nm_sleep(DBusConnection *conn,
+					DBusMessage *msg, void *data)
+{
+	DBusMessage *reply;
+
+	DBG("conn %p", conn);
+
+	reply = dbus_message_new_method_return(msg);
+	if (reply == NULL)
+		return NULL;
+
+	dbus_message_append_args(reply, DBUS_TYPE_INVALID);
+
+	return reply;
+}
+
+static DBusMessage *nm_wake(DBusConnection *conn,
+					DBusMessage *msg, void *data)
+{
+	DBusMessage *reply;
+
+	DBG("conn %p", conn);
+
+	reply = dbus_message_new_method_return(msg);
+	if (reply == NULL)
+		return NULL;
+
+	dbus_message_append_args(reply, DBUS_TYPE_INVALID);
+
+	return reply;
+}
+
+static DBusMessage *nm_state(DBusConnection *conn,
+					DBusMessage *msg, void *data)
+{
+	DBusMessage *reply;
+	dbus_uint32_t state;
+
+	DBG("conn %p", conn);
+
+	reply = dbus_message_new_method_return(msg);
+	if (reply == NULL)
+		return NULL;
+
+	dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
+							DBUS_TYPE_INVALID);
+
+	return reply;
+}
+
+static GDBusMethodTable nm_methods[] = {
+	{ "sleep", "",  "",   nm_sleep        },
+	{ "wake",  "",  "",   nm_wake         },
+	{ "state", "",  "u",  nm_state        },
+	{ },
+};
+
+static int nmcompat_init(void)
+{
+	gboolean ret;
+
+	DBG("");
+
+	connection = connman_dbus_get_connection();
+	if (connection == NULL)
+		return -1;
+
+	if (g_dbus_request_name(connection, NM_SERVICE, NULL) == FALSE) {
+		connman_error("nmcompat: can't register nm service\n");
+		return -1;
+	}
+
+	if (connman_notifier_register(&notifier) < 0) {
+		connman_error("nmcompat: failed to register notifier");
+		return -1;
+	}
+
+	ret = g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
+					nm_methods, NULL, NULL, NULL, NULL);
+	if (ret == FALSE) {
+		connman_error("nmcompat: can't register " NM_INTERFACE);
+		return -1;
+	}
+
+	return 0;
+}
+
+static void nmcompat_exit(void)
+{
+	DBG("");
+
+	connman_notifier_unregister(&notifier);
+
+	if (connection == NULL)
+		return;
+
+	g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
+
+	dbus_connection_unref(connection);
+}
+
+CONNMAN_PLUGIN_DEFINE(nmcompat, "NetworkManager compatibility interfaces",
+			VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
+			nmcompat_init, nmcompat_exit)




More information about the connman mailing list