RFCOMM socket : free connection info before sending callback
[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 TIZEN_FEATURE_BT_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_FEATURE_BT_DPM
32 #include "bt-dpm.h"
33 #endif
34
35 #ifdef TIZEN_FEATURE_BT_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         __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
273         if (info->rfcomm_conns == NULL)
274                 rfcomm_cb_data_remove(info);
275
276         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
277                         BLUETOOTH_ERROR_NONE, &disconn_info,
278                         event_info->cb, event_info->user_data);
279
280         BT_DBG("-");
281 }
282
283 static gboolean __rfcomm_client_disconnect(gpointer user_data)
284 {
285         rfcomm_cb_data_t *info = (rfcomm_cb_data_t *)user_data;
286
287         BT_INFO_C("### Disconnected [RFCOMM Client]");
288
289         retv_if(info == NULL, FALSE);
290
291         if (g_slist_find(rfcomm_clients, info) == NULL) {
292                 BT_INFO("rfcomm resource is already freed");
293                 return FALSE;
294         }
295         info->idle_id = 0;
296
297         g_slist_foreach(info->rfcomm_conns,
298                 (GFunc)_bt_rfcomm_disconnect_conn_info, info);
299
300         BT_DBG("-");
301         return FALSE;
302 }
303
304 static gboolean __is_error_by_disconnect(GError *err)
305 {
306         return !g_strcmp0(err->message, "Connection reset by peer") ||
307                         !g_strcmp0(err->message, "Connection timed out") ||
308                         !g_strcmp0(err->message, "Software caused connection abort");
309 }
310
311 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
312                                                                 gpointer data)
313 {
314         char *buffer = NULL;
315         gsize len = 0;
316         int result = BLUETOOTH_ERROR_NONE;
317         rfcomm_cb_data_t *info = data;
318         rfcomm_conn_info_t *conn_info = NULL;
319         bt_event_info_t *event_info;
320         bluetooth_rfcomm_received_data_t data_r;
321         GIOStatus status = G_IO_STATUS_NORMAL;
322         GError *err = NULL;
323         int fd;
324         BT_DBG("");
325
326         retv_if(info == NULL, FALSE);
327         fd = g_io_channel_unix_get_fd(chan);
328         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
329                 BT_ERR_C("RFComm Client  disconnected: %d", fd);
330
331                 conn_info = __get_conn_info_from_fd(info, fd);
332                 if (conn_info == NULL) {
333                         BT_ERR("No Connection info found with FD [%d]", fd);
334                         return FALSE;
335                 }
336
337                 if (conn_info->disconnected == FALSE) {
338                         close(conn_info->fd);
339                         conn_info->disconnected = TRUE;
340                 }
341                 __rfcomm_client_disconnect(info);
342                 return FALSE;
343         }
344
345         buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
346
347         status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
348                         &len, &err);
349         if (status != G_IO_STATUS_NORMAL) {
350                 BT_ERR("IO Channel read is failed with %d", status);
351
352                 g_free(buffer);
353                 if (err) {
354                         BT_ERR("IO Channel read error [%s]", err->message);
355                         if (status == G_IO_STATUS_ERROR &&
356                                         __is_error_by_disconnect(err)) {
357                                 BT_ERR("cond : %d", cond);
358                                 g_error_free(err);
359
360                                 conn_info = __get_conn_info_from_fd(info, fd);
361                                 if (conn_info == NULL) {
362                                         BT_ERR("No Connection info found with FD [%d]", fd);
363                                         return FALSE;
364                                 }
365
366                                 if (conn_info->disconnected == FALSE) {
367                                         close(conn_info->fd);
368                                         conn_info->disconnected = TRUE;
369                                 }
370                                 __rfcomm_client_disconnect(info);
371                                 return FALSE;
372                         }
373                         g_error_free(err);
374                 }
375                 return TRUE;
376         }
377
378         event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
379         if (event_info == NULL) {
380                 g_free(buffer);
381                 return TRUE;
382         }
383
384         data_r.socket_fd = fd;
385         data_r.buffer_size = len;
386         data_r.buffer = buffer;
387
388         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
389                         result, &data_r,
390                         event_info->cb, event_info->user_data);
391
392         g_free(buffer);
393         return TRUE;
394 }
395
396 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
397         int result)
398 {
399         bluetooth_rfcomm_connection_t conn_info;
400         bt_event_info_t *event_info;
401         rfcomm_conn_info_t *conn_list_info = NULL;
402
403         if (result == BLUETOOTH_ERROR_NONE)
404                 BT_INFO_C("### Connected [RFCOMM Client]");
405
406         event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
407         if (event_info == NULL)
408                 return;
409
410         memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
411         conn_info.device_role = RFCOMM_ROLE_CLIENT;
412         g_strlcpy(conn_info.uuid, cb_data->uuid, BLUETOOTH_UUID_STRING_MAX);
413         _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
414                         dev_address);
415         conn_list_info = __get_conn_info_from_address(cb_data, dev_address);
416         if (conn_list_info == NULL) {
417                 BT_ERR("Device addres %s not found in connection list", dev_address);
418                 return;
419         }
420         conn_info.socket_fd = conn_list_info->fd;
421         conn_info.server_id = -1;
422
423         BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
424                         result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
425         _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
426                         result, &conn_info,
427                         event_info->cb, event_info->user_data);
428         BT_DBG("-");
429 }
430
431 void _bt_rfcomm_client_disconnect_all(void)
432 {
433         GSList *client;
434         GSList *conn;
435
436         BT_INFO_C("### Disconnect all RFCOMM client connections");
437
438         for (client = rfcomm_clients; client; ) {
439                 rfcomm_cb_data_t *info = client->data;
440
441                 for (conn = info->rfcomm_conns; conn; conn = conn->next) {
442                         rfcomm_conn_info_t *conn_info = conn->data;
443
444                         if (conn_info == NULL)
445                                 continue;
446
447                         if (conn_info->watch_id == 0 || conn_info->disconnected)
448                                 continue;
449
450                         close(conn_info->fd);
451                         conn_info->disconnected = TRUE;
452
453                         _bt_disconnect_ext_profile(conn_info->bt_addr,
454                                                    info->obj_path);
455                 }
456
457                 client = client->next;
458                 __rfcomm_client_disconnect(info);
459         }
460
461         return;
462 }
463 #endif
464
465 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
466 {
467         rfcomm_cb_data_t *info;
468         GIOChannel *data_io;
469         rfcomm_conn_info_t *conn_info = NULL;
470         char address[BT_ADDRESS_STRING_SIZE];
471
472         BT_INFO("%s %d", path, fd);
473
474         _bt_convert_addr_type_to_string(address,
475                                 (unsigned char *)addr);
476
477         info = __find_rfcomm_info_from_path(path);
478         if (info == NULL) {
479                 BT_ERR("rfcomm info is NULL");
480                 return -1;
481         }
482
483         conn_info = __get_conn_info_from_address(info, address);
484         if (conn_info == NULL) {
485                 BT_ERR("connection info is NULL");
486                 return -1;
487         }
488
489         conn_info->fd = fd;
490
491         data_io = g_io_channel_unix_new(fd);
492
493         g_io_channel_set_encoding(data_io, NULL, NULL);
494         g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
495
496         conn_info->watch_id = g_io_add_watch(data_io,
497                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
498                                 __client_data_received_cb, info);
499
500         g_io_channel_unref(data_io);
501
502         __client_connected_cb(info, address, BLUETOOTH_ERROR_NONE);
503
504         return 0;
505 }
506
507 static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
508                                                         gpointer user_data)
509
510 {
511         GError *error = NULL;
512         GVariant *value;
513         rfcomm_cb_data_t *cb_data;
514         char dev_address[BT_ADDRESS_STRING_SIZE];
515         const char *path;
516         BT_DBG("+");
517
518         ret_if(user_data == NULL);
519
520         cb_data = user_data;
521
522         value = g_dbus_proxy_call_finish(proxy, res, &error);
523         if (value == NULL) {
524                 int result;
525                 g_dbus_error_strip_remote_error(error);
526                 BT_ERR("Error : %s \n", error->message);
527
528                 if (g_strcmp0(error->message, "In Progress") == 0)
529                         result = BLUETOOTH_ERROR_DEVICE_BUSY;
530                 else
531                         result = BLUETOOTH_ERROR_INTERNAL;
532                 path = g_dbus_proxy_get_object_path(proxy);
533                 _bt_convert_device_path_to_address(path, dev_address);
534                 __rfcomm_client_connected_cb(cb_data, dev_address, result);
535
536                 g_error_free(error);
537         } else {
538                 g_variant_unref(value);
539         }
540
541         if (proxy)
542                 g_object_unref(proxy);
543
544         BT_DBG("-");
545 }
546
547 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
548                                 GAsyncResult *res, gpointer user_data)
549 {
550         rfcomm_cb_data_t *cb_data;
551         int ret = 0;
552         GError *err = NULL;
553         GVariant *value;
554         bt_register_profile_info_t info = {0};
555         int result = BLUETOOTH_ERROR_NONE;
556         char dev_address[BT_ADDRESS_STRING_SIZE];
557         const char *path;
558
559         BT_DBG("+");
560
561         ret_if(user_data == NULL);
562
563         cb_data = user_data;
564
565         path = g_dbus_proxy_get_object_path(proxy);
566
567         _bt_convert_device_path_to_address(path, dev_address);
568         BT_DBG("Device Adress [%s]", dev_address);
569         value = g_dbus_proxy_call_finish(proxy, res, &err);
570         if (proxy)
571                 g_object_unref(proxy);
572         if (value)
573                 g_variant_unref(value);
574
575         if (err != NULL) {
576                 g_dbus_error_strip_remote_error(err);
577                 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
578                 if (!strcmp("Operation canceled", err->message)) {
579                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
580                 } else if (!strcmp("In Progress", err->message)) {
581                         result = BLUETOOTH_ERROR_IN_PROGRESS;
582                 } else if (!strcmp("Host is down", err->message)) {
583                         result = BLUETOOTH_ERROR_HOST_DOWN;
584                 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
585                         result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
586                         ret = _bt_cancel_discovers(dev_address);
587                         if (ret != BLUETOOTH_ERROR_NONE)
588                                 BT_ERR("Error: While CancelDiscovery");
589                 } else {
590                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
591                 }
592                 __rfcomm_client_connected_cb(cb_data, dev_address, result);
593                 goto done;
594         } else {
595                 BT_INFO("Services are Updated checking required uuid is there");
596                 /* Check here for uuid present */
597                 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
598                 if (ret == BLUETOOTH_ERROR_NONE) {
599                         info.uuid = (char *)cb_data->uuid;
600                         info.obj_path = cb_data->obj_path;
601                         info.role = "client";
602
603                         ret = _bt_register_profile(&info, FALSE);
604                         if (ret < 0)
605                                 BT_DBG("Error: register profile");
606                         ret = _bt_connect_profile(dev_address, cb_data->uuid,
607                                                 __bt_connect_response_cb, cb_data);
608
609                         if (ret != BLUETOOTH_ERROR_NONE) {
610                                 BT_ERR("ConnectProfile failed");
611                                 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
612                                 __rfcomm_client_connected_cb(cb_data, dev_address, result);
613                                 goto done;
614                         }
615                 } else {
616                         BT_ERR("remote uuid not found");
617                         result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
618                         __rfcomm_client_connected_cb(cb_data, dev_address, result);
619                 }
620         }
621 done:
622         if (err)
623                 g_clear_error(&err);
624 }
625
626 BT_EXPORT_API int bluetooth_rfcomm_connect(
627                 const bluetooth_device_address_t *remote_bt_address,
628                 const char *remote_uuid)
629 {
630
631 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
632         rfcomm_cb_data_t *cb_data = NULL;
633         rfcomm_conn_info_t *conn = NULL;
634 #else
635         int result;
636         int connect_type;
637         bt_user_info_t *user_info;
638         char uuid[BLUETOOTH_UUID_STRING_MAX];
639 #endif
640         BT_CHECK_PARAMETER(remote_bt_address, return);
641         BT_CHECK_PARAMETER(remote_uuid, return);
642         BT_CHECK_ENABLED(return);
643
644 #ifdef TIZEN_FEATURE_BT_DPM
645         if (_bt_check_dpm(BT_DPM_ADDRESS, (void *)remote_bt_address) == BT_DPM_RESTRICTED) {
646                 BT_ERR("Blacklist device");
647                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
648         }
649
650         if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED ||
651                 _bt_check_dpm(BT_DPM_HF_ONLY, NULL) == BT_DPM_RESTRICTED) {
652                 BT_ERR("Not allow to connect the RFCOMM service");
653                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
654         }
655
656         if (_bt_check_dpm(BT_DPM_DESKTOP, NULL) == BT_DPM_RESTRICTED) {
657                 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
658                 bluetooth_device_class_t dev_class;
659
660                 _bt_convert_addr_type_to_string(address, (unsigned char *)remote_bt_address->addr);
661                 _bt_get_cod_by_address(address, &dev_class);
662
663                 if (dev_class.major_class == BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
664                         BT_ERR("Reject a authorization due to MDM Policy");
665                         return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
666                 }
667         }
668 #endif
669
670 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
671         BT_INFO_C("### Connect RFCOMM");
672         int ret;
673         int id, object_id;
674         char *path;
675
676         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
677              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
678                 BT_ERR("Don't have a privilege to use this API");
679                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
680         }
681
682         id = __rfcomm_assign_id();
683         if (id < 0)
684                 return BLUETOOTH_ERROR_INTERNAL;
685
686         cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
687         if (!cb_data) {
688                 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
689
690                 object_id = _bt_register_new_conn(path, new_connection);
691                 if (object_id < 0) {
692                         __rfcomm_delete_id(id);
693                         g_free(path);
694                         return BLUETOOTH_ERROR_INTERNAL;
695                 }
696
697                 cb_data = g_new0(rfcomm_cb_data_t, 1);
698                 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
699                 cb_data->obj_path = path;
700                 cb_data->object_id = object_id;
701                 cb_data->id = id;
702         }
703
704         conn = g_new0(rfcomm_conn_info_t, 1);
705         conn->fd = -1;
706         _bt_convert_addr_type_to_string(conn->bt_addr,
707                                 (unsigned char *)remote_bt_address->addr);
708
709         BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
710         cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
711
712         ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
713                                 __bt_discover_service_response_cb, cb_data);
714         if (ret != BLUETOOTH_ERROR_NONE) {
715                 BT_ERR("Error returned while service discovery");
716                 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
717                 if (cb_data->rfcomm_conns == NULL)
718                         rfcomm_cb_data_remove(cb_data);
719                 return BLUETOOTH_ERROR_INTERNAL;
720         }
721
722         if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
723                 BT_INFO("Adding callback information to rfcomm_clients");
724                 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
725         } else
726                 BT_INFO("Callback information is already added");
727
728         return BLUETOOTH_ERROR_NONE;
729 #else
730         user_info = _bt_get_user_data(BT_COMMON);
731         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
732
733         /* connect_type:  BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
734         /* In now, we only support to connecty using UUID */
735         connect_type = BT_RFCOMM_UUID;
736
737         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
738              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
739                 BT_ERR("Don't have a privilege to use this API");
740                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
741         }
742
743         BT_INIT_PARAMS();
744         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
745
746         g_array_append_vals(in_param1, remote_bt_address,
747                                 sizeof(bluetooth_device_address_t));
748
749         g_strlcpy(uuid, remote_uuid, sizeof(uuid));
750         g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
751
752         g_array_append_vals(in_param3, &connect_type, sizeof(int));
753
754         result = _bt_send_request_async(BT_BLUEZ_SERVICE,
755                                 BT_RFCOMM_CLIENT_CONNECT,
756                                 in_param1, in_param2,
757                                 in_param3, in_param4,
758                                 user_info->cb, user_info->user_data);
759
760         BT_DBG("result: %x", result);
761
762         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
763
764         return result;
765 #endif
766 }
767
768 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
769 {
770         GSList *l;
771         GSList *conn_list = NULL;
772         rfcomm_cb_data_t *client_info;
773         rfcomm_conn_info_t *conn_info;
774         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
775
776         BT_CHECK_PARAMETER(device_address, return);
777         BT_CHECK_PARAMETER(connected, return);
778
779         _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
780         *connected = FALSE;
781
782         for (l = rfcomm_clients; l != NULL; l = l->next) {
783                 client_info = l->data;
784                 if (client_info == NULL)
785                         continue;
786                 for (conn_list = client_info->rfcomm_conns;
787                         conn_list != NULL; conn_list = conn_list->next) {
788                         conn_info = conn_list->data;
789                         if (conn_info == NULL)
790                                 continue;
791
792                         if (g_strcmp0(address, conn_info->bt_addr) == 0) {
793                                 *connected = TRUE;
794                                 return BLUETOOTH_ERROR_NONE;
795                         }
796                 }
797         }
798
799         return BLUETOOTH_ERROR_NONE;
800 }
801
802 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
803 {
804         int result;
805         int connected = FALSE;
806
807         BT_CHECK_ENABLED(return);
808
809         BT_INIT_PARAMS();
810         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
811
812         result = _bt_send_request(BT_BLUEZ_SERVICE,
813                         BT_RFCOMM_CLIENT_IS_CONNECTED,
814                         in_param1, in_param2, in_param3,
815                         in_param4, &out_param);
816
817         BT_DBG("result: %x", result);
818
819         if (result == BLUETOOTH_ERROR_NONE) {
820                 connected = g_array_index(out_param,
821                                 int, 0);
822         } else {
823                 BT_ERR("Fail to send request");
824         }
825
826         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
827
828         return connected;
829 }
830
831 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
832 {
833 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
834         rfcomm_cb_data_t *info;
835         rfcomm_conn_info_t *conn_info;
836
837         BT_INFO_C("### Disconnect RFCOMM");
838
839         BT_CHECK_ENABLED(return);
840
841         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT)
842              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
843                 BT_ERR("Don't have a privilege to use this API");
844                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
845         }
846
847         BT_DBG("Requested FD %d", socket_fd);
848         if (socket_fd < 0) {
849                 BT_ERR("Invalid FD");
850                 return BLUETOOTH_ERROR_INVALID_PARAM;
851         }
852         BT_DBG("FDD %d", socket_fd);
853
854         info = __find_rfcomm_info_with_fd(socket_fd);
855         if (info == NULL) {
856                 BT_DBG("Could not find in client, so check in server");
857                 return bluetooth_rfcomm_server_disconnect(socket_fd);
858         }
859
860         conn_info = __get_conn_info_from_fd(info, socket_fd);
861         if (conn_info == NULL) {
862                 BT_ERR("Could not find connection info");
863                 return BLUETOOTH_ERROR_INTERNAL;
864         }
865
866         if (conn_info->watch_id == 0 || conn_info->disconnected) {
867                 BT_ERR("Invalid state");
868                 return BLUETOOTH_ERROR_NOT_CONNECTED;
869         }
870
871         close(conn_info->fd);
872         conn_info->disconnected = TRUE;
873
874         BT_INFO("conn_info %s", conn_info->bt_addr);
875         _bt_disconnect_ext_profile(conn_info->bt_addr, info->obj_path);
876
877         if (info->idle_id == 0)
878                 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
879
880         return BLUETOOTH_ERROR_NONE;
881 #else
882         int result;
883
884         BT_CHECK_ENABLED(return);
885
886         BT_INIT_PARAMS();
887         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
888
889         /* Support the OSP */
890         if (socket_fd == -1) {
891                 /* Cancel connect */
892                 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CANCEL_CONNECT,
893                         in_param1, in_param2, in_param3, in_param4, &out_param);
894         } else {
895                 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
896                 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT,
897                         in_param1, in_param2, in_param3, in_param4, &out_param);
898         }
899
900         BT_DBG("result: %x", result);
901
902         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
903
904         return result;
905 #endif
906 }
907
908 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
909 {
910 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
911         int written;
912 #else
913         char *buffer;
914 #endif
915         int result;
916
917         BT_CHECK_PARAMETER(buf, return);
918         if (fd < 0) {
919                 BT_ERR("Invalid FD");
920                 return BLUETOOTH_ERROR_INVALID_PARAM;
921         }
922
923 #ifndef TIZEN_FEATURE_BT_RFCOMM_DIRECT
924         BT_CHECK_ENABLED(return);
925 #endif
926         retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
927
928 #ifdef TIZEN_FEATURE_BT_DPM
929         if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED ||
930                 _bt_check_dpm(BT_DPM_HF_ONLY, NULL) == BT_DPM_RESTRICTED) {
931                 BT_ERR("Not allow to write RFCOMM data");
932                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
933         }
934 #endif
935
936 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
937         switch (privilege_token) {
938         case 0:
939                 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE);
940
941                 if (result == BLUETOOTH_ERROR_NONE) {
942                         privilege_token = 1; /* Have a permission */
943                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
944                         BT_ERR("Don't have a privilege to use this API");
945                         privilege_token = -1; /* Don't have a permission */
946                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
947                 } else {
948                         /* Just break - It is not related with permission error */
949                 }
950                 break;
951         case 1:
952                 /* Already have a privilege */
953                 break;
954         case -1:
955                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
956         default:
957                 /* Invalid privilge token value */
958                 return BLUETOOTH_ERROR_INTERNAL;
959         }
960
961         written = write(fd, buf, length);
962         /*BT_DBG("Length %d, written = %d, balance(%d)",
963                          length, written, length - written); */
964         return written;
965 #else
966         BT_INIT_PARAMS();
967         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
968
969         buffer = g_malloc0(length + 1);
970
971         memcpy(buffer, buf, length);
972
973         g_array_append_vals(in_param1, &fd, sizeof(int));
974         g_array_append_vals(in_param2, &length, sizeof(int));
975         g_array_append_vals(in_param3, buffer, length);
976
977         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
978                 in_param1, in_param2, in_param3, in_param4, &out_param);
979
980         BT_DBG("result: %x", result);
981
982         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
983
984         g_free(buffer);
985
986         return result;
987 #endif
988 }
989