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 #ifdef TIZEN_PROFILE_WEARABLE
41 #define VCONF_KEY_BT_HOST_BT_MAC_ADDR "db/wms/host_bt_mac"
48 char device_address[BT_ADDRESS_STRING_SIZE + 1];
49 } bt_connected_headset_data_t;
51 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
55 } bt_auto_connect_timer_t;
57 bt_auto_connect_timer_t auto_connect_timer = {0, };
60 static GList *g_connected_list;
62 static bt_headset_wait_t *g_wait_data;
64 static bt_audio_function_data_t *pdata;
66 static void __bt_remove_device_from_wait_list();
68 static void __bt_free_wait_data();
70 static gboolean __bt_device_support_uuid(char *remote_address,
71 bt_audio_type_t type);
73 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
74 static int __bt_auto_connect_alarm_cb(alarm_id_t alarm_id, void* user_param);
77 static void __bt_hf_request_cb(GDBusProxy *proxy, GAsyncResult *res,
80 GError *g_error = NULL;
81 GVariant *out_param1 = NULL;
82 GVariant *reply = NULL;
83 int result = BLUETOOTH_ERROR_NONE;
84 bt_function_data_t *func_data;
85 request_info_t *req_info;
87 reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
88 g_object_unref(proxy);
90 func_data = user_data;
92 if (func_data == NULL) {
94 BT_ERR("func_data == NULL");
98 req_info = _bt_get_request_info(func_data->req_id);
99 if (req_info == NULL) {
100 BT_ERR("req_info == NULL");
105 BT_ERR("HF Connect Dbus Call Error");
106 result = _bt_convert_gerror(g_error);
108 BT_ERR("Error: %s\n", g_error->message);
109 g_clear_error(&g_error);
112 g_variant_unref(reply);
115 if (req_info->context == NULL)
118 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
119 func_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
121 g_dbus_method_invocation_return_value(req_info->context,
122 g_variant_new("(iv)", result, out_param1));
124 _bt_delete_request_list(req_info->req_id);
128 g_free(func_data->address);
133 void _bt_audio_check_pending_connect()
136 bluetooth_device_address_t device_address;
141 if (pdata->pending == BT_PENDING_CONNECT) {
143 _bt_convert_addr_string_to_type(device_address.addr,
145 _bt_audio_connect(pdata->req_id,
150 g_free(pdata->address);
159 static void __bt_audio_request_cb(GDBusProxy *proxy, GAsyncResult *res,
162 GError *g_error = NULL;
163 GVariant *out_param1 = NULL;
164 GVariant *reply = NULL;
165 int result = BLUETOOTH_ERROR_NONE;
166 bt_audio_function_data_t *func_data;
167 request_info_t *req_info;
169 reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
170 g_object_unref(proxy);
171 g_variant_unref(reply);
173 func_data = user_data;
175 if (func_data == NULL) {
177 BT_ERR("func_data == NULL");
181 if (func_data->pending != BT_PENDING_NONE && g_error == NULL) {
182 bluetooth_device_address_t device_address;
183 _bt_convert_addr_string_to_type(device_address.addr,
186 if (func_data->pending == BT_PENDING_CONNECT) {
188 if (__bt_device_support_uuid(func_data->address,
191 pdata = g_new0(bt_audio_function_data_t, 1);
192 pdata->req_id = func_data->req_id;
193 pdata->address = strdup(func_data->address);
194 pdata->pending = func_data->pending;
199 if (_bt_is_service_connected(func_data->address
201 _bt_audio_disconnect(func_data->req_id,
202 BT_AUDIO_HSP, &device_address, NULL);
211 req_info = _bt_get_request_info(func_data->req_id);
212 if (req_info == NULL) {
213 BT_ERR("req_info == NULL");
220 BT_ERR("Audio Connect/Disconnect Dbus Call Error: %s\n", g_error->message);
222 result = BLUETOOTH_ERROR_INTERNAL;
223 /* If there is error then we need to set local initiated connection as false
224 * If don't do this then when headset initiate connection for HFP then we don't
225 * initiate connection for A2dp to headset as this flag was set to true in first
226 * connection failure attempt and not set in error case.*/
227 _bt_headset_set_local_connection(FALSE);
229 /* Remove the device from the list */
230 _bt_remove_headset_from_list(func_data->type, func_data->address);
232 /* Error, check if any waiting device is there */
233 if (g_wait_data == NULL)
236 if (g_strcmp0(g_wait_data->address, func_data->address) != 0) {
237 bluetooth_device_address_t device_address;
238 _bt_convert_addr_string_to_type(device_address.addr,
239 g_wait_data->address);
240 _bt_audio_connect(g_wait_data->req_id, g_wait_data->type,
241 &device_address, NULL);
244 /* Event will be sent by the event reciever */
246 if (req_info->context == NULL) {
247 BT_DBG("req_info->context is NULL");
251 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
252 func_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
254 g_dbus_method_invocation_return_value(req_info->context,
255 g_variant_new("(iv)", result, out_param1));
257 _bt_delete_request_list(req_info->req_id);
259 g_clear_error(&g_error);
262 g_free(func_data->address);
267 static void __bt_free_wait_data()
269 if (g_wait_data != NULL) {
270 g_free(g_wait_data->address);
276 static void __bt_remove_device_from_wait_list()
278 /* Before deleting the request update the UI */
279 GVariant *out_param_1 = NULL;
280 int result = BLUETOOTH_ERROR_INTERNAL;
281 request_info_t *req_info;
283 req_info = _bt_get_request_info(g_wait_data->req_id);
284 if (req_info == NULL) {
285 BT_ERR("req_info == NULL");
289 out_param_1 = g_variant_new_from_data((const GVariantType *)"ay",
290 g_wait_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
292 g_dbus_method_invocation_return_value(req_info->context,
293 g_variant_new("(iv)", result, out_param_1));
295 _bt_delete_request_list(g_wait_data->req_id);
298 static void __bt_set_headset_disconnection_type(const char *address)
300 bt_connected_headset_data_t *connected_device;
303 node = g_list_first(g_connected_list);
304 while (node != NULL) {
305 connected_device = node->data;
306 if (g_strcmp0(connected_device->device_address, address) == 0) {
307 g_wait_data->disconnection_type = connected_device->type;
310 node = g_list_next(node);
314 gboolean _bt_is_headset_type_connected(int type, char *address)
318 node = g_list_first(g_connected_list);
319 while (node != NULL) {
320 bt_connected_headset_data_t *connected_device = node->data;
322 if (connected_device->type & type) {
324 g_strlcpy(address, connected_device->device_address,
325 BT_ADDRESS_STRING_SIZE + 1);
329 node = g_list_next(node);
334 #ifdef TIZEN_SUPPORT_DUAL_HF
335 gboolean __bt_is_companion_device(const char *addr)
337 #ifdef TIZEN_PROFILE_WEARABLE
338 char *host_device_address = NULL;
339 host_device_address = vconf_get_str(VCONF_KEY_BT_HOST_BT_MAC_ADDR);
341 if (!host_device_address) {
342 BT_INFO("Failed to get a companion device address");
346 if (g_strcmp0(host_device_address, addr) == 0) {
347 BT_INFO("Found companion device");
348 free(host_device_address);
352 free(host_device_address);
355 /* TODO : Need to add companion device check condition for Phone models */
361 static int __bt_is_headset_connected(int type, int req_id,
364 gboolean connected = FALSE;
365 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
366 bluetooth_device_address_t device_address;
367 bt_connected_headset_data_t *connected_device = NULL;
368 #ifdef TIZEN_SUPPORT_DUAL_HF
369 gboolean is_companion_device = FALSE;
372 /* Check if any other headset is connected */
375 node = g_list_first(g_connected_list);
376 while (node != NULL) {
377 connected_device = node->data;
378 if ((connected_device->type & type)) {
379 g_strlcpy(connected_address, connected_device->device_address,
380 BT_ADDRESS_STRING_SIZE + 1);
381 #ifdef TIZEN_SUPPORT_DUAL_HF
382 is_companion_device = __bt_is_companion_device(connected_address);
383 BT_INFO(" is_companion_device[%d]", is_companion_device);
385 if (!is_companion_device) {
394 node = g_list_next(node);
398 return BLUETOOTH_ERROR_NOT_CONNECTED;
400 BT_DBG("connected headset %s", connected_address);
402 if (g_strcmp0(connected_address, address) == 0)
403 return BLUETOOTH_ERROR_ALREADY_CONNECT;
404 #ifdef TIZEN_SUPPORT_DUAL_HF
405 else if (TRUE == __bt_is_companion_device(address))
406 return BLUETOOTH_ERROR_NOT_CONNECTED;
409 /* Convert BD adress from string type */
410 _bt_convert_addr_string_to_type(device_address.addr, connected_address);
411 int value = BLUETOOTH_ERROR_NONE;
412 value = _bt_audio_disconnect(0, connected_device->type & type, &device_address, NULL);
414 /* If already one device is waiting, remove current waiting device and add new */
415 if (value == BLUETOOTH_ERROR_NONE) {
416 if (g_wait_data != NULL) {
417 if (g_strcmp0(g_wait_data->address, address) != 0) {
418 __bt_remove_device_from_wait_list();
419 __bt_free_wait_data();
423 if (g_wait_data == NULL) {
424 g_wait_data = g_malloc0(sizeof(bt_headset_wait_t));
425 g_wait_data->address = g_strdup(address);
426 g_wait_data->req_id = req_id;
427 g_wait_data->type = type;
428 g_wait_data->ag_flag = FALSE;
430 /* Set disconnection type */
431 __bt_set_headset_disconnection_type(connected_address);
438 static int __bt_is_headset_connecting(int type)
440 bt_connected_headset_data_t *connected_device = NULL;
442 /* Check if any other headset is connected */
445 node = g_list_first(g_connected_list);
446 while (node != NULL) {
447 connected_device = node->data;
448 if (connected_device->device_state == BT_STATE_CONNECTING)
449 return BLUETOOTH_ERROR_CONNECTION_BUSY;
450 node = g_list_next(node);
453 return BLUETOOTH_ERROR_NONE;
456 int __bt_is_headset_disconnecting(int type)
458 bt_connected_headset_data_t *connected_device = NULL;
460 /* Check if any other headset is connected */
463 node = g_list_first(g_connected_list);
464 while (node != NULL) {
465 connected_device = node->data;
466 if (connected_device->device_state == BT_STATE_DISCONNECTING)
467 return BLUETOOTH_ERROR_CONNECTION_BUSY;
469 node = g_list_next(node);
472 return BLUETOOTH_ERROR_NONE;
475 void _bt_set_audio_wait_data_flag(gboolean flag)
477 BT_DBG("_bt_set_audio_wait_data_flag \n");
478 g_wait_data->ag_flag = flag;
481 bt_headset_wait_t *_bt_get_audio_wait_data(void)
483 BT_DBG("_bt_get_audio_wait_data \n");
487 void _bt_rel_wait_data(void)
489 BT_DBG("_bt_rel_wait_data \n");
490 __bt_free_wait_data();
493 void _bt_add_headset_to_list(int type, int status, const char *address)
495 bt_connected_headset_data_t *connected_device;
496 bt_connected_headset_data_t *device;
499 BT_DBG("_bt_add_headset_to_list \n");
501 node = g_list_first(g_connected_list);
502 while (node != NULL) {
503 device = (bt_connected_headset_data_t *)node->data;
505 if (g_strcmp0(device->device_address, address) == 0) {
506 BT_DBG("Address match, update connection type \n");
507 if (status == BT_STATE_CONNECTED)
508 device->type |= type;
509 device->device_state = status;
512 node = g_list_next(node);
515 connected_device = g_malloc0(sizeof(bt_connected_headset_data_t));
516 connected_device->device_state = status;
517 if ((status == BT_STATE_CONNECTED) || (status == BT_STATE_CONNECTING))
518 connected_device->type |= type;
519 g_strlcpy(connected_device->device_address, address,
520 sizeof(connected_device->device_address));
521 g_connected_list = g_list_append(g_connected_list, connected_device);
524 int _bt_get_device_state_from_list(int type, const char *address)
527 bt_connected_headset_data_t *device;
530 node = g_list_first(g_connected_list);
531 while (node != NULL) {
532 device = (bt_connected_headset_data_t *)node->data;
533 if (g_strcmp0(device->device_address, address) == 0) {
534 BT_DBG("Device found");
535 return device->device_state;
537 node = g_list_next(node);
540 BT_DBG("Device not found");
541 return BLUETOOTH_ERROR_INTERNAL;
544 void _bt_remove_headset_from_list(int type, const char *address)
548 BT_DBG("_bt_remove_headset_from_list \n");
550 node = g_list_first(g_connected_list);
551 while (node != NULL) {
552 bt_connected_headset_data_t *connected_device = node->data;
554 if (g_strcmp0(connected_device->device_address, address) != 0) {
555 node = g_list_next(node);
559 BT_DBG("Address match \n");
561 BT_DBG("Connection type = %x\n", connected_device->type);
565 if (connected_device->type & BT_AUDIO_A2DP)
566 connected_device->type &= ~(BT_AUDIO_A2DP);
569 if (connected_device->type & BT_AUDIO_HSP)
570 connected_device->type &= ~(BT_AUDIO_HSP);
573 if (connected_device->type & BT_AUDIO_ALL)
574 connected_device->type &= ~(BT_AUDIO_ALL);
577 if (connected_device->type & BT_AVRCP)
578 connected_device->type &= ~(BT_AVRCP);
580 case BT_AUDIO_A2DP_SOURCE:
581 if (connected_device->type & BT_AUDIO_A2DP_SOURCE)
582 connected_device->type &= ~(BT_AUDIO_A2DP_SOURCE);
585 BT_DBG("Connection type = %x\n", connected_device->type);
587 if (connected_device->type == 0x00) {
588 g_connected_list = g_list_remove(g_connected_list, connected_device);
589 g_free(connected_device);
591 connected_device->device_state = BT_STATE_CONNECTED;
594 node = g_list_next(node);
598 static gboolean __bt_device_support_uuid(char *remote_address,
599 bt_audio_type_t type)
601 GArray *dev_list = NULL;
605 bluetooth_device_info_t info;
606 char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
607 gboolean ret = FALSE;
611 dev_list = g_array_new(FALSE, FALSE, sizeof(gchar));
613 _bt_get_bonded_devices(&dev_list);
614 size = (dev_list->len) / sizeof(bluetooth_device_info_t);
616 for (i = 0; i < size; i++) {
617 info = g_array_index(dev_list, bluetooth_device_info_t, i);
618 _bt_convert_addr_type_to_string(bond_address,
619 info.device_address.addr);
620 if (strcmp(bond_address, remote_address) != 0)
623 BT_INFO("Device address Matched");
625 while (j != info.service_index) {
626 if (type == BT_AUDIO_HSP) {
627 if (strcmp(info.uuids[j], HFP_HS_UUID) == 0) {
628 BT_INFO("HFP HS UUID exists");
632 } else if (type == BT_AUDIO_A2DP) {
633 if (strcmp(info.uuids[j], A2DP_SINK_UUID) == 0) {
634 BT_INFO("A2DP SINK UUID exists");
643 g_array_free(dev_list, TRUE);
648 gboolean _bt_is_service_connected(char* address, int type)
652 node = g_list_first(g_connected_list);
653 while (node != NULL) {
654 bt_connected_headset_data_t *conn_device = node->data;
656 if ((g_strcmp0(conn_device->device_address, address) == 0) &&
657 (conn_device->type & type)) {
658 BT_INFO("Service connected");
662 node = g_list_next(node);
664 BT_INFO("Service not connected");
668 int _bt_audio_connect(int request_id, int type,
669 bluetooth_device_address_t *device_address,
672 int result = BLUETOOTH_ERROR_NONE;
673 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
674 GDBusProxy *adapter_proxy;
675 GDBusConnection *g_conn;
678 int value = BLUETOOTH_ERROR_NONE;
679 bt_audio_function_data_t *func_data;
680 guint hfp_hs_restricted = 0x0; /* set default "allowed" */
681 guint a2dp_restricted = 0x0;
683 BT_CHECK_PARAMETER(device_address, return);
685 adapter_proxy = _bt_get_adapter_proxy();
686 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
688 g_conn = _bt_gdbus_get_system_gconn();
689 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
691 _bt_convert_addr_type_to_string(address, device_address->addr);
693 func_data = g_malloc0(sizeof(bt_audio_function_data_t));
694 func_data->address = g_strdup(address);
695 func_data->req_id = request_id;
696 func_data->type = type;
697 func_data->pending = BT_PENDING_NONE;
699 result = _bt_get_restrict_profile(device_address, RESTRICTED_PROFILE_HFP_HS, &hfp_hs_restricted);
700 if (result != BLUETOOTH_ERROR_NONE)
701 BT_ERR("Can't get hfp_hs restriction info");
703 result = _bt_get_restrict_profile(device_address, RESTRICTED_PROFILE_A2DP, &a2dp_restricted);
704 if (result != BLUETOOTH_ERROR_NONE)
705 BT_ERR("Can't get a2dp restriction info");
712 uuid = A2DP_SINK_UUID;
715 uuid = AVRCP_TARGET_UUID;
717 case BT_AUDIO_A2DP_SOURCE:
718 uuid = A2DP_SOURCE_UUID;
721 if ((hfp_hs_restricted == 0x0) && (a2dp_restricted == 0x0)) { /* in case of both profiles are not restricted */
722 if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
724 func_data->pending = BT_PENDING_CONNECT;
726 } else if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
727 uuid = A2DP_SINK_UUID;
728 type = BT_AUDIO_A2DP;
730 BT_ERR("No audio role supported");
731 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
734 } else if ((hfp_hs_restricted == 0x1) && (a2dp_restricted == 0x1)) { /* in case of both profiles are restricted */
735 BT_ERR("All profiles are restricted");
736 result = BLUETOOTH_ERROR_INTERNAL;
738 } else if (a2dp_restricted == 0x01) { /* in case of a2dp is restricted, only connection for hfp_hs */
739 if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
743 BT_ERR("HFP_HS role is not supported");
744 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
747 } else if (hfp_hs_restricted == 0x01) { /* in case of hfp_hs is restricted, only connection for a2dp */
748 if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
749 uuid = A2DP_SINK_UUID;
750 type = BT_AUDIO_A2DP;
752 BT_ERR("A2DP role is not supported");
753 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
758 case BT_AVRCP_TARGET:
759 uuid = AVRCP_REMOTE_UUID;
762 BT_ERR("Unknown role");
763 result = BLUETOOTH_ERROR_INTERNAL;
766 BT_INFO("Connecting to service %s", uuid);
768 value = __bt_is_headset_connected(type, request_id, address);
770 if (value == BLUETOOTH_ERROR_ALREADY_CONNECT) {
771 return BLUETOOTH_ERROR_ALREADY_CONNECT;
772 } else if (value == BLUETOOTH_ERROR_NOT_CONNECTED) {
773 value = __bt_is_headset_connecting(type);
774 if (value != BLUETOOTH_ERROR_NONE) {
775 result = BLUETOOTH_ERROR_IN_PROGRESS;
778 _bt_headset_set_local_connection(TRUE);
779 ret = _bt_connect_profile(address, uuid,
780 __bt_audio_request_cb, func_data);
782 if (ret != BLUETOOTH_ERROR_NONE) {
783 BT_ERR("_bt_connect_profile Error");
784 _bt_headset_set_local_connection(FALSE);
785 g_free(func_data->address);
790 /* Add data to the connected list */
791 _bt_add_headset_to_list(type, BT_STATE_CONNECTING, address);
792 } else if (value == BLUETOOTH_ERROR_IN_PROGRESS) {
793 return BLUETOOTH_ERROR_IN_PROGRESS;
797 g_array_free(out_param1, TRUE);
799 return BLUETOOTH_ERROR_NONE;
801 if (out_param1 != NULL)
802 g_array_append_vals(out_param1, address,
808 int _bt_audio_disconnect(int request_id, int type,
809 bluetooth_device_address_t *device_address,
812 int result = BLUETOOTH_ERROR_NONE;
813 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
814 bt_audio_function_data_t *func_data;
815 GDBusProxy *adapter_proxy;
816 GDBusConnection *g_conn;
820 int value = BLUETOOTH_ERROR_NONE;
822 BT_CHECK_PARAMETER(device_address, return);
824 adapter_proxy = _bt_get_adapter_proxy();
825 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
827 g_conn = _bt_gdbus_get_system_gconn();
828 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
830 _bt_convert_addr_type_to_string(address, device_address->addr);
832 func_data = g_malloc0(sizeof(bt_audio_function_data_t));
833 func_data->address = g_strdup(address);
834 func_data->req_id = request_id;
835 func_data->pending = BT_PENDING_NONE;
836 func_data->type = type;
843 uuid = A2DP_SINK_UUID;
846 uuid = AVRCP_TARGET_UUID;
848 case BT_AUDIO_A2DP_SOURCE:
849 uuid = A2DP_SOURCE_UUID;
852 if (_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
853 uuid = A2DP_SINK_UUID;
854 func_data->pending = BT_PENDING_DISCONNECT;
855 } else if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
858 BT_ERR("No audio service connected");
859 result = BLUETOOTH_ERROR_NOT_CONNECTED;
863 case BT_AVRCP_TARGET:
864 uuid = AVRCP_REMOTE_UUID;
867 BT_ERR("Unknown role");
868 result = BLUETOOTH_ERROR_INTERNAL;
871 value = __bt_is_headset_disconnecting(type);
872 if (value != BLUETOOTH_ERROR_NONE) {
873 BT_INFO("Disconnect in progress");
874 result = BLUETOOTH_ERROR_IN_PROGRESS;
877 BT_INFO("Disconnecting service %s", uuid);
878 ret = _bt_disconnect_profile(address, uuid,
879 __bt_audio_request_cb, func_data);
881 if (ret != BLUETOOTH_ERROR_NONE) {
882 BT_ERR("_bt_disconnect_profile Error");
883 g_free(func_data->address);
889 * This logic is added for dual HF mode issue.
891 node = g_list_first(g_connected_list);
892 while (node != NULL) {
893 bt_connected_headset_data_t *connected_device = node->data;
895 if (g_strcmp0(connected_device->device_address, address) == 0) {
896 BT_DBG("Connection type update");
897 type = connected_device->type;
900 node = g_list_next(node);
902 _bt_add_headset_to_list(type, BT_STATE_DISCONNECTING, address);
905 g_array_free(out_param1, TRUE);
907 return BLUETOOTH_ERROR_NONE;
909 if (out_param1 != NULL)
910 g_array_append_vals(out_param1, address,
916 void _bt_remove_from_connected_list(const char *address)
918 bt_connected_headset_data_t *connected_device;
921 node = g_list_first(g_connected_list);
922 while (node != NULL) {
923 connected_device = node->data;
924 if (connected_device != NULL &&
925 g_strcmp0(connected_device->device_address, address) == 0) {
926 BT_ERR("Device is removed from the list");
927 g_connected_list = g_list_remove(g_connected_list, connected_device);
928 g_free(connected_device);
931 node = g_list_next(node);
935 int _bt_hf_connect(int request_id,
936 bluetooth_device_address_t *device_address,
939 int result = BLUETOOTH_ERROR_NONE;
940 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
941 bt_function_data_t *func_data;
942 GDBusProxy *adapter_proxy;
943 GDBusConnection *g_conn;
944 gboolean connected = FALSE;
948 BT_CHECK_PARAMETER(device_address, return);
950 _bt_convert_addr_type_to_string(address, device_address->addr);
952 adapter_proxy = _bt_get_adapter_proxy();
953 if (adapter_proxy == NULL) {
954 result = BLUETOOTH_ERROR_INTERNAL;
958 g_conn = _bt_gdbus_get_system_gconn();
959 if (g_conn == NULL) {
960 result = BLUETOOTH_ERROR_INTERNAL;
964 /* Check if HF is connected or not */
965 result = _bt_is_device_connected(device_address, BT_PROFILE_CONN_HFG, &connected);
966 if (connected == TRUE) {
967 BT_ERR("HF is already connected");
968 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
972 func_data = g_malloc0(sizeof(bt_function_data_t));
973 func_data->address = g_strdup(address);
974 func_data->req_id = request_id;
975 uuid = g_strdup(HFP_AG_UUID);
977 BT_DBG("Connecting to service %s", uuid);
979 ret = _bt_connect_profile(address, uuid,
980 __bt_hf_request_cb, func_data);
982 if (ret != BLUETOOTH_ERROR_NONE) {
983 BT_ERR("_bt_connect_profile Error");
984 g_free(func_data->address);
991 g_array_free(out_param1, TRUE);
993 return BLUETOOTH_ERROR_NONE;
995 if (out_param1 != NULL)
996 g_array_append_vals(out_param1, address,
1002 int _bt_hf_disconnect(int request_id,
1003 bluetooth_device_address_t *device_address,
1006 int result = BLUETOOTH_ERROR_NONE;
1007 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1008 bt_function_data_t *func_data;
1009 GDBusProxy *adapter_proxy;
1010 GDBusConnection *g_conn;
1015 BT_CHECK_PARAMETER(device_address, return);
1017 _bt_convert_addr_type_to_string(address, device_address->addr);
1019 adapter_proxy = _bt_get_adapter_proxy();
1020 if (adapter_proxy == NULL) {
1021 result = BLUETOOTH_ERROR_INTERNAL;
1025 g_conn = _bt_gdbus_get_system_gconn();
1026 if (g_conn == NULL) {
1027 result = BLUETOOTH_ERROR_INTERNAL;
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("Disconnecting service %s", uuid);
1037 ret = _bt_disconnect_profile(address, uuid,
1038 __bt_hf_request_cb, func_data);
1040 if (ret != BLUETOOTH_ERROR_NONE) {
1041 BT_ERR("_bt_disconnect_profile Error");
1042 g_free(func_data->address);
1049 g_array_free(out_param1, TRUE);
1051 return BLUETOOTH_ERROR_NONE;
1053 if (out_param1 != NULL)
1054 g_array_append_vals(out_param1, address,
1055 BT_ADDRESS_STR_LEN);
1060 int _bt_audio_set_content_protect(gboolean status)
1062 GDBusConnection *conn;
1063 GError *error = NULL;
1067 conn = _bt_gdbus_get_system_gconn();
1068 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1070 BT_DBG("Content Protection status = [%d]", status);
1072 g_dbus_connection_emit_signal(conn,
1073 NULL, BT_CONTENT_PROTECTION_PATH,
1074 BT_CONTENT_PROTECTION_INTERFACE,
1075 "ProtectionRequired",
1076 g_variant_new("(b)", status),
1080 /* dBUS gives error cause */
1081 ERR("Could not Emit Signal: errCode[%x], message[%s]",
1082 error->code, error->message);
1083 g_clear_error(&error);
1084 return BLUETOOTH_ERROR_INTERNAL;
1087 BT_DBG("Emit Signal done = [ProtectionRequired]");
1088 return BLUETOOTH_ERROR_NONE;
1091 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1092 static void __bt_auto_connect_request_cb(GDBusProxy *proxy, GAsyncResult *res,
1096 GVariant *reply = NULL;
1098 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1099 g_object_unref(proxy);
1100 g_variant_unref(reply);
1104 g_dbus_error_strip_remote_error(err);
1105 BT_INFO("Auto_connect_request Dbus Call Error: %s", err->message);
1107 if (strcmp("Host is down", err->message)) {
1108 BT_INFO("Fail reason is not 'Host Down'. Terminate auto connect");
1109 _bt_audio_stop_auto_connect();
1110 vconf_set_str(BT_LAST_CONNECTED_DEVICE, "");
1113 g_clear_error(&err);
1117 // It is the function that retry to create alarm.
1118 // Sometimes alarm mgr service is created later than bluetooth-frwk service in booting time.
1119 // So, in this case, we have to retry alarmmgr_add_alarm.
1120 static gboolean __bt_audio_alarm_retry_cb(gpointer data)
1123 alarm_id_t alarm_id;
1125 BT_DBG("__bt_audio_alarm_retry_cb called.");
1126 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1127 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1129 if (result != BLUETOOTH_ERROR_NONE)
1132 auto_connect_timer.alarm_id = alarm_id;
1137 static int __bt_auto_connect_alarm_cb(alarm_id_t alarm_id, void* user_param)
1139 int result = BLUETOOTH_ERROR_NONE;
1140 char *address = NULL;
1141 alarm_id_t new_alarm_id;
1143 BT_INFO("alram id = [%d] , retry_count [%d] ",
1144 alarm_id, auto_connect_timer.retry_count);
1146 if (alarm_id != auto_connect_timer.alarm_id)
1149 address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1151 if (address == NULL) {
1152 BT_ERR("Address vconf is null");
1153 auto_connect_timer.alarm_id = 0;
1157 BT_DBG("ADDRESS [%s]", address);
1159 result = _bt_connect_profile(address, A2DP_SOURCE_UUID,
1160 __bt_auto_connect_request_cb, NULL);
1162 auto_connect_timer.retry_count--;
1164 BT_DBG("result [%d]", result);
1166 if (auto_connect_timer.retry_count <= 0) {
1167 BT_INFO("Stopping Auto connect retry");
1168 auto_connect_timer.alarm_id = 0;
1170 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1171 __bt_auto_connect_alarm_cb, NULL, &new_alarm_id);
1172 if (result == BLUETOOTH_ERROR_NONE)
1173 auto_connect_timer.alarm_id = new_alarm_id;
1180 static void __bt_auto_connect_alarm_remove()
1183 if (auto_connect_timer.alarm_id > 0) {
1184 _bt_service_remove_alarm(auto_connect_timer.alarm_id);
1185 auto_connect_timer.alarm_id = 0;
1187 auto_connect_timer.retry_count = 0;
1190 int _bt_audio_start_auto_connect(gboolean linkloss_flag)
1192 int result = BLUETOOTH_ERROR_NONE;
1193 char *address = NULL;
1194 alarm_id_t alarm_id = 0;
1198 address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1200 if (address == NULL) {
1201 BT_ERR("No target device");
1205 __bt_auto_connect_alarm_remove();
1207 auto_connect_timer.retry_count = BT_AUTO_CONNECT_TIMEOUT_RETRY_TIME /
1208 BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS;
1210 if (linkloss_flag) {
1211 BT_INFO("Start auto connection after linkloss");
1212 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1213 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1214 if (result != BLUETOOTH_ERROR_NONE)
1215 g_timeout_add(500, (GSourceFunc)__bt_audio_alarm_retry_cb, NULL);
1217 auto_connect_timer.alarm_id = alarm_id;
1220 BT_INFO("Start auto connection after BT activated");
1221 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_BT_ACTIVATED,
1222 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1223 if (result == BLUETOOTH_ERROR_NONE)
1224 auto_connect_timer.alarm_id = alarm_id;
1228 return BLUETOOTH_ERROR_NONE;
1231 int _bt_audio_stop_auto_connect(void)
1234 __bt_auto_connect_alarm_remove();
1236 return BLUETOOTH_ERROR_NONE;
1239 void _bt_audio_set_auto_connect_device_addr(const char *address)
1241 if (address == NULL) {
1242 BT_ERR("address is null");
1246 BT_INFO("Last connected device is [%s]", address);
1247 vconf_set_str(BT_LAST_CONNECTED_DEVICE, address);
1249 #endif /*TIZEN_BT_A2DP_SINK_AUTO_CONNECT */