Add skeleton for agent infrastructure
[connman] / 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 *path;
78
79         DBG("conn %p", conn);
80
81         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
82                                                         DBUS_TYPE_INVALID);
83
84         reply = dbus_message_new_method_return(msg);
85         if (reply == NULL)
86                 return NULL;
87
88         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
89
90         __connman_agent_register(path);
91
92         return reply;
93 }
94
95 static DBusMessage *unregister_agent(DBusConnection *conn,
96                                         DBusMessage *msg, void *data)
97 {
98         DBusMessage *reply;
99         const char *path;
100
101         DBG("conn %p", conn);
102
103         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
104                                                         DBUS_TYPE_INVALID);
105
106         reply = dbus_message_new_method_return(msg);
107         if (reply == NULL)
108                 return NULL;
109
110         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
111
112         __connman_agent_unregister(path);
113
114         return reply;
115 }
116
117 static GDBusMethodTable manager_methods[] = {
118         { "ListInterfaces",  "",  "ao", list_interfaces  },
119         { "GetState",        "",  "s",  get_state        },
120         { "RegisterAgent",   "o", "",   register_agent   },
121         { "UnregisterAgent", "o", "",   unregister_agent },
122         { },
123 };
124
125 static GDBusSignalTable manager_signals[] = {
126         { "InterfaceAdded",   "o" },
127         { "InterfaceRemoved", "o" },
128         { "StateChanged",     "s" },
129         { },
130 };
131
132 static DBusMessage *activate_device(DBusConnection *conn,
133                                         DBusMessage *msg, void *data)
134 {
135         DBusMessage *reply;
136         const char *path;
137
138         DBG("conn %p", conn);
139
140         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
141                                                         DBUS_TYPE_INVALID);
142
143         DBG("device %s", path);
144
145         reply = dbus_message_new_method_return(msg);
146         if (reply == NULL)
147                 return NULL;
148
149         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
150
151         return reply;
152 }
153
154 static DBusMessage *set_wireless(DBusConnection *conn,
155                                         DBusMessage *msg, void *data)
156 {
157         DBusMessage *reply;
158         dbus_bool_t enabled;
159
160         DBG("conn %p", conn);
161
162         dbus_message_get_args(msg, NULL, DBUS_TYPE_BOOLEAN, &enabled,
163                                                         DBUS_TYPE_INVALID);
164
165         reply = dbus_message_new_method_return(msg);
166         if (reply == NULL)
167                 return NULL;
168
169         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
170
171         return reply;
172 }
173
174 static DBusMessage *get_wireless(DBusConnection *conn,
175                                         DBusMessage *msg, void *data)
176 {
177         DBusMessage *reply;
178         dbus_bool_t enabled = TRUE;
179
180         DBG("conn %p", conn);
181
182         reply = dbus_message_new_method_return(msg);
183         if (reply == NULL)
184                 return NULL;
185
186         dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &enabled,
187                                                         DBUS_TYPE_INVALID);
188
189         return reply;
190 }
191
192 static DBusMessage *do_sleep(DBusConnection *conn,
193                                         DBusMessage *msg, void *data)
194 {
195         DBusMessage *reply;
196
197         DBG("conn %p", conn);
198
199         reply = dbus_message_new_method_return(msg);
200         if (reply == NULL)
201                 return NULL;
202
203         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
204
205         return reply;
206 }
207
208 static DBusMessage *do_wake(DBusConnection *conn,
209                                         DBusMessage *msg, void *data)
210 {
211         DBusMessage *reply;
212
213         DBG("conn %p", conn);
214
215         reply = dbus_message_new_method_return(msg);
216         if (reply == NULL)
217                 return NULL;
218
219         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
220
221         return reply;
222 }
223
224 enum {
225         NM_STATE_UNKNOWN = 0,
226         NM_STATE_ASLEEP,
227         NM_STATE_CONNECTING,
228         NM_STATE_CONNECTED,
229         NM_STATE_DISCONNECTED
230 };
231
232 static DBusMessage *do_state(DBusConnection *conn,
233                                         DBusMessage *msg, void *data)
234 {
235         DBusMessage *reply;
236         dbus_uint32_t state = NM_STATE_DISCONNECTED;
237
238         DBG("conn %p", conn);
239
240         reply = dbus_message_new_method_return(msg);
241         if (reply == NULL)
242                 return NULL;
243
244         dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
245                                                         DBUS_TYPE_INVALID);
246
247         return reply;
248 }
249
250 static GDBusMethodTable nm_methods[] = {
251         { "getDevices",            "",  "ao", list_interfaces },
252         { "setActiveDevice",       "o", "",   activate_device },
253         { "setWirelessEnabled",    "b", "",   set_wireless    },
254         { "getWirelessEnabled",    "",  "b",  get_wireless    },
255         { "sleep",                 "",  "",   do_sleep        },
256         { "wake",                  "",  "",   do_wake         },
257         { "state",                 "",  "u",  do_state        },
258         { },
259 };
260
261 static DBusConnection *connection = NULL;
262 static int nm_compat = 0;
263
264 int __connman_manager_init(DBusConnection *conn, int compat)
265 {
266         DBG("conn %p", conn);
267
268         connection = dbus_connection_ref(conn);
269         if (connection == NULL)
270                 return -1;
271
272         g_dbus_register_object(connection, CONNMAN_MANAGER_PATH, NULL, NULL);
273
274         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
275                                                 CONNMAN_MANAGER_INTERFACE,
276                                                 manager_methods,
277                                                 manager_signals, NULL);
278
279         if (compat) {
280                 g_dbus_register_object(connection, NM_PATH, NULL, NULL);
281
282                 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
283                                                 nm_methods, NULL, NULL);
284
285                 nm_compat = 1;
286         }
287
288         return 0;
289 }
290
291 void __connman_manager_cleanup(void)
292 {
293         DBG("conn %p", connection);
294
295         if (nm_compat) {
296                 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
297
298                 g_dbus_unregister_object(connection, NM_PATH);
299         }
300
301         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
302                                                 CONNMAN_MANAGER_INTERFACE);
303
304         g_dbus_unregister_object(connection, CONNMAN_MANAGER_PATH);
305
306         dbus_connection_unref(connection);
307 }