4 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Anupam Roy <anupam.r@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include <sys/socket.h>
24 #include <sys/errno.h>
29 #include <oal-event.h>
30 #include <oal-hardware.h>
31 #include <oal-audio-src.h>
32 #include <oal-a2dp-sink.h>
34 #include "bt-service-common.h"
35 #include "bt-request-handler.h"
36 #include "bt-service-audio-common.h"
37 #include "bt-service-core-device.h"
38 #include "bt-service-a2dp-src.h"
39 #include "bt-service-a2dp-sink.h"
40 #include "bt-service-avrcp-tg.h"
41 #include "bt-service-avrcp-ctrl.h"
42 #include "bt-service-hf.h"
44 #ifdef TIZEN_SUPPORT_DUAL_HF
45 #define VCONF_KEY_BT_HOST_BT_MAC_ADDR "db/wms/host_bt_mac"
48 /* Static variables and Macros */
49 static GList *g_connected_list;
50 static bt_headset_wait_t *g_wait_data;
54 char device_address[BT_ADDRESS_STRING_SIZE + 1];
55 } bt_connected_headset_data_t;
57 /* List for saving device service search info */
58 static GSList *pending_audio_conn_list = NULL;
60 /* Headset connection data structures */
61 gboolean connection_local = FALSE;
63 static int curr_audio_role = BLUETOOTH_A2DP_SOURCE;
64 static int pending_audio_role = -1;
66 static void __bt_remove_device_from_wait_list(char *prev_waiting_device);
68 static void __bt_audio_event_handler(int oal_event, gpointer event_data);
70 static int __bt_process_audio_connect_all(bt_pending_audio_conn_t *info);
72 static int __bt_is_headset_connecting(int type);
74 static int __bt_is_headset_disconnecting(int type);
76 static void __bt_audio_cleanup_resources(void);
78 static gboolean __handle_pending_audio_select_role(gpointer data);
80 static void __bt_reply_pending_audio_connect_req(char *address, int result);
83 void _bt_headset_set_local_connection(gboolean value)
85 BT_INFO("setting connection_local to %d", value);
86 connection_local = value;
89 gboolean _bt_headset_get_local_connection()
91 return connection_local;
95 void _bt_set_audio_wait_data_flag(gboolean flag)
97 BT_DBG("_bt_set_audio_wait_data_flag \n");
98 g_wait_data->ag_flag = flag;
101 bt_headset_wait_t *_bt_get_audio_wait_data(void)
103 BT_DBG("_bt_get_audio_wait_data \n");
107 static void __bt_free_wait_data(void)
109 if (g_wait_data != NULL) {
110 g_free(g_wait_data->address);
116 void _bt_rel_wait_data(void)
118 BT_DBG("_bt_rel_wait_data \n");
119 __bt_free_wait_data();
122 void _bt_add_headset_to_list(int type, int status, const char *address)
124 bt_connected_headset_data_t *connected_device;
125 bt_connected_headset_data_t *device;
128 BT_DBG("_bt_add_headset_to_list \n");
130 node = g_list_first(g_connected_list);
131 while (node != NULL) {
132 device = (bt_connected_headset_data_t *)node->data;
134 if (g_strcmp0(device->device_address, address) == 0) {
135 BT_DBG("Address match, update connection type \n");
136 if (status == BT_STATE_CONNECTED)
137 device->type |= type;
138 device->device_state = status;
141 node = g_list_next(node);
144 connected_device = g_malloc0(sizeof(bt_connected_headset_data_t));
145 /* Fix : NULL_RETURNS */
146 if (connected_device == NULL) {
147 BT_ERR("No memory allocated");
151 connected_device->device_state = status;
152 //if ((status == BT_STATE_CONNECTED) || (status == BT_STATE_CONNECTING))
153 if (status == BT_STATE_CONNECTED)
154 connected_device->type |= type;
155 g_strlcpy(connected_device->device_address, address,
156 sizeof(connected_device->device_address));
157 g_connected_list = g_list_append(g_connected_list, connected_device);
158 BT_INFO("Added device[%s] in connected list, device state [%d] Device Type [%d]",
159 address, connected_device->device_state, connected_device->type);
162 gboolean _bt_is_headset_type_connected(int type, char *address)
166 node = g_list_first(g_connected_list);
167 while (node != NULL) {
168 bt_connected_headset_data_t *connected_device = node->data;
170 if (connected_device->type & type) {
172 g_strlcpy(address, connected_device->device_address,
173 BT_ADDRESS_STRING_SIZE);
177 node = g_list_next(node);
182 static void __bt_set_headset_disconnection_type(const char *address)
184 bt_connected_headset_data_t *connected_device;
187 node = g_list_first(g_connected_list);
188 while (node != NULL) {
189 connected_device = node->data;
190 if (g_strcmp0(connected_device->device_address, address) == 0) {
191 g_wait_data->disconnection_type = connected_device->type;
194 node = g_list_next(node);
198 #ifdef TIZEN_SUPPORT_DUAL_HF
199 gboolean __bt_is_companion_device(const char *addr)
201 if (TIZEN_PROFILE_WEARABLE) {
202 char *host_device_address = NULL;
203 host_device_address = vconf_get_str(VCONF_KEY_BT_HOST_BT_MAC_ADDR);
205 if (!host_device_address) {
206 BT_INFO("Failed to get a companion device address");
210 if (g_strcmp0(host_device_address, addr) == 0) {
211 BT_INFO("addr[%s] is companion device", addr);
217 /* TODO : Need to add companion device check condition for Phone models */
223 static int __bt_is_headset_disconnecting(int type)
225 bt_connected_headset_data_t *connected_device = NULL;
227 /* Check if any other headset is connected */
230 node = g_list_first(g_connected_list);
231 while (node != NULL) {
232 connected_device = node->data;
233 if (connected_device->device_state == BT_STATE_DISCONNECTING)
234 return BLUETOOTH_ERROR_CONNECTION_BUSY;
235 node = g_list_next(node);
238 return BLUETOOTH_ERROR_NONE;
241 static int __bt_is_headset_connecting(int type)
243 bt_connected_headset_data_t *connected_device = NULL;
245 /* Check if any other headset is connected */
248 node = g_list_first(g_connected_list);
249 while (node != NULL) {
250 connected_device = node->data;
251 if (connected_device->device_state == BT_STATE_CONNECTING) {
252 BT_ERR("@@Device [%s] is already under connecting state", connected_device->device_address);
253 return BLUETOOTH_ERROR_CONNECTION_BUSY;
255 node = g_list_next(node);
258 return BLUETOOTH_ERROR_NONE;
261 static int __bt_is_headset_connected(int current_conn_type, const char *address)
263 gboolean connected = FALSE;
265 char connected_address[BT_ADDRESS_STRING_SIZE + 1];
266 bluetooth_device_address_t device_address;
267 bt_connected_headset_data_t *connected_device = NULL;
268 #ifdef TIZEN_SUPPORT_DUAL_HF
269 gboolean is_companion_device = FALSE;
272 /* Check if any other headset is connected */
275 BT_INFO("Checking if any Headset connected or not: current device [%s] current dev type [%d]", address, current_conn_type);
276 node = g_list_first(g_connected_list);
277 while (node != NULL) {
278 connected_device = node->data;
279 BT_INFO(" A Device is already connected found in list [%s] conn type [%d]",
280 connected_device->device_address, connected_device->type);
281 if ((connected_device->type & current_conn_type)) {
282 g_strlcpy(connected_address, connected_device->device_address,
283 BT_ADDRESS_STRING_SIZE + 1);
284 #ifdef TIZEN_SUPPORT_DUAL_HF
285 is_companion_device = __bt_is_companion_device(connected_address);
286 BT_INFO(" is_companion_device[%d]", is_companion_device);
288 if (!is_companion_device) {
297 node = g_list_next(node);
301 BT_INFO("There is no connected device with connection type [%d]", current_conn_type);
302 return BLUETOOTH_ERROR_NOT_CONNECTED;
305 BT_DBG("connected headset %s", connected_address);
307 if (g_strcmp0(connected_address, address) == 0)
308 return BLUETOOTH_ERROR_ALREADY_CONNECT;
309 #ifdef TIZEN_SUPPORT_DUAL_HF
310 else if (TRUE == __bt_is_companion_device(address))
311 return BLUETOOTH_ERROR_NOT_CONNECTED;
314 /* Convert BD address from string type */
315 _bt_convert_addr_string_to_type(device_address.addr, connected_address);
316 int value = BLUETOOTH_ERROR_NONE;
317 BT_DBG("Already connected headset addr [%s] connected headset type [0x%x] current dev conn type [0x%x]",
318 connected_address, connected_device->type, current_conn_type);
319 disconn_type = connected_device->type & current_conn_type;
320 BT_DBG("Attempt disconnection of Type [0x%x] of already connected device" , disconn_type);
321 value = _bt_audio_disconnect(disconn_type, &device_address);
323 /* If already one device is waiting, remove current waiting device and add new */
324 if (value == BLUETOOTH_ERROR_NONE) {
325 if (g_wait_data != NULL) {
326 if (g_strcmp0(g_wait_data->address, address) != 0) {
327 BT_INFO("Already one device was waiting for connection [%s], remove it", g_wait_data->address);
328 __bt_remove_device_from_wait_list(g_wait_data->address);
329 __bt_free_wait_data();
333 if (g_wait_data == NULL) {
334 BT_INFO("Add current device [%s] into waiting list", address);
335 g_wait_data = g_malloc0(sizeof(bt_headset_wait_t));
336 g_wait_data->address = g_strdup(address);
337 g_wait_data->type = current_conn_type;
338 g_wait_data->ag_flag = FALSE;
340 /* Set disconnection type */
341 __bt_set_headset_disconnection_type(connected_address);
348 static void __bt_remove_device_from_wait_list(char *prev_waiting_device)
350 /* Before deleting the request update the UI */
351 bluetooth_device_address_t device_address;
352 int result = BLUETOOTH_ERROR_INTERNAL;
354 invocation_info_t *req_info;
355 BT_INFO("We are about to DBUS return previous waiting device [%s]", prev_waiting_device);
357 _bt_convert_addr_string_to_type(device_address.addr, prev_waiting_device);
359 req_info = _bt_get_request_info_data(BT_AV_CONNECT, prev_waiting_device);
360 if (req_info == NULL) {
361 BT_ERR("req_info == NULL");
364 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
365 g_array_append_vals(out_param, &device_address,
366 sizeof(bluetooth_device_address_t));
367 _bt_service_method_return(req_info->context,
369 g_array_free(out_param, TRUE);
370 g_free(req_info->user_data);
371 _bt_free_info_from_invocation_list(req_info);
375 static int __bt_process_audio_profile_connect(bt_audio_type_t type, bluetooth_device_address_t *address)
377 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
378 int result = BLUETOOTH_ERROR_NONE;
381 _bt_convert_addr_type_to_string(addr,
382 (unsigned char *)address->addr);
383 /* Attempt profile level connection based on type */
386 result = _bt_connect_remote_hfp(address);
389 result = _bt_a2dp_connect_remote_sink(address);
391 case BT_AVRCP_TARGET:
392 result = _bt_avrcp_connect_remote_ctrl(address);
395 result = _bt_avrcp_connect_remote_target(address);
397 case BT_AUDIO_A2DP_SOURCE:
398 return _bt_a2dp_connect_remote_source(address);
401 BT_ERR("Unknown role");
402 return BLUETOOTH_ERROR_INTERNAL;
405 if (result == BLUETOOTH_ERROR_NONE) {
406 //_bt_headset_set_local_connection(TRUE);
407 /* Add data to the connected list */
408 _bt_add_headset_to_list(type, BT_STATE_CONNECTING, addr);
410 BT_ERR("Profile [%d] connect failed!!", type);
416 static int __bt_process_audio_profile_disconnect(bt_audio_type_t type, bluetooth_device_address_t *address)
418 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
419 int result = BLUETOOTH_ERROR_NONE;
423 _bt_convert_addr_type_to_string(addr,
424 (unsigned char *)address->addr);
425 /* Attempt profile level connection based on type */
428 result = _bt_disconnect_remote_hfp(address);
431 result = _bt_a2dp_disconnect_remote_sink(address);
433 case BT_AVRCP_TARGET:
434 result = _bt_avrcp_disconnect_remote_ctrl(address);
437 result = _bt_avrcp_disconnect_remote_target(address);
439 case BT_AUDIO_A2DP_SOURCE:
440 return _bt_a2dp_disconnect_remote_source(address);
443 BT_ERR("Unknown role");
444 return BLUETOOTH_ERROR_INTERNAL;
447 if (result == BLUETOOTH_ERROR_NONE) {
449 * This logic is added for dual HF mode issue.
451 node = g_list_first(g_connected_list);
452 while (node != NULL) {
453 bt_connected_headset_data_t *connected_device = node->data;
455 if (g_strcmp0(connected_device->device_address, addr) == 0) {
456 BT_DBG("Connection type update");
457 type = connected_device->type;
460 node = g_list_next(node);
463 /* Update device status in connected list */
464 _bt_add_headset_to_list(type, BT_STATE_DISCONNECTING, addr);
466 BT_ERR("Profile [%d] connect failed!!", type);
472 int _bt_audio_initialize(bt_service_module_t module)
474 oal_status_t status = OAL_STATUS_SUCCESS;
475 int ret = BLUETOOTH_ERROR_NONE;
477 BT_INFO("_bt_audio_initialize: Module [%d]", module);
480 case BT_A2DP_SOURCE_MODULE: {
481 status = audio_enable(NULL, NULL);
482 if (OAL_STATUS_SUCCESS != status) {
483 BT_ERR("Failed to initialize Bluetooth A2DP Source Profile, status: %d", status);
484 return BLUETOOTH_ERROR_INTERNAL;
486 /* Register Audio module event handler */
487 _bt_service_register_event_handler_callback(module, _bt_a2dp_source_event_handler);
490 case BT_A2DP_SINK_MODULE: {
491 status = a2dp_sink_enable(NULL, NULL);
492 if (status != OAL_STATUS_SUCCESS) {
493 BT_ERR("Failed to initialize Bluetooth A2DP Sink Profile, status: %d", status);
494 return BLUETOOTH_ERROR_INTERNAL;
496 /* Register Audio sink module event handler */
497 _bt_service_register_event_handler_callback(module, _bt_a2dp_sink_event_handler);
500 case BT_HFP_MODULE: {
501 return BLUETOOTH_ERROR_NOT_SUPPORT;
504 status = hfp_enable(1);
505 if (OAL_STATUS_SUCCESS != status) {
506 BT_ERR("Failed to initialize Bluetooth HFP Audio Gateway Profile, status: %d", status);
507 return BLUETOOTH_ERROR_INTERNAL;
509 /* Register Audio module event handler */
510 _bt_service_register_event_handler_callback(module, _bt_hf_event_handler);
513 case BT_AUDIO_ALL_MODULE: {
514 /* Register Audio module event handler */
515 _bt_service_register_event_handler_callback(module, __bt_audio_event_handler);
518 case BT_AVRCP_CTRL_MODULE: {
519 status = avrcp_ct_enable();
520 if (status != OAL_STATUS_SUCCESS) {
521 BT_ERR("Failed to initialize Bluetooth AVRCP Controller Profile, status: %d", status);
522 return BLUETOOTH_ERROR_INTERNAL;
524 /* Register Avrcp Controller event handler */
525 _bt_service_register_event_handler_callback(module, _bt_avrcp_ctrl_event_handler);
528 case BT_AVRCP_MODULE: {
529 /* Initialize AVRCP Target */
530 ret = _bt_service_avrcp_enable();
534 BT_ERR("Not Supported: Module [%d]", module);
535 return BLUETOOTH_ERROR_NOT_SUPPORT;
538 BT_INFO("Bluetooth audio interface initialised");
542 static void __bt_audio_event_handler(int oal_event, gpointer event_data)
547 case OAL_EVENT_ADAPTER_DISABLED: {
548 BT_INFO("Adapter Disabled..cleanup resources");
549 __bt_audio_cleanup_resources();
552 case OAL_EVENT_ADAPTER_ENABLED: {
553 /*TODO Currently not handled */
554 BT_INFO("Adapter Enabled..");
558 BT_ERR("Unknown event..[%d]", oal_event);
563 static void __bt_audio_cleanup_resources(void)
567 bt_pending_audio_conn_t *info = NULL;
570 /* Remove connected device list */
571 node = g_list_first(g_connected_list);
572 while (node != NULL) {
573 bt_connected_headset_data_t *connected_device = node->data;
574 BT_INFO("Data found [%s] in connected device list...remove it..", connected_device->device_address);
575 g_connected_list = g_list_remove(g_connected_list, connected_device);
576 node = g_list_next(node);
579 /* Remove pending connection list */
580 for (l = pending_audio_conn_list; l != NULL; l = g_slist_next(l)) {
581 info = (bt_pending_audio_conn_t*)l->data;
584 BT_INFO("Info found for addr [%s], remove it...", info->address);
585 pending_audio_conn_list = g_slist_remove(pending_audio_conn_list, info);
586 g_free(info->address);
595 static int __bt_process_audio_connect_all(bt_pending_audio_conn_t *info)
597 bluetooth_device_address_t device_address;
598 int result = BLUETOOTH_ERROR_NONE;
599 int type = BT_AUDIO_ALL;
602 _bt_convert_addr_string_to_type(device_address.addr, info->address);
604 /* Attempt profile level connection */
605 if (info->is_hfp_supported) {
607 result = _bt_connect_remote_hfp(&device_address);
609 if (info->is_a2dp_sink_supported)
610 BT_INFO("A2DP is supported by [%s]", info->address);
612 BT_INFO("A2DP is not supported");
613 } else if (info->is_a2dp_sink_supported) {
614 type = BT_AUDIO_A2DP;
615 result = _bt_a2dp_connect_remote_sink(&device_address);
616 } else if (info->is_a2dp_src_supported) {
617 type = BT_AUDIO_A2DP_SOURCE;
618 result = _bt_a2dp_connect_remote_source(&device_address);
620 if (result == BLUETOOTH_ERROR_NONE) {
621 //_bt_headset_set_local_connection(TRUE);
622 /* Add data to the connected list */
623 _bt_add_headset_to_list(type, BT_STATE_CONNECTING, info->address);
625 BT_ERR("Profile connect failed!!");
631 bt_pending_audio_conn_t* _bt_get_pending_audio_conn_info(char *address)
634 bt_pending_audio_conn_t *info = NULL;
637 for (l = pending_audio_conn_list; l != NULL; l = g_slist_next(l)) {
638 info = (bt_pending_audio_conn_t*)l->data;
640 if (g_strcmp0(info->address, address) == 0 && info->search_status == SERVICE_SEARCH_DONE) {
641 BT_INFO("Pending data found for addr [%s]", info->address);
645 BT_INFO("Pending data not found!!");
649 static void __bt_reply_pending_audio_connect_req(char *address, int result)
652 invocation_info_t *req_info;
655 ret_if(NULL == address);
657 /* Reply to async request for Audio connect, if any */
658 req_info = _bt_get_request_info_data(BT_AUDIO_CONNECT, address);
659 ret_if(NULL == req_info);
661 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
662 g_array_append_vals(out_param, address, BT_ADDRESS_STRING_SIZE);
663 _bt_service_method_return(req_info->context, out_param, result);
665 g_array_free(out_param, TRUE);
666 g_free(req_info->user_data);
667 _bt_free_info_from_invocation_list(req_info);
672 void _bt_cleanup_audio_pending_conn_info_and_reply_pending_req(bt_pending_audio_conn_t *info, int result)
674 ret_if(NULL == info);
676 /* Reply to async request for Audio connect, if any */
677 __bt_reply_pending_audio_connect_req(info->address, result);
679 /* Remove pending audio conn info */
680 pending_audio_conn_list = g_slist_remove(pending_audio_conn_list, info);
681 g_free(info->address);
685 gboolean _bt_audio_check_pending_connection(char *address)
687 bt_pending_audio_conn_t *data = NULL;
688 bluetooth_device_address_t device_address;
691 data = _bt_get_pending_audio_conn_info(address);
694 BT_INFO("A2DP Connection is pending..initiate it...");
695 if (data->is_a2dp_sink_supported && data->conn_req_type == BT_AUDIO_ALL) {
696 _bt_convert_addr_string_to_type(device_address.addr, address);
697 if (_bt_audio_connect(BT_AUDIO_A2DP, &device_address) != BLUETOOTH_ERROR_NONE) {
698 BT_INFO("A2DP connect triggered for [%s] but it failed!!", data->address);
699 _bt_cleanup_audio_pending_conn_info_and_reply_pending_req(data, BLUETOOTH_ERROR_INTERNAL);
701 BT_INFO("A2DP connect triggered successfully for [%s]", data->address);
703 } else if (data->is_a2dp_src_supported && data->conn_req_type == BT_AUDIO_HFP_SOURCE) {
704 _bt_convert_addr_string_to_type(device_address.addr, address);
705 if (_bt_audio_connect(BT_AUDIO_A2DP_SOURCE, &device_address) != BLUETOOTH_ERROR_NONE) {
706 BT_INFO("A2DP sink connect triggered for [%s] but it failed!!", data->address);
707 _bt_cleanup_audio_pending_conn_info_and_reply_pending_req(data, BLUETOOTH_ERROR_INTERNAL);
709 BT_INFO("A2DP sink connect triggered successfully for [%s]", data->address);
714 BT_INFO("A2DP Connection is not pending..");
720 void _bt_audio_check_pending_disconnection(char *address, int type)
722 int ret = BLUETOOTH_ERROR_NONE;
723 bluetooth_device_address_t device_address;
726 if (_bt_is_service_connected(address, type)) {
727 BT_INFO("Service [%d] is connected with device [%s], disconnect it...", type, address);
728 _bt_convert_addr_string_to_type(device_address.addr, address);
729 ret = __bt_process_audio_profile_disconnect(type, &device_address);
731 if (ret != BLUETOOTH_ERROR_NONE)
732 BT_ERR("Disconnecting service [%d] with device [%s] failed!!", type, address);
734 BT_INFO("Service [%d] is Not connected with device [%s],..", type, address);
739 int __bt_handle_audio_all_connect(bt_pending_audio_conn_t *info)
741 bt_remote_dev_info_t *rem_info;
747 retv_if(NULL == info, BLUETOOTH_ERROR_INTERNAL);
748 retv_if(NULL == info->address, BLUETOOTH_ERROR_INTERNAL);
750 rem_info = _bt_service_get_remote_dev_info(info->address);
752 BT_ERR("_bt_service_get_remote_dev_info returned NULL");
753 ret = BLUETOOTH_ERROR_NOT_PAIRED;
757 for (indx = 0; indx < rem_info->uuid_count; indx++) {
758 BT_INFO("UUID [%s]", rem_info->uuids[indx]);
759 if (0 == g_strcmp0(A2DP_SINK_UUID, rem_info->uuids[indx])) {
760 BT_INFO("Device supports A2DP Sink Profile");
761 info->is_a2dp_sink_supported = TRUE;
762 } else if (0 == g_strcmp0(A2DP_SOURCE_UUID, rem_info->uuids[indx])) {
763 BT_INFO("Device supports A2DP Source Profile");
764 info->is_a2dp_src_supported = TRUE;
765 } else if ((g_strcmp0(HFP_HS_UUID, rem_info->uuids[indx]) == 0)) {
766 BT_INFO("Device supports HFP Profile");
767 info->is_hfp_supported = TRUE;
771 if (info->is_a2dp_sink_supported == FALSE &&
772 info->is_hfp_supported == FALSE &&
773 info->is_a2dp_src_supported == FALSE) {
774 BT_ERR("No audio profile is supported");
775 ret = BLUETOOTH_ERROR_NOT_SUPPORT;
779 BT_INFO("AUDIO_CONNECT_ALL request for [%s]", info->address);
780 info->search_status = SERVICE_SEARCH_DONE;
782 /* Give preference to HFP over A2DP for outgoing connection sequence for AUDIO_ALL_CONNECT */
783 if (info->is_hfp_supported)
784 info->type = BT_AUDIO_HSP;
785 else if (info->is_a2dp_sink_supported)
786 info->type = BT_AUDIO_A2DP;
787 else if (info->is_a2dp_src_supported)
788 info->type = BT_AUDIO_A2DP_SOURCE;
790 ret = __bt_is_headset_connected(info->type, info->address);
791 if (ret == BLUETOOTH_ERROR_ALREADY_CONNECT) {
792 BT_ERR("Can't start Audio All connect for [%s], reason: [%s]",
793 info->address, "Already connected...");
797 if (ret == BLUETOOTH_ERROR_IN_PROGRESS) {
798 BT_ERR("Can't start Audio All connect for [%s], reason: [%s]",
799 info->address, "Already in progress...");
803 if (ret == BLUETOOTH_ERROR_NOT_CONNECTED) {
804 BT_ERR("Great, can start Audio All connect for [%s]", info->address);
805 ret = __bt_is_headset_connecting(info->type);
806 if (ret != BLUETOOTH_ERROR_NONE) {
807 BT_ERR("Can't start Audio All connect for [%s], reason: [%s]",
808 info->address, "some other device conn in progress");
809 ret = BLUETOOTH_ERROR_IN_PROGRESS;
813 ret = __bt_process_audio_connect_all(info);
814 if (ret != BLUETOOTH_ERROR_NONE) {
815 BT_ERR("__bt_process_audio_connect_all failed");
819 /* If multiple profiles are supported, queue pending connection info */
820 if ((info->is_hfp_supported && info->is_a2dp_sink_supported) ||
821 (info->is_hfp_supported && info->is_a2dp_src_supported)) {
822 BT_INFO("[%s] Supports HFP and (A2DP_Src or, A2DP_Snk)", info->address);
823 pending_audio_conn_list = g_slist_append(pending_audio_conn_list, (gpointer)info);
825 BT_INFO("[%s] Supports one profile only", info->address);
827 * It means, we dont need pending connect info as only A2DP (Src/Snk) or
828 * HFP will be connected for present device, so lets free pending info.
830 g_free(info->address);
833 } else if (ret == BLUETOOTH_ERROR_NONE) {
834 BT_INFO("Waiting for disconnection...");
836 * It means, we dont need pending connect info as only A2DP (Src/Snk) or
837 * HFP will be connected for present device, so lets free pending info.
839 g_free(info->address);
844 return BLUETOOTH_ERROR_NONE;
847 BT_ERR("Audio connect all failed");
848 g_free(info->address);
853 static gboolean __audio_get_bonded_info(gpointer data)
855 bt_pending_audio_conn_t *info = data;
856 char address[BT_ADDRESS_STRING_SIZE];
860 retv_if(NULL == info, FALSE);
861 retv_if(NULL == info->address, FALSE);
863 if ((info->bonded_info_retries > 0) && !_bt_is_bonded_devices_retrived()) {
864 info->bonded_info_retries--;
868 g_strlcpy(address, info->address, BT_ADDRESS_STRING_SIZE);
869 if (info->bonded_info_retries <= 0) {
870 BT_ERR_C("Even after all retries, bonded devices stil not retrieved");
871 g_free(info->address);
876 if (BLUETOOTH_ERROR_NONE != __bt_handle_audio_all_connect(info)) {
877 BT_ERR("__bt_handle_audio_all_connect failed");
884 __bt_reply_pending_audio_connect_req(address, BLUETOOTH_ERROR_INTERNAL);
888 int _bt_audio_connect(int type, bluetooth_device_address_t *device_address)
890 int ret = BLUETOOTH_ERROR_NONE;
891 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
892 bt_pending_audio_conn_t *info = NULL;
896 BT_CHECK_PARAMETER(device_address, return);
898 _bt_convert_addr_type_to_string(address, device_address->addr);
899 BT_INFO("Adderss: [%s], request type: [%d]", address, type);
902 * If type is BT_AUDIO_ALL or BT_AUDIO_HFP_SOURCE, enqueue search info in
903 * order to find out supported services by the remote device.
905 if (type == BT_AUDIO_ALL || type == BT_AUDIO_HFP_SOURCE) {
906 if (NULL != _bt_get_pending_audio_conn_info(address)) {
907 BT_ERR("Connection for same device already in progress");
908 return BLUETOOTH_ERROR_IN_PROGRESS;
911 info = g_malloc0(sizeof(bt_pending_audio_conn_t));
912 info->search_status = SERVICE_SEARCH_STARTED;
913 info->address = g_strdup(address);
915 info->conn_req_type = type;
917 /* Request bonded device info */
918 if (!_bt_is_bonded_devices_retrived()) {
919 info->bonded_info_retries = 5;
920 /* Wait till bonded devices retrival is completed */
921 g_timeout_add_seconds(1, __audio_get_bonded_info, info);
922 return BLUETOOTH_ERROR_NONE;
925 return __bt_handle_audio_all_connect(info);
928 /* If type is A2DP Sink or HSP/HFP, check further */
929 ret = __bt_is_headset_connected(type, address);
930 if (ret == BLUETOOTH_ERROR_ALREADY_CONNECT) {
931 BT_ERR("Failed with BLUETOOTH_ERROR_ALREADY_CONNECT");
932 return BLUETOOTH_ERROR_ALREADY_CONNECT;
935 if (ret == BLUETOOTH_ERROR_IN_PROGRESS) {
936 BT_ERR("Failed with BLUETOOTH_ERROR_IN_PROGRESS");
937 return BLUETOOTH_ERROR_IN_PROGRESS;
940 if (ret == BLUETOOTH_ERROR_NONE) {
941 BT_INFO("Waiting for disconnection...");
942 } else if (ret == BLUETOOTH_ERROR_NOT_CONNECTED) {
943 ret = __bt_process_audio_profile_connect(type, device_address);
944 if (ret != BLUETOOTH_ERROR_NONE) {
945 BT_ERR("Failed with %d", ret);
950 return BLUETOOTH_ERROR_NONE;
953 int _bt_audio_disconnect(int type, bluetooth_device_address_t *device_address)
955 int ret = BLUETOOTH_ERROR_NONE;
956 int value = BLUETOOTH_ERROR_NONE;
957 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
959 _bt_convert_addr_type_to_string(address, device_address->addr);
960 BT_INFO("Audio Diconnect Request: type[%d] address [%s]", type, address);
962 if (type == BT_AUDIO_ALL) {
963 if (_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
964 type = BT_AUDIO_A2DP;
965 } else if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
968 BT_ERR("No audio service connected");
969 return BLUETOOTH_ERROR_NOT_CONNECTED;
971 } else if (type == BT_AUDIO_HFP_SOURCE) {
972 if (_bt_is_service_connected(address, BT_AUDIO_A2DP_SOURCE)) {
973 type = BT_AUDIO_A2DP_SOURCE;
974 } else if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
977 BT_ERR("No audio service connected");
978 return BLUETOOTH_ERROR_NOT_CONNECTED;
982 value = __bt_is_headset_disconnecting(type);
983 if (value != BLUETOOTH_ERROR_NONE) {
984 BT_INFO("Disconnect in progress");
985 return BLUETOOTH_ERROR_IN_PROGRESS;
988 ret = __bt_process_audio_profile_disconnect(type, device_address);
992 gboolean _bt_is_service_connected(char* address, int type)
995 node = g_list_first(g_connected_list);
996 while (node != NULL) {
997 bt_connected_headset_data_t *conn_device = node->data;
999 if ((g_strcmp0(conn_device->device_address, address) == 0) &&
1000 (conn_device->type & type)) {
1001 BT_INFO("Service connected");
1005 node = g_list_next(node);
1007 BT_INFO("Service not connected");
1011 void _bt_remove_headset_from_list(int type, const char *address)
1015 BT_DBG("_bt_remove_headset_from_list \n");
1017 node = g_list_first(g_connected_list);
1018 while (node != NULL) {
1019 bt_connected_headset_data_t *connected_device = node->data;
1021 if (g_strcmp0(connected_device->device_address, address) != 0) {
1022 node = g_list_next(node);
1026 BT_DBG("Address match \n");
1028 BT_DBG("Connection type = %x\n", connected_device->type);
1032 if (connected_device->type & BT_AUDIO_A2DP)
1033 connected_device->type &= ~(BT_AUDIO_A2DP);
1036 if (connected_device->type & BT_AUDIO_HSP)
1037 connected_device->type &= ~(BT_AUDIO_HSP);
1040 if (connected_device->type & BT_AUDIO_ALL)
1041 connected_device->type &= ~(BT_AUDIO_ALL);
1043 case BT_AVRCP_TARGET:
1044 if (connected_device->type & BT_AVRCP_TARGET)
1045 connected_device->type &= ~(BT_AVRCP_TARGET);
1048 if (connected_device->type & BT_AVRCP)
1049 connected_device->type &= ~(BT_AVRCP);
1051 case BT_AUDIO_A2DP_SOURCE:
1052 if (connected_device->type & BT_AUDIO_A2DP_SOURCE)
1053 connected_device->type &= ~(BT_AUDIO_A2DP_SOURCE);
1059 BT_DBG("Connection type = %x\n", connected_device->type);
1061 if (connected_device->type == 0x00) {
1062 BT_INFO("Device will be completely removed from connected list as the only profile connected or connecting got disconnected");
1063 g_connected_list = g_list_remove(g_connected_list, connected_device);
1064 g_free(connected_device);
1066 connected_device->device_state = BT_STATE_CONNECTED;
1069 node = g_list_next(node);
1073 * If Audio role selection request is pending (pending_audio_role > 0) then process pending
1074 * audio role select request on BT_AUDIO_A2DP/BT_AUDIO_A2DP_SOURCE device dicaonnection.
1076 if (pending_audio_role >= 0 &&
1077 (type == BT_AUDIO_A2DP || type == BT_AUDIO_A2DP_SOURCE))
1078 g_idle_add(__handle_pending_audio_select_role, NULL);
1081 static void __handle_pending_requests(int result, int service_function, void *user_data, unsigned int size)
1085 invocation_info_t *req_info;
1089 /* Get method invocation context */
1090 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1093 if (req_info == NULL || req_info->service_function != service_function)
1096 /* Create out param */
1097 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1099 switch (service_function) {
1100 case BT_AUDIO_SELECT_ROLE:
1101 _bt_service_method_return(req_info->context, out_param, result);
1102 g_array_free(out_param, TRUE);
1103 _bt_free_info_from_invocation_list(req_info);
1106 BT_ERR("Unknown service function[%d]", service_function);
1114 static gboolean __handle_pending_audio_select_role(gpointer data)
1117 oal_status_t status;
1120 if (BLUETOOTH_A2DP_SOURCE == pending_audio_role) {
1121 /* Disable A2DP Sink role */
1122 status = a2dp_sink_disable();
1123 if (status != OAL_STATUS_SUCCESS) {
1124 BT_ERR("Failed to Disable Bluetooth A2DP Sink Profile, status: %d", status);
1125 ret = BLUETOOTH_ERROR_INTERNAL;
1129 /* Disable AVRCP CT role */
1130 status = avrcp_ct_disable();
1131 if (status != OAL_STATUS_SUCCESS) {
1132 BT_ERR("Failed to Disable Bluetooth AVRCP Controller Profile, status: %d", status);
1133 ret = BLUETOOTH_ERROR_INTERNAL;
1137 /* Enable A2DP Source role */
1138 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
1139 if (ret != BLUETOOTH_ERROR_NONE) {
1140 BT_ERR("Failed to enable Bluetooth A2DP Source Profile");
1144 /* Enable AVRCP TG role */
1145 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
1146 if (ret != BLUETOOTH_ERROR_NONE) {
1147 BT_ERR("Failed to enable Bluetooth AVRCP Target Profile");
1151 /* Disable A2DP Source role */
1152 status = audio_disable();
1153 if (OAL_STATUS_SUCCESS != status) {
1154 BT_ERR("Failed to disable Bluetooth A2DP Source Profile, status: %d", status);
1155 ret = BLUETOOTH_ERROR_INTERNAL;
1159 /* Disable AVRCP TG role */
1160 ret = _bt_service_avrcp_disable();
1161 if (ret != BLUETOOTH_ERROR_NONE) {
1162 BT_ERR("Failed to Disable Bluetooth AVRCP Target Profile");
1166 /* Enable A2DP Sink role */
1167 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
1168 if (ret != BLUETOOTH_ERROR_NONE) {
1169 BT_ERR("Failed to enable Bluetooth A2DP Sink Profile");
1173 /* Enable AVRCP CT role */
1174 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
1175 if (ret != BLUETOOTH_ERROR_NONE) {
1176 BT_ERR("Failed to enable Bluetooth AVRCP Controller Profile");
1182 if (ret == BLUETOOTH_ERROR_NONE)
1183 _bt_audio_set_current_role(pending_audio_role);
1185 __handle_pending_requests(ret, BT_AUDIO_SELECT_ROLE, NULL, 0);
1186 pending_audio_role = -1;
1191 int _bt_audio_select_role(bluetooth_audio_role_t role)
1193 int result = BLUETOOTH_ERROR_NONE;
1194 char address[BT_ADDRESS_STRING_SIZE];
1195 bluetooth_device_address_t dev_addr;
1197 retv_if(BLUETOOTH_A2DP_SINK < role, BLUETOOTH_ERROR_INVALID_PARAM);
1198 retv_if(pending_audio_role >= 0, BLUETOOTH_ERROR_IN_PROGRESS);
1202 if (curr_audio_role == role) {
1203 BT_ERR("Desired audio role already enabled, return");
1204 return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
1207 pending_audio_role = role;
1209 if (BLUETOOTH_A2DP_SINK == role) {
1210 if (_bt_is_headset_type_connected(BT_AUDIO_A2DP, address)) {
1211 _bt_convert_addr_string_to_type(dev_addr.addr, address);
1212 result = _bt_audio_disconnect(BT_AUDIO_A2DP, &dev_addr);
1213 if (result != BLUETOOTH_ERROR_NONE) {
1214 BT_ERR("_bt_audio_disconnect(BT_AUDIO_A2DP, %s) Failed", address);
1215 result = BLUETOOTH_ERROR_INTERNAL;
1218 BT_INFO("No BT_AUDIO_A2DP device is connected, proceed with role switch");
1219 g_idle_add(__handle_pending_audio_select_role, NULL);
1220 result = BLUETOOTH_ERROR_NONE;
1223 if (_bt_is_headset_type_connected(BT_AUDIO_A2DP_SOURCE, address)) {
1224 _bt_convert_addr_string_to_type(dev_addr.addr, address);
1225 result = _bt_audio_disconnect(BT_AUDIO_A2DP_SOURCE, &dev_addr);
1226 if (result != BLUETOOTH_ERROR_NONE) {
1227 BT_ERR("_bt_audio_disconnect(BT_AUDIO_A2DP_SOURCE, %s) Failed", address);
1228 result = BLUETOOTH_ERROR_INTERNAL;
1231 BT_INFO("No BT_AUDIO_A2DP_SOURCE device is connected, proceed with role switch");
1232 g_idle_add(__handle_pending_audio_select_role, NULL);
1233 result = BLUETOOTH_ERROR_NONE;
1241 void _bt_audio_set_current_role(bluetooth_audio_role_t role)
1243 BT_INFO("role: %s", (role == BLUETOOTH_A2DP_SINK) ? "AUDIO_SINK" : "AUDIO_SOURCE");
1244 curr_audio_role = role;