Fix memory leak
[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         if (g_error == NULL)
210                 goto dbus_return;
211
212         BT_ERR("Audio Connect/Disconnect Dbus Call Error: %s\n", g_error->message);
213
214         result = BLUETOOTH_ERROR_INTERNAL;
215         /* If there is error then we need to set local initiated connection as false
216           * If don't do this then when headset initiate connection for HFP then we don't
217           * initiate connection for A2dp to headset as this flag was set to true in first
218           * connection failure attempt and not set in error case.*/
219         _bt_headset_set_local_connection(FALSE);
220
221         /* Remove the device from the list */
222         _bt_remove_headset_from_list(func_data->type, func_data->address);
223
224         /* Error, check if any waiting device is there */
225         if (g_wait_data == NULL)
226                 goto dbus_return;
227
228         if (g_strcmp0(g_wait_data->address, func_data->address) != 0) {
229                 bluetooth_device_address_t device_address;
230                 _bt_convert_addr_string_to_type(device_address.addr,
231                                 g_wait_data->address);
232                 _bt_audio_connect(g_wait_data->req_id, g_wait_data->type,
233                                 &device_address, NULL);
234         }
235
236         /* Event will be sent by the event reciever */
237 dbus_return:
238         req_info = _bt_get_request_info(func_data->req_id);
239         if (req_info == NULL) {
240                 BT_ERR("req_info == NULL");
241                 goto done;
242         } else if (req_info->context == NULL) {
243                 BT_ERR("req_info->context is NULL");
244                 goto done;
245         }
246
247         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
248                 func_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
249
250         g_dbus_method_invocation_return_value(req_info->context,
251                         g_variant_new("(iv)", result, out_param1));
252
253         _bt_delete_request_list(req_info->req_id);
254 done:
255         g_clear_error(&g_error);
256
257         if (func_data) {
258                 g_free(func_data->address);
259                 g_free(func_data);
260         }
261 }
262
263 static void __bt_free_wait_data()
264 {
265         if (g_wait_data != NULL) {
266                 g_free(g_wait_data->address);
267                 g_free(g_wait_data);
268                 g_wait_data = NULL;
269         }
270 }
271
272 static void __bt_remove_device_from_wait_list()
273 {
274         /* Before deleting the request update the UI */
275         GVariant *out_param_1 = NULL;
276         int result = BLUETOOTH_ERROR_INTERNAL;
277         request_info_t *req_info;
278
279         req_info = _bt_get_request_info(g_wait_data->req_id);
280         if (req_info == NULL) {
281                 BT_ERR("req_info == NULL");
282                 return;
283         }
284
285         out_param_1 = g_variant_new_from_data((const GVariantType *)"ay",
286                 g_wait_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
287
288         g_dbus_method_invocation_return_value(req_info->context,
289                         g_variant_new("(iv)", result, out_param_1));
290
291         _bt_delete_request_list(g_wait_data->req_id);
292 }
293
294 static void __bt_set_headset_disconnection_type(const char *address)
295 {
296         bt_connected_headset_data_t *connected_device;
297         GList *node;
298
299         node = g_list_first(g_connected_list);
300         while (node != NULL) {
301                 connected_device = node->data;
302                 if (g_strcmp0(connected_device->device_address, address) == 0) {
303                         g_wait_data->disconnection_type = connected_device->type;
304                         return;
305                 }
306                 node = g_list_next(node);
307         }
308 }
309
310 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
311 void _bt_check_already_connected_headset(int type, char *address)
312 {
313         GList *node;
314         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
315         bluetooth_device_address_t device_address;
316         int device_count = 0;
317
318         if (address != NULL) {
319                 node = g_list_first(g_connected_list);
320                 while (node != NULL) {
321                         bt_connected_headset_data_t *connected_device = node->data;
322                         if ((connected_device->type & type) &&
323                                 (g_strcmp0(connected_device->device_address, address) != 0)) {
324                                 device_count++;
325                                 /* Disconnect the earliest(1st) connected headset */
326                                 if (device_count == 1) {
327                                         g_strlcpy(connected_address,
328                                                 connected_device->device_address,
329                                                 BT_ADDRESS_STRING_SIZE + 1);
330                                         BT_DBG("Earliest/First Connected headset %s",
331                                                 connected_address);
332                                 } else if (device_count == MAX_CONNECTED_HEADSET) {
333                                         _bt_convert_addr_string_to_type(device_address.addr,
334                                                 connected_address);
335                                         _bt_audio_disconnect(0, type, &device_address, NULL);
336                                         return;
337                                 }
338                         }
339                         node = g_list_next(node);
340                 }
341         }
342 }
343 #endif
344
345 gboolean _bt_is_headset_type_connected(int type, char *address)
346 {
347         GList *node;
348
349         node = g_list_first(g_connected_list);
350         while (node != NULL) {
351                 bt_connected_headset_data_t *connected_device = node->data;
352
353                 if (connected_device->type & type) {
354                         if (address != NULL)
355                                 g_strlcpy(address, connected_device->device_address,
356                                                 BT_ADDRESS_STRING_SIZE + 1);
357                         return TRUE;
358                 }
359
360                 node = g_list_next(node);
361         }
362         return FALSE;
363 }
364
365 #ifdef TIZEN_SUPPORT_DUAL_HF
366 gboolean __bt_is_companion_device(const char *addr)
367 {
368         if (TIZEN_PROFILE_WEARABLE) {
369                 char *host_device_address = NULL;
370                 host_device_address = vconf_get_str(VCONF_KEY_BT_HOST_BT_MAC_ADDR);
371
372                 if (!host_device_address) {
373                         BT_INFO("Failed to get a companion device address");
374                         return FALSE;
375                 }
376
377                 if (g_strcmp0(host_device_address, addr) == 0) {
378                         BT_INFO("Found companion device");
379                         free(host_device_address);
380                         return TRUE;
381                 }
382
383                 free(host_device_address);
384                 return FALSE;
385         } else {
386                 /* TODO : Need to add companion device check condition for Phone models */
387                 return FALSE;
388         }
389 }
390 #endif
391
392 static int __bt_is_headset_connected(int type, int req_id,
393                                 const char *address)
394 {
395         gboolean connected = FALSE;
396         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
397         bluetooth_device_address_t device_address;
398         bt_connected_headset_data_t *connected_device = NULL;
399 #ifdef TIZEN_SUPPORT_DUAL_HF
400         gboolean is_companion_device = FALSE;
401 #endif
402
403         /* Check if any other headset is connected */
404         GList *node = NULL;;
405
406         node = g_list_first(g_connected_list);
407         while (node != NULL) {
408                 connected_device = node->data;
409                 if ((connected_device->type & type)) {
410 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
411                         device_count++;
412                         /* Disconnect the earliest(1st) connected headset */
413                         if (device_count == 1)
414                                 g_strlcpy(connected_address, connected_device->device_address,
415                                         BT_ADDRESS_STRING_SIZE + 1);
416
417                         if (g_strcmp0(connected_device->device_address, address) == 0)
418                                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
419 #else
420                         g_strlcpy(connected_address, connected_device->device_address,
421                                         BT_ADDRESS_STRING_SIZE + 1);
422 #endif
423 #ifdef TIZEN_SUPPORT_DUAL_HF
424                         is_companion_device = __bt_is_companion_device(connected_address);
425                         BT_INFO(" is_companion_device[%d]", is_companion_device);
426
427                         if (!is_companion_device) {
428                                 connected = TRUE;
429                                 break;
430                         }
431 #else
432 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
433                         if (device_count == MAX_CONNECTED_HEADSET) {
434                                 connected = TRUE;
435                                 break;
436                         }
437 #else
438                         connected = TRUE;
439                         break;
440 #endif
441 #endif
442                 }
443                 node = g_list_next(node);
444         }
445
446         if (!connected) {
447                 __bt_free_wait_data();
448                 return BLUETOOTH_ERROR_NOT_CONNECTED;
449         }
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_trusted = TRUE; /* set default "allowed" */
732         guint a2dp_trusted = TRUE;
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_trust_profile(device_address, TRUSTED_PROFILE_HFP_HF, &hfp_hs_trusted);
751         if (result != BLUETOOTH_ERROR_NONE)
752                 BT_ERR("Can't get hfp_hs restriction info");
753
754         result = _bt_get_trust_profile(device_address, TRUSTED_PROFILE_A2DP, &a2dp_trusted);
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_trusted == TRUE) && (a2dp_trusted == TRUE)) { /* 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_trusted == FALSE) && (a2dp_trusted == FALSE)) { /* 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_trusted == FALSE) { /* 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_trusted == FALSE) { /* 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         /* First Check if device is in connecting state if it than return error */
819         value = __bt_is_headset_connecting(type);
820         if (value != BLUETOOTH_ERROR_NONE) {
821                 result = BLUETOOTH_ERROR_IN_PROGRESS;
822                 goto fail;
823         }
824
825         value = __bt_is_headset_connected(type, request_id, address);
826
827         if (value == BLUETOOTH_ERROR_ALREADY_CONNECT) {
828                 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
829                 goto fail;
830         } else if (value == BLUETOOTH_ERROR_NOT_CONNECTED) {
831                 _bt_headset_set_local_connection(TRUE);
832                 ret = _bt_connect_profile(address, uuid,
833                                 __bt_audio_request_cb, func_data);
834
835                 if (ret != BLUETOOTH_ERROR_NONE) {
836                         BT_ERR("_bt_connect_profile Error");
837                         _bt_headset_set_local_connection(FALSE);
838                         g_free(func_data->address);
839                         g_free(func_data);
840                         return ret;
841                 }
842
843                 /* Add data to the connected list */
844                 _bt_add_headset_to_list(type, BT_STATE_CONNECTING, address);
845         } else if (value == BLUETOOTH_ERROR_IN_PROGRESS) {
846                 result = BLUETOOTH_ERROR_IN_PROGRESS;
847                 goto fail;
848         }
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         g_free(func_data->address);
857         g_free(func_data);
858
859         return result;
860 }
861
862 int _bt_audio_disconnect(int request_id, int type,
863                 bluetooth_device_address_t *device_address,
864                 GArray *out_param1)
865 {
866         int result = BLUETOOTH_ERROR_NONE;
867         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
868         bt_audio_function_data_t *func_data;
869         GDBusProxy *adapter_proxy;
870         GDBusConnection *g_conn;
871         GList *node;
872         int ret;
873         char *uuid;
874         int value = BLUETOOTH_ERROR_NONE;
875
876         BT_CHECK_PARAMETER(device_address, return);
877
878         adapter_proxy = _bt_get_adapter_proxy();
879         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
880
881         g_conn = _bt_gdbus_get_system_gconn();
882         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
883
884         _bt_convert_addr_type_to_string(address, device_address->addr);
885
886         func_data = g_malloc0(sizeof(bt_audio_function_data_t));
887         func_data->address = g_strdup(address);
888         func_data->req_id = request_id;
889         func_data->pending = BT_PENDING_NONE;
890         func_data->type = type;
891
892         switch (type) {
893         case BT_AUDIO_HSP:
894                 uuid = HFP_HS_UUID;
895                 break;
896         case BT_AUDIO_A2DP:
897                 uuid = A2DP_SINK_UUID;
898                 break;
899         case BT_AVRCP:
900                 uuid = AVRCP_TARGET_UUID;
901                 break;
902         case BT_AUDIO_A2DP_SOURCE:
903                 uuid = A2DP_SOURCE_UUID;
904                 break;
905         case BT_AUDIO_ALL:
906                 if (_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
907                         uuid = A2DP_SINK_UUID;
908                         func_data->pending = BT_PENDING_DISCONNECT;
909                 } else if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
910                         uuid = HFP_HS_UUID;
911                 } else {
912                         BT_ERR("No audio service connected");
913                         result = BLUETOOTH_ERROR_NOT_CONNECTED;
914                         goto fail;
915                 }
916                 break;
917         case BT_AVRCP_TARGET:
918                 uuid = AVRCP_REMOTE_UUID;
919                 break;
920         default:
921                 BT_ERR("Unknown role");
922                 result = BLUETOOTH_ERROR_INTERNAL;
923                 goto fail;
924         }
925         value = __bt_is_headset_disconnecting(type);
926         if (value != BLUETOOTH_ERROR_NONE) {
927                 BT_INFO("Disconnect in progress");
928                 result = BLUETOOTH_ERROR_IN_PROGRESS;
929                 goto fail;
930         }
931         BT_INFO("Disconnecting service %s", uuid);
932         ret = _bt_disconnect_profile(address, uuid,
933                         __bt_audio_request_cb, func_data);
934
935         if (ret != BLUETOOTH_ERROR_NONE) {
936                 BT_ERR("_bt_disconnect_profile Error");
937                 g_free(func_data->address);
938                 g_free(func_data);
939                 return ret;
940         }
941
942         /*
943          *      This logic is added for dual HF mode issue.
944          */
945         node = g_list_first(g_connected_list);
946         while (node != NULL) {
947                 bt_connected_headset_data_t *connected_device = node->data;
948
949                 if (g_strcmp0(connected_device->device_address, address) == 0) {
950                         BT_DBG("Connection type update");
951                         type = connected_device->type;
952                         break;
953                 }
954                 node = g_list_next(node);
955         }
956         _bt_add_headset_to_list(type, BT_STATE_DISCONNECTING, address);
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         g_free(func_data->address);
965         g_free(func_data);
966
967         return result;
968 }
969
970 void _bt_remove_from_connected_list(const char *address)
971 {
972         bt_connected_headset_data_t *connected_device;
973         GList *node;
974
975         node = g_list_first(g_connected_list);
976         while (node != NULL) {
977                 connected_device = node->data;
978                 if (connected_device != NULL &&
979                 g_strcmp0(connected_device->device_address, address) == 0) {
980                         BT_ERR("Device is removed from the list");
981                         g_connected_list = g_list_remove(g_connected_list, connected_device);
982                         g_free(connected_device);
983                         return;
984                 }
985                 node = g_list_next(node);
986         }
987 }
988
989 int _bt_hf_connect(int request_id,
990                 bluetooth_device_address_t *device_address,
991                 GArray *out_param1)
992 {
993         int result = BLUETOOTH_ERROR_NONE;
994         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
995         bt_function_data_t *func_data;
996         GDBusProxy *adapter_proxy;
997         GDBusConnection *g_conn;
998         gboolean connected = FALSE;
999         int ret;
1000         char *uuid;
1001
1002         BT_CHECK_PARAMETER(device_address, return);
1003
1004         _bt_convert_addr_type_to_string(address, device_address->addr);
1005
1006         adapter_proxy = _bt_get_adapter_proxy();
1007         if (adapter_proxy == NULL) {
1008                 result = BLUETOOTH_ERROR_INTERNAL;
1009                 goto fail;
1010         }
1011
1012         g_conn = _bt_gdbus_get_system_gconn();
1013         if (g_conn == NULL) {
1014                 result = BLUETOOTH_ERROR_INTERNAL;
1015                 goto fail;
1016         }
1017
1018         /* Check if HF is connected or not */
1019         result = _bt_is_device_connected(device_address, BT_PROFILE_CONN_HFG, &connected);
1020         if (connected == TRUE) {
1021                 BT_ERR("HF is already connected");
1022                 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
1023                 goto fail;
1024         }
1025
1026         func_data = g_malloc0(sizeof(bt_function_data_t));
1027         func_data->address = g_strdup(address);
1028         func_data->req_id = request_id;
1029         uuid = g_strdup(HFP_AG_UUID);
1030
1031         BT_DBG("Connecting to service %s", uuid);
1032
1033         ret = _bt_connect_profile(address, uuid,
1034                         __bt_hf_request_cb, func_data);
1035
1036         if (ret != BLUETOOTH_ERROR_NONE) {
1037                 BT_ERR("_bt_connect_profile Error");
1038                 g_free(func_data->address);
1039                 g_free(func_data);
1040                 g_free(uuid);
1041                 return ret;
1042         }
1043         g_free(uuid);
1044         if (out_param1)
1045                 g_array_free(out_param1, TRUE);
1046
1047         return BLUETOOTH_ERROR_NONE;
1048 fail:
1049         if (out_param1 != NULL)
1050                 g_array_append_vals(out_param1, address,
1051                                 BT_ADDRESS_STR_LEN);
1052
1053         return result;
1054 }
1055
1056 int _bt_hf_disconnect(int request_id,
1057                 bluetooth_device_address_t *device_address,
1058                 GArray *out_param1)
1059 {
1060         int result = BLUETOOTH_ERROR_NONE;
1061         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1062         bt_function_data_t *func_data;
1063         GDBusProxy *adapter_proxy;
1064         GDBusConnection *g_conn;
1065
1066         int ret;
1067         char *uuid;
1068
1069         BT_CHECK_PARAMETER(device_address, return);
1070
1071         _bt_convert_addr_type_to_string(address, device_address->addr);
1072
1073         adapter_proxy = _bt_get_adapter_proxy();
1074         if (adapter_proxy == NULL) {
1075                 result = BLUETOOTH_ERROR_INTERNAL;
1076                 goto fail;
1077         }
1078
1079         g_conn = _bt_gdbus_get_system_gconn();
1080         if (g_conn == NULL) {
1081                 result = BLUETOOTH_ERROR_INTERNAL;
1082                 goto fail;
1083         }
1084
1085         func_data = g_malloc0(sizeof(bt_function_data_t));
1086         func_data->address = g_strdup(address);
1087         func_data->req_id = request_id;
1088         uuid = g_strdup(HFP_AG_UUID);
1089
1090         BT_DBG("Disconnecting service %s", uuid);
1091         ret = _bt_disconnect_profile(address, uuid,
1092                         __bt_hf_request_cb, func_data);
1093
1094         if (ret != BLUETOOTH_ERROR_NONE) {
1095                 BT_ERR("_bt_disconnect_profile Error");
1096                 g_free(func_data->address);
1097                 g_free(func_data);
1098                 g_free(uuid);
1099                 return ret;
1100         }
1101         g_free(uuid);
1102
1103         return BLUETOOTH_ERROR_NONE;
1104 fail:
1105         if (out_param1 != NULL)
1106                 g_array_append_vals(out_param1, address,
1107                                 BT_ADDRESS_STR_LEN);
1108
1109         return result;
1110 }
1111
1112 int _bt_audio_set_content_protect(gboolean status)
1113 {
1114         GDBusConnection *conn;
1115         GError *error = NULL;
1116
1117         BT_DBG("+\n");
1118
1119         conn = _bt_gdbus_get_system_gconn();
1120         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1121
1122         BT_DBG("Content Protection status = [%d]", status);
1123
1124         g_dbus_connection_emit_signal(conn,
1125                         NULL, BT_CONTENT_PROTECTION_PATH,
1126                         BT_CONTENT_PROTECTION_INTERFACE,
1127                         "ProtectionRequired",
1128                         g_variant_new("(b)", status),
1129                         &error);
1130
1131         if (error) {
1132                 /* dBUS gives error cause */
1133                 ERR("Could not Emit Signal: errCode[%x], message[%s]",
1134                         error->code, error->message);
1135                 g_clear_error(&error);
1136                 return BLUETOOTH_ERROR_INTERNAL;
1137         }
1138
1139         BT_DBG("Emit Signal done = [ProtectionRequired]");
1140         return BLUETOOTH_ERROR_NONE;
1141 }
1142
1143 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1144 static void __bt_auto_connect_request_cb(GDBusProxy *proxy, GAsyncResult *res,
1145                                     gpointer user_data)
1146 {
1147         GError *err = NULL;
1148         GVariant *reply = NULL;
1149
1150         reply = g_dbus_proxy_call_finish(proxy, res, &err);
1151         g_object_unref(proxy);
1152         g_variant_unref(reply);
1153
1154         BT_DBG("+");
1155         if (err != NULL) {
1156                 g_dbus_error_strip_remote_error(err);
1157                 BT_INFO("Auto_connect_request Dbus Call Error: %s", err->message);
1158
1159                 if (strcmp("Host is down", err->message)) {
1160                         BT_INFO("Fail reason is not 'Host Down'. Terminate auto connect");
1161                         _bt_audio_stop_auto_connect();
1162                         vconf_set_str(BT_LAST_CONNECTED_DEVICE, "");
1163                 }
1164         }
1165         g_clear_error(&err);
1166 }
1167
1168
1169 // It is the function that retry to create alarm.
1170 // Sometimes alarm mgr service is created later than bluetooth-frwk service in booting time.
1171 // So, in this case, we have to retry alarmmgr_add_alarm.
1172 static gboolean __bt_audio_alarm_retry_cb(gpointer data)
1173 {
1174         int result;
1175         alarm_id_t alarm_id;
1176
1177         BT_DBG("__bt_audio_alarm_retry_cb called.");
1178         result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1179                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1180
1181         if (result != BLUETOOTH_ERROR_NONE)
1182                 return TRUE;
1183         else
1184                 auto_connect_timer.alarm_id = alarm_id;
1185
1186         return FALSE;
1187 }
1188
1189 static int __bt_auto_connect_alarm_cb(alarm_id_t alarm_id, void* user_param)
1190 {
1191         int result = BLUETOOTH_ERROR_NONE;
1192         char *address = NULL;
1193         alarm_id_t new_alarm_id;
1194
1195         BT_INFO("alram id = [%d] , retry_count [%d] ",
1196                                                 alarm_id, auto_connect_timer.retry_count);
1197
1198         if (alarm_id != auto_connect_timer.alarm_id)
1199                 return 0;
1200
1201         address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1202
1203         if (address == NULL) {
1204                 BT_ERR("Address vconf is null");
1205                 auto_connect_timer.alarm_id = 0;
1206                 return 0;
1207         }
1208
1209         BT_DBG("ADDRESS [%s]", address);
1210
1211         result = _bt_connect_profile(address, A2DP_SOURCE_UUID,
1212                         __bt_auto_connect_request_cb, NULL);
1213
1214         auto_connect_timer.retry_count--;
1215
1216         BT_DBG("result [%d]", result);
1217
1218         if (auto_connect_timer.retry_count <= 0) {
1219                 BT_INFO("Stopping Auto connect retry");
1220                 auto_connect_timer.alarm_id = 0;
1221         } else {
1222                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1223                                                 __bt_auto_connect_alarm_cb, NULL, &new_alarm_id);
1224                 if (result == BLUETOOTH_ERROR_NONE)
1225                         auto_connect_timer.alarm_id = new_alarm_id;
1226         }
1227
1228         free(address);
1229         return 0;
1230 }
1231
1232 static void __bt_auto_connect_alarm_remove()
1233 {
1234         BT_DBG("");
1235         if (auto_connect_timer.alarm_id > 0) {
1236                 _bt_service_remove_alarm(auto_connect_timer.alarm_id);
1237                 auto_connect_timer.alarm_id = 0;
1238         }
1239         auto_connect_timer.retry_count = 0;
1240 }
1241
1242 int _bt_audio_start_auto_connect(gboolean linkloss_flag)
1243 {
1244         int result = BLUETOOTH_ERROR_NONE;
1245         char *address = NULL;
1246         alarm_id_t alarm_id = 0;
1247
1248         BT_DBG("");
1249
1250         address = vconf_get_str(BT_LAST_CONNECTED_DEVICE);
1251
1252         if (address == NULL) {
1253                 BT_ERR("No target device");
1254                 return 0;
1255         }
1256
1257         __bt_auto_connect_alarm_remove();
1258
1259         auto_connect_timer.retry_count =  BT_AUTO_CONNECT_TIMEOUT_RETRY_TIME /
1260                                                 BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS;
1261
1262         if (linkloss_flag) {
1263                 BT_INFO("Start auto connection after linkloss");
1264                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_LINKLOSS,
1265                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1266                 if (result != BLUETOOTH_ERROR_NONE)
1267                         g_timeout_add(500, (GSourceFunc)__bt_audio_alarm_retry_cb, NULL);
1268                 else
1269                         auto_connect_timer.alarm_id = alarm_id;
1270
1271         } else {
1272                 BT_INFO("Start auto connection after BT activated");
1273                 result = _bt_service_set_alarm(BT_AUTO_CONNECT_TIMEOUT_AFTER_BT_ACTIVATED,
1274                                                 __bt_auto_connect_alarm_cb, NULL, &alarm_id);
1275                 if (result == BLUETOOTH_ERROR_NONE)
1276                         auto_connect_timer.alarm_id = alarm_id;
1277         }
1278
1279         free(address);
1280         return BLUETOOTH_ERROR_NONE;
1281 }
1282
1283 int _bt_audio_stop_auto_connect(void)
1284 {
1285         BT_DBG("");
1286         __bt_auto_connect_alarm_remove();
1287
1288         return BLUETOOTH_ERROR_NONE;
1289 }
1290
1291 void _bt_audio_set_auto_connect_device_addr(const char *address)
1292 {
1293         if (address == NULL) {
1294                 BT_ERR("address is null");
1295                 return;
1296         }
1297
1298         BT_INFO("Last connected device is [%s]", address);
1299         vconf_set_str(BT_LAST_CONNECTED_DEVICE, address);
1300 }
1301 #endif /*TIZEN_BT_A2DP_SINK_AUTO_CONNECT */
1302
1303 int _bt_audio_select_role(bluetooth_audio_role_t role)
1304 {
1305
1306         GDBusProxy *proxy;
1307         GVariant *ret;
1308         GError *error = NULL;
1309         GDBusConnection *g_conn = _bt_gdbus_get_system_gconn();
1310         gchar *adapter_path = _bt_get_adapter_path();
1311
1312         BT_INFO("_bt_audio_select_role called [%d]", role);
1313
1314         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1315         BT_INFO("Adapter Path = %s", adapter_path);
1316
1317         proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1318                 BT_BLUEZ_NAME, adapter_path, BT_MEDIA_INTERFACE, NULL, &error);
1319
1320         g_free(adapter_path);
1321
1322         if (proxy == NULL) {
1323                 BT_ERR("Unable to create proxy");
1324
1325                 if (error) {
1326                         BT_ERR("Error: %s", error->message);
1327                         g_clear_error(&error);
1328                 }
1329
1330                 return BLUETOOTH_ERROR_INTERNAL;
1331         }
1332
1333         if (role == BLUETOOTH_A2DP_SOURCE)
1334                 ret = g_dbus_proxy_call_sync(proxy, "SelectRole", g_variant_new("(s)", "source"),
1335                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1336         else
1337                 ret = g_dbus_proxy_call_sync(proxy, "SelectRole", g_variant_new("(s)", "sink"),
1338                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1339
1340         g_object_unref(proxy);
1341
1342         if (ret == NULL) {
1343                 BT_ERR("Call SelectRole Failed");
1344                 if (error) {
1345                         BT_ERR("errCode[%x], message[%s]", error->code, error->message);
1346                         g_clear_error(&error);
1347                 }
1348
1349                 return BLUETOOTH_ERROR_INTERNAL;
1350         }
1351
1352         g_variant_unref(ret);
1353         return BLUETOOTH_ERROR_NONE;
1354 }