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