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.
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_DPM_ENABLE
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)
134 if (conn->watch_id > 0) {
135 g_source_remove(conn->watch_id);
143 static void __rfcomm_remove_conn_info_t(rfcomm_cb_data_t *info, char *address)
146 rfcomm_conn_info_t *conn_info = NULL;
147 l = g_slist_find_custom(info->rfcomm_conns, address, (GCompareFunc)compare);
151 info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn_info);
152 __bt_free_conn(conn_info);
156 static rfcomm_conn_info_t *__get_conn_info_from_fd(rfcomm_cb_data_t *info,
160 rfcomm_conn_info_t *device_node = NULL;
161 for(l = info->rfcomm_conns; l != NULL; l = l->next) {
162 device_node = l->data;
163 if ( device_node && device_node->fd == fd)
169 static rfcomm_conn_info_t *__get_conn_info_from_address(rfcomm_cb_data_t *info,
173 rfcomm_conn_info_t *conn_info = NULL;
174 l = g_slist_find_custom(info->rfcomm_conns, dev_address,
175 (GCompareFunc)compare);
181 static void __rfcomm_client_connected_cb(rfcomm_cb_data_t *info,
182 char *dev_address, int result)
184 __client_connected_cb(info, dev_address, result);
185 __rfcomm_remove_conn_info_t(info, dev_address);
187 if (info->rfcomm_conns == NULL)
188 rfcomm_cb_data_remove(info);
191 static rfcomm_cb_data_t *__find_rfcomm_info_with_fd(int fd)
195 for (l = rfcomm_clients; l != NULL; l = l->next) {
196 rfcomm_cb_data_t *info = l->data;
197 device_fd = g_slist_find_custom(info->rfcomm_conns, &fd,
198 (GCompareFunc)compare_fd);
206 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
210 for (l = rfcomm_clients; l != NULL; l = l->next) {
211 rfcomm_cb_data_t *info = l->data;
214 if (g_strcmp0(info->obj_path, path) == 0)
221 static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
225 for (l = rfcomm_clients; l != NULL; l = l->next) {
226 rfcomm_cb_data_t *info = l->data;
228 if (g_strcmp0(info->uuid, uuid) == 0)
235 static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
236 rfcomm_cb_data_t *info)
238 if (conn_info == NULL)
241 bluetooth_rfcomm_disconnection_t disconn_info;
242 bt_event_info_t *event_info = NULL;
243 if (conn_info->disconnected == FALSE)
245 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
246 if (event_info == NULL) {
247 if (info->rfcomm_conns == NULL)
248 rfcomm_cb_data_remove(info);
251 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
252 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
253 g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
254 BT_DBG("Disconnected FD [%d]", conn_info->fd);
255 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
258 disconn_info.socket_fd = conn_info->fd;
260 BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
261 BLUETOOTH_ERROR_NONE, conn_info->bt_addr,
262 info->uuid, conn_info->fd);
263 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
264 BLUETOOTH_ERROR_NONE, &disconn_info,
265 event_info->cb, event_info->user_data);
268 BT_DBG("List is present deleting it");
269 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
271 if (info->rfcomm_conns == NULL)
272 rfcomm_cb_data_remove(info);
277 static gboolean __rfcomm_client_disconnect(gpointer user_data)
279 rfcomm_cb_data_t *info = (rfcomm_cb_data_t *) user_data;
280 BT_INFO_C("Disconnected [RFCOMM Client]");
281 retv_if(info == NULL, FALSE);
283 if (g_slist_find(rfcomm_clients, info) == NULL) {
284 BT_INFO("rfcomm resource is already freed");
288 g_slist_foreach(info->rfcomm_conns,
289 (GFunc) _bt_rfcomm_disconnect_conn_info, info);
294 static gboolean __is_error_by_disconnect(GError *err)
296 return !g_strcmp0(err->message, "Connection reset by peer") ||
297 !g_strcmp0(err->message, "Connection timed out") ||
298 !g_strcmp0(err->message, "Software caused connection abort");
301 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
306 int result = BLUETOOTH_ERROR_NONE;
307 rfcomm_cb_data_t *info = data;
308 rfcomm_conn_info_t *conn_info = NULL;
309 bt_event_info_t *event_info;
310 bluetooth_rfcomm_received_data_t data_r;
311 GIOStatus status = G_IO_STATUS_NORMAL;
316 retv_if(info == NULL, FALSE);
317 fd = g_io_channel_unix_get_fd(chan);
318 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
319 BT_ERR_C("RFComm Client disconnected: %d", fd);
320 conn_info = __get_conn_info_from_fd(info, fd);
321 if (conn_info == NULL) {
322 BT_ERR("No Connection info found with FD [%d]", fd);
325 conn_info->disconnected = TRUE;
326 __rfcomm_client_disconnect(info);
330 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
332 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
334 if (status != G_IO_STATUS_NORMAL) {
335 BT_ERR("IO Channel read is failed with %d", status);
339 BT_ERR("IO Channel read error [%s]", err->message);
340 if (status == G_IO_STATUS_ERROR &&
341 __is_error_by_disconnect(err)) {
342 BT_ERR("cond : %d", cond);
344 conn_info = __get_conn_info_from_fd(info, fd);
345 if (conn_info == NULL) {
346 BT_ERR("No Connection info found with FD [%d]", fd);
349 conn_info->disconnected = TRUE;
350 __rfcomm_client_disconnect(info);
358 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
359 if (event_info == NULL) {
364 data_r.socket_fd = fd;
365 data_r.buffer_size = len;
366 data_r.buffer = buffer;
368 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
370 event_info->cb, event_info->user_data);
376 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
379 bluetooth_rfcomm_connection_t conn_info;
380 bt_event_info_t *event_info;
381 rfcomm_conn_info_t *conn_list_info = NULL;
382 BT_INFO_C("Connected [RFCOMM Client]");
384 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
385 if (event_info == NULL)
388 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
389 conn_info.device_role = RFCOMM_ROLE_CLIENT;
390 g_strlcpy(conn_info.uuid, cb_data->uuid, BLUETOOTH_UUID_STRING_MAX);
391 _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
393 conn_list_info = __get_conn_info_from_address(cb_data, dev_address);
394 if (conn_list_info == NULL) {
395 BT_ERR("Device addres %s not found in connection list", dev_address);
398 conn_info.socket_fd = conn_list_info->fd;
399 conn_info.server_id = -1;
401 BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
402 result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
403 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
405 event_info->cb, event_info->user_data);
411 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
413 rfcomm_cb_data_t *info;
415 rfcomm_conn_info_t *conn_info = NULL;
416 BT_DBG("%s %d", path, fd);
417 char address[BT_ADDRESS_STRING_SIZE];
418 _bt_convert_addr_type_to_string(address,
419 (unsigned char *)addr);
420 info = __find_rfcomm_info_from_path(path);
423 conn_info = __get_conn_info_from_address(info, address);
425 if (conn_info == NULL) {
426 BT_ERR("Device Address %s not found in connection list", address);
430 BT_DBG("connection info fd %d", conn_info->fd);
431 data_io = g_io_channel_unix_new(fd);
432 g_io_channel_set_encoding(data_io, NULL, NULL);
433 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
434 conn_info->watch_id = g_io_add_watch(data_io,
435 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
436 __client_data_received_cb, info);
438 g_io_channel_unref(data_io);
440 __client_connected_cb(info, address,BLUETOOTH_ERROR_NONE);
445 static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
449 GError *error = NULL;
450 rfcomm_cb_data_t *cb_data;
451 char dev_address[BT_ADDRESS_STRING_SIZE];
455 ret_if(user_data == NULL);
459 if (!g_dbus_proxy_call_finish(proxy, res, &error)) {
461 g_dbus_error_strip_remote_error(error);
462 BT_ERR("Error : %s \n", error->message);
464 if (g_strcmp0(error->message, "In Progress") == 0)
465 result = BLUETOOTH_ERROR_DEVICE_BUSY;
467 result = BLUETOOTH_ERROR_INTERNAL;
468 path = g_dbus_proxy_get_object_path(proxy);
469 _bt_convert_device_path_to_address(path, dev_address);
470 __rfcomm_client_connected_cb(cb_data, dev_address, result);
475 g_object_unref(proxy);
480 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
481 GAsyncResult *res, gpointer user_data)
483 rfcomm_cb_data_t *cb_data;
486 bt_register_profile_info_t info = {0};
487 int result = BLUETOOTH_ERROR_NONE;
488 char dev_address[BT_ADDRESS_STRING_SIZE];
493 ret_if(user_data == NULL);
497 path = g_dbus_proxy_get_object_path(proxy);
499 _bt_convert_device_path_to_address(path, dev_address);
500 BT_DBG("Device Adress [%s]", dev_address);
501 g_dbus_proxy_call_finish(proxy, res, &err);
503 g_object_unref(proxy);
506 g_dbus_error_strip_remote_error(err);
507 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
508 if (!strcmp("Operation canceled", err->message)) {
509 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
510 } else if (!strcmp("In Progress", err->message)) {
511 result = BLUETOOTH_ERROR_IN_PROGRESS;
512 } else if (!strcmp("Host is down", err->message)) {
513 result = BLUETOOTH_ERROR_HOST_DOWN;
514 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
515 result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
516 ret = _bt_cancel_discovers(dev_address);
517 if (ret != BLUETOOTH_ERROR_NONE)
518 BT_ERR("Error: While CancelDiscovery");
520 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
522 __rfcomm_client_connected_cb(cb_data, dev_address, result);
525 BT_INFO("Services are Updated checking required uuid is there");
526 /* Check here for uuid present */
527 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
528 if (ret == BLUETOOTH_ERROR_NONE) {
529 info.uuid = (char *)cb_data->uuid;
530 info.obj_path = cb_data->obj_path;
531 info.role = "client";
533 ret = _bt_register_profile(&info, FALSE);
535 BT_DBG("Error: register profile");
536 ret = _bt_connect_profile(dev_address, cb_data->uuid,
537 __bt_connect_response_cb, cb_data);
539 if (ret != BLUETOOTH_ERROR_NONE) {
540 BT_ERR("ConnectProfile failed");
541 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
542 __rfcomm_client_connected_cb(cb_data, dev_address, result);
546 BT_ERR("remote uuid not found");
547 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
548 __rfcomm_client_connected_cb(cb_data, dev_address, result);
556 BT_EXPORT_API int bluetooth_rfcomm_connect(
557 const bluetooth_device_address_t *remote_bt_address,
558 const char *remote_uuid)
562 rfcomm_cb_data_t *cb_data = NULL;
563 rfcomm_conn_info_t *conn = NULL;
567 bt_user_info_t *user_info;
568 char uuid[BLUETOOTH_UUID_STRING_MAX];
570 BT_CHECK_PARAMETER(remote_bt_address, return);
571 BT_CHECK_PARAMETER(remote_uuid, return);
572 BT_CHECK_ENABLED(return);
574 #ifdef TIZEN_DPM_ENABLE
575 if (_bt_check_dpm(BT_DPM_ADDRESS, (void *)remote_bt_address) == BT_DPM_RESTRICTED) {
576 BT_ERR("Blacklist device");
577 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
580 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED ||
581 _bt_check_dpm(BT_DPM_HF_ONLY, NULL) == BT_DPM_RESTRICTED) {
582 BT_ERR("Not allow to connect the RFCOMM service");
583 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
586 if (_bt_check_dpm(BT_DPM_DESKTOP, NULL) == BT_DPM_RESTRICTED) {
587 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
588 bluetooth_device_class_t dev_class;
590 _bt_convert_addr_type_to_string(address, (unsigned char *)remote_bt_address->addr);
591 _bt_get_cod_by_address(address, &dev_class);
593 if (dev_class.major_class == BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
594 BT_ERR("Reject a authorization due to MDM Policy");
595 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
601 BT_INFO_C("<<<<<<<<< RFCOMM Connect request from app >>>>>>>>>>>");
606 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
607 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
608 BT_ERR("Don't have a privilege to use this API");
609 return BLUETOOTH_ERROR_PERMISSION_DEINED;
612 id = __rfcomm_assign_id();
614 return BLUETOOTH_ERROR_INTERNAL;
616 cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
618 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
620 object_id = _bt_register_new_conn(path, new_connection);
622 __rfcomm_delete_id(id);
623 return BLUETOOTH_ERROR_INTERNAL;
626 cb_data = g_new0(rfcomm_cb_data_t, 1);
627 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
628 cb_data->obj_path = path;
629 cb_data->object_id = object_id;
632 conn = g_new0(rfcomm_conn_info_t, 1);
634 _bt_convert_addr_type_to_string(conn->bt_addr,
635 (unsigned char *)remote_bt_address->addr);
637 BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
638 cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
639 ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
640 __bt_discover_service_response_cb, cb_data);
641 if (ret != BLUETOOTH_ERROR_NONE) {
642 BT_ERR("Error returned while service discovery");
643 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
644 if (cb_data->rfcomm_conns == NULL)
645 rfcomm_cb_data_remove(cb_data);
646 return BLUETOOTH_ERROR_INTERNAL;
648 if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
649 BT_INFO("Adding callback information to rfcomm_clients");
650 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
652 BT_INFO("Callback information is already added");
654 return BLUETOOTH_ERROR_NONE;
656 user_info = _bt_get_user_data(BT_COMMON);
657 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
659 /* connect_type: BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
660 /* In now, we only support to connecty using UUID */
661 connect_type = BT_RFCOMM_UUID;
663 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
664 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
665 BT_ERR("Don't have a privilege to use this API");
666 return BLUETOOTH_ERROR_PERMISSION_DEINED;
670 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
672 g_array_append_vals(in_param1, remote_bt_address,
673 sizeof(bluetooth_device_address_t));
675 g_strlcpy(uuid, remote_uuid, sizeof(uuid));
676 g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
678 g_array_append_vals(in_param3, &connect_type, sizeof(int));
680 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
681 BT_RFCOMM_CLIENT_CONNECT,
682 in_param1, in_param2,
683 in_param3, in_param4,
684 user_info->cb, user_info->user_data);
686 BT_DBG("result: %x", result);
688 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
694 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
697 GSList *conn_list = NULL;
698 rfcomm_cb_data_t *client_info;
699 rfcomm_conn_info_t *conn_info;
700 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
702 BT_CHECK_PARAMETER(device_address, return);
703 BT_CHECK_PARAMETER(connected, return);
705 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
708 for (l = rfcomm_clients; l != NULL; l = l->next) {
709 client_info = l->data;
710 if (client_info == NULL)
712 for(conn_list = client_info->rfcomm_conns;
713 conn_list != NULL; conn_list = conn_list->next) {
714 conn_info = conn_list->data;
715 if(conn_info == NULL)
718 if (g_strcmp0(address, conn_info->bt_addr) == 0) {
720 return BLUETOOTH_ERROR_NONE;
725 return BLUETOOTH_ERROR_NONE;
728 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
731 int connected = FALSE;
733 BT_CHECK_ENABLED(return);
736 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
738 result = _bt_send_request(BT_BLUEZ_SERVICE,
739 BT_RFCOMM_CLIENT_IS_CONNECTED,
740 in_param1, in_param2, in_param3,
741 in_param4, &out_param);
743 BT_DBG("result: %x", result);
745 if (result == BLUETOOTH_ERROR_NONE) {
746 connected = g_array_index(out_param,
749 BT_ERR("Fail to send request");
752 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
757 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
760 rfcomm_cb_data_t *info;
761 rfcomm_conn_info_t *conn_info;
762 BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
763 BT_CHECK_ENABLED(return);
765 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT)
766 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
767 BT_ERR("Don't have a privilege to use this API");
768 return BLUETOOTH_ERROR_PERMISSION_DEINED;
772 BT_ERR("Invalid FD");
773 return BLUETOOTH_ERROR_INVALID_PARAM;
775 BT_DBG("FDD %d", socket_fd);
777 info = __find_rfcomm_info_with_fd(socket_fd);
779 BT_DBG("Could not find in client, so check in server");
780 return bluetooth_rfcomm_server_disconnect(socket_fd);
782 conn_info = __get_conn_info_from_fd(info, socket_fd);
783 if (conn_info == NULL) {
784 BT_ERR("FATAL Error");
785 return BLUETOOTH_ERROR_INTERNAL;
787 if (conn_info->watch_id <= 0) {
788 BT_ERR("Invalid state");
789 return BLUETOOTH_ERROR_NOT_CONNECTED;
791 conn_info->disconnected = TRUE;
793 BT_INFO("conn_info %s", conn_info->bt_addr);
794 _bt_disconnect_profile(conn_info->bt_addr, info->uuid, NULL,NULL);
795 if (info->idle_id == 0)
796 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
798 return BLUETOOTH_ERROR_NONE;
801 int service_function;
803 BT_CHECK_ENABLED(return);
806 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
808 /* Support the OSP */
809 if (socket_fd == -1) {
811 service_function = BT_RFCOMM_CLIENT_CANCEL_CONNECT;
813 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
814 service_function = BT_RFCOMM_SOCKET_DISCONNECT;
817 result = _bt_send_request(BT_BLUEZ_SERVICE, service_function,
818 in_param1, in_param2, in_param3, in_param4, &out_param);
820 BT_DBG("result: %x", result);
822 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
828 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
837 BT_CHECK_PARAMETER(buf, return);
839 BT_ERR("Invalid FD");
840 return BLUETOOTH_ERROR_INVALID_PARAM;
843 BT_DBG("FD : %d", fd);
845 #ifndef RFCOMM_DIRECT
846 BT_CHECK_ENABLED(return);
848 retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
850 #ifdef TIZEN_DPM_ENABLE
851 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED ||
852 _bt_check_dpm(BT_DPM_HF_ONLY, NULL) == BT_DPM_RESTRICTED) {
853 BT_ERR("Not allow to write RFCOMM data");
854 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
859 switch (privilege_token) {
861 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE);
863 if (result == BLUETOOTH_ERROR_NONE) {
864 privilege_token = 1; /* Have a permission */
865 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
866 BT_ERR("Don't have a privilege to use this API");
867 privilege_token = -1; /* Don't have a permission */
868 return BLUETOOTH_ERROR_PERMISSION_DEINED;
870 /* Just break - It is not related with permission error */
874 /* Already have a privilege */
877 return BLUETOOTH_ERROR_PERMISSION_DEINED;
879 /* Invalid privilge token value */
880 return BLUETOOTH_ERROR_INTERNAL;
883 written = write(fd, buf, length);
884 /*BT_DBG("Length %d, written = %d, balance(%d)",
885 length, written, length - written); */
889 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
891 buffer = g_malloc0(length + 1);
893 memcpy(buffer, buf, length);
895 g_array_append_vals(in_param1, &fd, sizeof(int));
896 g_array_append_vals(in_param2, &length, sizeof(int));
897 g_array_append_vals(in_param3, buffer, length);
899 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
900 in_param1, in_param2, in_param3, in_param4, &out_param);
902 BT_DBG("result: %x", result);
904 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);