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"
62 char *path; /* D-Bus path */
63 struct btd_device *device;
69 char dev[16]; /* Interface name */
70 uint16_t id; /* Role: Service Class Identifier */
73 guint watch; /* Disconnect watch */
75 struct network_peer *peer;
81 } __attribute__ ((packed));
83 static DBusConnection *connection = NULL;
84 static GSList *peers = NULL;
86 static struct network_peer *find_peer(GSList *list, const char *path)
88 for (; list; list = list->next) {
89 struct network_peer *peer = list->data;
91 if (!strcmp(peer->path, path))
98 static struct network_conn *find_connection(GSList *list, uint16_t id)
100 for (; list; list = list->next) {
101 struct network_conn *nc = list->data;
110 static gboolean bnep_watchdog_cb(GIOChannel *chan, GIOCondition cond,
113 struct network_conn *nc = data;
115 if (connection != NULL) {
116 gboolean connected = FALSE;
117 const char *property = "";
118 emit_property_changed(connection, nc->peer->path,
119 NETWORK_PEER_INTERFACE, "Connected",
120 DBUS_TYPE_BOOLEAN, &connected);
121 emit_property_changed(connection, nc->peer->path,
122 NETWORK_PEER_INTERFACE, "Interface",
123 DBUS_TYPE_STRING, &property);
124 emit_property_changed(connection, nc->peer->path,
125 NETWORK_PEER_INTERFACE, "UUID",
126 DBUS_TYPE_STRING, &property);
127 device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
130 g_dbus_remove_watch(connection, nc->watch);
135 info("%s disconnected", nc->dev);
137 bnep_if_down(nc->dev);
138 nc->state = DISCONNECTED;
139 memset(nc->dev, 0, sizeof(nc->dev));
140 strcpy(nc->dev, "bnep%d");
145 static void cancel_connection(struct network_conn *nc, const char *err_msg)
150 g_dbus_remove_watch(connection, nc->watch);
154 if (nc->msg && err_msg) {
155 reply = btd_error_failed(nc->msg, err_msg);
156 g_dbus_send_message(connection, reply);
159 g_io_channel_shutdown(nc->io, TRUE, NULL);
160 g_io_channel_unref(nc->io);
163 nc->state = DISCONNECTED;
166 static void connection_destroy(DBusConnection *conn, void *user_data)
168 struct network_conn *nc = user_data;
170 if (nc->state == CONNECTED) {
171 bnep_if_down(nc->dev);
172 bnep_kill_connection(&nc->peer->dst);
174 cancel_connection(nc, NULL);
177 static void disconnect_cb(struct btd_device *device, gboolean removal,
180 struct network_conn *nc = user_data;
182 info("Network: disconnect %s", nc->peer->path);
184 connection_destroy(NULL, user_data);
187 static gboolean bnep_setup_cb(GIOChannel *chan, GIOCondition cond,
190 struct network_conn *nc = data;
191 struct bnep_control_rsp *rsp;
192 struct timeval timeo;
196 const char *pdev, *uuid;
199 if (cond & G_IO_NVAL)
202 if (cond & (G_IO_HUP | G_IO_ERR)) {
203 error("Hangup or error on l2cap server socket");
207 sk = g_io_channel_unix_get_fd(chan);
209 memset(pkt, 0, BNEP_MTU);
210 r = read(sk, pkt, sizeof(pkt) -1);
212 error("IO Channel read error");
217 error("No packet received on l2cap socket");
223 if ((size_t) r < sizeof(*rsp)) {
224 error("Packet received is not bnep type");
229 if (rsp->type != BNEP_CONTROL) {
230 error("Packet received is not bnep type");
234 if (rsp->ctrl != BNEP_SETUP_CONN_RSP)
237 r = ntohs(rsp->resp);
239 if (r != BNEP_SUCCESS) {
240 error("bnep failed");
244 memset(&timeo, 0, sizeof(timeo));
247 setsockopt(sk, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
249 if (bnep_connadd(sk, BNEP_SVC_PANU, nc->dev)) {
250 error("%s could not be added", nc->dev);
256 uuid = bnep_uuid(nc->id);
258 g_dbus_send_reply(connection, nc->msg,
259 DBUS_TYPE_STRING, &pdev,
263 emit_property_changed(connection, nc->peer->path,
264 NETWORK_PEER_INTERFACE, "Connected",
265 DBUS_TYPE_BOOLEAN, &connected);
266 emit_property_changed(connection, nc->peer->path,
267 NETWORK_PEER_INTERFACE, "Interface",
268 DBUS_TYPE_STRING, &pdev);
269 emit_property_changed(connection, nc->peer->path,
270 NETWORK_PEER_INTERFACE, "UUID",
271 DBUS_TYPE_STRING, &uuid);
273 nc->state = CONNECTED;
274 nc->dc_id = device_add_disconnect_watch(nc->peer->device, disconnect_cb,
277 info("%s connected", nc->dev);
279 g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
280 (GIOFunc) bnep_watchdog_cb, nc);
281 g_io_channel_unref(nc->io);
287 cancel_connection(nc, "bnep setup failed");
292 static int bnep_connect(struct network_conn *nc)
294 struct bnep_setup_conn_req *req;
295 struct __service_16 *s;
296 struct timeval timeo;
297 unsigned char pkt[BNEP_MTU];
302 req->type = BNEP_CONTROL;
303 req->ctrl = BNEP_SETUP_CONN_REQ;
304 req->uuid_size = 2; /* 16bit UUID */
305 s = (void *) req->service;
306 s->dst = htons(nc->id);
307 s->src = htons(BNEP_SVC_PANU);
309 memset(&timeo, 0, sizeof(timeo));
312 fd = g_io_channel_unix_get_fd(nc->io);
313 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
315 if (send(fd, pkt, sizeof(*req) + sizeof(*s), 0) < 0)
318 g_io_add_watch(nc->io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
319 (GIOFunc) bnep_setup_cb, nc);
324 static void connect_cb(GIOChannel *chan, GError *err, gpointer data)
326 struct network_conn *nc = data;
331 error("%s", err->message);
332 err_msg = err->message;
336 perr = bnep_connect(nc);
338 err_msg = strerror(-perr);
339 error("bnep connect(): %s (%d)", err_msg, -perr);
346 cancel_connection(nc, err_msg);
349 /* Connect and initiate BNEP session */
350 static DBusMessage *connection_connect(DBusConnection *conn,
351 DBusMessage *msg, void *data)
353 struct network_peer *peer = data;
354 struct network_conn *nc;
359 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &svc,
360 DBUS_TYPE_INVALID) == FALSE)
363 id = bnep_service_id(svc);
364 nc = find_connection(peer->connections, id);
366 return btd_error_not_supported(msg);
368 if (nc->state != DISCONNECTED)
369 return btd_error_already_connected(msg);
371 nc->io = bt_io_connect(BT_IO_L2CAP, connect_cb, nc,
373 BT_IO_OPT_SOURCE_BDADDR, &peer->src,
374 BT_IO_OPT_DEST_BDADDR, &peer->dst,
375 BT_IO_OPT_PSM, BNEP_PSM,
376 BT_IO_OPT_OMTU, BNEP_MTU,
377 BT_IO_OPT_IMTU, BNEP_MTU,
381 error("%s", err->message);
382 reply = btd_error_failed(msg, err->message);
387 nc->state = CONNECTING;
388 nc->msg = dbus_message_ref(msg);
389 nc->watch = g_dbus_add_disconnect_watch(conn,
390 dbus_message_get_sender(msg),
397 static DBusMessage *connection_cancel(DBusConnection *conn,
398 DBusMessage *msg, void *data)
400 struct network_conn *nc = data;
401 const char *owner = dbus_message_get_sender(nc->msg);
402 const char *caller = dbus_message_get_sender(msg);
404 if (!g_str_equal(owner, caller))
405 return btd_error_not_authorized(msg);
407 connection_destroy(conn, nc);
409 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
412 static DBusMessage *connection_disconnect(DBusConnection *conn,
413 DBusMessage *msg, void *data)
415 struct network_peer *peer = data;
418 for (l = peer->connections; l; l = l->next) {
419 struct network_conn *nc = l->data;
421 if (nc->state == DISCONNECTED)
424 return connection_cancel(conn, msg, nc);
427 return btd_error_not_connected(msg);
430 static DBusMessage *connection_get_properties(DBusConnection *conn,
431 DBusMessage *msg, void *data)
433 struct network_peer *peer = data;
434 struct network_conn *nc = NULL;
436 DBusMessageIter iter;
437 DBusMessageIter dict;
438 dbus_bool_t connected;
439 const char *property;
442 reply = dbus_message_new_method_return(msg);
446 dbus_message_iter_init_append(reply, &iter);
448 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
449 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
450 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
451 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
454 for (l = peer->connections; l; l = l->next) {
455 struct network_conn *tmp = l->data;
457 if (tmp->state != CONNECTED)
464 connected = nc ? TRUE : FALSE;
465 dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN, &connected);
468 property = nc ? nc->dev : "";
469 dict_append_entry(&dict, "Interface", DBUS_TYPE_STRING, &property);
472 property = nc ? bnep_uuid(nc->id) : "";
473 dict_append_entry(&dict, "UUID", DBUS_TYPE_STRING, &property);
475 dbus_message_iter_close_container(&iter, &dict);
480 static void connection_free(void *data)
482 struct network_conn *nc = data;
485 device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
487 connection_destroy(connection, nc);
493 static void peer_free(struct network_peer *peer)
495 g_slist_free_full(peer->connections, connection_free);
496 btd_device_unref(peer->device);
501 static void path_unregister(void *data)
503 struct network_peer *peer = data;
505 DBG("Unregistered interface %s on path %s",
506 NETWORK_PEER_INTERFACE, peer->path);
508 peers = g_slist_remove(peers, peer);
512 static GDBusMethodTable connection_methods[] = {
513 { "Connect", "s", "s", connection_connect,
514 G_DBUS_METHOD_FLAG_ASYNC },
515 { "Disconnect", "", "", connection_disconnect },
516 { "GetProperties", "", "a{sv}",connection_get_properties },
520 static GDBusSignalTable connection_signals[] = {
521 { "PropertyChanged", "sv" },
525 void connection_unregister(const char *path, uint16_t id)
527 struct network_peer *peer;
528 struct network_conn *nc;
530 peer = find_peer(peers, path);
534 nc = find_connection(peer->connections, id);
538 peer->connections = g_slist_remove(peer->connections, nc);
540 if (peer->connections)
543 g_dbus_unregister_interface(connection, path, NETWORK_PEER_INTERFACE);
546 static struct network_peer *create_peer(struct btd_device *device,
547 const char *path, bdaddr_t *src,
550 struct network_peer *peer;
552 peer = g_new0(struct network_peer, 1);
553 peer->device = btd_device_ref(device);
554 peer->path = g_strdup(path);
555 bacpy(&peer->src, src);
556 bacpy(&peer->dst, dst);
558 if (g_dbus_register_interface(connection, path,
559 NETWORK_PEER_INTERFACE,
561 connection_signals, NULL,
562 peer, path_unregister) == FALSE) {
563 error("D-Bus failed to register %s interface",
564 NETWORK_PEER_INTERFACE);
569 DBG("Registered interface %s on path %s",
570 NETWORK_PEER_INTERFACE, path);
575 int connection_register(struct btd_device *device, const char *path,
576 bdaddr_t *src, bdaddr_t *dst, uint16_t id)
578 struct network_peer *peer;
579 struct network_conn *nc;
584 peer = find_peer(peers, path);
586 peer = create_peer(device, path, src, dst);
589 peers = g_slist_append(peers, peer);
592 nc = find_connection(peer->connections, id);
596 nc = g_new0(struct network_conn, 1);
598 memset(nc->dev, 0, sizeof(nc->dev));
599 strcpy(nc->dev, "bnep%d");
600 nc->state = DISCONNECTED;
603 peer->connections = g_slist_append(peer->connections, nc);
608 int connection_init(DBusConnection *conn)
610 connection = dbus_connection_ref(conn);
615 void connection_exit(void)
617 dbus_connection_unref(connection);