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
33 #include <sys/socket.h>
42 #include "transport.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 if (driver->port != 0)
173 xml = g_markup_printf_escaped(driver->record,
178 xml = g_markup_printf_escaped(driver->record,
182 add_record(any->path, xml, service);
187 static void find_adapter_any_reply(DBusPendingCall *call, void *user_data)
189 DBusMessage *reply = dbus_pending_call_steal_reply(call);
194 dbus_error_init(&derr);
195 if (dbus_set_error_from_message(&derr, reply)) {
196 error("bluetooth: Replied with an error: %s, %s",
197 derr.name, derr.message);
198 dbus_error_free(&derr);
202 dbus_message_get_args(reply, NULL,
203 DBUS_TYPE_OBJECT_PATH, &path,
205 any->path = g_strdup(path);
207 for (l = any->services; l; l = l->next) {
208 struct bluetooth_service *service = l->data;
209 struct obex_service_driver *driver = service->driver;
212 if (driver->port != 0)
213 xml = g_markup_printf_escaped(driver->record,
218 xml = g_markup_printf_escaped(driver->record,
222 add_record(any->path, xml, service);
227 dbus_message_unref(reply);
230 static DBusPendingCall *find_adapter(const char *pattern,
231 DBusPendingCallNotifyFunction function,
235 DBusPendingCall *call;
237 DBG("FindAdapter(%s)", pattern);
239 msg = dbus_message_new_method_call("org.bluez", "/",
240 "org.bluez.Manager", "FindAdapter");
244 dbus_message_append_args(msg, DBUS_TYPE_STRING, &pattern,
247 if (!dbus_connection_send_with_reply(connection, msg, &call, -1)) {
248 dbus_message_unref(msg);
252 dbus_pending_call_set_notify(call, function, user_data, NULL);
254 dbus_message_unref(msg);
259 static void name_acquired(DBusConnection *conn, void *user_data)
261 DBusPendingCall *call;
263 call = find_adapter("any", find_adapter_any_reply, NULL);
265 dbus_pending_call_unref(call);
268 static void name_released(DBusConnection *conn, void *user_data)
272 /* reset handles so the services got register next time */
273 for (l = any->services; l; l = l->next) {
274 struct bluetooth_service *service = l->data;
284 static void service_cancel(struct pending_request *pending)
288 msg = dbus_message_new_method_call("org.bluez",
289 pending->adapter_path,
291 "CancelAuthorization");
293 g_dbus_send_message(connection, msg);
296 static void pending_request_free(struct pending_request *pending)
299 dbus_pending_call_unref(pending->call);
300 g_io_channel_unref(pending->io);
301 g_free(pending->adapter_path);
305 static void connect_event(GIOChannel *io, GError *err, void *user_data)
307 int sk = g_io_channel_unix_get_fd(io);
308 struct bluetooth_service *service = user_data;
309 struct obex_server *server = service->server;
311 int omtu = BT_TX_MTU;
312 int imtu = BT_RX_MTU;
313 gboolean stream = TRUE;
314 socklen_t len = sizeof(int);
319 if (getsockopt(sk, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
322 if (type != SOCK_SEQPACKET)
327 /* Read MTU if io is an L2CAP socket */
328 bt_io_get(io, BT_IO_L2CAP, NULL, BT_IO_OPT_OMTU, &omtu,
329 BT_IO_OPT_IMTU, &imtu,
333 if (obex_server_new_connection(server, io, omtu, imtu, stream) < 0)
334 g_io_channel_shutdown(io, TRUE, NULL);
339 error("%s", err->message);
340 g_io_channel_shutdown(io, TRUE, NULL);
344 static void service_reply(DBusPendingCall *call, void *user_data)
346 struct pending_request *pending = user_data;
347 GIOChannel *io = pending->io;
348 struct bluetooth_service *service = pending->service;
349 DBusMessage *reply = dbus_pending_call_steal_reply(call);
353 dbus_error_init(&derr);
354 if (dbus_set_error_from_message(&derr, reply)) {
355 error("bluetooth: RequestAuthorization error: %s, %s",
356 derr.name, derr.message);
358 if (dbus_error_has_name(&derr, DBUS_ERROR_NO_REPLY))
359 service_cancel(pending);
361 dbus_error_free(&derr);
362 g_io_channel_shutdown(io, TRUE, NULL);
366 DBG("RequestAuthorization succeeded");
368 if (!bt_io_accept(io, connect_event, service, NULL, &err)) {
369 error("%s", err->message);
371 g_io_channel_shutdown(io, TRUE, NULL);
375 g_source_remove(pending->watch);
376 pending_request_free(pending);
377 dbus_message_unref(reply);
380 static gboolean service_error(GIOChannel *io, GIOCondition cond,
383 struct pending_request *pending = user_data;
385 service_cancel(pending);
387 dbus_pending_call_cancel(pending->call);
389 pending_request_free(pending);
394 static void find_adapter_reply(DBusPendingCall *call, void *user_data)
396 struct pending_request *pending = user_data;
397 DBusMessage *reply = dbus_pending_call_steal_reply(call);
399 DBusPendingCall *pcall;
400 const char *path, *paddr = pending->address;
403 dbus_error_init(&derr);
404 if (dbus_set_error_from_message(&derr, reply)) {
405 error("Replied with an error: %s, %s",
406 derr.name, derr.message);
407 dbus_error_free(&derr);
411 dbus_message_get_args(reply, NULL,
412 DBUS_TYPE_OBJECT_PATH, &path,
415 DBG("FindAdapter -> %s", path);
416 pending->adapter_path = g_strdup(path);
417 dbus_message_unref(reply);
419 msg = dbus_message_new_method_call("org.bluez", path,
420 "org.bluez.Service", "RequestAuthorization");
422 dbus_message_append_args(msg, DBUS_TYPE_STRING, &paddr,
423 DBUS_TYPE_UINT32, &pending->service->handle,
426 if (!dbus_connection_send_with_reply(connection,
427 msg, &pcall, TIMEOUT)) {
428 dbus_message_unref(msg);
432 dbus_message_unref(msg);
434 DBG("RequestAuthorization(%s, %x)", paddr,
435 pending->service->handle);
437 if (!dbus_pending_call_set_notify(pcall, service_reply, pending,
439 dbus_pending_call_unref(pcall);
443 dbus_pending_call_unref(pending->call);
444 pending->call = pcall;
446 /* Catches errors before authorization response comes */
447 pending->watch = g_io_add_watch(pending->io,
448 G_IO_HUP | G_IO_ERR | G_IO_NVAL,
449 service_error, pending);
454 g_io_channel_shutdown(pending->io, TRUE, NULL);
455 pending_request_free(pending);
458 static int request_service_authorization(struct bluetooth_service *service,
463 struct pending_request *pending;
465 if (connection == NULL || any->path == NULL)
468 pending = g_new0(struct pending_request, 1);
469 pending->call = find_adapter(source, find_adapter_reply, pending);
470 if (!pending->call) {
475 pending->service = service;
476 pending->io = g_io_channel_ref(io);
477 memcpy(pending->address, address, sizeof(pending->address));
482 static void confirm_connection(GIOChannel *io, const char *source,
483 const char *address, void *user_data)
486 struct obex_service_driver *driver = user_data;
487 struct bluetooth_service *service;
490 service = find_service(driver);
491 if (service == NULL) {
492 error("bluetooth: Unable to find service");
496 if (driver->secure) {
497 if (request_service_authorization(service, io, source,
504 if (!bt_io_accept(io, connect_event, service, NULL, &err)) {
505 error("%s", err->message);
513 g_io_channel_shutdown(io, TRUE, NULL);
516 static void confirm_rfcomm(GIOChannel *io, void *user_data)
523 bt_io_get(io, BT_IO_RFCOMM, &err,
524 BT_IO_OPT_SOURCE, source,
525 BT_IO_OPT_DEST, address,
526 BT_IO_OPT_CHANNEL, &channel,
529 error("%s", err->message);
531 g_io_channel_shutdown(io, TRUE, NULL);
535 info("bluetooth: New connection from: %s, channel %u", address,
538 confirm_connection(io, source, address, user_data);
541 static void confirm_l2cap(GIOChannel *io, void *user_data)
548 bt_io_get(io, BT_IO_L2CAP, &err,
549 BT_IO_OPT_SOURCE, source,
550 BT_IO_OPT_DEST, address,
554 error("%s", err->message);
556 g_io_channel_shutdown(io, TRUE, NULL);
560 info("bluetooth: New connection from: %s, psm %u", address, psm);
562 confirm_connection(io, source, address, user_data);
565 static GSList *start(struct obex_server *server,
566 struct obex_service_driver *service)
568 BtIOSecLevel sec_level;
574 if (service->secure == TRUE)
575 sec_level = BT_IO_SEC_MEDIUM;
577 sec_level = BT_IO_SEC_LOW;
579 io = bt_io_listen(BT_IO_RFCOMM, NULL, confirm_rfcomm,
581 BT_IO_OPT_CHANNEL, service->channel,
582 BT_IO_OPT_SEC_LEVEL, sec_level,
585 error("bluetooth: unable to listen in channel %d: %s",
586 service->channel, err->message);
589 l = g_slist_prepend(l, io);
590 DBG("listening on channel %d", service->channel);
593 if (service->port == 0)
596 psm = service->port == OBEX_PORT_RANDOM ? 0 : service->port;
598 io = bt_io_listen(BT_IO_L2CAP, NULL, confirm_l2cap,
601 BT_IO_OPT_MODE, BT_IO_MODE_ERTM,
602 BT_IO_OPT_OMTU, BT_TX_MTU,
603 BT_IO_OPT_IMTU, BT_RX_MTU,
604 BT_IO_OPT_SEC_LEVEL, sec_level,
607 error("bluetooth: unable to listen in psm %d: %s",
608 service->port, err->message);
612 l = g_slist_prepend(l, io);
613 bt_io_get(io, BT_IO_L2CAP, &err, BT_IO_OPT_PSM, &service->port,
615 DBG("listening on psm %d", service->port);
621 static void *bluetooth_start(struct obex_server *server, int *err)
626 for (l = server->drivers; l; l = l->next) {
627 struct obex_service_driver *service = l->data;
630 l = start(server, service);
634 ios = g_slist_concat(ios, l);
637 register_record(server);
642 static void stop(gpointer data)
644 GIOChannel *io = data;
646 g_io_channel_shutdown(io, TRUE, NULL);
647 g_io_channel_unref(io);
650 static void bluetooth_stop(void *data)
654 g_slist_free_full(ios, stop);
657 static int bluetooth_getpeername(GIOChannel *io, char **name)
659 int sk = g_io_channel_unix_get_fd(io);
663 socklen_t len = sizeof(int);
665 if (getsockopt(sk, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
668 if (type == SOCK_STREAM)
669 bt_io_get(io, BT_IO_RFCOMM, &gerr,
670 BT_IO_OPT_DEST, address,
673 bt_io_get(io, BT_IO_L2CAP, &gerr,
674 BT_IO_OPT_DEST, address,
678 error("%s", gerr->message);
683 *name = g_strdup(address);
688 static struct obex_transport_driver driver = {
690 .start = bluetooth_start,
691 .getpeername = bluetooth_getpeername,
692 .stop = bluetooth_stop
695 static unsigned int listener_id = 0;
697 static int bluetooth_init(void)
699 any = g_new0(struct adapter_any, 1);
701 connection = g_dbus_setup_private(DBUS_BUS_SYSTEM, NULL, NULL);
702 if (connection == NULL)
705 listener_id = g_dbus_add_service_watch(connection, "org.bluez",
706 name_acquired, name_released, NULL, NULL);
708 return obex_transport_driver_register(&driver);
711 static void bluetooth_exit(void)
713 g_dbus_remove_watch(connection, listener_id);
716 g_slist_free_full(any->services, g_free);
722 dbus_connection_unref(connection);
724 obex_transport_driver_unregister(&driver);
727 OBEX_PLUGIN_DEFINE(bluetooth, bluetooth_init, bluetooth_exit)