Prevent the buffer overflow error for adv data
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-rfcomm-dbus-handler.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Atul Kumar Rai <a.rai@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28
29 #include <glib.h>
30 #include <gio/gio.h>
31 #include <dlog.h>
32 #include <vconf.h>
33
34 #include "bt-hal-rfcomm-dbus-handler.h"
35 #include "bt-hal-dbus-common-utils.h"
36
37 #define BT_HAL_RFCOMM_ID_MAX 245
38 #define BT_HAL_RFCOMM_MAX_BUFFER_SIZE 1024
39
40 typedef struct {
41         char remote_addr[BT_HAL_ADDRESS_STRING_SIZE];
42         int hal_fd;
43         unsigned int hal_watch;
44         int stack_fd;
45         unsigned int bt_watch;
46 } rfcomm_conn_info_t;
47
48 typedef struct {
49         char uuid[BT_HAL_UUID_STRING_LEN];
50         char *device_path;
51         char *obj_path;
52         int object_id;
53         int id;
54         rfcomm_conn_info_t *conn_info;
55 } rfcomm_cb_data_t;
56
57 typedef struct {
58         char uuid[BT_HAL_UUID_STRING_LEN];
59         char *svc_name;
60         char *obj_path;
61         int object_id;
62         int id;
63         int server_fd;
64         unsigned int server_watch;
65         GSList *conn_list;
66 } rfcomm_server_data_t;
67
68 static GSList *rfcomm_clients;
69 static GSList *rfcomm_servers;
70 static int latest_id = -1;
71 static gboolean id_used[BT_HAL_RFCOMM_ID_MAX];
72
73 int __rfcomm_assign_id(void)
74 {
75         int index;
76
77         DBG("latest_id: %d", latest_id);
78
79         index = latest_id + 1;
80         if (index >= BT_HAL_RFCOMM_ID_MAX)
81                 index = 0;
82
83         DBG("index: %d", index);
84
85         while (id_used[index] == TRUE) {
86                 if (index == latest_id) {
87                         /* No available ID */
88                         ERR("All request ID is used");
89                         return -1;
90                 }
91
92                 index++;
93                 if (index >= BT_HAL_RFCOMM_ID_MAX)
94                         index = 0;
95         }
96
97         latest_id = index;
98         id_used[index] = TRUE;
99         DBG("Assigned Id: %d", latest_id);
100
101         return latest_id;
102 }
103
104 void __rfcomm_delete_id(int id)
105 {
106         if (id >= BT_HAL_RFCOMM_ID_MAX || id < 0) {
107                 ERR("Invalid id %d", id);
108                 return;
109         }
110
111         id_used[id] = FALSE;
112         latest_id = id - 1;
113         DBG("id: %d, latest_id: %d", id, latest_id);
114 }
115
116 /*************************** RFCOMM Client Implementation ***************************/
117 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
118 {
119         GSList *l;
120
121         for (l = rfcomm_clients; l != NULL; l = l->next) {
122                 rfcomm_cb_data_t *info = l->data;
123
124                 if (info != NULL)
125                         if (g_strcmp0(info->obj_path, path) == 0)
126                                 return info;
127         }
128
129         return NULL;
130 }
131
132 static void __bt_free_conn(rfcomm_conn_info_t *conn)
133 {
134         DBG("+");
135
136         if (conn == NULL)
137                 return;
138
139         if (0 < conn->hal_fd)
140                 close(conn->hal_fd);
141
142         if (conn->hal_watch > 0) {
143                 g_source_remove(conn->hal_watch);
144                 conn->hal_watch = 0;
145         }
146
147         if (0 < conn->stack_fd)
148                 close(conn->stack_fd);
149
150         if (conn->bt_watch > 0) {
151                 g_source_remove(conn->bt_watch);
152                 conn->bt_watch = 0;
153         }
154
155         g_free(conn);
156         DBG("-");
157 }
158
159 static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
160 {
161         DBG("+");
162
163         if (cb_data->id >= 0)
164                 __rfcomm_delete_id(cb_data->id);
165
166         if (cb_data->object_id > 0)
167                 _bt_hal_unregister_gdbus_object(cb_data->object_id);
168
169         if (cb_data->obj_path) {
170                 INFO("Unregister profile");
171                 _bt_hal_unregister_profile(cb_data->obj_path);
172         }
173
174         g_free(cb_data->obj_path);
175
176         g_free(cb_data->device_path);
177         g_free(cb_data);
178
179         DBG("-");
180 }
181
182 static void __rfcomm_cb_data_remove(rfcomm_cb_data_t *info)
183 {
184         if (!info) {
185                 ERR("info == NULL");
186                 return;
187         }
188
189         rfcomm_clients = g_slist_remove(rfcomm_clients, info);
190         __bt_free_conn(info->conn_info);
191         __bt_free_cb_data(info);
192 }
193
194 static int write_all(int fd, unsigned char *buf, int len)
195 {
196         int sent = 0;
197
198         while (len > 0) {
199                 int written;
200
201                 written = write(fd, buf, len);
202                 if (written < 0) {
203                         if (errno == EINTR || errno == EAGAIN)
204                                 continue;
205                         return -1;
206                 }
207
208                 if (!written)
209                         return 0;
210
211                 len -= written; buf += written; sent += written;
212         }
213
214         return sent;
215 }
216
217 static gboolean app_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
218 {
219         gsize len;
220         int sent;
221         rfcomm_cb_data_t *info = data;
222         rfcomm_conn_info_t *conn_info;
223         unsigned char buff[BT_HAL_RFCOMM_MAX_BUFFER_SIZE];
224         GError *err = NULL;
225         int fd;
226         char err_msg[256] = {0, };
227
228         DBG("+");
229         fd = g_io_channel_unix_get_fd(io);
230         conn_info = info->conn_info;
231
232         if (cond & G_IO_HUP) {
233                 ERR("Socket %d hang up", fd);
234                 goto fail;
235         }
236
237         if (cond & (G_IO_ERR | G_IO_NVAL)) {
238                 ERR("Socket %d error", fd);
239                 goto fail;
240         }
241
242         if (!conn_info) {
243                 ERR("conn_info is NULL");
244                 return TRUE;
245         }
246
247         /* Read data from application */
248         if (g_io_channel_read_chars(io, (gchar *)buff,
249                         BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
250                         &len, &err) == G_IO_STATUS_ERROR) {
251                 if (err)
252                         ERR("IO Channel read error: %s", err->message);
253                 else
254                         ERR("IO Channel read error client");
255                 goto fail;
256         }
257
258         DBG("len: %zu", len);
259         if (0 == len) {
260                 ERR("Other end of socket is closed");
261                 goto fail;
262         }
263
264         /* Send data to remote device */
265         sent = write_all(conn_info->stack_fd, buff, len);
266         if (sent < 0) {
267                 strerror_r(errno, err_msg, sizeof(err_msg));
268                 ERR("write(): %s", err_msg);
269                 goto fail;
270         }
271
272         DBG("-");
273         return TRUE;
274 fail:
275         __rfcomm_cb_data_remove(info);
276         return FALSE;
277 }
278
279 static gboolean stack_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
280 {
281         unsigned int len;
282         int sent;
283         rfcomm_cb_data_t *info = data;
284         rfcomm_conn_info_t *conn_info;
285         unsigned char buff[BT_HAL_RFCOMM_MAX_BUFFER_SIZE];
286         GError *err = NULL;
287         int fd;
288         char err_msg[256] = {0, };
289
290         DBG("+");
291
292         fd = g_io_channel_unix_get_fd(io);
293         conn_info = info->conn_info;
294
295         if (cond & G_IO_HUP) {
296                 ERR("Socket %d hang up", fd);
297                 goto fail;
298         }
299
300         if (cond & (G_IO_ERR | G_IO_NVAL)) {
301                 ERR("Socket %d error", fd);
302                 goto fail;
303         }
304
305         if (!conn_info) {
306                 ERR("conn_info is NULL");
307                 return TRUE;
308         }
309
310         /* Read data from remote device */
311         if (g_io_channel_read_chars(io, (gchar *)buff,
312                         BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
313                         (gsize *)&len, &err) == G_IO_STATUS_ERROR) {
314                 if (err)
315                         ERR("IO Channel read error: %s", err->message);
316                 else
317                         ERR("IO Channel read error client");
318                 goto fail;
319         }
320
321         DBG("len: %d", len);
322         if (0 == len) {
323                 ERR("Other end of socket is closed");
324                 goto fail;
325         }
326
327         /* Send data to application */
328         sent = write_all(conn_info->hal_fd, buff, len);
329         if (sent < 0) {
330                 strerror_r(errno, err_msg, sizeof(err_msg));
331                 ERR("write(): %s", err_msg);
332                 goto fail;
333         }
334
335         DBG("-");
336         return TRUE;
337 fail:
338         __rfcomm_cb_data_remove(info);
339         return FALSE;
340 }
341
342 static int __new_connection(const char *path, int fd, bt_bdaddr_t *addr)
343 {
344         char address[BT_HAL_ADDRESS_STRING_SIZE];
345         rfcomm_cb_data_t *info;
346         rfcomm_conn_info_t *conn_info;
347         struct hal_ev_sock_connect ev;
348         GIOCondition cond;
349         int len;
350         GIOChannel *io;
351         char err_msg[256] = {0, };
352
353         /* TODO: Temperary, later need to fill correct channel form correct place */
354         int chan = 0;
355
356         if (NULL == path || NULL == addr) {
357                 ERR("NULL == path || NULL = addr");
358                 return -1;
359         }
360
361         info = __find_rfcomm_info_from_path(path);
362         if (info == NULL)
363                 return -1;
364
365         conn_info = info->conn_info;
366         _bt_hal_convert_addr_type_to_string(address, addr->address);
367         if (conn_info == NULL) {
368                 ERR("conn_info is NULL for dev:[%s]", address);
369                 return -1;
370         }
371
372         if (write(conn_info->hal_fd, &chan, sizeof(chan)) != sizeof(chan)) {
373                 ERR("Error sending RFCOMM channel");
374                 goto fail;
375         }
376
377         conn_info->stack_fd = fd;
378         DBG("Remote address: %s, RFCOMM fd: %d", address, conn_info->stack_fd);
379
380         /* Send rfcomm connected event */
381         memset(&ev, 0, sizeof(ev));
382         ev.size = sizeof(ev);
383         memcpy(ev.bdaddr, addr->address, 6);
384         ev.status = BT_STATUS_SUCCESS;
385         len = write_all(conn_info->hal_fd, (unsigned char *)&ev, sizeof(ev));
386         if (len < 0) {
387                 strerror_r(errno, err_msg, sizeof(err_msg));
388                 ERR("%s", err_msg);
389                 goto fail;
390         }
391
392         if (len != sizeof(ev)) {
393                 ERR("Error sending connect event");
394                 goto fail;;
395         }
396
397         /* Handle events from App */
398         cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
399         io = g_io_channel_unix_new(conn_info->hal_fd);
400         conn_info->hal_watch = g_io_add_watch(io, cond, app_event_cb, info);
401         g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
402         g_io_channel_unref(io);
403
404         /* Handle rfcomm events from bluez */
405         cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
406         io = g_io_channel_unix_new(conn_info->stack_fd);
407         conn_info->bt_watch = g_io_add_watch(io, cond, stack_event_cb, info);
408         g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
409         g_io_channel_unref(io);
410
411         return 0;
412 fail:
413         __rfcomm_cb_data_remove(info);
414         return -1;
415 }
416
417 static void __bt_connect_response_cb(GDBusProxy *proxy,
418                 GAsyncResult *res, gpointer user_data)
419 {
420         GError *error = NULL;
421         rfcomm_cb_data_t *cb_data;
422         GVariant *result;
423
424         DBG("+");
425
426         cb_data = user_data;
427         if (cb_data == NULL) {
428                 ERR("cb_data == NULL");
429                 return;
430         }
431
432         result = g_dbus_proxy_call_finish(proxy, res, &error);
433         if (!result) {
434                 ERR("Error : %s \n", error->message);
435                 __rfcomm_cb_data_remove(cb_data);
436                 g_error_free(error);
437         } else {
438                 g_variant_unref(result);
439         }
440
441         if (proxy)
442                 g_object_unref(proxy);
443
444         DBG("-");
445 }
446
447 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
448                 GAsyncResult *res, gpointer user_data)
449 {
450         rfcomm_cb_data_t *cb_data;
451         int ret = 0;
452         GError *err = NULL;
453         bt_hal_register_profile_info_t info = {0};
454         char dev_address[BT_HAL_ADDRESS_STRING_SIZE];
455         const char *path;
456
457         DBG("+");
458
459         cb_data = user_data;
460         if (!cb_data) {
461                 ERR("cb_data == NULL");
462                 return;
463         }
464
465         path = g_dbus_proxy_get_object_path(proxy);
466         _bt_hal_convert_device_path_to_address(path, dev_address);
467         DBG("Device Adress [%s]", dev_address);
468
469         g_dbus_proxy_call_finish(proxy, res, &err);
470         if (proxy)
471                 g_object_unref(proxy);
472         if (err != NULL) {
473                 ERR("Error occured in Proxy call [%s]\n", err->message);
474                 __rfcomm_cb_data_remove(cb_data);
475                 goto done;
476         } else {
477                 INFO("Services are Updated checking required uuid is there");
478                 /* Check here for uuid present */
479                 ret = _bt_hal_discover_service_uuids(dev_address, cb_data->uuid);
480                 if (ret == BT_STATUS_SUCCESS) {
481                         info.uuid = (char *)cb_data->uuid;
482                         info.obj_path = cb_data->obj_path;
483                         info.role = "client";
484
485                         ret = _bt_hal_register_profile(&info, FALSE);
486                         if (ret < 0)
487                                 DBG("Error: register profile");
488                         ret = _bt_hal_connect_profile(dev_address, cb_data->uuid,
489                                         __bt_connect_response_cb, cb_data);
490                         if (ret != BT_STATUS_SUCCESS) {
491                                 ERR("ConnectProfile failed");
492                                 __rfcomm_cb_data_remove(cb_data);
493                                 goto done;
494                         }
495                 } else {
496                         ERR("remote uuid not found");
497                         __rfcomm_cb_data_remove(cb_data);
498                 }
499         }
500 done:
501         if (err)
502                 g_clear_error(&err);
503 }
504
505 static rfcomm_cb_data_t *__get_rfcomm_cb_data(char *remote_uuid)
506 {
507         int id;
508         int object_id;
509         char *path;
510         rfcomm_cb_data_t *cb_data;
511
512         DBG("+");
513
514         id = __rfcomm_assign_id();
515         if (id < 0) {
516                 ERR("__rfcomm_assign_id failed");
517                 return NULL;
518         }
519
520         path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
521         object_id = _bt_hal_register_new_gdbus_object(path, __new_connection);
522         if (object_id < 0) {
523                 ERR("_bt_hal_register_new_gdbus_object failed");
524                 g_free(path);
525                 __rfcomm_delete_id(id);
526                 return NULL;
527         }
528
529         cb_data = g_malloc0(sizeof(rfcomm_cb_data_t));
530         g_strlcpy(cb_data->uuid, remote_uuid, BT_HAL_UUID_STRING_LEN);
531         cb_data->obj_path = path;
532         cb_data->object_id = object_id;
533         cb_data->id = id;
534
535         DBG("-");
536         return cb_data;
537 }
538
539 static rfcomm_conn_info_t *__rfcomm_create_conn_info(char *addr, int *sock)
540 {
541         int fds[2] = {-1, -1};
542         rfcomm_conn_info_t *conn;
543         char err_msg[256] = {0, };
544
545         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
546                 strerror_r(errno, err_msg, sizeof(err_msg));
547                 ERR("socketpair(): %s", err_msg);
548                 *sock = -1;
549                 return NULL;
550         }
551
552         conn = g_malloc0(sizeof(rfcomm_conn_info_t));
553         g_strlcpy(conn->remote_addr, addr, BT_HAL_ADDRESS_STRING_SIZE);
554         conn->hal_fd = fds[0];
555         conn->stack_fd = -1;
556         *sock = fds[1];
557
558         DBG("hal_fd: %d, sock: %d", conn->hal_fd, *sock);
559
560         return conn;
561 }
562
563 int _bt_hal_dbus_handler_rfcomm_connect(unsigned char *addr, unsigned char *uuid, int *sock)
564 {
565         int ret;
566         rfcomm_cb_data_t *cb_data;
567         rfcomm_conn_info_t *conn;
568         char remote_addr[BT_HAL_ADDRESS_STRING_SIZE];
569         char remote_uuid[BT_HAL_UUID_STRING_LEN];
570
571         if (!addr) {
572                 ERR("remote_addr is NULL");
573                 return BT_STATUS_PARM_INVALID;
574         }
575
576         if (!uuid) {
577                 ERR("remote_uuid is NULL");
578                 return BT_STATUS_PARM_INVALID;
579         }
580
581         if (!sock) {
582                 ERR("sock is NULL");
583                 return BT_STATUS_PARM_INVALID;
584         }
585
586         _bt_hal_convert_uuid_type_to_string(remote_uuid, uuid);
587         cb_data = __get_rfcomm_cb_data(remote_uuid);
588         if (!cb_data)
589                 return BT_STATUS_FAIL;
590
591         _bt_hal_convert_addr_type_to_string(remote_addr, addr);
592         DBG("Connecting to %s, uuid %s", remote_addr, remote_uuid);
593         conn = __rfcomm_create_conn_info(remote_addr, sock);
594         if (!conn) {
595                 __bt_free_cb_data(cb_data);
596                 return BT_STATUS_FAIL;
597         }
598
599         cb_data->conn_info = conn;
600         ret = _bt_hal_discover_services(remote_addr, (char *)remote_uuid,
601                         __bt_discover_service_response_cb, cb_data);
602         if (ret != BT_STATUS_SUCCESS) {
603                 ERR("Error returned while service discovery");
604                 __bt_free_conn(conn);
605                 __bt_free_cb_data(cb_data);
606                 return BT_STATUS_FAIL;
607         }
608
609         INFO("Adding callback information to rfcomm_clients");
610         rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
611
612         return BT_STATUS_SUCCESS;
613 }
614
615 /*************************** RFCOMM Server Implementation ***************************/
616 static rfcomm_server_data_t *__find_rfcomm_server_info_with_path(const gchar *path)
617 {
618         GSList *l;
619
620         for (l = rfcomm_servers; l != NULL; l = l->next) {
621                 rfcomm_server_data_t *info = l->data;
622
623                 if (g_strcmp0(info->obj_path, path) == 0)
624                         return info;
625         }
626
627         return NULL;
628 }
629
630 static rfcomm_server_data_t *__find_rfcomm_server_info_from_uuid(const char *uuid)
631 {
632         GSList *l;
633
634         for (l = rfcomm_servers; l != NULL; l = l->next) {
635                  rfcomm_server_data_t *info = l->data;
636
637                 if (g_strcmp0(info->uuid, uuid) == 0)
638                         return info;
639         }
640
641         return NULL;
642 }
643
644 static int __send_sock_fd(int sock_fd, const void *buf, int size, int send_fd)
645 {
646         ssize_t ret;
647         struct msghdr msg;
648         struct cmsghdr *cmsg;
649         struct iovec iov;
650         char cmsg_buf[CMSG_SPACE(sizeof(int))];
651         char err_msg[256] = {0, };
652
653         if (sock_fd == -1 || send_fd == -1)
654                 return -1;
655
656         memset(&msg, 0, sizeof(msg));
657         memset(cmsg_buf, 0, sizeof(cmsg_buf));
658
659         msg.msg_control = cmsg_buf;
660         msg.msg_controllen = sizeof(cmsg_buf);
661
662         cmsg = CMSG_FIRSTHDR(&msg);
663         cmsg->cmsg_level = SOL_SOCKET;
664         cmsg->cmsg_type = SCM_RIGHTS;
665         cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));
666
667         memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(send_fd));
668
669         iov.iov_base = (unsigned char *) buf;
670         iov.iov_len = size;
671
672         msg.msg_iov = &iov;
673         msg.msg_iovlen = 1;
674
675         ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL);
676         if (ret < 0) {
677                 strerror_r(errno, err_msg, sizeof(err_msg));
678                 ERR("sendmsg(): sock_fd %d send_fd %d: %s",
679                                 sock_fd, send_fd, err_msg);
680         }
681
682         return ret;
683 }
684
685 int __new_server_connection(const char *path, int fd, bt_bdaddr_t *addr)
686 {
687         int ret;
688         rfcomm_server_data_t *info;
689         struct hal_ev_sock_connect ev;
690
691         DBG("%s %d", path, fd);
692
693         if (0 > fd) {
694                 ERR("Invalid socket fd received");
695                 return -1;
696         }
697
698         info = __find_rfcomm_server_info_with_path(path);
699         if (info == NULL) {
700                 ERR("rfcomm server info not found");
701                 return -1;
702         }
703
704         /* Send rfcomm client connected event */
705         memset(&ev, 0, sizeof(ev));
706         ev.size = sizeof(ev);
707         memcpy(ev.bdaddr, addr->address, 6);
708         ev.status = BT_STATUS_SUCCESS;
709         ret = __send_sock_fd(info->server_fd, (void *)&ev, sizeof(ev), fd);
710         if (ret < 0) {
711                 ERR("Error sending connect event");
712                 close(fd);
713                 return -1;
714         }
715
716         /* Remove local reference to client socket fd */
717         close(fd);
718         return 0;
719 }
720
721 static void __free_rfcomm_server_data(rfcomm_server_data_t *data)
722 {
723         DBG("+");
724
725         if (!data) {
726                 ERR("server data is NULL");
727                 return;
728         }
729
730         if (data->id >= 0)
731                 __rfcomm_delete_id(data->id);
732
733         if (data->object_id > 0)
734                 _bt_hal_unregister_gdbus_object(data->object_id);
735
736         if (data->obj_path) {
737                 INFO("Unregister profile");
738                 _bt_hal_unregister_profile(data->obj_path);
739         }
740
741         if (0 < data->server_fd)
742                 close(data->server_fd);
743
744         if (data->server_watch > 0) {
745                 g_source_remove(data->server_watch);
746                 data->server_watch = 0;
747         }
748
749         g_free(data->obj_path);
750         g_free(data->svc_name);
751         g_free(data);
752
753         DBG("-");
754 }
755
756 static void __remove_rfcomm_server(rfcomm_server_data_t *data)
757 {
758
759         DBG("+");
760
761         rfcomm_servers = g_slist_remove(rfcomm_servers, data);
762         __free_rfcomm_server_data(data);
763
764         DBG("-");
765
766 }
767
768 static int __register_rfcomm_server(rfcomm_server_data_t *server_data)
769 {
770         int ret;
771         bt_hal_register_profile_info_t profile_info;
772
773         memset(&profile_info, 0x00, sizeof(bt_hal_register_profile_info_t));
774
775         profile_info.authentication = TRUE;
776         profile_info.authorization = TRUE;
777         profile_info.obj_path = server_data->obj_path;
778         profile_info.role = "server";
779         profile_info.service = server_data->uuid;
780         profile_info.uuid = server_data->uuid;
781
782         INFO("uuid %s, svc: %s, role: %s",
783                 profile_info.uuid, profile_info.service, profile_info.role);
784         ret = _bt_hal_register_profile(&profile_info, TRUE);
785         if (ret < 0) {
786                 ERR("Error: register profile");
787                 return BT_STATUS_FAIL;
788         }
789
790         return BT_STATUS_SUCCESS;
791 }
792
793 static rfcomm_server_data_t *__create_rfcomm_server(char *uuid, const char *svc_name, int *sock)
794 {
795         int id;
796         int ret;
797         int object_id;
798         char *path;
799         int fds[2] = {-1, -1};
800         rfcomm_server_data_t *data;
801         char err_msg[256] = {0, };
802
803         DBG("+");
804
805         data = __find_rfcomm_server_info_from_uuid(uuid);
806         if (data) {
807                 ERR("RFCOMM Server exists with UUID: %s, sv_name: %s", uuid, data->svc_name);
808                 return NULL;
809         }
810
811         id = __rfcomm_assign_id();
812         if (id < 0) {
813                 ERR("__rfcomm_assign_id failed");
814                 return NULL;
815         }
816
817         path = g_strdup_printf("/org/socket/server/%d/%d", getpid(), id);
818         object_id = _bt_hal_register_new_gdbus_object(path, __new_server_connection);
819         if (object_id < 0) {
820                 ERR("_bt_hal_register_new_gdbus_object failed");
821                 g_free(path);
822                 __rfcomm_delete_id(id);
823                 return NULL;
824         }
825
826         data = g_malloc0(sizeof(rfcomm_server_data_t));
827         g_strlcpy(data->uuid, uuid, BT_HAL_UUID_STRING_LEN);
828         data->svc_name = g_strdup(svc_name);
829         data->obj_path = path;
830         data->object_id = object_id;
831         data->id = id;
832
833         ret = __register_rfcomm_server(data);
834         if (ret != BT_STATUS_SUCCESS) {
835                 ERR("Error returned while registering service");
836                 __free_rfcomm_server_data(data);
837                 return NULL;
838         }
839
840         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
841                 strerror_r(errno, err_msg, sizeof(err_msg));
842                 ERR("socketpair(): %s", err_msg);
843                 __free_rfcomm_server_data(data);
844                 *sock = -1;
845                 return NULL;
846         }
847
848         data->server_fd = fds[0];
849         *sock = fds[1];
850
851         DBG("server_fd: %d, sock: %d", data->server_fd, *sock);
852         return data;
853 }
854
855 static gboolean __server_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
856 {
857         gsize len;
858         rfcomm_server_data_t *info = data;
859         unsigned char buff[BT_HAL_RFCOMM_MAX_BUFFER_SIZE];
860         GError *err = NULL;
861
862         DBG("+");
863
864         if (cond & G_IO_HUP) {
865                 ERR("Socket %d hang up", info->server_fd);
866                 goto fail;
867         }
868
869         if (cond & (G_IO_ERR | G_IO_NVAL)) {
870                 ERR("Socket %d error", info->server_fd);
871                 goto fail;
872         }
873
874         /* Read data from application */
875         if (g_io_channel_read_chars(io, (gchar *)buff,
876                         BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
877                         &len, &err) == G_IO_STATUS_ERROR) {
878                 if (err)
879                         ERR("IO Channel read error: %s", err->message);
880                 else
881                         ERR("IO Channel read error client");
882                 goto fail;
883         }
884
885         DBG("len: %zu", len);
886         if (0 == len) {
887                 ERR("Other end of socket is closed");
888                 goto fail;
889         }
890
891         DBG("-");
892         return TRUE;
893 fail:
894         __remove_rfcomm_server(info);
895         return FALSE;
896 }
897
898 int _bt_hal_dbus_handler_rfcomm_listen(const char *svc_name, unsigned char *uuid, int *sock)
899 {
900         int chan = 0;
901         rfcomm_server_data_t *server_data;
902         char server_uuid[BT_HAL_UUID_STRING_LEN];
903         GIOCondition cond;
904         GIOChannel *io;
905
906         if (!svc_name) {
907                 ERR("svc_name is NULL");
908                 return BT_STATUS_PARM_INVALID;
909         }
910
911         if (!uuid) {
912                 ERR("uuid is NULL");
913                 return BT_STATUS_PARM_INVALID;
914         }
915
916         if (!sock) {
917                 ERR("sock is NULL");
918                 return BT_STATUS_PARM_INVALID;
919         }
920
921         _bt_hal_convert_uuid_type_to_string(server_uuid, uuid);
922         server_data = __create_rfcomm_server(server_uuid, svc_name, sock);
923         if (!server_data)
924                 return BT_STATUS_BUSY;
925
926         /* TODO: Temperary, later need to fill correct channel form correct place */
927         if (write(server_data->server_fd, &chan, sizeof(chan)) != sizeof(chan)) {
928                 ERR("Error sending RFCOMM channel");
929                 __free_rfcomm_server_data(server_data);
930                 *sock = -1;
931                 return BT_STATUS_FAIL;
932         }
933
934         /* Rfcomm server: Handle events from Application */
935         cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
936         io = g_io_channel_unix_new(server_data->server_fd);
937         server_data->server_watch = g_io_add_watch(io, cond, __server_event_cb, server_data);
938         g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
939         g_io_channel_unref(io);
940
941         INFO("Adding server information to rfcomm_servers list");
942         rfcomm_servers = g_slist_append(rfcomm_servers, server_data);
943
944         DBG("-");
945         return BT_STATUS_SUCCESS;
946 }
947
948 gboolean _is_rfcomm_server_uuid(const char *uuid)
949 {
950         DBG("+");
951
952         if (!uuid)
953                 return FALSE;
954
955         if (NULL == __find_rfcomm_server_info_from_uuid(uuid))
956                 return FALSE;
957
958         DBG("-");
959         return TRUE;
960 }