ff76b58d5b186e5954f4ff1aaecf1bbdc99bf9fe
[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 address 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
517         connected_device->device_state = status;
518         if ((status == BT_STATE_CONNECTED) || (status == BT_STATE_CONNECTING))
519                 connected_device->type |= type;
520         g_strlcpy(connected_device->device_address, address,
521                         sizeof(connected_device->device_address));
522         g_connected_list = g_list_append(g_connected_list, connected_device);
523 }
524
525 int _bt_get_device_state_from_list(int type, const char *address)
526 {
527         GList *node;
528         bt_connected_headset_data_t *device;
529
530         BT_DBG("+");
531         node = g_list_first(g_connected_list);
532         while (node != NULL) {
533                 device = (bt_connected_headset_data_t *)node->data;
534                 if (g_strcmp0(device->device_address, address) == 0) {
535                         BT_DBG("Device found");
536                         return device->device_state;
537                 }
538                 node = g_list_next(node);
539         }
540
541         BT_DBG("Device not found");
542         return BLUETOOTH_ERROR_INTERNAL;
543 }
544
545 void _bt_remove_headset_from_list(int type, const char *address)
546 {
547         GList *node;
548
549         BT_DBG("_bt_remove_headset_from_list \n");
550
551         node = g_list_first(g_connected_list);
552         while (node != NULL) {
553                 bt_connected_headset_data_t *connected_device = node->data;
554
555                 if (g_strcmp0(connected_device->device_address, address) != 0) {
556                         node = g_list_next(node);
557                         continue;
558                 }
559
560                 BT_DBG("Address match \n");
561
562                 BT_DBG("Connection type = %x\n", connected_device->type);
563
564                 switch (type) {
565                 case BT_AUDIO_A2DP:
566                         if (connected_device->type & BT_AUDIO_A2DP)
567                                 connected_device->type &= ~(BT_AUDIO_A2DP);
568                         break;
569                 case BT_AUDIO_HSP:
570                         if (connected_device->type & BT_AUDIO_HSP)
571                                 connected_device->type &= ~(BT_AUDIO_HSP);
572                         break;
573                 case BT_AUDIO_ALL:
574                         if (connected_device->type & BT_AUDIO_ALL)
575                                 connected_device->type &= ~(BT_AUDIO_ALL);
576                         break;
577                 case BT_AVRCP:
578                         if (connected_device->type & BT_AVRCP)
579                                 connected_device->type &= ~(BT_AVRCP);
580                         break;
581                 case BT_AUDIO_A2DP_SOURCE:
582                         if (connected_device->type & BT_AUDIO_A2DP_SOURCE)
583                                 connected_device->type &= ~(BT_AUDIO_A2DP_SOURCE);
584                 }
585
586                 BT_DBG("Connection type = %x\n", connected_device->type);
587
588                 if (connected_device->type == 0x00) {
589                         g_connected_list = g_list_remove(g_connected_list, connected_device);
590                         g_free(connected_device);
591                 } else {
592                         connected_device->device_state = BT_STATE_CONNECTED;
593                 }
594
595                 node = g_list_next(node);
596         }
597 }
598
599 static gboolean __bt_device_support_uuid(char *remote_address,
600                                 bt_audio_type_t type)
601 {
602         GArray *dev_list = NULL;
603         int size;
604         int i;
605         int j;
606         bluetooth_device_info_t *info;
607         char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
608         gboolean ret = FALSE;
609
610         BT_DBG("+");
611
612         dev_list = g_array_new(FALSE, FALSE, sizeof(gchar));
613
614         _bt_get_bonded_devices(&dev_list);
615         size = (dev_list->len) / sizeof(bluetooth_device_info_t);
616
617         for (i = 0; i < size; i++) {
618                 info = &g_array_index(dev_list, bluetooth_device_info_t, i);
619                 _bt_convert_addr_type_to_string(bond_address,
620                                 info->device_address.addr);
621                 if (strcmp(bond_address, remote_address) != 0)
622                         continue;
623
624                 BT_INFO("Device address Matched");
625                 j = 0;
626                 while (j != info->service_index) {
627                         if (type == BT_AUDIO_HSP) {
628                                 if (strcmp(info->uuids[j], HFP_HS_UUID) == 0) {
629                                         BT_INFO("HFP HS UUID exists");
630                                         ret = TRUE;
631                                         goto end;
632                                 }
633                         } else if (type == BT_AUDIO_A2DP) {
634                                 if (strcmp(info->uuids[j], A2DP_SINK_UUID) == 0) {
635                                         BT_INFO("A2DP SINK UUID exists");
636                                         ret = TRUE;
637                                         goto end;
638                                 }
639                         }
640                         j++;
641                 }
642         }
643 end:
644         g_array_free(dev_list, TRUE);
645         BT_DBG("-");
646         return ret;
647 }
648
649 gboolean _bt_is_service_connected(char* address, int type)
650 {
651         GList *node;
652
653         node = g_list_first(g_connected_list);
654         while (node != NULL) {
655                 bt_connected_headset_data_t *conn_device = node->data;
656
657                 if ((g_strcmp0(conn_device->device_address, address) == 0) &&
658                         (conn_device->type & type)) {
659                                 BT_INFO("Service connected");
660                                 return TRUE;
661                 }
662
663                 node = g_list_next(node);
664         }
665         BT_INFO("Service not connected");
666         return FALSE;
667 }
668
669 int _bt_audio_connect(int request_id, int type,
670                 bluetooth_device_address_t *device_address,
671                 GArray *out_param1)
672 {
673         int result = BLUETOOTH_ERROR_NONE;
674         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
675         GDBusProxy *adapter_proxy;
676         GDBusConnection *g_conn;
677         int ret;
678         char *uuid = NULL;
679         int value = BLUETOOTH_ERROR_NONE;
680         bt_audio_function_data_t *func_data;
681         guint hfp_hs_restricted = 0x0; /* set default "allowed" */
682         guint a2dp_restricted = 0x0;
683
684         BT_CHECK_PARAMETER(device_address, return);
685
686         adapter_proxy = _bt_get_adapter_proxy();
687         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
688
689         g_conn = _bt_gdbus_get_system_gconn();
690         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
691
692         _bt_convert_addr_type_to_string(address, device_address->addr);
693
694         func_data = g_malloc0(sizeof(bt_audio_function_data_t));
695
696         func_data->address = g_strdup(address);
697         func_data->req_id = request_id;
698         func_data->type = type;
699         func_data->pending = BT_PENDING_NONE;
700
701         result = _bt_get_restrict_profile(device_address, RESTRICTED_PROFILE_HFP_HS, &hfp_hs_restricted);
702         if (result != BLUETOOTH_ERROR_NONE)
703                 BT_ERR("Can't get hfp_hs restriction info");
704
705         result = _bt_get_restrict_profile(device_address, RESTRICTED_PROFILE_A2DP, &a2dp_restricted);
706         if (result != BLUETOOTH_ERROR_NONE)
707                 BT_ERR("Can't get a2dp restriction info");
708
709         switch (type) {
710         case BT_AUDIO_HSP:
711                 uuid = HFP_HS_UUID;
712                 break;
713         case BT_AUDIO_A2DP:
714                 uuid = A2DP_SINK_UUID;
715                 break;
716         case BT_AVRCP:
717                 uuid = AVRCP_TARGET_UUID;
718                 break;
719         case BT_AUDIO_A2DP_SOURCE:
720                 uuid = A2DP_SOURCE_UUID;
721                 break;
722         case BT_AUDIO_ALL:
723                 if ((hfp_hs_restricted == 0x0) && (a2dp_restricted == 0x0)) { /* in case of both profiles are not restricted */
724                         if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
725                                 uuid = HFP_HS_UUID;
726                                 func_data->pending = BT_PENDING_CONNECT;
727                                 type = BT_AUDIO_HSP;
728                         } else if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
729                                 uuid = A2DP_SINK_UUID;
730                                 type = BT_AUDIO_A2DP;
731                         } else {
732                                 BT_ERR("No audio role supported");
733                                 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
734                                 goto fail;
735                         }
736                 } else if ((hfp_hs_restricted == 0x1) && (a2dp_restricted == 0x1)) { /* in case of both profiles are restricted */
737                         BT_ERR("All profiles are restricted");
738                         result = BLUETOOTH_ERROR_INTERNAL;
739                         goto fail;
740                 } else if (a2dp_restricted == 0x01) { /* in case of a2dp is restricted, only connection for hfp_hs */
741                         if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
742                                 uuid = HFP_HS_UUID;
743                                 type = BT_AUDIO_HSP;
744                         } else {
745                                 BT_ERR("HFP_HS role is not supported");
746                                 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
747                                 goto fail;
748                         }
749                 } else if (hfp_hs_restricted == 0x01) { /* in case of hfp_hs is restricted, only connection for a2dp */
750                         if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
751                                 uuid = A2DP_SINK_UUID;
752                                 type = BT_AUDIO_A2DP;
753                         } else {
754                                 BT_ERR("A2DP role is not supported");
755                                 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
756                                 goto fail;
757                         }
758                 }
759                 break;
760         case BT_AVRCP_TARGET:
761                 uuid = AVRCP_REMOTE_UUID;
762                 break;
763         default:
764                 BT_ERR("Unknown role");
765                 result = BLUETOOTH_ERROR_INTERNAL;
766                 goto fail;
767         }
768         BT_INFO("Connecting to service %s", uuid);
769
770         value = __bt_is_headset_connected(type, request_id, address);
771
772         if (value == BLUETOOTH_ERROR_ALREADY_CONNECT) {
773                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
774         } else if (value == BLUETOOTH_ERROR_NOT_CONNECTED) {
775                 value = __bt_is_headset_connecting(type);
776                 if (value != BLUETOOTH_ERROR_NONE) {
777                         result = BLUETOOTH_ERROR_IN_PROGRESS;
778                         goto fail;
779                 }
780                 _bt_headset_set_local_connection(TRUE);
781                 ret = _bt_connect_profile(address, uuid,
782                                 __bt_audio_request_cb, func_data);
783
784                 if (ret != BLUETOOTH_ERROR_NONE) {
785                         BT_ERR("_bt_connect_profile Error");
786                         _bt_headset_set_local_connection(FALSE);
787                         g_free(func_data->address);
788                         g_free(func_data);
789                         return ret;
790                 }
791
792                 /* Add data to the connected list */
793                 _bt_add_headset_to_list(type, BT_STATE_CONNECTING, address);
794         } else if (value == BLUETOOTH_ERROR_IN_PROGRESS) {
795                 return BLUETOOTH_ERROR_IN_PROGRESS;
796         }
797
798         if (out_param1)
799                 g_array_free(out_param1, TRUE);
800
801         return BLUETOOTH_ERROR_NONE;
802 fail:
803         if (out_param1 != NULL)
804                 g_array_append_vals(out_param1, address,
805                                 BT_ADDRESS_STR_LEN);
806
807         return result;
808 }
809
810 int _bt_audio_disconnect(int request_id, int type,
811                 bluetooth_device_address_t *device_address,
812                 GArray *out_param1)
813 {
814         int result = BLUETOOTH_ERROR_NONE;
815         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
816         bt_audio_function_data_t *func_data;
817         GDBusProxy *adapter_proxy;
818         GDBusConnection *g_conn;
819         GList *node;
820         int ret;
821         char *uuid;
822         int value = BLUETOOTH_ERROR_NONE;
823
824         BT_CHECK_PARAMETER(device_address, return);
825
826         adapter_proxy = _bt_get_adapter_proxy();
827         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
828
829         g_conn = _bt_gdbus_get_system_gconn();
830         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
831
832         _bt_convert_addr_type_to_string(address, device_address->addr);
833
834         func_data = g_malloc0(sizeof(bt_audio_function_data_t));
835         func_data->address = g_strdup(address);
836         func_data->req_id = request_id;
837         func_data->pending = BT_PENDING_NONE;
838         func_data->type = type;
839
840         switch (type) {
841         case BT_AUDIO_HSP:
842                 uuid = HFP_HS_UUID;
843                 break;
844         case BT_AUDIO_A2DP:
845                 uuid = A2DP_SINK_UUID;
846                 break;
847         case BT_AVRCP:
848                 uuid = AVRCP_TARGET_UUID;
849                 break;
850         case BT_AUDIO_A2DP_SOURCE:
851                 uuid = A2DP_SOURCE_UUID;
852                 break;
853         case BT_AUDIO_ALL:
854                 if (_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
855                         uuid = A2DP_SINK_UUID;
856                         func_data->pending = BT_PENDING_DISCONNECT;
857                 } else if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
858                         uuid = HFP_HS_UUID;
859                 } else {
860                         BT_ERR("No audio service connected");
861                         result = BLUETOOTH_ERROR_NOT_CONNECTED;
862                         goto fail;
863                 }
864                 break;
865         case BT_AVRCP_TARGET:
866                 uuid = AVRCP_REMOTE_UUID;
867                 break;
868         default:
869                 BT_ERR("Unknown role");
870                 result = BLUETOOTH_ERROR_INTERNAL;
871                 goto fail;
872         }
873         value = __bt_is_headset_disconnecting(type);
874         if (value != BLUETOOTH_ERROR_NONE) {
875                 BT_INFO("Disconnect in progress");
876                 result = BLUETOOTH_ERROR_IN_PROGRESS;
877                 goto fail;
878         }
879         BT_INFO("Disconnecting service %s", uuid);
880         ret = _bt_disconnect_profile(address, uuid,
881                         __bt_audio_request_cb, func_data);
882
883         if (ret != BLUETOOTH_ERROR_NONE) {
884                 BT_ERR("_bt_disconnect_profile Error");
885                 g_free(func_data->address);
886                 g_free(func_data);
887                 return ret;
888         }
889
890         /*
891          *      This logic is added for dual HF mode issue.
892          */
893         node = g_list_first(g_connected_list);
894         while (node != NULL) {
895                 bt_connected_headset_data_t *connected_device = node->data;
896
897                 if (g_strcmp0(connected_device->device_address, address) == 0) {
898                         BT_DBG("Connection type update");
899                         type = connected_device->type;
900                         break;
901                 }
902                 node = g_list_next(node);
903         }
904         _bt_add_headset_to_list(type, BT_STATE_DISCONNECTING, address);
905
906         if (out_param1)
907                 g_array_free(out_param1, TRUE);
908
909         return BLUETOOTH_ERROR_NONE;
910 fail:
911         if (out_param1 != NULL)
912                 g_array_append_vals(out_param1, address,
913                                 BT_ADDRESS_STR_LEN);
914
915         return result;
916 }
917
918 void _bt_remove_from_connected_list(const char *address)
919 {
920         bt_connected_headset_data_t *connected_device;
921         GList *node;
922
923         node = g_list_first(g_connected_list);
924         while (node != NULL) {
925                 connected_device = node->data;
926                 if (connected_device != NULL &&
927                 g_strcmp0(connected_device->device_address, address) == 0) {
928                         BT_ERR("Device is removed from the list");
929                         g_connected_list = g_list_remove(g_connected_list, connected_device);
930                         g_free(connected_device);
931                         return;
932                 }
933                 node = g_list_next(node);
934         }
935 }
936
937 int _bt_hf_connect(int request_id,
938                 bluetooth_device_address_t *device_address,
939                 GArray *out_param1)
940 {
941         int result = BLUETOOTH_ERROR_NONE;
942         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
943         bt_function_data_t *func_data;
944         GDBusProxy *adapter_proxy;
945         GDBusConnection *g_conn;
946         gboolean connected = FALSE;
947         int ret;
948         char *uuid;
949
950         BT_CHECK_PARAMETER(device_address, return);
951
952         _bt_convert_addr_type_to_string(address, device_address->addr);
953
954         adapter_proxy = _bt_get_adapter_proxy();
955         if (adapter_proxy == NULL) {
956                 result = BLUETOOTH_ERROR_INTERNAL;
957                 goto fail;
958         }
959
960         g_conn = _bt_gdbus_get_system_gconn();
961         if (g_conn == NULL) {
962                 result = BLUETOOTH_ERROR_INTERNAL;
963                 goto fail;
964         }
965
966         /* Check if HF is connected or not */
967         result = _bt_is_device_connected(device_address, BT_PROFILE_CONN_HFG, &connected);
968         if (connected == TRUE) {
969                 BT_ERR("HF is already connected");
970                 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
971                 goto fail;
972         }
973
974         func_data = g_malloc0(sizeof(bt_function_data_t));
975         func_data->address = g_strdup(address);
976         func_data->req_id = request_id;
977         uuid = g_strdup(HFP_AG_UUID);
978
979         BT_DBG("Connecting to service %s", uuid);
980
981         ret = _bt_connect_profile(address, uuid,
982                         __bt_hf_request_cb, func_data);
983
984         if (ret != BLUETOOTH_ERROR_NONE) {
985                 BT_ERR("_bt_connect_profile Error");
986                 g_free(func_data->address);
987                 g_free(func_data);
988                 g_free(uuid);
989                 return ret;
990         }
991         g_free(uuid);
992         if (out_param1)
993                 g_array_free(out_param1, TRUE);
994
995         return BLUETOOTH_ERROR_NONE;
996 fail:
997         if (out_param1 != NULL)
998                 g_array_append_vals(out_param1, address,
999                                 BT_ADDRESS_STR_LEN);
1000
1001         return result;
1002 }
1003
1004 int _bt_hf_disconnect(int request_id,
1005                 bluetooth_device_address_t *device_address,
1006                 GArray *out_param1)
1007 {
1008         int result = BLUETOOTH_ERROR_NONE;
1009         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1010         bt_function_data_t *func_data;
1011         GDBusProxy *adapter_proxy;
1012         GDBusConnection *g_conn;
1013
1014         int ret;
1015         char *uuid;
1016
1017         BT_CHECK_PARAMETER(device_address, return);
1018
1019         _bt_convert_addr_type_to_string(address, device_address->addr);
1020
1021         adapter_proxy = _bt_get_adapter_proxy();
1022         if (adapter_proxy == NULL) {
1023                 result = BLUETOOTH_ERROR_INTERNAL;
1024                 goto fail;
1025         }
1026
1027         g_conn = _bt_gdbus_get_system_gconn();
1028         if (g_conn == NULL) {
1029                 result = BLUETOOTH_ERROR_INTERNAL;
1030                 goto fail;
1031         }
1032
1033         func_data = g_malloc0(sizeof(bt_function_data_t));
1034         func_data->address = g_strdup(address);
1035         func_data->req_id = request_id;
1036         uuid = g_strdup(HFP_AG_UUID);
1037
1038         BT_DBG("Disconnecting service %s", uuid);
1039         ret = _bt_disconnect_profile(address, uuid,
1040                         __bt_hf_request_cb, func_data);
1041
1042         if (ret != BLUETOOTH_ERROR_NONE) {
1043                 BT_ERR("_bt_disconnect_profile Error");
1044                 g_free(func_data->address);
1045                 g_free(func_data);
1046                 g_free(uuid);
1047                 return ret;
1048         }
1049         g_free(uuid);
1050         if (out_param1)
1051                 g_array_free(out_param1, TRUE);
1052
1053         return BLUETOOTH_ERROR_NONE;
1054 fail:
1055         if (out_param1 != NULL)
1056                 g_array_append_vals(out_param1, address,
1057                                 BT_ADDRESS_STR_LEN);
1058
1059         return result;
1060 }
1061
1062 int _bt_audio_set_content_protect(gboolean status)
1063 {
1064         GDBusConnection *conn;
1065         GError *error = NULL;
1066
1067         BT_DBG("+\n");
1068
1069         conn = _bt_gdbus_get_system_gconn();
1070         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1071
1072         BT_DBG("Content Protection status = [%d]", status);
1073
1074         g_dbus_connection_emit_signal(conn,
1075                         NULL, BT_CONTENT_PROTECTION_PATH,
1076                         BT_CONTENT_PROTECTION_INTERFACE,
1077                         "ProtectionRequired",
1078                         g_variant_new("(b)", status),
1079                         &error);
1080
1081         if (error) {
1082                 /* dBUS gives error cause */
1083                 ERR("Could not Emit Signal: errCode[%x], message[%s]",
1084                         error->code, error->message);
1085                 g_clear_error(&error);
1086                 return BLUETOOTH_ERROR_INTERNAL;
1087         }
1088
1089         BT_DBG("Emit Signal done = [ProtectionRequired]");
1090         return BLUETOOTH_ERROR_NONE;
1091 }
1092
1093 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1094 static void __bt_auto_connect_request_cb(GDBusProxy *proxy, GAsyncResult *res,
1095                                     gpointer user_data)
1096 {
1097         GError *err = NULL;
1098         GVariant *reply = NULL;
1099
1100         reply = g_dbus_proxy_call_finish(proxy, res, &err);
1101         g_object_unref(proxy);
1102         g_variant_unref(reply);
1103
1104         BT_DBG("+");
1105         if (err != NULL) {
1106                 g_dbus_error_strip_remote_error(err);
1107                 BT_INFO("Auto_connect_request Dbus Call Error: %s", err->message);
1108
1109                 if (strcmp("Host is down", err->message)) {
1110                         BT_INFO("Fail reason is not 'Host Down'. Terminate auto connect");
1111                         _bt_audio_stop_auto_connect();
1112                         vconf_set_str(BT_LAST_CONNECTED_DEVICE, "");
1113                 }
1114         }
1115         g_clear_error(&err);
1116 }
1117
1118
1119 // It is the function that retry to create alarm.
1120 // Sometimes alarm mgr service is created later than bluetooth-frwk service in booting time.
1121 // So, in this case, we have to retry alarmmgr_add_alarm.
1122 static gboolean __bt_audio_alarm_retry_cb(gpointer data)
1123 {
1124         int result;
1125         alarm_id_t alarm_id;
1126
1127         BT_DBG("__bt_audio_alarm_retry_cb called.");
1128         result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1129                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1130
1131         if (result != BLUETOOTH_ERROR_NONE)
1132                 return TRUE;
1133         else
1134                 auto_connect_timer.alarm_id = alarm_id;
1135
1136         return FALSE;
1137 }
1138
1139 static int __bt_auto_connect_alarm_cb(alarm_id_t alarm_id, void* user_param)
1140 {
1141         int result = BLUETOOTH_ERROR_NONE;
1142         char *address = NULL;
1143         alarm_id_t new_alarm_id;
1144
1145         BT_INFO("alram id = [%d] , retry_count [%d] ",
1146                                                 alarm_id, auto_connect_timer.retry_count);
1147
1148         if (alarm_id != auto_connect_timer.alarm_id)
1149                 return 0;
1150
1151         address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1152
1153         if (address == NULL) {
1154                 BT_ERR("Address vconf is null");
1155                 auto_connect_timer.alarm_id = 0;
1156                 return 0;
1157         }
1158
1159         BT_DBG("ADDRESS [%s]", address);
1160
1161         result = _bt_connect_profile(address, A2DP_SOURCE_UUID,
1162                         __bt_auto_connect_request_cb, NULL);
1163
1164         auto_connect_timer.retry_count--;
1165
1166         BT_DBG("result [%d]", result);
1167
1168         if (auto_connect_timer.retry_count <= 0) {
1169                 BT_INFO("Stopping Auto connect retry");
1170                 auto_connect_timer.alarm_id = 0;
1171         } else {
1172                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1173                                                 __bt_auto_connect_alarm_cb, NULL, &new_alarm_id);
1174                 if (result == BLUETOOTH_ERROR_NONE)
1175                         auto_connect_timer.alarm_id = new_alarm_id;
1176         }
1177
1178         free(address);
1179         return 0;
1180 }
1181
1182 static void __bt_auto_connect_alarm_remove()
1183 {
1184         BT_DBG("");
1185         if (auto_connect_timer.alarm_id > 0) {
1186                 _bt_service_remove_alarm(auto_connect_timer.alarm_id);
1187                 auto_connect_timer.alarm_id = 0;
1188         }
1189         auto_connect_timer.retry_count = 0;
1190 }
1191
1192 int _bt_audio_start_auto_connect(gboolean linkloss_flag)
1193 {
1194         int result = BLUETOOTH_ERROR_NONE;
1195         char *address = NULL;
1196         alarm_id_t alarm_id = 0;
1197
1198         BT_DBG("");
1199
1200         address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1201
1202         if (address == NULL) {
1203                 BT_ERR("No target device");
1204                 return 0;
1205         }
1206
1207         __bt_auto_connect_alarm_remove();
1208
1209         auto_connect_timer.retry_count =  BT_AUTO_CONNECT_TIMEOUT_RETRY_TIME /
1210                                                 BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS;
1211
1212         if (linkloss_flag) {
1213                 BT_INFO("Start auto connection after linkloss");
1214                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1215                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1216                 if (result != BLUETOOTH_ERROR_NONE)
1217                         g_timeout_add(500, (GSourceFunc)__bt_audio_alarm_retry_cb, NULL);
1218                 else
1219                         auto_connect_timer.alarm_id = alarm_id;
1220
1221         } else {
1222                 BT_INFO("Start auto connection after BT activated");
1223                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_BT_ACTIVATED,
1224                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1225                 if (result == BLUETOOTH_ERROR_NONE)
1226                         auto_connect_timer.alarm_id = alarm_id;
1227         }
1228
1229         free(address);
1230         return BLUETOOTH_ERROR_NONE;
1231 }
1232
1233 int _bt_audio_stop_auto_connect(void)
1234 {
1235         BT_DBG("");
1236         __bt_auto_connect_alarm_remove();
1237
1238         return BLUETOOTH_ERROR_NONE;
1239 }
1240
1241 void _bt_audio_set_auto_connect_device_addr(const char *address)
1242 {
1243         if (address == NULL) {
1244                 BT_ERR("address is null");
1245                 return;
1246         }
1247
1248         BT_INFO("Last connected device is [%s]", address);
1249         vconf_set_str(BT_LAST_CONNECTED_DEVICE, address);
1250 }
1251 #endif /*TIZEN_BT_A2DP_SINK_AUTO_CONNECT */