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 #define BATTERY_MONITOR_RFCOMM_INTERVAL 5
54 static GSList *rfcomm_clients;
57 char bt_addr[BT_ADDRESS_STRING_SIZE];
60 gboolean disconnected;
64 char uuid[BLUETOOTH_UUID_STRING_MAX];
73 static unsigned int rx_data = 0, tx_data = 0;
74 static guint tx_tag = 0, rx_tag = 0;
76 static void __client_connected_cb(rfcomm_cb_data_t *cb_data,
77 char *dev_address, int result);
79 static bool __rfcomm_record_tx_data(void)
81 if (bluetooth_get_battery_monitor_state()) {
83 int ret = _bt_common_send_rfcomm_tx_details(tx_data);
84 if (ret == BLUETOOTH_ERROR_NONE) {
88 BT_ERR("RFCOMM tx data could not be registered");
98 static bool __rfcomm_record_rx_data(void)
100 if (bluetooth_get_battery_monitor_state()) {
102 int ret = _bt_common_send_rfcomm_rx_details(rx_data);
103 if (ret == BLUETOOTH_ERROR_NONE) {
107 BT_ERR("RFCOMM rx data could not be registered");
117 static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
121 if (cb_data->id >= 0)
122 __rfcomm_delete_id(cb_data->id);
124 if (cb_data->object_id > 0)
125 _bt_unregister_gdbus(cb_data->object_id);
127 if (cb_data->obj_path) {
128 BT_INFO("Unregister profile");
129 _bt_unregister_profile(cb_data->obj_path);
132 if (cb_data->idle_id != 0) {
133 BT_INFO("Removing idle source");
134 g_source_remove(cb_data->idle_id);
137 g_free(cb_data->obj_path);
139 g_free(cb_data->device_path);
144 static void rfcomm_cb_data_remove(rfcomm_cb_data_t *info)
147 BT_INFO("No more device connected remove info");
148 rfcomm_clients = g_slist_remove(rfcomm_clients, info);
149 __bt_free_cb_data(info);
153 gint compare(gpointer *a, gpointer *b)
155 rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
156 char *address = (char *)b;
157 return g_strcmp0(node->bt_addr, address);
160 gint compare_fd(gpointer *a, gpointer *b)
162 rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
169 static void __bt_free_conn(rfcomm_conn_info_t *conn)
176 if (conn->watch_id > 0) {
177 g_source_remove(conn->watch_id);
186 static void __rfcomm_remove_conn_info_t(rfcomm_cb_data_t *info, char *address)
189 rfcomm_conn_info_t *conn_info = NULL;
190 l = g_slist_find_custom(info->rfcomm_conns, address, (GCompareFunc)compare);
194 info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn_info);
195 __bt_free_conn(conn_info);
199 static rfcomm_conn_info_t *__get_conn_info_from_fd(rfcomm_cb_data_t *info,
203 rfcomm_conn_info_t *device_node = NULL;
204 for (l = info->rfcomm_conns; l != NULL; l = l->next) {
205 device_node = l->data;
206 if (device_node && device_node->fd == fd)
212 static rfcomm_conn_info_t *__get_conn_info_from_address(rfcomm_cb_data_t *info,
216 rfcomm_conn_info_t *conn_info = NULL;
217 l = g_slist_find_custom(info->rfcomm_conns, dev_address,
218 (GCompareFunc)compare);
224 static void __rfcomm_client_connected_cb(rfcomm_cb_data_t *info,
225 char *dev_address, int result)
227 if (g_slist_find(rfcomm_clients, info) == NULL) {
228 BT_INFO("rfcomm resource is already freed");
232 __client_connected_cb(info, dev_address, result);
233 __rfcomm_remove_conn_info_t(info, dev_address);
235 if (info->rfcomm_conns == NULL)
236 rfcomm_cb_data_remove(info);
239 static rfcomm_cb_data_t *__find_rfcomm_info_with_fd(int fd)
243 for (l = rfcomm_clients; l != NULL; l = l->next) {
244 rfcomm_cb_data_t *info = l->data;
245 device_fd = g_slist_find_custom(info->rfcomm_conns, &fd,
246 (GCompareFunc)compare_fd);
254 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
258 for (l = rfcomm_clients; l != NULL; l = l->next) {
259 rfcomm_cb_data_t *info = l->data;
262 if (g_strcmp0(info->obj_path, path) == 0)
269 static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
273 for (l = rfcomm_clients; l != NULL; l = l->next) {
274 rfcomm_cb_data_t *info = l->data;
276 if (g_strcmp0(info->uuid, uuid) == 0)
283 static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
284 rfcomm_cb_data_t *info)
286 if (conn_info == NULL)
289 bluetooth_rfcomm_disconnection_t disconn_info;
290 bt_event_info_t *event_info = NULL;
292 if (conn_info->disconnected == FALSE)
295 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
296 if (event_info == NULL) {
297 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
299 if (info->rfcomm_conns == NULL)
300 rfcomm_cb_data_remove(info);
304 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
305 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
306 g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
307 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
310 BT_DBG("Disconnected FD [%d]", conn_info->fd);
311 disconn_info.socket_fd = conn_info->fd;
313 BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
314 BLUETOOTH_ERROR_NONE, conn_info->bt_addr,
315 info->uuid, conn_info->fd);
316 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
317 BLUETOOTH_ERROR_NONE, &disconn_info,
318 event_info->cb, event_info->user_data);
320 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
322 if (info->rfcomm_conns == NULL)
323 rfcomm_cb_data_remove(info);
328 static gboolean __rfcomm_client_disconnect(gpointer user_data)
330 rfcomm_cb_data_t *info = (rfcomm_cb_data_t *)user_data;
332 BT_INFO_C("### Disconnected [RFCOMM Client]");
334 retv_if(info == NULL, FALSE);
336 if (g_slist_find(rfcomm_clients, info) == NULL) {
337 BT_INFO("rfcomm resource is already freed");
342 g_slist_foreach(info->rfcomm_conns,
343 (GFunc)_bt_rfcomm_disconnect_conn_info, info);
349 static gboolean __is_error_by_disconnect(GError *err)
351 return !g_strcmp0(err->message, "Connection reset by peer") ||
352 !g_strcmp0(err->message, "Connection timed out") ||
353 !g_strcmp0(err->message, "Software caused connection abort");
356 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
361 int result = BLUETOOTH_ERROR_NONE;
362 rfcomm_cb_data_t *info = data;
363 rfcomm_conn_info_t *conn_info = NULL;
364 bt_event_info_t *event_info;
365 bluetooth_rfcomm_received_data_t data_r;
366 GIOStatus status = G_IO_STATUS_NORMAL;
371 retv_if(info == NULL, FALSE);
372 fd = g_io_channel_unix_get_fd(chan);
373 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
374 BT_ERR_C("RFComm Client disconnected: %d", fd);
376 conn_info = __get_conn_info_from_fd(info, fd);
377 if (conn_info == NULL) {
378 BT_ERR("No Connection info found with FD [%d]", fd);
382 if (conn_info->disconnected == FALSE) {
383 close(conn_info->fd);
384 conn_info->disconnected = TRUE;
386 __rfcomm_client_disconnect(info);
390 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
392 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
394 if (status != G_IO_STATUS_NORMAL) {
395 BT_ERR("IO Channel read is failed with %d", status);
398 if (err || status == G_IO_STATUS_EOF) {
400 BT_ERR("IO Channel read error [%s]", err->message);
401 if ((status == G_IO_STATUS_EOF) ||
402 (status == G_IO_STATUS_ERROR &&
403 __is_error_by_disconnect(err))) {
405 BT_ERR("cond : %d", cond);
409 conn_info = __get_conn_info_from_fd(info, fd);
410 if (conn_info == NULL) {
411 BT_ERR("No Connection info found with FD [%d]", fd);
415 if (conn_info->disconnected == FALSE) {
416 close(conn_info->fd);
417 conn_info->disconnected = TRUE;
419 __rfcomm_client_disconnect(info);
428 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
429 if (event_info == NULL) {
434 data_r.socket_fd = fd;
435 data_r.buffer_size = len;
436 data_r.buffer = buffer;
438 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
440 event_info->cb, event_info->user_data);
442 if (bluetooth_get_battery_monitor_state()) {
444 BT_INFO("Adding rfcomm rx timeout function for battery monitor");
445 rx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_rx_data, NULL);
455 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
458 bluetooth_rfcomm_connection_t conn_info;
459 bt_event_info_t *event_info;
460 rfcomm_conn_info_t *conn_list_info = NULL;
462 if (result == BLUETOOTH_ERROR_NONE)
463 BT_INFO_C("### Connected [RFCOMM Client]");
465 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
466 if (event_info == NULL)
469 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
470 conn_info.device_role = RFCOMM_ROLE_CLIENT;
471 g_strlcpy(conn_info.uuid, cb_data->uuid, BLUETOOTH_UUID_STRING_MAX);
472 _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
474 conn_list_info = __get_conn_info_from_address(cb_data, dev_address);
475 if (conn_list_info == NULL) {
476 BT_ERR("Device addres %s not found in connection list", dev_address);
479 conn_info.socket_fd = conn_list_info->fd;
480 conn_info.server_id = -1;
482 BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
483 result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
484 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
486 event_info->cb, event_info->user_data);
490 void _bt_rfcomm_client_disconnect_all(void)
495 BT_INFO_C("### Disconnect all RFCOMM client connections");
497 for (client = rfcomm_clients; client; ) {
498 rfcomm_cb_data_t *info = client->data;
500 for (conn = info->rfcomm_conns; conn; conn = conn->next) {
501 rfcomm_conn_info_t *conn_info = conn->data;
503 if (conn_info == NULL)
506 if (conn_info->watch_id == 0 || conn_info->disconnected)
509 close(conn_info->fd);
510 conn_info->disconnected = TRUE;
512 _bt_disconnect_ext_profile(conn_info->bt_addr,
516 client = client->next;
517 __rfcomm_client_disconnect(info);
523 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
525 rfcomm_cb_data_t *info;
527 rfcomm_conn_info_t *conn_info = NULL;
528 char address[BT_ADDRESS_STRING_SIZE];
530 BT_INFO("%s %d", path, fd);
532 _bt_convert_addr_type_to_string(address,
533 (unsigned char *)addr);
535 info = __find_rfcomm_info_from_path(path);
537 BT_ERR("rfcomm info is NULL");
541 conn_info = __get_conn_info_from_address(info, address);
542 if (conn_info == NULL) {
543 BT_ERR("connection info is NULL");
549 data_io = g_io_channel_unix_new(fd);
551 g_io_channel_set_encoding(data_io, NULL, NULL);
552 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
554 conn_info->watch_id = g_io_add_watch(data_io,
555 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
556 __client_data_received_cb, info);
558 g_io_channel_unref(data_io);
560 __client_connected_cb(info, address, BLUETOOTH_ERROR_NONE);
565 static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
569 GError *error = NULL;
571 rfcomm_cb_data_t *cb_data;
572 char dev_address[BT_ADDRESS_STRING_SIZE];
576 ret_if(user_data == NULL);
580 value = g_dbus_proxy_call_finish(proxy, res, &error);
583 g_dbus_error_strip_remote_error(error);
584 BT_ERR("Error : %s \n", error->message);
586 if (g_strcmp0(error->message, "In Progress") == 0)
587 result = BLUETOOTH_ERROR_DEVICE_BUSY;
589 result = BLUETOOTH_ERROR_INTERNAL;
590 path = g_dbus_proxy_get_object_path(proxy);
591 _bt_convert_device_path_to_address(path, dev_address);
592 __rfcomm_client_connected_cb(cb_data, dev_address, result);
596 g_variant_unref(value);
600 g_object_unref(proxy);
605 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
606 GAsyncResult *res, gpointer user_data)
608 rfcomm_cb_data_t *cb_data;
612 bt_register_profile_info_t info = {0};
613 int result = BLUETOOTH_ERROR_NONE;
614 char dev_address[BT_ADDRESS_STRING_SIZE];
619 ret_if(user_data == NULL);
623 path = g_dbus_proxy_get_object_path(proxy);
625 _bt_convert_device_path_to_address(path, dev_address);
626 BT_DBG("Device Adress [%s]", dev_address);
627 value = g_dbus_proxy_call_finish(proxy, res, &err);
629 g_object_unref(proxy);
631 g_variant_unref(value);
634 g_dbus_error_strip_remote_error(err);
635 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
636 if (!strcmp("Operation canceled", err->message)) {
637 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
638 } else if (!strcmp("In Progress", err->message)) {
639 result = BLUETOOTH_ERROR_IN_PROGRESS;
640 } else if (!strcmp("Host is down", err->message)) {
641 result = BLUETOOTH_ERROR_HOST_DOWN;
642 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
643 result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
644 ret = _bt_cancel_discovers(dev_address);
645 if (ret != BLUETOOTH_ERROR_NONE)
646 BT_ERR("Error: While CancelDiscovery");
648 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
650 __rfcomm_client_connected_cb(cb_data, dev_address, result);
653 BT_INFO("Services are Updated checking required uuid is there");
654 /* Check here for uuid present */
655 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
656 if (ret == BLUETOOTH_ERROR_NONE) {
657 info.uuid = (char *)cb_data->uuid;
658 info.obj_path = cb_data->obj_path;
659 info.role = "client";
661 ret = _bt_register_profile(&info, FALSE);
663 BT_DBG("Error: register profile");
664 ret = _bt_connect_profile(dev_address, cb_data->uuid,
665 __bt_connect_response_cb, cb_data);
667 if (ret != BLUETOOTH_ERROR_NONE) {
668 BT_ERR("ConnectProfile failed");
669 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
670 __rfcomm_client_connected_cb(cb_data, dev_address, result);
674 BT_ERR("remote uuid not found");
675 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
676 __rfcomm_client_connected_cb(cb_data, dev_address, result);
684 GSList *rfcomm_clients;
691 } rfcomm_client_conn_info_t;
693 static gboolean __is_error_by_disconnect(GError *err)
695 return !g_strcmp0(err->message, "Connection reset by peer") ||
696 !g_strcmp0(err->message, "Connection timed out") ||
697 !g_strcmp0(err->message, "Software caused connection abort");
700 static rfcomm_client_conn_info_t *__find_rfcomm_conn_info_with_fd(int fd)
706 for (l = rfcomm_clients; l != NULL; l = l->next) {
707 rfcomm_client_conn_info_t *info = l->data;
709 if (info && info->sock_fd == fd) {
710 BT_INFO("Match found");
719 static void __rfcomm_remove_client_conn_info_t(rfcomm_client_conn_info_t *info)
721 ret_if(info == NULL);
723 rfcomm_clients = g_slist_remove(rfcomm_clients, info);
725 g_free(info->remote_addr);
728 static void __bt_rfcomm_client_disconnected(rfcomm_client_conn_info_t *conn_info)
731 bluetooth_rfcomm_disconnection_t disconn_info;
732 bt_event_info_t *event_info = NULL;
734 ret_if(conn_info == NULL);
736 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
737 ret_if(event_info == NULL);
739 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
740 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
741 disconn_info.socket_fd = conn_info->sock_fd;
742 g_strlcpy(disconn_info.uuid, conn_info->uuid, BLUETOOTH_UUID_STRING_MAX);
743 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
744 conn_info->remote_addr);
746 BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
747 BLUETOOTH_ERROR_NONE, conn_info->remote_addr,
748 conn_info->uuid, conn_info->sock_fd);
749 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
750 BLUETOOTH_ERROR_NONE, &disconn_info,
751 event_info->cb, event_info->user_data);
756 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
758 bt_event_info_t *event_info;
759 bluetooth_rfcomm_received_data_t data_r;
760 rfcomm_client_conn_info_t *conn_info;
766 GIOStatus status = G_IO_STATUS_NORMAL;
770 fd = g_io_channel_unix_get_fd(chan);
771 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
772 BT_ERR_C("RFComm Client disconnected: %d", fd);
776 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
777 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
779 if (status != G_IO_STATUS_NORMAL) {
780 BT_ERR("IO Channel read is failed with %d", status);
783 BT_ERR("IO Channel read error [%s]", err->message);
784 if (status == G_IO_STATUS_ERROR &&
785 __is_error_by_disconnect(err)) {
786 BT_ERR("cond : %d", cond);
797 BT_ERR("Length is zero, remote end hang up");
801 BT_DBG("fd: %d, len: %d, buffer: %s", fd, len, buffer);
803 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
804 if (event_info == NULL) {
805 BT_INFO("event_info == NULL");
810 data_r.socket_fd = fd;
811 data_r.buffer_size = len;
812 data_r.buffer = buffer;
814 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
815 BLUETOOTH_ERROR_NONE, &data_r,
816 event_info->cb, event_info->user_data);
822 conn_info = __find_rfcomm_conn_info_with_fd(fd);
824 __bt_rfcomm_client_disconnected(conn_info);
825 __rfcomm_remove_client_conn_info_t(conn_info);
827 BT_ERR("RFCOMM client conn_info not found");
832 static void __rfcomm_client_connection_create_watch(rfcomm_client_conn_info_t *conn_info)
836 ret_if(NULL == conn_info);
840 data_io = g_io_channel_unix_new(conn_info->sock_fd);
841 g_io_channel_set_encoding(data_io, NULL, NULL);
842 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
843 conn_info->watch_id = g_io_add_watch(data_io,
844 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
845 __client_data_received_cb, NULL);
846 g_io_channel_unref(data_io);
851 static void __bt_rfcomm_handle_new_client_connection(bluetooth_rfcomm_connection_t *info)
853 rfcomm_client_conn_info_t *conn_info;
855 ret_if(NULL == info);
859 conn_info = g_malloc0(sizeof(rfcomm_client_conn_info_t));
860 conn_info->remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
861 _bt_convert_addr_type_to_string(
862 conn_info->remote_addr, info->device_addr.addr);
863 conn_info->uuid = g_strdup(info->uuid);
864 conn_info->sock_fd = info->socket_fd;
866 BT_DBG("Address:%s, UUID:%s Socket: %d",
867 conn_info->remote_addr, conn_info->uuid, conn_info->sock_fd);
869 rfcomm_clients = g_slist_append(rfcomm_clients, conn_info);
870 __rfcomm_client_connection_create_watch(conn_info);
875 static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
880 size = g_variant_get_size(var);
882 data = (char *)g_variant_get_data(var);
884 param = g_array_append_vals(param, data, size);
890 /* TODO_40 : 4.0 merge */
891 /* Don't use this function directly. Instead of it, get the out parameter only */
892 static void __bt_get_event_info(int service_function, GArray *output,
893 int *event, int *event_type, void **param_data)
895 ret_if(event == NULL);
897 BT_DBG("service_function : %s (0x%x)",
898 _bt_convert_service_function_to_string(service_function),
900 switch (service_function) {
901 case BT_RFCOMM_CLIENT_CONNECT:
902 *event_type = BT_RFCOMM_CLIENT_EVENT;
903 *event = BLUETOOTH_EVENT_RFCOMM_CONNECTED;
904 ret_if(output == NULL);
905 *param_data = &g_array_index(output,
906 bluetooth_rfcomm_connection_t, 0);
909 BT_ERR("Unknown function");
915 static void __async_req_cb_with_unix_fd_list(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data)
917 int result = BLUETOOTH_ERROR_NONE;
918 int event_type = BT_ADAPTER_EVENT;
919 bt_req_info_t *cb_data = user_data;
920 bluetooth_event_param_t bt_event;
922 GError *error = NULL;
925 GArray *out_param1 = NULL;
926 GUnixFDList *out_fd_list = NULL;
930 memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
932 value = g_dbus_proxy_call_with_unix_fd_list_finish(proxy, &out_fd_list, res, &error);
935 /* dBUS gives error cause */
936 BT_ERR("D-Bus API failure: message[%s]",
938 g_clear_error(&error);
940 result = BLUETOOTH_ERROR_TIMEOUT;
942 ret_if(cb_data == NULL);
944 __bt_get_event_info(cb_data->service_function, NULL,
945 &bt_event.event, &event_type,
946 &bt_event.param_data);
950 g_variant_get(value, "(iv)", &result, ¶m1);
951 g_variant_unref(value);
954 out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
955 __bt_fill_garray_from_variant(param1, out_param1);
956 g_variant_unref(param1);
962 __bt_get_event_info(cb_data->service_function, out_param1,
963 &bt_event.event, &event_type,
964 &bt_event.param_data);
966 if (result == BLUETOOTH_ERROR_NONE && out_param1) {
967 if (BT_RFCOMM_CLIENT_CONNECT == cb_data->service_function) {
970 bluetooth_rfcomm_connection_t *conn_info;
972 conn_info = (bluetooth_rfcomm_connection_t *)bt_event.param_data;
974 BT_ERR("out_fd_list is NULL");
978 fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
979 BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
980 conn_info->socket_fd = fd_list_array[0];
982 BT_DBG("conn_info->socket_fd: %d", conn_info->socket_fd);
983 __bt_rfcomm_handle_new_client_connection(conn_info);
985 if (cb_data->cb != NULL) {
986 /* Send client connected event */
987 bt_event.result = result;
988 BT_INFO("event_type[%d], result=[%d]", event_type, result);
989 ((bluetooth_cb_func_ptr)cb_data->cb)(
990 bt_event.event, &bt_event, cb_data->user_data);
993 g_free(fd_list_array);
994 g_object_unref(out_fd_list);
1000 if (cb_data->cb == NULL)
1003 /* Only if fail case, call the callback function*/
1004 bt_event.result = result;
1006 BT_INFO("event_type[%d], result=[%d]", event_type, result);
1007 if (event_type == BT_RFCOMM_CLIENT_EVENT) {
1008 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
1009 &bt_event, cb_data->user_data);
1011 BT_INFO("Not handled event type : %d", event_type);
1015 g_array_free(out_param1, TRUE);
1022 BT_EXPORT_API int bluetooth_rfcomm_connect(
1023 const bluetooth_device_address_t *remote_bt_address,
1024 const char *remote_uuid)
1027 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1028 rfcomm_cb_data_t *cb_data = NULL;
1029 rfcomm_conn_info_t *conn = NULL;
1033 bt_user_info_t *user_info;
1034 char uuid[BLUETOOTH_UUID_STRING_MAX];
1036 BT_CHECK_PARAMETER(remote_bt_address, return);
1037 BT_CHECK_PARAMETER(remote_uuid, return);
1038 BT_CHECK_ENABLED(return);
1040 #ifdef TIZEN_FEATURE_BT_DPM
1041 if (_bt_check_dpm(BT_DPM_ADDRESS, (void *)remote_bt_address) == BT_DPM_RESTRICTED) {
1042 BT_ERR("Blacklist device");
1043 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1046 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED ||
1047 _bt_check_dpm(BT_DPM_HF_ONLY, NULL) == BT_DPM_RESTRICTED) {
1048 BT_ERR("Not allow to connect the RFCOMM service");
1049 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1052 if (_bt_check_dpm(BT_DPM_DESKTOP, NULL) == BT_DPM_RESTRICTED) {
1053 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1054 bluetooth_device_class_t dev_class;
1056 _bt_convert_addr_type_to_string(address, (unsigned char *)remote_bt_address->addr);
1057 _bt_get_cod_by_address(address, &dev_class);
1059 if (dev_class.major_class == BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
1060 BT_ERR("Reject a authorization due to MDM Policy");
1061 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1066 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1067 BT_INFO_C("### Connect RFCOMM");
1072 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CLIENT_CONNECT)
1073 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1074 BT_ERR("Don't have a privilege to use this API");
1075 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1078 id = __rfcomm_assign_id();
1080 return BLUETOOTH_ERROR_INTERNAL;
1082 cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
1084 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
1086 object_id = _bt_register_new_conn(path, new_connection);
1087 if (object_id < 0) {
1088 __rfcomm_delete_id(id);
1090 return BLUETOOTH_ERROR_INTERNAL;
1093 cb_data = g_new0(rfcomm_cb_data_t, 1);
1094 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
1095 cb_data->obj_path = path;
1096 cb_data->object_id = object_id;
1100 conn = g_new0(rfcomm_conn_info_t, 1);
1102 _bt_convert_addr_type_to_string(conn->bt_addr,
1103 (unsigned char *)remote_bt_address->addr);
1105 BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
1106 cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
1108 ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
1109 __bt_discover_service_response_cb, cb_data);
1110 if (ret != BLUETOOTH_ERROR_NONE) {
1111 BT_ERR("Error returned while service discovery");
1112 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
1113 if (cb_data->rfcomm_conns == NULL)
1114 rfcomm_cb_data_remove(cb_data);
1115 return BLUETOOTH_ERROR_INTERNAL;
1118 if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
1119 BT_INFO("Adding callback information to rfcomm_clients");
1120 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
1122 BT_INFO("Callback information is already added");
1124 return BLUETOOTH_ERROR_NONE;
1126 user_info = _bt_get_user_data(BT_COMMON);
1127 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
1129 /* connect_type: BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
1130 /* In now, we only support to connecty using UUID */
1131 connect_type = BT_RFCOMM_UUID;
1133 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CLIENT_CONNECT)
1134 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1135 BT_ERR("Don't have a privilege to use this API");
1136 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1140 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1142 g_array_append_vals(in_param1, remote_bt_address,
1143 sizeof(bluetooth_device_address_t));
1145 g_strlcpy(uuid, remote_uuid, sizeof(uuid));
1146 g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
1148 g_array_append_vals(in_param3, &connect_type, sizeof(int));
1150 result = _bt_send_request_async_with_unix_fd_list(BT_BLUEZ_SERVICE,
1151 BT_RFCOMM_CLIENT_CONNECT,
1152 in_param1, in_param2,
1153 in_param3, in_param4,
1154 user_info->cb, user_info->user_data,
1155 NULL, (GAsyncReadyCallback)__async_req_cb_with_unix_fd_list);
1157 BT_DBG("result: %x", result);
1159 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1165 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
1167 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1169 GSList *conn_list = NULL;
1170 rfcomm_cb_data_t *client_info;
1171 rfcomm_conn_info_t *conn_info;
1172 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1174 BT_CHECK_PARAMETER(device_address, return);
1175 BT_CHECK_PARAMETER(connected, return);
1177 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
1180 for (l = rfcomm_clients; l != NULL; l = l->next) {
1181 client_info = l->data;
1182 if (client_info == NULL)
1184 for (conn_list = client_info->rfcomm_conns;
1185 conn_list != NULL; conn_list = conn_list->next) {
1186 conn_info = conn_list->data;
1187 if (conn_info == NULL)
1190 if (g_strcmp0(address, conn_info->bt_addr) == 0) {
1192 return BLUETOOTH_ERROR_NONE;
1197 return BLUETOOTH_ERROR_NONE;
1200 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1202 BT_CHECK_PARAMETER(device_address, return);
1203 BT_CHECK_PARAMETER(connected, return);
1208 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
1209 BT_INFO("Client address: [%s]", address);
1211 for (l = rfcomm_clients; l != NULL; l = l->next) {
1212 rfcomm_client_conn_info_t *info = l->data;
1214 if (info && !strncasecmp(info->remote_addr, address, BT_ADDRESS_STRING_SIZE)) {
1215 BT_INFO("Match found");
1217 return BLUETOOTH_ERROR_NONE;
1222 return BLUETOOTH_ERROR_NONE;
1226 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
1229 int connected = FALSE;
1231 BT_CHECK_ENABLED(return);
1234 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1236 result = _bt_send_request(BT_BLUEZ_SERVICE,
1237 BT_RFCOMM_CLIENT_IS_CONNECTED,
1238 in_param1, in_param2, in_param3,
1239 in_param4, &out_param);
1241 BT_DBG("result: %x", result);
1243 if (result == BLUETOOTH_ERROR_NONE) {
1244 connected = g_array_index(out_param,
1247 BT_ERR("Fail to send request");
1250 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1255 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
1257 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1258 rfcomm_cb_data_t *info;
1259 rfcomm_conn_info_t *conn_info;
1261 BT_INFO_C("### Disconnect RFCOMM");
1263 BT_CHECK_ENABLED(return);
1265 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_DISCONNECT)
1266 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1267 BT_ERR("Don't have a privilege to use this API");
1268 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1271 BT_DBG("Requested FD %d", socket_fd);
1272 if (socket_fd < 0) {
1273 BT_ERR("Invalid FD");
1274 return BLUETOOTH_ERROR_INVALID_PARAM;
1276 BT_DBG("FDD %d", socket_fd);
1278 info = __find_rfcomm_info_with_fd(socket_fd);
1280 BT_DBG("Could not find in client, so check in server");
1281 return bluetooth_rfcomm_server_disconnect(socket_fd);
1284 conn_info = __get_conn_info_from_fd(info, socket_fd);
1285 if (conn_info == NULL) {
1286 BT_ERR("Could not find connection info");
1287 return BLUETOOTH_ERROR_INTERNAL;
1290 if (conn_info->watch_id == 0 || conn_info->disconnected) {
1291 BT_ERR("Invalid state");
1292 return BLUETOOTH_ERROR_NOT_CONNECTED;
1295 close(conn_info->fd);
1296 conn_info->disconnected = TRUE;
1298 BT_INFO("conn_info %s", conn_info->bt_addr);
1299 _bt_disconnect_ext_profile(conn_info->bt_addr, info->obj_path);
1301 if (info->idle_id == 0)
1302 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
1304 return BLUETOOTH_ERROR_NONE;
1306 rfcomm_client_conn_info_t *conn_info;
1308 BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
1310 BT_CHECK_ENABLED(return);
1311 retv_if(socket_fd < 0, BLUETOOTH_ERROR_INVALID_PARAM);
1313 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_DISCONNECT)
1314 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1315 BT_ERR("Don't have a privilege to use this API");
1316 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1319 BT_DBG("FD %d", socket_fd);
1321 conn_info = __find_rfcomm_conn_info_with_fd(socket_fd);
1322 if (conn_info == NULL) {
1323 BT_DBG("Could not find in client, so check in server");
1324 /* Check for fd in server list and perform the disconnection if present */
1325 return bluetooth_rfcomm_server_disconnect(socket_fd);
1328 if (conn_info->watch_id <= 0) {
1329 BT_ERR("Invalid state");
1330 return BLUETOOTH_ERROR_NOT_CONNECTED;
1334 * Just close socket here and return. Socket close will be detected via I/O watch
1335 * and disconnection event as well as info cleanup will be performed there.
1337 close(conn_info->sock_fd);
1339 return BLUETOOTH_ERROR_NONE;
1343 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1345 static int __write_all(int fd, const char *buf, int len)
1347 int sent = 0, try = 0;
1353 written = write(fd, buf, len);
1354 BT_DBG("written: %d", written);
1356 if (errno == EINTR || errno == EAGAIN) {
1378 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
1380 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1385 #ifndef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1386 BT_CHECK_ENABLED(return);
1388 BT_CHECK_PARAMETER(buf, return);
1390 BT_ERR("Invalid FD");
1391 return BLUETOOTH_ERROR_INVALID_PARAM;
1394 retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
1396 switch (privilege_token) {
1398 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_WRITE);
1400 if (result == BLUETOOTH_ERROR_NONE) {
1401 privilege_token = 1; /* Have a permission */
1402 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1403 BT_ERR("Don't have a privilege to use this API");
1404 privilege_token = -1; /* Don't have a permission */
1405 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1407 /* Just break - It is not related with permission error */
1411 /* Already have a privilege */
1414 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1416 /* Invalid privilge token value */
1417 return BLUETOOTH_ERROR_INTERNAL;
1420 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1421 written = write(fd, buf, length);
1422 /*BT_DBG("Length %d, written = %d, balance(%d)",
1423 length, written, length - written); */
1424 if (written > 0 && bluetooth_get_battery_monitor_state()) {
1426 BT_INFO("Adding rfcomm tx timeout function for battery monitor");
1427 tx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_tx_data, NULL);
1433 result = __write_all(fd, buf, length);
1434 if (result > 0 && bluetooth_get_battery_monitor_state()) {
1436 BT_INFO("Adding rfcomm tx timeout function for battery monitor");
1437 tx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_tx_data, NULL);