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
41 #include "bluetooth.h"
48 #define CLIENT_SERVICE "org.bluez.obex.client"
50 #define CLIENT_INTERFACE "org.bluez.obex.Client"
51 #define ERROR_INTERFACE "org.bluez.obex.Error"
52 #define CLIENT_PATH "/"
55 DBusConnection *connection;
59 static GSList *sessions = NULL;
61 static void shutdown_session(struct obc_session *session)
63 sessions = g_slist_remove(sessions, session);
64 obc_session_shutdown(session);
65 obc_session_unref(session);
68 static void unregister_session(void *data)
70 struct obc_session *session = data;
72 if (g_slist_find(sessions, session) == NULL)
75 sessions = g_slist_remove(sessions, session);
76 obc_session_unref(session);
79 static void create_callback(struct obc_session *session,
80 struct obc_transfer *transfer,
81 GError *err, void *user_data)
83 struct send_data *data = user_data;
87 DBusMessage *error = g_dbus_create_error(data->message,
88 ERROR_INTERFACE ".Failed",
90 g_dbus_send_message(data->connection, error);
91 shutdown_session(session);
96 path = obc_session_register(session, unregister_session);
98 g_dbus_send_reply(data->connection, data->message,
99 DBUS_TYPE_OBJECT_PATH, &path,
103 dbus_message_unref(data->message);
104 dbus_connection_unref(data->connection);
108 static int parse_device_dict(DBusMessageIter *iter,
109 const char **source, const char **target, uint8_t *channel)
111 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_DICT_ENTRY) {
112 DBusMessageIter entry, value;
115 dbus_message_iter_recurse(iter, &entry);
116 dbus_message_iter_get_basic(&entry, &key);
118 dbus_message_iter_next(&entry);
119 dbus_message_iter_recurse(&entry, &value);
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);
129 if (g_str_equal(key, "Channel") == TRUE)
130 dbus_message_iter_get_basic(&value, channel);
134 dbus_message_iter_next(iter);
140 static struct obc_session *find_session(const char *path)
144 for (l = sessions; l; l = l->next) {
145 struct obc_session *session = l->data;
147 if (g_str_equal(obc_session_get_path(session), path) == TRUE)
154 static DBusMessage *create_session(DBusConnection *connection,
155 DBusMessage *message, void *user_data)
157 DBusMessageIter iter, dict;
158 struct obc_session *session;
159 struct send_data *data;
160 const char *source = NULL, *dest = NULL, *target = NULL;
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);
168 dbus_message_iter_get_basic(&iter, &dest);
169 dbus_message_iter_next(&iter);
171 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
172 return g_dbus_create_error(message,
173 ERROR_INTERFACE ".InvalidArguments", NULL);
175 dbus_message_iter_recurse(&iter, &dict);
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);
182 data = g_try_malloc0(sizeof(*data));
184 return g_dbus_create_error(message,
185 ERROR_INTERFACE ".Error.NoMemory", NULL);
187 data->connection = dbus_connection_ref(connection);
188 data->message = dbus_message_ref(message);
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);
198 dbus_message_unref(data->message);
199 dbus_connection_unref(data->connection);
202 return g_dbus_create_error(message, ERROR_INTERFACE ".Failed", NULL);
205 static DBusMessage *remove_session(DBusConnection *connection,
206 DBusMessage *message, void *user_data)
208 struct obc_session *session;
209 const gchar *sender, *path;
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);
217 session = find_session(path);
219 return g_dbus_create_error(message,
220 ERROR_INTERFACE ".InvalidArguments", NULL);
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",
228 shutdown_session(session);
230 return dbus_message_new_method_return(message);
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) },
242 static DBusConnection *conn = NULL;
244 static struct obc_module {
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 },
258 int manager_init(void)
261 struct obc_module *module;
263 dbus_error_init(&derr);
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);
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);
281 for (module = modules; module && module->init; module++) {
282 if (module->init() < 0)
285 DBG("Module %s loaded", module->name);
291 void manager_exit(void)
293 struct obc_module *module;
298 for (module = modules; module && module->exit; module++)
301 g_dbus_unregister_interface(conn, CLIENT_PATH, CLIENT_INTERFACE);
302 dbus_connection_unref(conn);