Update copyright information
[framework/connectivity/connman.git] / src / agent.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <gdbus.h>
31
32 #include "connman.h"
33
34 static DBusConnection *connection = NULL;
35 static guint agent_watch = 0;
36 static gchar *agent_path = NULL;
37 static gchar *agent_sender = NULL;
38
39 static void agent_free(void)
40 {
41         agent_watch = 0;
42
43         g_free(agent_sender);
44         agent_sender = NULL;
45
46         g_free(agent_path);
47         agent_path = NULL;
48 }
49
50 static void agent_disconnect(DBusConnection *connection, void *data)
51 {
52         DBG("data %p", data);
53
54         agent_free();
55 }
56
57 int __connman_agent_register(const char *sender, const char *path)
58 {
59         DBG("sender %s path %s", sender, path);
60
61         if (agent_path != NULL)
62                 return -EEXIST;
63
64         agent_sender = g_strdup(sender);
65         agent_path = g_strdup(path);
66
67         agent_watch = g_dbus_add_disconnect_watch(connection, sender,
68                                                 agent_disconnect, NULL, NULL);
69
70         return 0;
71 }
72
73 int __connman_agent_unregister(const char *sender, const char *path)
74 {
75         DBG("sender %s path %s", sender, path);
76
77         if (agent_path == NULL)
78                 return -ESRCH;
79
80         if (agent_watch > 0)
81                 g_dbus_remove_watch(connection, agent_watch);
82
83         agent_free();
84
85         return 0;
86 }
87
88 int __connman_agent_request_passphrase(struct connman_service *service,
89                                 passphrase_cb_t callback, void *user_data)
90 {
91         DBusMessage *message;
92         const char *path;
93
94         DBG("service %p", service);
95
96         if (agent_path == NULL)
97                 return -ESRCH;
98
99         message = dbus_message_new_method_call(agent_sender, agent_path,
100                                 CONNMAN_AGENT_INTERFACE, "RequestPassphrase");
101         if (message == NULL)
102                 return -ENOMEM;
103
104         dbus_message_set_no_reply(message, TRUE);
105
106         path = __connman_service_get_path(service);
107
108         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &path,
109                                                         DBUS_TYPE_INVALID);
110
111         g_dbus_send_message(connection, message);
112
113         return -EIO;
114 }
115
116 int __connman_agent_init(void)
117 {
118         DBG("");
119
120         connection = connman_dbus_get_connection();
121         if (connection == NULL)
122                 return -1;
123
124         return 0;
125 }
126
127 void __connman_agent_cleanup(void)
128 {
129         DBusMessage *message;
130
131         DBG("");
132
133         if (connection == NULL)
134                 return;
135
136         if (agent_watch > 0)
137                 g_dbus_remove_watch(connection, agent_watch);
138
139         if (agent_path == NULL)
140                 return;
141
142         message = dbus_message_new_method_call(agent_sender, agent_path,
143                                         CONNMAN_AGENT_INTERFACE, "Release");
144         if (message == NULL)
145                 return;
146
147         dbus_message_set_no_reply(message, TRUE);
148
149         g_dbus_send_message(connection, message);
150
151         agent_free();
152
153         dbus_connection_unref(connection);
154 }