Adapt HF Profile Connect & Disconnect to BT HAL framework
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / audio / bt-service-audio.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Anupam Roy <anupam.r@samsung.com>
7  *
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
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <sys/un.h>
23 #include <sys/socket.h>
24 #include <sys/errno.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <vconf.h>
28
29 #include <oal-event.h>
30 #include <oal-hardware.h>
31 #include <oal-audio-src.h>
32 #include <oal-a2dp-sink.h>
33
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"
43 #include "bt-service-hf-client.h"
44 #include "oal-hf-client.h"
45
46 #ifdef TIZEN_SUPPORT_DUAL_HF
47 #define VCONF_KEY_BT_HOST_BT_MAC_ADDR "db/wms/host_bt_mac"
48 #endif
49
50 /* Static variables and Macros */
51 static GList *g_connected_list;
52 static bt_headset_wait_t *g_wait_data;
53 typedef struct {
54         unsigned int type;
55         int device_state;
56         char device_address[BT_ADDRESS_STRING_SIZE + 1];
57 } bt_connected_headset_data_t;
58
59 /* List for saving device service search info */
60 static GSList *pending_audio_conn_list = NULL;
61
62 /* Headset connection data structures */
63 gboolean connection_local = FALSE;
64
65 static int curr_audio_role = BLUETOOTH_A2DP_SOURCE;
66 static int pending_audio_role = -1;
67
68 static void __bt_remove_device_from_wait_list(char *prev_waiting_device);
69
70 static void __bt_audio_event_handler(int oal_event, gpointer event_data);
71
72 static int __bt_process_audio_connect_all(bt_pending_audio_conn_t *info);
73
74 static int __bt_is_headset_connecting(int type);
75
76 static int __bt_is_headset_disconnecting(int type);
77
78 static void __bt_audio_cleanup_resources(void);
79
80 static gboolean __handle_pending_audio_select_role(gpointer data);
81
82 static void __bt_reply_pending_audio_connect_req(char *address, int result);
83
84 #if 0
85 void _bt_headset_set_local_connection(gboolean value)
86 {
87         BT_INFO("setting connection_local to %d", value);
88         connection_local = value;
89 }
90
91 gboolean _bt_headset_get_local_connection()
92 {
93         return connection_local;
94 }
95 #endif
96
97 void _bt_set_audio_wait_data_flag(gboolean flag)
98 {
99         BT_DBG("_bt_set_audio_wait_data_flag \n");
100         g_wait_data->ag_flag = flag;
101 }
102
103 bt_headset_wait_t *_bt_get_audio_wait_data(void)
104 {
105         BT_DBG("_bt_get_audio_wait_data \n");
106         return g_wait_data;
107 }
108
109 static void __bt_free_wait_data(void)
110 {
111         if (g_wait_data != NULL) {
112                 g_free(g_wait_data->address);
113                 g_free(g_wait_data);
114                 g_wait_data = NULL;
115         }
116 }
117
118 void _bt_rel_wait_data(void)
119 {
120         BT_DBG("_bt_rel_wait_data \n");
121         __bt_free_wait_data();
122 }
123
124 void _bt_add_headset_to_list(int type, int status, const char *address)
125 {
126         bt_connected_headset_data_t *connected_device;
127         bt_connected_headset_data_t *device;
128         GList *node;
129
130         BT_DBG("_bt_add_headset_to_list \n");
131
132         node = g_list_first(g_connected_list);
133         while (node != NULL) {
134                 device = (bt_connected_headset_data_t *)node->data;
135
136                 if (g_strcmp0(device->device_address, address) == 0) {
137                         BT_DBG("Address match, update connection type \n");
138                         if (status == BT_STATE_CONNECTED)
139                                 device->type |= type;
140                         device->device_state = status;
141                         return;
142                 }
143                 node = g_list_next(node);
144         }
145
146         connected_device = g_malloc0(sizeof(bt_connected_headset_data_t));
147         /* Fix : NULL_RETURNS */
148         if (connected_device == NULL) {
149                 BT_ERR("No memory allocated");
150                 return;
151         }
152
153         connected_device->device_state = status;
154         //if ((status == BT_STATE_CONNECTED) || (status == BT_STATE_CONNECTING))
155         if (status == BT_STATE_CONNECTED)
156                 connected_device->type |= type;
157         g_strlcpy(connected_device->device_address, address,
158                         sizeof(connected_device->device_address));
159         g_connected_list = g_list_append(g_connected_list, connected_device);
160         BT_INFO("Added device[%s] in connected list, device state [%d] Device Type [%d]",
161                                 address, connected_device->device_state, connected_device->type);
162 }
163
164 gboolean _bt_is_headset_type_connected(int type, char *address)
165 {
166         GList *node;
167
168         node = g_list_first(g_connected_list);
169         while (node != NULL) {
170                 bt_connected_headset_data_t *connected_device = node->data;
171
172                 if (connected_device->type & type) {
173                         if (address != NULL)
174                                 g_strlcpy(address, connected_device->device_address,
175                                                 BT_ADDRESS_STRING_SIZE);
176                         return TRUE;
177                 }
178
179                 node = g_list_next(node);
180         }
181         return FALSE;
182 }
183
184
185 gboolean _bt_is_headset_address_type_connected(int type, const char *address)
186 {
187         GList *node;
188
189         node = g_list_first(g_connected_list);
190         while (node != NULL) {
191                 bt_connected_headset_data_t *connected_device = node->data;
192
193                 if (connected_device && (connected_device->type & type)) {
194                         if (memcmp(connected_device->device_address, address, 19) == 0)
195                                 return TRUE;
196                 }
197
198                 node = g_list_next(node);
199         }
200         return FALSE;
201 }
202
203 static void __bt_set_headset_disconnection_type(const char *address)
204 {
205         bt_connected_headset_data_t *connected_device;
206         GList *node;
207
208         node = g_list_first(g_connected_list);
209         while (node != NULL) {
210                 connected_device = node->data;
211                 if (g_strcmp0(connected_device->device_address, address) == 0) {
212                         g_wait_data->disconnection_type = connected_device->type;
213                         return;
214                 }
215                 node = g_list_next(node);
216         }
217 }
218
219 #ifdef TIZEN_SUPPORT_DUAL_HF
220 gboolean __bt_is_companion_device(const char *addr)
221 {
222         if (TIZEN_PROFILE_WEARABLE) {
223                 char *host_device_address = NULL;
224                 host_device_address = vconf_get_str(VCONF_KEY_BT_HOST_BT_MAC_ADDR);
225
226                 if (!host_device_address) {
227                         BT_INFO("Failed to get a companion device address");
228                         return FALSE;
229                 }
230
231                 if (g_strcmp0(host_device_address, addr) == 0) {
232                         BT_INFO("addr[%s] is companion device", addr);
233                         return TRUE;
234                 }
235
236                 return FALSE;
237         } else {
238         /* TODO : Need to add companion device check condition for Phone models */
239         return FALSE;
240         }
241 }
242 #endif
243
244 static int __bt_is_headset_disconnecting(int type)
245 {
246         bt_connected_headset_data_t *connected_device = NULL;
247
248         /* Check if any other headset is connected */
249         GList *node = NULL;
250
251         node = g_list_first(g_connected_list);
252         while (node != NULL) {
253                 connected_device = node->data;
254                 if (connected_device->device_state == BT_STATE_DISCONNECTING)
255                         return BLUETOOTH_ERROR_CONNECTION_BUSY;
256                 node = g_list_next(node);
257         }
258
259         return BLUETOOTH_ERROR_NONE;
260 }
261
262 static int __bt_is_headset_connecting(int type)
263 {
264         bt_connected_headset_data_t *connected_device = NULL;
265
266         /* Check if any other headset is connected */
267         GList *node = NULL;
268
269         node = g_list_first(g_connected_list);
270         while (node != NULL) {
271                 connected_device = node->data;
272                 if (connected_device->device_state == BT_STATE_CONNECTING) {
273                         BT_ERR("@@Device [%s] is already under connecting state", connected_device->device_address);
274                         return BLUETOOTH_ERROR_CONNECTION_BUSY;
275                 }
276                 node = g_list_next(node);
277         }
278
279         return BLUETOOTH_ERROR_NONE;
280 }
281
282 static int __bt_is_headset_connected(int current_conn_type, const char *address)
283 {
284         gboolean connected = FALSE;
285         int disconn_type;
286         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
287         bluetooth_device_address_t device_address;
288         bt_connected_headset_data_t *connected_device = NULL;
289 #ifdef TIZEN_SUPPORT_DUAL_HF
290         gboolean is_companion_device = FALSE;
291 #endif
292
293         /* Check if any other headset is connected */
294         GList *node = NULL;;
295
296         BT_INFO("Checking  if any Headset connected or not: current device [%s] current dev type [%d]", address, current_conn_type);
297         node = g_list_first(g_connected_list);
298         while (node != NULL) {
299                 connected_device = node->data;
300                 BT_INFO(" A Device is already connected found in list [%s] conn type [%d]",
301                                 connected_device->device_address, connected_device->type);
302                 if ((connected_device->type & current_conn_type)) {
303                         g_strlcpy(connected_address, connected_device->device_address,
304                                         BT_ADDRESS_STRING_SIZE + 1);
305 #ifdef TIZEN_SUPPORT_DUAL_HF
306                         is_companion_device = __bt_is_companion_device(connected_address);
307                         BT_INFO(" is_companion_device[%d]", is_companion_device);
308
309                         if (!is_companion_device) {
310                                 connected = TRUE;
311                                 break;
312                         }
313 #else
314                         connected = TRUE;
315                         break;
316 #endif
317                 }
318                 node = g_list_next(node);
319         }
320
321         if (!connected) {
322                 BT_INFO("There is no connected device with connection type [%d]", current_conn_type);
323                 return BLUETOOTH_ERROR_NOT_CONNECTED;
324         }
325
326         BT_DBG("connected headset %s", connected_address);
327
328         if (g_strcmp0(connected_address, address) == 0)
329                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
330 #ifdef TIZEN_SUPPORT_DUAL_HF
331         else if (TRUE == __bt_is_companion_device(address))
332                 return BLUETOOTH_ERROR_NOT_CONNECTED;
333 #endif
334
335         /* Convert BD address from string type */
336         _bt_convert_addr_string_to_type(device_address.addr, connected_address);
337         int value = BLUETOOTH_ERROR_NONE;
338         BT_DBG("Already connected headset addr  [%s] connected headset type [0x%x] current dev conn type [0x%x]",
339                                 connected_address, connected_device->type, current_conn_type);
340         disconn_type = connected_device->type & current_conn_type;
341         BT_DBG("Attempt disconnection of Type [0x%x] of already connected device" , disconn_type);
342         value = _bt_audio_disconnect(disconn_type, &device_address);
343
344         /* If already one device is waiting, remove current waiting device and add new */
345         if (value == BLUETOOTH_ERROR_NONE) {
346                 if (g_wait_data != NULL) {
347                         if (g_strcmp0(g_wait_data->address, address) != 0) {
348                                 BT_INFO("Already one device was waiting for connection [%s], remove it", g_wait_data->address);
349                                 __bt_remove_device_from_wait_list(g_wait_data->address);
350                                 __bt_free_wait_data();
351                         }
352                 }
353
354                 if (g_wait_data == NULL) {
355                         BT_INFO("Add current device [%s] into waiting list", address);
356                         g_wait_data = g_malloc0(sizeof(bt_headset_wait_t));
357                         g_wait_data->address = g_strdup(address);
358                         g_wait_data->type = current_conn_type;
359                         g_wait_data->ag_flag = FALSE;
360
361                         /* Set disconnection type */
362                         __bt_set_headset_disconnection_type(connected_address);
363                 }
364         }
365
366         return value;
367 }
368
369 static void __bt_remove_device_from_wait_list(char *prev_waiting_device)
370 {
371         /* Before deleting the request update the UI */
372         bluetooth_device_address_t device_address;
373         int result = BLUETOOTH_ERROR_INTERNAL;
374         GArray *out_param;
375         invocation_info_t *req_info;
376         BT_INFO("We are about to DBUS return previous waiting device [%s]", prev_waiting_device);
377
378         _bt_convert_addr_string_to_type(device_address.addr, prev_waiting_device);
379
380         req_info = _bt_get_request_info_data(BT_AV_CONNECT, prev_waiting_device);
381         if (req_info == NULL) {
382                 BT_ERR("req_info == NULL");
383                 return;
384         } else {
385                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
386                 g_array_append_vals(out_param, &device_address,
387                                 sizeof(bluetooth_device_address_t));
388                 _bt_service_method_return(req_info->context,
389                                 out_param, result);
390                 g_array_free(out_param, TRUE);
391                 g_free(req_info->user_data);
392                 _bt_free_info_from_invocation_list(req_info);
393         }
394 }
395
396 static int __bt_process_audio_profile_connect(bt_audio_type_t type, bluetooth_device_address_t *address)
397 {
398         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
399         int result = BLUETOOTH_ERROR_NONE;
400         BT_INFO("+");
401
402         _bt_convert_addr_type_to_string(addr,
403                         (unsigned char *)address->addr);
404         /* Attempt profile level connection based on type */
405         switch (type) {
406         case BT_AUDIO_HSP:
407                 result = _bt_connect_remote_hfp(address);
408                 break;
409         case BT_AUDIO_A2DP:
410                 result = _bt_a2dp_connect_remote_sink(address);
411                 break;
412         case BT_AVRCP_TARGET:
413                 result = _bt_avrcp_connect_remote_ctrl(address);
414                 break;
415         case BT_AVRCP:
416                 result = _bt_avrcp_connect_remote_target(address);
417                 break;
418         case BT_AUDIO_A2DP_SOURCE:
419                 return _bt_a2dp_connect_remote_source(address);
420                 break;
421         case BT_AUDIO_AG:
422                 return _bt_connect_remote_ag(address);
423         default:
424                 BT_ERR("Unknown role");
425                 return BLUETOOTH_ERROR_INTERNAL;
426         }
427
428         if (result == BLUETOOTH_ERROR_NONE) {
429                 //_bt_headset_set_local_connection(TRUE);
430                 /* Add data to the connected list */
431                 _bt_add_headset_to_list(type, BT_STATE_CONNECTING, addr);
432         } else {
433                 BT_ERR("Profile [%d] connect failed!!", type);
434         }
435         BT_INFO("-");
436         return result;
437 }
438
439 static int __bt_process_audio_profile_disconnect(bt_audio_type_t type, bluetooth_device_address_t *address)
440 {
441         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
442         int result = BLUETOOTH_ERROR_NONE;
443         GList *node;
444         BT_INFO("+");
445
446         _bt_convert_addr_type_to_string(addr,
447                         (unsigned char *)address->addr);
448         /* Attempt profile level connection based on type */
449         switch (type) {
450         case BT_AUDIO_HSP:
451                 result = _bt_disconnect_remote_hfp(address);
452                 break;
453         case BT_AUDIO_A2DP:
454                 result = _bt_a2dp_disconnect_remote_sink(address);
455                 break;
456         case BT_AVRCP_TARGET:
457                 result = _bt_avrcp_disconnect_remote_ctrl(address);
458                 break;
459         case BT_AVRCP:
460                 result = _bt_avrcp_disconnect_remote_target(address);
461                 break;
462         case BT_AUDIO_A2DP_SOURCE:
463                 return _bt_a2dp_disconnect_remote_source(address);
464                 break;
465         default:
466                 BT_ERR("Unknown role");
467                 return BLUETOOTH_ERROR_INTERNAL;
468         }
469
470         if (result == BLUETOOTH_ERROR_NONE) {
471                 /*
472                  *      This logic is added for dual HF mode issue.
473                  */
474                 node = g_list_first(g_connected_list);
475                 while (node != NULL) {
476                         bt_connected_headset_data_t *connected_device = node->data;
477
478                         if (g_strcmp0(connected_device->device_address, addr) == 0) {
479                                 BT_DBG("Connection type update");
480                                 type = connected_device->type;
481                                 break;
482                         }
483                         node = g_list_next(node);
484                 }
485
486                 /* Update device status in connected list */
487                 _bt_add_headset_to_list(type, BT_STATE_DISCONNECTING, addr);
488         } else {
489                 BT_ERR("Profile [%d] connect failed!!", type);
490         }
491         BT_INFO("-");
492         return result;
493 }
494
495 int _bt_audio_initialize(bt_service_module_t module)
496 {
497         oal_status_t status = OAL_STATUS_SUCCESS;
498         int ret  = BLUETOOTH_ERROR_NONE;
499
500         BT_INFO("_bt_audio_initialize: Module [%d]", module);
501
502         switch (module) {
503         case BT_A2DP_SOURCE_MODULE: {
504                 status = audio_enable(NULL, NULL);
505                 if (OAL_STATUS_SUCCESS != status) {
506                         BT_ERR("Failed to initialize Bluetooth A2DP Source Profile, status: %d", status);
507                         return BLUETOOTH_ERROR_INTERNAL;
508                 }
509                 /* Register Audio module event handler */
510                 _bt_service_register_event_handler_callback(module, _bt_a2dp_source_event_handler);
511                 break;
512         }
513         case BT_A2DP_SINK_MODULE: {
514                 status = a2dp_sink_enable(NULL, NULL);
515                 if (status != OAL_STATUS_SUCCESS) {
516                         BT_ERR("Failed to initialize Bluetooth A2DP Sink Profile, status: %d", status);
517                         return BLUETOOTH_ERROR_INTERNAL;
518                 }
519                 /* Register Audio sink module event handler */
520                 _bt_service_register_event_handler_callback(module, _bt_a2dp_sink_event_handler);
521                 break;
522         }
523         case BT_HFP_MODULE: {
524                 status = hf_client_enable();
525                 if (OAL_STATUS_SUCCESS != status) {
526                         BT_ERR("Failed to initialize Bluetooth HFP client Profile, status: %d", status);
527                         return BLUETOOTH_ERROR_INTERNAL;
528                 }
529                 /* Register Audio module event handler */
530                 _bt_service_register_event_handler_callback(module, _bt_hf_client_event_handler);
531                 break;
532         }
533         case BT_AG_MODULE: {
534                 status = hfp_enable(1);
535                 if (OAL_STATUS_SUCCESS != status) {
536                         BT_ERR("Failed to initialize Bluetooth HFP Audio Gateway Profile, status: %d", status);
537                         return BLUETOOTH_ERROR_INTERNAL;
538                 }
539                 /* Register Audio module event handler */
540                 _bt_service_register_event_handler_callback(module, _bt_hf_event_handler);
541                 break;
542         }
543         case BT_AUDIO_ALL_MODULE: {
544                 /* Register Audio module event handler */
545                 _bt_service_register_event_handler_callback(module, __bt_audio_event_handler);
546                 break;
547         }
548         case BT_AVRCP_CTRL_MODULE: {
549                 status = avrcp_ct_enable();
550                 if (status != OAL_STATUS_SUCCESS) {
551                         BT_ERR("Failed to initialize Bluetooth AVRCP Controller Profile, status: %d", status);
552                         return BLUETOOTH_ERROR_INTERNAL;
553                 }
554                 /* Register Avrcp Controller event handler */
555                 _bt_service_register_event_handler_callback(module, _bt_avrcp_ctrl_event_handler);
556                 break;
557         }
558         case BT_AVRCP_MODULE: {
559                 /* Initialize AVRCP Target */
560                 ret = _bt_service_avrcp_enable();
561                 break;
562         }
563         default:
564                 BT_ERR("Not Supported: Module [%d]", module);
565                 return BLUETOOTH_ERROR_NOT_SUPPORT;
566         }
567
568         BT_INFO("Bluetooth audio interface initialised");
569         return ret;
570 }
571
572 static void __bt_audio_event_handler(int oal_event, gpointer event_data)
573 {
574         BT_INFO("+");
575
576         switch (oal_event) {
577         case OAL_EVENT_ADAPTER_DISABLED: {
578                 BT_INFO("Adapter Disabled..cleanup resources");
579                 __bt_audio_cleanup_resources();
580                 break;
581         }
582         case OAL_EVENT_ADAPTER_ENABLED: {
583                 /*TODO Currently not handled */
584                 BT_INFO("Adapter Enabled..");
585                 break;
586         }
587         default:
588                 BT_ERR("Unknown event..[%d]", oal_event);
589         }
590         BT_INFO("-");
591 }
592
593 static void __bt_audio_cleanup_resources(void)
594 {
595         GList *node;
596         GSList *l;
597         bt_pending_audio_conn_t *info = NULL;
598         BT_INFO("+");
599
600         /* Remove connected device list */
601         node = g_list_first(g_connected_list);
602         while (node != NULL) {
603                 bt_connected_headset_data_t *connected_device = node->data;
604                 BT_INFO("Data found [%s] in connected device list...remove it..", connected_device->device_address);
605                 g_connected_list = g_list_remove(g_connected_list, connected_device);
606                 node = g_list_next(node);
607         }
608
609         /* Remove pending connection list */
610         for (l = pending_audio_conn_list; l != NULL; l = g_slist_next(l)) {
611                 info = (bt_pending_audio_conn_t*)l->data;
612
613                 if (info) {
614                         BT_INFO("Info found for addr [%s], remove it...", info->address);
615                         pending_audio_conn_list = g_slist_remove(pending_audio_conn_list, info);
616                         g_free(info->address);
617                         g_free(info);
618                         info = NULL;
619                 }
620         }
621
622         BT_INFO("-");
623 }
624
625 static int __bt_process_audio_connect_all(bt_pending_audio_conn_t *info)
626 {
627         bluetooth_device_address_t device_address;
628         int result = BLUETOOTH_ERROR_NONE;
629         int type = BT_AUDIO_ALL;
630         BT_INFO("+");
631
632         _bt_convert_addr_string_to_type(device_address.addr, info->address);
633
634         /* Attempt profile level connection */
635         if (info->is_hfp_supported) {
636                 type = BT_AUDIO_HSP;
637                 result = _bt_connect_remote_hfp(&device_address);
638
639                 if (info->is_a2dp_sink_supported)
640                         BT_INFO("A2DP is supported by [%s]", info->address);
641                 else
642                         BT_INFO("A2DP is not supported");
643         } else if (info->is_a2dp_sink_supported) {
644                 type = BT_AUDIO_A2DP;
645                 result = _bt_a2dp_connect_remote_sink(&device_address);
646         } else if (info->is_a2dp_src_supported) {
647                 type = BT_AUDIO_A2DP_SOURCE;
648                 result = _bt_a2dp_connect_remote_source(&device_address);
649         }
650         if (result == BLUETOOTH_ERROR_NONE) {
651                 //_bt_headset_set_local_connection(TRUE);
652                 /* Add data to the connected list */
653                 _bt_add_headset_to_list(type, BT_STATE_CONNECTING, info->address);
654         } else {
655                 BT_ERR("Profile connect failed!!");
656         }
657         BT_INFO("-");
658         return result;
659 }
660
661 bt_pending_audio_conn_t* _bt_get_pending_audio_conn_info(char *address)
662 {
663         BT_INFO("+");
664         bt_pending_audio_conn_t *info = NULL;
665         GSList *l;
666
667         for (l = pending_audio_conn_list; l != NULL; l = g_slist_next(l)) {
668                 info = (bt_pending_audio_conn_t*)l->data;
669
670                 if (g_strcmp0(info->address, address) == 0 && info->search_status == SERVICE_SEARCH_DONE) {
671                         BT_INFO("Pending data found for addr [%s]", info->address);
672                         return info;
673                 }
674         }
675         BT_INFO("Pending data not found!!");
676         return NULL;
677 }
678
679 static void __bt_reply_pending_audio_connect_req(char *address, int result)
680 {
681         GArray *out_param;
682         invocation_info_t *req_info;
683
684         BT_DBG("+");
685         ret_if(NULL == address);
686
687         /* Reply to async request for Audio connect, if any */
688         req_info = _bt_get_request_info_data(BT_AUDIO_CONNECT, address);
689         ret_if(NULL == req_info);
690
691         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
692         g_array_append_vals(out_param, address, BT_ADDRESS_STRING_SIZE);
693         _bt_service_method_return(req_info->context, out_param, result);
694
695         g_array_free(out_param, TRUE);
696         g_free(req_info->user_data);
697         _bt_free_info_from_invocation_list(req_info);
698
699         BT_DBG("-");
700 }
701
702 void _bt_cleanup_audio_pending_conn_info_and_reply_pending_req(bt_pending_audio_conn_t *info, int result)
703 {
704         ret_if(NULL == info);
705
706         /* Reply to async request for Audio connect, if any */
707         __bt_reply_pending_audio_connect_req(info->address, result);
708
709         /* Remove pending audio conn info */
710         pending_audio_conn_list = g_slist_remove(pending_audio_conn_list, info);
711         g_free(info->address);
712         g_free(info);
713 }
714
715 gboolean _bt_audio_check_pending_connection(char *address)
716 {
717         bt_pending_audio_conn_t *data = NULL;
718         bluetooth_device_address_t device_address;
719         BT_INFO("+");
720
721         data = _bt_get_pending_audio_conn_info(address);
722
723         if (data) {
724                 BT_INFO("A2DP Connection is pending..initiate it...");
725                 if (data->is_a2dp_sink_supported && data->conn_req_type == BT_AUDIO_ALL) {
726                         _bt_convert_addr_string_to_type(device_address.addr, address);
727                         if (_bt_audio_connect(BT_AUDIO_A2DP, &device_address) != BLUETOOTH_ERROR_NONE) {
728                                 BT_INFO("A2DP connect triggered for [%s]  but it failed!!", data->address);
729                                 _bt_cleanup_audio_pending_conn_info_and_reply_pending_req(data, BLUETOOTH_ERROR_INTERNAL);
730                         } else {
731                                 BT_INFO("A2DP connect triggered successfully for [%s]", data->address);
732                         }
733                 } else if (data->is_a2dp_src_supported && data->conn_req_type == BT_AUDIO_HFP_SOURCE) {
734                         _bt_convert_addr_string_to_type(device_address.addr, address);
735                         if (_bt_audio_connect(BT_AUDIO_A2DP_SOURCE, &device_address) != BLUETOOTH_ERROR_NONE) {
736                                 BT_INFO("A2DP sink connect triggered for [%s]  but it failed!!", data->address);
737                                 _bt_cleanup_audio_pending_conn_info_and_reply_pending_req(data, BLUETOOTH_ERROR_INTERNAL);
738                         } else {
739                                 BT_INFO("A2DP sink connect triggered successfully for [%s]", data->address);
740                         }
741                 }
742                 return TRUE;
743         } else {
744                 BT_INFO("A2DP Connection is not pending..");
745         }
746         BT_INFO("-");
747         return FALSE;
748 }
749
750 void _bt_audio_check_pending_disconnection(char *address, int type)
751 {
752         int ret = BLUETOOTH_ERROR_NONE;
753         bluetooth_device_address_t device_address;
754         BT_INFO("+");
755
756         if (_bt_is_service_connected(address, type)) {
757                 BT_INFO("Service [%d] is connected with device [%s], disconnect it...", type, address);
758                 _bt_convert_addr_string_to_type(device_address.addr, address);
759                 ret = __bt_process_audio_profile_disconnect(type, &device_address);
760
761                 if (ret != BLUETOOTH_ERROR_NONE)
762                         BT_ERR("Disconnecting service [%d] with device [%s] failed!!", type, address);
763         } else {
764                 BT_INFO("Service [%d] is Not connected with device [%s],..", type, address);
765         }
766         BT_INFO("-");
767 }
768
769 int __bt_handle_audio_all_connect(bt_pending_audio_conn_t *info)
770 {
771         bt_remote_dev_info_t *rem_info;
772         int ret;
773         int indx;
774
775         BT_DBG("+");
776
777         retv_if(NULL == info, BLUETOOTH_ERROR_INTERNAL);
778         retv_if(NULL == info->address, BLUETOOTH_ERROR_INTERNAL);
779
780         rem_info = _bt_service_get_remote_dev_info(info->address);
781         if (!rem_info) {
782                 BT_ERR("_bt_service_get_remote_dev_info returned NULL");
783                 ret = BLUETOOTH_ERROR_NOT_PAIRED;
784                 goto fail;
785         }
786
787         for (indx = 0; indx < rem_info->uuid_count; indx++) {
788                 BT_INFO("UUID [%s]", rem_info->uuids[indx]);
789                 if (0 == g_strcmp0(A2DP_SINK_UUID, rem_info->uuids[indx])) {
790                         BT_INFO("Device supports A2DP Sink Profile");
791                         info->is_a2dp_sink_supported = TRUE;
792                 } else if (0 == g_strcmp0(A2DP_SOURCE_UUID, rem_info->uuids[indx])) {
793                         BT_INFO("Device supports A2DP Source Profile");
794                         info->is_a2dp_src_supported = TRUE;
795                 } else if ((g_strcmp0(HFP_HS_UUID, rem_info->uuids[indx]) == 0)) {
796                         BT_INFO("Device supports HFP Profile");
797                         info->is_hfp_supported = TRUE;
798                 }
799         }
800
801         if (info->is_a2dp_sink_supported == FALSE &&
802                         info->is_hfp_supported == FALSE &&
803                         info->is_a2dp_src_supported == FALSE) {
804                 BT_ERR("No audio profile is supported");
805                 ret = BLUETOOTH_ERROR_NOT_SUPPORT;
806                 goto fail;
807         }
808
809         BT_INFO("AUDIO_CONNECT_ALL request for [%s]", info->address);
810         info->search_status = SERVICE_SEARCH_DONE;
811
812         /* Give preference to HFP over A2DP for outgoing connection sequence for AUDIO_ALL_CONNECT */
813         if (info->is_hfp_supported)
814                 info->type = BT_AUDIO_HSP;
815         else if (info->is_a2dp_sink_supported)
816                 info->type = BT_AUDIO_A2DP;
817         else if (info->is_a2dp_src_supported)
818                 info->type = BT_AUDIO_A2DP_SOURCE;
819
820         ret = __bt_is_headset_connected(info->type, info->address);
821         if (ret == BLUETOOTH_ERROR_ALREADY_CONNECT) {
822                 BT_ERR("Can't start Audio All connect for [%s], reason: [%s]",
823                                 info->address, "Already connected...");
824                 goto fail;
825         }
826
827         if (ret == BLUETOOTH_ERROR_IN_PROGRESS) {
828                 BT_ERR("Can't start Audio All connect for [%s], reason: [%s]",
829                                 info->address, "Already in progress...");
830                 goto fail;
831         }
832
833         if (ret == BLUETOOTH_ERROR_NOT_CONNECTED) {
834                 BT_ERR("Great, can start Audio All connect for [%s]", info->address);
835                 ret = __bt_is_headset_connecting(info->type);
836                 if (ret != BLUETOOTH_ERROR_NONE) {
837                         BT_ERR("Can't start Audio All connect for [%s], reason: [%s]",
838                                         info->address, "some other device conn in progress");
839                         ret = BLUETOOTH_ERROR_IN_PROGRESS;
840                         goto fail;
841                 }
842
843                 ret = __bt_process_audio_connect_all(info);
844                 if (ret != BLUETOOTH_ERROR_NONE) {
845                         BT_ERR("__bt_process_audio_connect_all failed");
846                         goto fail;
847                 }
848
849                 /* If multiple profiles are supported, queue pending connection info */
850                 if ((info->is_hfp_supported && info->is_a2dp_sink_supported) ||
851                                 (info->is_hfp_supported && info->is_a2dp_src_supported)) {
852                         BT_INFO("[%s] Supports HFP and (A2DP_Src or, A2DP_Snk)", info->address);
853                         pending_audio_conn_list = g_slist_append(pending_audio_conn_list, (gpointer)info);
854                 } else {
855                         BT_INFO("[%s] Supports one profile only", info->address);
856                         /*
857                          * It means, we dont need pending connect info as only A2DP (Src/Snk) or
858                          * HFP will be connected for present device, so lets free pending info.
859                          */
860                         g_free(info->address);
861                         g_free(info);
862                 }
863         } else if (ret == BLUETOOTH_ERROR_NONE) {
864                 BT_INFO("Waiting for disconnection...");
865                 /*
866                  * It means, we dont need pending connect info as only A2DP (Src/Snk) or
867                  * HFP will be connected for present device, so lets free pending info.
868                  */
869                 g_free(info->address);
870                 g_free(info);
871         }
872
873         BT_DBG("-");
874         return BLUETOOTH_ERROR_NONE;
875
876 fail:
877         BT_ERR("Audio connect all failed");
878         g_free(info->address);
879         g_free(info);
880         return ret;
881 }
882
883 static gboolean __audio_get_bonded_info(gpointer data)
884 {
885         bt_pending_audio_conn_t *info = data;
886         char address[BT_ADDRESS_STRING_SIZE];
887
888         BT_DBG("+");
889
890         retv_if(NULL == info, FALSE);
891         retv_if(NULL == info->address, FALSE);
892
893         if ((info->bonded_info_retries > 0) && !_bt_is_bonded_devices_retrived()) {
894                 info->bonded_info_retries--;
895                 return TRUE;
896         }
897
898         g_strlcpy(address, info->address, BT_ADDRESS_STRING_SIZE);
899         if (info->bonded_info_retries <= 0) {
900                 BT_ERR_C("Even after all retries, bonded devices stil not retrieved");
901                 g_free(info->address);
902                 g_free(info);
903                 goto failed;
904         }
905
906         if (BLUETOOTH_ERROR_NONE != __bt_handle_audio_all_connect(info)) {
907                 BT_ERR("__bt_handle_audio_all_connect failed");
908                 goto failed;
909         }
910
911         BT_DBG("-");
912         return FALSE;
913 failed:
914         __bt_reply_pending_audio_connect_req(address, BLUETOOTH_ERROR_INTERNAL);
915         return FALSE;
916 }
917
918 int _bt_audio_connect(int type, bluetooth_device_address_t *device_address)
919 {
920         int ret = BLUETOOTH_ERROR_NONE;
921         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
922         bt_pending_audio_conn_t *info = NULL;
923
924         BT_INFO("+");
925
926         BT_CHECK_PARAMETER(device_address, return);
927
928         _bt_convert_addr_type_to_string(address, device_address->addr);
929         BT_INFO("Adderss: [%s], request type: [%d]", address, type);
930
931         /*
932          * If type is BT_AUDIO_ALL or BT_AUDIO_HFP_SOURCE, enqueue search info in
933          * order to find out supported services by the remote device.
934          */
935         if (type == BT_AUDIO_ALL || type ==  BT_AUDIO_HFP_SOURCE) {
936                 if (NULL != _bt_get_pending_audio_conn_info(address)) {
937                         BT_ERR("Connection for same device already in progress");
938                         return BLUETOOTH_ERROR_IN_PROGRESS;
939                 }
940
941                 info = g_malloc0(sizeof(bt_pending_audio_conn_t));
942                 info->search_status = SERVICE_SEARCH_STARTED;
943                 info->address = g_strdup(address);
944                 info->type = type;
945                 info->conn_req_type = type;
946
947                 /* Request bonded device info */
948                 if (!_bt_is_bonded_devices_retrived()) {
949                         info->bonded_info_retries = 5;
950                         /* Wait till bonded devices retrival is completed */
951                         g_timeout_add_seconds(1, __audio_get_bonded_info, info);
952                         return BLUETOOTH_ERROR_NONE;
953                 }
954
955                 return __bt_handle_audio_all_connect(info);
956         }
957
958         /* If type is A2DP Sink or HSP/HFP, check further */
959         ret =  __bt_is_headset_connected(type, address);
960         if (ret == BLUETOOTH_ERROR_ALREADY_CONNECT) {
961                 BT_ERR("Failed with BLUETOOTH_ERROR_ALREADY_CONNECT");
962                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
963         }
964
965         if (ret == BLUETOOTH_ERROR_IN_PROGRESS) {
966                 BT_ERR("Failed with BLUETOOTH_ERROR_IN_PROGRESS");
967                 return BLUETOOTH_ERROR_IN_PROGRESS;
968         }
969
970         if (ret == BLUETOOTH_ERROR_NONE) {
971                 BT_INFO("Waiting for disconnection...");
972         } else if (ret == BLUETOOTH_ERROR_NOT_CONNECTED) {
973                 ret = __bt_process_audio_profile_connect(type, device_address);
974                 if (ret != BLUETOOTH_ERROR_NONE) {
975                         BT_ERR("Failed with %d", ret);
976                         return ret;
977                 }
978         }
979
980         return BLUETOOTH_ERROR_NONE;
981 }
982
983 int _bt_audio_disconnect(int type, bluetooth_device_address_t *device_address)
984 {
985         int ret = BLUETOOTH_ERROR_NONE;
986         int value = BLUETOOTH_ERROR_NONE;
987         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
988
989         _bt_convert_addr_type_to_string(address, device_address->addr);
990         BT_INFO("Audio Diconnect Request: type[%d] address [%s]", type, address);
991
992         if (type == BT_AUDIO_ALL) {
993                 if (_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
994                         type = BT_AUDIO_A2DP;
995                 } else if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
996                         type = BT_AUDIO_HSP;
997                 } else {
998                         BT_ERR("No audio service connected");
999                         return BLUETOOTH_ERROR_NOT_CONNECTED;
1000                 }
1001         } else if (type == BT_AUDIO_HFP_SOURCE) {
1002                 if (_bt_is_service_connected(address, BT_AUDIO_A2DP_SOURCE)) {
1003                         type = BT_AUDIO_A2DP_SOURCE;
1004                 } else if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
1005                         type = BT_AUDIO_HSP;
1006                 } else {
1007                         BT_ERR("No audio service connected");
1008                         return BLUETOOTH_ERROR_NOT_CONNECTED;
1009                 }
1010         }
1011
1012         value = __bt_is_headset_disconnecting(type);
1013         if (value != BLUETOOTH_ERROR_NONE) {
1014                 BT_INFO("Disconnect in progress");
1015                 return BLUETOOTH_ERROR_IN_PROGRESS;
1016         }
1017
1018         ret = __bt_process_audio_profile_disconnect(type, device_address);
1019         return ret;
1020 }
1021
1022 gboolean _bt_is_service_connected(char* address, int type)
1023 {
1024         GList *node;
1025         node = g_list_first(g_connected_list);
1026         while (node != NULL) {
1027                 bt_connected_headset_data_t *conn_device = node->data;
1028
1029                 if ((g_strcmp0(conn_device->device_address, address) == 0) &&
1030                                 (conn_device->type & type)) {
1031                         BT_INFO("Service connected");
1032                         return TRUE;
1033                 }
1034
1035                 node = g_list_next(node);
1036         }
1037         BT_INFO("Service not connected");
1038         return FALSE;
1039 }
1040
1041 void _bt_remove_headset_from_list(int type, const char *address)
1042 {
1043         GList *node;
1044
1045         BT_DBG("_bt_remove_headset_from_list \n");
1046
1047         node = g_list_first(g_connected_list);
1048         while (node != NULL) {
1049                 bt_connected_headset_data_t *connected_device = node->data;
1050
1051                 if (g_strcmp0(connected_device->device_address, address) != 0) {
1052                         node = g_list_next(node);
1053                         continue;
1054                 }
1055
1056                 BT_DBG("Address match \n");
1057
1058                 BT_DBG("Connection type = %x\n", connected_device->type);
1059
1060                 switch (type) {
1061                 case BT_AUDIO_A2DP:
1062                         if (connected_device->type & BT_AUDIO_A2DP)
1063                                 connected_device->type &= ~(BT_AUDIO_A2DP);
1064                         break;
1065                 case BT_AUDIO_HSP:
1066                         if (connected_device->type & BT_AUDIO_HSP)
1067                                 connected_device->type &= ~(BT_AUDIO_HSP);
1068                         break;
1069                 case BT_AUDIO_ALL:
1070                         if (connected_device->type & BT_AUDIO_ALL)
1071                                 connected_device->type &= ~(BT_AUDIO_ALL);
1072                         break;
1073                 case BT_AVRCP_TARGET:
1074                         if (connected_device->type & BT_AVRCP_TARGET)
1075                                 connected_device->type &= ~(BT_AVRCP_TARGET);
1076                         break;
1077                 case BT_AVRCP:
1078                         if (connected_device->type & BT_AVRCP)
1079                                 connected_device->type &= ~(BT_AVRCP);
1080                         break;
1081                 case BT_AUDIO_A2DP_SOURCE:
1082                         if (connected_device->type & BT_AUDIO_A2DP_SOURCE)
1083                                 connected_device->type &= ~(BT_AUDIO_A2DP_SOURCE);
1084                         break;
1085                 default:
1086                         break;
1087                 }
1088
1089                 BT_DBG("Connection type = %x\n", connected_device->type);
1090
1091                 if (connected_device->type == 0x00) {
1092                         BT_INFO("Device will be completely removed from connected list as the only profile connected or connecting got disconnected");
1093                         g_connected_list = g_list_remove(g_connected_list, connected_device);
1094                         g_free(connected_device);
1095                 } else {
1096                         connected_device->device_state = BT_STATE_CONNECTED;
1097                 }
1098
1099                 node = g_list_next(node);
1100         }
1101
1102         /*
1103          * If Audio role selection request is pending (pending_audio_role > 0) then process pending
1104          * audio role select request on BT_AUDIO_A2DP/BT_AUDIO_A2DP_SOURCE device dicaonnection.
1105          */
1106         if (pending_audio_role >= 0 &&
1107                 (type == BT_AUDIO_A2DP || type == BT_AUDIO_A2DP_SOURCE))
1108                 g_idle_add(__handle_pending_audio_select_role, NULL);
1109 }
1110
1111 static void __handle_pending_requests(int result, int service_function, void *user_data, unsigned int size)
1112 {
1113         GSList *l;
1114         GArray *out_param;
1115         invocation_info_t *req_info;
1116
1117         BT_DBG("+");
1118
1119         /* Get method invocation context */
1120         for (l = _bt_get_invocation_list(); l != NULL; ) {
1121                 req_info = l->data;
1122                 l = g_slist_next(l);
1123
1124                 if (req_info == NULL || req_info->service_function != service_function)
1125                         continue;
1126
1127                 /* Create out param */
1128                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1129
1130                 switch (service_function) {
1131                 case BT_AUDIO_SELECT_ROLE:
1132                         _bt_service_method_return(req_info->context, out_param, result);
1133                         g_array_free(out_param, TRUE);
1134                         _bt_free_info_from_invocation_list(req_info);
1135                         return;
1136                 default:
1137                         BT_ERR("Unknown service function[%d]", service_function);
1138                         return;
1139                 }
1140         }
1141
1142         BT_DBG("-");
1143 }
1144
1145 static gboolean __handle_pending_audio_select_role(gpointer data)
1146 {
1147         int ret;
1148         oal_status_t status;
1149
1150         BT_DBG("+");
1151         if (BLUETOOTH_A2DP_SOURCE == pending_audio_role) {
1152                 /* Disable A2DP Sink role */
1153                 status = a2dp_sink_disable();
1154                 if (status != OAL_STATUS_SUCCESS) {
1155                         BT_ERR("Failed to Disable Bluetooth A2DP Sink Profile, status: %d", status);
1156                         ret = BLUETOOTH_ERROR_INTERNAL;
1157                         goto done;
1158                 }
1159
1160                 /* Disable AVRCP CT role */
1161                 status = avrcp_ct_disable();
1162                 if (status != OAL_STATUS_SUCCESS) {
1163                         BT_ERR("Failed to Disable Bluetooth AVRCP Controller Profile, status: %d", status);
1164                         ret = BLUETOOTH_ERROR_INTERNAL;
1165                         goto done;
1166                 }
1167
1168                 /* Enable A2DP Source role */
1169                 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
1170                 if (ret != BLUETOOTH_ERROR_NONE) {
1171                         BT_ERR("Failed to enable Bluetooth A2DP Source Profile");
1172                         goto done;
1173                 }
1174
1175                 /* Enable AVRCP TG role */
1176                 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
1177                 if (ret != BLUETOOTH_ERROR_NONE) {
1178                         BT_ERR("Failed to enable Bluetooth AVRCP Target Profile");
1179                         goto done;
1180                 }
1181         } else {
1182                 /* Disable A2DP Source role */
1183                 status = audio_disable();
1184                 if (OAL_STATUS_SUCCESS != status) {
1185                         BT_ERR("Failed to disable Bluetooth A2DP Source Profile, status: %d", status);
1186                         ret = BLUETOOTH_ERROR_INTERNAL;
1187                         goto done;
1188                 }
1189
1190                 /* Disable AVRCP TG role */
1191                 ret = _bt_service_avrcp_disable();
1192                 if (ret != BLUETOOTH_ERROR_NONE) {
1193                         BT_ERR("Failed to Disable Bluetooth AVRCP Target Profile");
1194                         goto done;
1195                 }
1196
1197                 /* Enable A2DP Sink role */
1198                 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
1199                 if (ret != BLUETOOTH_ERROR_NONE) {
1200                         BT_ERR("Failed to enable Bluetooth A2DP Sink Profile");
1201                         goto done;
1202                 }
1203
1204                 /* Enable AVRCP CT role */
1205                 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
1206                 if (ret != BLUETOOTH_ERROR_NONE) {
1207                         BT_ERR("Failed to enable Bluetooth AVRCP Controller Profile");
1208                         goto done;
1209                 }
1210         }
1211
1212 done:
1213         if (ret == BLUETOOTH_ERROR_NONE)
1214                 _bt_audio_set_current_role(pending_audio_role);
1215
1216         __handle_pending_requests(ret, BT_AUDIO_SELECT_ROLE, NULL, 0);
1217         pending_audio_role = -1;
1218         BT_DBG("-");
1219         return FALSE;
1220 }
1221
1222 int _bt_audio_select_role(bluetooth_audio_role_t role)
1223 {
1224         int result = BLUETOOTH_ERROR_NONE;
1225         char address[BT_ADDRESS_STRING_SIZE];
1226         bluetooth_device_address_t dev_addr;
1227
1228         retv_if(BLUETOOTH_A2DP_SINK < role, BLUETOOTH_ERROR_INVALID_PARAM);
1229         retv_if(pending_audio_role >= 0, BLUETOOTH_ERROR_IN_PROGRESS);
1230
1231         BT_DBG("+");
1232
1233         if (curr_audio_role == role) {
1234                 BT_ERR("Desired audio role already enabled, return");
1235                 return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
1236         }
1237
1238         pending_audio_role = role;
1239
1240         if (BLUETOOTH_A2DP_SINK == role) {
1241                 if (_bt_is_headset_type_connected(BT_AUDIO_A2DP, address)) {
1242                         _bt_convert_addr_string_to_type(dev_addr.addr, address);
1243                         result = _bt_audio_disconnect(BT_AUDIO_A2DP, &dev_addr);
1244                         if (result != BLUETOOTH_ERROR_NONE) {
1245                                 BT_ERR("_bt_audio_disconnect(BT_AUDIO_A2DP, %s) Failed", address);
1246                                 result = BLUETOOTH_ERROR_INTERNAL;
1247                         }
1248                 } else {
1249                         BT_INFO("No BT_AUDIO_A2DP device is connected, proceed with role switch");
1250                         g_idle_add(__handle_pending_audio_select_role, NULL);
1251                         result = BLUETOOTH_ERROR_NONE;
1252                 }
1253         } else {
1254                 if (_bt_is_headset_type_connected(BT_AUDIO_A2DP_SOURCE, address)) {
1255                         _bt_convert_addr_string_to_type(dev_addr.addr, address);
1256                         result = _bt_audio_disconnect(BT_AUDIO_A2DP_SOURCE, &dev_addr);
1257                         if (result != BLUETOOTH_ERROR_NONE) {
1258                                 BT_ERR("_bt_audio_disconnect(BT_AUDIO_A2DP_SOURCE, %s) Failed", address);
1259                                 result = BLUETOOTH_ERROR_INTERNAL;
1260                         }
1261                 } else {
1262                         BT_INFO("No BT_AUDIO_A2DP_SOURCE device is connected, proceed with role switch");
1263                         g_idle_add(__handle_pending_audio_select_role, NULL);
1264                         result = BLUETOOTH_ERROR_NONE;
1265                 }
1266         }
1267
1268         BT_DBG("-");
1269         return result;
1270 }
1271
1272 void _bt_audio_set_current_role(bluetooth_audio_role_t role)
1273 {
1274         BT_INFO("role: %s", (role == BLUETOOTH_A2DP_SINK) ? "AUDIO_SINK" : "AUDIO_SOURCE");
1275         curr_audio_role = role;
1276 }
1277
1278 int _bt_hf_connect(bluetooth_device_address_t *device_address)
1279 {
1280         int result = BLUETOOTH_ERROR_NONE;
1281         BT_INFO("+");
1282
1283         result = _bt_connect_remote_ag(device_address);
1284         if (result != BLUETOOTH_ERROR_NONE)
1285                 BT_ERR("HF Client connect to remote AG failed");
1286
1287         return result;
1288 }
1289
1290 int _bt_hf_disconnect(bluetooth_device_address_t *device_address)
1291 {
1292         int result = BLUETOOTH_ERROR_NONE;
1293         BT_INFO("+");
1294
1295         result = _bt_disconnect_remote_ag(device_address);
1296         if (result != BLUETOOTH_ERROR_NONE)
1297                 BT_ERR("HF Client disconnect to remote AG failed");
1298
1299         return result;
1300 }