Fix the issue on AppControl share (BT) operation
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-rfcomm-server.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <string.h>
19 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
20 #include <gio/gio.h>
21 #include <gio/gunixfdlist.h>
22 #include <sys/socket.h>
23 #endif
24
25 #include "bluetooth-api.h"
26 #include "bt-internal-types.h"
27
28 #include "bt-common.h"
29 #include "bt-request-sender.h"
30 #include "bt-event-handler.h"
31 #include "bt-dpm.h"
32
33 #define BLUETOOTH_SOCK_CONNECT_INFO_LEN 16
34 #define BATTERY_MONITOR_RFCOMM_INTERVAL 5
35
36 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
37
38 static GSList *rfcomm_nodes;
39
40 typedef struct {
41         bluetooth_device_address_t addr;
42         int fd;
43         guint watch_id;
44         gboolean disconnected;
45 } rfcomm_conn_t;
46
47 typedef struct {
48         guint object_id;
49         gchar *path;
50         int id;
51         char *uuid;
52         GSList *rfcomm_conns;
53         guint disconnect_idle_id;
54 } rfcomm_info_t;
55
56 static unsigned int rx_data = 0;
57 static guint rx_tag = 0;
58
59 static bool __rfcomm_record_rx_data(void)
60 {
61         if (rx_data) {
62                 int ret = _bt_common_send_rfcomm_rx_details(rx_data);
63                 if (ret == BLUETOOTH_ERROR_NONE) {
64                         rx_data = 0;
65                         return TRUE;
66                 } else {
67                         BT_ERR("RFCOMM rx data could not be registered");
68                 }
69         }
70
71         rx_data = 0;
72         rx_tag = 0;
73
74         return FALSE;
75 }
76
77 static rfcomm_info_t *__find_rfcomm_info_with_id(int id)
78 {
79         GSList *l;
80
81         for (l = rfcomm_nodes; l != NULL; l = l->next) {
82                 rfcomm_info_t *info = l->data;
83
84                 if (info->id == id)
85                         return info;
86         }
87
88         return NULL;
89 }
90
91 static rfcomm_info_t *__find_rfcomm_info_with_fd(int fd)
92 {
93         GSList *l;
94         GSList *ll;
95
96         for (l = rfcomm_nodes; l != NULL; l = l->next) {
97                 rfcomm_info_t *info = l->data;
98
99                 for (ll = info->rfcomm_conns; ll; ll = ll->next) {
100                         rfcomm_conn_t *conn = ll->data;
101
102                         if (conn && conn->fd == fd)
103                                 return info;
104                 }
105         }
106
107         return NULL;
108 }
109
110 static rfcomm_info_t *__find_rfcomm_info_with_path(const gchar *path)
111 {
112         GSList *l;
113
114         for (l = rfcomm_nodes; l != NULL; l = l->next) {
115                 rfcomm_info_t *info = l->data;
116
117                 if (g_ascii_strcasecmp(info->path, path) == 0)
118                         return info;
119         }
120
121         return NULL;
122 }
123
124 static rfcomm_info_t *__find_rfcomm_info_with_uuid(const char *uuid)
125 {
126         GSList *l;
127
128         for (l = rfcomm_nodes; l != NULL; l = l->next) {
129                 rfcomm_info_t *info = l->data;
130
131                 if (g_ascii_strcasecmp(info->uuid, uuid) == 0)
132                         return info;
133         }
134
135         return NULL;
136 }
137
138 static rfcomm_conn_t *__find_rfcomm_conn_with_fd(rfcomm_info_t *info,
139                                                       int fd)
140 {
141         GSList *l;
142         rfcomm_conn_t *conn;
143
144         for (l = info->rfcomm_conns; l; l = l->next) {
145                 conn = l->data;
146
147                 if (conn && conn->fd == fd)
148                         return conn;
149         }
150
151         return NULL;
152 }
153
154 static void __rfcomm_remove_conn(rfcomm_info_t *info, int fd)
155 {
156         rfcomm_conn_t *conn;
157
158         conn = __find_rfcomm_conn_with_fd(info, fd);
159         if (conn == NULL)
160                 return;
161
162         info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn);
163
164         if (conn->watch_id > 0)
165                 g_source_remove(conn->watch_id);
166         g_free(conn);
167 }
168
169 gboolean _check_uuid_path(char *path, char *uuid)
170 {
171         rfcomm_info_t *info = NULL;
172         info = __find_rfcomm_info_with_path(path);
173         if (!info)
174                 return FALSE;
175
176         if (g_ascii_strcasecmp(info->uuid, uuid) == 0)
177                 return TRUE;
178
179         return FALSE;
180 }
181
182 static void __connected_cb(rfcomm_info_t *info, rfcomm_conn_t *conn,
183                            bt_event_info_t *event_info)
184 {
185         bluetooth_rfcomm_connection_t conn_info;
186
187         memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
188
189         conn_info.device_role = RFCOMM_ROLE_SERVER;
190         g_strlcpy(conn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
191         conn_info.socket_fd = conn->fd;
192         conn_info.device_addr = conn->addr;
193         conn_info.server_id = info->id;
194
195         BT_INFO_C("### Connected [RFCOMM Server]");
196
197         if (_bt_common_send_rfcomm_conn_info(RFCOMM_ROLE_SERVER,
198                         TRUE, conn_info.socket_fd) != BLUETOOTH_ERROR_NONE)
199                 BT_ERR("Fail to send the connection info");
200
201         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
202                         BLUETOOTH_ERROR_NONE, &conn_info,
203                         event_info->cb, event_info->user_data);
204 }
205
206 static void __rfcomm_server_disconnect_conn(rfcomm_conn_t *conn,
207                                             rfcomm_info_t *info)
208 {
209         bluetooth_rfcomm_disconnection_t disconn_info;
210         bt_event_info_t *event_info;
211
212         if (conn == NULL)
213                 return;
214
215         if (conn->disconnected == FALSE)
216                 return;
217
218         if (conn->watch_id > 0) {
219                 g_source_remove(conn->watch_id);
220                 conn->watch_id = 0;
221         }
222
223         event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
224         if (event_info == NULL) {
225                 BT_ERR("event_info is NULL");
226                 __rfcomm_remove_conn(info, conn->fd);
227                 return;
228         }
229
230         memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
231         disconn_info.device_role = RFCOMM_ROLE_SERVER;
232         g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
233         disconn_info.device_addr = conn->addr;
234
235         BT_INFO("Disconnected FD [%d]", conn->fd);
236         disconn_info.socket_fd = conn->fd;
237
238         if (_bt_common_send_rfcomm_conn_info(RFCOMM_ROLE_SERVER,
239                         FALSE, disconn_info.socket_fd) != BLUETOOTH_ERROR_NONE)
240                 BT_ERR("Fail to send the connection info");
241
242         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
243                         BLUETOOTH_ERROR_NONE, &disconn_info,
244                         event_info->cb, event_info->user_data);
245
246         __rfcomm_remove_conn(info, conn->fd);
247 }
248
249 static gboolean __rfcomm_server_disconnect(rfcomm_info_t *info)
250 {
251         BT_INFO_C("### Disconnected [RFCOMM Server]");
252
253         if (g_slist_find(rfcomm_nodes, info) == NULL) {
254                 BT_INFO("rfcomm resource is already freed");
255                 return FALSE;
256         }
257
258         info->disconnect_idle_id = 0;
259
260         g_slist_foreach(info->rfcomm_conns,
261                         (GFunc)__rfcomm_server_disconnect_conn, info);
262
263         BT_DBG("-");
264         return FALSE;
265 }
266
267 static gboolean __is_error_by_disconnect(GError *err)
268 {
269         return !g_strcmp0(err->message, "Connection reset by peer") ||
270                         !g_strcmp0(err->message, "Connection timed out") ||
271                         !g_strcmp0(err->message, "Software caused connection abort");
272 }
273
274 static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond,
275                                                                 gpointer data)
276 {
277         char *buffer = NULL;
278         gsize len = 0;
279         int result = BLUETOOTH_ERROR_NONE;
280         rfcomm_info_t *info = data;
281         rfcomm_conn_t *conn;
282         bt_event_info_t *event_info;
283         bluetooth_rfcomm_received_data_t data_r;
284         GIOStatus status = G_IO_STATUS_NORMAL;
285         GError *err = NULL;
286         int fd;
287
288         retv_if(info == NULL, FALSE);
289
290         fd = g_io_channel_unix_get_fd(chan);
291         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
292                 BT_ERR_C("RFComm Server disconnected: %d", fd);
293
294                 if (info->disconnect_idle_id > 0) {
295                         BT_INFO("Disconnect idle still not process remove source");
296                         g_source_remove(info->disconnect_idle_id);
297                         info->disconnect_idle_id = 0;
298                 }
299
300                 conn = __find_rfcomm_conn_with_fd(info, fd);
301                 if (conn == NULL) {
302                         BT_ERR("No Connection info found with FD [%d]", fd);
303                         return FALSE;
304                 }
305
306                 if (conn->disconnected == FALSE) {
307                         close(conn->fd);
308                         conn->disconnected = TRUE;
309                 }
310                 __rfcomm_server_disconnect(info);
311                 return FALSE;
312         }
313
314         buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
315
316         status =  g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
317                                                 &len, &err);
318         if (status != G_IO_STATUS_NORMAL) {
319                 BT_ERR("IO Channel read is failed with %d", status);
320
321                 g_free(buffer);
322                 if (!err)
323                         return TRUE;
324
325                 BT_ERR("IO Channel read error [%s]", err->message);
326                 if (status == G_IO_STATUS_ERROR &&
327                     __is_error_by_disconnect(err)) {
328                         BT_ERR("cond : %d", cond);
329                         g_error_free(err);
330
331                         if (info->disconnect_idle_id > 0) {
332                                 BT_INFO("Disconnect idle still not process remove source");
333                                 g_source_remove(info->disconnect_idle_id);
334                                 info->disconnect_idle_id = 0;
335                         }
336
337                         conn = __find_rfcomm_conn_with_fd(info, fd);
338                         if (conn == NULL) {
339                                 BT_ERR("No Connection info found with FD [%d]", fd);
340                                 return FALSE;
341                         }
342
343                         if (conn->disconnected == FALSE) {
344                                 close(conn->fd);
345                                 conn->disconnected = TRUE;
346                         }
347                         __rfcomm_server_disconnect(info);
348                         return FALSE;
349                 }
350                 g_error_free(err);
351                 return TRUE;
352         }
353
354         if (len == 0)
355                 BT_ERR("Length is zero");
356
357         event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
358         if (event_info == NULL) {
359                 BT_ERR("event_info is NULL. Unable to invoke the callback");
360                 g_free(buffer);
361                 return TRUE;
362         }
363
364         data_r.socket_fd = fd;
365         data_r.buffer_size = len;
366         data_r.buffer = buffer;
367
368         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
369                         result, &data_r,
370                         event_info->cb, event_info->user_data);
371
372         if (bluetooth_get_battery_monitor_state()) {
373                 if (rx_tag == 0) {
374                         BT_DBG("Adding rfcomm rx timeout function for battery monitor");
375                         rx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_rx_data, NULL);
376                 }
377                 rx_data += len;
378         }
379
380         g_free(buffer);
381
382         return TRUE;
383 }
384
385 int new_server_connection(const char *path, int fd, bluetooth_device_address_t *addr)
386 {
387         rfcomm_info_t *info;
388         rfcomm_conn_t *conn;
389         GIOChannel *data_io;
390         bt_event_info_t *event_info;
391
392         BT_INFO("%s %d", path, fd);
393
394         info = __find_rfcomm_info_with_path(path);
395         if (info == NULL) {
396                 BT_ERR("rfcomm info is NULL");
397                 return -1;
398         }
399
400         if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
401                 char addr_str[20];
402
403                 BT_ERR("Not allow to use SPP profile");
404
405                 close(fd);
406                 _bt_convert_addr_type_to_string(addr_str, addr->addr);
407                 _bt_disconnect_ext_profile(addr_str, info->path);
408
409                 return -1;
410         }
411
412         conn = g_new0(rfcomm_conn_t, 1);
413         conn->fd = fd;
414         memcpy(&conn->addr, addr, sizeof(bluetooth_device_address_t));
415         info->rfcomm_conns = g_slist_append(info->rfcomm_conns, conn);
416
417         data_io = g_io_channel_unix_new(conn->fd);
418
419         g_io_channel_set_encoding(data_io, NULL, NULL);
420         g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
421
422         conn->watch_id = g_io_add_watch(data_io,
423                            G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
424                            __data_received_cb, info);
425
426         g_io_channel_unref(data_io);
427
428         event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
429         if (event_info)
430                 __connected_cb(info, conn, event_info);
431
432         return 0;
433 }
434
435 static rfcomm_info_t *__register_method()
436 {
437         gchar *path;
438         rfcomm_info_t *info;
439         int object_id;
440         int id;
441
442         id = __rfcomm_assign_id();
443         if (id < 0)
444                 return NULL;
445
446         path = g_strdup_printf("/org/socket/server/%d/%d", getpid(), id);
447
448         object_id = _bt_register_new_conn(path, new_server_connection);
449         if (object_id < 0) {
450                 __rfcomm_delete_id(id);
451                 return NULL;
452         }
453         info = g_new0(rfcomm_info_t, 1);
454         info->object_id = (guint)object_id;
455         info->path = path;
456         info->id = id;
457
458         rfcomm_nodes = g_slist_append(rfcomm_nodes, info);
459
460         return info;
461 }
462
463 static rfcomm_info_t *__register_method_2(const char *path, const char *bus_name)
464 {
465         rfcomm_info_t *info;
466         int object_id;
467
468         object_id = _bt_register_new_conn_ex(path, bus_name, new_server_connection);
469         if (object_id < 0)
470                 return NULL;
471
472         info = g_new0(rfcomm_info_t, 1);
473         info->object_id = (guint)object_id;
474         info->path = g_strdup(path);
475         info->id = -1;
476
477         rfcomm_nodes = g_slist_append(rfcomm_nodes, info);
478
479         return info;
480 }
481
482 void free_rfcomm_conn(rfcomm_conn_t *conn, rfcomm_info_t *info)
483 {
484         if (conn->disconnected == FALSE) {
485                 close(conn->fd);
486                 conn->disconnected = TRUE;
487         }
488         __rfcomm_server_disconnect_conn(conn, info);
489 }
490
491 void free_rfcomm_info(rfcomm_info_t *info)
492 {
493         BT_DBG("");
494
495         if (info->disconnect_idle_id > 0) {
496                 BT_INFO("Disconnect idle still not process remove source");
497                 g_source_remove(info->disconnect_idle_id);
498                 info->disconnect_idle_id = 0;
499         }
500
501         __rfcomm_delete_id(info->id);
502         _bt_unregister_gdbus(info->object_id);
503
504         g_slist_foreach(info->rfcomm_conns, (GFunc)free_rfcomm_conn, info);
505
506         g_free(info->path);
507         g_free(info->uuid);
508         g_free(info);
509 }
510
511 void _bt_rfcomm_server_free_all(void)
512 {
513         BT_DBG("Free all the servers");
514
515         g_slist_free_full(rfcomm_nodes, (GDestroyNotify)free_rfcomm_info);
516         rfcomm_nodes = NULL;
517 }
518
519 void _bt_rfcomm_server_disconnect_all(void)
520 {
521         GSList *server;
522         GSList *conn;
523         char addr[20];
524
525         BT_INFO(" ### Disconnect all RFCOMM server connections");
526
527         for (server = rfcomm_nodes; server; ) {
528                 rfcomm_info_t *info = server->data;
529
530                 for (conn = info->rfcomm_conns; conn; conn = conn->next) {
531                         rfcomm_conn_t *conn_info = conn->data;
532
533                         if (conn_info == NULL)
534                                 continue;
535
536                         if (conn_info->watch_id == 0 || conn_info->disconnected)
537                                 continue;
538
539                         close(conn_info->fd);
540                         conn_info->disconnected = TRUE;
541
542                         _bt_convert_addr_type_to_string(addr,
543                                                         conn_info->addr.addr);
544                         _bt_disconnect_ext_profile(addr, info->path);
545                 }
546
547                 server = server->next;
548                 __rfcomm_server_disconnect(info);
549         }
550
551         return;
552 }
553
554 void _bt_rfcomm_server_reset_timer(void)
555 {
556         if (rx_tag > 0) {
557                 g_source_remove(rx_tag);
558                 rx_tag = 0;
559         }
560
561         rx_data = 0;
562 }
563 #else
564
565 #define BT_RFCOMM_SERVER_ID_MAX 254
566
567 typedef struct {
568         char addr[BT_ADDRESS_STRING_SIZE];
569         int sock_fd;
570         int watch_id;
571         int server_id;
572 } rfcomm_remote_client_info_t;
573
574 typedef struct {
575         char uuid[BLUETOOTH_UUID_STRING_MAX];
576         int server_id;
577         int server_fd;
578         int watch_id;
579         int max_pending_conn;
580         gboolean auto_accept;
581         char pending_addr[BT_ADDRESS_STRING_SIZE];
582         GSList *conn_list;
583 } rfcomm_server_info_t;
584
585 static GSList *rfcomm_servers;
586 static gboolean id_used[BT_RFCOMM_SERVER_ID_MAX];
587 int latest_id = 0;
588
589 int __rfcomm_assign_server_id(void)
590 {
591         int index;
592
593         BT_DBG("latest_id: %d", latest_id);
594
595         index = latest_id + 1;
596
597         if (index >= BT_RFCOMM_SERVER_ID_MAX)
598                 index = 0;
599
600         BT_DBG("index: %d", index);
601
602         while (id_used[index] == TRUE) {
603                 if (index == latest_id) {
604                         /* No available ID */
605                         BT_ERR("All request ID is used");
606                         return -1;
607                 }
608
609                 index++;
610
611                 if (index >= BT_RFCOMM_SERVER_ID_MAX)
612                         index = 0;
613         }
614
615         latest_id = index;
616         id_used[index] = TRUE;
617
618         BT_DBG("Assigned Id: %d", latest_id);
619
620         return latest_id;
621 }
622
623 void __rfcomm_delete_server_id(int id)
624 {
625         ret_if(id >= BT_RFCOMM_SERVER_ID_MAX);
626         ret_if(id < 0);
627
628         id_used[id] = FALSE;
629
630         /* Next server will use this ID */
631         latest_id = id - 1;
632 }
633
634 static rfcomm_server_info_t *__get_rfcomm_server_info_with_uuid(const char *uuid)
635 {
636         GSList *l;
637
638         if (!uuid)
639                 return NULL;
640
641         for (l = rfcomm_servers; l != NULL; l = l->next) {
642                 rfcomm_server_info_t *info = l->data;
643
644                 if (!strncasecmp(info->uuid, uuid, strlen(info->uuid)))
645                         return info;
646         }
647
648         return NULL;
649 }
650
651 int _get_rfcomm_server_id(char *uuid, gboolean *auto_accept)
652 {
653         rfcomm_server_info_t *server_info;
654
655         server_info = __get_rfcomm_server_info_with_uuid(uuid);
656         if (!server_info)
657                 return -1;
658
659         *auto_accept = server_info->auto_accept;
660         return server_info->server_id;
661 }
662
663 static rfcomm_server_info_t *__get_rfcomm_server_info_with_id(int server_id)
664 {
665         GSList *l;
666
667         for (l = rfcomm_servers; l != NULL; l = l->next) {
668                 rfcomm_server_info_t *info = l->data;
669                 if (!info)
670                         continue;
671
672                 BT_DBG("info->server_fd: %d, sock_fd:%d", info->server_id, server_id);
673                 if (info->server_id == server_id)
674                         return info;
675         }
676
677         return NULL;
678 }
679
680 void _bt_rfcomm_server_set_pending_conn(int server_id, char *address)
681 {
682         rfcomm_server_info_t *server_info;
683
684
685         if (!address)
686                 return;
687
688         server_info = __get_rfcomm_server_info_with_id(server_id);
689         if (!server_info)
690                 return;
691
692         g_strlcpy(server_info->pending_addr, address, BT_ADDRESS_STRING_SIZE);
693 }
694
695 static rfcomm_remote_client_info_t *__get_rfcomm_rem_client_info_with_fd(int sock_fd)
696 {
697         GSList *l;
698         GSList *l1;
699
700         for (l = rfcomm_servers; l != NULL; l = l->next) {
701                 rfcomm_server_info_t *info = l->data;
702
703                 if (!info)
704                         continue;
705
706                 for (l1 = info->conn_list; l1 != NULL; l1 = l1->next) {
707                         rfcomm_remote_client_info_t *client_info = l1->data;
708                         if (!client_info)
709                                 continue;
710
711                         if (client_info->sock_fd == sock_fd)
712                                 return client_info;
713                 }
714         }
715
716         return NULL;
717 }
718
719 static rfcomm_remote_client_info_t *__get_rfcomm_rem_client_info_with_addr(char *addr)
720 {
721         GSList *l;
722         GSList *l1;
723
724         retv_if(NULL == addr, NULL);
725
726         for (l = rfcomm_servers; l != NULL; l = l->next) {
727                 rfcomm_server_info_t *info = l->data;
728
729                 if (!info)
730                         continue;
731
732                 for (l1 = info->conn_list; l1 != NULL; l1 = l1->next) {
733                         rfcomm_remote_client_info_t *client_info = l1->data;
734                         if (!client_info)
735                                 continue;
736
737                         if (!strncasecmp(client_info->addr, addr, strlen(client_info->addr)))
738                                 return client_info;
739                 }
740         }
741
742         return NULL;
743 }
744
745 static void __remove_remote_client_info(rfcomm_remote_client_info_t *rem_client)
746 {
747         BT_DBG("+");
748
749         if (rem_client == NULL)
750                 return;
751
752         if (0 < rem_client->sock_fd) {
753                 shutdown(rem_client->sock_fd, SHUT_RDWR);
754                 close(rem_client->sock_fd);
755         }
756
757         if (rem_client->watch_id > 0)
758                 g_source_remove(rem_client->watch_id);
759
760         g_free(rem_client);
761
762         BT_DBG("-");
763 }
764
765 static void __handle_rfcomm_client_disconnected(rfcomm_server_info_t *server_info,
766                 rfcomm_remote_client_info_t *rem_client)
767 {
768         bluetooth_rfcomm_disconnection_t disconn_info;
769         bt_event_info_t *event_info;
770
771         BT_DBG("+");
772
773         if (rem_client == NULL || server_info == NULL)
774                 return;
775
776         event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
777         if (event_info == NULL)
778                 return;
779
780         memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
781         disconn_info.device_role = RFCOMM_ROLE_SERVER;
782         g_strlcpy(disconn_info.uuid, server_info->uuid, BLUETOOTH_UUID_STRING_MAX);
783         _bt_convert_addr_string_to_type(disconn_info.device_addr.addr, rem_client->addr);
784         BT_DBG("Disconnected FD [%d]", rem_client->sock_fd);
785         disconn_info.socket_fd = rem_client->sock_fd;
786
787         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
788                         BLUETOOTH_ERROR_NONE, &disconn_info,
789                         event_info->cb, event_info->user_data);
790 }
791
792 static void __remove_rfcomm_server(rfcomm_server_info_t *info)
793 {
794         rfcomm_remote_client_info_t *client_info;
795
796         BT_DBG("+");
797
798         if (!info)
799                 return;
800
801         rfcomm_servers = g_slist_remove(rfcomm_servers, info);
802         if (info->conn_list) {
803                 do {
804                         client_info = info->conn_list->data;
805                         if (!client_info)
806                                 break;
807
808                         info->conn_list = g_slist_remove(info->conn_list, client_info);
809                         __handle_rfcomm_client_disconnected(info, client_info);
810                         __remove_remote_client_info(client_info);
811                 } while (info->conn_list);
812         }
813
814         if (info->server_fd) {
815                 shutdown(info->server_fd, SHUT_RDWR);
816                 close(info->server_fd);
817         }
818
819         if (info->watch_id)
820                 g_source_remove(info->watch_id);
821
822         __rfcomm_delete_server_id(info->server_id);
823         g_free(info);
824
825         BT_DBG("-");
826 }
827
828 static void __connected_cb(rfcomm_remote_client_info_t *client_info, bt_event_info_t *event_info)
829 {
830         bluetooth_rfcomm_connection_t conn_info;
831         rfcomm_server_info_t *server_info;
832
833         server_info = __get_rfcomm_server_info_with_id(client_info->server_id);
834         ret_if(server_info == NULL);
835
836         memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
837         conn_info.device_role = RFCOMM_ROLE_SERVER;
838         g_strlcpy(conn_info.uuid, server_info->uuid, BLUETOOTH_UUID_STRING_MAX);
839         conn_info.socket_fd = client_info->sock_fd;
840         _bt_convert_addr_string_to_type(conn_info.device_addr.addr, client_info->addr);
841         conn_info.server_id = server_info->server_id;
842
843         BT_INFO_C("Connected [RFCOMM Server]");
844         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
845                         BLUETOOTH_ERROR_NONE, &conn_info,
846                         event_info->cb, event_info->user_data);
847 }
848
849 static int __process_cmsg(struct msghdr *msg)
850 {
851         int sock_fd = -1;
852         struct cmsghdr *cmsg_ptr = NULL;
853
854         for (cmsg_ptr = CMSG_FIRSTHDR(msg); cmsg_ptr != NULL;
855                         cmsg_ptr = CMSG_NXTHDR(msg, cmsg_ptr)) {
856
857                 if (cmsg_ptr->cmsg_level != SOL_SOCKET)
858                         continue;
859
860                 if (cmsg_ptr->cmsg_type == SCM_RIGHTS) {
861                         //int *desc = (int *)CMSG_DATA(cmsg_ptr);
862                         int count
863                                 = ((cmsg_ptr->cmsg_len - CMSG_LEN(0)) / sizeof(int));
864
865                         if (count < 0) {
866                                 BT_ERR("ERROR Invalid count of descriptors");
867                                 continue;
868                         }
869
870                         //sock_fd = desc[0];
871                         memcpy(&sock_fd, CMSG_DATA(cmsg_ptr), sizeof(sock_fd));
872                         BT_DBG("Remote client fd: %d", sock_fd);
873                 }
874         }
875
876         return sock_fd;
877 }
878
879 static int __read_incomming_client_connection(
880                 int server_fd, char *buf, unsigned int len, int *client_fd)
881 {
882         int ret;
883         struct msghdr msg;
884         struct iovec iv;
885         struct cmsghdr cmsgbuf[2 * sizeof(struct cmsghdr) + 4];
886         int retryCount = 0;
887
888         retv_if(0 > server_fd, -1);
889         retv_if(NULL == client_fd, -1);
890
891         BT_INFO("server_fd = %d", server_fd);
892
893         memset(&msg, 0, sizeof(msg));
894         memset(&iv, 0, sizeof(iv));
895
896         iv.iov_base = buf;
897         iv.iov_len = len;
898         msg.msg_iov = &iv;
899         msg.msg_iovlen = 1;
900         msg.msg_control = cmsgbuf;
901         msg.msg_controllen = sizeof(cmsgbuf);
902
903         for (retryCount = 0; retryCount < 5; retryCount++) {
904                 ret = recvmsg(server_fd, &msg, 0);
905                 BT_DBG("recvmsg ret = %d", ret);
906                 if (ret < 0 && errno == EINTR)
907                         continue;
908                 else
909                         break;
910         }
911
912         if (ret < 0 && errno == EPIPE) {
913                 /* End of stream, server listining stopped */
914                 BT_ERR("EOS errno: %d", errno);
915                 return 0;
916         }
917
918         if (ret < 0) {
919                 BT_ERR("Ret errno: %d", errno);
920                 return -1;
921         }
922
923         if ((msg.msg_flags & (MSG_CTRUNC | MSG_OOB | MSG_ERRQUEUE)) != 0) {
924                 BT_ERR("MSG Flags errno: %d", errno);
925                 return -1;
926         }
927
928         BT_INFO("Connection received");
929         *client_fd = __process_cmsg(&msg);
930         if (*client_fd < 0)
931                 BT_ERR("Invalid client_fd received");
932
933         return ret;
934 }
935
936 static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond,
937                                                                 gpointer data)
938 {
939         char *buffer = NULL;
940         gsize len = 0;
941         int result = BLUETOOTH_ERROR_NONE;
942         bt_event_info_t *event_info;
943         bluetooth_rfcomm_received_data_t data_r;
944         GIOStatus status = G_IO_STATUS_NORMAL;
945         GError *err = NULL;
946         rfcomm_remote_client_info_t *client_info = data;
947         rfcomm_server_info_t *server_info;
948
949         retv_if(client_info == NULL, FALSE);
950
951         server_info = __get_rfcomm_server_info_with_id(client_info->server_id);
952
953         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
954                 BT_ERR_C("RFComm Server  disconnected: %d", client_info->sock_fd);
955                 goto fail;
956         }
957
958         buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
959         status =  g_io_channel_read_chars(chan, buffer,
960                         BT_RFCOMM_BUFFER_LEN, &len, &err);
961         if (status != G_IO_STATUS_NORMAL) {
962                 BT_ERR("IO Channel read is failed with %d", status);
963                 g_free(buffer);
964                 if (err) {
965                         BT_ERR("IO Channel read error [%s]", err->message);
966                         if (status == G_IO_STATUS_ERROR &&
967                                         !g_strcmp0(err->message, "Connection reset by peer")) {
968                                 BT_ERR("cond : %d", cond);
969                                 g_error_free(err);
970                                 goto fail;
971                         }
972                         g_error_free(err);
973                 }
974                 return TRUE;
975         }
976
977         if (len == 0) {
978                 BT_ERR("Length is zero, remote end hang up");
979                 goto fail;
980         }
981
982         event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
983         if (event_info == NULL) {
984                 g_free(buffer);
985                 return TRUE;
986         }
987
988         data_r.socket_fd = client_info->sock_fd;
989         data_r.buffer_size = len;
990         data_r.buffer = buffer;
991
992         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
993                         result, &data_r,
994                         event_info->cb, event_info->user_data);
995
996         g_free(buffer);
997
998         return TRUE;
999
1000 fail:
1001         BT_ERR("Failure occured, remove client connection");
1002         server_info->conn_list = g_slist_remove(
1003                         server_info->conn_list, client_info);
1004         __handle_rfcomm_client_disconnected(server_info, client_info);
1005         __remove_remote_client_info(client_info);
1006         return FALSE;
1007 }
1008
1009 static gboolean __new_connection_request_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
1010 {
1011         int len;
1012         int size;
1013         int channel;
1014         int status;
1015         int client_fd;
1016         char buf[BLUETOOTH_SOCK_CONNECT_INFO_LEN];
1017         unsigned char addr[BT_ADDRESS_LENGTH_MAX];
1018
1019         bt_event_info_t *event_info;
1020         GIOChannel *io;
1021         rfcomm_remote_client_info_t *rem_client;
1022         rfcomm_server_info_t *server_info = data;
1023
1024         if (!server_info) {
1025                 BT_ERR("Server info is invalid");
1026                 return FALSE;
1027         }
1028
1029         if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
1030                 BT_INFO("RFCOMM Server with fd:%d is closed with cond:0x%X",
1031                                 server_info->server_fd, cond);
1032                 goto err;
1033         }
1034
1035         BT_INFO("Server fd: %d", server_info->server_fd);
1036         len = __read_incomming_client_connection(
1037                         server_info->server_fd, buf, BLUETOOTH_SOCK_CONNECT_INFO_LEN, &client_fd);
1038         BT_DBG("Socket Read len: %d", len);
1039         if (len == 0) {
1040                 BT_ERR("Listen stopped");
1041                 goto err;
1042         } else if (len != BLUETOOTH_SOCK_CONNECT_INFO_LEN) {
1043                 BT_ERR("Read length is not same as socket info length");
1044                 goto err;
1045         }
1046
1047         len = 0;
1048         /* Read size of data */
1049         size = buf[len] | (buf[len + 1] << 8);
1050         len += 2;
1051
1052         /* Read bluetooth address */
1053         memcpy(addr, buf + len, BT_ADDRESS_LENGTH_MAX);
1054         len += BT_ADDRESS_LENGTH_MAX;
1055
1056         /* Read channel */
1057         channel = buf[len] | (buf[len + 1] << 8) |
1058                 (buf[len + 2] << 16) | (buf[len + 3] << 24);
1059         len += 4;
1060
1061         /* Read status */
1062         status = buf[len] | (buf[len + 1] << 8) |
1063                 (buf[len + 2] << 16) | (buf[len + 3] << 24);
1064         len += 4;
1065
1066         BT_DBG("size: %d, channel: %d, status: %d", size, channel, status);
1067
1068         rem_client = g_malloc0(sizeof(rfcomm_remote_client_info_t));
1069         rem_client->sock_fd = client_fd;
1070         rem_client->server_id = server_info->server_id;
1071         _bt_convert_addr_type_to_string(rem_client->addr, addr);
1072
1073         BT_INFO("New client [%s] connection with socket_fd: %d, server_id: %d",
1074                         rem_client->addr, rem_client->sock_fd, rem_client->server_id);
1075
1076         io = g_io_channel_unix_new(rem_client->sock_fd);
1077         g_io_channel_set_encoding(io, NULL, NULL);
1078         g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
1079         rem_client->watch_id = g_io_add_watch(io,
1080                         G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
1081                         __data_received_cb, rem_client);
1082         g_io_channel_unref(io);
1083
1084         server_info->conn_list = g_slist_append(server_info->conn_list, rem_client);
1085         event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
1086         if (event_info)
1087                 __connected_cb(rem_client, event_info);
1088
1089         return TRUE;
1090
1091 err:
1092         /* Error occurred, Remove RFCOMM server*/
1093         __remove_rfcomm_server(server_info);
1094         return FALSE;
1095 }
1096
1097 static int __rfcomm_listen(rfcomm_server_info_t *server_info, bool accept)
1098 {
1099         int result;
1100         GUnixFDList *out_fd_list = NULL;
1101         GIOChannel *server_io;
1102
1103         retv_if(server_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1104
1105         BT_INIT_PARAMS();
1106         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1107
1108         if (accept == false) {
1109                 g_array_append_vals(in_param1, server_info->uuid, BLUETOOTH_UUID_STRING_MAX);
1110                 result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN,
1111                                 in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
1112         } else {
1113                 g_array_append_vals(in_param1, server_info->uuid, BLUETOOTH_UUID_STRING_MAX);
1114                 result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN_AND_ACCEPT,
1115                                 in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
1116         }
1117
1118         BT_DBG("result: %x", result);
1119         if (result != BLUETOOTH_ERROR_NONE) {
1120                 BT_ERR("Fail to send request");
1121                 return result;
1122         } else if (NULL == out_fd_list) {
1123                 BT_ERR("out_fd_list is NULL");
1124                 return BLUETOOTH_ERROR_INTERNAL;
1125         } else {
1126                 int *fd_list_array;
1127                 int len = 0;
1128
1129                 if (!out_fd_list)
1130                         return BLUETOOTH_ERROR_INTERNAL;
1131
1132                 fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
1133                 BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
1134                 server_info->server_fd = fd_list_array[0];
1135                 BT_INFO("Socket fd: %d", server_info->server_fd);
1136
1137                 g_free(fd_list_array);
1138                 g_object_unref(out_fd_list);
1139         }
1140
1141         server_io = g_io_channel_unix_new(server_info->server_fd);
1142         g_io_channel_set_encoding(server_io, NULL, NULL);
1143         g_io_channel_set_flags(server_io, G_IO_FLAG_NONBLOCK, NULL);
1144         server_info->watch_id = g_io_add_watch(server_io,
1145                         G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
1146                         __new_connection_request_cb, server_info);
1147         g_io_channel_unref(server_io);
1148
1149         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1150
1151         return BLUETOOTH_ERROR_NONE;
1152 }
1153 #endif
1154
1155 BT_EXPORT_API int bluetooth_rfcomm_create_socket(const char *uuid)
1156 {
1157 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1158         rfcomm_info_t *info;
1159 #else
1160         rfcomm_server_info_t *server_info;
1161 #endif
1162
1163         BT_CHECK_ENABLED(return);
1164         BT_CHECK_PARAMETER(uuid, return);
1165         BT_INFO("UUID Provided %s", uuid);
1166
1167         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CREATE_SOCKET)
1168                 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1169                 BT_ERR("Don't have a privilege to use this API");
1170                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1171         }
1172
1173         if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1174                 BT_ERR("Not allow to use SPP profile");
1175                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1176         }
1177
1178 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1179         BT_INFO("<<<<<<<<< RFCOMM Create socket from app >>>>>>>>>");
1180         info = __register_method();
1181         if (info == NULL)
1182                 return -1;
1183
1184         info->uuid = g_strdup(uuid);
1185         info->disconnect_idle_id = 0;
1186         return info->id;
1187 #else
1188         BT_INFO("<<<<<<<<< RFCOMM Create socket from app >>>>>>>>>");
1189
1190         server_info = __get_rfcomm_server_info_with_uuid(uuid);
1191         if (!server_info) {
1192                 server_info = g_malloc0(sizeof(rfcomm_server_info_t));
1193                 g_strlcpy(server_info->uuid, uuid, BLUETOOTH_UUID_STRING_MAX);
1194                 server_info->server_id = __rfcomm_assign_server_id();
1195                 server_info->server_fd = -1;
1196                 server_info->watch_id = -1;
1197                 server_info->auto_accept = FALSE;
1198                 rfcomm_servers = g_slist_append(rfcomm_servers, server_info);
1199         }
1200         return server_info->server_id;
1201 #endif
1202 }
1203
1204 BT_EXPORT_API int bluetooth_rfcomm_create_socket_ex(const char *uuid, const char *bus_name, const char *path)
1205 {
1206 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1207         rfcomm_info_t *info;
1208
1209         BT_CHECK_ENABLED(return);
1210         BT_CHECK_PARAMETER(path, return);
1211         BT_INFO("PATH Provided %s", path);
1212
1213         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CREATE_SOCKET_EX)
1214                 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1215                 BT_ERR("Don't have a privilege to use this API");
1216                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1217         }
1218
1219         if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1220                 BT_ERR("Not allow to use SPP profile");
1221                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1222         }
1223
1224         BT_INFO("<<<<<<<<< RFCOMM Create socket from app >>>>>>>>>");
1225         info = __register_method_2(path, bus_name);
1226         if (info == NULL)
1227                 return BLUETOOTH_ERROR_IN_PROGRESS;
1228         info->uuid = g_strdup(uuid);
1229         info->disconnect_idle_id = 0;
1230
1231         return BLUETOOTH_ERROR_NONE;
1232 #else
1233         return BLUETOOTH_ERROR_NOT_SUPPORT;
1234 #endif
1235 }
1236
1237
1238 BT_EXPORT_API int bluetooth_rfcomm_remove_socket(int id)
1239 {
1240 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1241         rfcomm_info_t *info;
1242 #else
1243         rfcomm_server_info_t *server_info;
1244 #endif
1245
1246         BT_CHECK_ENABLED(return);
1247
1248         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_REMOVE_SOCKET)
1249                 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1250                 BT_ERR("Don't have a privilege to use this API");
1251                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1252         }
1253
1254         if (id < 0) {
1255                 BT_ERR("Invalid ID");
1256                 return BLUETOOTH_ERROR_INVALID_PARAM;
1257         }
1258
1259 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1260         BT_INFO("RFCOMM Remove socket request from app, ID [%d]", id);
1261
1262         info = __find_rfcomm_info_with_id(id);
1263         if (info == NULL)
1264                 return BLUETOOTH_ERROR_INVALID_PARAM;
1265
1266         _bt_unregister_osp_server_in_agent(BT_RFCOMM_SERVER, info->uuid);
1267         _bt_unregister_profile(info->path);
1268
1269         rfcomm_nodes = g_slist_remove(rfcomm_nodes, info);
1270         free_rfcomm_info(info);
1271
1272         return BLUETOOTH_ERROR_NONE;
1273 #else
1274         BT_INFO("<<<<<<<<< RFCOMM Remove socket request from app, fd=[%d] >>>>>>>>>>>", socket_fd);
1275
1276         server_info = __get_rfcomm_server_info_with_id(id);
1277         if (!server_info) {
1278                 BT_ERR("server_info not found for socket_fd: %d", id);
1279                 return BLUETOOTH_ERROR_INVALID_PARAM;
1280         }
1281
1282         __remove_rfcomm_server(server_info);
1283
1284         return BLUETOOTH_ERROR_NONE;
1285 #endif
1286 }
1287
1288 BT_EXPORT_API int bluetooth_rfcomm_remove_socket_ex(const char *uuid)
1289 {
1290 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1291         rfcomm_info_t *info;
1292
1293         BT_CHECK_ENABLED(return);
1294
1295         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_REMOVE_SOCKET)
1296                 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1297                 BT_ERR("Don't have a privilege to use this API");
1298                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1299         }
1300
1301         BT_INFO("RFCOMM Remove socket request from app, uuid=[%s]", uuid);
1302
1303         info = __find_rfcomm_info_with_uuid(uuid);
1304         if (info == NULL)
1305                 return BLUETOOTH_ERROR_INVALID_PARAM;
1306
1307         _bt_unregister_osp_server_in_agent(BT_RFCOMM_SERVER, info->uuid);
1308         _bt_unregister_profile(info->path);
1309
1310         rfcomm_nodes = g_slist_remove(rfcomm_nodes, info);
1311         free_rfcomm_info(info);
1312
1313         return BLUETOOTH_ERROR_NONE;
1314 #else
1315         return BLUETOOTH_ERROR_NOT_SUPPORT;
1316 #endif
1317 }
1318
1319 BT_EXPORT_API int bluetooth_rfcomm_server_disconnect(int socket_fd)
1320 {
1321 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1322         rfcomm_info_t *info;
1323         rfcomm_conn_t *conn;
1324
1325         char address[20];
1326
1327         BT_INFO(" ### Disconnect RFCOMM server");
1328         if (socket_fd < 0) {
1329                 BT_ERR("Invalid FD");
1330                 return BLUETOOTH_ERROR_INVALID_PARAM;
1331         }
1332
1333         info = __find_rfcomm_info_with_fd(socket_fd);
1334         if (info == NULL)
1335                 return BLUETOOTH_ERROR_INVALID_PARAM;
1336
1337         conn = __find_rfcomm_conn_with_fd(info, socket_fd);
1338         if (conn == NULL)
1339                 return BLUETOOTH_ERROR_INVALID_PARAM;
1340
1341         if (conn->watch_id == 0 || conn->disconnected)
1342                 return BLUETOOTH_ERROR_NOT_CONNECTED;
1343
1344         close(conn->fd);
1345         conn->disconnected = TRUE;
1346
1347         _bt_convert_addr_type_to_string(address, conn->addr.addr);
1348
1349         BT_DBG("Address %s", address);
1350         _bt_disconnect_ext_profile(address, info->path);
1351
1352         if (info->disconnect_idle_id == 0)
1353                 info->disconnect_idle_id = g_idle_add(
1354                                 (GSourceFunc)__rfcomm_server_disconnect, info);
1355         BT_DBG("-");
1356
1357         return BLUETOOTH_ERROR_NONE;
1358 #else
1359         rfcomm_remote_client_info_t *client_info;
1360
1361         BT_CHECK_ENABLED(return);
1362
1363         BT_INFO(">>>>>>>>RFCOMM server disconnect request from APP>>>>>>>>>");
1364         if (socket_fd < 0) {
1365                 BT_ERR("Invalid FD");
1366                 return BLUETOOTH_ERROR_INVALID_PARAM;
1367         }
1368
1369         client_info = __get_rfcomm_rem_client_info_with_fd(socket_fd);
1370         if (!client_info) {
1371                 BT_ERR("client_info not found for socket_fd: %d", socket_fd);
1372                 return BLUETOOTH_ERROR_NOT_CONNECTED;
1373         }
1374
1375         if (client_info->sock_fd) {
1376                 shutdown(client_info->sock_fd, SHUT_RDWR);
1377                 close(client_info->sock_fd);
1378                 client_info->sock_fd = -1;
1379         }
1380
1381         return BLUETOOTH_ERROR_NONE;
1382 #endif
1383 }
1384
1385 BT_EXPORT_API gboolean bluetooth_rfcomm_is_server_uuid_available(const char *uuid)
1386 {
1387         int result;
1388         gboolean available = TRUE;
1389         char uuid_str[BLUETOOTH_UUID_STRING_MAX];
1390
1391         retv_if(uuid == NULL, FALSE);
1392         retv_if(bluetooth_check_adapter() ==
1393                                 BLUETOOTH_ADAPTER_DISABLED, FALSE);
1394
1395         BT_INIT_PARAMS();
1396         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1397
1398         g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
1399         g_array_append_vals(in_param1, uuid_str, BLUETOOTH_UUID_STRING_MAX);
1400
1401         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_IS_UUID_AVAILABLE,
1402                 in_param1, in_param2, in_param3, in_param4, &out_param);
1403
1404         BT_DBG("result: %x", result);
1405
1406         if (result == BLUETOOTH_ERROR_NONE)
1407                 available = g_array_index(out_param, gboolean, 0);
1408
1409         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1410
1411         BT_DBG("available: %d", available);
1412
1413         return available;
1414 }
1415
1416 BT_EXPORT_API int bluetooth_rfcomm_server_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
1417 {
1418 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1419         GSList *l;
1420         GSList *ll;
1421         rfcomm_info_t *info;
1422         rfcomm_conn_t *conn;
1423 #else
1424         char input_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
1425         rfcomm_remote_client_info_t *info;
1426 #endif
1427
1428         BT_CHECK_PARAMETER(device_address, return);
1429         BT_CHECK_PARAMETER(connected, return);
1430
1431         *connected = FALSE;
1432
1433 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1434         for (l = rfcomm_nodes; l; l = l->next) {
1435                 info = l->data;
1436
1437                 if (info == NULL || info->rfcomm_conns == NULL)
1438                         continue;
1439
1440                 for (ll = info->rfcomm_conns; ll; ll = ll->next) {
1441                         conn = ll->data;
1442
1443                         if (memcmp(device_address, &conn->addr,
1444                                         sizeof(bluetooth_device_address_t)))
1445                                 continue;
1446
1447                         *connected = TRUE;
1448                         return BLUETOOTH_ERROR_NONE;
1449                 }
1450         }
1451
1452         return BLUETOOTH_ERROR_NONE;
1453 #else
1454         _bt_convert_addr_type_to_string(input_addr, (unsigned char *)device_address->addr);
1455         info = __get_rfcomm_rem_client_info_with_addr(input_addr);
1456         if (info)
1457                 *connected = TRUE;
1458
1459         return BLUETOOTH_ERROR_NONE;
1460 #endif
1461 }
1462
1463 BT_EXPORT_API int bluetooth_rfcomm_listen_and_accept(int id, int max_pending_connection)
1464 {
1465 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1466         rfcomm_info_t *info;
1467 #else
1468         rfcomm_server_info_t *server_info;
1469 #endif
1470
1471         BT_CHECK_ENABLED(return);
1472         if (id < 0) {
1473                 BT_ERR("Invalid ID");
1474                 return BLUETOOTH_ERROR_INVALID_PARAM;
1475         }
1476
1477         if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1478                 BT_ERR("Not allow to use SPP profile");
1479                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1480         }
1481
1482 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1483         BT_INFO("RFCOMM Listen & accept from app");
1484
1485         info = __find_rfcomm_info_with_id(id);
1486         if (info == NULL)
1487                 return BLUETOOTH_ERROR_INVALID_PARAM;
1488
1489         bt_register_profile_info_t profile_info;
1490         int result;
1491
1492         profile_info.authentication = TRUE;
1493         profile_info.authorization = TRUE;
1494         profile_info.obj_path = info->path;
1495         profile_info.role = NULL;
1496         profile_info.service = info->uuid;
1497         profile_info.uuid = info->uuid;
1498
1499         BT_INFO("uuid %s", profile_info.uuid);
1500         result = _bt_register_profile(&profile_info, TRUE);
1501
1502         return result;
1503 #else
1504         BT_INFO("<<<<<<<<< RFCOMM Listen & accept from app >>>>>>>>>>>");
1505
1506         server_info = __get_rfcomm_server_info_with_id(id);
1507         if (!server_info) {
1508                 BT_ERR("server_info not found for id: %d", id);
1509                 return BLUETOOTH_ERROR_INVALID_PARAM;
1510         }
1511
1512         if (server_info->server_fd >= 0) {
1513                 BT_ERR("server already listining");
1514                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1515         }
1516
1517         server_info->max_pending_conn = max_pending_connection;
1518         server_info->auto_accept = TRUE;
1519
1520         return __rfcomm_listen(server_info, true);
1521 #endif
1522 }
1523
1524 BT_EXPORT_API int bluetooth_rfcomm_listen_and_accept_ex(const char *uuid,
1525                                         int max_pending_connection,
1526                                         const char *bus_name, const char *path)
1527 {
1528 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1529         rfcomm_info_t *info;
1530
1531         BT_CHECK_ENABLED(return);
1532
1533         if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1534                 BT_ERR("Not allow to use SPP profile");
1535                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1536         }
1537
1538         BT_INFO("RFCOMM Listen & accept from app");
1539
1540         info = __find_rfcomm_info_with_uuid(uuid);
1541         if (info == NULL)
1542                 return BLUETOOTH_ERROR_INVALID_PARAM;
1543
1544         bt_register_profile_info_t profile_info;
1545         int result;
1546
1547         profile_info.authentication = TRUE;
1548         profile_info.authorization = TRUE;
1549         profile_info.obj_path = info->path;
1550         profile_info.role = NULL;
1551         profile_info.service = info->uuid;
1552         profile_info.uuid = info->uuid;
1553
1554         BT_INFO("uuid %s", profile_info.uuid);
1555         result = _bt_register_profile_ex(&profile_info, TRUE, bus_name, path);
1556
1557         return result;
1558 #else
1559         return BLUETOOTH_ERROR_NOT_SUPPORT;
1560 #endif
1561 }
1562
1563 BT_EXPORT_API int bluetooth_rfcomm_listen(int id, int max_pending_connection)
1564 {
1565 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1566         rfcomm_info_t *info;
1567 #else
1568         rfcomm_server_info_t *server_info;
1569 #endif
1570
1571         BT_CHECK_ENABLED(return);
1572         if (id < 0) {
1573                 BT_ERR("Invalid ID");
1574                 return BLUETOOTH_ERROR_INVALID_PARAM;
1575         }
1576
1577         if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1578                 BT_ERR("Not allow to use SPP profile");
1579                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1580         }
1581
1582 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1583         BT_INFO("RFCOMM Listen");
1584
1585         info = __find_rfcomm_info_with_id(id);
1586         if (info == NULL)
1587                 return BLUETOOTH_ERROR_INVALID_PARAM;
1588
1589         bt_register_profile_info_t profile_info;
1590         int result;
1591
1592         profile_info.authentication = TRUE;
1593         profile_info.authorization = TRUE;
1594         profile_info.obj_path = info->path;
1595         profile_info.role = NULL;
1596         profile_info.service = info->uuid;
1597         profile_info.uuid = info->uuid;
1598         BT_INFO("UUID %s", info->uuid);
1599         BT_INFO("PATH %s", info->path);
1600         result = _bt_register_profile_platform(&profile_info, TRUE);
1601         if (result != BLUETOOTH_ERROR_NONE)
1602                 return result;
1603
1604         return _bt_register_osp_server_in_agent(BT_RFCOMM_SERVER, info->uuid,
1605                                                 info->path, id);
1606
1607 #else
1608         BT_INFO("<<<<<<<<< RFCOMM Listen >>>>>>>>>>>");
1609
1610         server_info = __get_rfcomm_server_info_with_id(id);
1611         if (!server_info) {
1612                 BT_ERR("server_info not found for id: %d", id);
1613                 return BLUETOOTH_ERROR_INVALID_PARAM;
1614         }
1615
1616         if (server_info->server_fd >= 0) {
1617                 BT_ERR("server already listining");
1618                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1619         }
1620
1621         server_info->max_pending_conn = max_pending_connection;
1622         server_info->auto_accept = FALSE;
1623         return __rfcomm_listen(server_info, false);
1624 #endif
1625 }
1626
1627 BT_EXPORT_API int bluetooth_rfcomm_accept_connection(int server_fd)
1628 {
1629         int result;
1630 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1631 #else
1632         rfcomm_server_info_t *server_info;
1633 #endif
1634
1635         BT_CHECK_ENABLED(return);
1636
1637         if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1638                 BT_ERR("Not allow to use SPP profile");
1639                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1640         }
1641
1642         if (server_fd < 0) {
1643                 BT_ERR("Invalid FD");
1644                 return BLUETOOTH_ERROR_INVALID_PARAM;
1645         }
1646
1647         BT_INIT_PARAMS();
1648         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1649
1650 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1651         g_array_append_vals(in_param1, &server_fd, sizeof(int));
1652 #else
1653         server_info = __get_rfcomm_server_info_with_id(server_fd);
1654         if (!server_info) {
1655                 BT_ERR("No server with fd: %d", server_fd);
1656                 return BLUETOOTH_ERROR_INVALID_PARAM;
1657         }
1658
1659         g_array_append_vals(in_param1, server_info->pending_addr, BT_ADDRESS_STRING_SIZE);
1660 #endif
1661
1662         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_ACCEPT_CONNECTION,
1663                 in_param1, in_param2, in_param3, in_param4, &out_param);
1664
1665         BT_DBG("result: %x", result);
1666
1667         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1668
1669         return result;
1670 }
1671
1672 BT_EXPORT_API int bluetooth_rfcomm_reject_connection(int server_fd)
1673 {
1674         int result;
1675 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1676 #else
1677         rfcomm_server_info_t *server_info;
1678 #endif
1679
1680         BT_CHECK_ENABLED(return);
1681
1682         if (server_fd < 0) {
1683                 BT_ERR("Invalid FD");
1684                 return BLUETOOTH_ERROR_INVALID_PARAM;
1685         }
1686
1687         BT_INFO("+");
1688
1689         BT_INIT_PARAMS();
1690         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1691
1692 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1693         g_array_append_vals(in_param1, &server_fd, sizeof(int));
1694 #else
1695         server_info = __get_rfcomm_server_info_with_id(server_fd);
1696         if (!server_info) {
1697                 BT_ERR("No server with fd: %d", server_fd);
1698                 return BLUETOOTH_ERROR_INVALID_PARAM;
1699         }
1700
1701         g_array_append_vals(in_param1, server_info->pending_addr, BT_ADDRESS_STRING_SIZE);
1702 #endif
1703         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_REJECT_CONNECTION,
1704                 in_param1, in_param2, in_param3, in_param4, &out_param);
1705
1706         BT_DBG("result: %x", result);
1707
1708         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1709
1710         return result;
1711 }
1712