Remove old bt-service files
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-rfcomm-client.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 #include <gio/gunixfdlist.h>
20
21 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
22 #include <errno.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
32 #ifdef TIZEN_FEATURE_BT_DPM
33 #include "bt-dpm.h"
34 #endif
35
36 /* Variable for privilege, only for write API,
37   before we should reduce time to bt-service dbus calling
38   -1 : Don't have a permission to access API
39   0 : Initial value, not yet check
40   1 : Have a permission to access API
41 */
42 static int privilege_token;
43
44
45 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
46 #define BT_TIMEOUT_MESSAGE "Did not receive a reply. Possible causes include: " \
47                         "the remote application did not send a reply, " \
48                         "the message bus security policy blocked the reply, " \
49                         "the reply timeout expired, or the network connection " \
50                         "was broken."
51
52 #define BATTERY_MONITOR_RFCOMM_INTERVAL 5
53
54 static GSList *rfcomm_clients;
55
56 typedef struct {
57         char bt_addr[BT_ADDRESS_STRING_SIZE];
58         int fd;
59         guint watch_id;
60         gboolean disconnected;
61 } rfcomm_conn_info_t;
62
63 typedef struct {
64         char uuid[BLUETOOTH_UUID_STRING_MAX];
65         char *device_path;
66         char *obj_path;
67         int object_id;
68         int id;
69         GSList *rfcomm_conns;
70         unsigned int idle_id;
71 } rfcomm_cb_data_t;
72
73 static unsigned int rx_data = 0, tx_data = 0;
74 static guint tx_tag = 0, rx_tag = 0;
75
76 static void __client_connected_cb(rfcomm_cb_data_t *cb_data,
77         char *dev_address, int result);
78
79 static bool __rfcomm_record_tx_data(void)
80 {
81         if (tx_data > 0) {
82                 int ret = _bt_common_send_rfcomm_tx_details(tx_data);
83                 if (ret == BLUETOOTH_ERROR_NONE) {
84                         tx_data = 0;
85                         return TRUE;
86                 } else {
87                         BT_ERR("RFCOMM tx data could not be registered");
88                 }
89         }
90
91         tx_data = 0;
92         tx_tag = 0;
93
94         return FALSE;
95 }
96
97 static bool __rfcomm_record_rx_data(void)
98 {
99         if (rx_data) {
100                 int ret = _bt_common_send_rfcomm_rx_details(rx_data);
101                 if (ret == BLUETOOTH_ERROR_NONE) {
102                         rx_data = 0;
103                         return TRUE;
104                 } else {
105                         BT_ERR("RFCOMM rx data could not be registered");
106                 }
107         }
108
109         rx_data = 0;
110         rx_tag = 0;
111
112         return FALSE;
113 }
114
115 static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
116 {
117         BT_DBG("+");
118
119         if (cb_data->id >= 0)
120                 __rfcomm_delete_id(cb_data->id);
121
122         if (cb_data->object_id > 0)
123                 _bt_unregister_gdbus(cb_data->object_id);
124
125         if (cb_data->obj_path) {
126                 BT_INFO("Unregister profile");
127                 _bt_unregister_profile(cb_data->obj_path);
128         }
129
130         if (cb_data->idle_id != 0) {
131                 BT_INFO("Removing idle source");
132                 g_source_remove(cb_data->idle_id);
133         }
134
135         g_free(cb_data->obj_path);
136
137         g_free(cb_data->device_path);
138         g_free(cb_data);
139         BT_DBG("-");
140 }
141
142 static void rfcomm_cb_data_remove(rfcomm_cb_data_t *info)
143 {
144         if (info) {
145                 BT_INFO("No more device connected remove info");
146                 rfcomm_clients = g_slist_remove(rfcomm_clients, info);
147                 __bt_free_cb_data(info);
148         }
149 }
150
151 gint compare(gpointer *a, gpointer *b)
152 {
153         rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
154         char *address = (char *)b;
155         return g_strcmp0(node->bt_addr, address);
156 }
157
158 gint compare_fd(gpointer *a, gpointer *b)
159 {
160         rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
161         int *fd = (int *)b;
162         if (node->fd == *fd)
163                 return 0;
164
165         return 1;
166 }
167 static void __bt_free_conn(rfcomm_conn_info_t *conn)
168 {
169         BT_DBG("+");
170
171         if (conn == NULL)
172                 return;
173
174         if (conn->watch_id > 0) {
175                 g_source_remove(conn->watch_id);
176                 conn->watch_id = 0;
177         }
178
179         g_free(conn);
180
181         BT_DBG("-");
182 }
183
184 static void __rfcomm_remove_conn_info_t(rfcomm_cb_data_t *info, char *address)
185 {
186         GSList *l = NULL;
187         rfcomm_conn_info_t *conn_info = NULL;
188         l = g_slist_find_custom(info->rfcomm_conns, address, (GCompareFunc)compare);
189         if (l)
190                 conn_info = l->data;
191         if (conn_info) {
192                 info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn_info);
193                 __bt_free_conn(conn_info);
194         }
195 }
196
197 static rfcomm_conn_info_t *__get_conn_info_from_fd(rfcomm_cb_data_t *info,
198         int fd)
199 {
200         GSList *l;
201         rfcomm_conn_info_t *device_node = NULL;
202         for (l = info->rfcomm_conns; l != NULL; l = l->next) {
203                 device_node = l->data;
204                 if (device_node && device_node->fd == fd)
205                         return device_node;
206         }
207         return NULL;
208 }
209
210 static rfcomm_conn_info_t *__get_conn_info_from_address(rfcomm_cb_data_t *info,
211                 char *dev_address)
212 {
213         GSList *l = NULL;
214         rfcomm_conn_info_t *conn_info = NULL;
215         l = g_slist_find_custom(info->rfcomm_conns, dev_address,
216                 (GCompareFunc)compare);
217         if (l)
218                 conn_info = l->data;
219         return conn_info;
220 }
221
222 static void __rfcomm_client_connected_cb(rfcomm_cb_data_t *info,
223         char *dev_address, int result)
224 {
225         if (g_slist_find(rfcomm_clients, info) == NULL) {
226                 BT_INFO("rfcomm resource is already freed");
227                 return;
228         }
229
230         __client_connected_cb(info, dev_address, result);
231         __rfcomm_remove_conn_info_t(info, dev_address);
232
233         if (info->rfcomm_conns == NULL)
234                 rfcomm_cb_data_remove(info);
235 }
236
237 static rfcomm_cb_data_t *__find_rfcomm_info_with_fd(int fd)
238 {
239         GSList *l;
240         GSList *device_fd;
241         for (l = rfcomm_clients; l != NULL; l = l->next) {
242                 rfcomm_cb_data_t *info = l->data;
243                 device_fd = g_slist_find_custom(info->rfcomm_conns, &fd,
244                         (GCompareFunc)compare_fd);
245                 if (device_fd)
246                         return info;
247         }
248
249         return NULL;
250 }
251
252 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
253 {
254         GSList *l;
255
256         for (l = rfcomm_clients; l != NULL; l = l->next) {
257                 rfcomm_cb_data_t *info = l->data;
258
259                 if (info != NULL)
260                         if (g_strcmp0(info->obj_path, path) == 0)
261                                 return info;
262         }
263
264         return NULL;
265 }
266
267 static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
268 {
269         GSList *l;
270
271         for (l = rfcomm_clients; l != NULL; l = l->next) {
272                 rfcomm_cb_data_t *info = l->data;
273
274                 if (g_strcmp0(info->uuid, uuid) == 0)
275                         return info;
276         }
277
278         return NULL;
279 }
280
281 static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
282         rfcomm_cb_data_t *info)
283 {
284         if (conn_info == NULL)
285                 return;
286
287         bluetooth_rfcomm_disconnection_t disconn_info;
288         bt_event_info_t *event_info = NULL;
289
290         if (conn_info->disconnected == FALSE)
291                 return;
292
293         event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
294         if (event_info == NULL) {
295                 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
296
297                 if (info->rfcomm_conns == NULL)
298                         rfcomm_cb_data_remove(info);
299                 return;
300         }
301
302         memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
303         disconn_info.device_role = RFCOMM_ROLE_CLIENT;
304         g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
305         _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
306                                         conn_info->bt_addr);
307
308         BT_DBG("Disconnected FD [%d]", conn_info->fd);
309         disconn_info.socket_fd = conn_info->fd;
310
311         BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
312                         BLUETOOTH_ERROR_NONE, conn_info->bt_addr,
313                         info->uuid, conn_info->fd);
314         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
315                         BLUETOOTH_ERROR_NONE, &disconn_info,
316                         event_info->cb, event_info->user_data);
317
318         __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
319
320         if (info->rfcomm_conns == NULL)
321                 rfcomm_cb_data_remove(info);
322
323         if (_bt_common_send_rfcomm_conn_info(RFCOMM_ROLE_CLIENT,
324                         FALSE, disconn_info.socket_fd) != BLUETOOTH_ERROR_NONE)
325                 BT_ERR("Fail to send the connection info");
326
327         BT_DBG("-");
328 }
329
330 static gboolean __rfcomm_client_disconnect(gpointer user_data)
331 {
332         rfcomm_cb_data_t *info = (rfcomm_cb_data_t *)user_data;
333
334         BT_INFO_C("### Disconnected [RFCOMM Client]");
335
336         retv_if(info == NULL, FALSE);
337
338         if (g_slist_find(rfcomm_clients, info) == NULL) {
339                 BT_INFO("rfcomm resource is already freed");
340                 return FALSE;
341         }
342         info->idle_id = 0;
343
344         g_slist_foreach(info->rfcomm_conns,
345                 (GFunc)_bt_rfcomm_disconnect_conn_info, info);
346
347         BT_DBG("-");
348         return FALSE;
349 }
350
351 static gboolean __is_error_by_disconnect(GError *err)
352 {
353         return !g_strcmp0(err->message, "Connection reset by peer") ||
354                         !g_strcmp0(err->message, "Connection timed out") ||
355                         !g_strcmp0(err->message, "Software caused connection abort");
356 }
357
358 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
359                                                                 gpointer data)
360 {
361         char *buffer = NULL;
362         gsize len = 0;
363         int result = BLUETOOTH_ERROR_NONE;
364         rfcomm_cb_data_t *info = data;
365         rfcomm_conn_info_t *conn_info = NULL;
366         bt_event_info_t *event_info;
367         bluetooth_rfcomm_received_data_t data_r;
368         GIOStatus status = G_IO_STATUS_NORMAL;
369         GError *err = NULL;
370         int fd;
371         static int resource_unavailable_cnt = 0;
372
373         retv_if(info == NULL, FALSE);
374         fd = g_io_channel_unix_get_fd(chan);
375         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
376                 BT_ERR_C("RFComm Client  disconnected: %d", fd);
377
378                 conn_info = __get_conn_info_from_fd(info, fd);
379                 if (conn_info == NULL) {
380                         BT_ERR("No Connection info found with FD [%d]", fd);
381                         return FALSE;
382                 }
383
384                 if (conn_info->disconnected == FALSE) {
385                         close(conn_info->fd);
386                         conn_info->disconnected = TRUE;
387                 }
388                 __rfcomm_client_disconnect(info);
389                 return FALSE;
390         }
391
392         buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
393
394         status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
395                         &len, &err);
396         if (status != G_IO_STATUS_NORMAL) {
397                 BT_ERR("IO Channel read is failed with %d", status);
398
399                 g_free(buffer);
400
401                 if (err) {
402                         BT_ERR("IO Channel read error [%s]", err->message);
403                         if (status == G_IO_STATUS_ERROR &&
404                                         __is_error_by_disconnect(err)) {
405
406                                 BT_ERR("cond : %d", cond);
407                                 g_error_free(err);
408                                 goto fail;
409                         }
410                         g_error_free(err);
411                 }
412
413                 if (status == G_IO_STATUS_ERROR ||
414                         status == G_IO_STATUS_EOF) {
415                         goto fail;
416                 } else if (status == G_IO_STATUS_AGAIN) {
417                         resource_unavailable_cnt++;
418                         if (resource_unavailable_cnt > 10)
419                                 goto fail;
420                 }
421
422                 return TRUE;
423         }
424         resource_unavailable_cnt = 0;
425
426         event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
427         if (event_info == NULL) {
428                 g_free(buffer);
429                 return TRUE;
430         }
431
432         data_r.socket_fd = fd;
433         data_r.buffer_size = len;
434         data_r.buffer = buffer;
435
436         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
437                         result, &data_r,
438                         event_info->cb, event_info->user_data);
439
440         if (bluetooth_get_battery_monitor_state()) {
441                 if (rx_tag == 0) {
442                         BT_DBG("Adding rfcomm rx timeout function for battery monitor");
443                         rx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_rx_data, NULL);
444                 }
445                 rx_data += len;
446         }
447
448         g_free(buffer);
449         return TRUE;
450
451 fail:
452         conn_info = __get_conn_info_from_fd(info, fd);
453         if (conn_info == NULL) {
454                 BT_ERR("No Connection info found with FD [%d]", fd);
455                 return FALSE;
456         }
457
458         if (conn_info->disconnected == FALSE) {
459                 close(conn_info->fd);
460                 conn_info->disconnected = TRUE;
461         }
462         __rfcomm_client_disconnect(info);
463
464         return FALSE;
465 }
466
467 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
468         int result)
469 {
470         bluetooth_rfcomm_connection_t conn_info;
471         bt_event_info_t *event_info;
472         rfcomm_conn_info_t *conn_list_info = NULL;
473
474         if (result == BLUETOOTH_ERROR_NONE)
475                 BT_INFO_C("### Connected [RFCOMM Client]");
476
477         event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
478         if (event_info == NULL)
479                 return;
480
481         memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
482         conn_info.device_role = RFCOMM_ROLE_CLIENT;
483         g_strlcpy(conn_info.uuid, cb_data->uuid, BLUETOOTH_UUID_STRING_MAX);
484         _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
485                         dev_address);
486         conn_list_info = __get_conn_info_from_address(cb_data, dev_address);
487         if (conn_list_info == NULL) {
488                 BT_ERR("Device addres %s not found in connection list", dev_address);
489                 return;
490         }
491         conn_info.socket_fd = conn_list_info->fd;
492         conn_info.server_id = -1;
493
494         if (_bt_common_send_rfcomm_conn_info(RFCOMM_ROLE_CLIENT,
495                         TRUE, conn_info.socket_fd) != BLUETOOTH_ERROR_NONE)
496                 BT_ERR("Fail to send the connection info");
497
498         BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
499                         result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
500         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
501                         result, &conn_info,
502                         event_info->cb, event_info->user_data);
503         BT_DBG("-");
504 }
505
506 void _bt_rfcomm_client_disconnect_all(void)
507 {
508         GSList *client;
509         GSList *conn;
510
511         BT_INFO_C("### Disconnect all RFCOMM client connections");
512
513         for (client = rfcomm_clients; client; ) {
514                 rfcomm_cb_data_t *info = client->data;
515
516                 for (conn = info->rfcomm_conns; conn; conn = conn->next) {
517                         rfcomm_conn_info_t *conn_info = conn->data;
518
519                         if (conn_info == NULL)
520                                 continue;
521
522                         if (conn_info->watch_id == 0 || conn_info->disconnected)
523                                 continue;
524
525                         close(conn_info->fd);
526                         conn_info->disconnected = TRUE;
527
528                         _bt_disconnect_ext_profile(conn_info->bt_addr,
529                                                    info->obj_path);
530                 }
531
532                 client = client->next;
533                 __rfcomm_client_disconnect(info);
534         }
535
536         return;
537 }
538
539 void _bt_rfcomm_client_reset_timer(void)
540 {
541         if (rx_tag > 0) {
542                 rx_tag = 0;
543                 g_source_remove(rx_tag);
544         }
545
546         if (tx_tag > 0) {
547                 g_source_remove(tx_tag);
548                 tx_tag = 0;
549         }
550
551         rx_data = 0;
552         tx_data = 0;
553 }
554
555 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
556 {
557         rfcomm_cb_data_t *info;
558         GIOChannel *data_io;
559         rfcomm_conn_info_t *conn_info = NULL;
560         char address[BT_ADDRESS_STRING_SIZE];
561
562         BT_INFO("%s %d", path, fd);
563
564         _bt_convert_addr_type_to_string(address,
565                                 (unsigned char *)addr);
566
567         info = __find_rfcomm_info_from_path(path);
568         if (info == NULL) {
569                 BT_ERR("rfcomm info is NULL");
570                 return -1;
571         }
572
573         conn_info = __get_conn_info_from_address(info, address);
574         if (conn_info == NULL) {
575                 BT_ERR("connection info is NULL");
576                 return -1;
577         }
578
579         conn_info->fd = fd;
580
581         data_io = g_io_channel_unix_new(fd);
582
583         g_io_channel_set_encoding(data_io, NULL, NULL);
584         g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
585
586         conn_info->watch_id = g_io_add_watch(data_io,
587                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
588                                 __client_data_received_cb, info);
589
590         g_io_channel_unref(data_io);
591
592         __client_connected_cb(info, address, BLUETOOTH_ERROR_NONE);
593
594         return 0;
595 }
596
597 static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
598                                                         gpointer user_data)
599
600 {
601         GError *error = NULL;
602         GVariant *value;
603         rfcomm_cb_data_t *cb_data;
604         char dev_address[BT_ADDRESS_STRING_SIZE];
605         const char *path;
606         BT_DBG("+");
607
608         ret_if(user_data == NULL);
609
610         cb_data = user_data;
611
612         value = g_dbus_proxy_call_finish(proxy, res, &error);
613         if (value == NULL) {
614                 int result;
615                 g_dbus_error_strip_remote_error(error);
616                 BT_ERR("Error : %s \n", error->message);
617
618                 if (g_strcmp0(error->message, "In Progress") == 0)
619                         result = BLUETOOTH_ERROR_DEVICE_BUSY;
620                 else
621                         result = BLUETOOTH_ERROR_INTERNAL;
622                 path = g_dbus_proxy_get_object_path(proxy);
623                 _bt_convert_device_path_to_address(path, dev_address);
624                 __rfcomm_client_connected_cb(cb_data, dev_address, result);
625
626                 g_error_free(error);
627         } else {
628                 g_variant_unref(value);
629         }
630
631         if (proxy)
632                 g_object_unref(proxy);
633
634         BT_DBG("-");
635 }
636
637 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
638                                 GAsyncResult *res, gpointer user_data)
639 {
640         rfcomm_cb_data_t *cb_data;
641         int ret = 0;
642         GError *err = NULL;
643         GVariant *value;
644         bt_register_profile_info_t info = {0};
645         int result = BLUETOOTH_ERROR_NONE;
646         char dev_address[BT_ADDRESS_STRING_SIZE];
647         const char *path;
648
649         BT_DBG("+");
650
651         ret_if(user_data == NULL);
652
653         cb_data = user_data;
654
655         path = g_dbus_proxy_get_object_path(proxy);
656
657         _bt_convert_device_path_to_address(path, dev_address);
658         BT_DBG("Device Adress [%s]", dev_address);
659         value = g_dbus_proxy_call_finish(proxy, res, &err);
660         if (proxy)
661                 g_object_unref(proxy);
662         if (value)
663                 g_variant_unref(value);
664
665         if (err != NULL) {
666                 g_dbus_error_strip_remote_error(err);
667                 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
668                 if (!strcmp("Operation canceled", err->message)) {
669                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
670                 } else if (!strcmp("In Progress", err->message)) {
671                         result = BLUETOOTH_ERROR_IN_PROGRESS;
672                 } else if (!strcmp("Host is down", err->message)) {
673                         result = BLUETOOTH_ERROR_HOST_DOWN;
674                 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
675                         result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
676                         ret = _bt_cancel_discovers(dev_address);
677                         if (ret != BLUETOOTH_ERROR_NONE)
678                                 BT_ERR("Error: While CancelDiscovery");
679                 } else {
680                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
681                 }
682                 __rfcomm_client_connected_cb(cb_data, dev_address, result);
683                 goto done;
684         } else {
685                 BT_INFO("Services are Updated checking required uuid is there");
686                 /* Check here for uuid present */
687                 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
688                 if (ret == BLUETOOTH_ERROR_NONE) {
689                         info.uuid = (char *)cb_data->uuid;
690                         info.obj_path = cb_data->obj_path;
691                         info.role = "client";
692
693                         ret = _bt_register_profile(&info, FALSE);
694                         if (ret < 0)
695                                 BT_DBG("Error: register profile");
696                         ret = _bt_connect_profile(dev_address, cb_data->uuid,
697                                                 __bt_connect_response_cb, cb_data);
698
699                         if (ret != BLUETOOTH_ERROR_NONE) {
700                                 BT_ERR("ConnectProfile failed");
701                                 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
702                                 __rfcomm_client_connected_cb(cb_data, dev_address, result);
703                                 goto done;
704                         }
705                 } else {
706                         BT_ERR("remote uuid not found");
707                         result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
708                         __rfcomm_client_connected_cb(cb_data, dev_address, result);
709                 }
710         }
711 done:
712         if (err)
713                 g_clear_error(&err);
714 }
715 #else
716 GSList *rfcomm_clients;
717
718 typedef struct {
719         char *uuid;
720         char *remote_addr;
721         int sock_fd;
722         int watch_id;
723 } rfcomm_client_conn_info_t;
724
725 static gboolean __is_error_by_disconnect(GError *err)
726 {
727         return !g_strcmp0(err->message, "Connection reset by peer") ||
728                 !g_strcmp0(err->message, "Connection timed out") ||
729                 !g_strcmp0(err->message, "Software caused connection abort");
730 }
731
732 static rfcomm_client_conn_info_t *__find_rfcomm_conn_info_with_fd(int fd)
733 {
734         GSList *l;
735
736         BT_DBG("+");
737
738         for (l = rfcomm_clients; l != NULL; l = l->next) {
739                 rfcomm_client_conn_info_t *info = l->data;
740
741                 if (info && info->sock_fd == fd) {
742                         BT_INFO("Match found");
743                         return info;
744                 }
745         }
746
747         BT_DBG("-");
748         return NULL;
749 }
750
751 static void __rfcomm_remove_client_conn_info_t(rfcomm_client_conn_info_t *info)
752 {
753         ret_if(info == NULL);
754
755         rfcomm_clients = g_slist_remove(rfcomm_clients, info);
756         g_free(info->uuid);
757         g_free(info->remote_addr);
758 }
759
760 static void __bt_rfcomm_client_disconnected(rfcomm_client_conn_info_t *conn_info)
761 {
762
763         bluetooth_rfcomm_disconnection_t disconn_info;
764         bt_event_info_t *event_info = NULL;
765
766         ret_if(conn_info == NULL);
767
768         event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
769         ret_if(event_info == NULL);
770
771         memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
772         disconn_info.device_role = RFCOMM_ROLE_CLIENT;
773         disconn_info.socket_fd = conn_info->sock_fd;
774         g_strlcpy(disconn_info.uuid, conn_info->uuid, BLUETOOTH_UUID_STRING_MAX);
775         _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
776                         conn_info->remote_addr);
777
778         BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
779                         BLUETOOTH_ERROR_NONE, conn_info->remote_addr,
780                         conn_info->uuid, conn_info->sock_fd);
781         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
782                         BLUETOOTH_ERROR_NONE, &disconn_info,
783                         event_info->cb, event_info->user_data);
784
785         BT_DBG("-");
786 }
787
788 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
789 {
790         bt_event_info_t *event_info;
791         bluetooth_rfcomm_received_data_t data_r;
792         rfcomm_client_conn_info_t *conn_info;
793
794         int fd;
795         gsize len = 0;
796         char *buffer;
797         GError *err = NULL;
798         GIOStatus status = G_IO_STATUS_NORMAL;
799
800         BT_DBG("+");
801
802         fd = g_io_channel_unix_get_fd(chan);
803         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
804                 BT_ERR_C("RFComm Client  disconnected: %d", fd);
805                 goto fail;
806         }
807
808         buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
809         status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
810                         &len, &err);
811         if (status != G_IO_STATUS_NORMAL) {
812                 BT_ERR("IO Channel read is failed with %d", status);
813                 g_free(buffer);
814                 if (err) {
815                         BT_ERR("IO Channel read error [%s]", err->message);
816                         if (status == G_IO_STATUS_ERROR &&
817                                         __is_error_by_disconnect(err)) {
818                                 BT_ERR("cond : %d", cond);
819                                 g_error_free(err);
820                                 goto fail;
821                         }
822                         g_error_free(err);
823                 }
824
825                 return TRUE;
826         }
827
828         if (len == 0) {
829                 BT_ERR("Length is zero, remote end hang up");
830                 goto fail;
831         }
832
833         BT_DBG("fd: %d, len: %d, buffer: %s", fd, len, buffer);
834
835         event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
836         if (event_info == NULL) {
837                 BT_INFO("event_info == NULL");
838                 g_free(buffer);
839                 return TRUE;
840         }
841
842         data_r.socket_fd = fd;
843         data_r.buffer_size = len;
844         data_r.buffer = buffer;
845
846         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
847                         BLUETOOTH_ERROR_NONE, &data_r,
848                         event_info->cb, event_info->user_data);
849
850         g_free(buffer);
851         return TRUE;
852
853 fail:
854         conn_info = __find_rfcomm_conn_info_with_fd(fd);
855         if (conn_info) {
856                 __bt_rfcomm_client_disconnected(conn_info);
857                 __rfcomm_remove_client_conn_info_t(conn_info);
858         } else {
859                 BT_ERR("RFCOMM client conn_info not found");
860         }
861         return FALSE;
862 }
863
864 static void __rfcomm_client_connection_create_watch(rfcomm_client_conn_info_t *conn_info)
865 {
866         GIOChannel *data_io;
867
868         ret_if(NULL == conn_info);
869
870         BT_DBG("+");
871
872         data_io = g_io_channel_unix_new(conn_info->sock_fd);
873         g_io_channel_set_encoding(data_io, NULL, NULL);
874         g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
875         conn_info->watch_id = g_io_add_watch(data_io,
876                         G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
877                         __client_data_received_cb, NULL);
878         g_io_channel_unref(data_io);
879
880         BT_DBG("-");
881 }
882
883 static void __bt_rfcomm_handle_new_client_connection(bluetooth_rfcomm_connection_t *info)
884 {
885         rfcomm_client_conn_info_t *conn_info;
886
887         ret_if(NULL == info);
888
889         BT_DBG("+");
890
891         conn_info = g_malloc0(sizeof(rfcomm_client_conn_info_t));
892         conn_info->remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
893         _bt_convert_addr_type_to_string(
894                         conn_info->remote_addr, info->device_addr.addr);
895         conn_info->uuid = g_strdup(info->uuid);
896         conn_info->sock_fd = info->socket_fd;
897
898         BT_DBG("Address:%s, UUID:%s Socket: %d",
899                         conn_info->remote_addr, conn_info->uuid, conn_info->sock_fd);
900
901         rfcomm_clients = g_slist_append(rfcomm_clients, conn_info);
902         __rfcomm_client_connection_create_watch(conn_info);
903
904         BT_DBG("-");
905 }
906
907 static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
908 {
909         char *data;
910         int size;
911
912         size = g_variant_get_size(var);
913         if (size > 0) {
914                 data = (char *)g_variant_get_data(var);
915                 if (data)
916                         param = g_array_append_vals(param, data, size);
917
918         }
919 }
920
921
922 /* TODO_40 : 4.0 merge  */
923 /* Don't use this function directly. Instead of it, get the out parameter only */
924 static void __bt_get_event_info(int service_function, GArray *output,
925                         int *event, int *event_type, void **param_data)
926 {
927         ret_if(event == NULL);
928
929         BT_DBG("service_function : %s (0x%x)",
930                 _bt_convert_service_function_to_string(service_function),
931                 service_function);
932         switch (service_function) {
933         case BT_RFCOMM_CLIENT_CONNECT:
934                 *event_type = BT_RFCOMM_CLIENT_EVENT;
935                 *event = BLUETOOTH_EVENT_RFCOMM_CONNECTED;
936                 ret_if(output == NULL);
937                 *param_data = &g_array_index(output,
938                                 bluetooth_rfcomm_connection_t, 0);
939                 break;
940         default:
941                 BT_ERR("Unknown function");
942                 return;
943         }
944 }
945
946
947 static void __async_req_cb_with_unix_fd_list(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data)
948 {
949         int result = BLUETOOTH_ERROR_NONE;
950         int event_type = BT_ADAPTER_EVENT;
951         bt_req_info_t *cb_data = user_data;
952         bluetooth_event_param_t bt_event;
953
954         GError *error = NULL;
955         GVariant *value;
956         GVariant *param1;
957         GArray *out_param1 = NULL;
958         GUnixFDList *out_fd_list = NULL;
959
960         BT_DBG("+");
961
962         memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
963
964         value = g_dbus_proxy_call_with_unix_fd_list_finish(proxy, &out_fd_list, res, &error);
965         if (value == NULL) {
966                 if (error) {
967                         /* dBUS gives error cause */
968                         BT_ERR("D-Bus API failure: message[%s]",
969                                         error->message);
970                         g_clear_error(&error);
971                 }
972                 result = BLUETOOTH_ERROR_TIMEOUT;
973
974                 ret_if(cb_data == NULL);
975
976                 __bt_get_event_info(cb_data->service_function, NULL,
977                                 &bt_event.event, &event_type,
978                                 &bt_event.param_data);
979                 goto failed;
980         }
981
982         g_variant_get(value, "(iv)", &result, &param1);
983         g_variant_unref(value);
984
985         if (param1) {
986                 out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
987                 __bt_fill_garray_from_variant(param1, out_param1);
988                 g_variant_unref(param1);
989         }
990
991         if (!cb_data)
992                 goto done;
993
994         __bt_get_event_info(cb_data->service_function, out_param1,
995                         &bt_event.event, &event_type,
996                         &bt_event.param_data);
997
998         if (result == BLUETOOTH_ERROR_NONE && out_param1) {
999                 if (BT_RFCOMM_CLIENT_CONNECT == cb_data->service_function) {
1000                         int *fd_list_array;
1001                         int len = 0;
1002                         bluetooth_rfcomm_connection_t *conn_info;
1003
1004                         conn_info = (bluetooth_rfcomm_connection_t *)bt_event.param_data;
1005                         if (!out_fd_list) {
1006                                 BT_ERR("out_fd_list is NULL");
1007                                 goto failed;
1008                         }
1009
1010                         fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
1011                         BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
1012                         conn_info->socket_fd = fd_list_array[0];
1013
1014                         BT_DBG("conn_info->socket_fd: %d", conn_info->socket_fd);
1015                         __bt_rfcomm_handle_new_client_connection(conn_info);
1016
1017                         if (cb_data->cb != NULL) {
1018                                 /* Send client connected event */
1019                                 bt_event.result = result;
1020                                 BT_INFO("event_type[%d], result=[%d]", event_type, result);
1021                                 ((bluetooth_cb_func_ptr)cb_data->cb)(
1022                                         bt_event.event, &bt_event, cb_data->user_data);
1023                         }
1024
1025                         g_free(fd_list_array);
1026                         g_object_unref(out_fd_list);
1027                 }
1028                 goto done;
1029         }
1030
1031 failed:
1032         if (cb_data->cb == NULL)
1033                 goto done;
1034
1035         /* Only if fail case, call the callback function*/
1036         bt_event.result = result;
1037
1038         BT_INFO("event_type[%d], result=[%d]", event_type, result);
1039         if (event_type == BT_RFCOMM_CLIENT_EVENT) {
1040                 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
1041                         &bt_event, cb_data->user_data);
1042         } else {
1043                 BT_INFO("Not handled event type : %d", event_type);
1044         }
1045 done:
1046         if (out_param1)
1047                 g_array_free(out_param1, TRUE);
1048
1049         g_free(cb_data);
1050         BT_DBG("-");
1051 }
1052 #endif
1053
1054 BT_EXPORT_API int bluetooth_rfcomm_connect(
1055                 const bluetooth_device_address_t *remote_bt_address,
1056                 const char *remote_uuid)
1057 {
1058
1059 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1060         rfcomm_cb_data_t *cb_data = NULL;
1061         rfcomm_conn_info_t *conn = NULL;
1062 #else
1063         int result;
1064         int connect_type;
1065         bt_user_info_t *user_info;
1066         char uuid[BLUETOOTH_UUID_STRING_MAX];
1067 #endif
1068         BT_CHECK_PARAMETER(remote_bt_address, return);
1069         BT_CHECK_PARAMETER(remote_uuid, return);
1070         BT_CHECK_ENABLED(return);
1071
1072 #ifdef TIZEN_FEATURE_BT_DPM
1073         if (_bt_check_dpm(BT_DPM_ADDRESS, (void *)remote_bt_address) == BT_DPM_RESTRICTED) {
1074                 BT_ERR("Blacklist device");
1075                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1076         }
1077
1078         if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED ||
1079                 _bt_check_dpm(BT_DPM_HF_ONLY, NULL) == BT_DPM_RESTRICTED) {
1080                 BT_ERR("Not allow to connect the RFCOMM service");
1081                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1082         }
1083
1084         if (_bt_check_dpm(BT_DPM_DESKTOP, NULL) == BT_DPM_RESTRICTED) {
1085                 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1086                 bluetooth_device_class_t dev_class;
1087
1088                 _bt_convert_addr_type_to_string(address, (unsigned char *)remote_bt_address->addr);
1089                 _bt_get_cod_by_address(address, &dev_class);
1090
1091                 if (dev_class.major_class == BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
1092                         BT_ERR("Reject a authorization due to MDM Policy");
1093                         return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1094                 }
1095         }
1096 #endif
1097
1098 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1099         BT_INFO_C("### Connect RFCOMM");
1100         int ret;
1101         int id, object_id;
1102         char *path;
1103
1104         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CLIENT_CONNECT)
1105              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1106                 BT_ERR("Don't have a privilege to use this API");
1107                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1108         }
1109
1110         id = __rfcomm_assign_id();
1111         if (id < 0)
1112                 return BLUETOOTH_ERROR_INTERNAL;
1113
1114         cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
1115         if (!cb_data) {
1116                 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
1117
1118                 object_id = _bt_register_new_conn(path, new_connection);
1119                 if (object_id < 0) {
1120                         __rfcomm_delete_id(id);
1121                         g_free(path);
1122                         return BLUETOOTH_ERROR_INTERNAL;
1123                 }
1124
1125                 cb_data = g_new0(rfcomm_cb_data_t, 1);
1126                 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
1127                 cb_data->obj_path = path;
1128                 cb_data->object_id = object_id;
1129                 cb_data->id = id;
1130         }
1131
1132         conn = g_new0(rfcomm_conn_info_t, 1);
1133         conn->fd = -1;
1134         _bt_convert_addr_type_to_string(conn->bt_addr,
1135                                 (unsigned char *)remote_bt_address->addr);
1136
1137         BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
1138         cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
1139
1140         ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
1141                                 __bt_discover_service_response_cb, cb_data);
1142         if (ret != BLUETOOTH_ERROR_NONE) {
1143                 BT_ERR("Error returned while service discovery");
1144                 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
1145                 if (cb_data->rfcomm_conns == NULL)
1146                         rfcomm_cb_data_remove(cb_data);
1147                 return BLUETOOTH_ERROR_INTERNAL;
1148         }
1149
1150         if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
1151                 BT_INFO("Adding callback information to rfcomm_clients");
1152                 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
1153         } else
1154                 BT_INFO("Callback information is already added");
1155
1156         return BLUETOOTH_ERROR_NONE;
1157 #else
1158         user_info = _bt_get_user_data(BT_COMMON);
1159         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
1160
1161         /* connect_type:  BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
1162         /* In now, we only support to connecty using UUID */
1163         connect_type = BT_RFCOMM_UUID;
1164
1165         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CLIENT_CONNECT)
1166              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1167                 BT_ERR("Don't have a privilege to use this API");
1168                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1169         }
1170
1171         BT_INIT_PARAMS();
1172         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1173
1174         g_array_append_vals(in_param1, remote_bt_address,
1175                                 sizeof(bluetooth_device_address_t));
1176
1177         g_strlcpy(uuid, remote_uuid, sizeof(uuid));
1178         g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
1179
1180         g_array_append_vals(in_param3, &connect_type, sizeof(int));
1181
1182         result = _bt_send_request_async_with_unix_fd_list(BT_BLUEZ_SERVICE,
1183                                 BT_RFCOMM_CLIENT_CONNECT,
1184                                 in_param1, in_param2,
1185                                 in_param3, in_param4,
1186                                 user_info->cb, user_info->user_data,
1187                                 NULL, (GAsyncReadyCallback)__async_req_cb_with_unix_fd_list);
1188
1189         BT_DBG("result: %x", result);
1190
1191         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1192
1193         return result;
1194 #endif
1195 }
1196
1197 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
1198 {
1199 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1200         GSList *l;
1201         GSList *conn_list = NULL;
1202         rfcomm_cb_data_t *client_info;
1203         rfcomm_conn_info_t *conn_info;
1204         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1205
1206         BT_CHECK_PARAMETER(device_address, return);
1207         BT_CHECK_PARAMETER(connected, return);
1208
1209         _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
1210         *connected = FALSE;
1211
1212         for (l = rfcomm_clients; l != NULL; l = l->next) {
1213                 client_info = l->data;
1214                 if (client_info == NULL)
1215                         continue;
1216                 for (conn_list = client_info->rfcomm_conns;
1217                         conn_list != NULL; conn_list = conn_list->next) {
1218                         conn_info = conn_list->data;
1219                         if (conn_info == NULL)
1220                                 continue;
1221
1222                         if (g_strcmp0(address, conn_info->bt_addr) == 0) {
1223                                 *connected = TRUE;
1224                                 return BLUETOOTH_ERROR_NONE;
1225                         }
1226                 }
1227         }
1228
1229         return BLUETOOTH_ERROR_NONE;
1230 #else
1231         GSList *l;
1232         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1233
1234         BT_CHECK_PARAMETER(device_address, return);
1235         BT_CHECK_PARAMETER(connected, return);
1236
1237         BT_DBG("+");
1238
1239         *connected = FALSE;
1240         _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
1241         BT_INFO("Client address: [%s]", address);
1242
1243         for (l = rfcomm_clients; l != NULL; l = l->next) {
1244                 rfcomm_client_conn_info_t *info = l->data;
1245
1246                 if (info && !strncasecmp(info->remote_addr, address, BT_ADDRESS_STRING_SIZE)) {
1247                         BT_INFO("Match found");
1248                         *connected = TRUE;
1249                         return BLUETOOTH_ERROR_NONE;
1250                 }
1251         }
1252
1253         BT_DBG("-");
1254         return BLUETOOTH_ERROR_NONE;
1255 #endif
1256 }
1257
1258 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
1259 {
1260         int result;
1261         int connected = FALSE;
1262
1263         BT_CHECK_ENABLED(return);
1264
1265         BT_INIT_PARAMS();
1266         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1267
1268         result = _bt_send_request(BT_BLUEZ_SERVICE,
1269                         BT_RFCOMM_CLIENT_IS_CONNECTED,
1270                         in_param1, in_param2, in_param3,
1271                         in_param4, &out_param);
1272
1273         BT_DBG("result: %x", result);
1274
1275         if (result == BLUETOOTH_ERROR_NONE) {
1276                 connected = g_array_index(out_param,
1277                                 int, 0);
1278         } else {
1279                 BT_ERR("Fail to send request");
1280         }
1281
1282         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1283
1284         return connected;
1285 }
1286
1287 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
1288 {
1289 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1290         rfcomm_cb_data_t *info;
1291         rfcomm_conn_info_t *conn_info;
1292
1293         BT_INFO_C("### Disconnect RFCOMM");
1294
1295         BT_CHECK_ENABLED(return);
1296
1297         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_DISCONNECT)
1298              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1299                 BT_ERR("Don't have a privilege to use this API");
1300                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1301         }
1302
1303         BT_DBG("Requested FD %d", socket_fd);
1304         if (socket_fd < 0) {
1305                 BT_ERR("Invalid FD");
1306                 return BLUETOOTH_ERROR_INVALID_PARAM;
1307         }
1308         BT_DBG("FDD %d", socket_fd);
1309
1310         info = __find_rfcomm_info_with_fd(socket_fd);
1311         if (info == NULL) {
1312                 BT_DBG("Could not find in client, so check in server");
1313                 return bluetooth_rfcomm_server_disconnect(socket_fd);
1314         }
1315
1316         conn_info = __get_conn_info_from_fd(info, socket_fd);
1317         if (conn_info == NULL) {
1318                 BT_ERR("Could not find connection info");
1319                 return BLUETOOTH_ERROR_INTERNAL;
1320         }
1321
1322         if (conn_info->watch_id == 0 || conn_info->disconnected) {
1323                 BT_ERR("Invalid state");
1324                 return BLUETOOTH_ERROR_NOT_CONNECTED;
1325         }
1326
1327         close(conn_info->fd);
1328         conn_info->disconnected = TRUE;
1329
1330         BT_INFO("conn_info %s", conn_info->bt_addr);
1331         _bt_disconnect_ext_profile(conn_info->bt_addr, info->obj_path);
1332
1333         if (info->idle_id == 0)
1334                 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
1335
1336         return BLUETOOTH_ERROR_NONE;
1337 #else
1338         rfcomm_client_conn_info_t *conn_info;
1339
1340         BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
1341
1342         BT_CHECK_ENABLED(return);
1343         retv_if(socket_fd < 0, BLUETOOTH_ERROR_INVALID_PARAM);
1344
1345         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_DISCONNECT)
1346                         == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1347                 BT_ERR("Don't have a privilege to use this API");
1348                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1349         }
1350
1351         BT_DBG("FD %d", socket_fd);
1352
1353         conn_info = __find_rfcomm_conn_info_with_fd(socket_fd);
1354         if (conn_info == NULL) {
1355                 BT_DBG("Could not find in client, so check in server");
1356                 /* Check for fd in server list and perform the disconnection if present */
1357                 return bluetooth_rfcomm_server_disconnect(socket_fd);
1358         }
1359
1360         if (conn_info->watch_id <= 0) {
1361                 BT_ERR("Invalid state");
1362                 return BLUETOOTH_ERROR_NOT_CONNECTED;
1363         }
1364
1365         /*
1366          * Just close socket here and return. Socket close will be detected via I/O watch
1367          * and disconnection event as well as info cleanup will be performed there.
1368          */
1369         close(conn_info->sock_fd);
1370
1371         return BLUETOOTH_ERROR_NONE;
1372 #endif
1373 }
1374
1375 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1376 #else
1377 static int __write_all(int fd, const char *buf, int len)
1378 {
1379         int sent = 0, try = 0;
1380
1381         BT_DBG("+");
1382         while (len > 0) {
1383                 int written;
1384
1385                 written = write(fd, buf, len);
1386                 BT_DBG("written: %d", written);
1387                 if (written < 0) {
1388                         if (errno == EINTR || errno == EAGAIN) {
1389                                 try++;
1390                                 if (try <= 49)
1391                                         continue;
1392                         }
1393                         return -1;
1394                 }
1395
1396                 if (!written)
1397                         return 0;
1398
1399                 len -= written;
1400                 buf += written;
1401                 sent += written;
1402                 try = 0;
1403         }
1404
1405         BT_DBG("-");
1406         return sent;
1407 }
1408 #endif
1409
1410 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
1411 {
1412 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1413         int written;
1414 #endif
1415         int result;
1416
1417 #ifndef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1418         BT_CHECK_ENABLED(return);
1419 #endif
1420         BT_CHECK_PARAMETER(buf, return);
1421         if (fd < 0) {
1422                 BT_ERR("Invalid FD");
1423                 return BLUETOOTH_ERROR_INVALID_PARAM;
1424         }
1425
1426         retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
1427
1428         switch (privilege_token) {
1429         case 0:
1430                 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_WRITE);
1431
1432                 if (result == BLUETOOTH_ERROR_NONE) {
1433                         privilege_token = 1; /* Have a permission */
1434                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1435                         BT_ERR("Don't have a privilege to use this API");
1436                         privilege_token = -1; /* Don't have a permission */
1437                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
1438                 } else {
1439                         /* Just break - It is not related with permission error */
1440                 }
1441                 break;
1442         case 1:
1443                 /* Already have a privilege */
1444                 break;
1445         case -1:
1446                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1447         default:
1448                 /* Invalid privilge token value */
1449                 return BLUETOOTH_ERROR_INTERNAL;
1450         }
1451
1452 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1453         written = write(fd, buf, length);
1454         /*BT_DBG("Length %d, written = %d, balance(%d)",
1455                         length, written, length - written); */
1456         if (written > 0 && bluetooth_get_battery_monitor_state()) {
1457                 if (tx_tag == 0) {
1458                         BT_DBG("Adding rfcomm tx timeout function for battery monitor");
1459                         tx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_tx_data, NULL);
1460                 }
1461                 tx_data += written;
1462         }
1463         return written;
1464 #else
1465         result = __write_all(fd, buf, length);
1466         if (result > 0 && bluetooth_get_battery_monitor_state()) {
1467                 if (tx_tag == 0) {
1468                         BT_DBG("Adding rfcomm tx timeout function for battery monitor");
1469                         tx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_tx_data, NULL);
1470                 }
1471                 tx_data += written;
1472         }
1473         return result;
1474 #endif
1475 }