Fix SVACE issues in bluetooth-frwk
[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 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
315 void _bt_check_already_connected_headset(int type, char *address)
316 {
317         GList *node;
318         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
319         bluetooth_device_address_t device_address;
320         int device_count = 0;
321
322         if (address != NULL) {
323                 node = g_list_first(g_connected_list);
324                 while (node != NULL) {
325                         bt_connected_headset_data_t *connected_device = node->data;
326                         if ((connected_device->type & type) &&
327                                 (g_strcmp0(connected_device->device_address, address) != 0)) {
328                                 device_count++;
329                                 /* Disconnect the earliest(1st) connected headset */
330                                 if (device_count == 1) {
331                                         g_strlcpy(connected_address,
332                                                 connected_device->device_address,
333                                                 BT_ADDRESS_STRING_SIZE + 1);
334                                         BT_DBG("Earliest/First Connected headset %s",
335                                                 connected_address);
336                                 } else if (device_count == MAX_CONNECTED_HEADSET) {
337                                         _bt_convert_addr_string_to_type(device_address.addr,
338                                                 connected_address);
339                                         _bt_audio_disconnect(0, type, &device_address, NULL);
340                                         return;
341                                 }
342                         }
343                         node = g_list_next(node);
344                 }
345         }
346 }
347 #endif
348
349 gboolean _bt_is_headset_type_connected(int type, char *address)
350 {
351         GList *node;
352
353         node = g_list_first(g_connected_list);
354         while (node != NULL) {
355                 bt_connected_headset_data_t *connected_device = node->data;
356
357                 if (connected_device->type & type) {
358                         if (address != NULL)
359                                 g_strlcpy(address, connected_device->device_address,
360                                                 BT_ADDRESS_STRING_SIZE + 1);
361                         return TRUE;
362                 }
363
364                 node = g_list_next(node);
365         }
366         return FALSE;
367 }
368
369 #ifdef TIZEN_SUPPORT_DUAL_HF
370 gboolean __bt_is_companion_device(const char *addr)
371 {
372 #ifdef TIZEN_PROFILE_WEARABLE
373         char *host_device_address = NULL;
374         host_device_address = vconf_get_str(VCONF_KEY_BT_HOST_BT_MAC_ADDR);
375
376         if (!host_device_address) {
377                 BT_INFO("Failed to get a companion device address");
378                 return FALSE;
379         }
380
381         if (g_strcmp0(host_device_address, addr) == 0) {
382                 BT_INFO("Found companion device");
383                 free(host_device_address);
384                 return TRUE;
385         }
386
387         free(host_device_address);
388         return FALSE;
389 #else
390         /* TODO : Need to add companion device check condition for Phone models */
391         return FALSE;
392 #endif
393 }
394 #endif
395
396 static int __bt_is_headset_connected(int type, int req_id,
397                                 const char *address)
398 {
399         gboolean connected = FALSE;
400         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
401         bluetooth_device_address_t device_address;
402         bt_connected_headset_data_t *connected_device = NULL;
403 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
404         int device_count = 0;
405 #endif
406
407 #ifdef TIZEN_SUPPORT_DUAL_HF
408         gboolean is_companion_device = FALSE;
409 #endif
410
411         /* Check if any other headset is connected */
412         GList *node = NULL;;
413
414         node = g_list_first(g_connected_list);
415         while (node != NULL) {
416                 connected_device = node->data;
417                 if ((connected_device->type & type)) {
418 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
419                         device_count++;
420                         /* Disconnect the earliest(1st) connected headset */
421                         if (device_count == 1)
422                                 g_strlcpy(connected_address, connected_device->device_address,
423                                         BT_ADDRESS_STRING_SIZE + 1);
424
425                         if (g_strcmp0(connected_device->device_address, address) == 0)
426                                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
427 #else
428                         g_strlcpy(connected_address, connected_device->device_address,
429                                         BT_ADDRESS_STRING_SIZE + 1);
430 #endif
431 #ifdef TIZEN_SUPPORT_DUAL_HF
432                         is_companion_device = __bt_is_companion_device(connected_address);
433                         BT_INFO(" is_companion_device[%d]", is_companion_device);
434
435                         if (!is_companion_device) {
436                                 connected = TRUE;
437                                 break;
438                         }
439 #else
440 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
441                         if (device_count == MAX_CONNECTED_HEADSET) {
442                                 connected = TRUE;
443                                 break;
444                         }
445 #else
446                         connected = TRUE;
447                         break;
448 #endif
449 #endif
450                 }
451                 node = g_list_next(node);
452         }
453
454         if (!connected)
455                 return BLUETOOTH_ERROR_NOT_CONNECTED;
456
457         BT_DBG("connected headset %s", connected_address);
458
459         if (g_strcmp0(connected_address, address) == 0)
460                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
461 #ifdef TIZEN_SUPPORT_DUAL_HF
462         else if (TRUE == __bt_is_companion_device(address))
463                 return BLUETOOTH_ERROR_NOT_CONNECTED;
464 #endif
465
466         /* Convert BD adress from string type */
467         _bt_convert_addr_string_to_type(device_address.addr, connected_address);
468         int value = BLUETOOTH_ERROR_NONE;
469         value = _bt_audio_disconnect(0, connected_device->type & type, &device_address, NULL);
470
471         /* If already one device is waiting, remove current waiting device and add new */
472         if (value == BLUETOOTH_ERROR_NONE) {
473                 if (g_wait_data != NULL) {
474                         if (g_strcmp0(g_wait_data->address, address) != 0) {
475                                 __bt_remove_device_from_wait_list();
476                                 __bt_free_wait_data();
477                         }
478                 }
479
480                 if (g_wait_data == NULL) {
481                         g_wait_data = g_malloc0(sizeof(bt_headset_wait_t));
482                         g_wait_data->address = g_strdup(address);
483                         g_wait_data->req_id = req_id;
484                         g_wait_data->type = type;
485                         g_wait_data->ag_flag = FALSE;
486
487                         /* Set disconnection type */
488                         __bt_set_headset_disconnection_type(connected_address);
489                 }
490         }
491
492         return value;
493 }
494
495 static int __bt_is_headset_connecting(int type)
496 {
497         bt_connected_headset_data_t *connected_device = NULL;
498
499         /* Check if any other headset is connected */
500         GList *node = NULL;
501
502         node = g_list_first(g_connected_list);
503         while (node != NULL) {
504                 connected_device = node->data;
505                 if (connected_device->device_state == BT_STATE_CONNECTING)
506                         return BLUETOOTH_ERROR_CONNECTION_BUSY;
507                 node = g_list_next(node);
508         }
509
510         return BLUETOOTH_ERROR_NONE;
511 }
512
513 int __bt_is_headset_disconnecting(int type)
514 {
515         bt_connected_headset_data_t *connected_device = NULL;
516
517         /* Check if any other headset is connected */
518         GList *node = NULL;
519
520         node = g_list_first(g_connected_list);
521         while (node != NULL) {
522                 connected_device = node->data;
523                 if (connected_device->device_state == BT_STATE_DISCONNECTING)
524                         return BLUETOOTH_ERROR_CONNECTION_BUSY;
525
526                 node = g_list_next(node);
527         }
528
529         return BLUETOOTH_ERROR_NONE;
530 }
531
532 void _bt_set_audio_wait_data_flag(gboolean flag)
533 {
534         BT_DBG("_bt_set_audio_wait_data_flag \n");
535         g_wait_data->ag_flag = flag;
536 }
537
538 bt_headset_wait_t *_bt_get_audio_wait_data(void)
539 {
540         BT_DBG("_bt_get_audio_wait_data \n");
541         return g_wait_data;
542 }
543
544 void _bt_rel_wait_data(void)
545 {
546         BT_DBG("_bt_rel_wait_data \n");
547         __bt_free_wait_data();
548 }
549
550 void _bt_add_headset_to_list(int type, int status, const char *address)
551 {
552         bt_connected_headset_data_t *connected_device;
553         bt_connected_headset_data_t *device;
554         GList *node;
555
556         BT_DBG("_bt_add_headset_to_list \n");
557
558         node = g_list_first(g_connected_list);
559         while (node != NULL) {
560                 device = (bt_connected_headset_data_t *)node->data;
561
562                 if (g_strcmp0(device->device_address, address) == 0) {
563                         BT_DBG("Address match, update connection type \n");
564                         if (status == BT_STATE_CONNECTED)
565                                 device->type |= type;
566                         device->device_state = status;
567                         return;
568                 }
569                 node = g_list_next(node);
570         }
571
572         connected_device = g_malloc0(sizeof(bt_connected_headset_data_t));
573         connected_device->device_state = status;
574         if ((status == BT_STATE_CONNECTED) || (status == BT_STATE_CONNECTING))
575                 connected_device->type |= type;
576         g_strlcpy(connected_device->device_address, address,
577                         sizeof(connected_device->device_address));
578         g_connected_list = g_list_append(g_connected_list, connected_device);
579 }
580
581 int _bt_get_device_state_from_list(int type, const char *address)
582 {
583         GList *node;
584         bt_connected_headset_data_t *device;
585
586         BT_DBG("+");
587         node = g_list_first(g_connected_list);
588         while (node != NULL) {
589                 device = (bt_connected_headset_data_t *)node->data;
590                 if (g_strcmp0(device->device_address, address) == 0) {
591                         BT_DBG("Device found");
592                         return device->device_state;
593                 }
594                 node = g_list_next(node);
595         }
596
597         BT_DBG("Device not found");
598         return BLUETOOTH_ERROR_INTERNAL;
599 }
600
601 void _bt_remove_headset_from_list(int type, const char *address)
602 {
603         GList *node;
604
605         BT_DBG("_bt_remove_headset_from_list \n");
606
607         node = g_list_first(g_connected_list);
608         while (node != NULL) {
609                 bt_connected_headset_data_t *connected_device = node->data;
610
611                 if (g_strcmp0(connected_device->device_address, address) != 0) {
612                         node = g_list_next(node);
613                         continue;
614                 }
615
616                 BT_DBG("Address match \n");
617
618                 BT_DBG("Connection type = %x\n", connected_device->type);
619
620                 switch (type) {
621                 case BT_AUDIO_A2DP:
622                         if (connected_device->type & BT_AUDIO_A2DP)
623                                 connected_device->type &= ~(BT_AUDIO_A2DP);
624                         break;
625                 case BT_AUDIO_HSP:
626                         if (connected_device->type & BT_AUDIO_HSP)
627                                 connected_device->type &= ~(BT_AUDIO_HSP);
628                         break;
629                 case BT_AUDIO_ALL:
630                         if (connected_device->type & BT_AUDIO_ALL)
631                                 connected_device->type &= ~(BT_AUDIO_ALL);
632                         break;
633                 case BT_AVRCP:
634                         if (connected_device->type & BT_AVRCP)
635                                 connected_device->type &= ~(BT_AVRCP);
636                         break;
637                 case BT_AUDIO_A2DP_SOURCE:
638                         if (connected_device->type & BT_AUDIO_A2DP_SOURCE)
639                                 connected_device->type &= ~(BT_AUDIO_A2DP_SOURCE);
640                 }
641
642                 BT_DBG("Connection type = %x\n", connected_device->type);
643
644                 if (connected_device->type == 0x00) {
645                         g_connected_list = g_list_remove(g_connected_list, connected_device);
646                         g_free(connected_device);
647                 } else {
648                         connected_device->device_state = BT_STATE_CONNECTED;
649                 }
650
651                 node = g_list_next(node);
652         }
653 }
654
655 static gboolean __bt_device_support_uuid(char *remote_address,
656                                 bt_audio_type_t type)
657 {
658         GArray *dev_list = NULL;
659         int size;
660         int i;
661         int j;
662         bluetooth_device_info_t info;
663         char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
664         gboolean ret = FALSE;
665
666         BT_DBG("+");
667
668         dev_list = g_array_new(FALSE, FALSE, sizeof(gchar));
669
670         _bt_get_bonded_devices(&dev_list);
671         size = (dev_list->len) / sizeof(bluetooth_device_info_t);
672
673         for (i = 0; i < size; i++) {
674                 info = g_array_index(dev_list, bluetooth_device_info_t, i);
675                 _bt_convert_addr_type_to_string(bond_address,
676                                 info.device_address.addr);
677                 if (strcmp(bond_address, remote_address) != 0)
678                         continue;
679
680                 BT_INFO("Device address Matched");
681                 j = 0;
682                 while (j != info.service_index) {
683                         if (type == BT_AUDIO_HSP) {
684                                 if (strcmp(info.uuids[j], HFP_HS_UUID) == 0) {
685                                         BT_INFO("HFP HS UUID exists");
686                                         ret = TRUE;
687                                         goto end;
688                                 }
689                         } else if (type == BT_AUDIO_A2DP) {
690                                 if (strcmp(info.uuids[j], A2DP_SINK_UUID) == 0) {
691                                         BT_INFO("A2DP SINK UUID exists");
692                                         ret = TRUE;
693                                         goto end;
694                                 }
695                         }
696                         j++;
697                 }
698         }
699 end:
700         g_array_free(dev_list, TRUE);
701         BT_DBG("-");
702         return ret;
703 }
704
705 gboolean _bt_is_service_connected(char* address, int type)
706 {
707         GList *node;
708
709         node = g_list_first(g_connected_list);
710         while (node != NULL) {
711                 bt_connected_headset_data_t *conn_device = node->data;
712
713                 if ((g_strcmp0(conn_device->device_address, address) == 0) &&
714                         (conn_device->type & type)) {
715                                 BT_INFO("Service connected");
716                                 return TRUE;
717                 }
718
719                 node = g_list_next(node);
720         }
721         BT_INFO("Service not connected");
722         return FALSE;
723 }
724
725 int _bt_audio_connect(int request_id, int type,
726                 bluetooth_device_address_t *device_address,
727                 GArray *out_param1)
728 {
729         int result = BLUETOOTH_ERROR_NONE;
730         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
731         GDBusProxy *adapter_proxy;
732         GDBusConnection *g_conn;
733         int ret;
734         char *uuid = NULL;
735         int value = BLUETOOTH_ERROR_NONE;
736         bt_audio_function_data_t *func_data;
737         guint hfp_hs_restricted = 0x0; /* set default "allowed" */
738         guint a2dp_restricted = 0x0;
739
740         BT_CHECK_PARAMETER(device_address, return);
741
742         adapter_proxy = _bt_get_adapter_proxy();
743         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
744
745         g_conn = _bt_gdbus_get_system_gconn();
746         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
747
748         _bt_convert_addr_type_to_string(address, device_address->addr);
749
750         func_data = g_malloc0(sizeof(bt_audio_function_data_t));
751         func_data->address = g_strdup(address);
752         func_data->req_id = request_id;
753         func_data->type = type;
754         func_data->pending = BT_PENDING_NONE;
755
756         result = _bt_get_restrict_profile(device_address, RESTRICTED_PROFILE_HFP_HS, &hfp_hs_restricted);
757         if (result != BLUETOOTH_ERROR_NONE)
758                 BT_ERR("Can't get hfp_hs restriction info");
759
760         result = _bt_get_restrict_profile(device_address, RESTRICTED_PROFILE_A2DP, &a2dp_restricted);
761         if (result != BLUETOOTH_ERROR_NONE)
762                 BT_ERR("Can't get a2dp restriction info");
763
764         switch (type) {
765         case BT_AUDIO_HSP:
766                 uuid = HFP_HS_UUID;
767                 break;
768         case BT_AUDIO_A2DP:
769                 uuid = A2DP_SINK_UUID;
770                 break;
771         case BT_AVRCP:
772                 uuid = AVRCP_TARGET_UUID;
773                 break;
774         case BT_AUDIO_A2DP_SOURCE:
775                 uuid = A2DP_SOURCE_UUID;
776                 break;
777         case BT_AUDIO_ALL:
778                 if ((hfp_hs_restricted == 0x0) && (a2dp_restricted == 0x0)) { /* in case of both profiles are not restricted */
779                         if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
780                                 uuid = HFP_HS_UUID;
781                                 func_data->pending = BT_PENDING_CONNECT;
782                                 type = BT_AUDIO_HSP;
783                         } else if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
784                                 uuid = A2DP_SINK_UUID;
785                                 type = BT_AUDIO_A2DP;
786                         } else {
787                                 BT_ERR("No audio role supported");
788                                 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
789                                 goto fail;
790                         }
791                 } else if ((hfp_hs_restricted == 0x1) && (a2dp_restricted == 0x1)) { /* in case of both profiles are restricted */
792                         BT_ERR("All profiles are restricted");
793                         result = BLUETOOTH_ERROR_INTERNAL;
794                         goto fail;
795                 } else if (a2dp_restricted == 0x01) { /* in case of a2dp is restricted, only connection for hfp_hs */
796                         if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
797                                 uuid = HFP_HS_UUID;
798                                 type = BT_AUDIO_HSP;
799                         } else {
800                                 BT_ERR("HFP_HS role is not supported");
801                                 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
802                                 goto fail;
803                         }
804                 } else if (hfp_hs_restricted == 0x01) { /* in case of hfp_hs is restricted, only connection for a2dp */
805                         if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
806                                 uuid = A2DP_SINK_UUID;
807                                 type = BT_AUDIO_A2DP;
808                         } else {
809                                 BT_ERR("A2DP role is not supported");
810                                 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
811                                 goto fail;
812                         }
813                 }
814                 break;
815         case BT_AVRCP_TARGET:
816                 uuid = AVRCP_REMOTE_UUID;
817                 break;
818         default:
819                 BT_ERR("Unknown role");
820                 result = BLUETOOTH_ERROR_INTERNAL;
821                 goto fail;
822         }
823         BT_INFO("Connecting to service %s", uuid);
824
825         value = __bt_is_headset_connected(type, request_id, address);
826
827         if (value == BLUETOOTH_ERROR_ALREADY_CONNECT) {
828                 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
829                 goto fail;
830         } else if (value == BLUETOOTH_ERROR_NOT_CONNECTED) {
831                 value = __bt_is_headset_connecting(type);
832                 if (value != BLUETOOTH_ERROR_NONE) {
833                         result = BLUETOOTH_ERROR_IN_PROGRESS;
834                         goto fail;
835                 }
836                 _bt_headset_set_local_connection(TRUE);
837                 ret = _bt_connect_profile(address, uuid,
838                                 __bt_audio_request_cb, func_data);
839
840                 if (ret != BLUETOOTH_ERROR_NONE) {
841                         BT_ERR("_bt_connect_profile Error");
842                         _bt_headset_set_local_connection(FALSE);
843                         g_free(func_data->address);
844                         g_free(func_data);
845                         return ret;
846                 }
847
848                 /* Add data to the connected list */
849                 _bt_add_headset_to_list(type, BT_STATE_CONNECTING, address);
850         } else if (value == BLUETOOTH_ERROR_IN_PROGRESS) {
851                 result = BLUETOOTH_ERROR_IN_PROGRESS;
852                 goto fail;
853         }
854
855         if (out_param1)
856                 g_array_free(out_param1, TRUE);
857
858         return BLUETOOTH_ERROR_NONE;
859 fail:
860         if (out_param1 != NULL)
861                 g_array_append_vals(out_param1, address,
862                                 BT_ADDRESS_STR_LEN);
863
864         g_free(func_data->address);
865         g_free(func_data);
866
867         return result;
868 }
869
870 int _bt_audio_disconnect(int request_id, int type,
871                 bluetooth_device_address_t *device_address,
872                 GArray *out_param1)
873 {
874         int result = BLUETOOTH_ERROR_NONE;
875         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
876         bt_audio_function_data_t *func_data;
877         GDBusProxy *adapter_proxy;
878         GDBusConnection *g_conn;
879         GList *node;
880         int ret;
881         char *uuid;
882         int value = BLUETOOTH_ERROR_NONE;
883
884         BT_CHECK_PARAMETER(device_address, return);
885
886         adapter_proxy = _bt_get_adapter_proxy();
887         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
888
889         g_conn = _bt_gdbus_get_system_gconn();
890         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
891
892         _bt_convert_addr_type_to_string(address, device_address->addr);
893
894         func_data = g_malloc0(sizeof(bt_audio_function_data_t));
895         func_data->address = g_strdup(address);
896         func_data->req_id = request_id;
897         func_data->pending = BT_PENDING_NONE;
898         func_data->type = type;
899
900         switch (type) {
901         case BT_AUDIO_HSP:
902                 uuid = HFP_HS_UUID;
903                 break;
904         case BT_AUDIO_A2DP:
905                 uuid = A2DP_SINK_UUID;
906                 break;
907         case BT_AVRCP:
908                 uuid = AVRCP_TARGET_UUID;
909                 break;
910         case BT_AUDIO_A2DP_SOURCE:
911                 uuid = A2DP_SOURCE_UUID;
912                 break;
913         case BT_AUDIO_ALL:
914                 if (_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
915                         uuid = A2DP_SINK_UUID;
916                         func_data->pending = BT_PENDING_DISCONNECT;
917                 } else if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
918                         uuid = HFP_HS_UUID;
919                 } else {
920                         BT_ERR("No audio service connected");
921                         result = BLUETOOTH_ERROR_NOT_CONNECTED;
922                         goto fail;
923                 }
924                 break;
925         case BT_AVRCP_TARGET:
926                 uuid = AVRCP_REMOTE_UUID;
927                 break;
928         default:
929                 BT_ERR("Unknown role");
930                 result = BLUETOOTH_ERROR_INTERNAL;
931                 goto fail;
932         }
933         value = __bt_is_headset_disconnecting(type);
934         if (value != BLUETOOTH_ERROR_NONE) {
935                 BT_INFO("Disconnect in progress");
936                 result = BLUETOOTH_ERROR_IN_PROGRESS;
937                 goto fail;
938         }
939         BT_INFO("Disconnecting service %s", uuid);
940         ret = _bt_disconnect_profile(address, uuid,
941                         __bt_audio_request_cb, func_data);
942
943         if (ret != BLUETOOTH_ERROR_NONE) {
944                 BT_ERR("_bt_disconnect_profile Error");
945                 g_free(func_data->address);
946                 g_free(func_data);
947                 return ret;
948         }
949
950         /*
951          *      This logic is added for dual HF mode issue.
952          */
953         node = g_list_first(g_connected_list);
954         while (node != NULL) {
955                 bt_connected_headset_data_t *connected_device = node->data;
956
957                 if (g_strcmp0(connected_device->device_address, address) == 0) {
958                         BT_DBG("Connection type update");
959                         type = connected_device->type;
960                         break;
961                 }
962                 node = g_list_next(node);
963         }
964         _bt_add_headset_to_list(type, BT_STATE_DISCONNECTING, address);
965
966         if (out_param1)
967                 g_array_free(out_param1, TRUE);
968
969         return BLUETOOTH_ERROR_NONE;
970 fail:
971         if (out_param1 != NULL)
972                 g_array_append_vals(out_param1, address,
973                                 BT_ADDRESS_STR_LEN);
974
975         g_free(func_data->address);
976         g_free(func_data);
977
978         return result;
979 }
980
981 void _bt_remove_from_connected_list(const char *address)
982 {
983         bt_connected_headset_data_t *connected_device;
984         GList *node;
985
986         node = g_list_first(g_connected_list);
987         while (node != NULL) {
988                 connected_device = node->data;
989                 if (connected_device != NULL &&
990                 g_strcmp0(connected_device->device_address, address) == 0) {
991                         BT_ERR("Device is removed from the list");
992                         g_connected_list = g_list_remove(g_connected_list, connected_device);
993                         g_free(connected_device);
994                         return;
995                 }
996                 node = g_list_next(node);
997         }
998 }
999
1000 int _bt_hf_connect(int request_id,
1001                 bluetooth_device_address_t *device_address,
1002                 GArray *out_param1)
1003 {
1004         int result = BLUETOOTH_ERROR_NONE;
1005         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1006         bt_function_data_t *func_data;
1007         GDBusProxy *adapter_proxy;
1008         GDBusConnection *g_conn;
1009         gboolean connected = FALSE;
1010         int ret;
1011         char *uuid;
1012
1013         BT_CHECK_PARAMETER(device_address, return);
1014
1015         _bt_convert_addr_type_to_string(address, device_address->addr);
1016
1017         adapter_proxy = _bt_get_adapter_proxy();
1018         if (adapter_proxy == NULL) {
1019                 result = BLUETOOTH_ERROR_INTERNAL;
1020                 goto fail;
1021         }
1022
1023         g_conn = _bt_gdbus_get_system_gconn();
1024         if (g_conn == NULL) {
1025                 result = BLUETOOTH_ERROR_INTERNAL;
1026                 goto fail;
1027         }
1028
1029         /* Check if HF is connected or not */
1030         result = _bt_is_device_connected(device_address, BT_PROFILE_CONN_HFG, &connected);
1031         if (connected == TRUE) {
1032                 BT_ERR("HF is already connected");
1033                 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
1034                 goto fail;
1035         }
1036
1037         func_data = g_malloc0(sizeof(bt_function_data_t));
1038         func_data->address = g_strdup(address);
1039         func_data->req_id = request_id;
1040         uuid = g_strdup(HFP_AG_UUID);
1041
1042         BT_DBG("Connecting to service %s", uuid);
1043
1044         ret = _bt_connect_profile(address, uuid,
1045                         __bt_hf_request_cb, func_data);
1046
1047         if (ret != BLUETOOTH_ERROR_NONE) {
1048                 BT_ERR("_bt_connect_profile Error");
1049                 g_free(func_data->address);
1050                 g_free(func_data);
1051                 g_free(uuid);
1052                 return ret;
1053         }
1054         g_free(uuid);
1055         if (out_param1)
1056                 g_array_free(out_param1, TRUE);
1057
1058         return BLUETOOTH_ERROR_NONE;
1059 fail:
1060         if (out_param1 != NULL)
1061                 g_array_append_vals(out_param1, address,
1062                                 BT_ADDRESS_STR_LEN);
1063
1064         return result;
1065 }
1066
1067 int _bt_hf_disconnect(int request_id,
1068                 bluetooth_device_address_t *device_address,
1069                 GArray *out_param1)
1070 {
1071         int result = BLUETOOTH_ERROR_NONE;
1072         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1073         bt_function_data_t *func_data;
1074         GDBusProxy *adapter_proxy;
1075         GDBusConnection *g_conn;
1076
1077         int ret;
1078         char *uuid;
1079
1080         BT_CHECK_PARAMETER(device_address, return);
1081
1082         _bt_convert_addr_type_to_string(address, device_address->addr);
1083
1084         adapter_proxy = _bt_get_adapter_proxy();
1085         if (adapter_proxy == NULL) {
1086                 result = BLUETOOTH_ERROR_INTERNAL;
1087                 goto fail;
1088         }
1089
1090         g_conn = _bt_gdbus_get_system_gconn();
1091         if (g_conn == NULL) {
1092                 result = BLUETOOTH_ERROR_INTERNAL;
1093                 goto fail;
1094         }
1095
1096         func_data = g_malloc0(sizeof(bt_function_data_t));
1097         func_data->address = g_strdup(address);
1098         func_data->req_id = request_id;
1099         uuid = g_strdup(HFP_AG_UUID);
1100
1101         BT_DBG("Disconnecting service %s", uuid);
1102         ret = _bt_disconnect_profile(address, uuid,
1103                         __bt_hf_request_cb, func_data);
1104
1105         if (ret != BLUETOOTH_ERROR_NONE) {
1106                 BT_ERR("_bt_disconnect_profile Error");
1107                 g_free(func_data->address);
1108                 g_free(func_data);
1109                 g_free(uuid);
1110                 return ret;
1111         }
1112         g_free(uuid);
1113         if (out_param1)
1114                 g_array_free(out_param1, TRUE);
1115
1116         return BLUETOOTH_ERROR_NONE;
1117 fail:
1118         if (out_param1 != NULL)
1119                 g_array_append_vals(out_param1, address,
1120                                 BT_ADDRESS_STR_LEN);
1121
1122         return result;
1123 }
1124
1125 int _bt_audio_set_content_protect(gboolean status)
1126 {
1127         GDBusConnection *conn;
1128         GError *error = NULL;
1129
1130         BT_DBG("+\n");
1131
1132         conn = _bt_gdbus_get_system_gconn();
1133         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1134
1135         BT_DBG("Content Protection status = [%d]", status);
1136
1137         g_dbus_connection_emit_signal(conn,
1138                         NULL, BT_CONTENT_PROTECTION_PATH,
1139                         BT_CONTENT_PROTECTION_INTERFACE,
1140                         "ProtectionRequired",
1141                         g_variant_new("(b)", status),
1142                         &error);
1143
1144         if (error) {
1145                 /* dBUS gives error cause */
1146                 ERR("Could not Emit Signal: errCode[%x], message[%s]",
1147                         error->code, error->message);
1148                 g_clear_error(&error);
1149                 return BLUETOOTH_ERROR_INTERNAL;
1150         }
1151
1152         BT_DBG("Emit Signal done = [ProtectionRequired]");
1153         return BLUETOOTH_ERROR_NONE;
1154 }
1155
1156 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1157 static void __bt_auto_connect_request_cb(GDBusProxy *proxy, GAsyncResult *res,
1158                                     gpointer user_data)
1159 {
1160         GError *err = NULL;
1161         GVariant *reply = NULL;
1162
1163         reply = g_dbus_proxy_call_finish(proxy, res, &err);
1164         g_object_unref(proxy);
1165         g_variant_unref(reply);
1166
1167         BT_DBG("+");
1168         if (err != NULL) {
1169                 g_dbus_error_strip_remote_error(err);
1170                 BT_INFO("Auto_connect_request Dbus Call Error: %s", err->message);
1171
1172                 if (strcmp("Host is down", err->message)) {
1173                         BT_INFO("Fail reason is not 'Host Down'. Terminate auto connect");
1174                         _bt_audio_stop_auto_connect();
1175                         vconf_set_str(BT_LAST_CONNECTED_DEVICE, "");
1176                 }
1177         }
1178         g_clear_error(&err);
1179 }
1180
1181
1182 // It is the function that retry to create alarm.
1183 // Sometimes alarm mgr service is created later than bluetooth-frwk service in booting time.
1184 // So, in this case, we have to retry alarmmgr_add_alarm.
1185 static gboolean __bt_audio_alarm_retry_cb(gpointer data)
1186 {
1187         int result;
1188         alarm_id_t alarm_id;
1189
1190         BT_DBG("__bt_audio_alarm_retry_cb called.");
1191         result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1192                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1193
1194         if (result != BLUETOOTH_ERROR_NONE)
1195                 return TRUE;
1196         else
1197                 auto_connect_timer.alarm_id = alarm_id;
1198
1199         return FALSE;
1200 }
1201
1202 static int __bt_auto_connect_alarm_cb(alarm_id_t alarm_id, void* user_param)
1203 {
1204         int result = BLUETOOTH_ERROR_NONE;
1205         char *address = NULL;
1206         alarm_id_t new_alarm_id;
1207
1208         BT_INFO("alram id = [%d] , retry_count [%d] ",
1209                                                 alarm_id, auto_connect_timer.retry_count);
1210
1211         if (alarm_id != auto_connect_timer.alarm_id)
1212                 return 0;
1213
1214         address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1215
1216         if (address == NULL) {
1217                 BT_ERR("Address vconf is null");
1218                 auto_connect_timer.alarm_id = 0;
1219                 return 0;
1220         }
1221
1222         BT_DBG("ADDRESS [%s]", address);
1223
1224         result = _bt_connect_profile(address, A2DP_SOURCE_UUID,
1225                         __bt_auto_connect_request_cb, NULL);
1226
1227         auto_connect_timer.retry_count--;
1228
1229         BT_DBG("result [%d]", result);
1230
1231         if (auto_connect_timer.retry_count <= 0) {
1232                 BT_INFO("Stopping Auto connect retry");
1233                 auto_connect_timer.alarm_id = 0;
1234         } else {
1235                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1236                                                 __bt_auto_connect_alarm_cb, NULL, &new_alarm_id);
1237                 if (result == BLUETOOTH_ERROR_NONE)
1238                         auto_connect_timer.alarm_id = new_alarm_id;
1239         }
1240
1241         free(address);
1242         return 0;
1243 }
1244
1245 static void __bt_auto_connect_alarm_remove()
1246 {
1247         BT_DBG("");
1248         if (auto_connect_timer.alarm_id > 0) {
1249                 _bt_service_remove_alarm(auto_connect_timer.alarm_id);
1250                 auto_connect_timer.alarm_id = 0;
1251         }
1252         auto_connect_timer.retry_count = 0;
1253 }
1254
1255 int _bt_audio_start_auto_connect(gboolean linkloss_flag)
1256 {
1257         int result = BLUETOOTH_ERROR_NONE;
1258         char *address = NULL;
1259         alarm_id_t alarm_id = 0;
1260
1261         BT_DBG("");
1262
1263         address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1264
1265         if (address == NULL) {
1266                 BT_ERR("No target device");
1267                 return 0;
1268         }
1269
1270         __bt_auto_connect_alarm_remove();
1271
1272         auto_connect_timer.retry_count =  BT_AUTO_CONNECT_TIMEOUT_RETRY_TIME /
1273                                                 BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS;
1274
1275         if (linkloss_flag) {
1276                 BT_INFO("Start auto connection after linkloss");
1277                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1278                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1279                 if (result != BLUETOOTH_ERROR_NONE)
1280                         g_timeout_add(500, (GSourceFunc)__bt_audio_alarm_retry_cb, NULL);
1281                 else
1282                         auto_connect_timer.alarm_id = alarm_id;
1283
1284         } else {
1285                 BT_INFO("Start auto connection after BT activated");
1286                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_BT_ACTIVATED,
1287                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1288                 if (result == BLUETOOTH_ERROR_NONE)
1289                         auto_connect_timer.alarm_id = alarm_id;
1290         }
1291
1292         free(address);
1293         return BLUETOOTH_ERROR_NONE;
1294 }
1295
1296 int _bt_audio_stop_auto_connect(void)
1297 {
1298         BT_DBG("");
1299         __bt_auto_connect_alarm_remove();
1300
1301         return BLUETOOTH_ERROR_NONE;
1302 }
1303
1304 void _bt_audio_set_auto_connect_device_addr(const char *address)
1305 {
1306         if (address == NULL) {
1307                 BT_ERR("address is null");
1308                 return;
1309         }
1310
1311         BT_INFO("Last connected device is [%s]", address);
1312         vconf_set_str(BT_LAST_CONNECTED_DEVICE, address);
1313 }
1314 #endif /*TIZEN_BT_A2DP_SINK_AUTO_CONNECT */