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