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-helper.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)
90 for (l = list; l; l = l->next) {
91 struct network_peer *peer = l->data;
93 if (!strcmp(peer->path, path))
100 static struct network_conn *find_connection(GSList *list, uint16_t id)
104 for (l = list; l; l = l->next) {
105 struct network_conn *nc = l->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 #ifdef __TIZEN_PATCH__
123 char address[20] = {0};
124 const gchar* paddr = address;
125 ba2str(&nc->peer->dst, address);
127 emit_property_changed(connection, nc->peer->path,
128 NETWORK_PEER_INTERFACE, "Connected",
129 DBUS_TYPE_BOOLEAN, &connected);
130 emit_property_changed(connection, nc->peer->path,
131 NETWORK_PEER_INTERFACE, "Interface",
132 DBUS_TYPE_STRING, &property);
133 emit_property_changed(connection, nc->peer->path,
134 NETWORK_PEER_INTERFACE, "UUID",
135 DBUS_TYPE_STRING, &property);
136 #ifdef __TIZEN_PATCH__
137 emit_property_changed(connection, nc->peer->path,
138 NETWORK_PEER_INTERFACE, "Address",
139 DBUS_TYPE_STRING, &paddr);
141 device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
144 g_dbus_remove_watch(connection, nc->watch);
149 info("%s disconnected", nc->dev);
151 bnep_if_down(nc->dev);
152 nc->state = DISCONNECTED;
153 memset(nc->dev, 0, sizeof(nc->dev));
154 strcpy(nc->dev, "bnep%d");
159 static void cancel_connection(struct network_conn *nc, const char *err_msg)
164 g_dbus_remove_watch(connection, nc->watch);
168 if (nc->msg && err_msg) {
169 reply = btd_error_failed(nc->msg, err_msg);
170 g_dbus_send_message(connection, reply);
173 g_io_channel_shutdown(nc->io, TRUE, NULL);
174 g_io_channel_unref(nc->io);
177 nc->state = DISCONNECTED;
180 static void connection_destroy(DBusConnection *conn, void *user_data)
182 struct network_conn *nc = user_data;
184 if (nc->state == CONNECTED) {
185 bnep_if_down(nc->dev);
186 bnep_kill_connection(&nc->peer->dst);
188 cancel_connection(nc, NULL);
191 static void disconnect_cb(struct btd_device *device, gboolean removal,
194 struct network_conn *nc = user_data;
196 info("Network: disconnect %s", nc->peer->path);
198 connection_destroy(NULL, user_data);
201 static gboolean bnep_setup_cb(GIOChannel *chan, GIOCondition cond,
204 struct network_conn *nc = data;
205 struct bnep_control_rsp *rsp;
206 struct timeval timeo;
210 const char *pdev, *uuid;
212 #ifdef __TIZEN_PATCH__
213 char address[20] = {0};
214 const gchar* paddr = address;
217 if (cond & G_IO_NVAL)
220 if (cond & (G_IO_HUP | G_IO_ERR)) {
221 error("Hangup or error on l2cap server socket");
225 sk = g_io_channel_unix_get_fd(chan);
227 memset(pkt, 0, BNEP_MTU);
228 r = read(sk, pkt, sizeof(pkt) -1);
230 error("IO Channel read error");
235 error("No packet received on l2cap socket");
241 if ((size_t) r < sizeof(*rsp)) {
242 error("Packet received is not bnep type");
247 if (rsp->type != BNEP_CONTROL) {
248 error("Packet received is not bnep type");
252 if (rsp->ctrl != BNEP_SETUP_CONN_RSP)
255 r = ntohs(rsp->resp);
257 if (r != BNEP_SUCCESS) {
258 error("bnep failed");
262 memset(&timeo, 0, sizeof(timeo));
265 setsockopt(sk, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
267 if (bnep_connadd(sk, BNEP_SVC_PANU, nc->dev)) {
268 error("%s could not be added", nc->dev);
274 uuid = bnep_uuid(nc->id);
276 g_dbus_send_reply(connection, nc->msg,
277 DBUS_TYPE_STRING, &pdev,
281 #ifdef __TIZEN_PATCH__
282 ba2str(&nc->peer->dst, address);
284 emit_property_changed(connection, nc->peer->path,
285 NETWORK_PEER_INTERFACE, "Connected",
286 DBUS_TYPE_BOOLEAN, &connected);
287 emit_property_changed(connection, nc->peer->path,
288 NETWORK_PEER_INTERFACE, "Interface",
289 DBUS_TYPE_STRING, &pdev);
290 emit_property_changed(connection, nc->peer->path,
291 NETWORK_PEER_INTERFACE, "UUID",
292 DBUS_TYPE_STRING, &uuid);
293 #ifdef __TIZEN_PATCH__
294 emit_property_changed(connection, nc->peer->path,
295 NETWORK_PEER_INTERFACE, "Address",
296 DBUS_TYPE_STRING, &paddr);
298 nc->state = CONNECTED;
299 nc->dc_id = device_add_disconnect_watch(nc->peer->device, disconnect_cb,
302 info("%s connected", nc->dev);
304 g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
305 (GIOFunc) bnep_watchdog_cb, nc);
306 g_io_channel_unref(nc->io);
312 cancel_connection(nc, "bnep setup failed");
317 static int bnep_connect(struct network_conn *nc)
319 struct bnep_setup_conn_req *req;
320 struct __service_16 *s;
321 struct timeval timeo;
322 unsigned char pkt[BNEP_MTU];
327 req->type = BNEP_CONTROL;
328 req->ctrl = BNEP_SETUP_CONN_REQ;
329 req->uuid_size = 2; /* 16bit UUID */
330 s = (void *) req->service;
331 s->dst = htons(nc->id);
332 s->src = htons(BNEP_SVC_PANU);
334 memset(&timeo, 0, sizeof(timeo));
337 fd = g_io_channel_unix_get_fd(nc->io);
338 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
340 if (send(fd, pkt, sizeof(*req) + sizeof(*s), 0) < 0)
343 g_io_add_watch(nc->io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
344 (GIOFunc) bnep_setup_cb, nc);
349 static void connect_cb(GIOChannel *chan, GError *err, gpointer data)
351 struct network_conn *nc = data;
356 error("%s", err->message);
357 err_msg = err->message;
361 perr = bnep_connect(nc);
363 err_msg = strerror(-perr);
364 error("bnep connect(): %s (%d)", err_msg, -perr);
371 cancel_connection(nc, err_msg);
374 /* Connect and initiate BNEP session */
375 static DBusMessage *connection_connect(DBusConnection *conn,
376 DBusMessage *msg, void *data)
378 struct network_peer *peer = data;
379 struct network_conn *nc;
384 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &svc,
385 DBUS_TYPE_INVALID) == FALSE)
388 id = bnep_service_id(svc);
389 nc = find_connection(peer->connections, id);
391 return btd_error_not_supported(msg);
393 if (nc->state != DISCONNECTED)
394 return btd_error_already_connected(msg);
396 nc->io = bt_io_connect(BT_IO_L2CAP, connect_cb, nc,
398 BT_IO_OPT_SOURCE_BDADDR, &peer->src,
399 BT_IO_OPT_DEST_BDADDR, &peer->dst,
400 BT_IO_OPT_PSM, BNEP_PSM,
401 BT_IO_OPT_OMTU, BNEP_MTU,
402 BT_IO_OPT_IMTU, BNEP_MTU,
406 error("%s", err->message);
407 reply = btd_error_failed(msg, err->message);
412 nc->state = CONNECTING;
413 nc->msg = dbus_message_ref(msg);
414 nc->watch = g_dbus_add_disconnect_watch(conn,
415 dbus_message_get_sender(msg),
422 static DBusMessage *connection_cancel(DBusConnection *conn,
423 DBusMessage *msg, void *data)
425 struct network_conn *nc = data;
426 const char *owner = dbus_message_get_sender(nc->msg);
427 const char *caller = dbus_message_get_sender(msg);
429 if (!g_str_equal(owner, caller))
430 return btd_error_not_authorized(msg);
432 connection_destroy(conn, nc);
434 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
437 static DBusMessage *connection_disconnect(DBusConnection *conn,
438 DBusMessage *msg, void *data)
440 struct network_peer *peer = data;
443 for (l = peer->connections; l; l = l->next) {
444 struct network_conn *nc = l->data;
446 if (nc->state == DISCONNECTED)
449 return connection_cancel(conn, msg, nc);
452 return btd_error_not_connected(msg);
455 static DBusMessage *connection_get_properties(DBusConnection *conn,
456 DBusMessage *msg, void *data)
458 struct network_peer *peer = data;
459 struct network_conn *nc = NULL;
461 DBusMessageIter iter;
462 DBusMessageIter dict;
463 dbus_bool_t connected;
464 const char *property;
467 reply = dbus_message_new_method_return(msg);
471 dbus_message_iter_init_append(reply, &iter);
473 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
474 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
475 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
476 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
479 for (l = peer->connections; l; l = l->next) {
480 struct network_conn *tmp = l->data;
482 if (tmp->state != CONNECTED)
489 connected = nc ? TRUE : FALSE;
490 dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN, &connected);
493 property = nc ? nc->dev : "";
494 dict_append_entry(&dict, "Interface", DBUS_TYPE_STRING, &property);
497 property = nc ? bnep_uuid(nc->id) : "";
498 dict_append_entry(&dict, "UUID", DBUS_TYPE_STRING, &property);
500 dbus_message_iter_close_container(&iter, &dict);
505 static void connection_free(struct network_conn *nc)
508 device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
510 connection_destroy(connection, nc);
516 static void peer_free(struct network_peer *peer)
518 g_slist_foreach(peer->connections, (GFunc) connection_free, NULL);
519 g_slist_free(peer->connections);
520 btd_device_unref(peer->device);
525 static void path_unregister(void *data)
527 struct network_peer *peer = data;
529 DBG("Unregistered interface %s on path %s",
530 NETWORK_PEER_INTERFACE, peer->path);
532 peers = g_slist_remove(peers, peer);
536 static GDBusMethodTable connection_methods[] = {
537 { "Connect", "s", "s", connection_connect,
538 G_DBUS_METHOD_FLAG_ASYNC },
539 { "Disconnect", "", "", connection_disconnect },
540 { "GetProperties", "", "a{sv}",connection_get_properties },
544 static GDBusSignalTable connection_signals[] = {
545 { "PropertyChanged", "sv" },
549 void connection_unregister(const char *path, uint16_t id)
551 struct network_peer *peer;
552 struct network_conn *nc;
554 peer = find_peer(peers, path);
558 nc = find_connection(peer->connections, id);
562 peer->connections = g_slist_remove(peer->connections, nc);
564 if (peer->connections)
567 g_dbus_unregister_interface(connection, path, NETWORK_PEER_INTERFACE);
570 static struct network_peer *create_peer(struct btd_device *device,
571 const char *path, bdaddr_t *src,
574 struct network_peer *peer;
576 peer = g_new0(struct network_peer, 1);
577 peer->device = btd_device_ref(device);
578 peer->path = g_strdup(path);
579 bacpy(&peer->src, src);
580 bacpy(&peer->dst, dst);
582 if (g_dbus_register_interface(connection, path,
583 NETWORK_PEER_INTERFACE,
585 connection_signals, NULL,
586 peer, path_unregister) == FALSE) {
587 error("D-Bus failed to register %s interface",
588 NETWORK_PEER_INTERFACE);
593 DBG("Registered interface %s on path %s",
594 NETWORK_PEER_INTERFACE, path);
599 int connection_register(struct btd_device *device, const char *path,
600 bdaddr_t *src, bdaddr_t *dst, uint16_t id)
602 struct network_peer *peer;
603 struct network_conn *nc;
608 peer = find_peer(peers, path);
610 peer = create_peer(device, path, src, dst);
613 peers = g_slist_append(peers, peer);
616 nc = find_connection(peer->connections, id);
620 nc = g_new0(struct network_conn, 1);
622 memset(nc->dev, 0, sizeof(nc->dev));
623 strcpy(nc->dev, "bnep%d");
624 nc->state = DISCONNECTED;
627 peer->connections = g_slist_append(peer->connections, nc);
632 int connection_init(DBusConnection *conn)
634 connection = dbus_connection_ref(conn);
639 void connection_exit(void)
641 dbus_connection_unref(connection);