summaryrefslogtreecommitdiff
path: root/plugins/nmcompat.c
diff options
context:
space:
mode:
authorKalle Valo <kalle.valo@canonical.com>2011-01-28 17:02:36 +0200
committerSamuel Ortiz <sameo@linux.intel.com>2011-01-28 16:20:06 +0100
commit298b67120507e9f19fa60e56d8abbe4f870125cc (patch)
tree379fb8af912cab3d5916ac668e478f5ebbfe17b9 /plugins/nmcompat.c
parentdb075ce314d647680cce30d97fc174a3aee02f77 (diff)
downloadconnman-298b67120507e9f19fa60e56d8abbe4f870125cc.tar.gz
connman-298b67120507e9f19fa60e56d8abbe4f870125cc.tar.bz2
connman-298b67120507e9f19fa60e56d8abbe4f870125cc.zip
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.
Diffstat (limited to 'plugins/nmcompat.c')
-rw-r--r--plugins/nmcompat.c188
1 files changed, 188 insertions, 0 deletions
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 <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;
+
+ 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)