tizen 2.3 release
[framework/connectivity/bluez.git] / profiles / network / connection.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdbool.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <netinet/in.h>
33
34 #include <bluetooth/bluetooth.h>
35 #include <bluetooth/bnep.h>
36 #include <bluetooth/sdp.h>
37
38 #include <glib.h>
39 #include <gdbus/gdbus.h>
40
41 #include "btio/btio.h"
42 #include "src/log.h"
43 #include "src/dbus-common.h"
44 #include "src/adapter.h"
45 #include "src/device.h"
46 #include "src/profile.h"
47 #include "src/service.h"
48 #include "src/error.h"
49
50 #include "bnep.h"
51 #include "connection.h"
52
53 #define NETWORK_PEER_INTERFACE "org.bluez.Network1"
54 #define BNEP_INTERFACE "bnep%d"
55
56 typedef enum {
57         CONNECTED,
58         CONNECTING,
59         DISCONNECTED
60 } conn_state;
61
62 struct network_peer {
63         struct btd_device *device;
64         GSList          *connections;
65 };
66
67 struct network_conn {
68         struct btd_service *service;
69         char            dev[16];        /* Interface name */
70         uint16_t        id;             /* Role: Service Class Identifier */
71         conn_state      state;
72         GIOChannel      *io;
73         guint           dc_id;
74         struct network_peer *peer;
75         DBusMessage     *connect;
76         struct bnep     *session;
77 };
78
79 static GSList *peers = NULL;
80
81 static uint16_t get_service_id(struct btd_service *service)
82 {
83         return bnep_service_id(btd_service_get_profile(service)->remote_uuid);
84 }
85
86 static struct network_peer *find_peer(GSList *list, struct btd_device *device)
87 {
88         for (; list; list = list->next) {
89                 struct network_peer *peer = list->data;
90
91                 if (peer->device == device)
92                         return peer;
93         }
94
95         return NULL;
96 }
97
98 static struct network_conn *find_connection_by_state(GSList *list,
99                                                         conn_state state)
100 {
101         for (; list; list = list->next) {
102                 struct network_conn *nc = list->data;
103
104                 if (nc->state == state)
105                         return nc;
106         }
107
108         return NULL;
109 }
110
111 static void bnep_disconn_cb(gpointer data)
112 {
113         struct network_conn *nc = data;
114         DBusConnection *conn = btd_get_dbus_connection();
115         const char *path = device_get_path(nc->peer->device);
116
117 #ifdef __TIZEN_PATCH__
118         nc->state = DISCONNECTED;
119 #endif
120
121         g_dbus_emit_property_changed(conn, path,
122                                         NETWORK_PEER_INTERFACE, "Connected");
123         g_dbus_emit_property_changed(conn, path,
124                                         NETWORK_PEER_INTERFACE, "Interface");
125         g_dbus_emit_property_changed(conn, path,
126                                         NETWORK_PEER_INTERFACE, "UUID");
127         device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
128         nc->dc_id = 0;
129
130         btd_service_disconnecting_complete(nc->service, 0);
131
132         info("%s disconnected", nc->dev);
133
134         nc->state = DISCONNECTED;
135         memset(nc->dev, 0, sizeof(nc->dev));
136         strncpy(nc->dev, BNEP_INTERFACE, 16);
137         nc->dev[15] = '\0';
138
139         bnep_free(nc->session);
140         nc->session = NULL;
141 }
142
143 static void local_connect_cb(struct network_conn *nc, int err)
144 {
145         DBusConnection *conn = btd_get_dbus_connection();
146         const char *pdev = nc->dev;
147
148         if (err < 0) {
149                 DBusMessage *reply = btd_error_failed(nc->connect,
150                                                         strerror(-err));
151                 g_dbus_send_message(conn, reply);
152         } else {
153                 g_dbus_send_reply(conn, nc->connect, DBUS_TYPE_STRING, &pdev,
154                                                         DBUS_TYPE_INVALID);
155         }
156
157         dbus_message_unref(nc->connect);
158         nc->connect = NULL;
159 }
160
161 static void cancel_connection(struct network_conn *nc, int err)
162 {
163         btd_service_connecting_complete(nc->service, err);
164
165         if (nc->connect)
166                 local_connect_cb(nc, err);
167
168         if (nc->io) {
169                 g_io_channel_shutdown(nc->io, FALSE, NULL);
170                 g_io_channel_unref(nc->io);
171                 nc->io = NULL;
172         }
173
174         if (nc->state == CONNECTED)
175                 bnep_disconnect(nc->session);
176
177 #ifndef __TIZEN_PATCH__
178         bnep_free(nc->session);
179         nc->session = NULL;
180 #endif
181         nc->state = DISCONNECTED;
182 }
183
184 static void connection_destroy(DBusConnection *conn, void *user_data)
185 {
186         struct network_conn *nc = user_data;
187
188         cancel_connection(nc, -EIO);
189 }
190
191 static void disconnect_cb(struct btd_device *device, gboolean removal,
192                                 void *user_data)
193 {
194         struct network_conn *nc = user_data;
195
196         info("Network: disconnect %s", device_get_path(nc->peer->device));
197
198         connection_destroy(NULL, user_data);
199 }
200
201 static void bnep_conn_cb(char *iface, int err, void *data)
202 {
203         struct network_conn *nc = data;
204         const char *path;
205         DBusConnection *conn;
206
207         DBG("");
208
209         if (err < 0) {
210                 error("connect failed %s", strerror(-err));
211                 goto failed;
212         }
213
214         info("%s connected", nc->dev);
215
216         memcpy(nc->dev, iface, sizeof(nc->dev));
217         btd_service_connecting_complete(nc->service, 0);
218
219         if (nc->connect)
220                 local_connect_cb(nc, 0);
221
222         conn = btd_get_dbus_connection();
223         path = device_get_path(nc->peer->device);
224
225         g_dbus_emit_property_changed(conn, path,
226                                         NETWORK_PEER_INTERFACE, "Connected");
227         g_dbus_emit_property_changed(conn, path,
228                                         NETWORK_PEER_INTERFACE, "Interface");
229         g_dbus_emit_property_changed(conn, path,
230                                         NETWORK_PEER_INTERFACE, "UUID");
231
232         nc->state = CONNECTED;
233         nc->dc_id = device_add_disconnect_watch(nc->peer->device, disconnect_cb,
234                                                                 nc, NULL);
235
236         return;
237
238 failed:
239         cancel_connection(nc, -EIO);
240 }
241
242 static void connect_cb(GIOChannel *chan, GError *err, gpointer data)
243 {
244         struct network_conn *nc = data;
245         int sk, perr;
246
247         if (err) {
248                 error("%s", err->message);
249                 goto failed;
250         }
251
252         sk = g_io_channel_unix_get_fd(nc->io);
253         nc->session = bnep_new(sk, BNEP_SVC_PANU, nc->id, BNEP_INTERFACE);
254         if (!nc->session)
255                 goto failed;
256
257         perr = bnep_connect(nc->session, bnep_conn_cb, nc);
258         if (perr < 0) {
259                 error("bnep connect(): %s (%d)", strerror(-perr), -perr);
260                 goto failed;
261         }
262
263         bnep_set_disconnect(nc->session, bnep_disconn_cb, nc);
264
265         if (nc->io) {
266                 g_io_channel_unref(nc->io);
267                 nc->io = NULL;
268         }
269
270         return;
271
272 failed:
273         cancel_connection(nc, -EIO);
274 }
275
276 static DBusMessage *local_connect(DBusConnection *conn,
277                                                 DBusMessage *msg, void *data)
278 {
279         struct network_peer *peer = data;
280         struct btd_service *service;
281         struct network_conn *nc;
282         const char *svc;
283         const char *uuid;
284         uint16_t id;
285         int err;
286
287         if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &svc,
288                                                 DBUS_TYPE_INVALID) == FALSE)
289                 return btd_error_invalid_args(msg);
290
291         id = bnep_service_id(svc);
292         uuid = bnep_uuid(id);
293
294         if (uuid == NULL)
295                 return btd_error_invalid_args(msg);
296
297         service = btd_device_get_service(peer->device, uuid);
298         if (service == NULL)
299                 return btd_error_not_supported(msg);
300
301         nc = btd_service_get_user_data(service);
302
303         if (nc->connect != NULL)
304                 return btd_error_busy(msg);
305
306         err = connection_connect(nc->service);
307         if (err < 0)
308                 return btd_error_failed(msg, strerror(-err));
309
310         nc->connect = dbus_message_ref(msg);
311
312         return NULL;
313 }
314
315 /* Connect and initiate BNEP session */
316 int connection_connect(struct btd_service *service)
317 {
318         struct network_conn *nc = btd_service_get_user_data(service);
319         struct network_peer *peer = nc->peer;
320         uint16_t id = get_service_id(service);
321         GError *err = NULL;
322         const bdaddr_t *src;
323         const bdaddr_t *dst;
324
325         DBG("id %u", id);
326
327         if (nc->state != DISCONNECTED)
328                 return -EALREADY;
329
330         src = btd_adapter_get_address(device_get_adapter(peer->device));
331         dst = device_get_address(peer->device);
332
333         nc->io = bt_io_connect(connect_cb, nc,
334                                 NULL, &err,
335                                 BT_IO_OPT_SOURCE_BDADDR, src,
336                                 BT_IO_OPT_DEST_BDADDR, dst,
337                                 BT_IO_OPT_PSM, BNEP_PSM,
338                                 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
339                                 BT_IO_OPT_OMTU, BNEP_MTU,
340                                 BT_IO_OPT_IMTU, BNEP_MTU,
341                                 BT_IO_OPT_INVALID);
342         if (!nc->io)
343                 return -EIO;
344
345         nc->state = CONNECTING;
346
347         return 0;
348 }
349
350 int connection_disconnect(struct btd_service *service)
351 {
352         struct network_conn *nc = btd_service_get_user_data(service);
353
354         if (nc->state == DISCONNECTED)
355                 return 0;
356
357         connection_destroy(NULL, nc);
358
359         return 0;
360 }
361
362 static DBusMessage *local_disconnect(DBusConnection *conn,
363                                         DBusMessage *msg, void *data)
364 {
365         struct network_peer *peer = data;
366         GSList *l;
367
368         for (l = peer->connections; l; l = l->next) {
369                 struct network_conn *nc = l->data;
370                 int err;
371
372                 if (nc->state == DISCONNECTED)
373                         continue;
374
375                 err = connection_disconnect(nc->service);
376                 if (err < 0)
377                         return btd_error_failed(msg, strerror(-err));
378
379                 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
380         }
381
382         return btd_error_not_connected(msg);
383 }
384
385 static gboolean
386 network_property_get_connected(const GDBusPropertyTable *property,
387                                         DBusMessageIter *iter, void *data)
388 {
389         struct network_peer *peer = data;
390         struct network_conn *nc;
391         dbus_bool_t connected;
392
393         nc = find_connection_by_state(peer->connections, CONNECTED);
394         connected = nc != NULL ? TRUE : FALSE;
395
396         dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &connected);
397
398         return TRUE;
399 }
400
401 static gboolean network_property_exists(const GDBusPropertyTable *property,
402                                                                 void *data)
403 {
404         struct network_peer *peer = data;
405         struct network_conn *nc;
406
407         nc = find_connection_by_state(peer->connections, CONNECTED);
408         if (nc == NULL)
409                 return FALSE;
410
411         return TRUE;
412 }
413
414 static gboolean
415 network_property_get_interface(const GDBusPropertyTable *property,
416                                         DBusMessageIter *iter, void *data)
417 {
418         struct network_peer *peer = data;
419         struct network_conn *nc;
420         const char *iface;
421
422         nc = find_connection_by_state(peer->connections, CONNECTED);
423         if (nc == NULL)
424                 return FALSE;
425
426         iface = nc->dev;
427
428         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &iface);
429
430         return TRUE;
431 }
432
433 static gboolean network_property_get_uuid(const GDBusPropertyTable *property,
434                                         DBusMessageIter *iter, void *data)
435 {
436         struct network_peer *peer = data;
437         struct network_conn *nc;
438         const char *uuid;
439
440         nc = find_connection_by_state(peer->connections, CONNECTED);
441         if (nc == NULL)
442                 return FALSE;
443
444         uuid = bnep_uuid(nc->id);
445
446         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &uuid);
447
448         return TRUE;
449 }
450
451 static void connection_free(void *data)
452 {
453         struct network_conn *nc = data;
454
455         if (nc->dc_id)
456                 device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
457
458         connection_destroy(NULL, nc);
459
460         if (nc->connect)
461                 dbus_message_unref(nc->connect);
462
463         btd_service_unref(nc->service);
464         g_free(nc);
465 }
466
467 static void peer_free(struct network_peer *peer)
468 {
469         g_slist_free_full(peer->connections, connection_free);
470         btd_device_unref(peer->device);
471         g_free(peer);
472 }
473
474 static void path_unregister(void *data)
475 {
476         struct network_peer *peer = data;
477
478         DBG("Unregistered interface %s on path %s",
479                 NETWORK_PEER_INTERFACE, device_get_path(peer->device));
480
481         peers = g_slist_remove(peers, peer);
482         peer_free(peer);
483 }
484
485 static const GDBusMethodTable connection_methods[] = {
486         { GDBUS_ASYNC_METHOD("Connect",
487                                 GDBUS_ARGS({"uuid", "s"}),
488                                 GDBUS_ARGS({"interface", "s"}),
489                                 local_connect) },
490         { GDBUS_METHOD("Disconnect",
491                         NULL, NULL, local_disconnect) },
492         { }
493 };
494
495 static const GDBusPropertyTable connection_properties[] = {
496         { "Connected", "b", network_property_get_connected },
497         { "Interface", "s", network_property_get_interface, NULL,
498                                                 network_property_exists },
499         { "UUID", "s", network_property_get_uuid, NULL,
500                                                 network_property_exists },
501         { }
502 };
503
504 void connection_unregister(struct btd_service *service)
505 {
506         struct btd_device *device = btd_service_get_device(service);
507         struct network_conn *conn = btd_service_get_user_data(service);
508         struct network_peer *peer = conn->peer;
509         uint16_t id = get_service_id(service);
510
511         DBG("%s id %u", device_get_path(device), id);
512
513         peer->connections = g_slist_remove(peer->connections, conn);
514         connection_free(conn);
515
516         if (peer->connections != NULL)
517                 return;
518
519         g_dbus_unregister_interface(btd_get_dbus_connection(),
520                                                 device_get_path(device),
521                                                 NETWORK_PEER_INTERFACE);
522 }
523
524 static struct network_peer *create_peer(struct btd_device *device)
525 {
526         struct network_peer *peer;
527         const char *path;
528
529         peer = g_new0(struct network_peer, 1);
530         peer->device = btd_device_ref(device);
531
532         path = device_get_path(device);
533
534         if (g_dbus_register_interface(btd_get_dbus_connection(), path,
535                                         NETWORK_PEER_INTERFACE,
536                                         connection_methods,
537                                         NULL, connection_properties,
538                                         peer, path_unregister) == FALSE) {
539                 error("D-Bus failed to register %s interface",
540                         NETWORK_PEER_INTERFACE);
541                 peer_free(peer);
542                 return NULL;
543         }
544
545         DBG("Registered interface %s on path %s",
546                 NETWORK_PEER_INTERFACE, path);
547
548         return peer;
549 }
550
551 int connection_register(struct btd_service *service)
552 {
553         struct btd_device *device = btd_service_get_device(service);
554         struct network_peer *peer;
555         struct network_conn *nc;
556         uint16_t id = get_service_id(service);
557
558         DBG("%s id %u", device_get_path(device), id);
559
560         peer = find_peer(peers, device);
561         if (!peer) {
562                 peer = create_peer(device);
563                 if (!peer)
564                         return -1;
565                 peers = g_slist_append(peers, peer);
566         }
567
568         nc = g_new0(struct network_conn, 1);
569         nc->id = id;
570         nc->service = btd_service_ref(service);
571         nc->state = DISCONNECTED;
572         nc->peer = peer;
573
574         btd_service_set_user_data(service, nc);
575
576         DBG("id %u registered", id);
577
578         peer->connections = g_slist_append(peer->connections, nc);
579
580         return 0;
581 }