5 * Copyright (C) 2007-2010 Nokia Corporation
6 * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
41 #include "transport.h"
45 #include "glib-helper.h"
47 #define BT_RX_MTU 32767
48 #define BT_TX_MTU 32767
50 #define TIMEOUT 60*1000 /* Timeout for user response (miliseconds) */
52 struct pending_request {
53 DBusPendingCall *call;
54 struct bluetooth_service *service;
61 struct bluetooth_service {
62 struct obex_server *server;
63 struct obex_service_driver *driver;
68 char *path; /* Adapter ANY path */
69 GSList *services; /* List of services to register records */
72 static DBusConnection *connection = NULL;
73 static struct adapter_any *any = NULL;
75 static void add_record_reply(DBusPendingCall *call, void *user_data)
77 struct bluetooth_service *service = user_data;
78 DBusMessage *reply = dbus_pending_call_steal_reply(call);
82 dbus_error_init(&derr);
83 if (dbus_set_error_from_message(&derr, reply)) {
84 error("bluetooth: Replied with an error: %s, %s",
85 derr.name, derr.message);
86 dbus_error_free(&derr);
89 dbus_message_get_args(reply, NULL,
90 DBUS_TYPE_UINT32, &handle,
93 service->handle = handle;
95 DBG("Registered: %s, handle: 0x%x",
96 service->driver->name, service->handle);
99 dbus_message_unref(reply);
102 static int add_record(const char *path, const char *xml,
103 struct bluetooth_service *service)
106 DBusPendingCall *call;
109 msg = dbus_message_new_method_call("org.bluez", path,
110 "org.bluez.Service", "AddRecord");
112 dbus_message_append_args(msg, DBUS_TYPE_STRING, &xml,
115 if (dbus_connection_send_with_reply(connection,
116 msg, &call, -1) == FALSE) {
121 dbus_pending_call_set_notify(call, add_record_reply, service, NULL);
122 dbus_pending_call_unref(call);
125 dbus_message_unref(msg);
129 static struct bluetooth_service *find_service(
130 struct obex_service_driver *driver)
134 for (l = any->services; l; l = l->next) {
135 struct bluetooth_service *service = l->data;
137 if (service->driver == driver)
144 static void register_record(struct obex_server *server)
148 if (connection == NULL)
151 for (l = server->drivers; l; l = l->next) {
152 struct obex_service_driver *driver = l->data;
153 struct bluetooth_service *service;
156 service = find_service(driver);
157 if (service == NULL) {
158 service = g_new0(struct bluetooth_service, 1);
159 service->driver = driver;
160 service->server = server;
161 any->services = g_slist_append(any->services, service);
164 /* Service already has a record registered */
165 if (service->handle != 0)
168 /* Adapter ANY is not available yet: Add record later */
169 if (any->path == NULL)
172 xml = g_markup_printf_escaped(driver->record, driver->channel,
174 add_record(any->path, xml, service);
179 static void find_adapter_any_reply(DBusPendingCall *call, void *user_data)
181 DBusMessage *reply = dbus_pending_call_steal_reply(call);
186 dbus_error_init(&derr);
187 if (dbus_set_error_from_message(&derr, reply)) {
188 error("bluetooth: Replied with an error: %s, %s",
189 derr.name, derr.message);
190 dbus_error_free(&derr);
194 dbus_message_get_args(reply, NULL,
195 DBUS_TYPE_OBJECT_PATH, &path,
197 any->path = g_strdup(path);
199 for (l = any->services; l; l = l->next) {
200 struct bluetooth_service *service = l->data;
203 xml = g_markup_printf_escaped(service->driver->record,
204 service->driver->channel,
205 service->driver->name);
206 add_record(any->path, xml, service);
211 dbus_message_unref(reply);
214 static DBusPendingCall *find_adapter(const char *pattern,
215 DBusPendingCallNotifyFunction function,
219 DBusPendingCall *call;
221 DBG("FindAdapter(%s)", pattern);
223 msg = dbus_message_new_method_call("org.bluez", "/",
224 "org.bluez.Manager", "FindAdapter");
228 dbus_message_append_args(msg, DBUS_TYPE_STRING, &pattern,
231 if (!dbus_connection_send_with_reply(connection, msg, &call, -1)) {
232 dbus_message_unref(msg);
236 dbus_pending_call_set_notify(call, function, user_data, NULL);
238 dbus_message_unref(msg);
243 static void name_acquired(DBusConnection *conn, void *user_data)
245 DBusPendingCall *call;
247 call = find_adapter("any", find_adapter_any_reply, NULL);
249 dbus_pending_call_unref(call);
252 static void name_released(DBusConnection *conn, void *user_data)
256 /* reset handles so the services got register next time */
257 for (l = any->services; l; l = l->next) {
258 struct bluetooth_service *service = l->data;
268 static void service_cancel(struct pending_request *pending)
272 msg = dbus_message_new_method_call("org.bluez",
273 pending->adapter_path,
275 "CancelAuthorization");
277 g_dbus_send_message(connection, msg);
280 static void pending_request_free(struct pending_request *pending)
283 dbus_pending_call_unref(pending->call);
284 g_io_channel_unref(pending->io);
285 g_free(pending->adapter_path);
289 static void connect_event(GIOChannel *io, GError *err, void *user_data)
291 struct bluetooth_service *service = user_data;
292 struct obex_server *server = service->server;
297 if (obex_server_new_connection(server, io, BT_TX_MTU, BT_RX_MTU) < 0)
298 g_io_channel_shutdown(io, TRUE, NULL);
303 error("%s", err->message);
304 g_io_channel_shutdown(io, TRUE, NULL);
308 static void service_reply(DBusPendingCall *call, void *user_data)
310 struct pending_request *pending = user_data;
311 GIOChannel *io = pending->io;
312 struct bluetooth_service *service = pending->service;
313 DBusMessage *reply = dbus_pending_call_steal_reply(call);
317 dbus_error_init(&derr);
318 if (dbus_set_error_from_message(&derr, reply)) {
319 error("bluetooth: RequestAuthorization error: %s, %s",
320 derr.name, derr.message);
322 if (dbus_error_has_name(&derr, DBUS_ERROR_NO_REPLY))
323 service_cancel(pending);
325 dbus_error_free(&derr);
326 g_io_channel_shutdown(io, TRUE, NULL);
330 DBG("RequestAuthorization succeeded");
332 if (!bt_io_accept(io, connect_event, service, NULL, &err)) {
333 error("%s", err->message);
335 g_io_channel_shutdown(io, TRUE, NULL);
339 g_source_remove(pending->watch);
340 pending_request_free(pending);
341 dbus_message_unref(reply);
344 static gboolean service_error(GIOChannel *io, GIOCondition cond,
347 struct pending_request *pending = user_data;
349 service_cancel(pending);
351 dbus_pending_call_cancel(pending->call);
353 pending_request_free(pending);
358 static void find_adapter_reply(DBusPendingCall *call, void *user_data)
360 struct pending_request *pending = user_data;
361 DBusMessage *reply = dbus_pending_call_steal_reply(call);
363 DBusPendingCall *pcall;
364 const char *path, *paddr = pending->address;
367 dbus_error_init(&derr);
368 if (dbus_set_error_from_message(&derr, reply)) {
369 error("Replied with an error: %s, %s",
370 derr.name, derr.message);
371 dbus_error_free(&derr);
375 dbus_message_get_args(reply, NULL,
376 DBUS_TYPE_OBJECT_PATH, &path,
379 DBG("FindAdapter -> %s", path);
380 pending->adapter_path = g_strdup(path);
381 dbus_message_unref(reply);
383 msg = dbus_message_new_method_call("org.bluez", path,
384 "org.bluez.Service", "RequestAuthorization");
386 dbus_message_append_args(msg, DBUS_TYPE_STRING, &paddr,
387 DBUS_TYPE_UINT32, &pending->service->handle,
390 if (!dbus_connection_send_with_reply(connection,
391 msg, &pcall, TIMEOUT)) {
392 dbus_message_unref(msg);
396 dbus_message_unref(msg);
398 DBG("RequestAuthorization(%s, %x)", paddr,
399 pending->service->handle);
401 if (!dbus_pending_call_set_notify(pcall, service_reply, pending,
403 dbus_pending_call_unref(pcall);
407 dbus_pending_call_unref(pending->call);
408 pending->call = pcall;
410 /* Catches errors before authorization response comes */
411 pending->watch = g_io_add_watch(pending->io,
412 G_IO_HUP | G_IO_ERR | G_IO_NVAL,
413 service_error, pending);
418 g_io_channel_shutdown(pending->io, TRUE, NULL);
419 pending_request_free(pending);
422 static int request_service_authorization(struct bluetooth_service *service,
423 GIOChannel *io, const char *address)
425 struct pending_request *pending;
429 if (connection == NULL || any->path == NULL)
432 bt_io_get(io, BT_IO_RFCOMM, &err,
433 BT_IO_OPT_SOURCE, source,
436 error("%s", err->message);
441 pending = g_new0(struct pending_request, 1);
442 pending->call = find_adapter(source, find_adapter_reply, pending);
443 if (!pending->call) {
448 pending->service = service;
449 pending->io = g_io_channel_ref(io);
450 memcpy(pending->address, address, sizeof(pending->address));
455 static void confirm_event(GIOChannel *io, void *user_data)
457 struct bluetooth_service *service;
461 struct obex_service_driver *driver = user_data;
463 bt_io_get(io, BT_IO_RFCOMM, &err,
464 BT_IO_OPT_DEST, address,
465 BT_IO_OPT_CHANNEL, &channel,
468 error("%s", err->message);
473 info("bluetooth: New connection from: %s, channel %u", address,
476 service = find_service(driver);
477 if (service == NULL) {
478 error("bluetooth: Unable to find service");
482 if (driver->secure) {
483 if (request_service_authorization(service, io, address) < 0)
489 if (!bt_io_accept(io, connect_event, service, NULL, &err)) {
490 error("%s", err->message);
498 g_io_channel_shutdown(io, TRUE, NULL);
501 static GIOChannel *start(struct obex_server *server,
502 struct obex_service_driver *service)
504 BtIOSecLevel sec_level;
508 if (service->secure == TRUE)
509 sec_level = BT_IO_SEC_MEDIUM;
511 sec_level = BT_IO_SEC_LOW;
514 io = bt_io_listen(BT_IO_RFCOMM, NULL, confirm_event,
516 BT_IO_OPT_CHANNEL, service->channel,
517 BT_IO_OPT_SEC_LEVEL, sec_level,
520 error("bluetooth: unable to listen in channel %d: %s",
521 service->channel, err->message);
524 DBG("listening on channel %d", service->channel);
529 static void *bluetooth_start(struct obex_server *server, int *err)
534 for (l = server->drivers; l; l = l->next) {
535 struct obex_service_driver *service = l->data;
538 io = start(server, service);
542 ios = g_slist_prepend(ios, io);
545 register_record(server);
550 static void stop(gpointer data)
552 GIOChannel *io = data;
554 g_io_channel_shutdown(io, TRUE, NULL);
555 g_io_channel_unref(io);
558 static void bluetooth_stop(void *data)
562 g_slist_free_full(ios, stop);
565 static int bluetooth_getpeername(GIOChannel *io, char **name)
570 bt_io_get(io, BT_IO_RFCOMM, &gerr,
571 BT_IO_OPT_DEST, address,
576 *name = g_strdup(address);
581 static struct obex_transport_driver driver = {
583 .start = bluetooth_start,
584 .getpeername = bluetooth_getpeername,
585 .stop = bluetooth_stop
588 static unsigned int listener_id = 0;
590 static int bluetooth_init(void)
592 any = g_new0(struct adapter_any, 1);
594 connection = g_dbus_setup_private(DBUS_BUS_SYSTEM, NULL, NULL);
595 if (connection == NULL)
598 listener_id = g_dbus_add_service_watch(connection, "org.bluez",
599 name_acquired, name_released, NULL, NULL);
601 return obex_transport_driver_register(&driver);
604 static void bluetooth_exit(void)
606 g_dbus_remove_watch(connection, listener_id);
609 g_slist_free_full(any->services, g_free);
615 dbus_connection_unref(connection);
617 obex_transport_driver_unregister(&driver);
620 OBEX_PLUGIN_DEFINE(bluetooth, bluetooth_init, bluetooth_exit)