Add handling for agent registration and monitoring
[platform/upstream/connman.git] / src / manager.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  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 <gdbus.h>
27
28 #include "connman.h"
29
30 static const char *master_state = "unknown";
31
32 static DBusMessage *list_interfaces(DBusConnection *conn,
33                                         DBusMessage *msg, void *data)
34 {
35         DBusMessage *reply;
36         DBusMessageIter array, iter;
37
38         DBG("conn %p", conn);
39
40         reply = dbus_message_new_method_return(msg);
41         if (reply == NULL)
42                 return NULL;
43
44         dbus_message_iter_init_append(reply, &array);
45
46         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
47                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
48
49         __connman_iface_list(&iter);
50
51         dbus_message_iter_close_container(&array, &iter);
52
53         return reply;
54 }
55
56 static DBusMessage *get_state(DBusConnection *conn,
57                                         DBusMessage *msg, void *data)
58 {
59         DBusMessage *reply;
60
61         DBG("conn %p", conn);
62
63         reply = dbus_message_new_method_return(msg);
64         if (reply == NULL)
65                 return NULL;
66
67         dbus_message_append_args(reply, DBUS_TYPE_STRING, &master_state,
68                                                         DBUS_TYPE_INVALID);
69
70         return reply;
71 }
72
73 static DBusMessage *register_agent(DBusConnection *conn,
74                                         DBusMessage *msg, void *data)
75 {
76         DBusMessage *reply;
77         const char *sender, *path;
78
79         DBG("conn %p", conn);
80
81         sender = dbus_message_get_sender(msg);
82
83         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
84                                                         DBUS_TYPE_INVALID);
85
86         reply = dbus_message_new_method_return(msg);
87         if (reply == NULL)
88                 return NULL;
89
90         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
91
92         __connman_agent_register(sender, path);
93
94         return reply;
95 }
96
97 static DBusMessage *unregister_agent(DBusConnection *conn,
98                                         DBusMessage *msg, void *data)
99 {
100         DBusMessage *reply;
101         const char *sender, *path;
102
103         DBG("conn %p", conn);
104
105         sender = dbus_message_get_sender(msg);
106
107         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
108                                                         DBUS_TYPE_INVALID);
109
110         reply = dbus_message_new_method_return(msg);
111         if (reply == NULL)
112                 return NULL;
113
114         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
115
116         __connman_agent_unregister(sender, path);
117
118         return reply;
119 }
120
121 static GDBusMethodTable manager_methods[] = {
122         { "ListInterfaces",  "",  "ao", list_interfaces  },
123         { "GetState",        "",  "s",  get_state        },
124         { "RegisterAgent",   "o", "",   register_agent   },
125         { "UnregisterAgent", "o", "",   unregister_agent },
126         { },
127 };
128
129 static GDBusSignalTable manager_signals[] = {
130         { "InterfaceAdded",   "o" },
131         { "InterfaceRemoved", "o" },
132         { "StateChanged",     "s" },
133         { },
134 };
135
136 static DBusMessage *activate_device(DBusConnection *conn,
137                                         DBusMessage *msg, void *data)
138 {
139         DBusMessage *reply;
140         const char *path;
141
142         DBG("conn %p", conn);
143
144         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
145                                                         DBUS_TYPE_INVALID);
146
147         DBG("device %s", path);
148
149         reply = dbus_message_new_method_return(msg);
150         if (reply == NULL)
151                 return NULL;
152
153         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
154
155         return reply;
156 }
157
158 static DBusMessage *set_wireless(DBusConnection *conn,
159                                         DBusMessage *msg, void *data)
160 {
161         DBusMessage *reply;
162         dbus_bool_t enabled;
163
164         DBG("conn %p", conn);
165
166         dbus_message_get_args(msg, NULL, DBUS_TYPE_BOOLEAN, &enabled,
167                                                         DBUS_TYPE_INVALID);
168
169         reply = dbus_message_new_method_return(msg);
170         if (reply == NULL)
171                 return NULL;
172
173         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
174
175         return reply;
176 }
177
178 static DBusMessage *get_wireless(DBusConnection *conn,
179                                         DBusMessage *msg, void *data)
180 {
181         DBusMessage *reply;
182         dbus_bool_t enabled = TRUE;
183
184         DBG("conn %p", conn);
185
186         reply = dbus_message_new_method_return(msg);
187         if (reply == NULL)
188                 return NULL;
189
190         dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &enabled,
191                                                         DBUS_TYPE_INVALID);
192
193         return reply;
194 }
195
196 static DBusMessage *do_sleep(DBusConnection *conn,
197                                         DBusMessage *msg, void *data)
198 {
199         DBusMessage *reply;
200
201         DBG("conn %p", conn);
202
203         reply = dbus_message_new_method_return(msg);
204         if (reply == NULL)
205                 return NULL;
206
207         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
208
209         return reply;
210 }
211
212 static DBusMessage *do_wake(DBusConnection *conn,
213                                         DBusMessage *msg, void *data)
214 {
215         DBusMessage *reply;
216
217         DBG("conn %p", conn);
218
219         reply = dbus_message_new_method_return(msg);
220         if (reply == NULL)
221                 return NULL;
222
223         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
224
225         return reply;
226 }
227
228 enum {
229         NM_STATE_UNKNOWN = 0,
230         NM_STATE_ASLEEP,
231         NM_STATE_CONNECTING,
232         NM_STATE_CONNECTED,
233         NM_STATE_DISCONNECTED
234 };
235
236 static DBusMessage *do_state(DBusConnection *conn,
237                                         DBusMessage *msg, void *data)
238 {
239         DBusMessage *reply;
240         dbus_uint32_t state = NM_STATE_DISCONNECTED;
241
242         DBG("conn %p", conn);
243
244         reply = dbus_message_new_method_return(msg);
245         if (reply == NULL)
246                 return NULL;
247
248         dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
249                                                         DBUS_TYPE_INVALID);
250
251         return reply;
252 }
253
254 static GDBusMethodTable nm_methods[] = {
255         { "getDevices",            "",  "ao", list_interfaces },
256         { "setActiveDevice",       "o", "",   activate_device },
257         { "setWirelessEnabled",    "b", "",   set_wireless    },
258         { "getWirelessEnabled",    "",  "b",  get_wireless    },
259         { "sleep",                 "",  "",   do_sleep        },
260         { "wake",                  "",  "",   do_wake         },
261         { "state",                 "",  "u",  do_state        },
262         { },
263 };
264
265 static DBusConnection *connection = NULL;
266 static int nm_compat = 0;
267
268 int __connman_manager_init(DBusConnection *conn, int compat)
269 {
270         DBG("conn %p", conn);
271
272         connection = dbus_connection_ref(conn);
273         if (connection == NULL)
274                 return -1;
275
276         g_dbus_register_object(connection, CONNMAN_MANAGER_PATH, NULL, NULL);
277
278         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
279                                                 CONNMAN_MANAGER_INTERFACE,
280                                                 manager_methods,
281                                                 manager_signals, NULL);
282
283         if (compat) {
284                 g_dbus_register_object(connection, NM_PATH, NULL, NULL);
285
286                 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
287                                                 nm_methods, NULL, NULL);
288
289                 nm_compat = 1;
290         }
291
292         return 0;
293 }
294
295 void __connman_manager_cleanup(void)
296 {
297         DBG("conn %p", connection);
298
299         if (nm_compat) {
300                 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
301
302                 g_dbus_unregister_object(connection, NM_PATH);
303         }
304
305         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
306                                                 CONNMAN_MANAGER_INTERFACE);
307
308         g_dbus_unregister_object(connection, CONNMAN_MANAGER_PATH);
309
310         dbus_connection_unref(connection);
311 }