From 298b67120507e9f19fa60e56d8abbe4f870125cc Mon Sep 17 00:00:00 2001 From: Kalle Valo Date: Fri, 28 Jan 2011 17:02:36 +0200 Subject: nmcompat: Add plugin 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 | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 plugins/nmcompat.c diff --git a/Makefile.plugins b/Makefile.plugins index 18416979..af3cfa0b 100644 --- a/Makefile.plugins +++ b/Makefile.plugins @@ -269,6 +269,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 66bee66c..38aa4b43 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 00000000..876c00f3 --- /dev/null +++ b/plugins/nmcompat.c @@ -0,0 +1,188 @@ +/* + * + * 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 +#endif + +#include + +#define CONNMAN_API_SUBJECT_TO_CHANGE +#include +#include +#include +#include + +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; + + 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(¬ifier) < 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(¬ifier); + + 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) -- cgit v1.2.3