b2e36e44ade40bc12e6fe65e3e9f9ec136c07cea
[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 #ifdef RFCOMM_DIRECT
20 #include <errno.h>
21 #include <gio/gunixfdlist.h>
22 #endif
23
24 #include "bluetooth-api.h"
25 #include "bt-internal-types.h"
26
27 #include "bt-common.h"
28 #include "bt-request-sender.h"
29 #include "bt-event-handler.h"
30
31 #ifdef RFCOMM_DIRECT
32
33 #define BT_TIMEOUT_MESSAGE "Did not receive a reply. Possible causes include: " \
34                         "the remote application did not send a reply, " \
35                         "the message bus security policy blocked the reply, " \
36                         "the reply timeout expired, or the network connection " \
37                         "was broken."
38
39 static GSList *rfcomm_clients;
40
41 /* Variable for privilege, only for write API,
42   before we should reduce time to bt-service dbus calling
43   -1 : Don't have a permission to access API
44   0 : Initial value, not yet check
45   1 : Have a permission to access API
46 */
47 static int privilege_token;
48
49 typedef struct {
50         char bt_addr[BT_ADDRESS_STRING_SIZE];
51         int fd;
52         int watch_id;
53         gboolean disconnected;
54 } rfcomm_conn_info_t;
55
56 typedef struct {
57         char uuid[BLUETOOTH_UUID_STRING_MAX];
58         char *device_path;
59         char *obj_path;
60         int object_id;
61         int id;
62         GSList *rfcomm_conns;
63         unsigned int idle_id;
64 } rfcomm_cb_data_t;
65
66 static void __client_connected_cb(rfcomm_cb_data_t *cb_data,
67         char *dev_address, int result);
68
69 static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
70 {
71         BT_DBG("+");
72
73         if (cb_data->id >= 0)
74                 __rfcomm_delete_id(cb_data->id);
75
76         if (cb_data->object_id > 0)
77                 _bt_unregister_gdbus(cb_data->object_id);
78
79         if (cb_data->obj_path) {
80                 BT_INFO("Unregister profile");
81                 _bt_unregister_profile(cb_data->obj_path);
82         }
83
84         if (cb_data->idle_id != 0) {
85                 BT_INFO("Removing idle source");
86                 g_source_remove(cb_data->idle_id);
87         }
88
89         g_free(cb_data->obj_path);
90
91         g_free(cb_data->device_path);
92         g_free(cb_data);
93         BT_DBG("-");
94 }
95
96 static void rfcomm_cb_data_remove(rfcomm_cb_data_t *info)
97 {
98         if (info) {
99                 BT_INFO("No more device connected remove info");
100                 rfcomm_clients = g_slist_remove(rfcomm_clients, info);
101                 __bt_free_cb_data(info);
102         }
103 }
104
105 gint compare(gpointer *a, gpointer *b)
106 {
107         rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
108         char *address = (char *)b;
109         return g_strcmp0(node->bt_addr, address);
110 }
111
112 gint compare_fd(gpointer *a, gpointer *b)
113 {
114         rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
115         int fd = (int )*b;
116         if (node->fd == fd)
117                 return 0;
118         return 1;
119 }
120 static void __bt_free_conn(rfcomm_conn_info_t *conn)
121 {
122         BT_DBG("+");
123
124         if (conn == NULL)
125                 return;
126
127         if (conn->fd > 0)
128                 close(conn->fd);
129         if (conn->watch_id > 0) {
130                 g_source_remove(conn->watch_id);
131                 conn->watch_id = 0;
132         }
133         g_free(conn);
134
135         BT_DBG("-");
136 }
137
138 static void __rfcomm_remove_conn_info_t(rfcomm_cb_data_t *info, char *address)
139 {
140         GSList *l = NULL;
141         rfcomm_conn_info_t *conn_info = NULL;
142         l = g_slist_find_custom(info->rfcomm_conns, address, (GCompareFunc)compare);
143         if (l)
144                 conn_info = l->data;
145         if (conn_info) {
146                 info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn_info);
147                 __bt_free_conn(conn_info);
148         }
149 }
150
151 static rfcomm_conn_info_t *__get_conn_info_from_fd(rfcomm_cb_data_t *info,
152         int fd)
153 {
154         GSList *l;
155         rfcomm_conn_info_t *device_node = NULL;
156         for(l = info->rfcomm_conns; l != NULL; l = l->next) {
157                 device_node = l->data;
158                 if ( device_node && device_node->fd == fd)
159                         return device_node;
160         }
161         return NULL;
162 }
163
164 static rfcomm_conn_info_t *__get_conn_info_from_address(rfcomm_cb_data_t *info,
165                 char *dev_address)
166 {
167         GSList *l = NULL;
168         rfcomm_conn_info_t *conn_info = NULL;
169         l = g_slist_find_custom(info->rfcomm_conns, dev_address,
170                 (GCompareFunc)compare);
171         if (l)
172                 conn_info = l->data;
173         return conn_info;
174 }
175
176 static void __rfcomm_client_connected_cb(rfcomm_cb_data_t *info,
177         char *dev_address, int result)
178 {
179         __client_connected_cb(info, dev_address, result);
180         __rfcomm_remove_conn_info_t(info, dev_address);
181
182         if (info->rfcomm_conns == NULL)
183                 rfcomm_cb_data_remove(info);
184 }
185
186 static rfcomm_cb_data_t *__find_rfcomm_info_with_fd(int fd)
187 {
188         GSList *l;
189         GSList *device_fd;
190         for (l = rfcomm_clients; l != NULL; l = l->next) {
191                 rfcomm_cb_data_t *info = l->data;
192                 device_fd = g_slist_find_custom(info->rfcomm_conns, &fd,
193                         (GCompareFunc)compare_fd);
194                 if (device_fd)
195                         return info;
196         }
197
198         return NULL;
199 }
200
201 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
202 {
203         GSList *l;
204
205         for (l = rfcomm_clients; l != NULL; l = l->next) {
206                 rfcomm_cb_data_t *info = l->data;
207
208                 if (info != NULL)
209                         if (g_strcmp0(info->obj_path, path) == 0)
210                                 return info;
211         }
212
213         return NULL;
214 }
215
216 static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
217 {
218         GSList *l;
219
220         for (l = rfcomm_clients; l != NULL; l = l->next) {
221                 rfcomm_cb_data_t *info = l->data;
222
223                 if (g_strcmp0(info->uuid, uuid) == 0)
224                         return info;
225         }
226
227         return NULL;
228 }
229
230 static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
231         rfcomm_cb_data_t *info)
232 {
233         if (conn_info == NULL)
234                 return;
235
236         bluetooth_rfcomm_disconnection_t disconn_info;
237         bt_event_info_t *event_info = NULL;
238         if (conn_info->disconnected == FALSE)
239                 return;
240         event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
241         if (event_info == NULL) {
242                 if (info->rfcomm_conns == NULL)
243                         rfcomm_cb_data_remove(info);
244                 return;
245         }
246         memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
247         disconn_info.device_role = RFCOMM_ROLE_CLIENT;
248         g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
249         BT_DBG("Disconnected FD [%d]", conn_info->fd);
250         _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
251                                         conn_info->bt_addr);
252
253         disconn_info.socket_fd = conn_info->fd;
254
255         BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
256                         BLUETOOTH_ERROR_NONE, conn_info->bt_addr,
257                         info->uuid, conn_info->fd);
258         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
259                         BLUETOOTH_ERROR_NONE, &disconn_info,
260                         event_info->cb, event_info->user_data);
261
262         if(conn_info) {
263                 BT_DBG("List is present deleting it");
264                 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
265         }
266         if (info->rfcomm_conns == NULL)
267                 rfcomm_cb_data_remove(info);
268
269         BT_DBG("-");
270 }
271
272 static gboolean __rfcomm_client_disconnect(gpointer user_data)
273 {
274         rfcomm_cb_data_t *info = (rfcomm_cb_data_t *) user_data;
275         BT_INFO_C("Disconnected [RFCOMM Client]");
276         retv_if(info == NULL, FALSE);
277
278         if (g_slist_find(rfcomm_clients, info) == NULL) {
279                 BT_INFO("rfcomm resource is already freed");
280                 return FALSE;
281         }
282         info->idle_id = 0;
283         g_slist_foreach(info->rfcomm_conns,
284                 (GFunc) _bt_rfcomm_disconnect_conn_info, info);
285         BT_DBG("-");
286         return FALSE;
287 }
288
289 static gboolean __is_error_by_disconnect(GError *err)
290 {
291         return !g_strcmp0(err->message, "Connection reset by peer") ||
292                         !g_strcmp0(err->message, "Connection timed out") ||
293                         !g_strcmp0(err->message, "Software caused connection abort");
294 }
295
296 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
297                                                                 gpointer data)
298 {
299         char *buffer = NULL;
300         gsize len = 0;
301         int result = BLUETOOTH_ERROR_NONE;
302         rfcomm_cb_data_t *info = data;
303         rfcomm_conn_info_t *conn_info = NULL;
304         bt_event_info_t *event_info;
305         bluetooth_rfcomm_received_data_t data_r;
306         GIOStatus status = G_IO_STATUS_NORMAL;
307         GError *err = NULL;
308         int fd;
309         BT_DBG("");
310
311         retv_if(info == NULL, FALSE);
312         fd = g_io_channel_unix_get_fd(chan);
313         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
314                 BT_ERR_C("RFComm Client  disconnected: %d", fd);
315                 conn_info = __get_conn_info_from_fd(info, fd);
316                 if (conn_info == NULL) {
317                         BT_ERR("No Connection info found with FD [%d]", fd);
318                         return FALSE;
319                 }
320                 conn_info->disconnected = TRUE;
321                 __rfcomm_client_disconnect(info);
322                 return FALSE;
323         }
324
325         buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
326
327         status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
328                         &len, &err);
329         if (status != G_IO_STATUS_NORMAL) {
330                 BT_ERR("IO Channel read is failed with %d", status);
331
332                 g_free(buffer);
333                 if (err) {
334                         BT_ERR("IO Channel read error [%s]", err->message);
335                         if (status == G_IO_STATUS_ERROR &&
336                                         __is_error_by_disconnect(err)) {
337                                 BT_ERR("cond : %d", cond);
338                                 g_error_free(err);
339                                 conn_info = __get_conn_info_from_fd(info, fd);
340                                 if (conn_info == NULL) {
341                                         BT_ERR("No Connection info found with FD [%d]", fd);
342                                         return FALSE;
343                                 }
344                                 conn_info->disconnected = TRUE;
345                                 __rfcomm_client_disconnect(info);
346                                 return FALSE;
347                         }
348                         g_error_free(err);
349                 }
350                 return TRUE;
351         }
352
353         event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
354         if (event_info == NULL) {
355                 g_free(buffer);
356                 return TRUE;
357         }
358
359         data_r.socket_fd = fd;
360         data_r.buffer_size = len;
361         data_r.buffer = buffer;
362
363         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
364                         result, &data_r,
365                         event_info->cb, event_info->user_data);
366
367         g_free(buffer);
368         return TRUE;
369 }
370
371 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
372         int result)
373 {
374         bluetooth_rfcomm_connection_t conn_info;
375         bt_event_info_t *event_info;
376         rfcomm_conn_info_t *conn_list_info = NULL;
377         BT_INFO_C("Connected [RFCOMM Client]");
378
379         event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
380         if (event_info == NULL)
381                 return;
382
383         memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
384         conn_info.device_role = RFCOMM_ROLE_CLIENT;
385         g_strlcpy(conn_info.uuid, cb_data->uuid, BLUETOOTH_UUID_STRING_MAX);
386         _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
387                         dev_address);
388         conn_list_info = __get_conn_info_from_address(cb_data, dev_address);
389         if (conn_list_info == NULL) {
390                 BT_ERR("Device addres %s not found in connection list", dev_address);
391                 return;
392         }
393         conn_info.socket_fd = conn_list_info->fd;
394         conn_info.server_id = -1;
395
396         BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
397                         result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
398         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
399                         result, &conn_info,
400                         event_info->cb, event_info->user_data);
401         BT_DBG("-");
402 }
403
404 #endif
405
406 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
407 {
408         rfcomm_cb_data_t *info;
409         GIOChannel *data_io;
410         rfcomm_conn_info_t *conn_info = NULL;
411         BT_DBG("%s %d", path, fd);
412         char address[BT_ADDRESS_STRING_SIZE];
413         _bt_convert_addr_type_to_string(address,
414                                 (unsigned char *)addr);
415         info = __find_rfcomm_info_from_path(path);
416         if (info == NULL)
417                 return -1;
418         conn_info = __get_conn_info_from_address(info, address);
419
420         if (conn_info == NULL) {
421                 BT_ERR("Device Address %s not found in connection list", address);
422                 return -1;
423         }
424         conn_info->fd = fd;
425         BT_DBG("connection info fd %d", conn_info->fd);
426         data_io = g_io_channel_unix_new(fd);
427         g_io_channel_set_encoding(data_io, NULL, NULL);
428         g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
429         conn_info->watch_id = g_io_add_watch(data_io,
430                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
431                                 __client_data_received_cb, info);
432
433         g_io_channel_unref(data_io);
434
435         __client_connected_cb(info, address,BLUETOOTH_ERROR_NONE);
436
437         return 0;
438 }
439
440 static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
441                                                         gpointer user_data)
442
443 {
444         GError *error = NULL;
445         rfcomm_cb_data_t *cb_data;
446         char dev_address[BT_ADDRESS_STRING_SIZE];
447         const char *path;
448         BT_DBG("+");
449
450         ret_if(user_data == NULL);
451
452         cb_data = user_data;
453
454         if (!g_dbus_proxy_call_finish(proxy, res, &error)) {
455                 int result;
456                 g_dbus_error_strip_remote_error(error);
457                 BT_ERR("Error : %s \n", error->message);
458
459                 if (g_strcmp0(error->message, "In Progress") == 0)
460                         result = BLUETOOTH_ERROR_DEVICE_BUSY;
461                 else
462                         result = BLUETOOTH_ERROR_INTERNAL;
463                 path = g_dbus_proxy_get_object_path(proxy);
464                 _bt_convert_device_path_to_address(path, dev_address);
465                 __rfcomm_client_connected_cb(cb_data, dev_address, result);
466
467                 g_error_free(error);
468         }
469         if (proxy)
470                 g_object_unref(proxy);
471
472         BT_DBG("-");
473 }
474
475 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
476                                 GAsyncResult *res, gpointer user_data)
477 {
478         rfcomm_cb_data_t *cb_data;
479         int ret = 0;
480         GError *err = NULL;
481         bt_register_profile_info_t info = {0};
482         int result = BLUETOOTH_ERROR_NONE;
483         char dev_address[BT_ADDRESS_STRING_SIZE];
484         const char *path;
485
486         BT_DBG("+");
487
488         ret_if(user_data == NULL);
489
490         cb_data = user_data;
491
492         path = g_dbus_proxy_get_object_path(proxy);
493
494         _bt_convert_device_path_to_address(path, dev_address);
495         BT_DBG("Device Adress [%s]", dev_address);
496         g_dbus_proxy_call_finish(proxy, res, &err);
497         if (proxy)
498                 g_object_unref(proxy);
499
500         if (err != NULL) {
501                 g_dbus_error_strip_remote_error(err);
502                 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
503                 if (!strcmp("Operation canceled", err->message)) {
504                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
505                 } else if (!strcmp("In Progress", err->message)) {
506                         result = BLUETOOTH_ERROR_IN_PROGRESS;
507                 } else if (!strcmp("Host is down", err->message)) {
508                         result = BLUETOOTH_ERROR_HOST_DOWN;
509                 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
510                         result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
511                         ret = _bt_cancel_discovers(dev_address);
512                         if (ret != BLUETOOTH_ERROR_NONE)
513                                 BT_ERR("Error: While CancelDiscovery");
514                 } else {
515                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
516                 }
517                 __rfcomm_client_connected_cb(cb_data, dev_address, result);
518                 goto done;
519         } else {
520                 BT_INFO("Services are Updated checking required uuid is there");
521                 /* Check here for uuid present */
522                 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
523                 if (ret == BLUETOOTH_ERROR_NONE) {
524                         info.uuid = (char *)cb_data->uuid;
525                         info.obj_path = cb_data->obj_path;
526                         info.role = "client";
527
528                         ret = _bt_register_profile(&info, FALSE);
529                         if (ret < 0)
530                                 BT_DBG("Error: register profile");
531                         ret = _bt_connect_profile(dev_address, cb_data->uuid,
532                                                 __bt_connect_response_cb, cb_data);
533
534                         if (ret != BLUETOOTH_ERROR_NONE) {
535                                 BT_ERR("ConnectProfile failed");
536                                 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
537                                 __rfcomm_client_connected_cb(cb_data, dev_address, result);
538                                 goto done;
539                         }
540                 } else {
541                         BT_ERR("remote uuid not found");
542                         result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
543                         __rfcomm_client_connected_cb(cb_data, dev_address, result);
544                 }
545         }
546 done:
547         if (err)
548                 g_clear_error(&err);
549 }
550
551 BT_EXPORT_API int bluetooth_rfcomm_connect(
552                 const bluetooth_device_address_t *remote_bt_address,
553                 const char *remote_uuid)
554 {
555
556 #ifdef RFCOMM_DIRECT
557         rfcomm_cb_data_t *cb_data = NULL;
558         rfcomm_conn_info_t *conn = NULL;
559 #else
560         int result;
561         int connect_type;
562         bt_user_info_t *user_info;
563         char uuid[BLUETOOTH_UUID_STRING_MAX];
564 #endif
565         BT_CHECK_PARAMETER(remote_bt_address, return);
566         BT_CHECK_PARAMETER(remote_uuid, return);
567         BT_CHECK_ENABLED(return);
568
569 #ifdef RFCOMM_DIRECT
570         BT_INFO_C("<<<<<<<<< RFCOMM Connect request from app >>>>>>>>>>>");
571         int ret;
572         int id, object_id;
573         char *path;
574
575         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
576              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
577                 BT_ERR("Don't have a privilege to use this API");
578                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
579         }
580
581         id = __rfcomm_assign_id();
582         if (id < 0)
583                 return BLUETOOTH_ERROR_INTERNAL;
584
585         cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
586         if (!cb_data) {
587                 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
588
589                 object_id = _bt_register_new_conn(path, new_connection);
590                 if (object_id < 0) {
591                         __rfcomm_delete_id(id);
592                         return BLUETOOTH_ERROR_INTERNAL;
593                 }
594
595                 cb_data = g_new0(rfcomm_cb_data_t, 1);
596                 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
597                 cb_data->obj_path = path;
598                 cb_data->object_id = object_id;
599                 cb_data->id = id;
600         }
601         conn = g_new0(rfcomm_conn_info_t, 1);
602         conn->fd = -1;
603         _bt_convert_addr_type_to_string(conn->bt_addr,
604                                 (unsigned char *)remote_bt_address->addr);
605
606         BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
607         cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
608         ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
609                                 __bt_discover_service_response_cb, cb_data);
610         if (ret != BLUETOOTH_ERROR_NONE) {
611                 BT_ERR("Error returned while service discovery");
612                 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
613                 if (cb_data->rfcomm_conns == NULL)
614                         rfcomm_cb_data_remove(cb_data);
615                 return BLUETOOTH_ERROR_INTERNAL;
616         }
617         if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
618                 BT_INFO("Adding callback information to rfcomm_clients");
619                 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
620         } else
621                 BT_INFO("Callback information is already added");
622
623         return BLUETOOTH_ERROR_NONE;
624 #else
625         user_info = _bt_get_user_data(BT_COMMON);
626         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
627
628         /* connect_type:  BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
629         /* In now, we only support to connecty using UUID */
630         connect_type = BT_RFCOMM_UUID;
631
632         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
633              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
634                 BT_ERR("Don't have a privilege to use this API");
635                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
636         }
637
638         BT_INIT_PARAMS();
639         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
640
641         g_array_append_vals(in_param1, remote_bt_address,
642                                 sizeof(bluetooth_device_address_t));
643
644         g_strlcpy(uuid, remote_uuid, sizeof(uuid));
645         g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
646
647         g_array_append_vals(in_param3, &connect_type, sizeof(int));
648
649         result = _bt_send_request_async(BT_BLUEZ_SERVICE,
650                                 BT_RFCOMM_CLIENT_CONNECT,
651                                 in_param1, in_param2,
652                                 in_param3, in_param4,
653                                 user_info->cb, user_info->user_data);
654
655         BT_DBG("result: %x", result);
656
657         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
658
659         return result;
660 #endif
661 }
662
663 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
664 {
665         GSList *l;
666         GSList *conn_list = NULL;
667         rfcomm_cb_data_t *client_info;
668         rfcomm_conn_info_t *conn_info;
669         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
670
671         BT_CHECK_PARAMETER(device_address, return);
672         BT_CHECK_PARAMETER(connected, return);
673
674         _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
675         *connected = FALSE;
676
677         for (l = rfcomm_clients; l != NULL; l = l->next) {
678                 client_info = l->data;
679                 if (client_info == NULL)
680                         continue;
681                 for(conn_list = client_info->rfcomm_conns;
682                         conn_list != NULL; conn_list = conn_list->next) {
683                         conn_info = conn_list->data;
684                         if(conn_info == NULL)
685                                 continue;
686
687                         if (g_strcmp0(address, conn_info->bt_addr) == 0) {
688                                 *connected = TRUE;
689                                 return BLUETOOTH_ERROR_NONE;
690                         }
691                 }
692         }
693
694         return BLUETOOTH_ERROR_NONE;
695 }
696
697 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
698 {
699         int result;
700         int connected = FALSE;
701
702         BT_CHECK_ENABLED(return);
703
704         BT_INIT_PARAMS();
705         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
706
707         result = _bt_send_request(BT_BLUEZ_SERVICE,
708                         BT_RFCOMM_CLIENT_IS_CONNECTED,
709                         in_param1, in_param2, in_param3,
710                         in_param4, &out_param);
711
712         BT_DBG("result: %x", result);
713
714         if (result == BLUETOOTH_ERROR_NONE) {
715                 connected = g_array_index(out_param,
716                                 int, 0);
717         } else {
718                 BT_ERR("Fail to send request");
719         }
720
721         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
722
723         return connected;
724 }
725
726 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
727 {
728 #ifdef RFCOMM_DIRECT
729         rfcomm_cb_data_t *info;
730         rfcomm_conn_info_t *conn_info;
731         BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
732         BT_CHECK_ENABLED(return);
733
734         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT)
735              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
736                 BT_ERR("Don't have a privilege to use this API");
737                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
738         }
739
740         if (socket_fd < 0) {
741                 BT_ERR("Invalid FD");
742                 return BLUETOOTH_ERROR_INVALID_PARAM;
743         }
744         BT_DBG("FDD %d", socket_fd);
745
746         info = __find_rfcomm_info_with_fd(socket_fd);
747         if (info == NULL) {
748                 BT_DBG("Could not find in client, so check in server");
749                 return bluetooth_rfcomm_server_disconnect(socket_fd);
750         }
751         conn_info = __get_conn_info_from_fd(info, socket_fd);
752         if (conn_info == NULL) {
753                 BT_ERR("FATAL Error");
754                 return BLUETOOTH_ERROR_INTERNAL;
755         }
756         if (conn_info->watch_id <= 0) {
757                 BT_ERR("Invalid state");
758                 return BLUETOOTH_ERROR_NOT_CONNECTED;
759         }
760         conn_info->disconnected = TRUE;
761         close(socket_fd);
762         BT_INFO("conn_info %s", conn_info->bt_addr);
763         _bt_disconnect_profile(conn_info->bt_addr, info->uuid, NULL,NULL);
764         if (info->idle_id == 0)
765                 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
766
767         return BLUETOOTH_ERROR_NONE;
768 #else
769         int result;
770         int service_function;
771
772         BT_CHECK_ENABLED(return);
773
774         BT_INIT_PARAMS();
775         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
776
777         /* Support the OSP */
778         if (socket_fd == -1) {
779                 /* Cancel connect */
780                 service_function = BT_RFCOMM_CLIENT_CANCEL_CONNECT;
781         } else {
782                 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
783                 service_function = BT_RFCOMM_SOCKET_DISCONNECT;
784         }
785
786         result = _bt_send_request(BT_BLUEZ_SERVICE, service_function,
787                 in_param1, in_param2, in_param3, in_param4, &out_param);
788
789         BT_DBG("result: %x", result);
790
791         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
792
793         return result;
794 #endif
795 }
796
797 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
798 {
799 #ifdef RFCOMM_DIRECT
800         int written;
801 #else
802         char *buffer;
803 #endif
804         int result;
805
806         BT_CHECK_PARAMETER(buf, return);
807         if (fd < 0) {
808                 BT_ERR("Invalid FD");
809                 return BLUETOOTH_ERROR_INVALID_PARAM;
810         }
811
812         BT_DBG("FD : %d", fd);
813
814 #ifndef RFCOMM_DIRECT
815         BT_CHECK_ENABLED(return);
816 #endif
817         retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
818
819 #ifdef RFCOMM_DIRECT
820         switch (privilege_token) {
821         case 0:
822                 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE);
823
824                 if (result == BLUETOOTH_ERROR_NONE) {
825                         privilege_token = 1; /* Have a permission */
826                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
827                         BT_ERR("Don't have a privilege to use this API");
828                         privilege_token = -1; /* Don't have a permission */
829                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
830                 } else {
831                         /* Just break - It is not related with permission error */
832                 }
833                 break;
834         case 1:
835                 /* Already have a privilege */
836                 break;
837         case -1:
838                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
839         default:
840                 /* Invalid privilge token value */
841                 return BLUETOOTH_ERROR_INTERNAL;
842         }
843
844         written = write(fd, buf, length);
845         /*BT_DBG("Length %d, written = %d, balance(%d)",
846                          length, written, length - written); */
847         return written;
848 #else
849         BT_INIT_PARAMS();
850         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
851
852         buffer = g_malloc0(length + 1);
853
854         memcpy(buffer, buf, length);
855
856         g_array_append_vals(in_param1, &fd, sizeof(int));
857         g_array_append_vals(in_param2, &length, sizeof(int));
858         g_array_append_vals(in_param3, buffer, length);
859
860         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
861                 in_param1, in_param2, in_param3, in_param4, &out_param);
862
863         BT_DBG("result: %x", result);
864
865         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
866
867         g_free(buffer);
868
869         return result;
870 #endif
871 }
872