5 * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
36 #include "gdbus/gdbus.h"
38 #include "obexd/src/log.h"
39 #include "obexd/src/manager.h"
42 #include "bluetooth.h"
49 #ifdef __TIZEN_PATCH__
50 #include "mns-tizen.h"
52 #define CLIENT_INTERFACE "org.bluez.obex.Client1"
53 #define ERROR_INTERFACE "org.bluez.obex.Error"
54 #define CLIENT_PATH "/org/bluez/obex"
57 DBusConnection *connection;
61 static GSList *sessions = NULL;
63 static void shutdown_session(struct obc_session *session)
65 obc_session_shutdown(session);
66 obc_session_unref(session);
69 #ifdef __TIZEN_PATCH__
70 void release_session(struct obc_session *session)
73 sessions = g_slist_remove(sessions, session);
74 shutdown_session(session);
78 static void release_session(struct obc_session *session)
80 sessions = g_slist_remove(sessions, session);
81 shutdown_session(session);
85 static void unregister_session(void *data)
87 struct obc_session *session = data;
89 if (g_slist_find(sessions, session) == NULL)
92 sessions = g_slist_remove(sessions, session);
93 obc_session_unref(session);
96 static void create_callback(struct obc_session *session,
97 struct obc_transfer *transfer,
98 GError *err, void *user_data)
100 struct send_data *data = user_data;
104 DBusMessage *error = g_dbus_create_error(data->message,
105 ERROR_INTERFACE ".Failed",
107 g_dbus_send_message(data->connection, error);
108 shutdown_session(session);
113 path = obc_session_register(session, unregister_session);
115 DBusMessage *error = g_dbus_create_error(data->message,
116 ERROR_INTERFACE ".Failed",
118 g_dbus_send_message(data->connection, error);
119 shutdown_session(session);
123 sessions = g_slist_append(sessions, session);
124 g_dbus_send_reply(data->connection, data->message,
125 DBUS_TYPE_OBJECT_PATH, &path,
129 dbus_message_unref(data->message);
130 dbus_connection_unref(data->connection);
134 static int parse_device_dict(DBusMessageIter *iter,
135 const char **source, const char **target, uint8_t *channel)
137 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_DICT_ENTRY) {
138 DBusMessageIter entry, value;
141 dbus_message_iter_recurse(iter, &entry);
142 dbus_message_iter_get_basic(&entry, &key);
144 dbus_message_iter_next(&entry);
145 dbus_message_iter_recurse(&entry, &value);
147 switch (dbus_message_iter_get_arg_type(&value)) {
148 case DBUS_TYPE_STRING:
149 if (g_str_equal(key, "Source") == TRUE)
150 dbus_message_iter_get_basic(&value, source);
151 else if (g_str_equal(key, "Target") == TRUE)
152 dbus_message_iter_get_basic(&value, target);
155 if (g_str_equal(key, "Channel") == TRUE)
156 dbus_message_iter_get_basic(&value, channel);
160 dbus_message_iter_next(iter);
166 static struct obc_session *find_session(const char *path)
169 #ifdef __TIZEN_PATCH__
173 for (l = sessions; l; l = l->next) {
174 struct obc_session *session = l->data;
176 if (g_strcmp0(obc_session_get_path(session), path) == 0)
183 static DBusMessage *create_session(DBusConnection *connection,
184 DBusMessage *message, void *user_data)
186 DBusMessageIter iter, dict;
187 struct obc_session *session;
188 struct send_data *data;
189 const char *source = NULL, *dest = NULL, *target = NULL;
192 dbus_message_iter_init(message, &iter);
193 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
194 return g_dbus_create_error(message,
195 ERROR_INTERFACE ".InvalidArguments", NULL);
197 dbus_message_iter_get_basic(&iter, &dest);
198 dbus_message_iter_next(&iter);
200 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
201 return g_dbus_create_error(message,
202 ERROR_INTERFACE ".InvalidArguments", NULL);
204 dbus_message_iter_recurse(&iter, &dict);
206 parse_device_dict(&dict, &source, &target, &channel);
207 if (dest == NULL || target == NULL)
208 return g_dbus_create_error(message,
209 ERROR_INTERFACE ".InvalidArguments", NULL);
211 data = g_try_malloc0(sizeof(*data));
213 return g_dbus_create_error(message,
214 ERROR_INTERFACE ".Error.NoMemory", NULL);
216 data->connection = dbus_connection_ref(connection);
217 data->message = dbus_message_ref(message);
219 session = obc_session_create(source, dest, target, channel,
220 dbus_message_get_sender(message),
221 create_callback, data);
222 if (session != NULL) {
226 dbus_message_unref(data->message);
227 dbus_connection_unref(data->connection);
230 return g_dbus_create_error(message, ERROR_INTERFACE ".Failed", NULL);
233 static DBusMessage *remove_session(DBusConnection *connection,
234 DBusMessage *message, void *user_data)
236 struct obc_session *session;
237 const char *sender, *path;
239 if (dbus_message_get_args(message, NULL,
240 DBUS_TYPE_OBJECT_PATH, &path,
241 DBUS_TYPE_INVALID) == FALSE)
242 return g_dbus_create_error(message,
243 ERROR_INTERFACE ".InvalidArguments", NULL);
245 session = find_session(path);
247 return g_dbus_create_error(message,
248 ERROR_INTERFACE ".InvalidArguments", NULL);
250 sender = dbus_message_get_sender(message);
251 if (g_str_equal(sender, obc_session_get_owner(session)) == FALSE)
252 return g_dbus_create_error(message,
253 ERROR_INTERFACE ".NotAuthorized",
256 release_session(session);
258 return dbus_message_new_method_return(message);
261 static const GDBusMethodTable client_methods[] = {
262 { GDBUS_ASYNC_METHOD("CreateSession",
263 GDBUS_ARGS({ "destination", "s" }, { "args", "a{sv}" }),
264 GDBUS_ARGS({ "session", "o" }), create_session) },
265 { GDBUS_ASYNC_METHOD("RemoveSession",
266 GDBUS_ARGS({ "session", "o" }), NULL, remove_session) },
270 static DBusConnection *conn = NULL;
272 static struct obc_module {
277 { "bluetooth", bluetooth_init, bluetooth_exit },
278 { "opp", opp_init, opp_exit },
279 { "ftp", ftp_init, ftp_exit },
280 { "pbap", pbap_init, pbap_exit },
281 { "sync", sync_init, sync_exit },
282 { "map", map_init, map_exit },
283 #ifdef __TIZEN_PATCH__
284 { "mns", mns_init, mns_exit },
289 int client_manager_init(void)
292 struct obc_module *module;
294 dbus_error_init(&derr);
296 conn = manager_dbus_get_connection();
298 error("Can't get client D-Bus connection");
302 if (g_dbus_register_interface(conn, CLIENT_PATH, CLIENT_INTERFACE,
303 client_methods, NULL, NULL,
304 NULL, NULL) == FALSE) {
305 error("Can't register client interface");
306 dbus_connection_unref(conn);
311 for (module = modules; module && module->init; module++) {
312 if (module->init() < 0)
315 DBG("Module %s loaded", module->name);
321 void client_manager_exit(void)
323 struct obc_module *module;
328 for (module = modules; module && module->exit; module++)
331 g_dbus_unregister_interface(conn, CLIENT_PATH, CLIENT_INTERFACE);
333 dbus_connection_unref(conn);