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 #include <gio/gunixfdlist.h>
21 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
25 #include "bluetooth-api.h"
26 #include "bt-internal-types.h"
28 #include "bt-common.h"
29 #include "bt-request-sender.h"
30 #include "bt-event-handler.h"
32 #ifdef TIZEN_FEATURE_BT_DPM
36 /* Variable for privilege, only for write API,
37 before we should reduce time to bt-service dbus calling
38 -1 : Don't have a permission to access API
39 0 : Initial value, not yet check
40 1 : Have a permission to access API
42 static int privilege_token;
45 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
46 #define BT_TIMEOUT_MESSAGE "Did not receive a reply. Possible causes include: " \
47 "the remote application did not send a reply, " \
48 "the message bus security policy blocked the reply, " \
49 "the reply timeout expired, or the network connection " \
52 static GSList *rfcomm_clients;
55 char bt_addr[BT_ADDRESS_STRING_SIZE];
58 gboolean disconnected;
62 char uuid[BLUETOOTH_UUID_STRING_MAX];
71 static void __client_connected_cb(rfcomm_cb_data_t *cb_data,
72 char *dev_address, int result);
74 static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
79 __rfcomm_delete_id(cb_data->id);
81 if (cb_data->object_id > 0)
82 _bt_unregister_gdbus(cb_data->object_id);
84 if (cb_data->obj_path) {
85 BT_INFO("Unregister profile");
86 _bt_unregister_profile(cb_data->obj_path);
89 if (cb_data->idle_id != 0) {
90 BT_INFO("Removing idle source");
91 g_source_remove(cb_data->idle_id);
94 g_free(cb_data->obj_path);
96 g_free(cb_data->device_path);
101 static void rfcomm_cb_data_remove(rfcomm_cb_data_t *info)
104 BT_INFO("No more device connected remove info");
105 rfcomm_clients = g_slist_remove(rfcomm_clients, info);
106 __bt_free_cb_data(info);
110 gint compare(gpointer *a, gpointer *b)
112 rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
113 char *address = (char *)b;
114 return g_strcmp0(node->bt_addr, address);
117 gint compare_fd(gpointer *a, gpointer *b)
119 rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
126 static void __bt_free_conn(rfcomm_conn_info_t *conn)
133 if (conn->watch_id > 0) {
134 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 if (g_slist_find(rfcomm_clients, info) == NULL) {
185 BT_INFO("rfcomm resource is already freed");
189 __client_connected_cb(info, dev_address, result);
190 __rfcomm_remove_conn_info_t(info, dev_address);
192 if (info->rfcomm_conns == NULL)
193 rfcomm_cb_data_remove(info);
196 static rfcomm_cb_data_t *__find_rfcomm_info_with_fd(int fd)
200 for (l = rfcomm_clients; l != NULL; l = l->next) {
201 rfcomm_cb_data_t *info = l->data;
202 device_fd = g_slist_find_custom(info->rfcomm_conns, &fd,
203 (GCompareFunc)compare_fd);
211 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
215 for (l = rfcomm_clients; l != NULL; l = l->next) {
216 rfcomm_cb_data_t *info = l->data;
219 if (g_strcmp0(info->obj_path, path) == 0)
226 static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
230 for (l = rfcomm_clients; l != NULL; l = l->next) {
231 rfcomm_cb_data_t *info = l->data;
233 if (g_strcmp0(info->uuid, uuid) == 0)
240 static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
241 rfcomm_cb_data_t *info)
243 if (conn_info == NULL)
246 bluetooth_rfcomm_disconnection_t disconn_info;
247 bt_event_info_t *event_info = NULL;
249 if (conn_info->disconnected == FALSE)
252 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
253 if (event_info == NULL) {
254 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
256 if (info->rfcomm_conns == NULL)
257 rfcomm_cb_data_remove(info);
261 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
262 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
263 g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
264 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
267 BT_DBG("Disconnected FD [%d]", conn_info->fd);
268 disconn_info.socket_fd = conn_info->fd;
270 BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
271 BLUETOOTH_ERROR_NONE, conn_info->bt_addr,
272 info->uuid, conn_info->fd);
273 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
274 BLUETOOTH_ERROR_NONE, &disconn_info,
275 event_info->cb, event_info->user_data);
277 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
279 if (info->rfcomm_conns == NULL)
280 rfcomm_cb_data_remove(info);
285 static gboolean __rfcomm_client_disconnect(gpointer user_data)
287 rfcomm_cb_data_t *info = (rfcomm_cb_data_t *)user_data;
289 BT_INFO_C("### Disconnected [RFCOMM Client]");
291 retv_if(info == NULL, FALSE);
293 if (g_slist_find(rfcomm_clients, info) == NULL) {
294 BT_INFO("rfcomm resource is already freed");
299 g_slist_foreach(info->rfcomm_conns,
300 (GFunc)_bt_rfcomm_disconnect_conn_info, info);
306 static gboolean __is_error_by_disconnect(GError *err)
308 return !g_strcmp0(err->message, "Connection reset by peer") ||
309 !g_strcmp0(err->message, "Connection timed out") ||
310 !g_strcmp0(err->message, "Software caused connection abort");
313 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
318 int result = BLUETOOTH_ERROR_NONE;
319 rfcomm_cb_data_t *info = data;
320 rfcomm_conn_info_t *conn_info = NULL;
321 bt_event_info_t *event_info;
322 bluetooth_rfcomm_received_data_t data_r;
323 GIOStatus status = G_IO_STATUS_NORMAL;
328 retv_if(info == NULL, FALSE);
329 fd = g_io_channel_unix_get_fd(chan);
330 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
331 BT_ERR_C("RFComm Client disconnected: %d", fd);
333 conn_info = __get_conn_info_from_fd(info, fd);
334 if (conn_info == NULL) {
335 BT_ERR("No Connection info found with FD [%d]", fd);
339 if (conn_info->disconnected == FALSE) {
340 close(conn_info->fd);
341 conn_info->disconnected = TRUE;
343 __rfcomm_client_disconnect(info);
347 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
349 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
351 if (status != G_IO_STATUS_NORMAL) {
352 BT_ERR("IO Channel read is failed with %d", status);
356 BT_ERR("IO Channel read error [%s]", err->message);
357 if (status == G_IO_STATUS_ERROR &&
358 __is_error_by_disconnect(err)) {
359 BT_ERR("cond : %d", cond);
362 conn_info = __get_conn_info_from_fd(info, fd);
363 if (conn_info == NULL) {
364 BT_ERR("No Connection info found with FD [%d]", fd);
368 if (conn_info->disconnected == FALSE) {
369 close(conn_info->fd);
370 conn_info->disconnected = TRUE;
372 __rfcomm_client_disconnect(info);
380 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
381 if (event_info == NULL) {
386 data_r.socket_fd = fd;
387 data_r.buffer_size = len;
388 data_r.buffer = buffer;
390 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
392 event_info->cb, event_info->user_data);
398 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
401 bluetooth_rfcomm_connection_t conn_info;
402 bt_event_info_t *event_info;
403 rfcomm_conn_info_t *conn_list_info = NULL;
405 if (result == BLUETOOTH_ERROR_NONE)
406 BT_INFO_C("### Connected [RFCOMM Client]");
408 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
409 if (event_info == NULL)
412 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
413 conn_info.device_role = RFCOMM_ROLE_CLIENT;
414 g_strlcpy(conn_info.uuid, cb_data->uuid, BLUETOOTH_UUID_STRING_MAX);
415 _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
417 conn_list_info = __get_conn_info_from_address(cb_data, dev_address);
418 if (conn_list_info == NULL) {
419 BT_ERR("Device addres %s not found in connection list", dev_address);
422 conn_info.socket_fd = conn_list_info->fd;
423 conn_info.server_id = -1;
425 BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
426 result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
427 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
429 event_info->cb, event_info->user_data);
433 void _bt_rfcomm_client_disconnect_all(void)
438 BT_INFO_C("### Disconnect all RFCOMM client connections");
440 for (client = rfcomm_clients; client; ) {
441 rfcomm_cb_data_t *info = client->data;
443 for (conn = info->rfcomm_conns; conn; conn = conn->next) {
444 rfcomm_conn_info_t *conn_info = conn->data;
446 if (conn_info == NULL)
449 if (conn_info->watch_id == 0 || conn_info->disconnected)
452 close(conn_info->fd);
453 conn_info->disconnected = TRUE;
455 _bt_disconnect_ext_profile(conn_info->bt_addr,
459 client = client->next;
460 __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 GSList *rfcomm_clients;
634 } rfcomm_client_conn_info_t;
636 static gboolean __is_error_by_disconnect(GError *err)
638 return !g_strcmp0(err->message, "Connection reset by peer") ||
639 !g_strcmp0(err->message, "Connection timed out") ||
640 !g_strcmp0(err->message, "Software caused connection abort");
643 static rfcomm_client_conn_info_t *__find_rfcomm_conn_info_with_fd(int fd)
649 for (l = rfcomm_clients; l != NULL; l = l->next) {
650 rfcomm_client_conn_info_t *info = l->data;
652 if (info && info->sock_fd == fd) {
653 BT_INFO("Match found");
662 static void __rfcomm_remove_client_conn_info_t(rfcomm_client_conn_info_t *info)
664 ret_if(info == NULL);
666 rfcomm_clients = g_slist_remove(rfcomm_clients, info);
668 g_free(info->remote_addr);
671 static void __bt_rfcomm_client_disconnected(rfcomm_client_conn_info_t *conn_info)
674 bluetooth_rfcomm_disconnection_t disconn_info;
675 bt_event_info_t *event_info = NULL;
677 ret_if(conn_info == NULL);
679 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
680 ret_if(event_info == NULL);
682 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
683 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
684 disconn_info.socket_fd = conn_info->sock_fd;
685 g_strlcpy(disconn_info.uuid, conn_info->uuid, BLUETOOTH_UUID_STRING_MAX);
686 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
687 conn_info->remote_addr);
689 BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
690 BLUETOOTH_ERROR_NONE, conn_info->remote_addr,
691 conn_info->uuid, conn_info->sock_fd);
692 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
693 BLUETOOTH_ERROR_NONE, &disconn_info,
694 event_info->cb, event_info->user_data);
699 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
701 bt_event_info_t *event_info;
702 bluetooth_rfcomm_received_data_t data_r;
703 rfcomm_client_conn_info_t *conn_info;
709 GIOStatus status = G_IO_STATUS_NORMAL;
713 fd = g_io_channel_unix_get_fd(chan);
714 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
715 BT_ERR_C("RFComm Client disconnected: %d", fd);
719 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
720 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
722 if (status != G_IO_STATUS_NORMAL) {
723 BT_ERR("IO Channel read is failed with %d", status);
726 BT_ERR("IO Channel read error [%s]", err->message);
727 if (status == G_IO_STATUS_ERROR &&
728 __is_error_by_disconnect(err)) {
729 BT_ERR("cond : %d", cond);
740 BT_ERR("Length is zero, remote end hang up");
744 BT_DBG("fd: %d, len: %d, buffer: %s", fd, len, buffer);
746 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
747 if (event_info == NULL) {
748 BT_INFO("event_info == NULL");
753 data_r.socket_fd = fd;
754 data_r.buffer_size = len;
755 data_r.buffer = buffer;
757 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
758 BLUETOOTH_ERROR_NONE, &data_r,
759 event_info->cb, event_info->user_data);
765 conn_info = __find_rfcomm_conn_info_with_fd(fd);
767 __bt_rfcomm_client_disconnected(conn_info);
768 __rfcomm_remove_client_conn_info_t(conn_info);
770 BT_ERR("RFCOMM client conn_info not found");
775 static void __rfcomm_client_connection_create_watch(rfcomm_client_conn_info_t *conn_info)
779 ret_if(NULL == conn_info);
783 data_io = g_io_channel_unix_new(conn_info->sock_fd);
784 g_io_channel_set_encoding(data_io, NULL, NULL);
785 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
786 conn_info->watch_id = g_io_add_watch(data_io,
787 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
788 __client_data_received_cb, NULL);
789 g_io_channel_unref(data_io);
794 static void __bt_rfcomm_handle_new_client_connection(bluetooth_rfcomm_connection_t *info)
796 rfcomm_client_conn_info_t *conn_info;
798 ret_if(NULL == info);
802 conn_info = g_malloc0(sizeof(rfcomm_client_conn_info_t));
803 conn_info->remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
804 _bt_convert_addr_type_to_string(
805 conn_info->remote_addr, info->device_addr.addr);
806 conn_info->uuid = g_strdup(info->uuid);
807 conn_info->sock_fd = info->socket_fd;
809 BT_DBG("Address:%s, UUID:%s Socket: %d",
810 conn_info->remote_addr, conn_info->uuid, conn_info->sock_fd);
812 rfcomm_clients = g_slist_append(rfcomm_clients, conn_info);
813 __rfcomm_client_connection_create_watch(conn_info);
818 static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
823 size = g_variant_get_size(var);
825 data = (char *)g_variant_get_data(var);
827 param = g_array_append_vals(param, data, size);
833 /* TODO_40 : 4.0 merge */
834 /* Don't use this function directly. Instead of it, get the out parameter only */
835 static void __bt_get_event_info(int service_function, GArray *output,
836 int *event, int *event_type, void **param_data)
838 ret_if(event == NULL);
840 BT_DBG("service_function : %s (0x%x)",
841 _bt_convert_service_function_to_string(service_function),
843 switch (service_function) {
844 case BT_RFCOMM_CLIENT_CONNECT:
845 *event_type = BT_RFCOMM_CLIENT_EVENT;
846 *event = BLUETOOTH_EVENT_RFCOMM_CONNECTED;
847 ret_if(output == NULL);
848 *param_data = &g_array_index(output,
849 bluetooth_rfcomm_connection_t, 0);
852 BT_ERR("Unknown function");
858 static void __async_req_cb_with_unix_fd_list(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data)
860 int result = BLUETOOTH_ERROR_NONE;
861 int event_type = BT_ADAPTER_EVENT;
862 bt_req_info_t *cb_data = user_data;
863 bluetooth_event_param_t bt_event;
865 GError *error = NULL;
868 GArray *out_param1 = NULL;
869 GUnixFDList *out_fd_list = NULL;
873 memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
875 value = g_dbus_proxy_call_with_unix_fd_list_finish(proxy, &out_fd_list, res, &error);
878 /* dBUS gives error cause */
879 BT_ERR("D-Bus API failure: message[%s]",
881 g_clear_error(&error);
883 result = BLUETOOTH_ERROR_TIMEOUT;
885 ret_if(cb_data == NULL);
887 __bt_get_event_info(cb_data->service_function, NULL,
888 &bt_event.event, &event_type,
889 &bt_event.param_data);
893 g_variant_get(value, "(iv)", &result, ¶m1);
894 g_variant_unref(value);
897 out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
898 __bt_fill_garray_from_variant(param1, out_param1);
899 g_variant_unref(param1);
905 __bt_get_event_info(cb_data->service_function, out_param1,
906 &bt_event.event, &event_type,
907 &bt_event.param_data);
909 if (result == BLUETOOTH_ERROR_NONE && out_param1) {
910 if (BT_RFCOMM_CLIENT_CONNECT == cb_data->service_function) {
913 bluetooth_rfcomm_connection_t *conn_info;
915 conn_info = (bluetooth_rfcomm_connection_t *)bt_event.param_data;
917 BT_ERR("out_fd_list is NULL");
921 fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
922 BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
923 conn_info->socket_fd = fd_list_array[0];
925 BT_DBG("conn_info->socket_fd: %d", conn_info->socket_fd);
926 __bt_rfcomm_handle_new_client_connection(conn_info);
928 if (cb_data->cb != NULL) {
929 /* Send client connected event */
930 bt_event.result = result;
931 BT_INFO("event_type[%d], result=[%d]", event_type, result);
932 ((bluetooth_cb_func_ptr)cb_data->cb)(
933 bt_event.event, &bt_event, cb_data->user_data);
936 g_free(fd_list_array);
937 g_object_unref(out_fd_list);
943 if (cb_data->cb == NULL)
946 /* Only if fail case, call the callback function*/
947 bt_event.result = result;
949 BT_INFO("event_type[%d], result=[%d]", event_type, result);
950 if (event_type == BT_RFCOMM_CLIENT_EVENT) {
951 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
952 &bt_event, cb_data->user_data);
954 BT_INFO("Not handled event type : %d", event_type);
958 g_array_free(out_param1, TRUE);
965 BT_EXPORT_API int bluetooth_rfcomm_connect(
966 const bluetooth_device_address_t *remote_bt_address,
967 const char *remote_uuid)
970 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
971 rfcomm_cb_data_t *cb_data = NULL;
972 rfcomm_conn_info_t *conn = NULL;
976 bt_user_info_t *user_info;
977 char uuid[BLUETOOTH_UUID_STRING_MAX];
979 BT_CHECK_PARAMETER(remote_bt_address, return);
980 BT_CHECK_PARAMETER(remote_uuid, return);
981 BT_CHECK_ENABLED(return);
983 #ifdef TIZEN_FEATURE_BT_DPM
984 if (_bt_check_dpm(BT_DPM_ADDRESS, (void *)remote_bt_address) == BT_DPM_RESTRICTED) {
985 BT_ERR("Blacklist device");
986 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
989 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED ||
990 _bt_check_dpm(BT_DPM_HF_ONLY, NULL) == BT_DPM_RESTRICTED) {
991 BT_ERR("Not allow to connect the RFCOMM service");
992 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
995 if (_bt_check_dpm(BT_DPM_DESKTOP, NULL) == BT_DPM_RESTRICTED) {
996 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
997 bluetooth_device_class_t dev_class;
999 _bt_convert_addr_type_to_string(address, (unsigned char *)remote_bt_address->addr);
1000 _bt_get_cod_by_address(address, &dev_class);
1002 if (dev_class.major_class == BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
1003 BT_ERR("Reject a authorization due to MDM Policy");
1004 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1009 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1010 BT_INFO_C("### Connect RFCOMM");
1015 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CLIENT_CONNECT)
1016 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1017 BT_ERR("Don't have a privilege to use this API");
1018 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1021 id = __rfcomm_assign_id();
1023 return BLUETOOTH_ERROR_INTERNAL;
1025 cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
1027 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
1029 object_id = _bt_register_new_conn(path, new_connection);
1030 if (object_id < 0) {
1031 __rfcomm_delete_id(id);
1033 return BLUETOOTH_ERROR_INTERNAL;
1036 cb_data = g_new0(rfcomm_cb_data_t, 1);
1037 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
1038 cb_data->obj_path = path;
1039 cb_data->object_id = object_id;
1043 conn = g_new0(rfcomm_conn_info_t, 1);
1045 _bt_convert_addr_type_to_string(conn->bt_addr,
1046 (unsigned char *)remote_bt_address->addr);
1048 BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
1049 cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
1051 ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
1052 __bt_discover_service_response_cb, cb_data);
1053 if (ret != BLUETOOTH_ERROR_NONE) {
1054 BT_ERR("Error returned while service discovery");
1055 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
1056 if (cb_data->rfcomm_conns == NULL)
1057 rfcomm_cb_data_remove(cb_data);
1058 return BLUETOOTH_ERROR_INTERNAL;
1061 if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
1062 BT_INFO("Adding callback information to rfcomm_clients");
1063 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
1065 BT_INFO("Callback information is already added");
1067 return BLUETOOTH_ERROR_NONE;
1069 user_info = _bt_get_user_data(BT_COMMON);
1070 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
1072 /* connect_type: BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
1073 /* In now, we only support to connecty using UUID */
1074 connect_type = BT_RFCOMM_UUID;
1076 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CLIENT_CONNECT)
1077 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1078 BT_ERR("Don't have a privilege to use this API");
1079 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1083 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1085 g_array_append_vals(in_param1, remote_bt_address,
1086 sizeof(bluetooth_device_address_t));
1088 g_strlcpy(uuid, remote_uuid, sizeof(uuid));
1089 g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
1091 g_array_append_vals(in_param3, &connect_type, sizeof(int));
1093 result = _bt_send_request_async_with_unix_fd_list(BT_BLUEZ_SERVICE,
1094 BT_RFCOMM_CLIENT_CONNECT,
1095 in_param1, in_param2,
1096 in_param3, in_param4,
1097 user_info->cb, user_info->user_data,
1098 NULL, (GAsyncReadyCallback)__async_req_cb_with_unix_fd_list);
1100 BT_DBG("result: %x", result);
1102 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1108 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
1110 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1112 GSList *conn_list = NULL;
1113 rfcomm_cb_data_t *client_info;
1114 rfcomm_conn_info_t *conn_info;
1115 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1117 BT_CHECK_PARAMETER(device_address, return);
1118 BT_CHECK_PARAMETER(connected, return);
1120 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
1123 for (l = rfcomm_clients; l != NULL; l = l->next) {
1124 client_info = l->data;
1125 if (client_info == NULL)
1127 for (conn_list = client_info->rfcomm_conns;
1128 conn_list != NULL; conn_list = conn_list->next) {
1129 conn_info = conn_list->data;
1130 if (conn_info == NULL)
1133 if (g_strcmp0(address, conn_info->bt_addr) == 0) {
1135 return BLUETOOTH_ERROR_NONE;
1140 return BLUETOOTH_ERROR_NONE;
1143 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1145 BT_CHECK_PARAMETER(device_address, return);
1146 BT_CHECK_PARAMETER(connected, return);
1151 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
1152 BT_INFO("Client address: [%s]", address);
1154 for (l = rfcomm_clients; l != NULL; l = l->next) {
1155 rfcomm_client_conn_info_t *info = l->data;
1157 if (info && !strncasecmp(info->remote_addr, address, BT_ADDRESS_STRING_SIZE)) {
1158 BT_INFO("Match found");
1160 return BLUETOOTH_ERROR_NONE;
1165 return BLUETOOTH_ERROR_NONE;
1169 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
1172 int connected = FALSE;
1174 BT_CHECK_ENABLED(return);
1177 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1179 result = _bt_send_request(BT_BLUEZ_SERVICE,
1180 BT_RFCOMM_CLIENT_IS_CONNECTED,
1181 in_param1, in_param2, in_param3,
1182 in_param4, &out_param);
1184 BT_DBG("result: %x", result);
1186 if (result == BLUETOOTH_ERROR_NONE) {
1187 connected = g_array_index(out_param,
1190 BT_ERR("Fail to send request");
1193 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1198 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
1200 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1201 rfcomm_cb_data_t *info;
1202 rfcomm_conn_info_t *conn_info;
1204 BT_INFO_C("### Disconnect RFCOMM");
1206 BT_CHECK_ENABLED(return);
1208 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_DISCONNECT)
1209 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1210 BT_ERR("Don't have a privilege to use this API");
1211 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1214 BT_DBG("Requested FD %d", socket_fd);
1215 if (socket_fd < 0) {
1216 BT_ERR("Invalid FD");
1217 return BLUETOOTH_ERROR_INVALID_PARAM;
1219 BT_DBG("FDD %d", socket_fd);
1221 info = __find_rfcomm_info_with_fd(socket_fd);
1223 BT_DBG("Could not find in client, so check in server");
1224 return bluetooth_rfcomm_server_disconnect(socket_fd);
1227 conn_info = __get_conn_info_from_fd(info, socket_fd);
1228 if (conn_info == NULL) {
1229 BT_ERR("Could not find connection info");
1230 return BLUETOOTH_ERROR_INTERNAL;
1233 if (conn_info->watch_id == 0 || conn_info->disconnected) {
1234 BT_ERR("Invalid state");
1235 return BLUETOOTH_ERROR_NOT_CONNECTED;
1238 close(conn_info->fd);
1239 conn_info->disconnected = TRUE;
1241 BT_INFO("conn_info %s", conn_info->bt_addr);
1242 _bt_disconnect_ext_profile(conn_info->bt_addr, info->obj_path);
1244 if (info->idle_id == 0)
1245 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
1247 return BLUETOOTH_ERROR_NONE;
1249 rfcomm_client_conn_info_t *conn_info;
1251 BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
1253 BT_CHECK_ENABLED(return);
1254 retv_if(socket_fd < 0, BLUETOOTH_ERROR_INVALID_PARAM);
1256 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_DISCONNECT)
1257 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1258 BT_ERR("Don't have a privilege to use this API");
1259 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1262 BT_DBG("FD %d", socket_fd);
1264 conn_info = __find_rfcomm_conn_info_with_fd(socket_fd);
1265 if (conn_info == NULL) {
1266 BT_DBG("Could not find in client, so check in server");
1267 /* Check for fd in server list and perform the disconnection if present */
1268 return bluetooth_rfcomm_server_disconnect(socket_fd);
1271 if (conn_info->watch_id <= 0) {
1272 BT_ERR("Invalid state");
1273 return BLUETOOTH_ERROR_NOT_CONNECTED;
1277 * Just close socket here and return. Socket close will be detected via I/O watch
1278 * and disconnection event as well as info cleanup will be performed there.
1280 close(conn_info->sock_fd);
1282 return BLUETOOTH_ERROR_NONE;
1286 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1288 static int __write_all(int fd, const char *buf, int len)
1290 int sent = 0, try = 0;
1296 written = write(fd, buf, len);
1297 BT_DBG("written: %d", written);
1299 if (errno == EINTR || errno == EAGAIN) {
1321 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
1323 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1328 #ifndef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1329 BT_CHECK_ENABLED(return);
1331 BT_CHECK_PARAMETER(buf, return);
1333 BT_ERR("Invalid FD");
1334 return BLUETOOTH_ERROR_INVALID_PARAM;
1337 BT_DBG("FD : %d", fd);
1339 retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
1341 switch (privilege_token) {
1343 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_WRITE);
1345 if (result == BLUETOOTH_ERROR_NONE) {
1346 privilege_token = 1; /* Have a permission */
1347 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1348 BT_ERR("Don't have a privilege to use this API");
1349 privilege_token = -1; /* Don't have a permission */
1350 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1352 /* Just break - It is not related with permission error */
1356 /* Already have a privilege */
1359 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1361 /* Invalid privilge token value */
1362 return BLUETOOTH_ERROR_INTERNAL;
1365 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1366 written = write(fd, buf, length);
1367 /*BT_DBG("Length %d, written = %d, balance(%d)",
1368 length, written, length - written); */
1371 result = __write_all(fd, buf, length);