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>
42 #include "dbus-common.h"
48 #include "connection.h"
50 #define NETWORK_PEER_INTERFACE "org.bluez.Network"
51 #define CON_SETUP_RETRIES 3
52 #define CON_SETUP_TO 9
63 char *path; /* D-Bus path */
64 struct btd_device *device;
70 char dev[16]; /* Interface name */
71 uint16_t id; /* Role: Service Class Identifier */
74 guint watch; /* Disconnect watch */
76 struct network_peer *peer;
84 } __attribute__ ((packed));
86 static DBusConnection *connection = NULL;
87 static GSList *peers = NULL;
89 static struct network_peer *find_peer(GSList *list, const char *path)
91 for (; list; list = list->next) {
92 struct network_peer *peer = list->data;
94 if (!strcmp(peer->path, path))
101 static struct network_conn *find_connection(GSList *list, uint16_t id)
103 for (; list; list = list->next) {
104 struct network_conn *nc = list->data;
113 static gboolean bnep_watchdog_cb(GIOChannel *chan, GIOCondition cond,
116 struct network_conn *nc = data;
118 if (connection != NULL) {
119 gboolean connected = FALSE;
120 const char *property = "";
121 emit_property_changed(connection, nc->peer->path,
122 NETWORK_PEER_INTERFACE, "Connected",
123 DBUS_TYPE_BOOLEAN, &connected);
124 emit_property_changed(connection, nc->peer->path,
125 NETWORK_PEER_INTERFACE, "Interface",
126 DBUS_TYPE_STRING, &property);
127 emit_property_changed(connection, nc->peer->path,
128 NETWORK_PEER_INTERFACE, "UUID",
129 DBUS_TYPE_STRING, &property);
130 device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
133 g_dbus_remove_watch(connection, nc->watch);
138 info("%s disconnected", nc->dev);
140 bnep_if_down(nc->dev);
141 nc->state = DISCONNECTED;
142 memset(nc->dev, 0, sizeof(nc->dev));
143 strcpy(nc->dev, "bnep%d");
148 static void cancel_connection(struct network_conn *nc, const char *err_msg)
152 if (nc->timeout_source > 0) {
153 g_source_remove(nc->timeout_source);
154 nc->timeout_source = 0;
158 g_dbus_remove_watch(connection, nc->watch);
162 if (nc->msg && err_msg) {
163 reply = btd_error_failed(nc->msg, err_msg);
164 g_dbus_send_message(connection, reply);
167 g_io_channel_shutdown(nc->io, TRUE, NULL);
168 g_io_channel_unref(nc->io);
171 nc->state = DISCONNECTED;
174 static void connection_destroy(DBusConnection *conn, void *user_data)
176 struct network_conn *nc = user_data;
178 if (nc->state == CONNECTED) {
179 bnep_if_down(nc->dev);
180 bnep_kill_connection(&nc->peer->dst);
182 cancel_connection(nc, NULL);
185 static void disconnect_cb(struct btd_device *device, gboolean removal,
188 struct network_conn *nc = user_data;
190 info("Network: disconnect %s", nc->peer->path);
192 connection_destroy(NULL, user_data);
195 static gboolean bnep_setup_cb(GIOChannel *chan, GIOCondition cond,
198 struct network_conn *nc = data;
199 struct bnep_control_rsp *rsp;
200 struct timeval timeo;
204 const char *pdev, *uuid;
207 if (cond & G_IO_NVAL)
210 g_source_remove(nc->timeout_source);
211 nc->timeout_source = 0;
213 if (cond & (G_IO_HUP | G_IO_ERR)) {
214 error("Hangup or error on l2cap server socket");
218 sk = g_io_channel_unix_get_fd(chan);
220 memset(pkt, 0, BNEP_MTU);
221 r = read(sk, pkt, sizeof(pkt) -1);
223 error("IO Channel read error");
228 error("No packet received on l2cap socket");
234 if ((size_t) r < sizeof(*rsp)) {
235 error("Packet received is not bnep type");
240 if (rsp->type != BNEP_CONTROL) {
241 error("Packet received is not bnep type");
245 if (rsp->ctrl != BNEP_SETUP_CONN_RSP)
248 r = ntohs(rsp->resp);
250 if (r != BNEP_SUCCESS) {
251 error("bnep failed");
255 memset(&timeo, 0, sizeof(timeo));
258 setsockopt(sk, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
260 if (bnep_connadd(sk, BNEP_SVC_PANU, nc->dev)) {
261 error("%s could not be added", nc->dev);
267 uuid = bnep_uuid(nc->id);
269 g_dbus_send_reply(connection, nc->msg,
270 DBUS_TYPE_STRING, &pdev,
274 emit_property_changed(connection, nc->peer->path,
275 NETWORK_PEER_INTERFACE, "Connected",
276 DBUS_TYPE_BOOLEAN, &connected);
277 emit_property_changed(connection, nc->peer->path,
278 NETWORK_PEER_INTERFACE, "Interface",
279 DBUS_TYPE_STRING, &pdev);
280 emit_property_changed(connection, nc->peer->path,
281 NETWORK_PEER_INTERFACE, "UUID",
282 DBUS_TYPE_STRING, &uuid);
284 nc->state = CONNECTED;
285 nc->dc_id = device_add_disconnect_watch(nc->peer->device, disconnect_cb,
288 info("%s connected", nc->dev);
290 g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
291 (GIOFunc) bnep_watchdog_cb, nc);
292 g_io_channel_unref(nc->io);
298 cancel_connection(nc, "bnep setup failed");
303 static int bnep_send_conn_req(struct network_conn *nc)
305 struct bnep_setup_conn_req *req;
306 struct __service_16 *s;
307 unsigned char pkt[BNEP_MTU];
312 req->type = BNEP_CONTROL;
313 req->ctrl = BNEP_SETUP_CONN_REQ;
314 req->uuid_size = 2; /* 16bit UUID */
315 s = (void *) req->service;
316 s->dst = htons(nc->id);
317 s->src = htons(BNEP_SVC_PANU);
319 fd = g_io_channel_unix_get_fd(nc->io);
320 if (write(fd, pkt, sizeof(*req) + sizeof(*s)) < 0) {
322 error("bnep connection req send failed: %s", strerror(errno));
331 static gboolean bnep_conn_req_to(gpointer user_data)
333 struct network_conn *nc;
336 if (nc->attempt_cnt == CON_SETUP_RETRIES) {
337 error("Too many bnep connection attempts");
339 error("bnep connection setup TO, retrying...");
340 if (!bnep_send_conn_req(nc))
344 cancel_connection(nc, "bnep setup failed");
349 static int bnep_connect(struct network_conn *nc)
354 if ((err = bnep_send_conn_req(nc)))
357 nc->timeout_source = g_timeout_add_seconds(CON_SETUP_TO,
358 bnep_conn_req_to, nc);
360 g_io_add_watch(nc->io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
361 (GIOFunc) bnep_setup_cb, nc);
366 static void connect_cb(GIOChannel *chan, GError *err, gpointer data)
368 struct network_conn *nc = data;
373 error("%s", err->message);
374 err_msg = err->message;
378 perr = bnep_connect(nc);
380 err_msg = strerror(-perr);
381 error("bnep connect(): %s (%d)", err_msg, -perr);
388 cancel_connection(nc, err_msg);
391 /* Connect and initiate BNEP session */
392 static DBusMessage *connection_connect(DBusConnection *conn,
393 DBusMessage *msg, void *data)
395 struct network_peer *peer = data;
396 struct network_conn *nc;
401 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &svc,
402 DBUS_TYPE_INVALID) == FALSE)
405 id = bnep_service_id(svc);
406 nc = find_connection(peer->connections, id);
408 return btd_error_not_supported(msg);
410 if (nc->state != DISCONNECTED)
411 return btd_error_already_connected(msg);
413 nc->io = bt_io_connect(BT_IO_L2CAP, connect_cb, nc,
415 BT_IO_OPT_SOURCE_BDADDR, &peer->src,
416 BT_IO_OPT_DEST_BDADDR, &peer->dst,
417 BT_IO_OPT_PSM, BNEP_PSM,
418 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
419 BT_IO_OPT_OMTU, BNEP_MTU,
420 BT_IO_OPT_IMTU, BNEP_MTU,
424 error("%s", err->message);
425 reply = btd_error_failed(msg, err->message);
430 nc->state = CONNECTING;
431 nc->msg = dbus_message_ref(msg);
432 nc->watch = g_dbus_add_disconnect_watch(conn,
433 dbus_message_get_sender(msg),
440 static DBusMessage *connection_cancel(DBusConnection *conn,
441 DBusMessage *msg, void *data)
443 struct network_conn *nc = data;
444 const char *owner = dbus_message_get_sender(nc->msg);
445 const char *caller = dbus_message_get_sender(msg);
447 if (!g_str_equal(owner, caller))
448 return btd_error_not_authorized(msg);
450 connection_destroy(conn, nc);
452 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
455 static DBusMessage *connection_disconnect(DBusConnection *conn,
456 DBusMessage *msg, void *data)
458 struct network_peer *peer = data;
461 for (l = peer->connections; l; l = l->next) {
462 struct network_conn *nc = l->data;
464 if (nc->state == DISCONNECTED)
467 return connection_cancel(conn, msg, nc);
470 return btd_error_not_connected(msg);
473 static DBusMessage *connection_get_properties(DBusConnection *conn,
474 DBusMessage *msg, void *data)
476 struct network_peer *peer = data;
477 struct network_conn *nc = NULL;
479 DBusMessageIter iter;
480 DBusMessageIter dict;
481 dbus_bool_t connected;
482 const char *property;
485 reply = dbus_message_new_method_return(msg);
489 dbus_message_iter_init_append(reply, &iter);
491 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
492 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
493 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
494 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
497 for (l = peer->connections; l; l = l->next) {
498 struct network_conn *tmp = l->data;
500 if (tmp->state != CONNECTED)
507 connected = nc ? TRUE : FALSE;
508 dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN, &connected);
511 property = nc ? nc->dev : "";
512 dict_append_entry(&dict, "Interface", DBUS_TYPE_STRING, &property);
515 property = nc ? bnep_uuid(nc->id) : "";
516 dict_append_entry(&dict, "UUID", DBUS_TYPE_STRING, &property);
518 dbus_message_iter_close_container(&iter, &dict);
523 static void connection_free(void *data)
525 struct network_conn *nc = data;
528 device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
530 connection_destroy(connection, nc);
536 static void peer_free(struct network_peer *peer)
538 g_slist_free_full(peer->connections, connection_free);
539 btd_device_unref(peer->device);
544 static void path_unregister(void *data)
546 struct network_peer *peer = data;
548 DBG("Unregistered interface %s on path %s",
549 NETWORK_PEER_INTERFACE, peer->path);
551 peers = g_slist_remove(peers, peer);
555 static const GDBusMethodTable connection_methods[] = {
556 { GDBUS_ASYNC_METHOD("Connect",
557 NULL, NULL, connection_connect) },
558 { GDBUS_METHOD("Disconnect",
559 NULL, NULL, connection_disconnect) },
560 { GDBUS_METHOD("GetProperties",
561 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
562 connection_get_properties) },
566 static const GDBusSignalTable connection_signals[] = {
567 { GDBUS_SIGNAL("PropertyChanged",
568 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
572 void connection_unregister(const char *path, uint16_t id)
574 struct network_peer *peer;
575 struct network_conn *nc;
577 peer = find_peer(peers, path);
581 nc = find_connection(peer->connections, id);
585 peer->connections = g_slist_remove(peer->connections, nc);
587 if (peer->connections)
590 g_dbus_unregister_interface(connection, path, NETWORK_PEER_INTERFACE);
593 static struct network_peer *create_peer(struct btd_device *device,
594 const char *path, bdaddr_t *src,
597 struct network_peer *peer;
599 peer = g_new0(struct network_peer, 1);
600 peer->device = btd_device_ref(device);
601 peer->path = g_strdup(path);
602 bacpy(&peer->src, src);
603 bacpy(&peer->dst, dst);
605 if (g_dbus_register_interface(connection, path,
606 NETWORK_PEER_INTERFACE,
608 connection_signals, NULL,
609 peer, path_unregister) == FALSE) {
610 error("D-Bus failed to register %s interface",
611 NETWORK_PEER_INTERFACE);
616 DBG("Registered interface %s on path %s",
617 NETWORK_PEER_INTERFACE, path);
622 int connection_register(struct btd_device *device, const char *path,
623 bdaddr_t *src, bdaddr_t *dst, uint16_t id)
625 struct network_peer *peer;
626 struct network_conn *nc;
631 peer = find_peer(peers, path);
633 peer = create_peer(device, path, src, dst);
636 peers = g_slist_append(peers, peer);
639 nc = find_connection(peer->connections, id);
643 nc = g_new0(struct network_conn, 1);
645 memset(nc->dev, 0, sizeof(nc->dev));
646 strcpy(nc->dev, "bnep%d");
647 nc->state = DISCONNECTED;
650 peer->connections = g_slist_append(peer->connections, nc);
655 int connection_init(DBusConnection *conn)
657 connection = dbus_connection_ref(conn);
662 void connection_exit(void)
664 dbus_connection_unref(connection);