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