A2DP role change: framework API.
[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                 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
823                 goto fail;
824         } else if (value == BLUETOOTH_ERROR_NOT_CONNECTED) {
825                 value = __bt_is_headset_connecting(type);
826                 if (value != BLUETOOTH_ERROR_NONE) {
827                         result = BLUETOOTH_ERROR_IN_PROGRESS;
828                         goto fail;
829                 }
830                 _bt_headset_set_local_connection(TRUE);
831                 ret = _bt_connect_profile(address, uuid,
832                                 __bt_audio_request_cb, func_data);
833
834                 if (ret != BLUETOOTH_ERROR_NONE) {
835                         BT_ERR("_bt_connect_profile Error");
836                         _bt_headset_set_local_connection(FALSE);
837                         g_free(func_data->address);
838                         g_free(func_data);
839                         return ret;
840                 }
841
842                 /* Add data to the connected list */
843                 _bt_add_headset_to_list(type, BT_STATE_CONNECTING, address);
844         } else if (value == BLUETOOTH_ERROR_IN_PROGRESS) {
845                 result = BLUETOOTH_ERROR_IN_PROGRESS;
846                 goto fail;
847         }
848
849         if (out_param1)
850                 g_array_free(out_param1, TRUE);
851
852         return BLUETOOTH_ERROR_NONE;
853 fail:
854         if (out_param1 != NULL)
855                 g_array_append_vals(out_param1, address,
856                                 BT_ADDRESS_STR_LEN);
857
858         g_free(func_data->address);
859         g_free(func_data);
860
861         return result;
862 }
863
864 int _bt_audio_disconnect(int request_id, int type,
865                 bluetooth_device_address_t *device_address,
866                 GArray *out_param1)
867 {
868         int result = BLUETOOTH_ERROR_NONE;
869         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
870         bt_audio_function_data_t *func_data;
871         GDBusProxy *adapter_proxy;
872         GDBusConnection *g_conn;
873         GList *node;
874         int ret;
875         char *uuid;
876         int value = BLUETOOTH_ERROR_NONE;
877
878         BT_CHECK_PARAMETER(device_address, return);
879
880         adapter_proxy = _bt_get_adapter_proxy();
881         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
882
883         g_conn = _bt_gdbus_get_system_gconn();
884         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
885
886         _bt_convert_addr_type_to_string(address, device_address->addr);
887
888         func_data = g_malloc0(sizeof(bt_audio_function_data_t));
889         func_data->address = g_strdup(address);
890         func_data->req_id = request_id;
891         func_data->pending = BT_PENDING_NONE;
892         func_data->type = type;
893
894         switch (type) {
895         case BT_AUDIO_HSP:
896                 uuid = HFP_HS_UUID;
897                 break;
898         case BT_AUDIO_A2DP:
899                 uuid = A2DP_SINK_UUID;
900                 break;
901         case BT_AVRCP:
902                 uuid = AVRCP_TARGET_UUID;
903                 break;
904         case BT_AUDIO_A2DP_SOURCE:
905                 uuid = A2DP_SOURCE_UUID;
906                 break;
907         case BT_AUDIO_ALL:
908                 if (_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
909                         uuid = A2DP_SINK_UUID;
910                         func_data->pending = BT_PENDING_DISCONNECT;
911                 } else if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
912                         uuid = HFP_HS_UUID;
913                 } else {
914                         BT_ERR("No audio service connected");
915                         result = BLUETOOTH_ERROR_NOT_CONNECTED;
916                         goto fail;
917                 }
918                 break;
919         case BT_AVRCP_TARGET:
920                 uuid = AVRCP_REMOTE_UUID;
921                 break;
922         default:
923                 BT_ERR("Unknown role");
924                 result = BLUETOOTH_ERROR_INTERNAL;
925                 goto fail;
926         }
927         value = __bt_is_headset_disconnecting(type);
928         if (value != BLUETOOTH_ERROR_NONE) {
929                 BT_INFO("Disconnect in progress");
930                 result = BLUETOOTH_ERROR_IN_PROGRESS;
931                 goto fail;
932         }
933         BT_INFO("Disconnecting service %s", uuid);
934         ret = _bt_disconnect_profile(address, uuid,
935                         __bt_audio_request_cb, func_data);
936
937         if (ret != BLUETOOTH_ERROR_NONE) {
938                 BT_ERR("_bt_disconnect_profile Error");
939                 g_free(func_data->address);
940                 g_free(func_data);
941                 return ret;
942         }
943
944         /*
945          *      This logic is added for dual HF mode issue.
946          */
947         node = g_list_first(g_connected_list);
948         while (node != NULL) {
949                 bt_connected_headset_data_t *connected_device = node->data;
950
951                 if (g_strcmp0(connected_device->device_address, address) == 0) {
952                         BT_DBG("Connection type update");
953                         type = connected_device->type;
954                         break;
955                 }
956                 node = g_list_next(node);
957         }
958         _bt_add_headset_to_list(type, BT_STATE_DISCONNECTING, address);
959
960         if (out_param1)
961                 g_array_free(out_param1, TRUE);
962
963         return BLUETOOTH_ERROR_NONE;
964 fail:
965         if (out_param1 != NULL)
966                 g_array_append_vals(out_param1, address,
967                                 BT_ADDRESS_STR_LEN);
968
969         g_free(func_data->address);
970         g_free(func_data);
971
972         return result;
973 }
974
975 void _bt_remove_from_connected_list(const char *address)
976 {
977         bt_connected_headset_data_t *connected_device;
978         GList *node;
979
980         node = g_list_first(g_connected_list);
981         while (node != NULL) {
982                 connected_device = node->data;
983                 if (connected_device != NULL &&
984                 g_strcmp0(connected_device->device_address, address) == 0) {
985                         BT_ERR("Device is removed from the list");
986                         g_connected_list = g_list_remove(g_connected_list, connected_device);
987                         g_free(connected_device);
988                         return;
989                 }
990                 node = g_list_next(node);
991         }
992 }
993
994 int _bt_hf_connect(int request_id,
995                 bluetooth_device_address_t *device_address,
996                 GArray *out_param1)
997 {
998         int result = BLUETOOTH_ERROR_NONE;
999         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1000         bt_function_data_t *func_data;
1001         GDBusProxy *adapter_proxy;
1002         GDBusConnection *g_conn;
1003         gboolean connected = FALSE;
1004         int ret;
1005         char *uuid;
1006
1007         BT_CHECK_PARAMETER(device_address, return);
1008
1009         _bt_convert_addr_type_to_string(address, device_address->addr);
1010
1011         adapter_proxy = _bt_get_adapter_proxy();
1012         if (adapter_proxy == NULL) {
1013                 result = BLUETOOTH_ERROR_INTERNAL;
1014                 goto fail;
1015         }
1016
1017         g_conn = _bt_gdbus_get_system_gconn();
1018         if (g_conn == NULL) {
1019                 result = BLUETOOTH_ERROR_INTERNAL;
1020                 goto fail;
1021         }
1022
1023         /* Check if HF is connected or not */
1024         result = _bt_is_device_connected(device_address, BT_PROFILE_CONN_HFG, &connected);
1025         if (connected == TRUE) {
1026                 BT_ERR("HF is already connected");
1027                 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
1028                 goto fail;
1029         }
1030
1031         func_data = g_malloc0(sizeof(bt_function_data_t));
1032         func_data->address = g_strdup(address);
1033         func_data->req_id = request_id;
1034         uuid = g_strdup(HFP_AG_UUID);
1035
1036         BT_DBG("Connecting to service %s", uuid);
1037
1038         ret = _bt_connect_profile(address, uuid,
1039                         __bt_hf_request_cb, func_data);
1040
1041         if (ret != BLUETOOTH_ERROR_NONE) {
1042                 BT_ERR("_bt_connect_profile Error");
1043                 g_free(func_data->address);
1044                 g_free(func_data);
1045                 g_free(uuid);
1046                 return ret;
1047         }
1048         g_free(uuid);
1049         if (out_param1)
1050                 g_array_free(out_param1, TRUE);
1051
1052         return BLUETOOTH_ERROR_NONE;
1053 fail:
1054         if (out_param1 != NULL)
1055                 g_array_append_vals(out_param1, address,
1056                                 BT_ADDRESS_STR_LEN);
1057
1058         return result;
1059 }
1060
1061 int _bt_hf_disconnect(int request_id,
1062                 bluetooth_device_address_t *device_address,
1063                 GArray *out_param1)
1064 {
1065         int result = BLUETOOTH_ERROR_NONE;
1066         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1067         bt_function_data_t *func_data;
1068         GDBusProxy *adapter_proxy;
1069         GDBusConnection *g_conn;
1070
1071         int ret;
1072         char *uuid;
1073
1074         BT_CHECK_PARAMETER(device_address, return);
1075
1076         _bt_convert_addr_type_to_string(address, device_address->addr);
1077
1078         adapter_proxy = _bt_get_adapter_proxy();
1079         if (adapter_proxy == NULL) {
1080                 result = BLUETOOTH_ERROR_INTERNAL;
1081                 goto fail;
1082         }
1083
1084         g_conn = _bt_gdbus_get_system_gconn();
1085         if (g_conn == NULL) {
1086                 result = BLUETOOTH_ERROR_INTERNAL;
1087                 goto fail;
1088         }
1089
1090         func_data = g_malloc0(sizeof(bt_function_data_t));
1091         func_data->address = g_strdup(address);
1092         func_data->req_id = request_id;
1093         uuid = g_strdup(HFP_AG_UUID);
1094
1095         BT_DBG("Disconnecting service %s", uuid);
1096         ret = _bt_disconnect_profile(address, uuid,
1097                         __bt_hf_request_cb, func_data);
1098
1099         if (ret != BLUETOOTH_ERROR_NONE) {
1100                 BT_ERR("_bt_disconnect_profile Error");
1101                 g_free(func_data->address);
1102                 g_free(func_data);
1103                 g_free(uuid);
1104                 return ret;
1105         }
1106         g_free(uuid);
1107         if (out_param1)
1108                 g_array_free(out_param1, TRUE);
1109
1110         return BLUETOOTH_ERROR_NONE;
1111 fail:
1112         if (out_param1 != NULL)
1113                 g_array_append_vals(out_param1, address,
1114                                 BT_ADDRESS_STR_LEN);
1115
1116         return result;
1117 }
1118
1119 int _bt_audio_set_content_protect(gboolean status)
1120 {
1121         GDBusConnection *conn;
1122         GError *error = NULL;
1123
1124         BT_DBG("+\n");
1125
1126         conn = _bt_gdbus_get_system_gconn();
1127         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1128
1129         BT_DBG("Content Protection status = [%d]", status);
1130
1131         g_dbus_connection_emit_signal(conn,
1132                         NULL, BT_CONTENT_PROTECTION_PATH,
1133                         BT_CONTENT_PROTECTION_INTERFACE,
1134                         "ProtectionRequired",
1135                         g_variant_new("(b)", status),
1136                         &error);
1137
1138         if (error) {
1139                 /* dBUS gives error cause */
1140                 ERR("Could not Emit Signal: errCode[%x], message[%s]",
1141                         error->code, error->message);
1142                 g_clear_error(&error);
1143                 return BLUETOOTH_ERROR_INTERNAL;
1144         }
1145
1146         BT_DBG("Emit Signal done = [ProtectionRequired]");
1147         return BLUETOOTH_ERROR_NONE;
1148 }
1149
1150 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1151 static void __bt_auto_connect_request_cb(GDBusProxy *proxy, GAsyncResult *res,
1152                                     gpointer user_data)
1153 {
1154         GError *err = NULL;
1155         GVariant *reply = NULL;
1156
1157         reply = g_dbus_proxy_call_finish(proxy, res, &err);
1158         g_object_unref(proxy);
1159         g_variant_unref(reply);
1160
1161         BT_DBG("+");
1162         if (err != NULL) {
1163                 g_dbus_error_strip_remote_error(err);
1164                 BT_INFO("Auto_connect_request Dbus Call Error: %s", err->message);
1165
1166                 if (strcmp("Host is down", err->message)) {
1167                         BT_INFO("Fail reason is not 'Host Down'. Terminate auto connect");
1168                         _bt_audio_stop_auto_connect();
1169                         vconf_set_str(BT_LAST_CONNECTED_DEVICE, "");
1170                 }
1171         }
1172         g_clear_error(&err);
1173 }
1174
1175
1176 // It is the function that retry to create alarm.
1177 // Sometimes alarm mgr service is created later than bluetooth-frwk service in booting time.
1178 // So, in this case, we have to retry alarmmgr_add_alarm.
1179 static gboolean __bt_audio_alarm_retry_cb(gpointer data)
1180 {
1181         int result;
1182         alarm_id_t alarm_id;
1183
1184         BT_DBG("__bt_audio_alarm_retry_cb called.");
1185         result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1186                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1187
1188         if (result != BLUETOOTH_ERROR_NONE)
1189                 return TRUE;
1190         else
1191                 auto_connect_timer.alarm_id = alarm_id;
1192
1193         return FALSE;
1194 }
1195
1196 static int __bt_auto_connect_alarm_cb(alarm_id_t alarm_id, void* user_param)
1197 {
1198         int result = BLUETOOTH_ERROR_NONE;
1199         char *address = NULL;
1200         alarm_id_t new_alarm_id;
1201
1202         BT_INFO("alram id = [%d] , retry_count [%d] ",
1203                                                 alarm_id, auto_connect_timer.retry_count);
1204
1205         if (alarm_id != auto_connect_timer.alarm_id)
1206                 return 0;
1207
1208         address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1209
1210         if (address == NULL) {
1211                 BT_ERR("Address vconf is null");
1212                 auto_connect_timer.alarm_id = 0;
1213                 return 0;
1214         }
1215
1216         BT_DBG("ADDRESS [%s]", address);
1217
1218         result = _bt_connect_profile(address, A2DP_SOURCE_UUID,
1219                         __bt_auto_connect_request_cb, NULL);
1220
1221         auto_connect_timer.retry_count--;
1222
1223         BT_DBG("result [%d]", result);
1224
1225         if (auto_connect_timer.retry_count <= 0) {
1226                 BT_INFO("Stopping Auto connect retry");
1227                 auto_connect_timer.alarm_id = 0;
1228         } else {
1229                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1230                                                 __bt_auto_connect_alarm_cb, NULL, &new_alarm_id);
1231                 if (result == BLUETOOTH_ERROR_NONE)
1232                         auto_connect_timer.alarm_id = new_alarm_id;
1233         }
1234
1235         free(address);
1236         return 0;
1237 }
1238
1239 static void __bt_auto_connect_alarm_remove()
1240 {
1241         BT_DBG("");
1242         if (auto_connect_timer.alarm_id > 0) {
1243                 _bt_service_remove_alarm(auto_connect_timer.alarm_id);
1244                 auto_connect_timer.alarm_id = 0;
1245         }
1246         auto_connect_timer.retry_count = 0;
1247 }
1248
1249 int _bt_audio_start_auto_connect(gboolean linkloss_flag)
1250 {
1251         int result = BLUETOOTH_ERROR_NONE;
1252         char *address = NULL;
1253         alarm_id_t alarm_id = 0;
1254
1255         BT_DBG("");
1256
1257         address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1258
1259         if (address == NULL) {
1260                 BT_ERR("No target device");
1261                 return 0;
1262         }
1263
1264         __bt_auto_connect_alarm_remove();
1265
1266         auto_connect_timer.retry_count =  BT_AUTO_CONNECT_TIMEOUT_RETRY_TIME /
1267                                                 BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS;
1268
1269         if (linkloss_flag) {
1270                 BT_INFO("Start auto connection after linkloss");
1271                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1272                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1273                 if (result != BLUETOOTH_ERROR_NONE)
1274                         g_timeout_add(500, (GSourceFunc)__bt_audio_alarm_retry_cb, NULL);
1275                 else
1276                         auto_connect_timer.alarm_id = alarm_id;
1277
1278         } else {
1279                 BT_INFO("Start auto connection after BT activated");
1280                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_BT_ACTIVATED,
1281                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1282                 if (result == BLUETOOTH_ERROR_NONE)
1283                         auto_connect_timer.alarm_id = alarm_id;
1284         }
1285
1286         free(address);
1287         return BLUETOOTH_ERROR_NONE;
1288 }
1289
1290 int _bt_audio_stop_auto_connect(void)
1291 {
1292         BT_DBG("");
1293         __bt_auto_connect_alarm_remove();
1294
1295         return BLUETOOTH_ERROR_NONE;
1296 }
1297
1298 void _bt_audio_set_auto_connect_device_addr(const char *address)
1299 {
1300         if (address == NULL) {
1301                 BT_ERR("address is null");
1302                 return;
1303         }
1304
1305         BT_INFO("Last connected device is [%s]", address);
1306         vconf_set_str(BT_LAST_CONNECTED_DEVICE, address);
1307 }
1308 #endif /*TIZEN_BT_A2DP_SINK_AUTO_CONNECT */
1309
1310 int _bt_audio_select_role(bluetooth_audio_role_t role)
1311 {
1312
1313         GDBusProxy *proxy;
1314         GVariant *ret;
1315         GError *error = NULL;
1316         GDBusConnection *g_conn = _bt_gdbus_get_system_gconn();
1317         gchar *adapter_path = _bt_get_adapter_path();
1318
1319         BT_INFO("_bt_audio_select_role called [%d]", role);
1320
1321         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1322         BT_INFO("Adapter Path = %s", adapter_path);
1323
1324         proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1325                 BT_BLUEZ_NAME, adapter_path, BT_MEDIA_INTERFACE, NULL, &error);
1326
1327         g_free(adapter_path);
1328
1329         if (proxy == NULL) {
1330                 BT_ERR("Unable to create proxy");
1331
1332                 if (error) {
1333                         BT_ERR("Error: %s", error->message);
1334                         g_clear_error(&error);
1335                 }
1336
1337                 return BLUETOOTH_ERROR_INTERNAL;
1338         }
1339
1340         if (role == BLUETOOTH_A2DP_SOURCE)
1341                 ret = g_dbus_proxy_call_sync(proxy, "SelectRole", g_variant_new("(s)", "source"),
1342                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1343         else
1344                 ret = g_dbus_proxy_call_sync(proxy, "SelectRole", g_variant_new("(s)", "sink"),
1345                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1346
1347         g_object_unref(proxy);
1348
1349         if (ret == NULL) {
1350                 BT_ERR("Call SelectRole Failed");
1351                 if (error) {
1352                         BT_ERR("errCode[%x], message[%s]", error->code, error->message);
1353                         g_clear_error(&error);
1354                 }
1355
1356                 return BLUETOOTH_ERROR_INTERNAL;
1357         }
1358
1359         g_variant_unref(ret);
1360         return BLUETOOTH_ERROR_NONE;
1361 }