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