upgrade obexd to 0.47
[profile/ivi/obexd.git] / client / manager.c
1 /*
2  *
3  *  OBEX Client
4  *
5  *  Copyright (C) 2007-2010  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <syslog.h>
33
34 #include <glib.h>
35 #include <gdbus.h>
36
37 #include "log.h"
38 #include "transfer.h"
39 #include "session.h"
40 #include "manager.h"
41 #include "bluetooth.h"
42 #include "opp.h"
43 #include "ftp.h"
44 #include "pbap.h"
45 #include "sync.h"
46 #include "map.h"
47
48 #define CLIENT_SERVICE          "org.bluez.obex.client"
49
50 #define CLIENT_INTERFACE        "org.bluez.obex.Client"
51 #define ERROR_INTERFACE         "org.bluez.obex.Error"
52 #define CLIENT_PATH             "/"
53
54 struct send_data {
55         DBusConnection *connection;
56         DBusMessage *message;
57 };
58
59 static GSList *sessions = NULL;
60
61 static void shutdown_session(struct obc_session *session)
62 {
63         sessions = g_slist_remove(sessions, session);
64         obc_session_shutdown(session);
65         obc_session_unref(session);
66 }
67
68 static void unregister_session(void *data)
69 {
70         struct obc_session *session = data;
71
72         if (g_slist_find(sessions, session) == NULL)
73                 return;
74
75         sessions = g_slist_remove(sessions, session);
76         obc_session_unref(session);
77 }
78
79 static void create_callback(struct obc_session *session,
80                                                 struct obc_transfer *transfer,
81                                                 GError *err, void *user_data)
82 {
83         struct send_data *data = user_data;
84         const char *path;
85
86         if (err != NULL) {
87                 DBusMessage *error = g_dbus_create_error(data->message,
88                                         ERROR_INTERFACE ".Failed",
89                                         "%s", err->message);
90                 g_dbus_send_message(data->connection, error);
91                 shutdown_session(session);
92                 goto done;
93         }
94
95
96         path = obc_session_register(session, unregister_session);
97
98         g_dbus_send_reply(data->connection, data->message,
99                                 DBUS_TYPE_OBJECT_PATH, &path,
100                                 DBUS_TYPE_INVALID);
101
102 done:
103         dbus_message_unref(data->message);
104         dbus_connection_unref(data->connection);
105         g_free(data);
106 }
107
108 static int parse_device_dict(DBusMessageIter *iter,
109                 const char **source, const char **target, uint8_t *channel)
110 {
111         while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_DICT_ENTRY) {
112                 DBusMessageIter entry, value;
113                 const char *key;
114
115                 dbus_message_iter_recurse(iter, &entry);
116                 dbus_message_iter_get_basic(&entry, &key);
117
118                 dbus_message_iter_next(&entry);
119                 dbus_message_iter_recurse(&entry, &value);
120
121                 switch (dbus_message_iter_get_arg_type(&value)) {
122                 case DBUS_TYPE_STRING:
123                         if (g_str_equal(key, "Source") == TRUE)
124                                 dbus_message_iter_get_basic(&value, source);
125                         else if (g_str_equal(key, "Target") == TRUE)
126                                 dbus_message_iter_get_basic(&value, target);
127                         break;
128                 case DBUS_TYPE_BYTE:
129                         if (g_str_equal(key, "Channel") == TRUE)
130                                 dbus_message_iter_get_basic(&value, channel);
131                         break;
132                 }
133
134                 dbus_message_iter_next(iter);
135         }
136
137         return 0;
138 }
139
140 static struct obc_session *find_session(const char *path)
141 {
142         GSList *l;
143
144         for (l = sessions; l; l = l->next) {
145                 struct obc_session *session = l->data;
146
147                 if (g_str_equal(obc_session_get_path(session), path) == TRUE)
148                         return session;
149         }
150
151         return NULL;
152 }
153
154 static DBusMessage *create_session(DBusConnection *connection,
155                                         DBusMessage *message, void *user_data)
156 {
157         DBusMessageIter iter, dict;
158         struct obc_session *session;
159         struct send_data *data;
160         const char *source = NULL, *dest = NULL, *target = NULL;
161         uint8_t channel = 0;
162
163         dbus_message_iter_init(message, &iter);
164         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
165                 return g_dbus_create_error(message,
166                                 ERROR_INTERFACE ".InvalidArguments", NULL);
167
168         dbus_message_iter_get_basic(&iter, &dest);
169         dbus_message_iter_next(&iter);
170
171         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
172                 return g_dbus_create_error(message,
173                                 ERROR_INTERFACE ".InvalidArguments", NULL);
174
175         dbus_message_iter_recurse(&iter, &dict);
176
177         parse_device_dict(&dict, &source, &target, &channel);
178         if (dest == NULL || target == NULL)
179                 return g_dbus_create_error(message,
180                                 ERROR_INTERFACE ".InvalidArguments", NULL);
181
182         data = g_try_malloc0(sizeof(*data));
183         if (data == NULL)
184                 return g_dbus_create_error(message,
185                                 ERROR_INTERFACE ".Error.NoMemory", NULL);
186
187         data->connection = dbus_connection_ref(connection);
188         data->message = dbus_message_ref(message);
189
190         session = obc_session_create(source, dest, target, channel,
191                                         dbus_message_get_sender(message),
192                                         create_callback, data);
193         if (session != NULL) {
194                 sessions = g_slist_append(sessions, session);
195                 return NULL;
196         }
197
198         dbus_message_unref(data->message);
199         dbus_connection_unref(data->connection);
200         g_free(data);
201
202         return g_dbus_create_error(message, ERROR_INTERFACE ".Failed", NULL);
203 }
204
205 static DBusMessage *remove_session(DBusConnection *connection,
206                                 DBusMessage *message, void *user_data)
207 {
208         struct obc_session *session;
209         const gchar *sender, *path;
210
211         if (dbus_message_get_args(message, NULL,
212                         DBUS_TYPE_OBJECT_PATH, &path,
213                         DBUS_TYPE_INVALID) == FALSE)
214                 return g_dbus_create_error(message,
215                                 ERROR_INTERFACE ".InvalidArguments", NULL);
216
217         session = find_session(path);
218         if (session == NULL)
219                 return g_dbus_create_error(message,
220                                 ERROR_INTERFACE ".InvalidArguments", NULL);
221
222         sender = dbus_message_get_sender(message);
223         if (g_str_equal(sender, obc_session_get_owner(session)) == FALSE)
224                 return g_dbus_create_error(message,
225                                 ERROR_INTERFACE ".NotAuthorized",
226                                 "Not Authorized");
227
228         shutdown_session(session);
229
230         return dbus_message_new_method_return(message);
231 }
232
233 static const GDBusMethodTable client_methods[] = {
234         { GDBUS_ASYNC_METHOD("CreateSession",
235                         GDBUS_ARGS({ "destination", "s" }, { "args", "a{sv}" }),
236                         GDBUS_ARGS({ "session", "o" }), create_session) },
237         { GDBUS_ASYNC_METHOD("RemoveSession",
238                         GDBUS_ARGS({ "session", "o" }), NULL, remove_session) },
239         { }
240 };
241
242 static DBusConnection *conn = NULL;
243
244 static struct obc_module {
245         const char *name;
246         int (*init) (void);
247         void (*exit) (void);
248 } modules[] = {
249         { "bluetooth", bluetooth_init, bluetooth_exit },
250         { "opp", opp_init, opp_exit },
251         { "ftp", ftp_init, ftp_exit },
252         { "pbap", pbap_init, pbap_exit },
253         { "sync", sync_init, sync_exit },
254         { "map", map_init, map_exit },
255         { }
256 };
257
258 int manager_init(void)
259 {
260         DBusError derr;
261         struct obc_module *module;
262
263         dbus_error_init(&derr);
264
265         conn = g_dbus_setup_bus(DBUS_BUS_SESSION, CLIENT_SERVICE, &derr);
266         if (dbus_error_is_set(&derr) == TRUE) {
267                 error("%s: %s", derr.name, derr.message);
268                 dbus_error_free(&derr);
269                 return -1;
270         }
271
272         if (g_dbus_register_interface(conn, CLIENT_PATH, CLIENT_INTERFACE,
273                                                 client_methods, NULL, NULL,
274                                                         NULL, NULL) == FALSE) {
275                 error("Can't register client interface");
276                 dbus_connection_unref(conn);
277                 conn = NULL;
278                 return -1;
279         }
280
281         for (module = modules; module && module->init; module++) {
282                 if (module->init() < 0)
283                         continue;
284
285                 DBG("Module %s loaded", module->name);
286         }
287
288         return 0;
289 }
290
291 void manager_exit(void)
292 {
293         struct obc_module *module;
294
295         if (conn == NULL)
296                 return;
297
298         for (module = modules; module && module->exit; module++)
299                 module->exit();
300
301         g_dbus_unregister_interface(conn, CLIENT_PATH, CLIENT_INTERFACE);
302         dbus_connection_unref(conn);
303 }