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