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.
22 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
25 #include <syspopup_caller.h>
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
30 #include "bt-service-audio.h"
31 #include "bt-service-adapter.h"
32 #include "bt-service-common.h"
33 #include "bt-service-device.h"
34 #include "bt-service-event.h"
35 #include "bt-service-util.h"
37 #include "bt-service-headset-connection.h"
39 #ifdef TIZEN_SUPPORT_DUAL_HF
40 #define VCONF_KEY_BT_HOST_BT_MAC_ADDR "db/wms/host_bt_mac"
46 char device_address[BT_ADDRESS_STRING_SIZE + 1];
47 } bt_connected_headset_data_t;
49 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
53 } bt_auto_connect_timer_t;
55 bt_auto_connect_timer_t auto_connect_timer = {0, };
58 static GList *g_connected_list;
60 static bt_headset_wait_t *g_wait_data;
62 static bt_audio_function_data_t *pdata;
64 static void __bt_remove_device_from_wait_list();
66 static void __bt_free_wait_data();
68 static gboolean __bt_device_support_uuid(char *remote_address,
69 bt_audio_type_t type);
71 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
72 static int __bt_auto_connect_alarm_cb(alarm_id_t alarm_id, void* user_param);
75 static void __bt_hf_request_cb(GDBusProxy *proxy, GAsyncResult *res,
78 GError *g_error = NULL;
79 GVariant *out_param1 = NULL;
80 GVariant *reply = NULL;
81 int result = BLUETOOTH_ERROR_NONE;
82 bt_function_data_t *func_data;
83 request_info_t *req_info;
85 reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
86 g_object_unref(proxy);
88 func_data = user_data;
90 if (func_data == NULL) {
92 BT_ERR("func_data == NULL");
96 req_info = _bt_get_request_info(func_data->req_id);
97 if (req_info == NULL) {
98 BT_ERR("req_info == NULL");
103 BT_ERR("HF Connect Dbus Call Error");
104 result = _bt_convert_gerror(g_error);
106 BT_ERR("Error: %s\n", g_error->message);
107 g_clear_error(&g_error);
110 g_variant_unref(reply);
113 if (req_info->context == NULL)
116 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
117 func_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
119 g_dbus_method_invocation_return_value(req_info->context,
120 g_variant_new("(iv)", result, out_param1));
122 _bt_delete_request_list(req_info->req_id);
126 g_free(func_data->address);
131 void _bt_audio_check_pending_connect()
134 bluetooth_device_address_t device_address;
139 if (pdata->pending == BT_PENDING_CONNECT) {
141 _bt_convert_addr_string_to_type(device_address.addr,
143 _bt_audio_connect(pdata->req_id,
148 g_free(pdata->address);
157 static void __bt_audio_request_cb(GDBusProxy *proxy, GAsyncResult *res,
160 GError *g_error = NULL;
161 GVariant *out_param1 = NULL;
162 GVariant *reply = NULL;
163 int result = BLUETOOTH_ERROR_NONE;
164 bt_audio_function_data_t *func_data;
165 request_info_t *req_info;
167 reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
168 g_object_unref(proxy);
169 g_variant_unref(reply);
171 func_data = user_data;
173 if (func_data == NULL) {
175 BT_ERR("func_data == NULL");
179 if (func_data->pending != BT_PENDING_NONE && g_error == NULL) {
180 bluetooth_device_address_t device_address;
181 _bt_convert_addr_string_to_type(device_address.addr,
184 if (func_data->pending == BT_PENDING_CONNECT) {
186 if (__bt_device_support_uuid(func_data->address,
189 pdata = g_new0(bt_audio_function_data_t, 1);
190 pdata->req_id = func_data->req_id;
191 pdata->address = strdup(func_data->address);
192 pdata->pending = func_data->pending;
197 if (_bt_is_service_connected(func_data->address
199 _bt_audio_disconnect(func_data->req_id,
200 BT_AUDIO_HSP, &device_address, NULL);
209 req_info = _bt_get_request_info(func_data->req_id);
210 if (req_info == NULL) {
211 BT_ERR("req_info == NULL");
218 BT_ERR("Audio Connect/Disconnect Dbus Call Error: %s\n", g_error->message);
220 result = BLUETOOTH_ERROR_INTERNAL;
221 /* If there is error then we need to set local initiated connection as false
222 * If don't do this then when headset initiate connection for HFP then we don't
223 * initiate connection for A2dp to headset as this flag was set to true in first
224 * connection failure attempt and not set in error case.*/
225 _bt_headset_set_local_connection(FALSE);
227 /* Remove the device from the list */
228 _bt_remove_headset_from_list(func_data->type, func_data->address);
230 /* Error, check if any waiting device is there */
231 if (g_wait_data == NULL)
234 if (g_strcmp0(g_wait_data->address, func_data->address) != 0) {
235 bluetooth_device_address_t device_address;
236 _bt_convert_addr_string_to_type(device_address.addr,
237 g_wait_data->address);
238 _bt_audio_connect(g_wait_data->req_id, g_wait_data->type,
239 &device_address, NULL);
242 /* Event will be sent by the event reciever */
244 if (req_info->context == NULL) {
245 BT_DBG("req_info->context is NULL");
249 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
250 func_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
252 g_dbus_method_invocation_return_value(req_info->context,
253 g_variant_new("(iv)", result, out_param1));
255 _bt_delete_request_list(req_info->req_id);
257 g_clear_error(&g_error);
260 g_free(func_data->address);
265 static void __bt_free_wait_data()
267 if (g_wait_data != NULL) {
268 g_free(g_wait_data->address);
274 static void __bt_remove_device_from_wait_list()
276 /* Before deleting the request update the UI */
277 GVariant *out_param_1 = NULL;
278 int result = BLUETOOTH_ERROR_INTERNAL;
279 request_info_t *req_info;
281 req_info = _bt_get_request_info(g_wait_data->req_id);
282 if (req_info == NULL) {
283 BT_ERR("req_info == NULL");
287 out_param_1 = g_variant_new_from_data((const GVariantType *)"ay",
288 g_wait_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
290 g_dbus_method_invocation_return_value(req_info->context,
291 g_variant_new("(iv)", result, out_param_1));
293 _bt_delete_request_list(g_wait_data->req_id);
296 static void __bt_set_headset_disconnection_type(const char *address)
298 bt_connected_headset_data_t *connected_device;
301 node = g_list_first(g_connected_list);
302 while (node != NULL) {
303 connected_device = node->data;
304 if (g_strcmp0(connected_device->device_address, address) == 0) {
305 g_wait_data->disconnection_type = connected_device->type;
308 node = g_list_next(node);
312 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
313 void _bt_check_already_connected_headset(int type, char *address)
316 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
317 bluetooth_device_address_t device_address;
318 int device_count = 0;
320 if (address != NULL) {
321 node = g_list_first(g_connected_list);
322 while (node != NULL) {
323 bt_connected_headset_data_t *connected_device = node->data;
324 if ((connected_device->type & type) &&
325 (g_strcmp0(connected_device->device_address, address) != 0)) {
327 /* Disconnect the earliest(1st) connected headset */
328 if (device_count == 1) {
329 g_strlcpy(connected_address,
330 connected_device->device_address,
331 BT_ADDRESS_STRING_SIZE + 1);
332 BT_DBG("Earliest/First Connected headset %s",
334 } else if (device_count == MAX_CONNECTED_HEADSET) {
335 _bt_convert_addr_string_to_type(device_address.addr,
337 _bt_audio_disconnect(0, type, &device_address, NULL);
341 node = g_list_next(node);
347 gboolean _bt_is_headset_type_connected(int type, char *address)
351 node = g_list_first(g_connected_list);
352 while (node != NULL) {
353 bt_connected_headset_data_t *connected_device = node->data;
355 if (connected_device->type & type) {
357 g_strlcpy(address, connected_device->device_address,
358 BT_ADDRESS_STRING_SIZE + 1);
362 node = g_list_next(node);
367 #ifdef TIZEN_SUPPORT_DUAL_HF
368 gboolean __bt_is_companion_device(const char *addr)
370 if (TIZEN_PROFILE_WEARABLE) {
371 char *host_device_address = NULL;
372 host_device_address = vconf_get_str(VCONF_KEY_BT_HOST_BT_MAC_ADDR);
374 if (!host_device_address) {
375 BT_INFO("Failed to get a companion device address");
379 if (g_strcmp0(host_device_address, addr) == 0) {
380 BT_INFO("Found companion device");
381 free(host_device_address);
385 free(host_device_address);
388 /* TODO : Need to add companion device check condition for Phone models */
394 static int __bt_is_headset_connected(int type, int req_id,
397 gboolean connected = FALSE;
398 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
399 bluetooth_device_address_t device_address;
400 bt_connected_headset_data_t *connected_device = NULL;
401 #ifdef TIZEN_SUPPORT_DUAL_HF
402 gboolean is_companion_device = FALSE;
405 /* Check if any other headset is connected */
408 node = g_list_first(g_connected_list);
409 while (node != NULL) {
410 connected_device = node->data;
411 if ((connected_device->type & type)) {
412 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
414 /* Disconnect the earliest(1st) connected headset */
415 if (device_count == 1)
416 g_strlcpy(connected_address, connected_device->device_address,
417 BT_ADDRESS_STRING_SIZE + 1);
419 if (g_strcmp0(connected_device->device_address, address) == 0)
420 return BLUETOOTH_ERROR_ALREADY_CONNECT;
422 g_strlcpy(connected_address, connected_device->device_address,
423 BT_ADDRESS_STRING_SIZE + 1);
425 #ifdef TIZEN_SUPPORT_DUAL_HF
426 is_companion_device = __bt_is_companion_device(connected_address);
427 BT_INFO(" is_companion_device[%d]", is_companion_device);
429 if (!is_companion_device) {
434 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
435 if (device_count == MAX_CONNECTED_HEADSET) {
445 node = g_list_next(node);
449 return BLUETOOTH_ERROR_NOT_CONNECTED;
451 BT_DBG("connected headset %s", connected_address);
453 if (g_strcmp0(connected_address, address) == 0)
454 return BLUETOOTH_ERROR_ALREADY_CONNECT;
455 #ifdef TIZEN_SUPPORT_DUAL_HF
456 else if (TRUE == __bt_is_companion_device(address))
457 return BLUETOOTH_ERROR_NOT_CONNECTED;
460 /* Convert BD adress from string type */
461 _bt_convert_addr_string_to_type(device_address.addr, connected_address);
462 int value = BLUETOOTH_ERROR_NONE;
463 value = _bt_audio_disconnect(0, connected_device->type & type, &device_address, NULL);
465 /* If already one device is waiting, remove current waiting device and add new */
466 if (value == BLUETOOTH_ERROR_NONE) {
467 if (g_wait_data != NULL) {
468 if (g_strcmp0(g_wait_data->address, address) != 0) {
469 __bt_remove_device_from_wait_list();
470 __bt_free_wait_data();
474 if (g_wait_data == NULL) {
475 g_wait_data = g_malloc0(sizeof(bt_headset_wait_t));
476 g_wait_data->address = g_strdup(address);
477 g_wait_data->req_id = req_id;
478 g_wait_data->type = type;
479 g_wait_data->ag_flag = FALSE;
481 /* Set disconnection type */
482 __bt_set_headset_disconnection_type(connected_address);
489 static int __bt_is_headset_connecting(int type)
491 bt_connected_headset_data_t *connected_device = NULL;
493 /* Check if any other headset is connected */
496 node = g_list_first(g_connected_list);
497 while (node != NULL) {
498 connected_device = node->data;
499 if (connected_device->device_state == BT_STATE_CONNECTING)
500 return BLUETOOTH_ERROR_CONNECTION_BUSY;
501 node = g_list_next(node);
504 return BLUETOOTH_ERROR_NONE;
507 int __bt_is_headset_disconnecting(int type)
509 bt_connected_headset_data_t *connected_device = NULL;
511 /* Check if any other headset is connected */
514 node = g_list_first(g_connected_list);
515 while (node != NULL) {
516 connected_device = node->data;
517 if (connected_device->device_state == BT_STATE_DISCONNECTING)
518 return BLUETOOTH_ERROR_CONNECTION_BUSY;
520 node = g_list_next(node);
523 return BLUETOOTH_ERROR_NONE;
526 void _bt_set_audio_wait_data_flag(gboolean flag)
528 BT_DBG("_bt_set_audio_wait_data_flag \n");
529 g_wait_data->ag_flag = flag;
532 bt_headset_wait_t *_bt_get_audio_wait_data(void)
534 BT_DBG("_bt_get_audio_wait_data \n");
538 void _bt_rel_wait_data(void)
540 BT_DBG("_bt_rel_wait_data \n");
541 __bt_free_wait_data();
544 void _bt_add_headset_to_list(int type, int status, const char *address)
546 bt_connected_headset_data_t *connected_device;
547 bt_connected_headset_data_t *device;
550 BT_DBG("_bt_add_headset_to_list \n");
552 node = g_list_first(g_connected_list);
553 while (node != NULL) {
554 device = (bt_connected_headset_data_t *)node->data;
556 if (g_strcmp0(device->device_address, address) == 0) {
557 BT_DBG("Address match, update connection type \n");
558 if (status == BT_STATE_CONNECTED)
559 device->type |= type;
560 device->device_state = status;
563 node = g_list_next(node);
566 connected_device = g_malloc0(sizeof(bt_connected_headset_data_t));
567 connected_device->device_state = status;
568 if ((status == BT_STATE_CONNECTED) || (status == BT_STATE_CONNECTING))
569 connected_device->type |= type;
570 g_strlcpy(connected_device->device_address, address,
571 sizeof(connected_device->device_address));
572 g_connected_list = g_list_append(g_connected_list, connected_device);
575 int _bt_get_device_state_from_list(int type, const char *address)
578 bt_connected_headset_data_t *device;
581 node = g_list_first(g_connected_list);
582 while (node != NULL) {
583 device = (bt_connected_headset_data_t *)node->data;
584 if (g_strcmp0(device->device_address, address) == 0) {
585 BT_DBG("Device found");
586 return device->device_state;
588 node = g_list_next(node);
591 BT_DBG("Device not found");
592 return BLUETOOTH_ERROR_INTERNAL;
595 void _bt_remove_headset_from_list(int type, const char *address)
599 BT_DBG("_bt_remove_headset_from_list \n");
601 node = g_list_first(g_connected_list);
602 while (node != NULL) {
603 bt_connected_headset_data_t *connected_device = node->data;
605 if (g_strcmp0(connected_device->device_address, address) != 0) {
606 node = g_list_next(node);
610 BT_DBG("Address match \n");
612 BT_DBG("Connection type = %x\n", connected_device->type);
616 if (connected_device->type & BT_AUDIO_A2DP)
617 connected_device->type &= ~(BT_AUDIO_A2DP);
620 if (connected_device->type & BT_AUDIO_HSP)
621 connected_device->type &= ~(BT_AUDIO_HSP);
624 if (connected_device->type & BT_AUDIO_ALL)
625 connected_device->type &= ~(BT_AUDIO_ALL);
628 if (connected_device->type & BT_AVRCP)
629 connected_device->type &= ~(BT_AVRCP);
631 case BT_AUDIO_A2DP_SOURCE:
632 if (connected_device->type & BT_AUDIO_A2DP_SOURCE)
633 connected_device->type &= ~(BT_AUDIO_A2DP_SOURCE);
636 BT_DBG("Connection type = %x\n", connected_device->type);
638 if (connected_device->type == 0x00) {
639 g_connected_list = g_list_remove(g_connected_list, connected_device);
640 g_free(connected_device);
642 connected_device->device_state = BT_STATE_CONNECTED;
645 node = g_list_next(node);
649 static gboolean __bt_device_support_uuid(char *remote_address,
650 bt_audio_type_t type)
652 GArray *dev_list = NULL;
656 bluetooth_device_info_t info;
657 char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
658 gboolean ret = FALSE;
662 dev_list = g_array_new(FALSE, FALSE, sizeof(gchar));
664 _bt_get_bonded_devices(&dev_list);
665 size = (dev_list->len) / sizeof(bluetooth_device_info_t);
667 for (i = 0; i < size; i++) {
668 info = g_array_index(dev_list, bluetooth_device_info_t, i);
669 _bt_convert_addr_type_to_string(bond_address,
670 info.device_address.addr);
671 if (strcmp(bond_address, remote_address) != 0)
674 BT_INFO("Device address Matched");
676 while (j != info.service_index) {
677 if (type == BT_AUDIO_HSP) {
678 if (strcmp(info.uuids[j], HFP_HS_UUID) == 0) {
679 BT_INFO("HFP HS UUID exists");
683 } else if (type == BT_AUDIO_A2DP) {
684 if (strcmp(info.uuids[j], A2DP_SINK_UUID) == 0) {
685 BT_INFO("A2DP SINK UUID exists");
694 g_array_free(dev_list, TRUE);
699 gboolean _bt_is_service_connected(char* address, int type)
703 node = g_list_first(g_connected_list);
704 while (node != NULL) {
705 bt_connected_headset_data_t *conn_device = node->data;
707 if ((g_strcmp0(conn_device->device_address, address) == 0) &&
708 (conn_device->type & type)) {
709 BT_INFO("Service connected");
713 node = g_list_next(node);
715 BT_INFO("Service not connected");
719 int _bt_audio_connect(int request_id, int type,
720 bluetooth_device_address_t *device_address,
723 int result = BLUETOOTH_ERROR_NONE;
724 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
725 GDBusProxy *adapter_proxy;
726 GDBusConnection *g_conn;
729 int value = BLUETOOTH_ERROR_NONE;
730 bt_audio_function_data_t *func_data;
731 guint hfp_hs_restricted = 0x0; /* set default "allowed" */
732 guint a2dp_restricted = 0x0;
734 BT_CHECK_PARAMETER(device_address, return);
736 adapter_proxy = _bt_get_adapter_proxy();
737 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
739 g_conn = _bt_gdbus_get_system_gconn();
740 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
742 _bt_convert_addr_type_to_string(address, device_address->addr);
744 func_data = g_malloc0(sizeof(bt_audio_function_data_t));
745 func_data->address = g_strdup(address);
746 func_data->req_id = request_id;
747 func_data->type = type;
748 func_data->pending = BT_PENDING_NONE;
750 result = _bt_get_restrict_profile(device_address, RESTRICTED_PROFILE_HFP_HS, &hfp_hs_restricted);
751 if (result != BLUETOOTH_ERROR_NONE)
752 BT_ERR("Can't get hfp_hs restriction info");
754 result = _bt_get_restrict_profile(device_address, RESTRICTED_PROFILE_A2DP, &a2dp_restricted);
755 if (result != BLUETOOTH_ERROR_NONE)
756 BT_ERR("Can't get a2dp restriction info");
763 uuid = A2DP_SINK_UUID;
766 uuid = AVRCP_TARGET_UUID;
768 case BT_AUDIO_A2DP_SOURCE:
769 uuid = A2DP_SOURCE_UUID;
772 if ((hfp_hs_restricted == 0x0) && (a2dp_restricted == 0x0)) { /* in case of both profiles are not restricted */
773 if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
775 func_data->pending = BT_PENDING_CONNECT;
777 } else if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
778 uuid = A2DP_SINK_UUID;
779 type = BT_AUDIO_A2DP;
781 BT_ERR("No audio role supported");
782 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
785 } else if ((hfp_hs_restricted == 0x1) && (a2dp_restricted == 0x1)) { /* in case of both profiles are restricted */
786 BT_ERR("All profiles are restricted");
787 result = BLUETOOTH_ERROR_INTERNAL;
789 } else if (a2dp_restricted == 0x01) { /* in case of a2dp is restricted, only connection for hfp_hs */
790 if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
794 BT_ERR("HFP_HS role is not supported");
795 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
798 } else if (hfp_hs_restricted == 0x01) { /* in case of hfp_hs is restricted, only connection for a2dp */
799 if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
800 uuid = A2DP_SINK_UUID;
801 type = BT_AUDIO_A2DP;
803 BT_ERR("A2DP role is not supported");
804 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
809 case BT_AVRCP_TARGET:
810 uuid = AVRCP_REMOTE_UUID;
813 BT_ERR("Unknown role");
814 result = BLUETOOTH_ERROR_INTERNAL;
817 BT_INFO("Connecting to service %s", uuid);
819 value = __bt_is_headset_connected(type, request_id, address);
821 if (value == BLUETOOTH_ERROR_ALREADY_CONNECT) {
822 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
824 } else if (value == BLUETOOTH_ERROR_NOT_CONNECTED) {
825 value = __bt_is_headset_connecting(type);
826 if (value != BLUETOOTH_ERROR_NONE) {
827 result = BLUETOOTH_ERROR_IN_PROGRESS;
830 _bt_headset_set_local_connection(TRUE);
831 ret = _bt_connect_profile(address, uuid,
832 __bt_audio_request_cb, func_data);
834 if (ret != BLUETOOTH_ERROR_NONE) {
835 BT_ERR("_bt_connect_profile Error");
836 _bt_headset_set_local_connection(FALSE);
837 g_free(func_data->address);
842 /* Add data to the connected list */
843 _bt_add_headset_to_list(type, BT_STATE_CONNECTING, address);
844 } else if (value == BLUETOOTH_ERROR_IN_PROGRESS) {
845 result = BLUETOOTH_ERROR_IN_PROGRESS;
850 g_array_free(out_param1, TRUE);
852 return BLUETOOTH_ERROR_NONE;
854 if (out_param1 != NULL)
855 g_array_append_vals(out_param1, address,
858 g_free(func_data->address);
864 int _bt_audio_disconnect(int request_id, int type,
865 bluetooth_device_address_t *device_address,
868 int result = BLUETOOTH_ERROR_NONE;
869 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
870 bt_audio_function_data_t *func_data;
871 GDBusProxy *adapter_proxy;
872 GDBusConnection *g_conn;
876 int value = BLUETOOTH_ERROR_NONE;
878 BT_CHECK_PARAMETER(device_address, return);
880 adapter_proxy = _bt_get_adapter_proxy();
881 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
883 g_conn = _bt_gdbus_get_system_gconn();
884 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
886 _bt_convert_addr_type_to_string(address, device_address->addr);
888 func_data = g_malloc0(sizeof(bt_audio_function_data_t));
889 func_data->address = g_strdup(address);
890 func_data->req_id = request_id;
891 func_data->pending = BT_PENDING_NONE;
892 func_data->type = type;
899 uuid = A2DP_SINK_UUID;
902 uuid = AVRCP_TARGET_UUID;
904 case BT_AUDIO_A2DP_SOURCE:
905 uuid = A2DP_SOURCE_UUID;
908 if (_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
909 uuid = A2DP_SINK_UUID;
910 func_data->pending = BT_PENDING_DISCONNECT;
911 } else if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
914 BT_ERR("No audio service connected");
915 result = BLUETOOTH_ERROR_NOT_CONNECTED;
919 case BT_AVRCP_TARGET:
920 uuid = AVRCP_REMOTE_UUID;
923 BT_ERR("Unknown role");
924 result = BLUETOOTH_ERROR_INTERNAL;
927 value = __bt_is_headset_disconnecting(type);
928 if (value != BLUETOOTH_ERROR_NONE) {
929 BT_INFO("Disconnect in progress");
930 result = BLUETOOTH_ERROR_IN_PROGRESS;
933 BT_INFO("Disconnecting service %s", uuid);
934 ret = _bt_disconnect_profile(address, uuid,
935 __bt_audio_request_cb, func_data);
937 if (ret != BLUETOOTH_ERROR_NONE) {
938 BT_ERR("_bt_disconnect_profile Error");
939 g_free(func_data->address);
945 * This logic is added for dual HF mode issue.
947 node = g_list_first(g_connected_list);
948 while (node != NULL) {
949 bt_connected_headset_data_t *connected_device = node->data;
951 if (g_strcmp0(connected_device->device_address, address) == 0) {
952 BT_DBG("Connection type update");
953 type = connected_device->type;
956 node = g_list_next(node);
958 _bt_add_headset_to_list(type, BT_STATE_DISCONNECTING, address);
961 g_array_free(out_param1, TRUE);
963 return BLUETOOTH_ERROR_NONE;
965 if (out_param1 != NULL)
966 g_array_append_vals(out_param1, address,
969 g_free(func_data->address);
975 void _bt_remove_from_connected_list(const char *address)
977 bt_connected_headset_data_t *connected_device;
980 node = g_list_first(g_connected_list);
981 while (node != NULL) {
982 connected_device = node->data;
983 if (connected_device != NULL &&
984 g_strcmp0(connected_device->device_address, address) == 0) {
985 BT_ERR("Device is removed from the list");
986 g_connected_list = g_list_remove(g_connected_list, connected_device);
987 g_free(connected_device);
990 node = g_list_next(node);
994 int _bt_hf_connect(int request_id,
995 bluetooth_device_address_t *device_address,
998 int result = BLUETOOTH_ERROR_NONE;
999 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1000 bt_function_data_t *func_data;
1001 GDBusProxy *adapter_proxy;
1002 GDBusConnection *g_conn;
1003 gboolean connected = FALSE;
1007 BT_CHECK_PARAMETER(device_address, return);
1009 _bt_convert_addr_type_to_string(address, device_address->addr);
1011 adapter_proxy = _bt_get_adapter_proxy();
1012 if (adapter_proxy == NULL) {
1013 result = BLUETOOTH_ERROR_INTERNAL;
1017 g_conn = _bt_gdbus_get_system_gconn();
1018 if (g_conn == NULL) {
1019 result = BLUETOOTH_ERROR_INTERNAL;
1023 /* Check if HF is connected or not */
1024 result = _bt_is_device_connected(device_address, BT_PROFILE_CONN_HFG, &connected);
1025 if (connected == TRUE) {
1026 BT_ERR("HF is already connected");
1027 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
1031 func_data = g_malloc0(sizeof(bt_function_data_t));
1032 func_data->address = g_strdup(address);
1033 func_data->req_id = request_id;
1034 uuid = g_strdup(HFP_AG_UUID);
1036 BT_DBG("Connecting to service %s", uuid);
1038 ret = _bt_connect_profile(address, uuid,
1039 __bt_hf_request_cb, func_data);
1041 if (ret != BLUETOOTH_ERROR_NONE) {
1042 BT_ERR("_bt_connect_profile Error");
1043 g_free(func_data->address);
1050 g_array_free(out_param1, TRUE);
1052 return BLUETOOTH_ERROR_NONE;
1054 if (out_param1 != NULL)
1055 g_array_append_vals(out_param1, address,
1056 BT_ADDRESS_STR_LEN);
1061 int _bt_hf_disconnect(int request_id,
1062 bluetooth_device_address_t *device_address,
1065 int result = BLUETOOTH_ERROR_NONE;
1066 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1067 bt_function_data_t *func_data;
1068 GDBusProxy *adapter_proxy;
1069 GDBusConnection *g_conn;
1074 BT_CHECK_PARAMETER(device_address, return);
1076 _bt_convert_addr_type_to_string(address, device_address->addr);
1078 adapter_proxy = _bt_get_adapter_proxy();
1079 if (adapter_proxy == NULL) {
1080 result = BLUETOOTH_ERROR_INTERNAL;
1084 g_conn = _bt_gdbus_get_system_gconn();
1085 if (g_conn == NULL) {
1086 result = BLUETOOTH_ERROR_INTERNAL;
1090 func_data = g_malloc0(sizeof(bt_function_data_t));
1091 func_data->address = g_strdup(address);
1092 func_data->req_id = request_id;
1093 uuid = g_strdup(HFP_AG_UUID);
1095 BT_DBG("Disconnecting service %s", uuid);
1096 ret = _bt_disconnect_profile(address, uuid,
1097 __bt_hf_request_cb, func_data);
1099 if (ret != BLUETOOTH_ERROR_NONE) {
1100 BT_ERR("_bt_disconnect_profile Error");
1101 g_free(func_data->address);
1108 g_array_free(out_param1, TRUE);
1110 return BLUETOOTH_ERROR_NONE;
1112 if (out_param1 != NULL)
1113 g_array_append_vals(out_param1, address,
1114 BT_ADDRESS_STR_LEN);
1119 int _bt_audio_set_content_protect(gboolean status)
1121 GDBusConnection *conn;
1122 GError *error = NULL;
1126 conn = _bt_gdbus_get_system_gconn();
1127 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1129 BT_DBG("Content Protection status = [%d]", status);
1131 g_dbus_connection_emit_signal(conn,
1132 NULL, BT_CONTENT_PROTECTION_PATH,
1133 BT_CONTENT_PROTECTION_INTERFACE,
1134 "ProtectionRequired",
1135 g_variant_new("(b)", status),
1139 /* dBUS gives error cause */
1140 ERR("Could not Emit Signal: errCode[%x], message[%s]",
1141 error->code, error->message);
1142 g_clear_error(&error);
1143 return BLUETOOTH_ERROR_INTERNAL;
1146 BT_DBG("Emit Signal done = [ProtectionRequired]");
1147 return BLUETOOTH_ERROR_NONE;
1150 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1151 static void __bt_auto_connect_request_cb(GDBusProxy *proxy, GAsyncResult *res,
1155 GVariant *reply = NULL;
1157 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1158 g_object_unref(proxy);
1159 g_variant_unref(reply);
1163 g_dbus_error_strip_remote_error(err);
1164 BT_INFO("Auto_connect_request Dbus Call Error: %s", err->message);
1166 if (strcmp("Host is down", err->message)) {
1167 BT_INFO("Fail reason is not 'Host Down'. Terminate auto connect");
1168 _bt_audio_stop_auto_connect();
1169 vconf_set_str(BT_LAST_CONNECTED_DEVICE, "");
1172 g_clear_error(&err);
1176 // It is the function that retry to create alarm.
1177 // Sometimes alarm mgr service is created later than bluetooth-frwk service in booting time.
1178 // So, in this case, we have to retry alarmmgr_add_alarm.
1179 static gboolean __bt_audio_alarm_retry_cb(gpointer data)
1182 alarm_id_t alarm_id;
1184 BT_DBG("__bt_audio_alarm_retry_cb called.");
1185 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1186 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1188 if (result != BLUETOOTH_ERROR_NONE)
1191 auto_connect_timer.alarm_id = alarm_id;
1196 static int __bt_auto_connect_alarm_cb(alarm_id_t alarm_id, void* user_param)
1198 int result = BLUETOOTH_ERROR_NONE;
1199 char *address = NULL;
1200 alarm_id_t new_alarm_id;
1202 BT_INFO("alram id = [%d] , retry_count [%d] ",
1203 alarm_id, auto_connect_timer.retry_count);
1205 if (alarm_id != auto_connect_timer.alarm_id)
1208 address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1210 if (address == NULL) {
1211 BT_ERR("Address vconf is null");
1212 auto_connect_timer.alarm_id = 0;
1216 BT_DBG("ADDRESS [%s]", address);
1218 result = _bt_connect_profile(address, A2DP_SOURCE_UUID,
1219 __bt_auto_connect_request_cb, NULL);
1221 auto_connect_timer.retry_count--;
1223 BT_DBG("result [%d]", result);
1225 if (auto_connect_timer.retry_count <= 0) {
1226 BT_INFO("Stopping Auto connect retry");
1227 auto_connect_timer.alarm_id = 0;
1229 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1230 __bt_auto_connect_alarm_cb, NULL, &new_alarm_id);
1231 if (result == BLUETOOTH_ERROR_NONE)
1232 auto_connect_timer.alarm_id = new_alarm_id;
1239 static void __bt_auto_connect_alarm_remove()
1242 if (auto_connect_timer.alarm_id > 0) {
1243 _bt_service_remove_alarm(auto_connect_timer.alarm_id);
1244 auto_connect_timer.alarm_id = 0;
1246 auto_connect_timer.retry_count = 0;
1249 int _bt_audio_start_auto_connect(gboolean linkloss_flag)
1251 int result = BLUETOOTH_ERROR_NONE;
1252 char *address = NULL;
1253 alarm_id_t alarm_id = 0;
1257 address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1259 if (address == NULL) {
1260 BT_ERR("No target device");
1264 __bt_auto_connect_alarm_remove();
1266 auto_connect_timer.retry_count = BT_AUTO_CONNECT_TIMEOUT_RETRY_TIME /
1267 BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS;
1269 if (linkloss_flag) {
1270 BT_INFO("Start auto connection after linkloss");
1271 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1272 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1273 if (result != BLUETOOTH_ERROR_NONE)
1274 g_timeout_add(500, (GSourceFunc)__bt_audio_alarm_retry_cb, NULL);
1276 auto_connect_timer.alarm_id = alarm_id;
1279 BT_INFO("Start auto connection after BT activated");
1280 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_BT_ACTIVATED,
1281 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1282 if (result == BLUETOOTH_ERROR_NONE)
1283 auto_connect_timer.alarm_id = alarm_id;
1287 return BLUETOOTH_ERROR_NONE;
1290 int _bt_audio_stop_auto_connect(void)
1293 __bt_auto_connect_alarm_remove();
1295 return BLUETOOTH_ERROR_NONE;
1298 void _bt_audio_set_auto_connect_device_addr(const char *address)
1300 if (address == NULL) {
1301 BT_ERR("address is null");
1305 BT_INFO("Last connected device is [%s]", address);
1306 vconf_set_str(BT_LAST_CONNECTED_DEVICE, address);
1308 #endif /*TIZEN_BT_A2DP_SINK_AUTO_CONNECT */
1310 int _bt_audio_select_role(bluetooth_audio_role_t role)
1315 GError *error = NULL;
1316 GDBusConnection *g_conn = _bt_gdbus_get_system_gconn();
1317 gchar *adapter_path = _bt_get_adapter_path();
1319 BT_INFO("_bt_audio_select_role called [%d]", role);
1321 retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1322 BT_INFO("Adapter Path = %s", adapter_path);
1324 proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1325 BT_BLUEZ_NAME, adapter_path, BT_MEDIA_INTERFACE, NULL, &error);
1327 g_free(adapter_path);
1329 if (proxy == NULL) {
1330 BT_ERR("Unable to create proxy");
1333 BT_ERR("Error: %s", error->message);
1334 g_clear_error(&error);
1337 return BLUETOOTH_ERROR_INTERNAL;
1340 if (role == BLUETOOTH_A2DP_SOURCE)
1341 ret = g_dbus_proxy_call_sync(proxy, "SelectRole", g_variant_new("(s)", "source"),
1342 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1344 ret = g_dbus_proxy_call_sync(proxy, "SelectRole", g_variant_new("(s)", "sink"),
1345 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1347 g_object_unref(proxy);
1350 BT_ERR("Call SelectRole Failed");
1352 BT_ERR("errCode[%x], message[%s]", error->code, error->message);
1353 g_clear_error(&error);
1356 return BLUETOOTH_ERROR_INTERNAL;
1359 g_variant_unref(ret);
1360 return BLUETOOTH_ERROR_NONE;