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"
33 /* Variable for privilege, only for write API,
34 before we should reduce time to bt-service dbus calling
35 -1 : Don't have a permission to access API
36 0 : Initial value, not yet check
37 1 : Have a permission to access API
39 static int privilege_token;
42 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
43 #define BT_TIMEOUT_MESSAGE "Did not receive a reply. Possible causes include: " \
44 "the remote application did not send a reply, " \
45 "the message bus security policy blocked the reply, " \
46 "the reply timeout expired, or the network connection " \
49 #define BATTERY_MONITOR_RFCOMM_INTERVAL 5
51 static GSList *rfcomm_clients;
54 char bt_addr[BT_ADDRESS_STRING_SIZE];
57 gboolean disconnected;
61 char uuid[BLUETOOTH_UUID_STRING_MAX];
70 static unsigned int rx_data = 0, tx_data = 0;
71 static guint tx_tag = 0, rx_tag = 0;
73 static void __client_connected_cb(rfcomm_cb_data_t *cb_data,
74 char *dev_address, int result);
76 static bool __rfcomm_record_tx_data(void)
79 int ret = _bt_common_send_rfcomm_tx_details(tx_data);
80 if (ret == BLUETOOTH_ERROR_NONE) {
84 BT_ERR("RFCOMM tx data could not be registered");
94 static bool __rfcomm_record_rx_data(void)
97 int ret = _bt_common_send_rfcomm_rx_details(rx_data);
98 if (ret == BLUETOOTH_ERROR_NONE) {
102 BT_ERR("RFCOMM rx data could not be registered");
112 static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
116 if (cb_data->id >= 0)
117 __rfcomm_delete_id(cb_data->id);
119 if (cb_data->object_id > 0)
120 _bt_unregister_gdbus(cb_data->object_id);
122 if (cb_data->obj_path) {
123 BT_INFO("Unregister profile");
124 _bt_unregister_profile(cb_data->obj_path);
127 if (cb_data->idle_id != 0) {
128 BT_INFO("Removing idle source");
129 g_source_remove(cb_data->idle_id);
132 g_free(cb_data->obj_path);
134 g_free(cb_data->device_path);
139 static void rfcomm_cb_data_remove(rfcomm_cb_data_t *info)
142 BT_INFO("No more device connected remove info");
143 rfcomm_clients = g_slist_remove(rfcomm_clients, info);
144 __bt_free_cb_data(info);
148 gint compare(gpointer *a, gpointer *b)
150 rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
151 char *address = (char *)b;
152 return g_strcmp0(node->bt_addr, address);
155 gint compare_fd(gpointer *a, gpointer *b)
157 rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
164 static void __bt_free_conn(rfcomm_conn_info_t *conn)
171 if (conn->watch_id > 0) {
172 g_source_remove(conn->watch_id);
181 static void __rfcomm_remove_conn_info_t(rfcomm_cb_data_t *info, char *address)
184 rfcomm_conn_info_t *conn_info = NULL;
185 l = g_slist_find_custom(info->rfcomm_conns, address, (GCompareFunc)compare);
189 info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn_info);
190 __bt_free_conn(conn_info);
194 static rfcomm_conn_info_t *__get_conn_info_from_fd(rfcomm_cb_data_t *info,
198 rfcomm_conn_info_t *device_node = NULL;
199 for (l = info->rfcomm_conns; l != NULL; l = l->next) {
200 device_node = l->data;
201 if (device_node && device_node->fd == fd)
207 static rfcomm_conn_info_t *__get_conn_info_from_address(rfcomm_cb_data_t *info,
211 rfcomm_conn_info_t *conn_info = NULL;
212 l = g_slist_find_custom(info->rfcomm_conns, dev_address,
213 (GCompareFunc)compare);
219 static void __rfcomm_client_connected_cb(rfcomm_cb_data_t *info,
220 char *dev_address, int result)
222 if (g_slist_find(rfcomm_clients, info) == NULL) {
223 BT_INFO("rfcomm resource is already freed");
227 __client_connected_cb(info, dev_address, result);
228 __rfcomm_remove_conn_info_t(info, dev_address);
230 if (info->rfcomm_conns == NULL)
231 rfcomm_cb_data_remove(info);
234 static rfcomm_cb_data_t *__find_rfcomm_info_with_fd(int fd)
238 for (l = rfcomm_clients; l != NULL; l = l->next) {
239 rfcomm_cb_data_t *info = l->data;
240 device_fd = g_slist_find_custom(info->rfcomm_conns, &fd,
241 (GCompareFunc)compare_fd);
249 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
253 for (l = rfcomm_clients; l != NULL; l = l->next) {
254 rfcomm_cb_data_t *info = l->data;
257 if (g_strcmp0(info->obj_path, path) == 0)
264 static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
268 for (l = rfcomm_clients; l != NULL; l = l->next) {
269 rfcomm_cb_data_t *info = l->data;
271 if (g_strcmp0(info->uuid, uuid) == 0)
278 static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
279 rfcomm_cb_data_t *info)
281 if (conn_info == NULL)
284 bluetooth_rfcomm_disconnection_t disconn_info;
285 bt_event_info_t *event_info = NULL;
287 if (conn_info->disconnected == FALSE)
290 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
291 if (event_info == NULL) {
292 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
294 if (info->rfcomm_conns == NULL)
295 rfcomm_cb_data_remove(info);
299 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
300 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
301 g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
302 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
305 BT_DBG("Disconnected FD [%d]", conn_info->fd);
306 disconn_info.socket_fd = conn_info->fd;
308 BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
309 BLUETOOTH_ERROR_NONE, conn_info->bt_addr,
310 info->uuid, conn_info->fd);
311 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
312 BLUETOOTH_ERROR_NONE, &disconn_info,
313 event_info->cb, event_info->user_data);
315 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
317 if (info->rfcomm_conns == NULL)
318 rfcomm_cb_data_remove(info);
320 if (_bt_common_send_rfcomm_conn_info(RFCOMM_ROLE_CLIENT,
321 FALSE, disconn_info.socket_fd) != BLUETOOTH_ERROR_NONE)
322 BT_ERR("Fail to send the connection info");
327 static gboolean __rfcomm_client_disconnect(gpointer user_data)
329 rfcomm_cb_data_t *info = (rfcomm_cb_data_t *)user_data;
331 BT_INFO_C("### Disconnected [RFCOMM Client]");
333 retv_if(info == NULL, FALSE);
335 if (g_slist_find(rfcomm_clients, info) == NULL) {
336 BT_INFO("rfcomm resource is already freed");
341 g_slist_foreach(info->rfcomm_conns,
342 (GFunc)_bt_rfcomm_disconnect_conn_info, info);
348 static gboolean __is_error_by_disconnect(GError *err)
350 return !g_strcmp0(err->message, "Connection reset by peer") ||
351 !g_strcmp0(err->message, "Connection timed out") ||
352 !g_strcmp0(err->message, "Software caused connection abort");
355 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
360 int result = BLUETOOTH_ERROR_NONE;
361 rfcomm_cb_data_t *info = data;
362 rfcomm_conn_info_t *conn_info = NULL;
363 bt_event_info_t *event_info;
364 bluetooth_rfcomm_received_data_t data_r;
365 GIOStatus status = G_IO_STATUS_NORMAL;
368 static int resource_unavailable_cnt = 0;
370 retv_if(info == NULL, FALSE);
371 fd = g_io_channel_unix_get_fd(chan);
372 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
373 BT_ERR_C("RFComm Client disconnected: %d", fd);
375 conn_info = __get_conn_info_from_fd(info, fd);
376 if (conn_info == NULL) {
377 BT_ERR("No Connection info found with FD [%d]", fd);
381 if (conn_info->disconnected == FALSE) {
382 close(conn_info->fd);
383 conn_info->disconnected = TRUE;
385 __rfcomm_client_disconnect(info);
389 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
391 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
393 if (status != G_IO_STATUS_NORMAL) {
394 BT_ERR("IO Channel read is failed with %d", status);
399 BT_ERR("IO Channel read error [%s]", err->message);
400 if (status == G_IO_STATUS_ERROR &&
401 __is_error_by_disconnect(err)) {
403 BT_ERR("cond : %d", cond);
410 if (status == G_IO_STATUS_ERROR ||
411 status == G_IO_STATUS_EOF) {
413 } else if (status == G_IO_STATUS_AGAIN) {
414 resource_unavailable_cnt++;
415 if (resource_unavailable_cnt > 10)
421 resource_unavailable_cnt = 0;
423 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
424 if (event_info == NULL) {
429 data_r.socket_fd = fd;
430 data_r.buffer_size = len;
431 data_r.buffer = buffer;
433 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
435 event_info->cb, event_info->user_data);
437 if (bluetooth_get_battery_monitor_state()) {
439 BT_DBG("Adding rfcomm rx timeout function for battery monitor");
440 rx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_rx_data, NULL);
449 conn_info = __get_conn_info_from_fd(info, fd);
450 if (conn_info == NULL) {
451 BT_ERR("No Connection info found with FD [%d]", fd);
455 if (conn_info->disconnected == FALSE) {
456 close(conn_info->fd);
457 conn_info->disconnected = TRUE;
459 __rfcomm_client_disconnect(info);
464 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
467 bluetooth_rfcomm_connection_t conn_info;
468 bt_event_info_t *event_info;
469 rfcomm_conn_info_t *conn_list_info = NULL;
471 if (result == BLUETOOTH_ERROR_NONE)
472 BT_INFO_C("### Connected [RFCOMM Client]");
474 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
475 if (event_info == NULL)
478 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
479 conn_info.device_role = RFCOMM_ROLE_CLIENT;
480 g_strlcpy(conn_info.uuid, cb_data->uuid, BLUETOOTH_UUID_STRING_MAX);
481 _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
483 conn_list_info = __get_conn_info_from_address(cb_data, dev_address);
484 if (conn_list_info == NULL) {
485 BT_ERR("Device addres %s not found in connection list", dev_address);
488 conn_info.socket_fd = conn_list_info->fd;
489 conn_info.server_id = -1;
491 if (_bt_common_send_rfcomm_conn_info(RFCOMM_ROLE_CLIENT,
492 TRUE, conn_info.socket_fd) != BLUETOOTH_ERROR_NONE)
493 BT_ERR("Fail to send the connection info");
495 BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
496 result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
497 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
499 event_info->cb, event_info->user_data);
503 void _bt_rfcomm_client_disconnect_all(void)
508 BT_INFO_C("### Disconnect all RFCOMM client connections");
510 for (client = rfcomm_clients; client; ) {
511 rfcomm_cb_data_t *info = client->data;
513 for (conn = info->rfcomm_conns; conn; conn = conn->next) {
514 rfcomm_conn_info_t *conn_info = conn->data;
516 if (conn_info == NULL)
519 if (conn_info->watch_id == 0 || conn_info->disconnected)
522 close(conn_info->fd);
523 conn_info->disconnected = TRUE;
525 _bt_disconnect_ext_profile(conn_info->bt_addr,
529 client = client->next;
530 __rfcomm_client_disconnect(info);
536 void _bt_rfcomm_client_reset_timer(void)
540 g_source_remove(rx_tag);
544 g_source_remove(tx_tag);
552 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
554 rfcomm_cb_data_t *info;
556 rfcomm_conn_info_t *conn_info = NULL;
557 char address[BT_ADDRESS_STRING_SIZE];
559 BT_INFO("%s %d", path, fd);
561 _bt_convert_addr_type_to_string(address,
562 (unsigned char *)addr);
564 info = __find_rfcomm_info_from_path(path);
566 BT_ERR("rfcomm info is NULL");
570 conn_info = __get_conn_info_from_address(info, address);
571 if (conn_info == NULL) {
572 BT_ERR("connection info is NULL");
578 data_io = g_io_channel_unix_new(fd);
580 g_io_channel_set_encoding(data_io, NULL, NULL);
581 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
583 conn_info->watch_id = g_io_add_watch(data_io,
584 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
585 __client_data_received_cb, info);
587 g_io_channel_unref(data_io);
589 __client_connected_cb(info, address, BLUETOOTH_ERROR_NONE);
594 static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
598 GError *error = NULL;
600 rfcomm_cb_data_t *cb_data;
601 char dev_address[BT_ADDRESS_STRING_SIZE];
605 ret_if(user_data == NULL);
609 value = g_dbus_proxy_call_finish(proxy, res, &error);
612 g_dbus_error_strip_remote_error(error);
613 BT_ERR("Error : %s \n", error->message);
615 if (g_strcmp0(error->message, "In Progress") == 0)
616 result = BLUETOOTH_ERROR_DEVICE_BUSY;
618 result = BLUETOOTH_ERROR_INTERNAL;
619 path = g_dbus_proxy_get_object_path(proxy);
620 _bt_convert_device_path_to_address(path, dev_address);
621 __rfcomm_client_connected_cb(cb_data, dev_address, result);
625 g_variant_unref(value);
629 g_object_unref(proxy);
634 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
635 GAsyncResult *res, gpointer user_data)
637 rfcomm_cb_data_t *cb_data;
641 bt_register_profile_info_t info = {0};
642 int result = BLUETOOTH_ERROR_NONE;
643 char dev_address[BT_ADDRESS_STRING_SIZE];
648 ret_if(user_data == NULL);
652 path = g_dbus_proxy_get_object_path(proxy);
654 _bt_convert_device_path_to_address(path, dev_address);
655 BT_DBG("Device Adress [%s]", dev_address);
656 value = g_dbus_proxy_call_finish(proxy, res, &err);
658 g_object_unref(proxy);
660 g_variant_unref(value);
663 g_dbus_error_strip_remote_error(err);
664 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
665 if (!strcmp("Operation canceled", err->message)) {
666 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
667 } else if (!strcmp("In Progress", err->message)) {
668 result = BLUETOOTH_ERROR_IN_PROGRESS;
669 } else if (!strcmp("Host is down", err->message)) {
670 result = BLUETOOTH_ERROR_HOST_DOWN;
671 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
672 result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
673 ret = _bt_cancel_discovers(dev_address);
674 if (ret != BLUETOOTH_ERROR_NONE)
675 BT_ERR("Error: While CancelDiscovery");
677 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
679 __rfcomm_client_connected_cb(cb_data, dev_address, result);
682 BT_INFO("Services are Updated checking required uuid is there");
683 /* Check here for uuid present */
684 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
685 if (ret == BLUETOOTH_ERROR_NONE) {
686 info.uuid = (char *)cb_data->uuid;
687 info.obj_path = cb_data->obj_path;
688 info.role = "client";
690 ret = _bt_register_profile(&info, FALSE);
692 BT_DBG("Error: register profile");
693 ret = _bt_connect_profile(dev_address, cb_data->uuid,
694 __bt_connect_response_cb, cb_data);
696 if (ret != BLUETOOTH_ERROR_NONE) {
697 BT_ERR("ConnectProfile failed");
698 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
699 __rfcomm_client_connected_cb(cb_data, dev_address, result);
703 BT_ERR("remote uuid not found");
704 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
705 __rfcomm_client_connected_cb(cb_data, dev_address, result);
713 GSList *rfcomm_clients;
720 } rfcomm_client_conn_info_t;
722 static gboolean __is_error_by_disconnect(GError *err)
724 return !g_strcmp0(err->message, "Connection reset by peer") ||
725 !g_strcmp0(err->message, "Connection timed out") ||
726 !g_strcmp0(err->message, "Software caused connection abort");
729 static rfcomm_client_conn_info_t *__find_rfcomm_conn_info_with_fd(int fd)
735 for (l = rfcomm_clients; l != NULL; l = l->next) {
736 rfcomm_client_conn_info_t *info = l->data;
738 if (info && info->sock_fd == fd) {
739 BT_INFO("Match found");
748 static void __rfcomm_remove_client_conn_info_t(rfcomm_client_conn_info_t *info)
750 ret_if(info == NULL);
752 rfcomm_clients = g_slist_remove(rfcomm_clients, info);
754 g_free(info->remote_addr);
757 static void __bt_rfcomm_client_disconnected(rfcomm_client_conn_info_t *conn_info)
760 bluetooth_rfcomm_disconnection_t disconn_info;
761 bt_event_info_t *event_info = NULL;
763 ret_if(conn_info == NULL);
765 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
766 ret_if(event_info == NULL);
768 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
769 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
770 disconn_info.socket_fd = conn_info->sock_fd;
771 g_strlcpy(disconn_info.uuid, conn_info->uuid, BLUETOOTH_UUID_STRING_MAX);
772 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
773 conn_info->remote_addr);
775 BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
776 BLUETOOTH_ERROR_NONE, conn_info->remote_addr,
777 conn_info->uuid, conn_info->sock_fd);
778 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
779 BLUETOOTH_ERROR_NONE, &disconn_info,
780 event_info->cb, event_info->user_data);
785 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
787 bt_event_info_t *event_info;
788 bluetooth_rfcomm_received_data_t data_r;
789 rfcomm_client_conn_info_t *conn_info;
795 GIOStatus status = G_IO_STATUS_NORMAL;
799 fd = g_io_channel_unix_get_fd(chan);
800 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
801 BT_ERR_C("RFComm Client disconnected: %d", fd);
805 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
806 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
808 if (status != G_IO_STATUS_NORMAL) {
809 BT_ERR("IO Channel read is failed with %d", status);
812 BT_ERR("IO Channel read error [%s]", err->message);
813 if (status == G_IO_STATUS_ERROR &&
814 __is_error_by_disconnect(err)) {
815 BT_ERR("cond : %d", cond);
826 BT_ERR("Length is zero, remote end hang up");
830 BT_DBG("fd: %d, len: %d, buffer: %s", fd, len, buffer);
832 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
833 if (event_info == NULL) {
834 BT_INFO("event_info == NULL");
839 data_r.socket_fd = fd;
840 data_r.buffer_size = len;
841 data_r.buffer = buffer;
843 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
844 BLUETOOTH_ERROR_NONE, &data_r,
845 event_info->cb, event_info->user_data);
851 conn_info = __find_rfcomm_conn_info_with_fd(fd);
853 __bt_rfcomm_client_disconnected(conn_info);
854 __rfcomm_remove_client_conn_info_t(conn_info);
856 BT_ERR("RFCOMM client conn_info not found");
861 static void __rfcomm_client_connection_create_watch(rfcomm_client_conn_info_t *conn_info)
865 ret_if(NULL == conn_info);
869 data_io = g_io_channel_unix_new(conn_info->sock_fd);
870 g_io_channel_set_encoding(data_io, NULL, NULL);
871 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
872 conn_info->watch_id = g_io_add_watch(data_io,
873 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
874 __client_data_received_cb, NULL);
875 g_io_channel_unref(data_io);
880 static void __bt_rfcomm_handle_new_client_connection(bluetooth_rfcomm_connection_t *info)
882 rfcomm_client_conn_info_t *conn_info;
884 ret_if(NULL == info);
888 conn_info = g_malloc0(sizeof(rfcomm_client_conn_info_t));
889 conn_info->remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
890 _bt_convert_addr_type_to_string(
891 conn_info->remote_addr, info->device_addr.addr);
892 conn_info->uuid = g_strdup(info->uuid);
893 conn_info->sock_fd = info->socket_fd;
895 BT_DBG("Address:%s, UUID:%s Socket: %d",
896 conn_info->remote_addr, conn_info->uuid, conn_info->sock_fd);
898 rfcomm_clients = g_slist_append(rfcomm_clients, conn_info);
899 __rfcomm_client_connection_create_watch(conn_info);
904 static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
909 size = g_variant_get_size(var);
911 data = (char *)g_variant_get_data(var);
913 param = g_array_append_vals(param, data, size);
919 /* TODO_40 : 4.0 merge */
920 /* Don't use this function directly. Instead of it, get the out parameter only */
921 static void __bt_get_event_info(int service_function, GArray *output,
922 int *event, int *event_type, void **param_data)
924 ret_if(event == NULL);
926 BT_DBG("service_function : %s (0x%x)",
927 _bt_convert_service_function_to_string(service_function),
929 switch (service_function) {
930 case BT_RFCOMM_CLIENT_CONNECT:
931 *event_type = BT_RFCOMM_CLIENT_EVENT;
932 *event = BLUETOOTH_EVENT_RFCOMM_CONNECTED;
933 ret_if(output == NULL);
934 *param_data = &g_array_index(output,
935 bluetooth_rfcomm_connection_t, 0);
938 BT_ERR("Unknown function");
944 static void __async_req_cb_with_unix_fd_list(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data)
946 int result = BLUETOOTH_ERROR_NONE;
947 int event_type = BT_ADAPTER_EVENT;
948 bt_req_info_t *cb_data = user_data;
949 bluetooth_event_param_t bt_event;
951 GError *error = NULL;
954 GArray *out_param1 = NULL;
955 GUnixFDList *out_fd_list = NULL;
959 memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
961 value = g_dbus_proxy_call_with_unix_fd_list_finish(proxy, &out_fd_list, res, &error);
964 /* dBUS gives error cause */
965 BT_ERR("D-Bus API failure: message[%s]",
967 g_clear_error(&error);
969 result = BLUETOOTH_ERROR_TIMEOUT;
971 ret_if(cb_data == NULL);
973 __bt_get_event_info(cb_data->service_function, NULL,
974 &bt_event.event, &event_type,
975 &bt_event.param_data);
979 g_variant_get(value, "(iv)", &result, ¶m1);
980 g_variant_unref(value);
983 out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
984 __bt_fill_garray_from_variant(param1, out_param1);
985 g_variant_unref(param1);
991 __bt_get_event_info(cb_data->service_function, out_param1,
992 &bt_event.event, &event_type,
993 &bt_event.param_data);
995 if (result == BLUETOOTH_ERROR_NONE && out_param1) {
996 if (BT_RFCOMM_CLIENT_CONNECT == cb_data->service_function) {
999 bluetooth_rfcomm_connection_t *conn_info;
1001 conn_info = (bluetooth_rfcomm_connection_t *)bt_event.param_data;
1003 BT_ERR("out_fd_list is NULL");
1007 fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
1008 BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
1009 conn_info->socket_fd = fd_list_array[0];
1011 BT_DBG("conn_info->socket_fd: %d", conn_info->socket_fd);
1012 __bt_rfcomm_handle_new_client_connection(conn_info);
1014 if (cb_data->cb != NULL) {
1015 /* Send client connected event */
1016 bt_event.result = result;
1017 BT_INFO("event_type[%d], result=[%d]", event_type, result);
1018 ((bluetooth_cb_func_ptr)cb_data->cb)(
1019 bt_event.event, &bt_event, cb_data->user_data);
1022 g_free(fd_list_array);
1023 g_object_unref(out_fd_list);
1029 if (cb_data->cb == NULL)
1032 /* Only if fail case, call the callback function*/
1033 bt_event.result = result;
1035 BT_INFO("event_type[%d], result=[%d]", event_type, result);
1036 if (event_type == BT_RFCOMM_CLIENT_EVENT) {
1037 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
1038 &bt_event, cb_data->user_data);
1040 BT_INFO("Not handled event type : %d", event_type);
1044 g_array_free(out_param1, TRUE);
1051 BT_EXPORT_API int bluetooth_rfcomm_connect(
1052 const bluetooth_device_address_t *remote_bt_address,
1053 const char *remote_uuid)
1056 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1057 rfcomm_cb_data_t *cb_data = NULL;
1058 rfcomm_conn_info_t *conn = NULL;
1062 bt_user_info_t *user_info;
1063 char uuid[BLUETOOTH_UUID_STRING_MAX];
1065 BT_CHECK_PARAMETER(remote_bt_address, return);
1066 BT_CHECK_PARAMETER(remote_uuid, return);
1067 BT_CHECK_ENABLED(return);
1069 if (_bt_check_dpm(BT_DPM_ADDRESS, (void *)remote_bt_address) == BT_DPM_RESTRICTED) {
1070 BT_ERR("Blacklist device");
1071 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1074 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED ||
1075 _bt_check_dpm(BT_DPM_HF_ONLY, NULL) == BT_DPM_RESTRICTED) {
1076 BT_ERR("Not allow to connect the RFCOMM service");
1077 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1080 if (_bt_check_dpm(BT_DPM_DESKTOP, NULL) == BT_DPM_RESTRICTED) {
1081 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1082 bluetooth_device_class_t dev_class;
1084 _bt_convert_addr_type_to_string(address, (unsigned char *)remote_bt_address->addr);
1085 _bt_get_cod_by_address(address, &dev_class);
1087 if (dev_class.major_class == BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
1088 BT_ERR("Reject a authorization due to MDM Policy");
1089 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1093 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1094 BT_INFO_C("### Connect RFCOMM");
1099 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CLIENT_CONNECT)
1100 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1101 BT_ERR("Don't have a privilege to use this API");
1102 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1105 id = __rfcomm_assign_id();
1107 return BLUETOOTH_ERROR_INTERNAL;
1109 cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
1111 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
1113 object_id = _bt_register_new_conn(path, new_connection);
1114 if (object_id < 0) {
1115 __rfcomm_delete_id(id);
1117 return BLUETOOTH_ERROR_INTERNAL;
1120 cb_data = g_new0(rfcomm_cb_data_t, 1);
1121 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
1122 cb_data->obj_path = path;
1123 cb_data->object_id = object_id;
1127 conn = g_new0(rfcomm_conn_info_t, 1);
1129 _bt_convert_addr_type_to_string(conn->bt_addr,
1130 (unsigned char *)remote_bt_address->addr);
1132 BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
1133 cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
1135 ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
1136 __bt_discover_service_response_cb, cb_data);
1137 if (ret != BLUETOOTH_ERROR_NONE) {
1138 BT_ERR("Error returned while service discovery");
1139 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
1140 if (cb_data->rfcomm_conns == NULL)
1141 rfcomm_cb_data_remove(cb_data);
1142 return BLUETOOTH_ERROR_INTERNAL;
1145 if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
1146 BT_INFO("Adding callback information to rfcomm_clients");
1147 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
1149 BT_INFO("Callback information is already added");
1151 return BLUETOOTH_ERROR_NONE;
1153 user_info = _bt_get_user_data(BT_COMMON);
1154 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
1156 /* connect_type: BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
1157 /* In now, we only support to connecty using UUID */
1158 connect_type = BT_RFCOMM_UUID;
1160 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CLIENT_CONNECT)
1161 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1162 BT_ERR("Don't have a privilege to use this API");
1163 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1167 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1169 g_array_append_vals(in_param1, remote_bt_address,
1170 sizeof(bluetooth_device_address_t));
1172 g_strlcpy(uuid, remote_uuid, sizeof(uuid));
1173 g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
1175 g_array_append_vals(in_param3, &connect_type, sizeof(int));
1177 result = _bt_send_request_async_with_unix_fd_list(BT_BLUEZ_SERVICE,
1178 BT_RFCOMM_CLIENT_CONNECT,
1179 in_param1, in_param2,
1180 in_param3, in_param4,
1181 user_info->cb, user_info->user_data,
1182 NULL, (GAsyncReadyCallback)__async_req_cb_with_unix_fd_list);
1184 BT_DBG("result: %x", result);
1186 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1192 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
1194 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1196 GSList *conn_list = NULL;
1197 rfcomm_cb_data_t *client_info;
1198 rfcomm_conn_info_t *conn_info;
1199 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1201 BT_CHECK_PARAMETER(device_address, return);
1202 BT_CHECK_PARAMETER(connected, return);
1204 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
1207 for (l = rfcomm_clients; l != NULL; l = l->next) {
1208 client_info = l->data;
1209 if (client_info == NULL)
1211 for (conn_list = client_info->rfcomm_conns;
1212 conn_list != NULL; conn_list = conn_list->next) {
1213 conn_info = conn_list->data;
1214 if (conn_info == NULL)
1217 if (g_strcmp0(address, conn_info->bt_addr) == 0) {
1219 return BLUETOOTH_ERROR_NONE;
1224 return BLUETOOTH_ERROR_NONE;
1227 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1229 BT_CHECK_PARAMETER(device_address, return);
1230 BT_CHECK_PARAMETER(connected, return);
1235 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
1236 BT_INFO("Client address: [%s]", address);
1238 for (l = rfcomm_clients; l != NULL; l = l->next) {
1239 rfcomm_client_conn_info_t *info = l->data;
1241 if (info && !strncasecmp(info->remote_addr, address, BT_ADDRESS_STRING_SIZE)) {
1242 BT_INFO("Match found");
1244 return BLUETOOTH_ERROR_NONE;
1249 return BLUETOOTH_ERROR_NONE;
1253 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
1256 int connected = FALSE;
1258 BT_CHECK_ENABLED(return);
1261 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1263 result = _bt_send_request(BT_BLUEZ_SERVICE,
1264 BT_RFCOMM_CLIENT_IS_CONNECTED,
1265 in_param1, in_param2, in_param3,
1266 in_param4, &out_param);
1268 BT_DBG("result: %x", result);
1270 if (result == BLUETOOTH_ERROR_NONE) {
1271 connected = g_array_index(out_param,
1274 BT_ERR("Fail to send request");
1277 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1282 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
1284 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1285 rfcomm_cb_data_t *info;
1286 rfcomm_conn_info_t *conn_info;
1288 BT_INFO_C("### Disconnect RFCOMM");
1290 BT_CHECK_ENABLED(return);
1292 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_DISCONNECT)
1293 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1294 BT_ERR("Don't have a privilege to use this API");
1295 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1298 BT_DBG("Requested FD %d", socket_fd);
1299 if (socket_fd < 0) {
1300 BT_ERR("Invalid FD");
1301 return BLUETOOTH_ERROR_INVALID_PARAM;
1303 BT_DBG("FDD %d", socket_fd);
1305 info = __find_rfcomm_info_with_fd(socket_fd);
1307 BT_DBG("Could not find in client, so check in server");
1308 return bluetooth_rfcomm_server_disconnect(socket_fd);
1311 conn_info = __get_conn_info_from_fd(info, socket_fd);
1312 if (conn_info == NULL) {
1313 BT_ERR("Could not find connection info");
1314 return BLUETOOTH_ERROR_INTERNAL;
1317 if (conn_info->watch_id == 0 || conn_info->disconnected) {
1318 BT_ERR("Invalid state");
1319 return BLUETOOTH_ERROR_NOT_CONNECTED;
1322 close(conn_info->fd);
1323 conn_info->disconnected = TRUE;
1325 BT_INFO("conn_info %s", conn_info->bt_addr);
1326 _bt_disconnect_ext_profile(conn_info->bt_addr, info->obj_path);
1328 if (info->idle_id == 0)
1329 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
1331 return BLUETOOTH_ERROR_NONE;
1333 rfcomm_client_conn_info_t *conn_info;
1335 BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
1337 BT_CHECK_ENABLED(return);
1338 retv_if(socket_fd < 0, BLUETOOTH_ERROR_INVALID_PARAM);
1340 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_DISCONNECT)
1341 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1342 BT_ERR("Don't have a privilege to use this API");
1343 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1346 BT_DBG("FD %d", socket_fd);
1348 conn_info = __find_rfcomm_conn_info_with_fd(socket_fd);
1349 if (conn_info == NULL) {
1350 BT_DBG("Could not find in client, so check in server");
1351 /* Check for fd in server list and perform the disconnection if present */
1352 return bluetooth_rfcomm_server_disconnect(socket_fd);
1355 if (conn_info->watch_id <= 0) {
1356 BT_ERR("Invalid state");
1357 return BLUETOOTH_ERROR_NOT_CONNECTED;
1361 * Just close socket here and return. Socket close will be detected via I/O watch
1362 * and disconnection event as well as info cleanup will be performed there.
1364 close(conn_info->sock_fd);
1366 return BLUETOOTH_ERROR_NONE;
1370 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1372 static int __write_all(int fd, const char *buf, int len)
1374 int sent = 0, try = 0;
1380 written = write(fd, buf, len);
1381 BT_DBG("written: %d", written);
1383 if (errno == EINTR || errno == EAGAIN) {
1405 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
1407 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1412 #ifndef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1413 BT_CHECK_ENABLED(return);
1415 BT_CHECK_PARAMETER(buf, return);
1417 BT_ERR("Invalid FD");
1418 return BLUETOOTH_ERROR_INVALID_PARAM;
1421 retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
1423 switch (privilege_token) {
1425 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_WRITE);
1427 if (result == BLUETOOTH_ERROR_NONE) {
1428 privilege_token = 1; /* Have a permission */
1429 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1430 BT_ERR("Don't have a privilege to use this API");
1431 privilege_token = -1; /* Don't have a permission */
1432 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1434 /* Just break - It is not related with permission error */
1438 /* Already have a privilege */
1441 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1443 /* Invalid privilge token value */
1444 return BLUETOOTH_ERROR_INTERNAL;
1447 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1448 written = write(fd, buf, length);
1449 /*BT_DBG("Length %d, written = %d, balance(%d)",
1450 length, written, length - written); */
1451 if (written > 0 && bluetooth_get_battery_monitor_state()) {
1453 BT_DBG("Adding rfcomm tx timeout function for battery monitor");
1454 tx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_tx_data, NULL);
1460 result = __write_all(fd, buf, length);
1461 if (result > 0 && bluetooth_get_battery_monitor_state()) {
1463 BT_DBG("Adding rfcomm tx timeout function for battery monitor");
1464 tx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_tx_data, NULL);