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