2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
21 #include <gio/gunixfdlist.h>
24 #include "bluetooth-api.h"
25 #include "bt-internal-types.h"
27 #include "bt-common.h"
28 #include "bt-request-sender.h"
29 #include "bt-event-handler.h"
31 #ifdef TIZEN_FEATURE_BT_DPM
35 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
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 " \
43 static GSList *rfcomm_clients;
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
51 static int privilege_token;
54 char bt_addr[BT_ADDRESS_STRING_SIZE];
57 gboolean disconnected;
61 char uuid[BLUETOOTH_UUID_STRING_MAX];
70 static void __client_connected_cb(rfcomm_cb_data_t *cb_data,
71 char *dev_address, int result);
73 static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
78 __rfcomm_delete_id(cb_data->id);
80 if (cb_data->object_id > 0)
81 _bt_unregister_gdbus(cb_data->object_id);
83 if (cb_data->obj_path) {
84 BT_INFO("Unregister profile");
85 _bt_unregister_profile(cb_data->obj_path);
88 if (cb_data->idle_id != 0) {
89 BT_INFO("Removing idle source");
90 g_source_remove(cb_data->idle_id);
93 g_free(cb_data->obj_path);
95 g_free(cb_data->device_path);
100 static void rfcomm_cb_data_remove(rfcomm_cb_data_t *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);
109 gint compare(gpointer *a, gpointer *b)
111 rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
112 char *address = (char *)b;
113 return g_strcmp0(node->bt_addr, address);
116 gint compare_fd(gpointer *a, gpointer *b)
118 rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
125 static void __bt_free_conn(rfcomm_conn_info_t *conn)
132 if (conn->watch_id > 0) {
133 g_source_remove(conn->watch_id);
142 static void __rfcomm_remove_conn_info_t(rfcomm_cb_data_t *info, char *address)
145 rfcomm_conn_info_t *conn_info = NULL;
146 l = g_slist_find_custom(info->rfcomm_conns, address, (GCompareFunc)compare);
150 info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn_info);
151 __bt_free_conn(conn_info);
155 static rfcomm_conn_info_t *__get_conn_info_from_fd(rfcomm_cb_data_t *info,
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)
168 static rfcomm_conn_info_t *__get_conn_info_from_address(rfcomm_cb_data_t *info,
172 rfcomm_conn_info_t *conn_info = NULL;
173 l = g_slist_find_custom(info->rfcomm_conns, dev_address,
174 (GCompareFunc)compare);
180 static void __rfcomm_client_connected_cb(rfcomm_cb_data_t *info,
181 char *dev_address, int result)
183 if (g_slist_find(rfcomm_clients, info) == NULL) {
184 BT_INFO("rfcomm resource is already freed");
188 __client_connected_cb(info, dev_address, result);
189 __rfcomm_remove_conn_info_t(info, dev_address);
191 if (info->rfcomm_conns == NULL)
192 rfcomm_cb_data_remove(info);
195 static rfcomm_cb_data_t *__find_rfcomm_info_with_fd(int 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);
210 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
214 for (l = rfcomm_clients; l != NULL; l = l->next) {
215 rfcomm_cb_data_t *info = l->data;
218 if (g_strcmp0(info->obj_path, path) == 0)
225 static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
229 for (l = rfcomm_clients; l != NULL; l = l->next) {
230 rfcomm_cb_data_t *info = l->data;
232 if (g_strcmp0(info->uuid, uuid) == 0)
239 static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
240 rfcomm_cb_data_t *info)
242 if (conn_info == NULL)
245 bluetooth_rfcomm_disconnection_t disconn_info;
246 bt_event_info_t *event_info = NULL;
248 if (conn_info->disconnected == FALSE)
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);
255 if (info->rfcomm_conns == NULL)
256 rfcomm_cb_data_remove(info);
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,
266 BT_DBG("Disconnected FD [%d]", conn_info->fd);
267 disconn_info.socket_fd = conn_info->fd;
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);
276 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
278 if (info->rfcomm_conns == NULL)
279 rfcomm_cb_data_remove(info);
284 static gboolean __rfcomm_client_disconnect(gpointer user_data)
286 rfcomm_cb_data_t *info = (rfcomm_cb_data_t *)user_data;
288 BT_INFO_C("### Disconnected [RFCOMM Client]");
290 retv_if(info == NULL, FALSE);
292 if (g_slist_find(rfcomm_clients, info) == NULL) {
293 BT_INFO("rfcomm resource is already freed");
298 g_slist_foreach(info->rfcomm_conns,
299 (GFunc)_bt_rfcomm_disconnect_conn_info, info);
305 static gboolean __is_error_by_disconnect(GError *err)
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");
312 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
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;
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);
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);
338 if (conn_info->disconnected == FALSE) {
339 close(conn_info->fd);
340 conn_info->disconnected = TRUE;
342 __rfcomm_client_disconnect(info);
346 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
348 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
350 if (status != G_IO_STATUS_NORMAL) {
351 BT_ERR("IO Channel read is failed with %d", status);
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);
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);
367 if (conn_info->disconnected == FALSE) {
368 close(conn_info->fd);
369 conn_info->disconnected = TRUE;
371 __rfcomm_client_disconnect(info);
379 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
380 if (event_info == NULL) {
385 data_r.socket_fd = fd;
386 data_r.buffer_size = len;
387 data_r.buffer = buffer;
389 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
391 event_info->cb, event_info->user_data);
397 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
400 bluetooth_rfcomm_connection_t conn_info;
401 bt_event_info_t *event_info;
402 rfcomm_conn_info_t *conn_list_info = NULL;
404 if (result == BLUETOOTH_ERROR_NONE)
405 BT_INFO_C("### Connected [RFCOMM Client]");
407 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
408 if (event_info == NULL)
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,
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);
421 conn_info.socket_fd = conn_list_info->fd;
422 conn_info.server_id = -1;
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,
428 event_info->cb, event_info->user_data);
432 void _bt_rfcomm_client_disconnect_all(void)
437 BT_INFO_C("### Disconnect all RFCOMM client connections");
439 for (client = rfcomm_clients; client; ) {
440 rfcomm_cb_data_t *info = client->data;
442 for (conn = info->rfcomm_conns; conn; conn = conn->next) {
443 rfcomm_conn_info_t *conn_info = conn->data;
445 if (conn_info == NULL)
448 if (conn_info->watch_id == 0 || conn_info->disconnected)
451 close(conn_info->fd);
452 conn_info->disconnected = TRUE;
454 _bt_disconnect_ext_profile(conn_info->bt_addr,
458 client = client->next;
459 __rfcomm_client_disconnect(info);
466 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
468 rfcomm_cb_data_t *info;
470 rfcomm_conn_info_t *conn_info = NULL;
471 char address[BT_ADDRESS_STRING_SIZE];
473 BT_INFO("%s %d", path, fd);
475 _bt_convert_addr_type_to_string(address,
476 (unsigned char *)addr);
478 info = __find_rfcomm_info_from_path(path);
480 BT_ERR("rfcomm info is NULL");
484 conn_info = __get_conn_info_from_address(info, address);
485 if (conn_info == NULL) {
486 BT_ERR("connection info is NULL");
492 data_io = g_io_channel_unix_new(fd);
494 g_io_channel_set_encoding(data_io, NULL, NULL);
495 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
497 conn_info->watch_id = g_io_add_watch(data_io,
498 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
499 __client_data_received_cb, info);
501 g_io_channel_unref(data_io);
503 __client_connected_cb(info, address, BLUETOOTH_ERROR_NONE);
508 static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
512 GError *error = NULL;
514 rfcomm_cb_data_t *cb_data;
515 char dev_address[BT_ADDRESS_STRING_SIZE];
519 ret_if(user_data == NULL);
523 value = g_dbus_proxy_call_finish(proxy, res, &error);
526 g_dbus_error_strip_remote_error(error);
527 BT_ERR("Error : %s \n", error->message);
529 if (g_strcmp0(error->message, "In Progress") == 0)
530 result = BLUETOOTH_ERROR_DEVICE_BUSY;
532 result = BLUETOOTH_ERROR_INTERNAL;
533 path = g_dbus_proxy_get_object_path(proxy);
534 _bt_convert_device_path_to_address(path, dev_address);
535 __rfcomm_client_connected_cb(cb_data, dev_address, result);
539 g_variant_unref(value);
543 g_object_unref(proxy);
548 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
549 GAsyncResult *res, gpointer user_data)
551 rfcomm_cb_data_t *cb_data;
555 bt_register_profile_info_t info = {0};
556 int result = BLUETOOTH_ERROR_NONE;
557 char dev_address[BT_ADDRESS_STRING_SIZE];
562 ret_if(user_data == NULL);
566 path = g_dbus_proxy_get_object_path(proxy);
568 _bt_convert_device_path_to_address(path, dev_address);
569 BT_DBG("Device Adress [%s]", dev_address);
570 value = g_dbus_proxy_call_finish(proxy, res, &err);
572 g_object_unref(proxy);
574 g_variant_unref(value);
577 g_dbus_error_strip_remote_error(err);
578 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
579 if (!strcmp("Operation canceled", err->message)) {
580 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
581 } else if (!strcmp("In Progress", err->message)) {
582 result = BLUETOOTH_ERROR_IN_PROGRESS;
583 } else if (!strcmp("Host is down", err->message)) {
584 result = BLUETOOTH_ERROR_HOST_DOWN;
585 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
586 result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
587 ret = _bt_cancel_discovers(dev_address);
588 if (ret != BLUETOOTH_ERROR_NONE)
589 BT_ERR("Error: While CancelDiscovery");
591 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
593 __rfcomm_client_connected_cb(cb_data, dev_address, result);
596 BT_INFO("Services are Updated checking required uuid is there");
597 /* Check here for uuid present */
598 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
599 if (ret == BLUETOOTH_ERROR_NONE) {
600 info.uuid = (char *)cb_data->uuid;
601 info.obj_path = cb_data->obj_path;
602 info.role = "client";
604 ret = _bt_register_profile(&info, FALSE);
606 BT_DBG("Error: register profile");
607 ret = _bt_connect_profile(dev_address, cb_data->uuid,
608 __bt_connect_response_cb, cb_data);
610 if (ret != BLUETOOTH_ERROR_NONE) {
611 BT_ERR("ConnectProfile failed");
612 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
613 __rfcomm_client_connected_cb(cb_data, dev_address, result);
617 BT_ERR("remote uuid not found");
618 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
619 __rfcomm_client_connected_cb(cb_data, dev_address, result);
627 BT_EXPORT_API int bluetooth_rfcomm_connect(
628 const bluetooth_device_address_t *remote_bt_address,
629 const char *remote_uuid)
632 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
633 rfcomm_cb_data_t *cb_data = NULL;
634 rfcomm_conn_info_t *conn = NULL;
638 bt_user_info_t *user_info;
639 char uuid[BLUETOOTH_UUID_STRING_MAX];
641 BT_CHECK_PARAMETER(remote_bt_address, return);
642 BT_CHECK_PARAMETER(remote_uuid, return);
643 BT_CHECK_ENABLED(return);
645 #ifdef TIZEN_FEATURE_BT_DPM
646 if (_bt_check_dpm(BT_DPM_ADDRESS, (void *)remote_bt_address) == BT_DPM_RESTRICTED) {
647 BT_ERR("Blacklist device");
648 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
651 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED ||
652 _bt_check_dpm(BT_DPM_HF_ONLY, NULL) == BT_DPM_RESTRICTED) {
653 BT_ERR("Not allow to connect the RFCOMM service");
654 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
657 if (_bt_check_dpm(BT_DPM_DESKTOP, NULL) == BT_DPM_RESTRICTED) {
658 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
659 bluetooth_device_class_t dev_class;
661 _bt_convert_addr_type_to_string(address, (unsigned char *)remote_bt_address->addr);
662 _bt_get_cod_by_address(address, &dev_class);
664 if (dev_class.major_class == BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
665 BT_ERR("Reject a authorization due to MDM Policy");
666 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
671 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
672 BT_INFO_C("### Connect RFCOMM");
677 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
678 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
679 BT_ERR("Don't have a privilege to use this API");
680 return BLUETOOTH_ERROR_PERMISSION_DEINED;
683 id = __rfcomm_assign_id();
685 return BLUETOOTH_ERROR_INTERNAL;
687 cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
689 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
691 object_id = _bt_register_new_conn(path, new_connection);
693 __rfcomm_delete_id(id);
695 return BLUETOOTH_ERROR_INTERNAL;
698 cb_data = g_new0(rfcomm_cb_data_t, 1);
699 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
700 cb_data->obj_path = path;
701 cb_data->object_id = object_id;
705 conn = g_new0(rfcomm_conn_info_t, 1);
707 _bt_convert_addr_type_to_string(conn->bt_addr,
708 (unsigned char *)remote_bt_address->addr);
710 BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
711 cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
713 ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
714 __bt_discover_service_response_cb, cb_data);
715 if (ret != BLUETOOTH_ERROR_NONE) {
716 BT_ERR("Error returned while service discovery");
717 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
718 if (cb_data->rfcomm_conns == NULL)
719 rfcomm_cb_data_remove(cb_data);
720 return BLUETOOTH_ERROR_INTERNAL;
723 if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
724 BT_INFO("Adding callback information to rfcomm_clients");
725 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
727 BT_INFO("Callback information is already added");
729 return BLUETOOTH_ERROR_NONE;
731 user_info = _bt_get_user_data(BT_COMMON);
732 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
734 /* connect_type: BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
735 /* In now, we only support to connecty using UUID */
736 connect_type = BT_RFCOMM_UUID;
738 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
739 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
740 BT_ERR("Don't have a privilege to use this API");
741 return BLUETOOTH_ERROR_PERMISSION_DEINED;
745 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
747 g_array_append_vals(in_param1, remote_bt_address,
748 sizeof(bluetooth_device_address_t));
750 g_strlcpy(uuid, remote_uuid, sizeof(uuid));
751 g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
753 g_array_append_vals(in_param3, &connect_type, sizeof(int));
755 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
756 BT_RFCOMM_CLIENT_CONNECT,
757 in_param1, in_param2,
758 in_param3, in_param4,
759 user_info->cb, user_info->user_data);
761 BT_DBG("result: %x", result);
763 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
769 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
772 GSList *conn_list = NULL;
773 rfcomm_cb_data_t *client_info;
774 rfcomm_conn_info_t *conn_info;
775 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
777 BT_CHECK_PARAMETER(device_address, return);
778 BT_CHECK_PARAMETER(connected, return);
780 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
783 for (l = rfcomm_clients; l != NULL; l = l->next) {
784 client_info = l->data;
785 if (client_info == NULL)
787 for (conn_list = client_info->rfcomm_conns;
788 conn_list != NULL; conn_list = conn_list->next) {
789 conn_info = conn_list->data;
790 if (conn_info == NULL)
793 if (g_strcmp0(address, conn_info->bt_addr) == 0) {
795 return BLUETOOTH_ERROR_NONE;
800 return BLUETOOTH_ERROR_NONE;
803 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
806 int connected = FALSE;
808 BT_CHECK_ENABLED(return);
811 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
813 result = _bt_send_request(BT_BLUEZ_SERVICE,
814 BT_RFCOMM_CLIENT_IS_CONNECTED,
815 in_param1, in_param2, in_param3,
816 in_param4, &out_param);
818 BT_DBG("result: %x", result);
820 if (result == BLUETOOTH_ERROR_NONE) {
821 connected = g_array_index(out_param,
824 BT_ERR("Fail to send request");
827 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
832 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
834 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
835 rfcomm_cb_data_t *info;
836 rfcomm_conn_info_t *conn_info;
838 BT_INFO_C("### Disconnect RFCOMM");
840 BT_CHECK_ENABLED(return);
842 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT)
843 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
844 BT_ERR("Don't have a privilege to use this API");
845 return BLUETOOTH_ERROR_PERMISSION_DEINED;
848 BT_DBG("Requested FD %d", socket_fd);
850 BT_ERR("Invalid FD");
851 return BLUETOOTH_ERROR_INVALID_PARAM;
853 BT_DBG("FDD %d", socket_fd);
855 info = __find_rfcomm_info_with_fd(socket_fd);
857 BT_DBG("Could not find in client, so check in server");
858 return bluetooth_rfcomm_server_disconnect(socket_fd);
861 conn_info = __get_conn_info_from_fd(info, socket_fd);
862 if (conn_info == NULL) {
863 BT_ERR("Could not find connection info");
864 return BLUETOOTH_ERROR_INTERNAL;
867 if (conn_info->watch_id == 0 || conn_info->disconnected) {
868 BT_ERR("Invalid state");
869 return BLUETOOTH_ERROR_NOT_CONNECTED;
872 close(conn_info->fd);
873 conn_info->disconnected = TRUE;
875 BT_INFO("conn_info %s", conn_info->bt_addr);
876 _bt_disconnect_ext_profile(conn_info->bt_addr, info->obj_path);
878 if (info->idle_id == 0)
879 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
881 return BLUETOOTH_ERROR_NONE;
885 BT_CHECK_ENABLED(return);
888 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
890 /* Support the OSP */
891 if (socket_fd == -1) {
893 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CANCEL_CONNECT,
894 in_param1, in_param2, in_param3, in_param4, &out_param);
896 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
897 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT,
898 in_param1, in_param2, in_param3, in_param4, &out_param);
901 BT_DBG("result: %x", result);
903 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
909 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
911 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
918 BT_CHECK_PARAMETER(buf, return);
920 BT_ERR("Invalid FD");
921 return BLUETOOTH_ERROR_INVALID_PARAM;
924 #ifndef TIZEN_FEATURE_BT_RFCOMM_DIRECT
925 BT_CHECK_ENABLED(return);
927 retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
929 #ifdef TIZEN_FEATURE_BT_DPM
930 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED ||
931 _bt_check_dpm(BT_DPM_HF_ONLY, NULL) == BT_DPM_RESTRICTED) {
932 BT_ERR("Not allow to write RFCOMM data");
933 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
937 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
938 switch (privilege_token) {
940 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE);
942 if (result == BLUETOOTH_ERROR_NONE) {
943 privilege_token = 1; /* Have a permission */
944 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
945 BT_ERR("Don't have a privilege to use this API");
946 privilege_token = -1; /* Don't have a permission */
947 return BLUETOOTH_ERROR_PERMISSION_DEINED;
949 /* Just break - It is not related with permission error */
953 /* Already have a privilege */
956 return BLUETOOTH_ERROR_PERMISSION_DEINED;
958 /* Invalid privilge token value */
959 return BLUETOOTH_ERROR_INTERNAL;
962 written = write(fd, buf, length);
963 /*BT_DBG("Length %d, written = %d, balance(%d)",
964 length, written, length - written); */
968 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
970 buffer = g_malloc0(length + 1);
972 memcpy(buffer, buf, length);
974 g_array_append_vals(in_param1, &fd, sizeof(int));
975 g_array_append_vals(in_param2, &length, sizeof(int));
976 g_array_append_vals(in_param3, buffer, length);
978 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
979 in_param1, in_param2, in_param3, in_param4, &out_param);
981 BT_DBG("result: %x", result);
983 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);