tizen 2.3.1 release
[framework/connectivity/bluez.git] / profiles / network / server.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 <unistd.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <netinet/in.h>
33
34 #include <glib.h>
35
36 #include "lib/bluetooth.h"
37 #include "lib/bnep.h"
38 #include "lib/sdp.h"
39 #include "lib/sdp_lib.h"
40 #include "lib/uuid.h"
41
42 #include "gdbus/gdbus.h"
43
44 #include "btio/btio.h"
45 #include "src/dbus-common.h"
46 #include "src/adapter.h"
47 #include "src/log.h"
48 #include "src/error.h"
49 #include "src/sdpd.h"
50
51 #include "bnep.h"
52 #include "server.h"
53
54 #define NETWORK_SERVER_INTERFACE "org.bluez.NetworkServer1"
55 #define BNEP_INTERFACE "bnep%d"
56 #define SETUP_TIMEOUT           1
57
58 /* Pending Authorization */
59 struct network_session {
60         bdaddr_t        dst;            /* Remote Bluetooth Address */
61         char            dev[16];        /* Interface name */
62         GIOChannel      *io;            /* Pending connect channel */
63         guint           watch;          /* BNEP socket watch */
64 };
65
66 struct network_adapter {
67         struct btd_adapter *adapter;    /* Adapter pointer */
68         GIOChannel      *io;            /* Bnep socket */
69         struct network_session *setup;  /* Setup in progress */
70         GSList          *servers;       /* Server register to adapter */
71 };
72
73 /* Main server structure */
74 struct network_server {
75         bdaddr_t        src;            /* Bluetooth Local Address */
76         char            *name;          /* Server service name */
77         char            *bridge;        /* Bridge name */
78         uint32_t        record_id;      /* Service record id */
79         uint16_t        id;             /* Service class identifier */
80         GSList          *sessions;      /* Active connections */
81         struct network_adapter *na;     /* Adapter reference */
82         guint           watch_id;       /* Client service watch */
83 };
84
85 static GSList *adapters = NULL;
86 static gboolean security = TRUE;
87
88 #ifdef  __TIZEN_PATCH__
89 static gboolean server_disconnected_cb(GIOChannel *chan,
90                         GIOCondition cond, gpointer user_data);
91 #endif
92
93 static struct network_adapter *find_adapter(GSList *list,
94                                         struct btd_adapter *adapter)
95 {
96         for (; list; list = list->next) {
97                 struct network_adapter *na = list->data;
98
99                 if (na->adapter == adapter)
100                         return na;
101         }
102
103         return NULL;
104 }
105
106 static struct network_server *find_server(GSList *list, uint16_t id)
107 {
108         for (; list; list = list->next) {
109                 struct network_server *ns = list->data;
110
111                 if (ns->id == id)
112                         return ns;
113         }
114
115         return NULL;
116 }
117
118 static struct network_server *find_server_by_uuid(GSList *list,
119                                                         const char *uuid)
120 {
121         for (; list; list = list->next) {
122                 struct network_server *ns = list->data;
123
124                 if (strcasecmp(uuid, bnep_uuid(ns->id)) == 0)
125                         return ns;
126
127                 if (strcasecmp(uuid, bnep_name(ns->id)) == 0)
128                         return ns;
129         }
130
131         return NULL;
132 }
133
134 #ifdef  __TIZEN_PATCH__
135 static struct network_session *find_session(GSList *list, GIOChannel *io)
136 {
137         GSList *l;
138
139         for (l = list; l; l = l->next) {
140                 struct network_session *session = l->data;
141
142                 if (session && session->io == io)
143                         return session;
144         }
145
146         return NULL;
147 }
148
149 static struct network_session *find_session_by_addr(GSList *list,
150                                                                                                         bdaddr_t dst_addr)
151 {
152         GSList *l;
153
154         for (l = list; l; l = l->next) {
155                 struct network_session *session = l->data;
156
157                 if (!bacmp(&session->dst, &dst_addr))
158                         return session;
159
160         }
161
162         return NULL;
163 }
164 #endif
165
166 static sdp_record_t *server_record_new(const char *name, uint16_t id)
167 {
168         sdp_list_t *svclass, *pfseq, *apseq, *root, *aproto;
169         uuid_t root_uuid, pan, l2cap, bnep;
170         sdp_profile_desc_t profile[1];
171         sdp_list_t *proto[2];
172         sdp_data_t *v, *p;
173         uint16_t psm = BNEP_PSM, version = 0x0100;
174         uint16_t security_desc = (security ? 0x0001 : 0x0000);
175 #ifdef __TIZEN_PATCH__
176         uint16_t net_access_type = 0x000a;
177         uint32_t max_net_access_rate = 0x001312d0;
178 #else
179         uint16_t net_access_type = 0xfffe;
180         uint32_t max_net_access_rate = 0;
181 #endif
182         const char *desc = "Network service";
183         sdp_record_t *record;
184
185         record = sdp_record_alloc();
186         if (!record)
187                 return NULL;
188
189         record->attrlist = NULL;
190         record->pattern = NULL;
191
192         switch (id) {
193         case BNEP_SVC_NAP:
194                 sdp_uuid16_create(&pan, NAP_SVCLASS_ID);
195                 svclass = sdp_list_append(NULL, &pan);
196                 sdp_set_service_classes(record, svclass);
197
198                 sdp_uuid16_create(&profile[0].uuid, NAP_PROFILE_ID);
199                 profile[0].version = 0x0100;
200                 pfseq = sdp_list_append(NULL, &profile[0]);
201                 sdp_set_profile_descs(record, pfseq);
202
203                 sdp_set_info_attr(record, name, NULL, desc);
204
205                 sdp_attr_add_new(record, SDP_ATTR_NET_ACCESS_TYPE,
206                                         SDP_UINT16, &net_access_type);
207                 sdp_attr_add_new(record, SDP_ATTR_MAX_NET_ACCESSRATE,
208                                         SDP_UINT32, &max_net_access_rate);
209                 break;
210         case BNEP_SVC_GN:
211                 sdp_uuid16_create(&pan, GN_SVCLASS_ID);
212                 svclass = sdp_list_append(NULL, &pan);
213                 sdp_set_service_classes(record, svclass);
214
215                 sdp_uuid16_create(&profile[0].uuid, GN_PROFILE_ID);
216                 profile[0].version = 0x0100;
217                 pfseq = sdp_list_append(NULL, &profile[0]);
218                 sdp_set_profile_descs(record, pfseq);
219
220                 sdp_set_info_attr(record, name, NULL, desc);
221                 break;
222         case BNEP_SVC_PANU:
223                 sdp_uuid16_create(&pan, PANU_SVCLASS_ID);
224                 svclass = sdp_list_append(NULL, &pan);
225                 sdp_set_service_classes(record, svclass);
226
227                 sdp_uuid16_create(&profile[0].uuid, PANU_PROFILE_ID);
228                 profile[0].version = 0x0100;
229                 pfseq = sdp_list_append(NULL, &profile[0]);
230                 sdp_set_profile_descs(record, pfseq);
231
232                 sdp_set_info_attr(record, name, NULL, desc);
233                 break;
234         default:
235                 sdp_record_free(record);
236                 return NULL;
237         }
238
239         sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
240         root = sdp_list_append(NULL, &root_uuid);
241         sdp_set_browse_groups(record, root);
242
243         sdp_uuid16_create(&l2cap, L2CAP_UUID);
244         proto[0] = sdp_list_append(NULL, &l2cap);
245         p = sdp_data_alloc(SDP_UINT16, &psm);
246         proto[0] = sdp_list_append(proto[0], p);
247         apseq    = sdp_list_append(NULL, proto[0]);
248
249         sdp_uuid16_create(&bnep, BNEP_UUID);
250         proto[1] = sdp_list_append(NULL, &bnep);
251         v = sdp_data_alloc(SDP_UINT16, &version);
252         proto[1] = sdp_list_append(proto[1], v);
253
254         /* Supported protocols */
255         {
256                 uint16_t ptype[] = {
257                         0x0800,  /* IPv4 */
258                         0x0806,  /* ARP */
259                 };
260                 sdp_data_t *head, *pseq;
261                 int p;
262
263                 for (p = 0, head = NULL; p < 2; p++) {
264                         sdp_data_t *data = sdp_data_alloc(SDP_UINT16, &ptype[p]);
265                         if (head)
266                                 sdp_seq_append(head, data);
267                         else
268                                 head = data;
269                 }
270                 pseq = sdp_data_alloc(SDP_SEQ16, head);
271                 proto[1] = sdp_list_append(proto[1], pseq);
272         }
273
274         apseq = sdp_list_append(apseq, proto[1]);
275
276         aproto = sdp_list_append(NULL, apseq);
277         sdp_set_access_protos(record, aproto);
278
279         sdp_add_lang_attr(record);
280
281         sdp_attr_add_new(record, SDP_ATTR_SECURITY_DESC,
282                                 SDP_UINT16, &security_desc);
283
284         sdp_data_free(p);
285         sdp_data_free(v);
286         sdp_list_free(apseq, NULL);
287         sdp_list_free(root, NULL);
288         sdp_list_free(aproto, NULL);
289         sdp_list_free(proto[0], NULL);
290         sdp_list_free(proto[1], NULL);
291         sdp_list_free(svclass, NULL);
292         sdp_list_free(pfseq, NULL);
293
294         return record;
295 }
296
297 static void session_free(void *data)
298 {
299         struct network_session *session = data;
300
301         if (session->watch)
302                 g_source_remove(session->watch);
303
304         if (session->io)
305                 g_io_channel_unref(session->io);
306
307         g_free(session);
308 }
309
310 static void setup_destroy(void *user_data)
311 {
312         struct network_adapter *na = user_data;
313         struct network_session *setup = na->setup;
314
315         if (!setup)
316                 return;
317
318         na->setup = NULL;
319
320         session_free(setup);
321 }
322
323 #ifdef  __TIZEN_PATCH__
324 static gboolean server_disconnected_cb(GIOChannel *chan,
325                         GIOCondition cond, gpointer user_data)
326 {
327         struct network_server *ns = NULL;
328         struct network_session *session = NULL;
329         char address[20] = {0};
330         const char* paddr = address;
331         char *name_str = NULL;
332
333         info("server_disconnected_cb");
334
335         if (!user_data)
336                 return FALSE;
337
338         ns = (struct network_server *) user_data;
339
340         session = find_session(ns->sessions, chan);
341         if (session) {
342                 name_str = g_strdup(session->dev);
343                 ba2str(&session->dst, address);
344         } else {
345                 info("Session is not exist!");
346                 name_str = g_strdup("bnep");
347         }
348
349         g_dbus_emit_signal(btd_get_dbus_connection(),
350                         adapter_get_path(ns->na->adapter),
351                         NETWORK_SERVER_INTERFACE, "PeerDisconnected",
352                         DBUS_TYPE_STRING, &name_str,
353                         DBUS_TYPE_STRING, &paddr,
354                         DBUS_TYPE_INVALID);
355
356         if (session) {
357                 ns->sessions = g_slist_remove(ns->sessions, session);
358                 session_free(session);
359         }
360
361         if (g_slist_length(ns->sessions) == 0 &&
362                 name_str != NULL) {
363                 bnep_if_down_wrapper(name_str);
364                 ns->sessions = NULL;
365         }
366
367         g_free(name_str);
368
369         return FALSE;
370 }
371 #endif
372
373 static gboolean bnep_setup(GIOChannel *chan,
374                         GIOCondition cond, gpointer user_data)
375 {
376         struct network_adapter *na = user_data;
377         struct network_server *ns;
378         uint8_t packet[BNEP_MTU];
379         struct bnep_setup_conn_req *req = (void *) packet;
380         uint16_t src_role, dst_role, rsp = BNEP_CONN_NOT_ALLOWED;
381         int n, sk;
382
383         if (cond & G_IO_NVAL)
384                 return FALSE;
385
386         if (cond & (G_IO_ERR | G_IO_HUP)) {
387                 error("Hangup or error on BNEP socket");
388                 return FALSE;
389         }
390
391         sk = g_io_channel_unix_get_fd(chan);
392
393         /* Reading BNEP_SETUP_CONNECTION_REQUEST_MSG */
394         n = read(sk, packet, sizeof(packet));
395         if (n < 0) {
396                 error("read(): %s(%d)", strerror(errno), errno);
397                 return FALSE;
398         }
399
400         /* Highest known Control command ID
401          * is BNEP_FILTER_MULT_ADDR_RSP = 0x06 */
402         if (req->type == BNEP_CONTROL &&
403                                 req->ctrl > BNEP_FILTER_MULT_ADDR_RSP) {
404                 uint8_t pkt[3];
405
406                 pkt[0] = BNEP_CONTROL;
407                 pkt[1] = BNEP_CMD_NOT_UNDERSTOOD;
408                 pkt[2] = req->ctrl;
409
410                 send(sk, pkt, sizeof(pkt), 0);
411
412                 return FALSE;
413         }
414
415         if (req->type != BNEP_CONTROL || req->ctrl != BNEP_SETUP_CONN_REQ)
416                 return FALSE;
417
418         rsp = bnep_setup_decode(req, &dst_role, &src_role);
419         if (rsp != BNEP_SUCCESS)
420                 goto reply;
421
422         rsp = BNEP_CONN_NOT_ALLOWED;
423
424         ns = find_server(na->servers, dst_role);
425         if (!ns) {
426                 error("Server unavailable: (0x%x)", dst_role);
427                 goto reply;
428         }
429
430         if (!ns->record_id) {
431                 error("Service record not available");
432                 goto reply;
433         }
434
435         if (!ns->bridge) {
436                 error("Bridge interface not configured");
437                 goto reply;
438         }
439
440         strncpy(na->setup->dev, BNEP_INTERFACE, 16);
441         na->setup->dev[15] = '\0';
442
443         if (bnep_server_add(sk, dst_role, ns->bridge, na->setup->dev,
444                                                         &na->setup->dst) < 0)
445                 goto reply;
446
447 #ifdef  __TIZEN_PATCH__
448 {
449         /* Emit connected signal to BT application */
450         const gchar *adapter_path = adapter_get_path(na->adapter);
451         const char *pdev = na->setup->dev;
452         char address[24] = { 0 };
453         char *paddr = address;
454
455         ba2str(&na->setup->dst, paddr);
456
457         ns->sessions = g_slist_append(ns->sessions, na->setup);
458
459         g_dbus_emit_signal(btd_get_dbus_connection(), adapter_path,
460                         NETWORK_SERVER_INTERFACE, "PeerConnected",
461                         DBUS_TYPE_STRING, &pdev,
462                         DBUS_TYPE_STRING, &paddr,
463                         DBUS_TYPE_INVALID);
464
465         na->setup->watch = g_io_add_watch_full(chan, G_PRIORITY_DEFAULT,
466                                         G_IO_HUP | G_IO_ERR | G_IO_NVAL,
467                                         server_disconnected_cb, ns, NULL);
468 }
469 #endif
470
471         na->setup = NULL;
472
473         rsp = BNEP_SUCCESS;
474
475 reply:
476         bnep_send_ctrl_rsp(sk, BNEP_CONTROL, BNEP_SETUP_CONN_RSP, rsp);
477
478         return FALSE;
479 }
480
481 static void connect_event(GIOChannel *chan, GError *err, gpointer user_data)
482 {
483         struct network_adapter *na = user_data;
484
485         if (err) {
486                 error("%s", err->message);
487                 setup_destroy(na);
488                 return;
489         }
490
491         g_io_channel_set_close_on_unref(chan, TRUE);
492
493         na->setup->watch = g_io_add_watch_full(chan, G_PRIORITY_DEFAULT,
494                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
495                                 bnep_setup, na, setup_destroy);
496 }
497
498 static void auth_cb(DBusError *derr, void *user_data)
499 {
500         struct network_adapter *na = user_data;
501         GError *err = NULL;
502
503         if (derr) {
504                 error("Access denied: %s", derr->message);
505                 goto reject;
506         }
507
508         if (!bt_io_accept(na->setup->io, connect_event, na, NULL,
509                                                         &err)) {
510                 error("bt_io_accept: %s", err->message);
511                 g_error_free(err);
512                 goto reject;
513         }
514
515         return;
516
517 reject:
518         g_io_channel_shutdown(na->setup->io, TRUE, NULL);
519         setup_destroy(na);
520 }
521
522 static void confirm_event(GIOChannel *chan, gpointer user_data)
523 {
524         struct network_adapter *na = user_data;
525         struct network_server *ns;
526         bdaddr_t src, dst;
527         char address[18];
528         GError *err = NULL;
529         guint ret;
530
531         bt_io_get(chan, &err,
532                         BT_IO_OPT_SOURCE_BDADDR, &src,
533                         BT_IO_OPT_DEST_BDADDR, &dst,
534                         BT_IO_OPT_DEST, address,
535                         BT_IO_OPT_INVALID);
536         if (err) {
537                 error("%s", err->message);
538                 g_error_free(err);
539                 goto drop;
540         }
541
542         DBG("BNEP: incoming connect from %s", address);
543
544         if (na->setup) {
545                 error("Refusing connect from %s: setup in progress", address);
546                 goto drop;
547         }
548
549         ns = find_server(na->servers, BNEP_SVC_NAP);
550         if (!ns || !ns->record_id || !ns->bridge)
551                 goto drop;
552
553         na->setup = g_new0(struct network_session, 1);
554         bacpy(&na->setup->dst, &dst);
555         na->setup->io = g_io_channel_ref(chan);
556
557         ret = btd_request_authorization(&src, &dst, BNEP_SVC_UUID,
558                                         auth_cb, na);
559         if (ret == 0) {
560                 error("Refusing connect from %s", address);
561                 setup_destroy(na);
562                 goto drop;
563         }
564
565         return;
566
567 drop:
568         g_io_channel_shutdown(chan, TRUE, NULL);
569 }
570
571 int server_init(gboolean secure)
572 {
573         security = secure;
574
575         return 0;
576 }
577
578 static uint32_t register_server_record(struct network_server *ns)
579 {
580         sdp_record_t *record;
581
582         record = server_record_new(ns->name, ns->id);
583         if (!record) {
584                 error("Unable to allocate new service record");
585                 return 0;
586         }
587
588         if (adapter_service_add(ns->na->adapter, record) < 0) {
589                 error("Failed to register service record");
590                 sdp_record_free(record);
591                 return 0;
592         }
593
594         DBG("got record id 0x%x", record->handle);
595
596         return record->handle;
597 }
598
599 static void server_remove_sessions(struct network_server *ns)
600 {
601         GSList *list;
602
603         for (list = ns->sessions; list; list = list->next) {
604                 struct network_session *session = list->data;
605
606                 if (*session->dev == '\0')
607                         continue;
608
609                 bnep_server_delete(ns->bridge, session->dev, &session->dst);
610         }
611
612 #ifndef __TIZEN_PATCH__
613         g_slist_free_full(ns->sessions, session_free);
614
615         ns->sessions = NULL;
616 #endif
617 }
618
619 static void server_disconnect(DBusConnection *conn, void *user_data)
620 {
621         struct network_server *ns = user_data;
622
623         server_remove_sessions(ns);
624
625         ns->watch_id = 0;
626
627         if (ns->record_id) {
628                 adapter_service_remove(ns->na->adapter, ns->record_id);
629                 ns->record_id = 0;
630         }
631
632         g_free(ns->bridge);
633         ns->bridge = NULL;
634 }
635
636 static DBusMessage *register_server(DBusConnection *conn,
637                                 DBusMessage *msg, void *data)
638 {
639         struct network_adapter *na = data;
640         struct network_server *ns;
641         DBusMessage *reply;
642         const char *uuid, *bridge;
643
644         if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &uuid,
645                                 DBUS_TYPE_STRING, &bridge, DBUS_TYPE_INVALID))
646                 return btd_error_invalid_args(msg);
647
648         ns = find_server_by_uuid(na->servers, uuid);
649         if (ns == NULL)
650                 return btd_error_failed(msg, "Invalid UUID");
651
652         if (ns->record_id)
653                 return btd_error_already_exists(msg);
654
655         reply = dbus_message_new_method_return(msg);
656         if (!reply)
657                 return NULL;
658
659         ns->record_id = register_server_record(ns);
660         if (!ns->record_id)
661                 return btd_error_failed(msg, "SDP record registration failed");
662
663         g_free(ns->bridge);
664         ns->bridge = g_strdup(bridge);
665
666         ns->watch_id = g_dbus_add_disconnect_watch(conn,
667                                         dbus_message_get_sender(msg),
668                                         server_disconnect, ns, NULL);
669
670         return reply;
671 }
672
673 static DBusMessage *unregister_server(DBusConnection *conn,
674                                         DBusMessage *msg, void *data)
675 {
676         struct network_adapter *na = data;
677         struct network_server *ns;
678         DBusMessage *reply;
679         const char *uuid;
680
681         if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &uuid,
682                                                         DBUS_TYPE_INVALID))
683                 return btd_error_invalid_args(msg);
684
685         ns = find_server_by_uuid(na->servers, uuid);
686         if (!ns)
687                 return btd_error_failed(msg, "Invalid UUID");
688
689 #ifdef  __TIZEN_PATCH__
690         if (!ns->record_id)
691                 return btd_error_not_available(msg);
692 #endif
693
694         reply = dbus_message_new_method_return(msg);
695         if (!reply)
696                 return NULL;
697
698         g_dbus_remove_watch(conn, ns->watch_id);
699
700         server_disconnect(conn, ns);
701
702         return reply;
703 }
704
705 static void adapter_free(struct network_adapter *na)
706 {
707         if (na->io != NULL) {
708                 g_io_channel_shutdown(na->io, TRUE, NULL);
709                 g_io_channel_unref(na->io);
710         }
711
712         setup_destroy(na);
713         btd_adapter_unref(na->adapter);
714         g_free(na);
715 }
716
717 static void server_free(void *data)
718 {
719         struct network_server *ns = data;
720
721         if (!ns)
722                 return;
723
724         server_remove_sessions(ns);
725
726         if (ns->record_id)
727                 adapter_service_remove(ns->na->adapter, ns->record_id);
728
729         g_dbus_remove_watch(btd_get_dbus_connection(), ns->watch_id);
730         g_free(ns->name);
731         g_free(ns->bridge);
732
733         g_free(ns);
734 }
735
736 static void path_unregister(void *data)
737 {
738         struct network_adapter *na = data;
739
740         DBG("Unregistered interface %s on path %s",
741                 NETWORK_SERVER_INTERFACE, adapter_get_path(na->adapter));
742
743         g_slist_free_full(na->servers, server_free);
744
745         adapters = g_slist_remove(adapters, na);
746         adapter_free(na);
747 }
748
749 #ifdef __TIZEN_PATCH__
750 static DBusMessage *disconnect_device(DBusConnection *conn, DBusMessage *msg,
751                                                                 void *data)
752 {
753         struct network_adapter *na = data;
754         struct network_server *ns;
755         struct network_session *session;
756         const char *addr = NULL;
757         bdaddr_t dst_addr;
758
759         if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &addr,
760                                                         DBUS_TYPE_INVALID))
761                 return btd_error_invalid_args(msg);
762
763         ns = find_server(na->servers, BNEP_SVC_NAP);
764
765         str2ba(addr, &dst_addr);
766         session = find_session_by_addr(ns->sessions, dst_addr);
767
768         if (session == NULL)
769                 return btd_error_failed(msg, "No active session");
770
771         if (session->io == NULL)
772                 return btd_error_not_connected(msg);
773
774         bnep_if_down_wrapper(session->dev);
775         bnep_conndel_wrapper(&dst_addr);
776
777         return dbus_message_new_method_return(msg);
778 }
779
780 static DBusMessage *get_properties(DBusConnection *conn,
781                                         DBusMessage *msg, void *data)
782 {
783         struct network_adapter *na = data;
784         struct network_server *ns;
785         struct network_session *session;
786         const char *addr = NULL;
787         bdaddr_t dst_addr;
788         DBusMessage *reply;
789         DBusMessageIter iter;
790         DBusMessageIter dict;
791         dbus_bool_t connected;
792         const char *property;
793
794         if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &addr,
795                                                         DBUS_TYPE_INVALID))
796                 return btd_error_invalid_args(msg);
797
798         reply = dbus_message_new_method_return(msg);
799         if (!reply)
800                 return NULL;
801
802         dbus_message_iter_init_append(reply, &iter);
803
804         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
805                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
806                 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
807                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
808
809         ns = find_server(na->servers, BNEP_SVC_NAP);
810
811         str2ba(addr, &dst_addr);
812         session = find_session_by_addr(ns->sessions, dst_addr);
813
814         connected = (session && session->io) ? TRUE : FALSE;
815         dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN, &connected);
816
817         /* Interface */
818         property = session ? session->dev : "";
819         dict_append_entry(&dict, "Interface", DBUS_TYPE_STRING, &property);
820
821         dbus_message_iter_close_container(&iter, &dict);
822
823         return reply;
824 }
825 #endif
826
827 #ifdef  __TIZEN_PATCH__
828 static GDBusSignalTable server_signals[] = {
829         { GDBUS_SIGNAL("PeerConnected",
830                         GDBUS_ARGS({ "device", "s" }, { "address", "s" })) },
831         { GDBUS_SIGNAL("PeerDisconnected",
832                         GDBUS_ARGS({ "device", "s" }, { "address", "s" })) },
833         { }
834 };
835 #endif
836
837 static const GDBusMethodTable server_methods[] = {
838         { GDBUS_METHOD("Register",
839                         GDBUS_ARGS({ "uuid", "s" }, { "bridge", "s" }), NULL,
840                         register_server) },
841         { GDBUS_METHOD("Unregister",
842                         GDBUS_ARGS({ "uuid", "s" }), NULL,
843                         unregister_server) },
844 #ifdef __TIZEN_PATCH__
845         { GDBUS_METHOD("Disconnect",
846                         GDBUS_ARGS({ "address", "s" }), NULL,
847                         disconnect_device) },
848         { GDBUS_METHOD("GetProperties",
849                         GDBUS_ARGS({ "address", "s" }),
850                         GDBUS_ARGS({ "properties", "a{sv}" }),
851                         get_properties) },
852 #endif
853         { }
854 };
855
856 static struct network_adapter *create_adapter(struct btd_adapter *adapter)
857 {
858         struct network_adapter *na;
859         GError *err = NULL;
860
861         na = g_new0(struct network_adapter, 1);
862         na->adapter = btd_adapter_ref(adapter);
863
864         na->io = bt_io_listen(NULL, confirm_event, na, NULL, &err,
865                                 BT_IO_OPT_SOURCE_BDADDR,
866                                 btd_adapter_get_address(adapter),
867                                 BT_IO_OPT_PSM, BNEP_PSM,
868                                 BT_IO_OPT_OMTU, BNEP_MTU,
869                                 BT_IO_OPT_IMTU, BNEP_MTU,
870                                 BT_IO_OPT_SEC_LEVEL,
871                                 security ? BT_IO_SEC_MEDIUM : BT_IO_SEC_LOW,
872                                 BT_IO_OPT_INVALID);
873         if (!na->io) {
874                 error("%s", err->message);
875                 g_error_free(err);
876                 adapter_free(na);
877                 return NULL;
878         }
879
880         return na;
881 }
882
883 int server_register(struct btd_adapter *adapter, uint16_t id)
884 {
885         struct network_adapter *na;
886         struct network_server *ns;
887         const char *path;
888
889         na = find_adapter(adapters, adapter);
890         if (!na) {
891                 na = create_adapter(adapter);
892                 if (!na)
893                         return -EINVAL;
894                 adapters = g_slist_append(adapters, na);
895         }
896
897         ns = find_server(na->servers, id);
898         if (ns)
899                 return 0;
900
901         ns = g_new0(struct network_server, 1);
902
903         ns->name = g_strdup("Network service");
904
905         path = adapter_get_path(adapter);
906
907         if (g_slist_length(na->servers) > 0)
908                 goto done;
909
910 #ifndef  __TIZEN_PATCH__
911         if (!g_dbus_register_interface(btd_get_dbus_connection(),
912                                         path, NETWORK_SERVER_INTERFACE,
913                                         server_methods, NULL, NULL,
914                                         na, path_unregister)) {
915                 error("D-Bus failed to register %s interface",
916                                                 NETWORK_SERVER_INTERFACE);
917                 server_free(ns);
918                 return -1;
919         }
920 #else
921         ns->sessions = NULL;
922
923         if (!g_dbus_register_interface(btd_get_dbus_connection(),
924                                         path, NETWORK_SERVER_INTERFACE,
925                                         server_methods, server_signals,
926                                         NULL,
927                                         na, path_unregister)) {
928                 error("D-Bus failed to register %s interface",
929                                         NETWORK_SERVER_INTERFACE);
930                 server_free(ns);
931                 return -1;
932         }
933 #endif
934
935         DBG("Registered interface %s on path %s", NETWORK_SERVER_INTERFACE,
936                                                                         path);
937
938 done:
939         bacpy(&ns->src, btd_adapter_get_address(adapter));
940         ns->id = id;
941         ns->na = na;
942         ns->record_id = 0;
943         na->servers = g_slist_append(na->servers, ns);
944
945         return 0;
946 }
947
948 int server_unregister(struct btd_adapter *adapter, uint16_t id)
949 {
950         struct network_adapter *na;
951         struct network_server *ns;
952
953         na = find_adapter(adapters, adapter);
954         if (!na)
955                 return -EINVAL;
956
957         ns = find_server(na->servers, id);
958         if (!ns)
959                 return -EINVAL;
960
961         na->servers = g_slist_remove(na->servers, ns);
962         server_free(ns);
963
964         if (g_slist_length(na->servers) > 0)
965                 return 0;
966
967         g_dbus_unregister_interface(btd_get_dbus_connection(),
968                                                 adapter_get_path(adapter),
969                                                 NETWORK_SERVER_INTERFACE);
970
971         return 0;
972 }