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);
355 if (err || status == G_IO_STATUS_EOF) {
357 BT_ERR("IO Channel read error [%s]", err->message);
358 if ((status == G_IO_STATUS_EOF) ||
359 (status == G_IO_STATUS_ERROR &&
360 __is_error_by_disconnect(err))) {
362 BT_ERR("cond : %d", cond);
366 conn_info = __get_conn_info_from_fd(info, fd);
367 if (conn_info == NULL) {
368 BT_ERR("No Connection info found with FD [%d]", fd);
372 if (conn_info->disconnected == FALSE) {
373 close(conn_info->fd);
374 conn_info->disconnected = TRUE;
376 __rfcomm_client_disconnect(info);
385 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
386 if (event_info == NULL) {
391 data_r.socket_fd = fd;
392 data_r.buffer_size = len;
393 data_r.buffer = buffer;
395 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
397 event_info->cb, event_info->user_data);
399 if (bluetooth_get_battery_monitor_state()) {
400 int ret = _bt_common_send_rfcomm_rx_details(&data_r);
401 if (ret != BLUETOOTH_ERROR_NONE)
402 BT_ERR("RFCOMM received data details not sent to battery monitor frwk");
410 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
413 bluetooth_rfcomm_connection_t conn_info;
414 bt_event_info_t *event_info;
415 rfcomm_conn_info_t *conn_list_info = NULL;
417 if (result == BLUETOOTH_ERROR_NONE)
418 BT_INFO_C("### Connected [RFCOMM Client]");
420 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
421 if (event_info == NULL)
424 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
425 conn_info.device_role = RFCOMM_ROLE_CLIENT;
426 g_strlcpy(conn_info.uuid, cb_data->uuid, BLUETOOTH_UUID_STRING_MAX);
427 _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
429 conn_list_info = __get_conn_info_from_address(cb_data, dev_address);
430 if (conn_list_info == NULL) {
431 BT_ERR("Device addres %s not found in connection list", dev_address);
434 conn_info.socket_fd = conn_list_info->fd;
435 conn_info.server_id = -1;
437 BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
438 result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
439 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
441 event_info->cb, event_info->user_data);
445 void _bt_rfcomm_client_disconnect_all(void)
450 BT_INFO_C("### Disconnect all RFCOMM client connections");
452 for (client = rfcomm_clients; client; ) {
453 rfcomm_cb_data_t *info = client->data;
455 for (conn = info->rfcomm_conns; conn; conn = conn->next) {
456 rfcomm_conn_info_t *conn_info = conn->data;
458 if (conn_info == NULL)
461 if (conn_info->watch_id == 0 || conn_info->disconnected)
464 close(conn_info->fd);
465 conn_info->disconnected = TRUE;
467 _bt_disconnect_ext_profile(conn_info->bt_addr,
471 client = client->next;
472 __rfcomm_client_disconnect(info);
478 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
480 rfcomm_cb_data_t *info;
482 rfcomm_conn_info_t *conn_info = NULL;
483 char address[BT_ADDRESS_STRING_SIZE];
485 BT_INFO("%s %d", path, fd);
487 _bt_convert_addr_type_to_string(address,
488 (unsigned char *)addr);
490 info = __find_rfcomm_info_from_path(path);
492 BT_ERR("rfcomm info is NULL");
496 conn_info = __get_conn_info_from_address(info, address);
497 if (conn_info == NULL) {
498 BT_ERR("connection info is NULL");
504 data_io = g_io_channel_unix_new(fd);
506 g_io_channel_set_encoding(data_io, NULL, NULL);
507 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
509 conn_info->watch_id = g_io_add_watch(data_io,
510 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
511 __client_data_received_cb, info);
513 g_io_channel_unref(data_io);
515 __client_connected_cb(info, address, BLUETOOTH_ERROR_NONE);
520 static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
524 GError *error = NULL;
526 rfcomm_cb_data_t *cb_data;
527 char dev_address[BT_ADDRESS_STRING_SIZE];
531 ret_if(user_data == NULL);
535 value = g_dbus_proxy_call_finish(proxy, res, &error);
538 g_dbus_error_strip_remote_error(error);
539 BT_ERR("Error : %s \n", error->message);
541 if (g_strcmp0(error->message, "In Progress") == 0)
542 result = BLUETOOTH_ERROR_DEVICE_BUSY;
544 result = BLUETOOTH_ERROR_INTERNAL;
545 path = g_dbus_proxy_get_object_path(proxy);
546 _bt_convert_device_path_to_address(path, dev_address);
547 __rfcomm_client_connected_cb(cb_data, dev_address, result);
551 g_variant_unref(value);
555 g_object_unref(proxy);
560 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
561 GAsyncResult *res, gpointer user_data)
563 rfcomm_cb_data_t *cb_data;
567 bt_register_profile_info_t info = {0};
568 int result = BLUETOOTH_ERROR_NONE;
569 char dev_address[BT_ADDRESS_STRING_SIZE];
574 ret_if(user_data == NULL);
578 path = g_dbus_proxy_get_object_path(proxy);
580 _bt_convert_device_path_to_address(path, dev_address);
581 BT_DBG("Device Adress [%s]", dev_address);
582 value = g_dbus_proxy_call_finish(proxy, res, &err);
584 g_object_unref(proxy);
586 g_variant_unref(value);
589 g_dbus_error_strip_remote_error(err);
590 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
591 if (!strcmp("Operation canceled", err->message)) {
592 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
593 } else if (!strcmp("In Progress", err->message)) {
594 result = BLUETOOTH_ERROR_IN_PROGRESS;
595 } else if (!strcmp("Host is down", err->message)) {
596 result = BLUETOOTH_ERROR_HOST_DOWN;
597 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
598 result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
599 ret = _bt_cancel_discovers(dev_address);
600 if (ret != BLUETOOTH_ERROR_NONE)
601 BT_ERR("Error: While CancelDiscovery");
603 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
605 __rfcomm_client_connected_cb(cb_data, dev_address, result);
608 BT_INFO("Services are Updated checking required uuid is there");
609 /* Check here for uuid present */
610 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
611 if (ret == BLUETOOTH_ERROR_NONE) {
612 info.uuid = (char *)cb_data->uuid;
613 info.obj_path = cb_data->obj_path;
614 info.role = "client";
616 ret = _bt_register_profile(&info, FALSE);
618 BT_DBG("Error: register profile");
619 ret = _bt_connect_profile(dev_address, cb_data->uuid,
620 __bt_connect_response_cb, cb_data);
622 if (ret != BLUETOOTH_ERROR_NONE) {
623 BT_ERR("ConnectProfile failed");
624 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
625 __rfcomm_client_connected_cb(cb_data, dev_address, result);
629 BT_ERR("remote uuid not found");
630 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
631 __rfcomm_client_connected_cb(cb_data, dev_address, result);
639 GSList *rfcomm_clients;
646 } rfcomm_client_conn_info_t;
648 static gboolean __is_error_by_disconnect(GError *err)
650 return !g_strcmp0(err->message, "Connection reset by peer") ||
651 !g_strcmp0(err->message, "Connection timed out") ||
652 !g_strcmp0(err->message, "Software caused connection abort");
655 static rfcomm_client_conn_info_t *__find_rfcomm_conn_info_with_fd(int fd)
661 for (l = rfcomm_clients; l != NULL; l = l->next) {
662 rfcomm_client_conn_info_t *info = l->data;
664 if (info && info->sock_fd == fd) {
665 BT_INFO("Match found");
674 static void __rfcomm_remove_client_conn_info_t(rfcomm_client_conn_info_t *info)
676 ret_if(info == NULL);
678 rfcomm_clients = g_slist_remove(rfcomm_clients, info);
680 g_free(info->remote_addr);
683 static void __bt_rfcomm_client_disconnected(rfcomm_client_conn_info_t *conn_info)
686 bluetooth_rfcomm_disconnection_t disconn_info;
687 bt_event_info_t *event_info = NULL;
689 ret_if(conn_info == NULL);
691 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
692 ret_if(event_info == NULL);
694 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
695 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
696 disconn_info.socket_fd = conn_info->sock_fd;
697 g_strlcpy(disconn_info.uuid, conn_info->uuid, BLUETOOTH_UUID_STRING_MAX);
698 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
699 conn_info->remote_addr);
701 BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
702 BLUETOOTH_ERROR_NONE, conn_info->remote_addr,
703 conn_info->uuid, conn_info->sock_fd);
704 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
705 BLUETOOTH_ERROR_NONE, &disconn_info,
706 event_info->cb, event_info->user_data);
711 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
713 bt_event_info_t *event_info;
714 bluetooth_rfcomm_received_data_t data_r;
715 rfcomm_client_conn_info_t *conn_info;
721 GIOStatus status = G_IO_STATUS_NORMAL;
725 fd = g_io_channel_unix_get_fd(chan);
726 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
727 BT_ERR_C("RFComm Client disconnected: %d", fd);
731 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
732 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
734 if (status != G_IO_STATUS_NORMAL) {
735 BT_ERR("IO Channel read is failed with %d", status);
738 BT_ERR("IO Channel read error [%s]", err->message);
739 if (status == G_IO_STATUS_ERROR &&
740 __is_error_by_disconnect(err)) {
741 BT_ERR("cond : %d", cond);
752 BT_ERR("Length is zero, remote end hang up");
756 BT_DBG("fd: %d, len: %d, buffer: %s", fd, len, buffer);
758 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
759 if (event_info == NULL) {
760 BT_INFO("event_info == NULL");
765 data_r.socket_fd = fd;
766 data_r.buffer_size = len;
767 data_r.buffer = buffer;
769 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
770 BLUETOOTH_ERROR_NONE, &data_r,
771 event_info->cb, event_info->user_data);
777 conn_info = __find_rfcomm_conn_info_with_fd(fd);
779 __bt_rfcomm_client_disconnected(conn_info);
780 __rfcomm_remove_client_conn_info_t(conn_info);
782 BT_ERR("RFCOMM client conn_info not found");
787 static void __rfcomm_client_connection_create_watch(rfcomm_client_conn_info_t *conn_info)
791 ret_if(NULL == conn_info);
795 data_io = g_io_channel_unix_new(conn_info->sock_fd);
796 g_io_channel_set_encoding(data_io, NULL, NULL);
797 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
798 conn_info->watch_id = g_io_add_watch(data_io,
799 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
800 __client_data_received_cb, NULL);
801 g_io_channel_unref(data_io);
806 static void __bt_rfcomm_handle_new_client_connection(bluetooth_rfcomm_connection_t *info)
808 rfcomm_client_conn_info_t *conn_info;
810 ret_if(NULL == info);
814 conn_info = g_malloc0(sizeof(rfcomm_client_conn_info_t));
815 conn_info->remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
816 _bt_convert_addr_type_to_string(
817 conn_info->remote_addr, info->device_addr.addr);
818 conn_info->uuid = g_strdup(info->uuid);
819 conn_info->sock_fd = info->socket_fd;
821 BT_DBG("Address:%s, UUID:%s Socket: %d",
822 conn_info->remote_addr, conn_info->uuid, conn_info->sock_fd);
824 rfcomm_clients = g_slist_append(rfcomm_clients, conn_info);
825 __rfcomm_client_connection_create_watch(conn_info);
830 static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
835 size = g_variant_get_size(var);
837 data = (char *)g_variant_get_data(var);
839 param = g_array_append_vals(param, data, size);
845 /* TODO_40 : 4.0 merge */
846 /* Don't use this function directly. Instead of it, get the out parameter only */
847 static void __bt_get_event_info(int service_function, GArray *output,
848 int *event, int *event_type, void **param_data)
850 ret_if(event == NULL);
852 BT_DBG("service_function : %s (0x%x)",
853 _bt_convert_service_function_to_string(service_function),
855 switch (service_function) {
856 case BT_RFCOMM_CLIENT_CONNECT:
857 *event_type = BT_RFCOMM_CLIENT_EVENT;
858 *event = BLUETOOTH_EVENT_RFCOMM_CONNECTED;
859 ret_if(output == NULL);
860 *param_data = &g_array_index(output,
861 bluetooth_rfcomm_connection_t, 0);
864 BT_ERR("Unknown function");
870 static void __async_req_cb_with_unix_fd_list(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data)
872 int result = BLUETOOTH_ERROR_NONE;
873 int event_type = BT_ADAPTER_EVENT;
874 bt_req_info_t *cb_data = user_data;
875 bluetooth_event_param_t bt_event;
877 GError *error = NULL;
880 GArray *out_param1 = NULL;
881 GUnixFDList *out_fd_list = NULL;
885 memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
887 value = g_dbus_proxy_call_with_unix_fd_list_finish(proxy, &out_fd_list, res, &error);
890 /* dBUS gives error cause */
891 BT_ERR("D-Bus API failure: message[%s]",
893 g_clear_error(&error);
895 result = BLUETOOTH_ERROR_TIMEOUT;
897 ret_if(cb_data == NULL);
899 __bt_get_event_info(cb_data->service_function, NULL,
900 &bt_event.event, &event_type,
901 &bt_event.param_data);
905 g_variant_get(value, "(iv)", &result, ¶m1);
906 g_variant_unref(value);
909 out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
910 __bt_fill_garray_from_variant(param1, out_param1);
911 g_variant_unref(param1);
917 __bt_get_event_info(cb_data->service_function, out_param1,
918 &bt_event.event, &event_type,
919 &bt_event.param_data);
921 if (result == BLUETOOTH_ERROR_NONE && out_param1) {
922 if (BT_RFCOMM_CLIENT_CONNECT == cb_data->service_function) {
925 bluetooth_rfcomm_connection_t *conn_info;
927 conn_info = (bluetooth_rfcomm_connection_t *)bt_event.param_data;
929 BT_ERR("out_fd_list is NULL");
933 fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
934 BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
935 conn_info->socket_fd = fd_list_array[0];
937 BT_DBG("conn_info->socket_fd: %d", conn_info->socket_fd);
938 __bt_rfcomm_handle_new_client_connection(conn_info);
940 if (cb_data->cb != NULL) {
941 /* Send client connected event */
942 bt_event.result = result;
943 BT_INFO("event_type[%d], result=[%d]", event_type, result);
944 ((bluetooth_cb_func_ptr)cb_data->cb)(
945 bt_event.event, &bt_event, cb_data->user_data);
948 g_free(fd_list_array);
949 g_object_unref(out_fd_list);
955 if (cb_data->cb == NULL)
958 /* Only if fail case, call the callback function*/
959 bt_event.result = result;
961 BT_INFO("event_type[%d], result=[%d]", event_type, result);
962 if (event_type == BT_RFCOMM_CLIENT_EVENT) {
963 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
964 &bt_event, cb_data->user_data);
966 BT_INFO("Not handled event type : %d", event_type);
970 g_array_free(out_param1, TRUE);
977 BT_EXPORT_API int bluetooth_rfcomm_connect(
978 const bluetooth_device_address_t *remote_bt_address,
979 const char *remote_uuid)
982 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
983 rfcomm_cb_data_t *cb_data = NULL;
984 rfcomm_conn_info_t *conn = NULL;
988 bt_user_info_t *user_info;
989 char uuid[BLUETOOTH_UUID_STRING_MAX];
991 BT_CHECK_PARAMETER(remote_bt_address, return);
992 BT_CHECK_PARAMETER(remote_uuid, return);
993 BT_CHECK_ENABLED(return);
995 #ifdef TIZEN_FEATURE_BT_DPM
996 if (_bt_check_dpm(BT_DPM_ADDRESS, (void *)remote_bt_address) == BT_DPM_RESTRICTED) {
997 BT_ERR("Blacklist device");
998 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1001 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED ||
1002 _bt_check_dpm(BT_DPM_HF_ONLY, NULL) == BT_DPM_RESTRICTED) {
1003 BT_ERR("Not allow to connect the RFCOMM service");
1004 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1007 if (_bt_check_dpm(BT_DPM_DESKTOP, NULL) == BT_DPM_RESTRICTED) {
1008 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1009 bluetooth_device_class_t dev_class;
1011 _bt_convert_addr_type_to_string(address, (unsigned char *)remote_bt_address->addr);
1012 _bt_get_cod_by_address(address, &dev_class);
1014 if (dev_class.major_class == BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
1015 BT_ERR("Reject a authorization due to MDM Policy");
1016 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1021 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1022 BT_INFO_C("### Connect RFCOMM");
1027 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CLIENT_CONNECT)
1028 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1029 BT_ERR("Don't have a privilege to use this API");
1030 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1033 id = __rfcomm_assign_id();
1035 return BLUETOOTH_ERROR_INTERNAL;
1037 cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
1039 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
1041 object_id = _bt_register_new_conn(path, new_connection);
1042 if (object_id < 0) {
1043 __rfcomm_delete_id(id);
1045 return BLUETOOTH_ERROR_INTERNAL;
1048 cb_data = g_new0(rfcomm_cb_data_t, 1);
1049 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
1050 cb_data->obj_path = path;
1051 cb_data->object_id = object_id;
1055 conn = g_new0(rfcomm_conn_info_t, 1);
1057 _bt_convert_addr_type_to_string(conn->bt_addr,
1058 (unsigned char *)remote_bt_address->addr);
1060 BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
1061 cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
1063 ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
1064 __bt_discover_service_response_cb, cb_data);
1065 if (ret != BLUETOOTH_ERROR_NONE) {
1066 BT_ERR("Error returned while service discovery");
1067 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
1068 if (cb_data->rfcomm_conns == NULL)
1069 rfcomm_cb_data_remove(cb_data);
1070 return BLUETOOTH_ERROR_INTERNAL;
1073 if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
1074 BT_INFO("Adding callback information to rfcomm_clients");
1075 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
1077 BT_INFO("Callback information is already added");
1079 return BLUETOOTH_ERROR_NONE;
1081 user_info = _bt_get_user_data(BT_COMMON);
1082 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
1084 /* connect_type: BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
1085 /* In now, we only support to connecty using UUID */
1086 connect_type = BT_RFCOMM_UUID;
1088 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CLIENT_CONNECT)
1089 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1090 BT_ERR("Don't have a privilege to use this API");
1091 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1095 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1097 g_array_append_vals(in_param1, remote_bt_address,
1098 sizeof(bluetooth_device_address_t));
1100 g_strlcpy(uuid, remote_uuid, sizeof(uuid));
1101 g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
1103 g_array_append_vals(in_param3, &connect_type, sizeof(int));
1105 result = _bt_send_request_async_with_unix_fd_list(BT_BLUEZ_SERVICE,
1106 BT_RFCOMM_CLIENT_CONNECT,
1107 in_param1, in_param2,
1108 in_param3, in_param4,
1109 user_info->cb, user_info->user_data,
1110 NULL, (GAsyncReadyCallback)__async_req_cb_with_unix_fd_list);
1112 BT_DBG("result: %x", result);
1114 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1120 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
1122 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1124 GSList *conn_list = NULL;
1125 rfcomm_cb_data_t *client_info;
1126 rfcomm_conn_info_t *conn_info;
1127 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1129 BT_CHECK_PARAMETER(device_address, return);
1130 BT_CHECK_PARAMETER(connected, return);
1132 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
1135 for (l = rfcomm_clients; l != NULL; l = l->next) {
1136 client_info = l->data;
1137 if (client_info == NULL)
1139 for (conn_list = client_info->rfcomm_conns;
1140 conn_list != NULL; conn_list = conn_list->next) {
1141 conn_info = conn_list->data;
1142 if (conn_info == NULL)
1145 if (g_strcmp0(address, conn_info->bt_addr) == 0) {
1147 return BLUETOOTH_ERROR_NONE;
1152 return BLUETOOTH_ERROR_NONE;
1155 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1157 BT_CHECK_PARAMETER(device_address, return);
1158 BT_CHECK_PARAMETER(connected, return);
1163 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
1164 BT_INFO("Client address: [%s]", address);
1166 for (l = rfcomm_clients; l != NULL; l = l->next) {
1167 rfcomm_client_conn_info_t *info = l->data;
1169 if (info && !strncasecmp(info->remote_addr, address, BT_ADDRESS_STRING_SIZE)) {
1170 BT_INFO("Match found");
1172 return BLUETOOTH_ERROR_NONE;
1177 return BLUETOOTH_ERROR_NONE;
1181 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
1184 int connected = FALSE;
1186 BT_CHECK_ENABLED(return);
1189 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1191 result = _bt_send_request(BT_BLUEZ_SERVICE,
1192 BT_RFCOMM_CLIENT_IS_CONNECTED,
1193 in_param1, in_param2, in_param3,
1194 in_param4, &out_param);
1196 BT_DBG("result: %x", result);
1198 if (result == BLUETOOTH_ERROR_NONE) {
1199 connected = g_array_index(out_param,
1202 BT_ERR("Fail to send request");
1205 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1210 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
1212 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1213 rfcomm_cb_data_t *info;
1214 rfcomm_conn_info_t *conn_info;
1216 BT_INFO_C("### Disconnect RFCOMM");
1218 BT_CHECK_ENABLED(return);
1220 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_DISCONNECT)
1221 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1222 BT_ERR("Don't have a privilege to use this API");
1223 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1226 BT_DBG("Requested FD %d", socket_fd);
1227 if (socket_fd < 0) {
1228 BT_ERR("Invalid FD");
1229 return BLUETOOTH_ERROR_INVALID_PARAM;
1231 BT_DBG("FDD %d", socket_fd);
1233 info = __find_rfcomm_info_with_fd(socket_fd);
1235 BT_DBG("Could not find in client, so check in server");
1236 return bluetooth_rfcomm_server_disconnect(socket_fd);
1239 conn_info = __get_conn_info_from_fd(info, socket_fd);
1240 if (conn_info == NULL) {
1241 BT_ERR("Could not find connection info");
1242 return BLUETOOTH_ERROR_INTERNAL;
1245 if (conn_info->watch_id == 0 || conn_info->disconnected) {
1246 BT_ERR("Invalid state");
1247 return BLUETOOTH_ERROR_NOT_CONNECTED;
1250 close(conn_info->fd);
1251 conn_info->disconnected = TRUE;
1253 BT_INFO("conn_info %s", conn_info->bt_addr);
1254 _bt_disconnect_ext_profile(conn_info->bt_addr, info->obj_path);
1256 if (info->idle_id == 0)
1257 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
1259 return BLUETOOTH_ERROR_NONE;
1261 rfcomm_client_conn_info_t *conn_info;
1263 BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
1265 BT_CHECK_ENABLED(return);
1266 retv_if(socket_fd < 0, BLUETOOTH_ERROR_INVALID_PARAM);
1268 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_DISCONNECT)
1269 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1270 BT_ERR("Don't have a privilege to use this API");
1271 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1274 BT_DBG("FD %d", socket_fd);
1276 conn_info = __find_rfcomm_conn_info_with_fd(socket_fd);
1277 if (conn_info == NULL) {
1278 BT_DBG("Could not find in client, so check in server");
1279 /* Check for fd in server list and perform the disconnection if present */
1280 return bluetooth_rfcomm_server_disconnect(socket_fd);
1283 if (conn_info->watch_id <= 0) {
1284 BT_ERR("Invalid state");
1285 return BLUETOOTH_ERROR_NOT_CONNECTED;
1289 * Just close socket here and return. Socket close will be detected via I/O watch
1290 * and disconnection event as well as info cleanup will be performed there.
1292 close(conn_info->sock_fd);
1294 return BLUETOOTH_ERROR_NONE;
1298 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1300 static int __write_all(int fd, const char *buf, int len)
1302 int sent = 0, try = 0;
1308 written = write(fd, buf, len);
1309 BT_DBG("written: %d", written);
1311 if (errno == EINTR || errno == EAGAIN) {
1333 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
1335 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1340 #ifndef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1341 BT_CHECK_ENABLED(return);
1343 BT_CHECK_PARAMETER(buf, return);
1345 BT_ERR("Invalid FD");
1346 return BLUETOOTH_ERROR_INVALID_PARAM;
1349 retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
1351 switch (privilege_token) {
1353 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_WRITE);
1355 if (result == BLUETOOTH_ERROR_NONE) {
1356 privilege_token = 1; /* Have a permission */
1357 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1358 BT_ERR("Don't have a privilege to use this API");
1359 privilege_token = -1; /* Don't have a permission */
1360 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1362 /* Just break - It is not related with permission error */
1366 /* Already have a privilege */
1369 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1371 /* Invalid privilge token value */
1372 return BLUETOOTH_ERROR_INTERNAL;
1375 if (bluetooth_get_battery_monitor_state()) {
1376 int ret = _bt_common_send_rfcomm_tx_details(length);
1377 if (ret != BLUETOOTH_ERROR_NONE)
1378 BT_ERR("RFCOMM tx data could not be sent");
1381 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1382 written = write(fd, buf, length);
1383 /*BT_DBG("Length %d, written = %d, balance(%d)",
1384 length, written, length - written); */
1387 result = __write_all(fd, buf, length);