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));
517 connected_device->device_state = status;
518 if ((status == BT_STATE_CONNECTED) || (status == BT_STATE_CONNECTING))
519 connected_device->type |= type;
520 g_strlcpy(connected_device->device_address, address,
521 sizeof(connected_device->device_address));
522 g_connected_list = g_list_append(g_connected_list, connected_device);
525 int _bt_get_device_state_from_list(int type, const char *address)
528 bt_connected_headset_data_t *device;
531 node = g_list_first(g_connected_list);
532 while (node != NULL) {
533 device = (bt_connected_headset_data_t *)node->data;
534 if (g_strcmp0(device->device_address, address) == 0) {
535 BT_DBG("Device found");
536 return device->device_state;
538 node = g_list_next(node);
541 BT_DBG("Device not found");
542 return BLUETOOTH_ERROR_INTERNAL;
545 void _bt_remove_headset_from_list(int type, const char *address)
549 BT_DBG("_bt_remove_headset_from_list \n");
551 node = g_list_first(g_connected_list);
552 while (node != NULL) {
553 bt_connected_headset_data_t *connected_device = node->data;
555 if (g_strcmp0(connected_device->device_address, address) != 0) {
556 node = g_list_next(node);
560 BT_DBG("Address match \n");
562 BT_DBG("Connection type = %x\n", connected_device->type);
566 if (connected_device->type & BT_AUDIO_A2DP)
567 connected_device->type &= ~(BT_AUDIO_A2DP);
570 if (connected_device->type & BT_AUDIO_HSP)
571 connected_device->type &= ~(BT_AUDIO_HSP);
574 if (connected_device->type & BT_AUDIO_ALL)
575 connected_device->type &= ~(BT_AUDIO_ALL);
578 if (connected_device->type & BT_AVRCP)
579 connected_device->type &= ~(BT_AVRCP);
581 case BT_AUDIO_A2DP_SOURCE:
582 if (connected_device->type & BT_AUDIO_A2DP_SOURCE)
583 connected_device->type &= ~(BT_AUDIO_A2DP_SOURCE);
586 BT_DBG("Connection type = %x\n", connected_device->type);
588 if (connected_device->type == 0x00) {
589 g_connected_list = g_list_remove(g_connected_list, connected_device);
590 g_free(connected_device);
592 connected_device->device_state = BT_STATE_CONNECTED;
595 node = g_list_next(node);
599 static gboolean __bt_device_support_uuid(char *remote_address,
600 bt_audio_type_t type)
602 GArray *dev_list = NULL;
606 bluetooth_device_info_t info;
607 char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
608 gboolean ret = FALSE;
612 dev_list = g_array_new(FALSE, FALSE, sizeof(gchar));
614 _bt_get_bonded_devices(&dev_list);
615 size = (dev_list->len) / sizeof(bluetooth_device_info_t);
617 for (i = 0; i < size; i++) {
618 info = g_array_index(dev_list, bluetooth_device_info_t, i);
619 _bt_convert_addr_type_to_string(bond_address,
620 info.device_address.addr);
621 if (strcmp(bond_address, remote_address) != 0)
624 BT_INFO("Device address Matched");
626 while (j != info.service_index) {
627 if (type == BT_AUDIO_HSP) {
628 if (strcmp(info.uuids[j], HFP_HS_UUID) == 0) {
629 BT_INFO("HFP HS UUID exists");
633 } else if (type == BT_AUDIO_A2DP) {
634 if (strcmp(info.uuids[j], A2DP_SINK_UUID) == 0) {
635 BT_INFO("A2DP SINK UUID exists");
644 g_array_free(dev_list, TRUE);
649 gboolean _bt_is_service_connected(char* address, int type)
653 node = g_list_first(g_connected_list);
654 while (node != NULL) {
655 bt_connected_headset_data_t *conn_device = node->data;
657 if ((g_strcmp0(conn_device->device_address, address) == 0) &&
658 (conn_device->type & type)) {
659 BT_INFO("Service connected");
663 node = g_list_next(node);
665 BT_INFO("Service not connected");
669 int _bt_audio_connect(int request_id, int type,
670 bluetooth_device_address_t *device_address,
673 int result = BLUETOOTH_ERROR_NONE;
674 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
675 GDBusProxy *adapter_proxy;
676 GDBusConnection *g_conn;
679 int value = BLUETOOTH_ERROR_NONE;
680 bt_audio_function_data_t *func_data;
681 guint hfp_hs_restricted = 0x0; /* set default "allowed" */
682 guint a2dp_restricted = 0x0;
684 BT_CHECK_PARAMETER(device_address, return);
686 adapter_proxy = _bt_get_adapter_proxy();
687 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
689 g_conn = _bt_gdbus_get_system_gconn();
690 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
692 _bt_convert_addr_type_to_string(address, device_address->addr);
694 func_data = g_malloc0(sizeof(bt_audio_function_data_t));
696 func_data->address = g_strdup(address);
697 func_data->req_id = request_id;
698 func_data->type = type;
699 func_data->pending = BT_PENDING_NONE;
701 result = _bt_get_restrict_profile(device_address, RESTRICTED_PROFILE_HFP_HS, &hfp_hs_restricted);
702 if (result != BLUETOOTH_ERROR_NONE)
703 BT_ERR("Can't get hfp_hs restriction info");
705 result = _bt_get_restrict_profile(device_address, RESTRICTED_PROFILE_A2DP, &a2dp_restricted);
706 if (result != BLUETOOTH_ERROR_NONE)
707 BT_ERR("Can't get a2dp restriction info");
714 uuid = A2DP_SINK_UUID;
717 uuid = AVRCP_TARGET_UUID;
719 case BT_AUDIO_A2DP_SOURCE:
720 uuid = A2DP_SOURCE_UUID;
723 if ((hfp_hs_restricted == 0x0) && (a2dp_restricted == 0x0)) { /* in case of both profiles are not restricted */
724 if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
726 func_data->pending = BT_PENDING_CONNECT;
728 } else if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
729 uuid = A2DP_SINK_UUID;
730 type = BT_AUDIO_A2DP;
732 BT_ERR("No audio role supported");
733 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
736 } else if ((hfp_hs_restricted == 0x1) && (a2dp_restricted == 0x1)) { /* in case of both profiles are restricted */
737 BT_ERR("All profiles are restricted");
738 result = BLUETOOTH_ERROR_INTERNAL;
740 } else if (a2dp_restricted == 0x01) { /* in case of a2dp is restricted, only connection for hfp_hs */
741 if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
745 BT_ERR("HFP_HS role is not supported");
746 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
749 } else if (hfp_hs_restricted == 0x01) { /* in case of hfp_hs is restricted, only connection for a2dp */
750 if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
751 uuid = A2DP_SINK_UUID;
752 type = BT_AUDIO_A2DP;
754 BT_ERR("A2DP role is not supported");
755 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
760 case BT_AVRCP_TARGET:
761 uuid = AVRCP_REMOTE_UUID;
764 BT_ERR("Unknown role");
765 result = BLUETOOTH_ERROR_INTERNAL;
768 BT_INFO("Connecting to service %s", uuid);
770 value = __bt_is_headset_connected(type, request_id, address);
772 if (value == BLUETOOTH_ERROR_ALREADY_CONNECT) {
773 return BLUETOOTH_ERROR_ALREADY_CONNECT;
774 } else if (value == BLUETOOTH_ERROR_NOT_CONNECTED) {
775 value = __bt_is_headset_connecting(type);
776 if (value != BLUETOOTH_ERROR_NONE) {
777 result = BLUETOOTH_ERROR_IN_PROGRESS;
780 _bt_headset_set_local_connection(TRUE);
781 ret = _bt_connect_profile(address, uuid,
782 __bt_audio_request_cb, func_data);
784 if (ret != BLUETOOTH_ERROR_NONE) {
785 BT_ERR("_bt_connect_profile Error");
786 _bt_headset_set_local_connection(FALSE);
787 g_free(func_data->address);
792 /* Add data to the connected list */
793 _bt_add_headset_to_list(type, BT_STATE_CONNECTING, address);
794 } else if (value == BLUETOOTH_ERROR_IN_PROGRESS) {
795 return BLUETOOTH_ERROR_IN_PROGRESS;
799 g_array_free(out_param1, TRUE);
801 return BLUETOOTH_ERROR_NONE;
803 if (out_param1 != NULL)
804 g_array_append_vals(out_param1, address,
810 int _bt_audio_disconnect(int request_id, int type,
811 bluetooth_device_address_t *device_address,
814 int result = BLUETOOTH_ERROR_NONE;
815 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
816 bt_audio_function_data_t *func_data;
817 GDBusProxy *adapter_proxy;
818 GDBusConnection *g_conn;
822 int value = BLUETOOTH_ERROR_NONE;
824 BT_CHECK_PARAMETER(device_address, return);
826 adapter_proxy = _bt_get_adapter_proxy();
827 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
829 g_conn = _bt_gdbus_get_system_gconn();
830 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
832 _bt_convert_addr_type_to_string(address, device_address->addr);
834 func_data = g_malloc0(sizeof(bt_audio_function_data_t));
835 func_data->address = g_strdup(address);
836 func_data->req_id = request_id;
837 func_data->pending = BT_PENDING_NONE;
838 func_data->type = type;
845 uuid = A2DP_SINK_UUID;
848 uuid = AVRCP_TARGET_UUID;
850 case BT_AUDIO_A2DP_SOURCE:
851 uuid = A2DP_SOURCE_UUID;
854 if (_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
855 uuid = A2DP_SINK_UUID;
856 func_data->pending = BT_PENDING_DISCONNECT;
857 } else if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
860 BT_ERR("No audio service connected");
861 result = BLUETOOTH_ERROR_NOT_CONNECTED;
865 case BT_AVRCP_TARGET:
866 uuid = AVRCP_REMOTE_UUID;
869 BT_ERR("Unknown role");
870 result = BLUETOOTH_ERROR_INTERNAL;
873 value = __bt_is_headset_disconnecting(type);
874 if (value != BLUETOOTH_ERROR_NONE) {
875 BT_INFO("Disconnect in progress");
876 result = BLUETOOTH_ERROR_IN_PROGRESS;
879 BT_INFO("Disconnecting service %s", uuid);
880 ret = _bt_disconnect_profile(address, uuid,
881 __bt_audio_request_cb, func_data);
883 if (ret != BLUETOOTH_ERROR_NONE) {
884 BT_ERR("_bt_disconnect_profile Error");
885 g_free(func_data->address);
891 * This logic is added for dual HF mode issue.
893 node = g_list_first(g_connected_list);
894 while (node != NULL) {
895 bt_connected_headset_data_t *connected_device = node->data;
897 if (g_strcmp0(connected_device->device_address, address) == 0) {
898 BT_DBG("Connection type update");
899 type = connected_device->type;
902 node = g_list_next(node);
904 _bt_add_headset_to_list(type, BT_STATE_DISCONNECTING, address);
907 g_array_free(out_param1, TRUE);
909 return BLUETOOTH_ERROR_NONE;
911 if (out_param1 != NULL)
912 g_array_append_vals(out_param1, address,
918 void _bt_remove_from_connected_list(const char *address)
920 bt_connected_headset_data_t *connected_device;
923 node = g_list_first(g_connected_list);
924 while (node != NULL) {
925 connected_device = node->data;
926 if (connected_device != NULL &&
927 g_strcmp0(connected_device->device_address, address) == 0) {
928 BT_ERR("Device is removed from the list");
929 g_connected_list = g_list_remove(g_connected_list, connected_device);
930 g_free(connected_device);
933 node = g_list_next(node);
937 int _bt_hf_connect(int request_id,
938 bluetooth_device_address_t *device_address,
941 int result = BLUETOOTH_ERROR_NONE;
942 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
943 bt_function_data_t *func_data;
944 GDBusProxy *adapter_proxy;
945 GDBusConnection *g_conn;
946 gboolean connected = FALSE;
950 BT_CHECK_PARAMETER(device_address, return);
952 _bt_convert_addr_type_to_string(address, device_address->addr);
954 adapter_proxy = _bt_get_adapter_proxy();
955 if (adapter_proxy == NULL) {
956 result = BLUETOOTH_ERROR_INTERNAL;
960 g_conn = _bt_gdbus_get_system_gconn();
961 if (g_conn == NULL) {
962 result = BLUETOOTH_ERROR_INTERNAL;
966 /* Check if HF is connected or not */
967 result = _bt_is_device_connected(device_address, BT_PROFILE_CONN_HFG, &connected);
968 if (connected == TRUE) {
969 BT_ERR("HF is already connected");
970 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
974 func_data = g_malloc0(sizeof(bt_function_data_t));
975 func_data->address = g_strdup(address);
976 func_data->req_id = request_id;
977 uuid = g_strdup(HFP_AG_UUID);
979 BT_DBG("Connecting to service %s", uuid);
981 ret = _bt_connect_profile(address, uuid,
982 __bt_hf_request_cb, func_data);
984 if (ret != BLUETOOTH_ERROR_NONE) {
985 BT_ERR("_bt_connect_profile Error");
986 g_free(func_data->address);
993 g_array_free(out_param1, TRUE);
995 return BLUETOOTH_ERROR_NONE;
997 if (out_param1 != NULL)
998 g_array_append_vals(out_param1, address,
1004 int _bt_hf_disconnect(int request_id,
1005 bluetooth_device_address_t *device_address,
1008 int result = BLUETOOTH_ERROR_NONE;
1009 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1010 bt_function_data_t *func_data;
1011 GDBusProxy *adapter_proxy;
1012 GDBusConnection *g_conn;
1017 BT_CHECK_PARAMETER(device_address, return);
1019 _bt_convert_addr_type_to_string(address, device_address->addr);
1021 adapter_proxy = _bt_get_adapter_proxy();
1022 if (adapter_proxy == NULL) {
1023 result = BLUETOOTH_ERROR_INTERNAL;
1027 g_conn = _bt_gdbus_get_system_gconn();
1028 if (g_conn == NULL) {
1029 result = BLUETOOTH_ERROR_INTERNAL;
1033 func_data = g_malloc0(sizeof(bt_function_data_t));
1034 func_data->address = g_strdup(address);
1035 func_data->req_id = request_id;
1036 uuid = g_strdup(HFP_AG_UUID);
1038 BT_DBG("Disconnecting service %s", uuid);
1039 ret = _bt_disconnect_profile(address, uuid,
1040 __bt_hf_request_cb, func_data);
1042 if (ret != BLUETOOTH_ERROR_NONE) {
1043 BT_ERR("_bt_disconnect_profile Error");
1044 g_free(func_data->address);
1051 g_array_free(out_param1, TRUE);
1053 return BLUETOOTH_ERROR_NONE;
1055 if (out_param1 != NULL)
1056 g_array_append_vals(out_param1, address,
1057 BT_ADDRESS_STR_LEN);
1062 int _bt_audio_set_content_protect(gboolean status)
1064 GDBusConnection *conn;
1065 GError *error = NULL;
1069 conn = _bt_gdbus_get_system_gconn();
1070 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1072 BT_DBG("Content Protection status = [%d]", status);
1074 g_dbus_connection_emit_signal(conn,
1075 NULL, BT_CONTENT_PROTECTION_PATH,
1076 BT_CONTENT_PROTECTION_INTERFACE,
1077 "ProtectionRequired",
1078 g_variant_new("(b)", status),
1082 /* dBUS gives error cause */
1083 ERR("Could not Emit Signal: errCode[%x], message[%s]",
1084 error->code, error->message);
1085 g_clear_error(&error);
1086 return BLUETOOTH_ERROR_INTERNAL;
1089 BT_DBG("Emit Signal done = [ProtectionRequired]");
1090 return BLUETOOTH_ERROR_NONE;
1093 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1094 static void __bt_auto_connect_request_cb(GDBusProxy *proxy, GAsyncResult *res,
1098 GVariant *reply = NULL;
1100 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1101 g_object_unref(proxy);
1102 g_variant_unref(reply);
1106 g_dbus_error_strip_remote_error(err);
1107 BT_INFO("Auto_connect_request Dbus Call Error: %s", err->message);
1109 if (strcmp("Host is down", err->message)) {
1110 BT_INFO("Fail reason is not 'Host Down'. Terminate auto connect");
1111 _bt_audio_stop_auto_connect();
1112 vconf_set_str(BT_LAST_CONNECTED_DEVICE, "");
1115 g_clear_error(&err);
1119 // It is the function that retry to create alarm.
1120 // Sometimes alarm mgr service is created later than bluetooth-frwk service in booting time.
1121 // So, in this case, we have to retry alarmmgr_add_alarm.
1122 static gboolean __bt_audio_alarm_retry_cb(gpointer data)
1125 alarm_id_t alarm_id;
1127 BT_DBG("__bt_audio_alarm_retry_cb called.");
1128 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1129 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1131 if (result != BLUETOOTH_ERROR_NONE)
1134 auto_connect_timer.alarm_id = alarm_id;
1139 static int __bt_auto_connect_alarm_cb(alarm_id_t alarm_id, void* user_param)
1141 int result = BLUETOOTH_ERROR_NONE;
1142 char *address = NULL;
1143 alarm_id_t new_alarm_id;
1145 BT_INFO("alram id = [%d] , retry_count [%d] ",
1146 alarm_id, auto_connect_timer.retry_count);
1148 if (alarm_id != auto_connect_timer.alarm_id)
1151 address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1153 if (address == NULL) {
1154 BT_ERR("Address vconf is null");
1155 auto_connect_timer.alarm_id = 0;
1159 BT_DBG("ADDRESS [%s]", address);
1161 result = _bt_connect_profile(address, A2DP_SOURCE_UUID,
1162 __bt_auto_connect_request_cb, NULL);
1164 auto_connect_timer.retry_count--;
1166 BT_DBG("result [%d]", result);
1168 if (auto_connect_timer.retry_count <= 0) {
1169 BT_INFO("Stopping Auto connect retry");
1170 auto_connect_timer.alarm_id = 0;
1172 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1173 __bt_auto_connect_alarm_cb, NULL, &new_alarm_id);
1174 if (result == BLUETOOTH_ERROR_NONE)
1175 auto_connect_timer.alarm_id = new_alarm_id;
1182 static void __bt_auto_connect_alarm_remove()
1185 if (auto_connect_timer.alarm_id > 0) {
1186 _bt_service_remove_alarm(auto_connect_timer.alarm_id);
1187 auto_connect_timer.alarm_id = 0;
1189 auto_connect_timer.retry_count = 0;
1192 int _bt_audio_start_auto_connect(gboolean linkloss_flag)
1194 int result = BLUETOOTH_ERROR_NONE;
1195 char *address = NULL;
1196 alarm_id_t alarm_id = 0;
1200 address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1202 if (address == NULL) {
1203 BT_ERR("No target device");
1207 __bt_auto_connect_alarm_remove();
1209 auto_connect_timer.retry_count = BT_AUTO_CONNECT_TIMEOUT_RETRY_TIME /
1210 BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS;
1212 if (linkloss_flag) {
1213 BT_INFO("Start auto connection after linkloss");
1214 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1215 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1216 if (result != BLUETOOTH_ERROR_NONE)
1217 g_timeout_add(500, (GSourceFunc)__bt_audio_alarm_retry_cb, NULL);
1219 auto_connect_timer.alarm_id = alarm_id;
1222 BT_INFO("Start auto connection after BT activated");
1223 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_BT_ACTIVATED,
1224 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1225 if (result == BLUETOOTH_ERROR_NONE)
1226 auto_connect_timer.alarm_id = alarm_id;
1230 return BLUETOOTH_ERROR_NONE;
1233 int _bt_audio_stop_auto_connect(void)
1236 __bt_auto_connect_alarm_remove();
1238 return BLUETOOTH_ERROR_NONE;
1241 void _bt_audio_set_auto_connect_device_addr(const char *address)
1243 if (address == NULL) {
1244 BT_ERR("address is null");
1248 BT_INFO("Last connected device is [%s]", address);
1249 vconf_set_str(BT_LAST_CONNECTED_DEVICE, address);
1251 #endif /*TIZEN_BT_A2DP_SINK_AUTO_CONNECT */