From 99983be83b0f1c0ecbce00276fd21ba05b4d681f Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 24 Jan 2008 15:01:28 +0100 Subject: [PATCH] Add handling for agent registration and monitoring --- src/agent.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ src/connman.h | 8 ++++-- src/main.c | 2 +- src/manager.c | 12 +++++--- 4 files changed, 93 insertions(+), 17 deletions(-) diff --git a/src/agent.c b/src/agent.c index d3a2746..4765868 100644 --- a/src/agent.c +++ b/src/agent.c @@ -23,30 +23,100 @@ #include #endif +#include +#include +#include + +#include + #include "connman.h" -int __connman_agent_init(void) +static DBusConnection *connection = NULL; +static guint agent_watch = 0; +static gchar *agent_path = NULL; +static gchar *agent_sender = NULL; + +static void agent_free(void) { - DBG(""); + agent_watch = 0; - return 0; + g_free(agent_sender); + agent_sender = NULL; + + g_free(agent_path); + agent_path = NULL; } -void __connman_agent_cleanup(void) +static gboolean agent_disconnect(void *data) +{ + DBG("data %p", data); + + agent_free(); + + return TRUE; +} + +int __connman_agent_register(const char *sender, const char *path) { - DBG(""); + DBG("sender %s path %s", sender, path); + + if (agent_path != NULL) + return -EEXIST; + + agent_sender = g_strdup(sender); + agent_path = g_strdup(path); + + agent_watch = g_dbus_add_disconnect_watch(connection, sender, + agent_disconnect, NULL, NULL); + + return 0; } -int __connman_agent_register(const char *path) +int __connman_agent_unregister(const char *sender, const char *path) { - DBG(""); + DBG("sender %s path %s", sender, path); + + if (agent_path == NULL) + return -ENOENT; + + if (agent_watch > 0) + g_dbus_remove_watch(connection, agent_watch); + + agent_free(); return 0; } -int __connman_agent_unregister(const char *path) +int __connman_agent_init(DBusConnection *conn) { - DBG(""); + DBG("conn %p", conn); + + connection = dbus_connection_ref(conn); + if (connection == NULL) + return -1; return 0; } + +void __connman_agent_cleanup(void) +{ + DBusMessage *msg; + + DBG("conn %p", connection); + + if (agent_watch > 0) + g_dbus_remove_watch(connection, agent_watch); + + msg = dbus_message_new_method_call(agent_sender, agent_path, + CONNMAN_AGENT_INTERFACE, "Release"); + + dbus_message_set_no_reply(msg, TRUE); + + dbus_connection_send(connection, msg, NULL); + + dbus_message_unref(msg); + + agent_free(); + + dbus_connection_unref(connection); +} diff --git a/src/connman.h b/src/connman.h index ff33489..703b8c9 100644 --- a/src/connman.h +++ b/src/connman.h @@ -28,6 +28,8 @@ #define CONNMAN_SERVICE "org.freedesktop.connman" +#define CONNMAN_AGENT_INTERFACE CONNMAN_SERVICE ".Agent" + #define CONNMAN_MANAGER_PATH "/" #define CONNMAN_MANAGER_INTERFACE CONNMAN_SERVICE ".Manager" @@ -42,11 +44,11 @@ int __connman_manager_init(DBusConnection *conn, int compat); void __connman_manager_cleanup(void); -int __connman_agent_init(void); +int __connman_agent_init(DBusConnection *conn); void __connman_agent_cleanup(void); -int __connman_agent_register(const char *path); -int __connman_agent_unregister(const char *path); +int __connman_agent_register(const char *sender, const char *path); +int __connman_agent_unregister(const char *sender, const char *path); #include diff --git a/src/main.c b/src/main.c index 1b314db..a54eaf6 100644 --- a/src/main.c +++ b/src/main.c @@ -120,7 +120,7 @@ int main(int argc, char *argv[]) compat = 0; } - __connman_agent_init(); + __connman_agent_init(conn); __connman_manager_init(conn, compat); diff --git a/src/manager.c b/src/manager.c index 2a1a738..11c2557 100644 --- a/src/manager.c +++ b/src/manager.c @@ -74,10 +74,12 @@ static DBusMessage *register_agent(DBusConnection *conn, DBusMessage *msg, void *data) { DBusMessage *reply; - const char *path; + const char *sender, *path; DBG("conn %p", conn); + sender = dbus_message_get_sender(msg); + dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID); @@ -87,7 +89,7 @@ static DBusMessage *register_agent(DBusConnection *conn, dbus_message_append_args(reply, DBUS_TYPE_INVALID); - __connman_agent_register(path); + __connman_agent_register(sender, path); return reply; } @@ -96,10 +98,12 @@ static DBusMessage *unregister_agent(DBusConnection *conn, DBusMessage *msg, void *data) { DBusMessage *reply; - const char *path; + const char *sender, *path; DBG("conn %p", conn); + sender = dbus_message_get_sender(msg); + dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID); @@ -109,7 +113,7 @@ static DBusMessage *unregister_agent(DBusConnection *conn, dbus_message_append_args(reply, DBUS_TYPE_INVALID); - __connman_agent_unregister(path); + __connman_agent_unregister(sender, path); return reply; } -- 2.7.4