76817048d8fa31414ed9c4dd1d03c5252fcc800c
[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                 g_dbus_error_strip_remote_error(error);
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                 g_dbus_error_strip_remote_error(err);
508                 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
509                 if (!strcmp("Operation canceled", err->message)) {
510                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
511                 } else if (!strcmp("In Progress", err->message)) {
512                         result = BLUETOOTH_ERROR_IN_PROGRESS;
513                 } else if (!strcmp("Host is down", err->message)) {
514                         result = BLUETOOTH_ERROR_HOST_DOWN;
515                 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
516                         result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
517                         ret = _bt_cancel_discovers(dev_address);
518                         if (ret != BLUETOOTH_ERROR_NONE)
519                                 BT_ERR("Error: While CancelDiscovery");
520                 } else {
521                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
522                 }
523                 __rfcomm_client_connected_cb(cb_data, dev_address, result);
524                 goto done;
525         } else {
526                 BT_INFO("Services are Updated checking required uuid is there");
527                 /* Check here for uuid present */
528                 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
529                 if (ret == BLUETOOTH_ERROR_NONE) {
530                         info.uuid = (char *)cb_data->uuid;
531                         info.obj_path = cb_data->obj_path;
532                         info.role = "client";
533
534                         ret = _bt_register_profile(&info, FALSE);
535                         if (ret < 0)
536                                 BT_DBG("Error: register profile");
537                         ret = _bt_connect_profile(dev_address, cb_data->uuid,
538                                                 __bt_connect_response_cb, cb_data);
539
540                         if (ret != BLUETOOTH_ERROR_NONE) {
541                                 BT_ERR("ConnectProfile failed");
542                                 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
543                                 __rfcomm_client_connected_cb(cb_data, dev_address, result);
544                                 goto done;
545                         }
546                 } else {
547                         BT_ERR("remote uuid not found");
548                         result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
549                         __rfcomm_client_connected_cb(cb_data, dev_address, result);
550                 }
551         }
552 done:
553         if (err)
554                 g_clear_error(&err);
555 }
556
557 BT_EXPORT_API int bluetooth_rfcomm_connect(
558                 const bluetooth_device_address_t *remote_bt_address,
559                 const char *remote_uuid)
560 {
561
562 #ifdef RFCOMM_DIRECT
563         rfcomm_cb_data_t *cb_data = NULL;
564         rfcomm_conn_info_t *conn = NULL;
565 #else
566         int result;
567         int connect_type;
568         bt_user_info_t *user_info;
569         char uuid[BLUETOOTH_UUID_STRING_MAX];
570 #endif
571         BT_CHECK_PARAMETER(remote_bt_address, return);
572         BT_CHECK_PARAMETER(remote_uuid, return);
573         BT_CHECK_ENABLED(return);
574
575 #ifdef RFCOMM_DIRECT
576         BT_INFO_C("<<<<<<<<< RFCOMM Connect request from app >>>>>>>>>>>");
577         int ret;
578         int id, object_id;
579         char *path;
580
581         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
582              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
583                 BT_ERR("Don't have a privilege to use this API");
584                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
585         }
586
587         id = __rfcomm_assign_id();
588         if (id < 0)
589                 return BLUETOOTH_ERROR_INTERNAL;
590
591         cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
592         if (!cb_data) {
593                 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
594
595                 object_id = _bt_register_new_conn(path, new_connection);
596                 if (object_id < 0) {
597                         __rfcomm_delete_id(id);
598                         return BLUETOOTH_ERROR_INTERNAL;
599                 }
600
601                 cb_data = g_new0(rfcomm_cb_data_t, 1);
602                 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
603                 cb_data->obj_path = path;
604                 cb_data->object_id = object_id;
605                 cb_data->id = id;
606         }
607         conn = g_new0(rfcomm_conn_info_t, 1);
608         conn->fd = -1;
609         _bt_convert_addr_type_to_string(conn->bt_addr,
610                                 (unsigned char *)remote_bt_address->addr);
611
612         BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
613         cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
614         ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
615                                 __bt_discover_service_response_cb, cb_data);
616         if (ret != BLUETOOTH_ERROR_NONE) {
617                 BT_ERR("Error returned while service discovery");
618                 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
619                 if (cb_data->rfcomm_conns == NULL)
620                         rfcomm_cb_data_remove(cb_data);
621                 return BLUETOOTH_ERROR_INTERNAL;
622         }
623         if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
624                 BT_INFO("Adding callback information to rfcomm_clients");
625                 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
626         } else
627                 BT_INFO("Callback information is already added");
628
629         return BLUETOOTH_ERROR_NONE;
630 #else
631         user_info = _bt_get_user_data(BT_COMMON);
632         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
633
634         /* connect_type:  BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
635         /* In now, we only support to connecty using UUID */
636         connect_type = BT_RFCOMM_UUID;
637
638         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
639              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
640                 BT_ERR("Don't have a privilege to use this API");
641                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
642         }
643
644         BT_INIT_PARAMS();
645         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
646
647         g_array_append_vals(in_param1, remote_bt_address,
648                                 sizeof(bluetooth_device_address_t));
649
650         g_strlcpy(uuid, remote_uuid, sizeof(uuid));
651         g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
652
653         g_array_append_vals(in_param3, &connect_type, sizeof(int));
654
655         result = _bt_send_request_async(BT_BLUEZ_SERVICE,
656                                 BT_RFCOMM_CLIENT_CONNECT,
657                                 in_param1, in_param2,
658                                 in_param3, in_param4,
659                                 user_info->cb, user_info->user_data);
660
661         BT_DBG("result: %x", result);
662
663         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
664
665         return result;
666 #endif
667 }
668
669 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
670 {
671         GSList *l;
672         GSList *conn_list = NULL;
673         rfcomm_cb_data_t *client_info;
674         rfcomm_conn_info_t *conn_info;
675         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
676
677         BT_CHECK_PARAMETER(device_address, return);
678         BT_CHECK_PARAMETER(connected, return);
679
680         _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
681         *connected = FALSE;
682
683         for (l = rfcomm_clients; l != NULL; l = l->next) {
684                 client_info = l->data;
685                 if (client_info == NULL)
686                         continue;
687                 for(conn_list = client_info->rfcomm_conns;
688                         conn_list != NULL; conn_list = conn_list->next) {
689                         conn_info = conn_list->data;
690                         if(conn_info == NULL)
691                                 continue;
692
693                         if (g_strcmp0(address, conn_info->bt_addr) == 0) {
694                                 *connected = TRUE;
695                                 return BLUETOOTH_ERROR_NONE;
696                         }
697                 }
698         }
699
700         return BLUETOOTH_ERROR_NONE;
701 }
702
703 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
704 {
705         int result;
706         int connected = FALSE;
707
708         BT_CHECK_ENABLED(return);
709
710         BT_INIT_PARAMS();
711         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
712
713         result = _bt_send_request(BT_BLUEZ_SERVICE,
714                         BT_RFCOMM_CLIENT_IS_CONNECTED,
715                         in_param1, in_param2, in_param3,
716                         in_param4, &out_param);
717
718         BT_DBG("result: %x", result);
719
720         if (result == BLUETOOTH_ERROR_NONE) {
721                 connected = g_array_index(out_param,
722                                 int, 0);
723         } else {
724                 BT_ERR("Fail to send request");
725         }
726
727         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
728
729         return connected;
730 }
731
732 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
733 {
734 #ifdef RFCOMM_DIRECT
735         rfcomm_cb_data_t *info;
736         rfcomm_conn_info_t *conn_info;
737         BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
738         BT_CHECK_ENABLED(return);
739
740         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT)
741              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
742                 BT_ERR("Don't have a privilege to use this API");
743                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
744         }
745
746         if (socket_fd < 0) {
747                 BT_ERR("Invalid FD");
748                 return BLUETOOTH_ERROR_INVALID_PARAM;
749         }
750         BT_DBG("FDD %d", socket_fd);
751
752         info = __find_rfcomm_info_with_fd(socket_fd);
753         if (info == NULL) {
754                 BT_DBG("Could not find in client, so check in server");
755                 return bluetooth_rfcomm_server_disconnect(socket_fd);
756         }
757         conn_info = __get_conn_info_from_fd(info, socket_fd);
758         if (conn_info == NULL) {
759                 BT_ERR("FATAL Error");
760                 return BLUETOOTH_ERROR_INTERNAL;
761         }
762         if (conn_info->watch_id <= 0) {
763                 BT_ERR("Invalid state");
764                 return BLUETOOTH_ERROR_NOT_CONNECTED;
765         }
766         conn_info->disconnected = TRUE;
767         close(socket_fd);
768         BT_INFO("conn_info %s", conn_info->bt_addr);
769         _bt_disconnect_profile(conn_info->bt_addr, info->uuid, NULL,NULL);
770         if (info->idle_id == 0)
771                 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
772
773         return BLUETOOTH_ERROR_NONE;
774 #else
775         int result;
776         int service_function;
777
778         BT_CHECK_ENABLED(return);
779
780         BT_INIT_PARAMS();
781         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
782
783         /* Support the OSP */
784         if (socket_fd == -1) {
785                 /* Cancel connect */
786                 service_function = BT_RFCOMM_CLIENT_CANCEL_CONNECT;
787         } else {
788                 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
789                 service_function = BT_RFCOMM_SOCKET_DISCONNECT;
790         }
791
792         result = _bt_send_request(BT_BLUEZ_SERVICE, service_function,
793                 in_param1, in_param2, in_param3, in_param4, &out_param);
794
795         BT_DBG("result: %x", result);
796
797         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
798
799         return result;
800 #endif
801 }
802
803 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
804 {
805 #ifdef RFCOMM_DIRECT
806         int written;
807 #else
808         char *buffer;
809 #endif
810         int result;
811
812         BT_CHECK_PARAMETER(buf, return);
813         if (fd < 0) {
814                 BT_ERR("Invalid FD");
815                 return BLUETOOTH_ERROR_INVALID_PARAM;
816         }
817
818         BT_DBG("FD : %d", fd);
819
820 #ifndef RFCOMM_DIRECT
821         BT_CHECK_ENABLED(return);
822 #endif
823         retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
824
825 #ifdef RFCOMM_DIRECT
826         switch (privilege_token) {
827         case 0:
828                 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE);
829
830                 if (result == BLUETOOTH_ERROR_NONE) {
831                         privilege_token = 1; /* Have a permission */
832                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
833                         BT_ERR("Don't have a privilege to use this API");
834                         privilege_token = -1; /* Don't have a permission */
835                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
836                 } else {
837                         /* Just break - It is not related with permission error */
838                 }
839                 break;
840         case 1:
841                 /* Already have a privilege */
842                 break;
843         case -1:
844                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
845         default:
846                 /* Invalid privilge token value */
847                 return BLUETOOTH_ERROR_INTERNAL;
848         }
849
850         written = write(fd, buf, length);
851         /*BT_DBG("Length %d, written = %d, balance(%d)",
852                          length, written, length - written); */
853         return written;
854 #else
855         BT_INIT_PARAMS();
856         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
857
858         buffer = g_malloc0(length + 1);
859
860         memcpy(buffer, buf, length);
861
862         g_array_append_vals(in_param1, &fd, sizeof(int));
863         g_array_append_vals(in_param2, &length, sizeof(int));
864         g_array_append_vals(in_param3, buffer, length);
865
866         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
867                 in_param1, in_param2, in_param3, in_param4, &out_param);
868
869         BT_DBG("result: %x", result);
870
871         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
872
873         g_free(buffer);
874
875         return result;
876 #endif
877 }
878