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