Fix progress percentage on DUT while waiting for remote authorization
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-audio.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <gio/gio.h>
19 #include <glib.h>
20 #include <dlog.h>
21 #include <string.h>
22 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
23 #include <vconf.h>
24 #endif
25 #include <syspopup_caller.h>
26
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
29
30 #include "bt-service-audio.h"
31 #include "bt-service-adapter.h"
32 #include "bt-service-common.h"
33 #include "bt-service-device.h"
34 #include "bt-service-event.h"
35 #include "bt-service-util.h"
36
37 #include "bt-service-headset-connection.h"
38
39 #ifdef TIZEN_SUPPORT_DUAL_HF
40 #ifdef TIZEN_PROFILE_WEARABLE
41 #define VCONF_KEY_BT_HOST_BT_MAC_ADDR "db/wms/host_bt_mac"
42 #endif
43 #endif
44
45 typedef struct {
46         unsigned int type;
47         int device_state;
48         char device_address[BT_ADDRESS_STRING_SIZE + 1];
49 } bt_connected_headset_data_t;
50
51 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
52 typedef struct {
53         int alarm_id;
54         int retry_count;
55 } bt_auto_connect_timer_t;
56
57 bt_auto_connect_timer_t auto_connect_timer = {0, };
58 #endif
59
60 static GList *g_connected_list;
61
62 static bt_headset_wait_t *g_wait_data;
63
64 static bt_audio_function_data_t *pdata;
65
66 static void __bt_remove_device_from_wait_list();
67
68 static void __bt_free_wait_data();
69
70 static gboolean __bt_device_support_uuid(char *remote_address,
71                                 bt_audio_type_t type);
72
73 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
74 static int __bt_auto_connect_alarm_cb(alarm_id_t alarm_id, void* user_param);
75 #endif
76
77 static void __bt_hf_request_cb(GDBusProxy *proxy, GAsyncResult *res,
78                                     gpointer user_data)
79 {
80         GError *g_error = NULL;
81         GVariant *out_param1 = NULL;
82         GVariant *reply = NULL;
83         int result = BLUETOOTH_ERROR_NONE;
84         bt_function_data_t *func_data;
85         request_info_t *req_info;
86
87         reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
88         g_object_unref(proxy);
89
90         func_data = user_data;
91
92         if (func_data == NULL) {
93                 /* Send reply */
94                 BT_ERR("func_data == NULL");
95                 goto done;
96         }
97
98         req_info = _bt_get_request_info(func_data->req_id);
99         if (req_info == NULL) {
100                 BT_ERR("req_info == NULL");
101                 goto done;
102         }
103
104         if (reply == NULL) {
105                 BT_ERR("HF Connect Dbus Call Error");
106                 result = _bt_convert_gerror(g_error);
107                 if (g_error) {
108                         BT_ERR("Error: %s\n", g_error->message);
109                         g_clear_error(&g_error);
110                 }
111         } else {
112                 g_variant_unref(reply);
113         }
114
115         if (req_info->context == NULL)
116                 goto done;
117
118         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
119                 func_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
120
121         g_dbus_method_invocation_return_value(req_info->context,
122                         g_variant_new("(iv)", result, out_param1));
123
124         _bt_delete_request_list(req_info->req_id);
125
126 done:
127         if (func_data) {
128                 g_free(func_data->address);
129                 g_free(func_data);
130         }
131 }
132
133 void _bt_audio_check_pending_connect()
134 {
135         BT_DBG("+");
136         bluetooth_device_address_t device_address;
137
138         if (pdata == NULL)
139                 return;
140
141         if (pdata->pending == BT_PENDING_CONNECT) {
142
143                 _bt_convert_addr_string_to_type(device_address.addr,
144                                 pdata->address);
145                 _bt_audio_connect(pdata->req_id,
146                                 BT_AUDIO_A2DP,
147                                 &device_address,
148                                 NULL);
149
150                 g_free(pdata->address);
151                 g_free(pdata);
152                 pdata = NULL;
153         }
154
155         BT_DBG("-");
156         return;
157 }
158
159 static void __bt_audio_request_cb(GDBusProxy *proxy, GAsyncResult *res,
160                                     gpointer user_data)
161 {
162         GError *g_error = NULL;
163         GVariant *out_param1 = NULL;
164         GVariant *reply = NULL;
165         int result = BLUETOOTH_ERROR_NONE;
166         bt_audio_function_data_t *func_data;
167         request_info_t *req_info;
168
169         reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
170         g_object_unref(proxy);
171         g_variant_unref(reply);
172
173         func_data = user_data;
174
175         if (func_data == NULL) {
176                 /* Send reply */
177                 BT_ERR("func_data == NULL");
178                 goto done;
179         }
180
181         if (func_data->pending != BT_PENDING_NONE && g_error == NULL) {
182                 bluetooth_device_address_t device_address;
183                 _bt_convert_addr_string_to_type(device_address.addr,
184                                         func_data->address);
185
186                 if (func_data->pending == BT_PENDING_CONNECT) {
187
188                         if (__bt_device_support_uuid(func_data->address,
189                                                         BT_AUDIO_A2DP)) {
190
191                                 pdata = g_new0(bt_audio_function_data_t, 1);
192                                 pdata->req_id = func_data->req_id;
193                                 pdata->address = strdup(func_data->address);
194                                 pdata->pending = func_data->pending;
195                         } else
196                                 goto check_req_info;
197
198                 } else {
199                         if (_bt_is_service_connected(func_data->address
200                                                         , BT_AUDIO_HSP)) {
201                                 _bt_audio_disconnect(func_data->req_id,
202                                         BT_AUDIO_HSP, &device_address, NULL);
203                         } else
204                                 goto check_req_info;
205                 }
206
207                 goto done;
208         }
209
210 check_req_info:
211         req_info = _bt_get_request_info(func_data->req_id);
212         if (req_info == NULL) {
213                 BT_ERR("req_info == NULL");
214                 goto done;
215         }
216
217         if (g_error == NULL)
218                 goto dbus_return;
219
220         BT_ERR("Audio Connect/Disconnect Dbus Call Error: %s\n", g_error->message);
221
222         result = BLUETOOTH_ERROR_INTERNAL;
223         /* If there is error then we need to set local initiated connection as false
224           * If don't do this then when headset initiate connection for HFP then we don't
225           * initiate connection for A2dp to headset as this flag was set to true in first
226           * connection failure attempt and not set in error case.*/
227         _bt_headset_set_local_connection(FALSE);
228
229         /* Remove the device from the list */
230         _bt_remove_headset_from_list(func_data->type, func_data->address);
231
232         /* Error, check if any waiting device is there */
233         if (g_wait_data == NULL)
234                 goto dbus_return;
235
236         if (g_strcmp0(g_wait_data->address, func_data->address) != 0) {
237                 bluetooth_device_address_t device_address;
238                 _bt_convert_addr_string_to_type(device_address.addr,
239                                 g_wait_data->address);
240                 _bt_audio_connect(g_wait_data->req_id, g_wait_data->type,
241                                 &device_address, NULL);
242         }
243
244         /* Event will be sent by the event reciever */
245 dbus_return:
246         if (req_info->context == NULL) {
247                 BT_DBG("req_info->context is NULL");
248                 goto done;
249         }
250
251         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
252                 func_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
253
254         g_dbus_method_invocation_return_value(req_info->context,
255                         g_variant_new("(iv)", result, out_param1));
256
257         _bt_delete_request_list(req_info->req_id);
258 done:
259         g_clear_error(&g_error);
260
261         if (func_data) {
262                 g_free(func_data->address);
263                 g_free(func_data);
264         }
265 }
266
267 static void __bt_free_wait_data()
268 {
269         if (g_wait_data != NULL) {
270                 g_free(g_wait_data->address);
271                 g_free(g_wait_data);
272                 g_wait_data = NULL;
273         }
274 }
275
276 static void __bt_remove_device_from_wait_list()
277 {
278         /* Before deleting the request update the UI */
279         GVariant *out_param_1 = NULL;
280         int result = BLUETOOTH_ERROR_INTERNAL;
281         request_info_t *req_info;
282
283         req_info = _bt_get_request_info(g_wait_data->req_id);
284         if (req_info == NULL) {
285                 BT_ERR("req_info == NULL");
286                 return;
287         }
288
289         out_param_1 = g_variant_new_from_data((const GVariantType *)"ay",
290                 g_wait_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
291
292         g_dbus_method_invocation_return_value(req_info->context,
293                         g_variant_new("(iv)", result, out_param_1));
294
295         _bt_delete_request_list(g_wait_data->req_id);
296 }
297
298 static void __bt_set_headset_disconnection_type(const char *address)
299 {
300         bt_connected_headset_data_t *connected_device;
301         GList *node;
302
303         node = g_list_first(g_connected_list);
304         while (node != NULL) {
305                 connected_device = node->data;
306                 if (g_strcmp0(connected_device->device_address, address) == 0) {
307                         g_wait_data->disconnection_type = connected_device->type;
308                         return;
309                 }
310                 node = g_list_next(node);
311         }
312 }
313
314 gboolean _bt_is_headset_type_connected(int type, char *address)
315 {
316         GList *node;
317
318         node = g_list_first(g_connected_list);
319         while (node != NULL) {
320                 bt_connected_headset_data_t *connected_device = node->data;
321
322                 if (connected_device->type & type) {
323                         if (address != NULL)
324                                 g_strlcpy(address, connected_device->device_address,
325                                                 BT_ADDRESS_STRING_SIZE + 1);
326                         return TRUE;
327                 }
328
329                 node = g_list_next(node);
330         }
331         return FALSE;
332 }
333
334 #ifdef TIZEN_SUPPORT_DUAL_HF
335 gboolean __bt_is_companion_device(const char *addr)
336 {
337 #ifdef TIZEN_PROFILE_WEARABLE
338         char *host_device_address = NULL;
339         host_device_address = vconf_get_str(VCONF_KEY_BT_HOST_BT_MAC_ADDR);
340
341         if (!host_device_address) {
342                 BT_INFO("Failed to get a companion device address");
343                 return FALSE;
344         }
345
346         if (g_strcmp0(host_device_address, addr) == 0) {
347                 BT_INFO("Found companion device");
348                 free(host_device_address);
349                 return TRUE;
350         }
351
352         free(host_device_address);
353         return FALSE;
354 #else
355         /* TODO : Need to add companion device check condition for Phone models */
356         return FALSE;
357 #endif
358 }
359 #endif
360
361 static int __bt_is_headset_connected(int type, int req_id,
362                                 const char *address)
363 {
364         gboolean connected = FALSE;
365         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
366         bluetooth_device_address_t device_address;
367         bt_connected_headset_data_t *connected_device = NULL;
368 #ifdef TIZEN_SUPPORT_DUAL_HF
369         gboolean is_companion_device = FALSE;
370 #endif
371
372         /* Check if any other headset is connected */
373         GList *node = NULL;;
374
375         node = g_list_first(g_connected_list);
376         while (node != NULL) {
377                 connected_device = node->data;
378                 if ((connected_device->type & type)) {
379                         g_strlcpy(connected_address, connected_device->device_address,
380                                         BT_ADDRESS_STRING_SIZE + 1);
381 #ifdef TIZEN_SUPPORT_DUAL_HF
382                         is_companion_device = __bt_is_companion_device(connected_address);
383                         BT_INFO(" is_companion_device[%d]", is_companion_device);
384
385                         if (!is_companion_device) {
386                                 connected = TRUE;
387                                 break;
388                         }
389 #else
390                         connected = TRUE;
391                         break;
392 #endif
393                 }
394                 node = g_list_next(node);
395         }
396
397         if (!connected)
398                 return BLUETOOTH_ERROR_NOT_CONNECTED;
399
400         BT_DBG("connected headset %s", connected_address);
401
402         if (g_strcmp0(connected_address, address) == 0)
403                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
404 #ifdef TIZEN_SUPPORT_DUAL_HF
405         else if (TRUE == __bt_is_companion_device(address))
406                 return BLUETOOTH_ERROR_NOT_CONNECTED;
407 #endif
408
409         /* Convert BD adress from string type */
410         _bt_convert_addr_string_to_type(device_address.addr, connected_address);
411         int value = BLUETOOTH_ERROR_NONE;
412         value = _bt_audio_disconnect(0, connected_device->type & type, &device_address, NULL);
413
414         /* If already one device is waiting, remove current waiting device and add new */
415         if (value == BLUETOOTH_ERROR_NONE) {
416                 if (g_wait_data != NULL) {
417                         if (g_strcmp0(g_wait_data->address, address) != 0) {
418                                 __bt_remove_device_from_wait_list();
419                                 __bt_free_wait_data();
420                         }
421                 }
422
423                 if (g_wait_data == NULL) {
424                         g_wait_data = g_malloc0(sizeof(bt_headset_wait_t));
425                         g_wait_data->address = g_strdup(address);
426                         g_wait_data->req_id = req_id;
427                         g_wait_data->type = type;
428                         g_wait_data->ag_flag = FALSE;
429
430                         /* Set disconnection type */
431                         __bt_set_headset_disconnection_type(connected_address);
432                 }
433         }
434
435         return value;
436 }
437
438 static int __bt_is_headset_connecting(int type)
439 {
440         bt_connected_headset_data_t *connected_device = NULL;
441
442         /* Check if any other headset is connected */
443         GList *node = NULL;
444
445         node = g_list_first(g_connected_list);
446         while (node != NULL) {
447                 connected_device = node->data;
448                 if (connected_device->device_state == BT_STATE_CONNECTING)
449                         return BLUETOOTH_ERROR_CONNECTION_BUSY;
450                 node = g_list_next(node);
451         }
452
453         return BLUETOOTH_ERROR_NONE;
454 }
455
456 int __bt_is_headset_disconnecting(int type)
457 {
458         bt_connected_headset_data_t *connected_device = NULL;
459
460         /* Check if any other headset is connected */
461         GList *node = NULL;
462
463         node = g_list_first(g_connected_list);
464         while (node != NULL) {
465                 connected_device = node->data;
466                 if (connected_device->device_state == BT_STATE_DISCONNECTING)
467                         return BLUETOOTH_ERROR_CONNECTION_BUSY;
468
469                 node = g_list_next(node);
470         }
471
472         return BLUETOOTH_ERROR_NONE;
473 }
474
475 void _bt_set_audio_wait_data_flag(gboolean flag)
476 {
477         BT_DBG("_bt_set_audio_wait_data_flag \n");
478         g_wait_data->ag_flag = flag;
479 }
480
481 bt_headset_wait_t *_bt_get_audio_wait_data(void)
482 {
483         BT_DBG("_bt_get_audio_wait_data \n");
484         return g_wait_data;
485 }
486
487 void _bt_rel_wait_data(void)
488 {
489         BT_DBG("_bt_rel_wait_data \n");
490         __bt_free_wait_data();
491 }
492
493 void _bt_add_headset_to_list(int type, int status, const char *address)
494 {
495         bt_connected_headset_data_t *connected_device;
496         bt_connected_headset_data_t *device;
497         GList *node;
498
499         BT_DBG("_bt_add_headset_to_list \n");
500
501         node = g_list_first(g_connected_list);
502         while (node != NULL) {
503                 device = (bt_connected_headset_data_t *)node->data;
504
505                 if (g_strcmp0(device->device_address, address) == 0) {
506                         BT_DBG("Address match, update connection type \n");
507                         if (status == BT_STATE_CONNECTED)
508                                 device->type |= type;
509                         device->device_state = status;
510                         return;
511                 }
512                 node = g_list_next(node);
513         }
514
515         connected_device = g_malloc0(sizeof(bt_connected_headset_data_t));
516         connected_device->device_state = status;
517         if ((status == BT_STATE_CONNECTED) || (status == BT_STATE_CONNECTING))
518                 connected_device->type |= type;
519         g_strlcpy(connected_device->device_address, address,
520                         sizeof(connected_device->device_address));
521         g_connected_list = g_list_append(g_connected_list, connected_device);
522 }
523
524 int _bt_get_device_state_from_list(int type, const char *address)
525 {
526         GList *node;
527         bt_connected_headset_data_t *device;
528
529         BT_DBG("+");
530         node = g_list_first(g_connected_list);
531         while (node != NULL) {
532                 device = (bt_connected_headset_data_t *)node->data;
533                 if (g_strcmp0(device->device_address, address) == 0) {
534                         BT_DBG("Device found");
535                         return device->device_state;
536                 }
537                 node = g_list_next(node);
538         }
539
540         BT_DBG("Device not found");
541         return BLUETOOTH_ERROR_INTERNAL;
542 }
543
544 void _bt_remove_headset_from_list(int type, const char *address)
545 {
546         GList *node;
547
548         BT_DBG("_bt_remove_headset_from_list \n");
549
550         node = g_list_first(g_connected_list);
551         while (node != NULL) {
552                 bt_connected_headset_data_t *connected_device = node->data;
553
554                 if (g_strcmp0(connected_device->device_address, address) != 0) {
555                         node = g_list_next(node);
556                         continue;
557                 }
558
559                 BT_DBG("Address match \n");
560
561                 BT_DBG("Connection type = %x\n", connected_device->type);
562
563                 switch (type) {
564                 case BT_AUDIO_A2DP:
565                         if (connected_device->type & BT_AUDIO_A2DP)
566                                 connected_device->type &= ~(BT_AUDIO_A2DP);
567                         break;
568                 case BT_AUDIO_HSP:
569                         if (connected_device->type & BT_AUDIO_HSP)
570                                 connected_device->type &= ~(BT_AUDIO_HSP);
571                         break;
572                 case BT_AUDIO_ALL:
573                         if (connected_device->type & BT_AUDIO_ALL)
574                                 connected_device->type &= ~(BT_AUDIO_ALL);
575                         break;
576                 case BT_AVRCP:
577                         if (connected_device->type & BT_AVRCP)
578                                 connected_device->type &= ~(BT_AVRCP);
579                         break;
580                 case BT_AUDIO_A2DP_SOURCE:
581                         if (connected_device->type & BT_AUDIO_A2DP_SOURCE)
582                                 connected_device->type &= ~(BT_AUDIO_A2DP_SOURCE);
583                 }
584
585                 BT_DBG("Connection type = %x\n", connected_device->type);
586
587                 if (connected_device->type == 0x00) {
588                         g_connected_list = g_list_remove(g_connected_list, connected_device);
589                         g_free(connected_device);
590                 } else {
591                         connected_device->device_state = BT_STATE_CONNECTED;
592                 }
593
594                 node = g_list_next(node);
595         }
596 }
597
598 static gboolean __bt_device_support_uuid(char *remote_address,
599                                 bt_audio_type_t type)
600 {
601         GArray *dev_list = NULL;
602         int size;
603         int i;
604         int j;
605         bluetooth_device_info_t info;
606         char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
607         gboolean ret = FALSE;
608
609         BT_DBG("+");
610
611         dev_list = g_array_new(FALSE, FALSE, sizeof(gchar));
612
613         _bt_get_bonded_devices(&dev_list);
614         size = (dev_list->len) / sizeof(bluetooth_device_info_t);
615
616         for (i = 0; i < size; i++) {
617                 info = g_array_index(dev_list, bluetooth_device_info_t, i);
618                 _bt_convert_addr_type_to_string(bond_address,
619                                 info.device_address.addr);
620                 if (strcmp(bond_address, remote_address) != 0)
621                         continue;
622
623                 BT_INFO("Device address Matched");
624                 j = 0;
625                 while (j != info.service_index) {
626                         if (type == BT_AUDIO_HSP) {
627                                 if (strcmp(info.uuids[j], HFP_HS_UUID) == 0) {
628                                         BT_INFO("HFP HS UUID exists");
629                                         ret = TRUE;
630                                         goto end;
631                                 }
632                         } else if (type == BT_AUDIO_A2DP) {
633                                 if (strcmp(info.uuids[j], A2DP_SINK_UUID) == 0) {
634                                         BT_INFO("A2DP SINK UUID exists");
635                                         ret = TRUE;
636                                         goto end;
637                                 }
638                         }
639                         j++;
640                 }
641         }
642 end:
643         g_array_free(dev_list, TRUE);
644         BT_DBG("-");
645         return ret;
646 }
647
648 gboolean _bt_is_service_connected(char* address, int type)
649 {
650         GList *node;
651
652         node = g_list_first(g_connected_list);
653         while (node != NULL) {
654                 bt_connected_headset_data_t *conn_device = node->data;
655
656                 if ((g_strcmp0(conn_device->device_address, address) == 0) &&
657                         (conn_device->type & type)) {
658                                 BT_INFO("Service connected");
659                                 return TRUE;
660                 }
661
662                 node = g_list_next(node);
663         }
664         BT_INFO("Service not connected");
665         return FALSE;
666 }
667
668 int _bt_audio_connect(int request_id, int type,
669                 bluetooth_device_address_t *device_address,
670                 GArray *out_param1)
671 {
672         int result = BLUETOOTH_ERROR_NONE;
673         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
674         GDBusProxy *adapter_proxy;
675         GDBusConnection *g_conn;
676         int ret;
677         char *uuid = NULL;
678         int value = BLUETOOTH_ERROR_NONE;
679         bt_audio_function_data_t *func_data;
680         guint hfp_hs_restricted = 0x0; /* set default "allowed" */
681         guint a2dp_restricted = 0x0;
682
683         BT_CHECK_PARAMETER(device_address, return);
684
685         adapter_proxy = _bt_get_adapter_proxy();
686         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
687
688         g_conn = _bt_gdbus_get_system_gconn();
689         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
690
691         _bt_convert_addr_type_to_string(address, device_address->addr);
692
693         func_data = g_malloc0(sizeof(bt_audio_function_data_t));
694         func_data->address = g_strdup(address);
695         func_data->req_id = request_id;
696         func_data->type = type;
697         func_data->pending = BT_PENDING_NONE;
698
699         result = _bt_get_restrict_profile(device_address, RESTRICTED_PROFILE_HFP_HS, &hfp_hs_restricted);
700         if (result != BLUETOOTH_ERROR_NONE)
701                 BT_ERR("Can't get hfp_hs restriction info");
702
703         result = _bt_get_restrict_profile(device_address, RESTRICTED_PROFILE_A2DP, &a2dp_restricted);
704         if (result != BLUETOOTH_ERROR_NONE)
705                 BT_ERR("Can't get a2dp restriction info");
706
707         switch (type) {
708         case BT_AUDIO_HSP:
709                 uuid = HFP_HS_UUID;
710                 break;
711         case BT_AUDIO_A2DP:
712                 uuid = A2DP_SINK_UUID;
713                 break;
714         case BT_AVRCP:
715                 uuid = AVRCP_TARGET_UUID;
716                 break;
717         case BT_AUDIO_A2DP_SOURCE:
718                 uuid = A2DP_SOURCE_UUID;
719                 break;
720         case BT_AUDIO_ALL:
721                 if ((hfp_hs_restricted == 0x0) && (a2dp_restricted == 0x0)) { /* in case of both profiles are not restricted */
722                         if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
723                                 uuid = HFP_HS_UUID;
724                                 func_data->pending = BT_PENDING_CONNECT;
725                                 type = BT_AUDIO_HSP;
726                         } else if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
727                                 uuid = A2DP_SINK_UUID;
728                                 type = BT_AUDIO_A2DP;
729                         } else {
730                                 BT_ERR("No audio role supported");
731                                 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
732                                 goto fail;
733                         }
734                 } else if ((hfp_hs_restricted == 0x1) && (a2dp_restricted == 0x1)) { /* in case of both profiles are restricted */
735                         BT_ERR("All profiles are restricted");
736                         result = BLUETOOTH_ERROR_INTERNAL;
737                         goto fail;
738                 } else if (a2dp_restricted == 0x01) { /* in case of a2dp is restricted, only connection for hfp_hs */
739                         if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
740                                 uuid = HFP_HS_UUID;
741                                 type = BT_AUDIO_HSP;
742                         } else {
743                                 BT_ERR("HFP_HS role is not supported");
744                                 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
745                                 goto fail;
746                         }
747                 } else if (hfp_hs_restricted == 0x01) { /* in case of hfp_hs is restricted, only connection for a2dp */
748                         if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
749                                 uuid = A2DP_SINK_UUID;
750                                 type = BT_AUDIO_A2DP;
751                         } else {
752                                 BT_ERR("A2DP role is not supported");
753                                 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
754                                 goto fail;
755                         }
756                 }
757                 break;
758         case BT_AVRCP_TARGET:
759                 uuid = AVRCP_REMOTE_UUID;
760                 break;
761         default:
762                 BT_ERR("Unknown role");
763                 result = BLUETOOTH_ERROR_INTERNAL;
764                 goto fail;
765         }
766         BT_INFO("Connecting to service %s", uuid);
767
768         value = __bt_is_headset_connected(type, request_id, address);
769
770         if (value == BLUETOOTH_ERROR_ALREADY_CONNECT) {
771                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
772         } else if (value == BLUETOOTH_ERROR_NOT_CONNECTED) {
773                 value = __bt_is_headset_connecting(type);
774                 if (value != BLUETOOTH_ERROR_NONE) {
775                         result = BLUETOOTH_ERROR_IN_PROGRESS;
776                         goto fail;
777                 }
778                 _bt_headset_set_local_connection(TRUE);
779                 ret = _bt_connect_profile(address, uuid,
780                                 __bt_audio_request_cb, func_data);
781
782                 if (ret != BLUETOOTH_ERROR_NONE) {
783                         BT_ERR("_bt_connect_profile Error");
784                         _bt_headset_set_local_connection(FALSE);
785                         g_free(func_data->address);
786                         g_free(func_data);
787                         return ret;
788                 }
789
790                 /* Add data to the connected list */
791                 _bt_add_headset_to_list(type, BT_STATE_CONNECTING, address);
792         } else if (value == BLUETOOTH_ERROR_IN_PROGRESS) {
793                 return BLUETOOTH_ERROR_IN_PROGRESS;
794         }
795
796         if (out_param1)
797                 g_array_free(out_param1, TRUE);
798
799         return BLUETOOTH_ERROR_NONE;
800 fail:
801         if (out_param1 != NULL)
802                 g_array_append_vals(out_param1, address,
803                                 BT_ADDRESS_STR_LEN);
804
805         return result;
806 }
807
808 int _bt_audio_disconnect(int request_id, int type,
809                 bluetooth_device_address_t *device_address,
810                 GArray *out_param1)
811 {
812         int result = BLUETOOTH_ERROR_NONE;
813         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
814         bt_audio_function_data_t *func_data;
815         GDBusProxy *adapter_proxy;
816         GDBusConnection *g_conn;
817         GList *node;
818         int ret;
819         char *uuid;
820         int value = BLUETOOTH_ERROR_NONE;
821
822         BT_CHECK_PARAMETER(device_address, return);
823
824         adapter_proxy = _bt_get_adapter_proxy();
825         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
826
827         g_conn = _bt_gdbus_get_system_gconn();
828         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
829
830         _bt_convert_addr_type_to_string(address, device_address->addr);
831
832         func_data = g_malloc0(sizeof(bt_audio_function_data_t));
833         func_data->address = g_strdup(address);
834         func_data->req_id = request_id;
835         func_data->pending = BT_PENDING_NONE;
836         func_data->type = type;
837
838         switch (type) {
839         case BT_AUDIO_HSP:
840                 uuid = HFP_HS_UUID;
841                 break;
842         case BT_AUDIO_A2DP:
843                 uuid = A2DP_SINK_UUID;
844                 break;
845         case BT_AVRCP:
846                 uuid = AVRCP_TARGET_UUID;
847                 break;
848         case BT_AUDIO_A2DP_SOURCE:
849                 uuid = A2DP_SOURCE_UUID;
850                 break;
851         case BT_AUDIO_ALL:
852                 if (_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
853                         uuid = A2DP_SINK_UUID;
854                         func_data->pending = BT_PENDING_DISCONNECT;
855                 } else if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
856                         uuid = HFP_HS_UUID;
857                 } else {
858                         BT_ERR("No audio service connected");
859                         result = BLUETOOTH_ERROR_NOT_CONNECTED;
860                         goto fail;
861                 }
862                 break;
863         case BT_AVRCP_TARGET:
864                 uuid = AVRCP_REMOTE_UUID;
865                 break;
866         default:
867                 BT_ERR("Unknown role");
868                 result = BLUETOOTH_ERROR_INTERNAL;
869                 goto fail;
870         }
871         value = __bt_is_headset_disconnecting(type);
872         if (value != BLUETOOTH_ERROR_NONE) {
873                 BT_INFO("Disconnect in progress");
874                 result = BLUETOOTH_ERROR_IN_PROGRESS;
875                 goto fail;
876         }
877         BT_INFO("Disconnecting service %s", uuid);
878         ret = _bt_disconnect_profile(address, uuid,
879                         __bt_audio_request_cb, func_data);
880
881         if (ret != BLUETOOTH_ERROR_NONE) {
882                 BT_ERR("_bt_disconnect_profile Error");
883                 g_free(func_data->address);
884                 g_free(func_data);
885                 return ret;
886         }
887
888         /*
889          *      This logic is added for dual HF mode issue.
890          */
891         node = g_list_first(g_connected_list);
892         while (node != NULL) {
893                 bt_connected_headset_data_t *connected_device = node->data;
894
895                 if (g_strcmp0(connected_device->device_address, address) == 0) {
896                         BT_DBG("Connection type update");
897                         type = connected_device->type;
898                         break;
899                 }
900                 node = g_list_next(node);
901         }
902         _bt_add_headset_to_list(type, BT_STATE_DISCONNECTING, address);
903
904         if (out_param1)
905                 g_array_free(out_param1, TRUE);
906
907         return BLUETOOTH_ERROR_NONE;
908 fail:
909         if (out_param1 != NULL)
910                 g_array_append_vals(out_param1, address,
911                                 BT_ADDRESS_STR_LEN);
912
913         return result;
914 }
915
916 void _bt_remove_from_connected_list(const char *address)
917 {
918         bt_connected_headset_data_t *connected_device;
919         GList *node;
920
921         node = g_list_first(g_connected_list);
922         while (node != NULL) {
923                 connected_device = node->data;
924                 if (connected_device != NULL &&
925                 g_strcmp0(connected_device->device_address, address) == 0) {
926                         BT_ERR("Device is removed from the list");
927                         g_connected_list = g_list_remove(g_connected_list, connected_device);
928                         g_free(connected_device);
929                         return;
930                 }
931                 node = g_list_next(node);
932         }
933 }
934
935 int _bt_hf_connect(int request_id,
936                 bluetooth_device_address_t *device_address,
937                 GArray *out_param1)
938 {
939         int result = BLUETOOTH_ERROR_NONE;
940         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
941         bt_function_data_t *func_data;
942         GDBusProxy *adapter_proxy;
943         GDBusConnection *g_conn;
944         gboolean connected = FALSE;
945         int ret;
946         char *uuid;
947
948         BT_CHECK_PARAMETER(device_address, return);
949
950         _bt_convert_addr_type_to_string(address, device_address->addr);
951
952         adapter_proxy = _bt_get_adapter_proxy();
953         if (adapter_proxy == NULL) {
954                 result = BLUETOOTH_ERROR_INTERNAL;
955                 goto fail;
956         }
957
958         g_conn = _bt_gdbus_get_system_gconn();
959         if (g_conn == NULL) {
960                 result = BLUETOOTH_ERROR_INTERNAL;
961                 goto fail;
962         }
963
964         /* Check if HF is connected or not */
965         result = _bt_is_device_connected(device_address, BT_PROFILE_CONN_HFG, &connected);
966         if (connected == TRUE) {
967                 BT_ERR("HF is already connected");
968                 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
969                 goto fail;
970         }
971
972         func_data = g_malloc0(sizeof(bt_function_data_t));
973         func_data->address = g_strdup(address);
974         func_data->req_id = request_id;
975         uuid = g_strdup(HFP_AG_UUID);
976
977         BT_DBG("Connecting to service %s", uuid);
978
979         ret = _bt_connect_profile(address, uuid,
980                         __bt_hf_request_cb, func_data);
981
982         if (ret != BLUETOOTH_ERROR_NONE) {
983                 BT_ERR("_bt_connect_profile Error");
984                 g_free(func_data->address);
985                 g_free(func_data);
986                 g_free(uuid);
987                 return ret;
988         }
989         g_free(uuid);
990         if (out_param1)
991                 g_array_free(out_param1, TRUE);
992
993         return BLUETOOTH_ERROR_NONE;
994 fail:
995         if (out_param1 != NULL)
996                 g_array_append_vals(out_param1, address,
997                                 BT_ADDRESS_STR_LEN);
998
999         return result;
1000 }
1001
1002 int _bt_hf_disconnect(int request_id,
1003                 bluetooth_device_address_t *device_address,
1004                 GArray *out_param1)
1005 {
1006         int result = BLUETOOTH_ERROR_NONE;
1007         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1008         bt_function_data_t *func_data;
1009         GDBusProxy *adapter_proxy;
1010         GDBusConnection *g_conn;
1011
1012         int ret;
1013         char *uuid;
1014
1015         BT_CHECK_PARAMETER(device_address, return);
1016
1017         _bt_convert_addr_type_to_string(address, device_address->addr);
1018
1019         adapter_proxy = _bt_get_adapter_proxy();
1020         if (adapter_proxy == NULL) {
1021                 result = BLUETOOTH_ERROR_INTERNAL;
1022                 goto fail;
1023         }
1024
1025         g_conn = _bt_gdbus_get_system_gconn();
1026         if (g_conn == NULL) {
1027                 result = BLUETOOTH_ERROR_INTERNAL;
1028                 goto fail;
1029         }
1030
1031         func_data = g_malloc0(sizeof(bt_function_data_t));
1032         func_data->address = g_strdup(address);
1033         func_data->req_id = request_id;
1034         uuid = g_strdup(HFP_AG_UUID);
1035
1036         BT_DBG("Disconnecting service %s", uuid);
1037         ret = _bt_disconnect_profile(address, uuid,
1038                         __bt_hf_request_cb, func_data);
1039
1040         if (ret != BLUETOOTH_ERROR_NONE) {
1041                 BT_ERR("_bt_disconnect_profile Error");
1042                 g_free(func_data->address);
1043                 g_free(func_data);
1044                 g_free(uuid);
1045                 return ret;
1046         }
1047         g_free(uuid);
1048         if (out_param1)
1049                 g_array_free(out_param1, TRUE);
1050
1051         return BLUETOOTH_ERROR_NONE;
1052 fail:
1053         if (out_param1 != NULL)
1054                 g_array_append_vals(out_param1, address,
1055                                 BT_ADDRESS_STR_LEN);
1056
1057         return result;
1058 }
1059
1060 int _bt_audio_set_content_protect(gboolean status)
1061 {
1062         GDBusConnection *conn;
1063         GError *error = NULL;
1064
1065         BT_DBG("+\n");
1066
1067         conn = _bt_gdbus_get_system_gconn();
1068         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1069
1070         BT_DBG("Content Protection status = [%d]", status);
1071
1072         g_dbus_connection_emit_signal(conn,
1073                         NULL, BT_CONTENT_PROTECTION_PATH,
1074                         BT_CONTENT_PROTECTION_INTERFACE,
1075                         "ProtectionRequired",
1076                         g_variant_new("(b)", status),
1077                         &error);
1078
1079         if (error) {
1080                 /* dBUS gives error cause */
1081                 ERR("Could not Emit Signal: errCode[%x], message[%s]",
1082                         error->code, error->message);
1083                 g_clear_error(&error);
1084                 return BLUETOOTH_ERROR_INTERNAL;
1085         }
1086
1087         BT_DBG("Emit Signal done = [ProtectionRequired]");
1088         return BLUETOOTH_ERROR_NONE;
1089 }
1090
1091 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1092 static void __bt_auto_connect_request_cb(GDBusProxy *proxy, GAsyncResult *res,
1093                                     gpointer user_data)
1094 {
1095         GError *err = NULL;
1096         GVariant *reply = NULL;
1097
1098         reply = g_dbus_proxy_call_finish(proxy, res, &err);
1099         g_object_unref(proxy);
1100         g_variant_unref(reply);
1101
1102         BT_DBG("+");
1103         if (err != NULL) {
1104                 g_dbus_error_strip_remote_error(err);
1105                 BT_INFO("Auto_connect_request Dbus Call Error: %s", err->message);
1106
1107                 if (strcmp("Host is down", err->message)) {
1108                         BT_INFO("Fail reason is not 'Host Down'. Terminate auto connect");
1109                         _bt_audio_stop_auto_connect();
1110                         vconf_set_str(BT_LAST_CONNECTED_DEVICE, "");
1111                 }
1112         }
1113         g_clear_error(&err);
1114 }
1115
1116
1117 // It is the function that retry to create alarm.
1118 // Sometimes alarm mgr service is created later than bluetooth-frwk service in booting time.
1119 // So, in this case, we have to retry alarmmgr_add_alarm.
1120 static gboolean __bt_audio_alarm_retry_cb(gpointer data)
1121 {
1122         int result;
1123         alarm_id_t alarm_id;
1124
1125         BT_DBG("__bt_audio_alarm_retry_cb called.");
1126         result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1127                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1128
1129         if (result != BLUETOOTH_ERROR_NONE)
1130                 return TRUE;
1131         else
1132                 auto_connect_timer.alarm_id = alarm_id;
1133
1134         return FALSE;
1135 }
1136
1137 static int __bt_auto_connect_alarm_cb(alarm_id_t alarm_id, void* user_param)
1138 {
1139         int result = BLUETOOTH_ERROR_NONE;
1140         char *address = NULL;
1141         alarm_id_t new_alarm_id;
1142
1143         BT_INFO("alram id = [%d] , retry_count [%d] ",
1144                                                 alarm_id, auto_connect_timer.retry_count);
1145
1146         if (alarm_id != auto_connect_timer.alarm_id)
1147                 return 0;
1148
1149         address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1150
1151         if (address == NULL) {
1152                 BT_ERR("Address vconf is null");
1153                 auto_connect_timer.alarm_id = 0;
1154                 return 0;
1155         }
1156
1157         BT_DBG("ADDRESS [%s]", address);
1158
1159         result = _bt_connect_profile(address, A2DP_SOURCE_UUID,
1160                         __bt_auto_connect_request_cb, NULL);
1161
1162         auto_connect_timer.retry_count--;
1163
1164         BT_DBG("result [%d]", result);
1165
1166         if (auto_connect_timer.retry_count <= 0) {
1167                 BT_INFO("Stopping Auto connect retry");
1168                 auto_connect_timer.alarm_id = 0;
1169         } else {
1170                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1171                                                 __bt_auto_connect_alarm_cb, NULL, &new_alarm_id);
1172                 if (result == BLUETOOTH_ERROR_NONE)
1173                         auto_connect_timer.alarm_id = new_alarm_id;
1174         }
1175
1176         free(address);
1177         return 0;
1178 }
1179
1180 static void __bt_auto_connect_alarm_remove()
1181 {
1182         BT_DBG("");
1183         if (auto_connect_timer.alarm_id > 0) {
1184                 _bt_service_remove_alarm(auto_connect_timer.alarm_id);
1185                 auto_connect_timer.alarm_id = 0;
1186         }
1187         auto_connect_timer.retry_count = 0;
1188 }
1189
1190 int _bt_audio_start_auto_connect(gboolean linkloss_flag)
1191 {
1192         int result = BLUETOOTH_ERROR_NONE;
1193         char *address = NULL;
1194         alarm_id_t alarm_id = 0;
1195
1196         BT_DBG("");
1197
1198         address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1199
1200         if (address == NULL) {
1201                 BT_ERR("No target device");
1202                 return 0;
1203         }
1204
1205         __bt_auto_connect_alarm_remove();
1206
1207         auto_connect_timer.retry_count =  BT_AUTO_CONNECT_TIMEOUT_RETRY_TIME /
1208                                                 BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS;
1209
1210         if (linkloss_flag) {
1211                 BT_INFO("Start auto connection after linkloss");
1212                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1213                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1214                 if (result != BLUETOOTH_ERROR_NONE)
1215                         g_timeout_add(500, (GSourceFunc)__bt_audio_alarm_retry_cb, NULL);
1216                 else
1217                         auto_connect_timer.alarm_id = alarm_id;
1218
1219         } else {
1220                 BT_INFO("Start auto connection after BT activated");
1221                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_BT_ACTIVATED,
1222                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1223                 if (result == BLUETOOTH_ERROR_NONE)
1224                         auto_connect_timer.alarm_id = alarm_id;
1225         }
1226
1227         free(address);
1228         return BLUETOOTH_ERROR_NONE;
1229 }
1230
1231 int _bt_audio_stop_auto_connect(void)
1232 {
1233         BT_DBG("");
1234         __bt_auto_connect_alarm_remove();
1235
1236         return BLUETOOTH_ERROR_NONE;
1237 }
1238
1239 void _bt_audio_set_auto_connect_device_addr(const char *address)
1240 {
1241         if (address == NULL) {
1242                 BT_ERR("address is null");
1243                 return;
1244         }
1245
1246         BT_INFO("Last connected device is [%s]", address);
1247         vconf_set_str(BT_LAST_CONNECTED_DEVICE, address);
1248 }
1249 #endif /*TIZEN_BT_A2DP_SINK_AUTO_CONNECT */