resolved the code rule warnings
[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
227         DBG("+");
228         fd = g_io_channel_unix_get_fd(io);
229         conn_info = info->conn_info;
230
231         if (cond & G_IO_HUP) {
232                 ERR("Socket %d hang up", fd);
233                 goto fail;
234         }
235
236         if (cond & (G_IO_ERR | G_IO_NVAL)) {
237                 ERR("Socket %d error", fd);
238                 goto fail;
239         }
240
241         if (!conn_info) {
242                 ERR("conn_info is NULL");
243                 return TRUE;
244         }
245
246         /* Read data from application */
247         if (g_io_channel_read_chars(io, (gchar *)buff,
248                         BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
249                         &len, &err) == G_IO_STATUS_ERROR) {
250                 if (err)
251                         ERR("IO Channel read error: %s", err->message);
252                 else
253                         ERR("IO Channel read error client");
254                 goto fail;
255         }
256
257         DBG("len: %d", len);
258         if (0 == len) {
259                 ERR("Other end of socket is closed");
260                 goto fail;
261         }
262
263         /* Send data to remote device */
264         sent = write_all(conn_info->stack_fd, buff, len);
265         if (sent < 0) {
266                 ERR("write(): %s", strerror(errno));
267                 goto fail;
268         }
269
270         DBG("-");
271         return TRUE;
272 fail:
273         __rfcomm_cb_data_remove(info);
274         return FALSE;
275 }
276
277 static gboolean stack_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
278 {
279         unsigned int len;
280         int sent;
281         rfcomm_cb_data_t *info = data;
282         rfcomm_conn_info_t *conn_info;
283         unsigned char buff[BT_HAL_RFCOMM_MAX_BUFFER_SIZE];
284         GError *err = NULL;
285         int fd;
286
287         DBG("+");
288
289         fd = g_io_channel_unix_get_fd(io);
290         conn_info = info->conn_info;
291
292         if (cond & G_IO_HUP) {
293                 ERR("Socket %d hang up", fd);
294                 goto fail;
295         }
296
297         if (cond & (G_IO_ERR | G_IO_NVAL)) {
298                 ERR("Socket %d error", fd);
299                 goto fail;
300         }
301
302         if (!conn_info) {
303                 ERR("conn_info is NULL");
304                 return TRUE;
305         }
306
307         /* Read data from remote device */
308         if (g_io_channel_read_chars(io, (gchar *)buff,
309                         BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
310                         &len, &err) == G_IO_STATUS_ERROR) {
311                 if (err)
312                         ERR("IO Channel read error: %s", err->message);
313                 else
314                         ERR("IO Channel read error client");
315                 goto fail;
316         }
317
318         DBG("len: %d", len);
319         if (0 == len) {
320                 ERR("Other end of socket is closed");
321                 goto fail;
322         }
323
324         /* Send data to application */
325         sent = write_all(conn_info->hal_fd, buff, len);
326         if (sent < 0) {
327                 ERR("write(): %s", strerror(errno));
328                 goto fail;
329         }
330
331         DBG("-");
332         return TRUE;
333 fail:
334         __rfcomm_cb_data_remove(info);
335         return FALSE;
336 }
337
338 static int __new_connection(const char *path, int fd, bt_bdaddr_t *addr)
339 {
340         char address[BT_HAL_ADDRESS_STRING_SIZE];
341         rfcomm_cb_data_t *info;
342         rfcomm_conn_info_t *conn_info;
343         struct hal_ev_sock_connect ev;
344         GIOCondition cond;
345         int len;
346         GIOChannel *io;
347
348         /* TODO: Temperary, later need to fill correct channel form correct place */
349         int chan = 0;
350
351         if (NULL == path || NULL == addr) {
352                 ERR("NULL == path || NULL = addr");
353                 return -1;
354         }
355
356         info = __find_rfcomm_info_from_path(path);
357         if (info == NULL)
358                 return -1;
359
360         conn_info = info->conn_info;
361         _bt_hal_convert_addr_type_to_string(address, addr->address);
362         if (conn_info == NULL) {
363                 ERR("conn_info is NULL for dev:[%s]", address);
364                 return -1;
365         }
366
367         if (write(conn_info->hal_fd, &chan, sizeof(chan)) != sizeof(chan)) {
368                 ERR("Error sending RFCOMM channel");
369                 goto fail;
370         }
371
372         conn_info->stack_fd = fd;
373         DBG("Remote address: %s, RFCOMM fd: %d", address, conn_info->stack_fd);
374
375         /* Send rfcomm connected event */
376         memset(&ev, 0, sizeof(ev));
377         ev.size = sizeof(ev);
378         memcpy(ev.bdaddr, addr->address, 6);
379         ev.status = BT_STATUS_SUCCESS;
380         len = write_all(conn_info->hal_fd, (unsigned char *)&ev, sizeof(ev));
381         if (len < 0) {
382                 ERR("%s", strerror(errno));
383                 goto fail;
384         }
385
386         if (len != sizeof(ev)) {
387                 ERR("Error sending connect event");
388                 goto fail;;
389         }
390
391         /* Handle events from App */
392         cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
393         io = g_io_channel_unix_new(conn_info->hal_fd);
394         conn_info->hal_watch = g_io_add_watch(io, cond, app_event_cb, info);
395         g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
396         g_io_channel_unref(io);
397
398         /* Handle rfcomm events from bluez */
399         cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
400         io = g_io_channel_unix_new(conn_info->stack_fd);
401         conn_info->bt_watch = g_io_add_watch(io, cond, stack_event_cb, info);
402         g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
403         g_io_channel_unref(io);
404
405         return 0;
406 fail:
407         __rfcomm_cb_data_remove(info);
408         return -1;
409 }
410
411 static void __bt_connect_response_cb(GDBusProxy *proxy,
412                 GAsyncResult *res, gpointer user_data)
413 {
414         GError *error = NULL;
415         rfcomm_cb_data_t *cb_data;
416
417         DBG("+");
418
419         cb_data = user_data;
420         if (cb_data == NULL) {
421                 ERR("cb_data == NULL");
422                 return;
423         }
424
425         if (!g_dbus_proxy_call_finish(proxy, res, &error)) {
426                 ERR("Error : %s \n", error->message);
427                 __rfcomm_cb_data_remove(cb_data);
428                 g_error_free(error);
429         }
430
431         if (proxy)
432                 g_object_unref(proxy);
433
434         DBG("-");
435 }
436
437 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
438                 GAsyncResult *res, gpointer user_data)
439 {
440         rfcomm_cb_data_t *cb_data;
441         int ret = 0;
442         GError *err = NULL;
443         bt_hal_register_profile_info_t info = {0};
444         char dev_address[BT_HAL_ADDRESS_STRING_SIZE];
445         const char *path;
446
447         DBG("+");
448
449         cb_data = user_data;
450         if (!cb_data) {
451                 ERR("cb_data == NULL");
452                 return;
453         }
454
455         path = g_dbus_proxy_get_object_path(proxy);
456         _bt_hal_convert_device_path_to_address(path, dev_address);
457         DBG("Device Adress [%s]", dev_address);
458
459         g_dbus_proxy_call_finish(proxy, res, &err);
460         if (proxy)
461                 g_object_unref(proxy);
462         if (err != NULL) {
463                 ERR("Error occured in Proxy call [%s]\n", err->message);
464                 __rfcomm_cb_data_remove(cb_data);
465                 goto done;
466         } else {
467                 INFO("Services are Updated checking required uuid is there");
468                 /* Check here for uuid present */
469                 ret = _bt_hal_discover_service_uuids(dev_address, cb_data->uuid);
470                 if (ret == BT_STATUS_SUCCESS) {
471                         info.uuid = (char *)cb_data->uuid;
472                         info.obj_path = cb_data->obj_path;
473                         info.role = "client";
474
475                         ret = _bt_hal_register_profile(&info, FALSE);
476                         if (ret < 0)
477                                 DBG("Error: register profile");
478                         ret = _bt_hal_connect_profile(dev_address, cb_data->uuid,
479                                         __bt_connect_response_cb, cb_data);
480                         if (ret != BT_STATUS_SUCCESS) {
481                                 ERR("ConnectProfile failed");
482                                 __rfcomm_cb_data_remove(cb_data);
483                                 goto done;
484                         }
485                 } else {
486                         ERR("remote uuid not found");
487                         __rfcomm_cb_data_remove(cb_data);
488                 }
489         }
490 done:
491         if (err)
492                 g_clear_error(&err);
493 }
494
495 static rfcomm_cb_data_t *__get_rfcomm_cb_data(char *remote_uuid)
496 {
497         int id;
498         int object_id;
499         char *path;
500         rfcomm_cb_data_t *cb_data;
501
502         DBG("+");
503
504         id = __rfcomm_assign_id();
505         if (id < 0) {
506                 ERR("__rfcomm_assign_id failed");
507                 return NULL;
508         }
509
510         path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
511         object_id = _bt_hal_register_new_gdbus_object(path, __new_connection);
512         if (object_id < 0) {
513                 ERR("_bt_hal_register_new_gdbus_object failed");
514                 g_free(path);
515                 __rfcomm_delete_id(id);
516                 return NULL;
517         }
518
519         cb_data = g_malloc0(sizeof(rfcomm_cb_data_t));
520         g_strlcpy(cb_data->uuid, remote_uuid, BT_HAL_UUID_STRING_LEN);
521         cb_data->obj_path = path;
522         cb_data->object_id = object_id;
523         cb_data->id = id;
524
525         DBG("-");
526         return cb_data;
527 }
528
529 static rfcomm_conn_info_t *__rfcomm_create_conn_info(char *addr, int *sock)
530 {
531         int fds[2] = {-1, -1};
532         rfcomm_conn_info_t *conn;
533
534         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
535                 ERR("socketpair(): %s", strerror(errno));
536                 *sock = -1;
537                 return NULL;
538         }
539
540         conn = g_malloc0(sizeof(rfcomm_conn_info_t));
541         g_strlcpy(conn->remote_addr, addr, BT_HAL_ADDRESS_STRING_SIZE);
542         conn->hal_fd = fds[0];
543         conn->stack_fd = -1;
544         *sock = fds[1];
545
546         DBG("hal_fd: %d, sock: %d", conn->hal_fd, *sock);
547
548         return conn;
549 }
550
551 int _bt_hal_dbus_handler_rfcomm_connect(unsigned char *addr, unsigned char *uuid, int *sock)
552 {
553         int ret;
554         rfcomm_cb_data_t *cb_data;
555         rfcomm_conn_info_t *conn;
556         char remote_addr[BT_HAL_ADDRESS_STRING_SIZE];
557         char remote_uuid[BT_HAL_UUID_STRING_LEN];
558
559         if (!addr) {
560                 ERR("remote_addr is NULL");
561                 return BT_STATUS_PARM_INVALID;
562         }
563
564         if (!uuid) {
565                 ERR("remote_uuid is NULL");
566                 return BT_STATUS_PARM_INVALID;
567         }
568
569         if (!sock) {
570                 ERR("sock is NULL");
571                 return BT_STATUS_PARM_INVALID;
572         }
573
574         _bt_hal_convert_uuid_type_to_string(remote_uuid, uuid);
575         cb_data = __get_rfcomm_cb_data(remote_uuid);
576         if (!cb_data)
577                 return BT_STATUS_FAIL;
578
579         _bt_hal_convert_addr_type_to_string(remote_addr, addr);
580         DBG("Connecting to %s, uuid %s", remote_addr, remote_uuid);
581         conn = __rfcomm_create_conn_info(remote_addr, sock);
582         if (!conn) {
583                 __bt_free_cb_data(cb_data);
584                 return BT_STATUS_FAIL;
585         }
586
587         cb_data->conn_info = conn;
588         ret = _bt_hal_discover_services(remote_addr, (char *)remote_uuid,
589                         __bt_discover_service_response_cb, cb_data);
590         if (ret != BT_STATUS_SUCCESS) {
591                 ERR("Error returned while service discovery");
592                 __bt_free_conn(conn);
593                 __bt_free_cb_data(cb_data);
594                 return BT_STATUS_FAIL;
595         }
596
597         INFO("Adding callback information to rfcomm_clients");
598         rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
599
600         return BT_STATUS_SUCCESS;
601 }
602
603 /*************************** RFCOMM Server Implementation ***************************/
604 static rfcomm_server_data_t *__find_rfcomm_server_info_with_path(const gchar *path)
605 {
606         GSList *l;
607
608         for (l = rfcomm_servers; l != NULL; l = l->next) {
609                 rfcomm_server_data_t *info = l->data;
610
611                 if (g_strcmp0(info->obj_path, path) == 0)
612                         return info;
613         }
614
615         return NULL;
616 }
617
618 static rfcomm_server_data_t *__find_rfcomm_server_info_from_uuid(const char *uuid)
619 {
620         GSList *l;
621
622         for (l = rfcomm_servers; l != NULL; l = l->next) {
623                  rfcomm_server_data_t *info = l->data;
624
625                 if (g_strcmp0(info->uuid, uuid) == 0)
626                         return info;
627         }
628
629         return NULL;
630 }
631
632 static int __send_sock_fd(int sock_fd, const void *buf, int size, int send_fd)
633 {
634         ssize_t ret;
635         struct msghdr msg;
636         struct cmsghdr *cmsg;
637         struct iovec iov;
638         char cmsg_buf[CMSG_SPACE(sizeof(int))];
639
640         if (sock_fd == -1 || send_fd == -1)
641                 return -1;
642
643         memset(&msg, 0, sizeof(msg));
644         memset(cmsg_buf, 0, sizeof(cmsg_buf));
645
646         msg.msg_control = cmsg_buf;
647         msg.msg_controllen = sizeof(cmsg_buf);
648
649         cmsg = CMSG_FIRSTHDR(&msg);
650         cmsg->cmsg_level = SOL_SOCKET;
651         cmsg->cmsg_type = SCM_RIGHTS;
652         cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));
653
654         memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(send_fd));
655
656         iov.iov_base = (unsigned char *) buf;
657         iov.iov_len = size;
658
659         msg.msg_iov = &iov;
660         msg.msg_iovlen = 1;
661
662         ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL);
663         if (ret < 0)
664                 ERR("sendmsg(): sock_fd %d send_fd %d: %s",
665                                 sock_fd, send_fd, strerror(errno));
666
667         return ret;
668 }
669
670 int __new_server_connection(const char *path, int fd, bt_bdaddr_t *addr)
671 {
672         int ret;
673         rfcomm_server_data_t *info;
674         struct hal_ev_sock_connect ev;
675
676         DBG("%s %d", path, fd);
677
678         if (0 > fd) {
679                 ERR("Invalid socket fd received");
680                 return -1;
681         }
682
683         info = __find_rfcomm_server_info_with_path(path);
684         if (info == NULL) {
685                 ERR("rfcomm server info not found");
686                 return -1;
687         }
688
689         /* Send rfcomm client connected event */
690         memset(&ev, 0, sizeof(ev));
691         ev.size = sizeof(ev);
692         memcpy(ev.bdaddr, addr->address, 6);
693         ev.status = BT_STATUS_SUCCESS;
694         ret = __send_sock_fd(info->server_fd, (void *)&ev, sizeof(ev), fd);
695         if (ret < 0) {
696                 ERR("Error sending connect event");
697                 close(fd);
698                 return -1;
699         }
700
701         /* Remove local reference to client socket fd */
702         close(fd);
703         return 0;
704 }
705
706 static void __free_rfcomm_server_data(rfcomm_server_data_t *data)
707 {
708         DBG("+");
709
710         if (!data) {
711                 ERR("server data is NULL");
712                 return;
713         }
714
715         if (data->id >= 0)
716                 __rfcomm_delete_id(data->id);
717
718         if (data->object_id > 0)
719                 _bt_hal_unregister_gdbus_object(data->object_id);
720
721         if (data->obj_path) {
722                 INFO("Unregister profile");
723                 _bt_hal_unregister_profile(data->obj_path);
724         }
725
726         if (0 < data->server_fd)
727                 close(data->server_fd);
728
729         if (data->server_watch > 0) {
730                 g_source_remove(data->server_watch);
731                 data->server_watch = 0;
732         }
733
734         g_free(data->obj_path);
735         g_free(data->svc_name);
736         g_free(data);
737
738         DBG("-");
739 }
740
741 static void __remove_rfcomm_server(rfcomm_server_data_t *data)
742 {
743
744         DBG("+");
745
746         rfcomm_servers = g_slist_remove(rfcomm_servers, data);
747         __free_rfcomm_server_data(data);
748
749         DBG("-");
750
751 }
752
753 static int __register_rfcomm_server(rfcomm_server_data_t *server_data)
754 {
755         int ret;
756         bt_hal_register_profile_info_t profile_info;
757
758         memset(&profile_info, 0x00, sizeof(bt_hal_register_profile_info_t));
759
760         profile_info.authentication = TRUE;
761         profile_info.authorization = TRUE;
762         profile_info.obj_path = server_data->obj_path;
763         profile_info.role = "server";
764         profile_info.service = server_data->uuid;
765         profile_info.uuid = server_data->uuid;
766
767         INFO("uuid %s, svc: %s, role: %s",
768                 profile_info.uuid, profile_info.service, profile_info.role);
769         ret = _bt_hal_register_profile(&profile_info, TRUE);
770         if (ret < 0) {
771                 ERR("Error: register profile");
772                 return BT_STATUS_FAIL;
773         }
774
775         return BT_STATUS_SUCCESS;
776 }
777
778 static rfcomm_server_data_t *__create_rfcomm_server(char *uuid, const char *svc_name, int *sock)
779 {
780         int id;
781         int ret;
782         int object_id;
783         char *path;
784         int fds[2] = {-1, -1};
785         rfcomm_server_data_t *data;
786
787         DBG("+");
788
789         data = __find_rfcomm_server_info_from_uuid(uuid);
790         if (data) {
791                 ERR("RFCOMM Server exists with UUID: %s, sv_name: %s", uuid, data->svc_name);
792                 return NULL;
793         }
794
795         id = __rfcomm_assign_id();
796         if (id < 0) {
797                 ERR("__rfcomm_assign_id failed");
798                 return NULL;
799         }
800
801         path = g_strdup_printf("/org/socket/server/%d/%d", getpid(), id);
802         object_id = _bt_hal_register_new_gdbus_object(path, __new_server_connection);
803         if (object_id < 0) {
804                 ERR("_bt_hal_register_new_gdbus_object failed");
805                 g_free(path);
806                 __rfcomm_delete_id(id);
807                 return NULL;
808         }
809
810         data = g_malloc0(sizeof(rfcomm_server_data_t));
811         g_strlcpy(data->uuid, uuid, BT_HAL_UUID_STRING_LEN);
812         data->svc_name = g_strdup(svc_name);
813         data->obj_path = path;
814         data->object_id = object_id;
815         data->id = id;
816
817         ret = __register_rfcomm_server(data);
818         if (ret != BT_STATUS_SUCCESS) {
819                 ERR("Error returned while registering service");
820                 __free_rfcomm_server_data(data);
821                 return NULL;
822         }
823
824         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
825                 ERR("socketpair(): %s", strerror(errno));
826                 __free_rfcomm_server_data(data);
827                 *sock = -1;
828                 return NULL;
829         }
830
831         data->server_fd = fds[0];
832         *sock = fds[1];
833
834         DBG("server_fd: %d, sock: %d", data->server_fd, *sock);
835         return data;
836 }
837
838 static gboolean __server_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
839 {
840         gsize len;
841         rfcomm_server_data_t *info = data;
842         unsigned char buff[BT_HAL_RFCOMM_MAX_BUFFER_SIZE];
843         GError *err = NULL;
844
845         DBG("+");
846
847         if (cond & G_IO_HUP) {
848                 ERR("Socket %d hang up", info->server_fd);
849                 goto fail;
850         }
851
852         if (cond & (G_IO_ERR | G_IO_NVAL)) {
853                 ERR("Socket %d error", info->server_fd);
854                 goto fail;
855         }
856
857         /* Read data from application */
858         if (g_io_channel_read_chars(io, (gchar *)buff,
859                         BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
860                         &len, &err) == G_IO_STATUS_ERROR) {
861                 if (err)
862                         ERR("IO Channel read error: %s", err->message);
863                 else
864                         ERR("IO Channel read error client");
865                 goto fail;
866         }
867
868         DBG("len: %d", len);
869         if (0 == len) {
870                 ERR("Other end of socket is closed");
871                 goto fail;
872         }
873
874         DBG("-");
875         return TRUE;
876 fail:
877         __remove_rfcomm_server(info);
878         return FALSE;
879 }
880
881 int _bt_hal_dbus_handler_rfcomm_listen(const char *svc_name, unsigned char *uuid, int *sock)
882 {
883         int chan = 0;
884         rfcomm_server_data_t *server_data;
885         char server_uuid[BT_HAL_UUID_STRING_LEN];
886         GIOCondition cond;
887         GIOChannel *io;
888
889         if (!svc_name) {
890                 ERR("svc_name is NULL");
891                 return BT_STATUS_PARM_INVALID;
892         }
893
894         if (!uuid) {
895                 ERR("uuid is NULL");
896                 return BT_STATUS_PARM_INVALID;
897         }
898
899         if (!sock) {
900                 ERR("sock is NULL");
901                 return BT_STATUS_PARM_INVALID;
902         }
903
904         _bt_hal_convert_uuid_type_to_string(server_uuid, uuid);
905         server_data = __create_rfcomm_server(server_uuid, svc_name, sock);
906         if (!server_data)
907                 return BT_STATUS_BUSY;
908
909         /* TODO: Temperary, later need to fill correct channel form correct place */
910         if (write(server_data->server_fd, &chan, sizeof(chan)) != sizeof(chan)) {
911                 ERR("Error sending RFCOMM channel");
912                 __free_rfcomm_server_data(server_data);
913                 *sock = -1;
914                 return BT_STATUS_FAIL;
915         }
916
917         /* Rfcomm server: Handle events from Application */
918         cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
919         io = g_io_channel_unix_new(server_data->server_fd);
920         server_data->server_watch = g_io_add_watch(io, cond, __server_event_cb, server_data);
921         g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
922         g_io_channel_unref(io);
923
924         INFO("Adding server information to rfcomm_servers list");
925         rfcomm_servers = g_slist_append(rfcomm_servers, server_data);
926
927         DBG("-");
928         return BT_STATUS_SUCCESS;
929 }
930
931 gboolean _is_rfcomm_server_uuid(const char *uuid)
932 {
933         DBG("+");
934
935         if (!uuid)
936                 return FALSE;
937
938         if (NULL == __find_rfcomm_server_info_from_uuid(uuid))
939                 return FALSE;
940
941         DBG("-");
942         return TRUE;
943 }