3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-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
31 #include <netinet/in.h>
33 #include <bluetooth/bluetooth.h>
34 #include <bluetooth/bnep.h>
35 #include <bluetooth/sdp.h>
41 #include "glib-compat.h"
43 #include "dbus-common.h"
49 #include "connection.h"
51 #define NETWORK_PEER_INTERFACE "org.bluez.Network"
52 #define CON_SETUP_RETRIES 3
53 #define CON_SETUP_TO 9
64 char *path; /* D-Bus path */
65 struct btd_device *device;
71 char dev[16]; /* Interface name */
72 uint16_t id; /* Role: Service Class Identifier */
75 guint watch; /* Disconnect watch */
77 struct network_peer *peer;
85 } __attribute__ ((packed));
87 static DBusConnection *connection = NULL;
88 static GSList *peers = NULL;
90 static struct network_peer *find_peer(GSList *list, const char *path)
92 for (; list; list = list->next) {
93 struct network_peer *peer = list->data;
95 if (!strcmp(peer->path, path))
102 static struct network_conn *find_connection(GSList *list, uint16_t id)
104 for (; list; list = list->next) {
105 struct network_conn *nc = list->data;
114 static gboolean bnep_watchdog_cb(GIOChannel *chan, GIOCondition cond,
117 struct network_conn *nc = data;
119 if (connection != NULL) {
120 gboolean connected = FALSE;
121 const char *property = "";
122 emit_property_changed(connection, nc->peer->path,
123 NETWORK_PEER_INTERFACE, "Connected",
124 DBUS_TYPE_BOOLEAN, &connected);
125 emit_property_changed(connection, nc->peer->path,
126 NETWORK_PEER_INTERFACE, "Interface",
127 DBUS_TYPE_STRING, &property);
128 emit_property_changed(connection, nc->peer->path,
129 NETWORK_PEER_INTERFACE, "UUID",
130 DBUS_TYPE_STRING, &property);
131 device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
134 g_dbus_remove_watch(connection, nc->watch);
139 info("%s disconnected", nc->dev);
141 bnep_if_down(nc->dev);
142 nc->state = DISCONNECTED;
143 memset(nc->dev, 0, sizeof(nc->dev));
144 strcpy(nc->dev, "bnep%d");
149 static void cancel_connection(struct network_conn *nc, const char *err_msg)
153 if (nc->timeout_source > 0) {
154 g_source_remove(nc->timeout_source);
155 nc->timeout_source = 0;
159 g_dbus_remove_watch(connection, nc->watch);
163 if (nc->msg && err_msg) {
164 reply = btd_error_failed(nc->msg, err_msg);
165 g_dbus_send_message(connection, reply);
168 g_io_channel_shutdown(nc->io, TRUE, NULL);
169 g_io_channel_unref(nc->io);
172 nc->state = DISCONNECTED;
175 static void connection_destroy(DBusConnection *conn, void *user_data)
177 struct network_conn *nc = user_data;
179 if (nc->state == CONNECTED) {
180 bnep_if_down(nc->dev);
181 bnep_kill_connection(&nc->peer->dst);
183 cancel_connection(nc, NULL);
186 static void disconnect_cb(struct btd_device *device, gboolean removal,
189 struct network_conn *nc = user_data;
191 info("Network: disconnect %s", nc->peer->path);
193 connection_destroy(NULL, user_data);
196 static gboolean bnep_setup_cb(GIOChannel *chan, GIOCondition cond,
199 struct network_conn *nc = data;
200 struct bnep_control_rsp *rsp;
201 struct timeval timeo;
205 const char *pdev, *uuid;
208 if (cond & G_IO_NVAL)
211 g_source_remove(nc->timeout_source);
212 nc->timeout_source = 0;
214 if (cond & (G_IO_HUP | G_IO_ERR)) {
215 error("Hangup or error on l2cap server socket");
219 sk = g_io_channel_unix_get_fd(chan);
221 memset(pkt, 0, BNEP_MTU);
222 r = read(sk, pkt, sizeof(pkt) -1);
224 error("IO Channel read error");
229 error("No packet received on l2cap socket");
235 if ((size_t) r < sizeof(*rsp)) {
236 error("Packet received is not bnep type");
241 if (rsp->type != BNEP_CONTROL) {
242 error("Packet received is not bnep type");
246 if (rsp->ctrl != BNEP_SETUP_CONN_RSP)
249 r = ntohs(rsp->resp);
251 if (r != BNEP_SUCCESS) {
252 error("bnep failed");
256 memset(&timeo, 0, sizeof(timeo));
259 setsockopt(sk, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
261 if (bnep_connadd(sk, BNEP_SVC_PANU, nc->dev)) {
262 error("%s could not be added", nc->dev);
268 uuid = bnep_uuid(nc->id);
270 g_dbus_send_reply(connection, nc->msg,
271 DBUS_TYPE_STRING, &pdev,
275 emit_property_changed(connection, nc->peer->path,
276 NETWORK_PEER_INTERFACE, "Connected",
277 DBUS_TYPE_BOOLEAN, &connected);
278 emit_property_changed(connection, nc->peer->path,
279 NETWORK_PEER_INTERFACE, "Interface",
280 DBUS_TYPE_STRING, &pdev);
281 emit_property_changed(connection, nc->peer->path,
282 NETWORK_PEER_INTERFACE, "UUID",
283 DBUS_TYPE_STRING, &uuid);
285 nc->state = CONNECTED;
286 nc->dc_id = device_add_disconnect_watch(nc->peer->device, disconnect_cb,
289 info("%s connected", nc->dev);
291 g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
292 (GIOFunc) bnep_watchdog_cb, nc);
293 g_io_channel_unref(nc->io);
299 cancel_connection(nc, "bnep setup failed");
304 static int bnep_send_conn_req(struct network_conn *nc)
306 struct bnep_setup_conn_req *req;
307 struct __service_16 *s;
308 unsigned char pkt[BNEP_MTU];
313 req->type = BNEP_CONTROL;
314 req->ctrl = BNEP_SETUP_CONN_REQ;
315 req->uuid_size = 2; /* 16bit UUID */
316 s = (void *) req->service;
317 s->dst = htons(nc->id);
318 s->src = htons(BNEP_SVC_PANU);
320 fd = g_io_channel_unix_get_fd(nc->io);
321 if (write(fd, pkt, sizeof(*req) + sizeof(*s)) < 0) {
323 error("bnep connection req send failed: %s", strerror(errno));
332 static gboolean bnep_conn_req_to(gpointer user_data)
334 struct network_conn *nc;
337 if (nc->attempt_cnt == CON_SETUP_RETRIES) {
338 error("Too many bnep connection attempts");
340 error("bnep connection setup TO, retrying...");
341 if (!bnep_send_conn_req(nc))
345 cancel_connection(nc, "bnep setup failed");
350 static int bnep_connect(struct network_conn *nc)
355 if ((err = bnep_send_conn_req(nc)))
358 nc->timeout_source = g_timeout_add_seconds(CON_SETUP_TO,
359 bnep_conn_req_to, nc);
361 g_io_add_watch(nc->io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
362 (GIOFunc) bnep_setup_cb, nc);
367 static void connect_cb(GIOChannel *chan, GError *err, gpointer data)
369 struct network_conn *nc = data;
374 error("%s", err->message);
375 err_msg = err->message;
379 perr = bnep_connect(nc);
381 err_msg = strerror(-perr);
382 error("bnep connect(): %s (%d)", err_msg, -perr);
389 cancel_connection(nc, err_msg);
392 /* Connect and initiate BNEP session */
393 static DBusMessage *connection_connect(DBusConnection *conn,
394 DBusMessage *msg, void *data)
396 struct network_peer *peer = data;
397 struct network_conn *nc;
402 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &svc,
403 DBUS_TYPE_INVALID) == FALSE)
406 id = bnep_service_id(svc);
407 nc = find_connection(peer->connections, id);
409 return btd_error_not_supported(msg);
411 if (nc->state != DISCONNECTED)
412 return btd_error_already_connected(msg);
414 nc->io = bt_io_connect(BT_IO_L2CAP, connect_cb, nc,
416 BT_IO_OPT_SOURCE_BDADDR, &peer->src,
417 BT_IO_OPT_DEST_BDADDR, &peer->dst,
418 BT_IO_OPT_PSM, BNEP_PSM,
419 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
420 BT_IO_OPT_OMTU, BNEP_MTU,
421 BT_IO_OPT_IMTU, BNEP_MTU,
425 error("%s", err->message);
426 reply = btd_error_failed(msg, err->message);
431 nc->state = CONNECTING;
432 nc->msg = dbus_message_ref(msg);
433 nc->watch = g_dbus_add_disconnect_watch(conn,
434 dbus_message_get_sender(msg),
441 static DBusMessage *connection_cancel(DBusConnection *conn,
442 DBusMessage *msg, void *data)
444 struct network_conn *nc = data;
445 const char *owner = dbus_message_get_sender(nc->msg);
446 const char *caller = dbus_message_get_sender(msg);
448 if (!g_str_equal(owner, caller))
449 return btd_error_not_authorized(msg);
451 connection_destroy(conn, nc);
453 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
456 static DBusMessage *connection_disconnect(DBusConnection *conn,
457 DBusMessage *msg, void *data)
459 struct network_peer *peer = data;
462 for (l = peer->connections; l; l = l->next) {
463 struct network_conn *nc = l->data;
465 if (nc->state == DISCONNECTED)
468 return connection_cancel(conn, msg, nc);
471 return btd_error_not_connected(msg);
474 static DBusMessage *connection_get_properties(DBusConnection *conn,
475 DBusMessage *msg, void *data)
477 struct network_peer *peer = data;
478 struct network_conn *nc = NULL;
480 DBusMessageIter iter;
481 DBusMessageIter dict;
482 dbus_bool_t connected;
483 const char *property;
486 reply = dbus_message_new_method_return(msg);
490 dbus_message_iter_init_append(reply, &iter);
492 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
493 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
494 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
495 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
498 for (l = peer->connections; l; l = l->next) {
499 struct network_conn *tmp = l->data;
501 if (tmp->state != CONNECTED)
508 connected = nc ? TRUE : FALSE;
509 dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN, &connected);
512 property = nc ? nc->dev : "";
513 dict_append_entry(&dict, "Interface", DBUS_TYPE_STRING, &property);
516 property = nc ? bnep_uuid(nc->id) : "";
517 dict_append_entry(&dict, "UUID", DBUS_TYPE_STRING, &property);
519 dbus_message_iter_close_container(&iter, &dict);
524 static void connection_free(void *data)
526 struct network_conn *nc = data;
529 device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
531 connection_destroy(connection, nc);
537 static void peer_free(struct network_peer *peer)
539 g_slist_free_full(peer->connections, connection_free);
540 btd_device_unref(peer->device);
545 static void path_unregister(void *data)
547 struct network_peer *peer = data;
549 DBG("Unregistered interface %s on path %s",
550 NETWORK_PEER_INTERFACE, peer->path);
552 peers = g_slist_remove(peers, peer);
556 static GDBusMethodTable connection_methods[] = {
557 { "Connect", "s", "s", connection_connect,
558 G_DBUS_METHOD_FLAG_ASYNC },
559 { "Disconnect", "", "", connection_disconnect },
560 { "GetProperties", "", "a{sv}",connection_get_properties },
564 static GDBusSignalTable connection_signals[] = {
565 { "PropertyChanged", "sv" },
569 void connection_unregister(const char *path, uint16_t id)
571 struct network_peer *peer;
572 struct network_conn *nc;
574 peer = find_peer(peers, path);
578 nc = find_connection(peer->connections, id);
582 peer->connections = g_slist_remove(peer->connections, nc);
584 if (peer->connections)
587 g_dbus_unregister_interface(connection, path, NETWORK_PEER_INTERFACE);
590 static struct network_peer *create_peer(struct btd_device *device,
591 const char *path, bdaddr_t *src,
594 struct network_peer *peer;
596 peer = g_new0(struct network_peer, 1);
597 peer->device = btd_device_ref(device);
598 peer->path = g_strdup(path);
599 bacpy(&peer->src, src);
600 bacpy(&peer->dst, dst);
602 if (g_dbus_register_interface(connection, path,
603 NETWORK_PEER_INTERFACE,
605 connection_signals, NULL,
606 peer, path_unregister) == FALSE) {
607 error("D-Bus failed to register %s interface",
608 NETWORK_PEER_INTERFACE);
613 DBG("Registered interface %s on path %s",
614 NETWORK_PEER_INTERFACE, path);
619 int connection_register(struct btd_device *device, const char *path,
620 bdaddr_t *src, bdaddr_t *dst, uint16_t id)
622 struct network_peer *peer;
623 struct network_conn *nc;
628 peer = find_peer(peers, path);
630 peer = create_peer(device, path, src, dst);
633 peers = g_slist_append(peers, peer);
636 nc = find_connection(peer->connections, id);
640 nc = g_new0(struct network_conn, 1);
642 memset(nc->dev, 0, sizeof(nc->dev));
643 strcpy(nc->dev, "bnep%d");
644 nc->state = DISCONNECTED;
647 peer->connections = g_slist_append(peer->connections, nc);
652 int connection_init(DBusConnection *conn)
654 connection = dbus_connection_ref(conn);
659 void connection_exit(void)
661 dbus_connection_unref(connection);