Fix the build warnings and typo error
[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
44 #ifdef TIZEN_SUPPORT_DUAL_HF
45 #define VCONF_KEY_BT_HOST_BT_MAC_ADDR "db/wms/host_bt_mac"
46 #endif
47
48 /* Static variables and Macros */
49 static GList *g_connected_list;
50 static bt_headset_wait_t *g_wait_data;
51 typedef struct {
52         unsigned int type;
53         int device_state;
54         char device_address[BT_ADDRESS_STRING_SIZE + 1];
55 } bt_connected_headset_data_t;
56
57 /* List for saving device service search info */
58 static GSList *pending_audio_conn_list = NULL;
59
60 /* Headset connection data structures */
61 gboolean connection_local = FALSE;
62
63 static int curr_audio_role = BLUETOOTH_A2DP_SOURCE;
64 static int pending_audio_role = -1;
65
66 static void __bt_remove_device_from_wait_list(char *prev_waiting_device);
67
68 static void __bt_audio_event_handler(int oal_event, gpointer event_data);
69
70 static int __bt_process_audio_connect_all(bt_pending_audio_conn_t *info);
71
72 static int __bt_is_headset_connecting(int type);
73
74 static int __bt_is_headset_disconnecting(int type);
75
76 static void __bt_audio_cleanup_resources(void);
77
78 static gboolean __handle_pending_audio_select_role(gpointer data);
79
80 static void __bt_reply_pending_audio_connect_req(char *address, int result);
81
82 #if 0
83 void _bt_headset_set_local_connection(gboolean value)
84 {
85         BT_INFO("setting connection_local to %d", value);
86         connection_local = value;
87 }
88
89 gboolean _bt_headset_get_local_connection()
90 {
91         return connection_local;
92 }
93 #endif
94
95 void _bt_set_audio_wait_data_flag(gboolean flag)
96 {
97         BT_DBG("_bt_set_audio_wait_data_flag \n");
98         g_wait_data->ag_flag = flag;
99 }
100
101 bt_headset_wait_t *_bt_get_audio_wait_data(void)
102 {
103         BT_DBG("_bt_get_audio_wait_data \n");
104         return g_wait_data;
105 }
106
107 static void __bt_free_wait_data(void)
108 {
109         if (g_wait_data != NULL) {
110                 g_free(g_wait_data->address);
111                 g_free(g_wait_data);
112                 g_wait_data = NULL;
113         }
114 }
115
116 void _bt_rel_wait_data(void)
117 {
118         BT_DBG("_bt_rel_wait_data \n");
119         __bt_free_wait_data();
120 }
121
122 void _bt_add_headset_to_list(int type, int status, const char *address)
123 {
124         bt_connected_headset_data_t *connected_device;
125         bt_connected_headset_data_t *device;
126         GList *node;
127
128         BT_DBG("_bt_add_headset_to_list \n");
129
130         node = g_list_first(g_connected_list);
131         while (node != NULL) {
132                 device = (bt_connected_headset_data_t *)node->data;
133
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;
139                         return;
140                 }
141                 node = g_list_next(node);
142         }
143
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");
148                 return;
149         }
150
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);
160 }
161
162 gboolean _bt_is_headset_type_connected(int type, char *address)
163 {
164         GList *node;
165
166         node = g_list_first(g_connected_list);
167         while (node != NULL) {
168                 bt_connected_headset_data_t *connected_device = node->data;
169
170                 if (connected_device->type & type) {
171                         if (address != NULL)
172                                 g_strlcpy(address, connected_device->device_address,
173                                                 BT_ADDRESS_STRING_SIZE);
174                         return TRUE;
175                 }
176
177                 node = g_list_next(node);
178         }
179         return FALSE;
180 }
181
182 static void __bt_set_headset_disconnection_type(const char *address)
183 {
184         bt_connected_headset_data_t *connected_device;
185         GList *node;
186
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;
192                         return;
193                 }
194                 node = g_list_next(node);
195         }
196 }
197
198 #ifdef TIZEN_SUPPORT_DUAL_HF
199 gboolean __bt_is_companion_device(const char *addr)
200 {
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);
204
205                 if (!host_device_address) {
206                         BT_INFO("Failed to get a companion device address");
207                         return FALSE;
208                 }
209
210                 if (g_strcmp0(host_device_address, addr) == 0) {
211                         BT_INFO("addr[%s] is companion device", addr);
212                         return TRUE;
213                 }
214
215                 return FALSE;
216         } else {
217         /* TODO : Need to add companion device check condition for Phone models */
218         return FALSE;
219         }
220 }
221 #endif
222
223 static int __bt_is_headset_disconnecting(int type)
224 {
225         bt_connected_headset_data_t *connected_device = NULL;
226
227         /* Check if any other headset is connected */
228         GList *node = NULL;
229
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);
236         }
237
238         return BLUETOOTH_ERROR_NONE;
239 }
240
241 static int __bt_is_headset_connecting(int type)
242 {
243         bt_connected_headset_data_t *connected_device = NULL;
244
245         /* Check if any other headset is connected */
246         GList *node = NULL;
247
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;
254                 }
255                 node = g_list_next(node);
256         }
257
258         return BLUETOOTH_ERROR_NONE;
259 }
260
261 static int __bt_is_headset_connected(int current_conn_type, const char *address)
262 {
263         gboolean connected = FALSE;
264         int disconn_type;
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;
270 #endif
271
272         /* Check if any other headset is connected */
273         GList *node = NULL;;
274
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);
287
288                         if (!is_companion_device) {
289                                 connected = TRUE;
290                                 break;
291                         }
292 #else
293                         connected = TRUE;
294                         break;
295 #endif
296                 }
297                 node = g_list_next(node);
298         }
299
300         if (!connected) {
301                 BT_INFO("There is no connected device with connection type [%d]", current_conn_type);
302                 return BLUETOOTH_ERROR_NOT_CONNECTED;
303         }
304
305         BT_DBG("connected headset %s", connected_address);
306
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;
312 #endif
313
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);
322
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();
330                         }
331                 }
332
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;
339
340                         /* Set disconnection type */
341                         __bt_set_headset_disconnection_type(connected_address);
342                 }
343         }
344
345         return value;
346 }
347
348 static void __bt_remove_device_from_wait_list(char *prev_waiting_device)
349 {
350         /* Before deleting the request update the UI */
351         bluetooth_device_address_t device_address;
352         int result = BLUETOOTH_ERROR_INTERNAL;
353         GArray *out_param;
354         invocation_info_t *req_info;
355         BT_INFO("We are about to DBUS return previous waiting device [%s]", prev_waiting_device);
356
357         _bt_convert_addr_string_to_type(device_address.addr, prev_waiting_device);
358
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");
362                 return;
363         } else {
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,
368                                 out_param, result);
369                 g_array_free(out_param, TRUE);
370                 g_free(req_info->user_data);
371                 _bt_free_info_from_invocation_list(req_info);
372         }
373 }
374
375 static int __bt_process_audio_profile_connect(bt_audio_type_t type, bluetooth_device_address_t *address)
376 {
377         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
378         int result = BLUETOOTH_ERROR_NONE;
379         BT_INFO("+");
380
381         _bt_convert_addr_type_to_string(addr,
382                         (unsigned char *)address->addr);
383         /* Attempt profile level connection based on type */
384         switch (type) {
385         case BT_AUDIO_HSP:
386                 result = _bt_connect_remote_hfp(address);
387                 break;
388         case BT_AUDIO_A2DP:
389                 result = _bt_a2dp_connect_remote_sink(address);
390                 break;
391         case BT_AVRCP_TARGET:
392                 result = _bt_avrcp_connect_remote_ctrl(address);
393                 break;
394         case BT_AVRCP:
395                 result = _bt_avrcp_connect_remote_target(address);
396                 break;
397         case BT_AUDIO_A2DP_SOURCE:
398                 return _bt_a2dp_connect_remote_source(address);
399                 break;
400         default:
401                 BT_ERR("Unknown role");
402                 return BLUETOOTH_ERROR_INTERNAL;
403         }
404
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);
409         } else {
410                 BT_ERR("Profile [%d] connect failed!!", type);
411         }
412         BT_INFO("-");
413         return result;
414 }
415
416 static int __bt_process_audio_profile_disconnect(bt_audio_type_t type, bluetooth_device_address_t *address)
417 {
418         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
419         int result = BLUETOOTH_ERROR_NONE;
420         GList *node;
421         BT_INFO("+");
422
423         _bt_convert_addr_type_to_string(addr,
424                         (unsigned char *)address->addr);
425         /* Attempt profile level connection based on type */
426         switch (type) {
427         case BT_AUDIO_HSP:
428                 result = _bt_disconnect_remote_hfp(address);
429                 break;
430         case BT_AUDIO_A2DP:
431                 result = _bt_a2dp_disconnect_remote_sink(address);
432                 break;
433         case BT_AVRCP_TARGET:
434                 result = _bt_avrcp_disconnect_remote_ctrl(address);
435                 break;
436         case BT_AVRCP:
437                 result = _bt_avrcp_disconnect_remote_target(address);
438                 break;
439         case BT_AUDIO_A2DP_SOURCE:
440                 return _bt_a2dp_disconnect_remote_source(address);
441                 break;
442         default:
443                 BT_ERR("Unknown role");
444                 return BLUETOOTH_ERROR_INTERNAL;
445         }
446
447         if (result == BLUETOOTH_ERROR_NONE) {
448                 /*
449                  *      This logic is added for dual HF mode issue.
450                  */
451                 node = g_list_first(g_connected_list);
452                 while (node != NULL) {
453                         bt_connected_headset_data_t *connected_device = node->data;
454
455                         if (g_strcmp0(connected_device->device_address, addr) == 0) {
456                                 BT_DBG("Connection type update");
457                                 type = connected_device->type;
458                                 break;
459                         }
460                         node = g_list_next(node);
461                 }
462
463                 /* Update device status in connected list */
464                 _bt_add_headset_to_list(type, BT_STATE_DISCONNECTING, addr);
465         } else {
466                 BT_ERR("Profile [%d] connect failed!!", type);
467         }
468         BT_INFO("-");
469         return result;
470 }
471
472 int _bt_audio_initialize(bt_service_module_t module)
473 {
474         oal_status_t status = OAL_STATUS_SUCCESS;
475         int ret  = BLUETOOTH_ERROR_NONE;
476
477         BT_INFO("_bt_audio_initialize: Module [%d]", module);
478
479         switch (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;
485                 }
486                 /* Register Audio module event handler */
487                 _bt_service_register_event_handler_callback(module, _bt_a2dp_source_event_handler);
488                 break;
489         }
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;
495                 }
496                 /* Register Audio sink module event handler */
497                 _bt_service_register_event_handler_callback(module, _bt_a2dp_sink_event_handler);
498                 break;
499         }
500         case BT_HFP_MODULE: {
501                 return BLUETOOTH_ERROR_NOT_SUPPORT;
502         }
503         case BT_AG_MODULE: {
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;
508                 }
509                 /* Register Audio module event handler */
510                 _bt_service_register_event_handler_callback(module, _bt_hf_event_handler);
511                 break;
512         }
513         case BT_AUDIO_ALL_MODULE: {
514                 /* Register Audio module event handler */
515                 _bt_service_register_event_handler_callback(module, __bt_audio_event_handler);
516                 break;
517         }
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;
523                 }
524                 /* Register Avrcp Controller event handler */
525                 _bt_service_register_event_handler_callback(module, _bt_avrcp_ctrl_event_handler);
526                 break;
527         }
528         case BT_AVRCP_MODULE: {
529                 /* Initialize AVRCP Target */
530                 ret = _bt_service_avrcp_enable();
531                 break;
532         }
533         default:
534                 BT_ERR("Not Supported: Module [%d]", module);
535                 return BLUETOOTH_ERROR_NOT_SUPPORT;
536         }
537
538         BT_INFO("Bluetooth audio interface initialised");
539         return ret;
540 }
541
542 static void __bt_audio_event_handler(int oal_event, gpointer event_data)
543 {
544         BT_INFO("+");
545
546         switch (oal_event) {
547         case OAL_EVENT_ADAPTER_DISABLED: {
548                 BT_INFO("Adapter Disabled..cleanup resources");
549                 __bt_audio_cleanup_resources();
550                 break;
551         }
552         case OAL_EVENT_ADAPTER_ENABLED: {
553                 /*TODO Currently not handled */
554                 BT_INFO("Adapter Enabled..");
555                 break;
556         }
557         default:
558                 BT_ERR("Unknown event..[%d]", oal_event);
559         }
560         BT_INFO("-");
561 }
562
563 static void __bt_audio_cleanup_resources(void)
564 {
565         GList *node;
566         GSList *l;
567         bt_pending_audio_conn_t *info = NULL;
568         BT_INFO("+");
569
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);
577         }
578
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;
582
583                 if (info) {
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);
587                         g_free(info);
588                         info = NULL;
589                 }
590         }
591
592         BT_INFO("-");
593 }
594
595 static int __bt_process_audio_connect_all(bt_pending_audio_conn_t *info)
596 {
597         bluetooth_device_address_t device_address;
598         int result = BLUETOOTH_ERROR_NONE;
599         int type = BT_AUDIO_ALL;
600         BT_INFO("+");
601
602         _bt_convert_addr_string_to_type(device_address.addr, info->address);
603
604         /* Attempt profile level connection */
605         if (info->is_hfp_supported) {
606                 type = BT_AUDIO_HSP;
607                 result = _bt_connect_remote_hfp(&device_address);
608
609                 if (info->is_a2dp_sink_supported)
610                         BT_INFO("A2DP is supported by [%s]", info->address);
611                 else
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);
619         }
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);
624         } else {
625                 BT_ERR("Profile connect failed!!");
626         }
627         BT_INFO("-");
628         return result;
629 }
630
631 bt_pending_audio_conn_t* _bt_get_pending_audio_conn_info(char *address)
632 {
633         BT_INFO("+");
634         bt_pending_audio_conn_t *info = NULL;
635         GSList *l;
636
637         for (l = pending_audio_conn_list; l != NULL; l = g_slist_next(l)) {
638                 info = (bt_pending_audio_conn_t*)l->data;
639
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);
642                         return info;
643                 }
644         }
645         BT_INFO("Pending data not found!!");
646         return NULL;
647 }
648
649 static void __bt_reply_pending_audio_connect_req(char *address, int result)
650 {
651         GArray *out_param;
652         invocation_info_t *req_info;
653
654         BT_DBG("+");
655         ret_if(NULL == address);
656
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);
660
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);
664
665         g_array_free(out_param, TRUE);
666         g_free(req_info->user_data);
667         _bt_free_info_from_invocation_list(req_info);
668
669         BT_DBG("-");
670 }
671
672 void _bt_cleanup_audio_pending_conn_info_and_reply_pending_req(bt_pending_audio_conn_t *info, int result)
673 {
674         ret_if(NULL == info);
675
676         /* Reply to async request for Audio connect, if any */
677         __bt_reply_pending_audio_connect_req(info->address, result);
678
679         /* Remove pending audio conn info */
680         pending_audio_conn_list = g_slist_remove(pending_audio_conn_list, info);
681         g_free(info->address);
682         g_free(info);
683 }
684
685 gboolean _bt_audio_check_pending_connection(char *address)
686 {
687         bt_pending_audio_conn_t *data = NULL;
688         bluetooth_device_address_t device_address;
689         BT_INFO("+");
690
691         data = _bt_get_pending_audio_conn_info(address);
692
693         if (data) {
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);
700                         } else {
701                                 BT_INFO("A2DP connect triggered successfully for [%s]", data->address);
702                         }
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);
708                         } else {
709                                 BT_INFO("A2DP sink connect triggered successfully for [%s]", data->address);
710                         }
711                 }
712                 return TRUE;
713         } else {
714                 BT_INFO("A2DP Connection is not pending..");
715         }
716         BT_INFO("-");
717         return FALSE;
718 }
719
720 void _bt_audio_check_pending_disconnection(char *address, int type)
721 {
722         int ret = BLUETOOTH_ERROR_NONE;
723         bluetooth_device_address_t device_address;
724         BT_INFO("+");
725
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);
730
731                 if (ret != BLUETOOTH_ERROR_NONE)
732                         BT_ERR("Disconnecting service [%d] with device [%s] failed!!", type, address);
733         } else {
734                 BT_INFO("Service [%d] is Not connected with device [%s],..", type, address);
735         }
736         BT_INFO("-");
737 }
738
739 int __bt_handle_audio_all_connect(bt_pending_audio_conn_t *info)
740 {
741         bt_remote_dev_info_t *rem_info;
742         int ret;
743         int indx;
744
745         BT_DBG("+");
746
747         retv_if(NULL == info, BLUETOOTH_ERROR_INTERNAL);
748         retv_if(NULL == info->address, BLUETOOTH_ERROR_INTERNAL);
749
750         rem_info = _bt_service_get_remote_dev_info(info->address);
751         if (!rem_info) {
752                 BT_ERR("_bt_service_get_remote_dev_info returned NULL");
753                 ret = BLUETOOTH_ERROR_NOT_PAIRED;
754                 goto fail;
755         }
756
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;
768                 }
769         }
770
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;
776                 goto fail;
777         }
778
779         BT_INFO("AUDIO_CONNECT_ALL request for [%s]", info->address);
780         info->search_status = SERVICE_SEARCH_DONE;
781
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;
789
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...");
794                 goto fail;
795         }
796
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...");
800                 goto fail;
801         }
802
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;
810                         goto fail;
811                 }
812
813                 ret = __bt_process_audio_connect_all(info);
814                 if (ret != BLUETOOTH_ERROR_NONE) {
815                         BT_ERR("__bt_process_audio_connect_all failed");
816                         goto fail;
817                 }
818
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);
824                 } else {
825                         BT_INFO("[%s] Supports one profile only", info->address);
826                         /*
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.
829                          */
830                         g_free(info->address);
831                         g_free(info);
832                 }
833         } else if (ret == BLUETOOTH_ERROR_NONE) {
834                 BT_INFO("Waiting for disconnection...");
835                 /*
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.
838                  */
839                 g_free(info->address);
840                 g_free(info);
841         }
842
843         BT_DBG("-");
844         return BLUETOOTH_ERROR_NONE;
845
846 fail:
847         BT_ERR("Audio connect all failed");
848         g_free(info->address);
849         g_free(info);
850         return ret;
851 }
852
853 static gboolean __audio_get_bonded_info(gpointer data)
854 {
855         bt_pending_audio_conn_t *info = data;
856         char address[BT_ADDRESS_STRING_SIZE];
857
858         BT_DBG("+");
859
860         retv_if(NULL == info, FALSE);
861         retv_if(NULL == info->address, FALSE);
862
863         if ((info->bonded_info_retries > 0) && !_bt_is_bonded_devices_retrived()) {
864                 info->bonded_info_retries--;
865                 return TRUE;
866         }
867
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);
872                 g_free(info);
873                 goto failed;
874         }
875
876         if (BLUETOOTH_ERROR_NONE != __bt_handle_audio_all_connect(info)) {
877                 BT_ERR("__bt_handle_audio_all_connect failed");
878                 goto failed;
879         }
880
881         BT_DBG("-");
882         return FALSE;
883 failed:
884         __bt_reply_pending_audio_connect_req(address, BLUETOOTH_ERROR_INTERNAL);
885         return FALSE;
886 }
887
888 int _bt_audio_connect(int type, bluetooth_device_address_t *device_address)
889 {
890         int ret = BLUETOOTH_ERROR_NONE;
891         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
892         bt_pending_audio_conn_t *info = NULL;
893
894         BT_INFO("+");
895
896         BT_CHECK_PARAMETER(device_address, return);
897
898         _bt_convert_addr_type_to_string(address, device_address->addr);
899         BT_INFO("Adderss: [%s], request type: [%d]", address, type);
900
901         /*
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.
904          */
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;
909                 }
910
911                 info = g_malloc0(sizeof(bt_pending_audio_conn_t));
912                 info->search_status = SERVICE_SEARCH_STARTED;
913                 info->address = g_strdup(address);
914                 info->type = type;
915                 info->conn_req_type = type;
916
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;
923                 }
924
925                 return __bt_handle_audio_all_connect(info);
926         }
927
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;
933         }
934
935         if (ret == BLUETOOTH_ERROR_IN_PROGRESS) {
936                 BT_ERR("Failed with BLUETOOTH_ERROR_IN_PROGRESS");
937                 return BLUETOOTH_ERROR_IN_PROGRESS;
938         }
939
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);
946                         return ret;
947                 }
948         }
949
950         return BLUETOOTH_ERROR_NONE;
951 }
952
953 int _bt_audio_disconnect(int type, bluetooth_device_address_t *device_address)
954 {
955         int ret = BLUETOOTH_ERROR_NONE;
956         int value = BLUETOOTH_ERROR_NONE;
957         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
958
959         _bt_convert_addr_type_to_string(address, device_address->addr);
960         BT_INFO("Audio Diconnect Request: type[%d] address [%s]", type, address);
961
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)) {
966                         type = BT_AUDIO_HSP;
967                 } else {
968                         BT_ERR("No audio service connected");
969                         return BLUETOOTH_ERROR_NOT_CONNECTED;
970                 }
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)) {
975                         type = BT_AUDIO_HSP;
976                 } else {
977                         BT_ERR("No audio service connected");
978                         return BLUETOOTH_ERROR_NOT_CONNECTED;
979                 }
980         }
981
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;
986         }
987
988         ret = __bt_process_audio_profile_disconnect(type, device_address);
989         return ret;
990 }
991
992 gboolean _bt_is_service_connected(char* address, int type)
993 {
994         GList *node;
995         node = g_list_first(g_connected_list);
996         while (node != NULL) {
997                 bt_connected_headset_data_t *conn_device = node->data;
998
999                 if ((g_strcmp0(conn_device->device_address, address) == 0) &&
1000                                 (conn_device->type & type)) {
1001                         BT_INFO("Service connected");
1002                         return TRUE;
1003                 }
1004
1005                 node = g_list_next(node);
1006         }
1007         BT_INFO("Service not connected");
1008         return FALSE;
1009 }
1010
1011 void _bt_remove_headset_from_list(int type, const char *address)
1012 {
1013         GList *node;
1014
1015         BT_DBG("_bt_remove_headset_from_list \n");
1016
1017         node = g_list_first(g_connected_list);
1018         while (node != NULL) {
1019                 bt_connected_headset_data_t *connected_device = node->data;
1020
1021                 if (g_strcmp0(connected_device->device_address, address) != 0) {
1022                         node = g_list_next(node);
1023                         continue;
1024                 }
1025
1026                 BT_DBG("Address match \n");
1027
1028                 BT_DBG("Connection type = %x\n", connected_device->type);
1029
1030                 switch (type) {
1031                 case BT_AUDIO_A2DP:
1032                         if (connected_device->type & BT_AUDIO_A2DP)
1033                                 connected_device->type &= ~(BT_AUDIO_A2DP);
1034                         break;
1035                 case BT_AUDIO_HSP:
1036                         if (connected_device->type & BT_AUDIO_HSP)
1037                                 connected_device->type &= ~(BT_AUDIO_HSP);
1038                         break;
1039                 case BT_AUDIO_ALL:
1040                         if (connected_device->type & BT_AUDIO_ALL)
1041                                 connected_device->type &= ~(BT_AUDIO_ALL);
1042                         break;
1043                 case BT_AVRCP_TARGET:
1044                         if (connected_device->type & BT_AVRCP_TARGET)
1045                                 connected_device->type &= ~(BT_AVRCP_TARGET);
1046                         break;
1047                 case BT_AVRCP:
1048                         if (connected_device->type & BT_AVRCP)
1049                                 connected_device->type &= ~(BT_AVRCP);
1050                         break;
1051                 case BT_AUDIO_A2DP_SOURCE:
1052                         if (connected_device->type & BT_AUDIO_A2DP_SOURCE)
1053                                 connected_device->type &= ~(BT_AUDIO_A2DP_SOURCE);
1054                         break;
1055                 default:
1056                         break;
1057                 }
1058
1059                 BT_DBG("Connection type = %x\n", connected_device->type);
1060
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);
1065                 } else {
1066                         connected_device->device_state = BT_STATE_CONNECTED;
1067                 }
1068
1069                 node = g_list_next(node);
1070         }
1071
1072         /*
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.
1075          */
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);
1079 }
1080
1081 static void __handle_pending_requests(int result, int service_function, void *user_data, unsigned int size)
1082 {
1083         GSList *l;
1084         GArray *out_param;
1085         invocation_info_t *req_info;
1086
1087         BT_DBG("+");
1088
1089         /* Get method invocation context */
1090         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1091                 req_info = l->data;
1092
1093                 if (req_info == NULL || req_info->service_function != service_function)
1094                         continue;
1095
1096                 /* Create out param */
1097                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1098
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);
1104                         return;
1105                 default:
1106                         BT_ERR("Unknown service function[%d]", service_function);
1107                         return;
1108                 }
1109         }
1110
1111         BT_DBG("-");
1112 }
1113
1114 static gboolean __handle_pending_audio_select_role(gpointer data)
1115 {
1116         int ret;
1117         oal_status_t status;
1118
1119         BT_DBG("+");
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;
1126                         goto done;
1127                 }
1128
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;
1134                         goto done;
1135                 }
1136
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");
1141                         goto done;
1142                 }
1143
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");
1148                         goto done;
1149                 }
1150         } else {
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;
1156                         goto done;
1157                 }
1158
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");
1163                         goto done;
1164                 }
1165
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");
1170                         goto done;
1171                 }
1172
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");
1177                         goto done;
1178                 }
1179         }
1180
1181 done:
1182         if (ret == BLUETOOTH_ERROR_NONE)
1183                 _bt_audio_set_current_role(pending_audio_role);
1184
1185         __handle_pending_requests(ret, BT_AUDIO_SELECT_ROLE, NULL, 0);
1186         pending_audio_role = -1;
1187         BT_DBG("-");
1188         return FALSE;
1189 }
1190
1191 int _bt_audio_select_role(bluetooth_audio_role_t role)
1192 {
1193         int result = BLUETOOTH_ERROR_NONE;
1194         char address[BT_ADDRESS_STRING_SIZE];
1195         bluetooth_device_address_t dev_addr;
1196
1197         retv_if(BLUETOOTH_A2DP_SINK < role, BLUETOOTH_ERROR_INVALID_PARAM);
1198         retv_if(pending_audio_role >= 0, BLUETOOTH_ERROR_IN_PROGRESS);
1199
1200         BT_DBG("+");
1201
1202         if (curr_audio_role == role) {
1203                 BT_ERR("Desired audio role already enabled, return");
1204                 return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
1205         }
1206
1207         pending_audio_role = role;
1208
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;
1216                         }
1217                 } else {
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;
1221                 }
1222         } else {
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;
1229                         }
1230                 } else {
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;
1234                 }
1235         }
1236
1237         BT_DBG("-");
1238         return result;
1239 }
1240
1241 void _bt_audio_set_current_role(bluetooth_audio_role_t role)
1242 {
1243         BT_INFO("role: %s", (role == BLUETOOTH_A2DP_SINK) ? "AUDIO_SINK" : "AUDIO_SOURCE");
1244         curr_audio_role = role;
1245 }