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