[Adapt Audio service] Implement HFP AG Role in bt-service
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / adapter / bt-service-core-adapter.c
1 /*
2  * Copyright (c) 2015 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Anupam Roy <anupam.r@samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <gio/gio.h>
22 #include <glib.h>
23 #include <dlog.h>
24 #include <string.h>
25 #include <vconf.h>
26 #include <vconf-internal-keys.h>
27 #include <syspopup_caller.h>
28 #include <aul.h>
29 #include <eventsystem.h>
30 #include <bundle_internal.h>
31
32 #include "alarm.h"
33
34 /*bt-service headers */
35 #include "bt-internal-types.h"
36 #include "bt-service-common.h"
37 #include "bt-service-util.h"
38 #include "bt-service-main.h"
39 #include "bt-service-core-adapter.h"
40 #include "bt-service-core-device.h"
41 #include "bt-service-event-receiver.h"
42 #include "bt-request-handler.h"
43 #include "bt-service-event.h"
44 #include "bt-service-audio-common.h"
45 #ifdef TIZEN_DPM_ENABLE
46 #include "bt-service-dpm.h"
47 #endif
48 #include "bt-service-hidhost.h"
49 #include "bt-service-socket.h"
50
51 /* OAL headers */
52 #include <oal-event.h>
53 #include <oal-manager.h>
54 #include <oal-adapter-mgr.h>
55
56 #define BT_ENABLE_TIMEOUT 20000 /* 20 seconds */
57
58 /*This file will contain state machines related to adapter and remote device */
59
60 /* Global variables */
61 typedef struct {
62         guint event_id;
63         int timeout;
64         time_t start_time;
65         gboolean alarm_init;
66         int alarm_id;
67 } bt_adapter_timer_t;
68
69 static bt_adapter_timer_t visible_timer;
70
71 static guint timer_id = 0;
72
73 /* Adapter default states */
74 static bt_status_t adapter_state = BT_DEACTIVATED;
75 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
76
77 /* Forward declarations */
78 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
79 static void __bt_post_oal_init(void);
80 static void __bt_handle_oal_initialisation(oal_event_t event);
81 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
82 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
83 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
84 static void __bt_adapter_update_bt_enabled(void);
85 static void __bt_adapter_update_bt_disabled(void);
86 static void __bt_adapter_state_set_status(bt_status_t status);
87 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
88 static void __bt_adapter_state_change_callback(int bt_status);
89 static int __bt_adapter_state_handle_request(gboolean enable);
90 static int __bt_adapter_state_discovery_request(gboolean enable);
91 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
92 static gboolean __bt_is_service_request_present(int service_function);
93 #ifdef TIZEN_MOBILE
94 static void __bt_set_visible_mode(void);
95 #endif
96 static void __bt_set_local_name(void);
97
98 /* Initialize BT stack (Initialize OAL layer) */
99 int _bt_stack_init(void)
100 {
101         int ret;
102
103         BT_INFO("[bt-service] Start to initialize BT stack");
104         /* Adapter enable request is successful, setup event handlers */
105         _bt_service_register_event_handler_callback(
106                         BT_ADAPTER_MODULE, __bt_adapter_event_handler);
107
108         ret = oal_bt_init(_bt_service_oal_event_receiver);
109
110         if (OAL_STATUS_PENDING == ret) {
111                 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
112                 return BLUETOOTH_ERROR_NONE;
113         } else if (OAL_STATUS_SUCCESS != ret) {
114                 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
115                 return BLUETOOTH_ERROR_INTERNAL;
116         }
117
118         __bt_post_oal_init();
119         return BLUETOOTH_ERROR_NONE;
120 }
121
122 int _bt_enable_adapter(void)
123 {
124         return __bt_adapter_state_handle_request(TRUE);
125 }
126
127 int _bt_disable_adapter(void)
128 {
129         return __bt_adapter_state_handle_request(FALSE);
130 }
131
132
133 int _bt_start_discovery(void)
134 {
135         return __bt_adapter_state_discovery_request(TRUE);
136 }
137
138 int _bt_cancel_discovery(void)
139 {
140         return __bt_adapter_state_discovery_request(FALSE);
141 }
142
143 gboolean _bt_is_discovering(void)
144 {
145         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
146                         || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
147                 return TRUE;
148         else
149                 return FALSE;
150 }
151
152 int _bt_get_local_address(void)
153 {
154         int result;
155
156         BT_DBG("+");
157
158         result =  adapter_get_address();
159         if (result != OAL_STATUS_SUCCESS) {
160                 BT_ERR("adapter_get_address failed: %d", result);
161                 result = BLUETOOTH_ERROR_INTERNAL;
162         } else
163                 result = BLUETOOTH_ERROR_NONE;
164
165         BT_DBG("-");
166         return result;
167 }
168
169 int _bt_get_local_version(void)
170 {
171         int result;
172         BT_DBG("+");
173
174         result =  adapter_get_version();
175         if (result != OAL_STATUS_SUCCESS) {
176                 BT_ERR("adapter_get_address failed: %d", result);
177                 result = BLUETOOTH_ERROR_INTERNAL;
178         } else
179                 result = BLUETOOTH_ERROR_NONE;
180
181         BT_DBG("-");
182         return result;
183 }
184
185 int _bt_get_local_name(void)
186 {
187         int result;
188
189         BT_DBG("+");
190
191         result =  adapter_get_name();
192         if (result != OAL_STATUS_SUCCESS) {
193                 BT_ERR("adapter_get_name failed: %d", result);
194                 result = BLUETOOTH_ERROR_INTERNAL;
195         } else
196                 result = BLUETOOTH_ERROR_NONE;
197
198         BT_DBG("-");
199         return result;
200 }
201
202 int _bt_set_local_name(char *local_name)
203 {
204         int result = BLUETOOTH_ERROR_NONE;
205         BT_DBG("+");
206
207         retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
208
209         result =  adapter_set_name(local_name);
210         if (result != OAL_STATUS_SUCCESS) {
211                 BT_ERR("adapter_set_name failed: %d", result);
212                 result = BLUETOOTH_ERROR_INTERNAL;
213         } else
214                 result = BLUETOOTH_ERROR_NONE;
215
216         BT_DBG("-");
217         return result;
218 }
219
220 int _bt_get_discoverable_mode(int *mode)
221 {
222         int scan_mode = 0;
223         int timeout = 0;
224
225         BT_DBG("+");
226
227         retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
228
229         adapter_is_discoverable(&scan_mode);
230         if (TRUE == scan_mode) {
231                 adapter_get_discoverable_timeout(&timeout);
232                 if (timeout > 0)
233                         *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
234                 else
235                         *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
236         } else {
237                 adapter_is_connectable(&scan_mode);
238                 if(scan_mode == TRUE)
239                         *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
240                 else {
241                         /*
242                          * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
243                          * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
244                          * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
245                          */
246                         *mode = -1;
247                 }
248         }
249
250         BT_DBG("-");
251         return BLUETOOTH_ERROR_NONE;
252 }
253
254 int _bt_get_timeout_value(int *timeout)
255 {
256         time_t current_time;
257         int time_diff;
258
259         /* Take current time */
260         time(&current_time);
261         time_diff = difftime(current_time, visible_timer.start_time);
262
263         BT_DBG("Time diff = %d\n", time_diff);
264         *timeout = visible_timer.timeout - time_diff;
265
266         return BLUETOOTH_ERROR_NONE;
267 }
268
269 static void __bt_visibility_alarm_remove()
270 {
271         if (visible_timer.event_id > 0) {
272                 g_source_remove(visible_timer.event_id);
273                 visible_timer.event_id = 0;
274         }
275
276         if (visible_timer.alarm_id > 0) {
277                 alarmmgr_remove_alarm(visible_timer.alarm_id);
278                 visible_timer.alarm_id = 0;
279         }
280 }
281
282 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
283 {
284         int result = BLUETOOTH_ERROR_NONE;
285         int timeout = 0;
286
287         BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
288
289         if (alarm_id != visible_timer.alarm_id)
290                 return 0;
291
292         if (visible_timer.event_id) {
293                 _bt_send_event(BT_ADAPTER_EVENT,
294                                 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
295                                 g_variant_new("(in)", result, timeout));
296                 g_source_remove(visible_timer.event_id);
297                 visible_timer.event_id = 0;
298                 visible_timer.timeout = 0;
299
300 #ifndef TIZEN_WEARABLE
301                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
302                         BT_ERR("Set vconf failed\n");
303 #endif
304         }
305         /* Switch Off visibility in Bluez */
306         _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
307         visible_timer.alarm_id = 0;
308         return 0;
309 }
310
311 static gboolean __bt_timeout_handler(gpointer user_data)
312 {
313         int result = BLUETOOTH_ERROR_NONE;
314         time_t current_time;
315         int time_diff;
316
317         /* Take current time */
318         time(&current_time);
319         time_diff = difftime(current_time, visible_timer.start_time);
320
321         /* Send event to application */
322         _bt_send_event(BT_ADAPTER_EVENT,
323                         BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
324                         g_variant_new("(in)", result, time_diff));
325
326         if (visible_timer.timeout <= time_diff) {
327                 g_source_remove(visible_timer.event_id);
328                 visible_timer.event_id = 0;
329                 visible_timer.timeout = 0;
330
331 #ifndef TIZEN_WEARABLE
332                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
333                         BT_ERR("Set vconf failed\n");
334 #endif
335                 return FALSE;
336         }
337
338         return TRUE;
339 }
340
341 static void __bt_visibility_alarm_create()
342 {
343         alarm_id_t alarm_id;
344         int result;
345
346         result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
347                         0, NULL, &alarm_id);
348         if (result < 0) {
349                 BT_ERR("Failed to create alarm error = %d\n", result);
350         } else {
351                 BT_DBG("Alarm created = %d\n", alarm_id);
352                 visible_timer.alarm_id = alarm_id;
353         }
354 }
355
356 static int __bt_set_visible_time(int timeout)
357 {
358         int result;
359
360         __bt_visibility_alarm_remove();
361
362         visible_timer.timeout = timeout;
363
364 #ifndef TIZEN_WEARABLE
365 #ifdef TIZEN_DPM_ENABLE
366         if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
367 #endif
368                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
369                         BT_ERR("Set vconf failed");
370 #ifdef TIZEN_DPM_ENABLE
371         }
372 #endif
373 #endif
374
375         if (timeout <= 0)
376                 return BLUETOOTH_ERROR_NONE;
377
378         if (!visible_timer.alarm_init) {
379                 /* Set Alarm timer to switch off BT */
380                 result = alarmmgr_init("bt-service");
381                 if (result != 0)
382                         return BLUETOOTH_ERROR_INTERNAL;
383
384                 visible_timer.alarm_init = TRUE;
385         }
386
387         result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
388         if (result != 0)
389                 return BLUETOOTH_ERROR_INTERNAL;
390
391         /* Take start time */
392         time(&(visible_timer.start_time));
393         visible_timer.event_id = g_timeout_add_seconds(1,
394                         __bt_timeout_handler, NULL);
395
396         __bt_visibility_alarm_create();
397
398         return BLUETOOTH_ERROR_NONE;
399 }
400
401 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
402 {
403         int result;
404
405         BT_DBG("+");
406
407         BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
408
409 #ifdef TIZEN_DPM_ENABLE
410         if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
411                         _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
412                 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
413                 return BLUETOOTH_ERROR_ACCESS_DENIED;
414         }
415         if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
416                         _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
417                 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
418                 return BLUETOOTH_ERROR_ACCESS_DENIED;
419         }
420 #endif
421
422         switch (discoverable_mode) {
423         case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
424                 result = adapter_set_connectable(TRUE);
425                 timeout = 0;
426                 break;
427         case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
428                 result = adapter_set_discoverable();
429                 timeout = 0;
430                 break;
431         case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
432                 result = adapter_set_discoverable();
433                 break;
434         default:
435                 return BLUETOOTH_ERROR_INVALID_PARAM;
436         }
437
438         if (result != OAL_STATUS_SUCCESS) {
439                 BT_ERR("set scan mode failed %d", result);
440                 return BLUETOOTH_ERROR_INTERNAL;
441         }
442
443         result = adapter_set_discoverable_timeout(timeout);
444         if (result != OAL_STATUS_SUCCESS) {
445                 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
446                 return BLUETOOTH_ERROR_INTERNAL;
447         }
448
449         if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
450                 timeout = -1;
451
452         result = __bt_set_visible_time(timeout);
453
454         BT_DBG("-");
455         return result;
456 }
457
458 gboolean _bt_is_connectable(void)
459 {
460         int connectable = 0;
461         int result;
462
463         BT_DBG("+");
464
465         adapter_is_connectable(&connectable);
466         if (connectable)
467                 result = TRUE;
468         else
469                 result = FALSE;
470
471         BT_DBG("Connectable: [%s]", result ? "TRUE":"FALSE");
472         BT_DBG("-");
473         return result;
474 }
475
476 int _bt_set_connectable(gboolean connectable)
477 {
478         int result = BLUETOOTH_ERROR_NONE;
479
480         BT_DBG("+");
481         result =  adapter_set_connectable(connectable);
482         if (result != OAL_STATUS_SUCCESS) {
483                 BT_ERR("adapter_get_address failed: %d", result);
484                 result = BLUETOOTH_ERROR_INTERNAL;
485         } else
486                 result = BLUETOOTH_ERROR_NONE;
487
488         BT_DBG("-");
489         return result;
490 }
491
492 int _bt_is_service_used(void)
493 {
494         int result;
495
496         BT_DBG("+");
497
498         result =  adapter_get_service_uuids();
499         if (result != OAL_STATUS_SUCCESS) {
500                 BT_ERR("adapter_get_service_uuids failed: %d", result);
501                 result = BLUETOOTH_ERROR_INTERNAL;
502         } else {
503                 result = BLUETOOTH_ERROR_NONE;
504         }
505
506         BT_DBG("-");
507         return result;
508 }
509
510 int _bt_adapter_get_bonded_devices(void)
511 {
512         int result = BLUETOOTH_ERROR_NONE;
513
514         BT_DBG("+");
515         result =  adapter_get_bonded_devices();
516         if (result != OAL_STATUS_SUCCESS) {
517                 BT_ERR("adapter_get_bonded_devices failed: %d", result);
518                 result = BLUETOOTH_ERROR_INTERNAL;
519         } else
520                 result = BLUETOOTH_ERROR_NONE;
521
522         BT_DBG("-");
523         return result;
524 }
525
526 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
527 {
528         int result = BLUETOOTH_ERROR_NONE;
529
530         BT_DBG("+");
531
532         switch(event_type) {
533         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
534         case OAL_EVENT_OAL_INITIALISED_FAILED:
535                 __bt_handle_oal_initialisation(event_type);
536                 break;
537         case OAL_EVENT_ADAPTER_ENABLED:
538                 __bt_adapter_state_change_callback(BT_ACTIVATED);
539                 break;
540         case OAL_EVENT_ADAPTER_DISABLED:
541                 __bt_adapter_state_change_callback(BT_DEACTIVATED);
542                 break;
543         case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
544                 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
545                 break;
546         case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
547                 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
548                 break;
549         case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
550                 bt_address_t *bd_addr = event_data;
551                 bluetooth_device_address_t local_address;
552
553                 /* Copy data */
554                 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
555                 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
556                                 local_address.addr[0], local_address.addr[1], local_address.addr[2],
557                                 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
558
559                 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
560                                 (void *) &local_address, sizeof(bluetooth_device_address_t));
561                 break;
562         }
563         case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
564                 char *name = event_data;
565                 BT_DBG("Adapter Name: %s", name);
566
567                 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
568                         bluetooth_device_name_t local_name;
569
570                         memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
571                         g_strlcpy(local_name.name,
572                                 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
573                         __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
574                                 (void *) &local_name, sizeof(bluetooth_device_name_t));
575                 } else {
576                         /* Send event to application */
577                         _bt_send_event(BT_ADAPTER_EVENT,
578                                         BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
579                                         g_variant_new("(is)", result, name));
580                 }
581                 break;
582         }
583         case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
584                 char *ver = event_data;
585                 bluetooth_version_t local_version;
586
587                 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
588                 g_strlcpy(local_version.version,
589                                 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
590                 BT_DBG("BT Version: %s", local_version.version);
591
592                 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
593                                 (void *) &local_version, sizeof(bluetooth_version_t));
594                 break;
595         }
596         case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
597                 int mode = -1;
598                 gboolean connectable = FALSE;
599
600                 BT_INFO("Adapter discoverable mode:"
601                                 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
602                 _bt_send_event(BT_ADAPTER_EVENT,
603                                 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
604                                 g_variant_new("(b)", connectable));
605
606                 _bt_send_event(BT_ADAPTER_EVENT,
607                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
608                                 g_variant_new("(in)", result, mode));
609                 break;
610         }
611         case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
612                 int mode;
613                 gboolean connectable = TRUE;
614
615                 BT_INFO("Adapter discoverable mode:"
616                                 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
617                 _bt_send_event(BT_ADAPTER_EVENT,
618                                 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
619                                 g_variant_new("(b)", connectable));
620
621                 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
622                 _bt_send_event(BT_ADAPTER_EVENT,
623                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
624                                 g_variant_new("(in)", result, mode));
625                 break;
626         }
627         case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
628                 int mode;
629
630                 BT_INFO("Adapter discoverable mode:"
631                                 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
632
633                 /* Send event to application */
634                 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
635                 _bt_send_event(BT_ADAPTER_EVENT,
636                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
637                                 g_variant_new("(in)", result, mode));
638
639                 break;
640         }
641         case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
642                 int *timeout = event_data;
643                 int mode;
644
645                 BT_INFO("Discoverable timeout: [%d]", *timeout);
646
647                 /* Send event to application */
648                 _bt_get_discoverable_mode(&mode);
649                 _bt_send_event(BT_ADAPTER_EVENT,
650                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
651                                 g_variant_new("(in)", result, mode));
652                 break;
653         }
654         case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
655                 int count;
656                 service_uuid_t *service_list;
657                 event_adapter_services_t *list = event_data;
658
659                 count = list->num;
660                 service_list = list->service_list;
661                 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
662                 break;
663         }
664         case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
665                 int i;
666                 int count;
667                 bluetooth_device_address_t *addr_list;
668
669                 event_device_list_t *bonded_device_list = event_data;
670                 count = bonded_device_list->num;
671
672                 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
673                 for (i = 0; i < count; i++) {
674                         memcpy(addr_list[i].addr,
675                                         bonded_device_list->devices[i].addr,
676                                         BLUETOOTH_ADDRESS_LENGTH);
677                 }
678
679                 __bt_adapter_handle_pending_requests(BT_GET_BONDED_DEVICES,
680                                 (void *)addr_list, bonded_device_list->num);
681                 break;
682         }
683         default:
684                 BT_ERR("Unhandled event..");
685                 break;
686         }
687
688         BT_DBG("-");
689 }
690
691 static int __bt_init_profiles()
692 {
693         int ret;
694
695         /*TODO: Init bluetooth profiles */
696         ret = _bt_hidhost_initialize();
697         if (ret != BLUETOOTH_ERROR_NONE) {
698                 BT_ERR("_bt_hidhost_initialize Failed");
699                 return ret;
700         }
701         ret = _bt_socket_init();
702         if (ret != BLUETOOTH_ERROR_NONE) {
703                 BT_ERR("_bt_socket_init Failed");
704                 return ret;
705         }
706         /* Initialize A2DP Source */
707         ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
708         if (ret != BLUETOOTH_ERROR_NONE) {
709                 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
710                 return ret;
711         }
712
713         /* Initialize HFP Audio Gateway */
714         ret = _bt_audio_initialize(BT_AG_MODULE);
715         if (ret != BLUETOOTH_ERROR_NONE) {
716                 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
717                 return ret;
718         }
719
720         return BLUETOOTH_ERROR_NONE;
721 }
722
723 /* OAL post initialization handler */
724 static void __bt_post_oal_init(void)
725 {
726         int ret;
727
728         BT_DBG("OAL initialized, Init profiles..");
729         ret = __bt_init_profiles();
730         if (ret != BLUETOOTH_ERROR_NONE)
731                 BT_ERR("Bluetooth profile init error: %d", ret);
732
733         return;
734 }
735
736 /* OAL initialization handler */
737 static void __bt_handle_oal_initialisation(oal_event_t event)
738 {
739         BT_DBG("");
740
741         switch(event) {
742         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
743                 __bt_post_oal_init();
744                 break;
745         case OAL_EVENT_OAL_INITIALISED_FAILED:
746                 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
747                 g_idle_add(_bt_terminate_service, NULL);
748                 break;
749         default:
750                 BT_ERR("Unknown Event");
751                 break;
752         }
753 }
754
755 static gboolean __bt_is_service_request_present(int service_function)
756 {
757         GSList *l;
758         invocation_info_t *req_info;
759
760         BT_DBG("+");
761
762         /* Get method invocation context */
763         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
764                 req_info = l->data;
765                 if (req_info && req_info->service_function == service_function)
766                         return TRUE;
767         }
768
769         BT_DBG("-");
770         return FALSE;
771 }
772
773 /* Internal functions of core adapter service */
774 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
775 {
776         GSList *l;
777         GArray *out_param;
778         invocation_info_t *req_info;
779         BT_INFO("+");
780
781         /* Get method invocation context */
782         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
783                 req_info = l->data;
784                 if (req_info == NULL || req_info->service_function != service_function)
785                         continue;
786
787                 /* Create out param */
788                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
789
790                 switch(service_function) {
791                 case BT_ENABLE_ADAPTER:
792                 case BT_DISABLE_ADAPTER: {
793                         gboolean done = TRUE;
794                         g_array_append_vals(out_param, &done, sizeof(gboolean));
795                         break;
796                 }
797                 case BT_GET_LOCAL_NAME:
798                 case BT_GET_LOCAL_ADDRESS:
799                 case BT_GET_LOCAL_VERSION:
800                         g_array_append_vals(out_param, user_data, size);
801                         break;
802                 case BT_IS_SERVICE_USED: {
803                         int i;
804                         gboolean used = FALSE;
805                         unsigned char *uuid;
806                         char uuid_str[BT_UUID_STRING_SIZE];
807                         char *request_uuid = req_info->user_data;
808                         service_uuid_t *service_list = user_data;
809
810                         BT_INFO("Check for service uuid: %s", request_uuid);
811                         for (i = 0; i < size; i++) {
812                                 uuid = service_list[i].uuid;
813                                 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
814                                 BT_INFO("Adapter Service: [%s]", uuid_str);
815                                 if (strcasecmp(uuid_str, request_uuid) == 0) {
816                                         BT_INFO("UUID matched!!");
817                                         used = TRUE;
818                                         break;
819                                 }
820                         }
821
822                         g_array_append_vals(out_param, &used, sizeof(gboolean));
823                         break;
824                 }
825                 case BT_GET_BONDED_DEVICES: {
826                         bluetooth_device_address_t *addr_list = user_data;
827                         bonded_devices_req_info_t *bonded_devices_req_info;
828                         char address[BT_ADDRESS_STRING_SIZE];
829                         int count = size;
830                         int res = BLUETOOTH_ERROR_NONE;
831
832                         /*
833                          * BT_GET_BONDED_DEVICES is already processed for this request,
834                          * continue for next BT_GET_BONDED_DEVICES request if any
835                          */
836                         if (NULL != req_info->user_data)
837                                 continue;
838
839                         BT_DBG("BT_GET_BONDED_DEVICES: count = [%d]", count);
840                         /* No bonded devices, return method invocation */
841                         if (0 == count || !addr_list)
842                                 break;
843
844                         /* Save address list in user data  for futur reference. */
845                         bonded_devices_req_info = g_malloc0(sizeof(bonded_devices_req_info));
846                         if (!bonded_devices_req_info) {
847                                 BT_ERR("Memory allocation failed");
848                                 req_info->result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
849                                 g_free(addr_list);
850                                 break;
851                         }
852
853                         bonded_devices_req_info->count = count;
854                         bonded_devices_req_info->addr_list = addr_list;
855                         bonded_devices_req_info->out_param = out_param;
856                         req_info->user_data = bonded_devices_req_info;
857
858                         while (bonded_devices_req_info->count > 0) {
859                                 bonded_devices_req_info->count -= 1;
860                                 res = _bt_device_get_bonded_device_info(
861                                                 &addr_list[bonded_devices_req_info->count]);
862                                 if (BLUETOOTH_ERROR_NONE == res)
863                                         return;
864                                 else {
865                                         _bt_convert_addr_type_to_string((char *)address,
866                                                         addr_list[bonded_devices_req_info->count].addr);
867                                         BT_ERR("_bt_device_get_bonded_device_info Failed for [%s]", address);
868                                         if (bonded_devices_req_info->count == 0) {
869                                                 g_free(bonded_devices_req_info->addr_list);
870                                                 g_free(bonded_devices_req_info);
871                                                 req_info->user_data = NULL;
872                                         }
873                                 }
874                         }
875                         break;
876                 }
877                 default:
878                         BT_ERR("Unknown service function[%d]", service_function);
879                 }
880
881                 _bt_service_method_return(req_info->context, out_param, req_info->result);
882                 g_array_free(out_param, TRUE);
883                 /* Now free invocation info for this request*/
884                 _bt_free_info_from_invocation_list(req_info);
885         }
886 }
887
888 /* Request return handlings */
889 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
890 {
891         BT_INFO("__bt_adapter_post_set_enabled>>");
892
893 #ifdef TIZEN_MOBILE
894         __bt_set_visible_mode();
895 #else
896 #ifdef TIZEN_TV
897         if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
898                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
899                 BT_ERR("Fail to set discoverable mode");
900 #endif
901 #endif
902         __bt_set_local_name();
903
904         /* Get All properties */
905         if (OAL_STATUS_SUCCESS != adapter_get_properties())
906                 BT_ERR("adapter_get_properties failed");
907
908         /* Add Adapter enabled post processing codes */
909         return FALSE;
910 }
911
912 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
913 {
914         BT_INFO("_bt_adapter_post_set_disabled>>");
915         /* Add Adapter disabled post processing codes */
916         return FALSE;
917 }
918
919 static void __bt_adapter_update_bt_enabled(void)
920 {
921         int result = BLUETOOTH_ERROR_NONE;
922         BT_INFO("_bt_adapter_update_bt_enabled>>");
923         /* Update Bluetooth Status to notify other modules */
924         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
925                 BT_ERR("Set vconf failed\n");
926
927         /* TODO:Add timer function to handle any further post processing */
928         g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
929
930         /*Return BT_ADAPTER_ENABLE Method invocation context */
931         __bt_adapter_handle_pending_requests(BT_ENABLE_ADAPTER, NULL, 0);
932         /*Send BT Enabled event to application */
933         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
934                         g_variant_new("(i)", result));
935 }
936
937 static void __bt_adapter_update_bt_disabled(void)
938 {
939         int result = BLUETOOTH_ERROR_NONE;
940         BT_INFO("_bt_adapter_update_bt_disabled>>");
941
942         int power_off_status = 0;
943         int ret;
944
945         /* Update the vconf BT status in normal Deactivation case only */
946         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
947         BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
948
949         /* TODO:Add timer function to handle any further post processing */
950         g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
951
952         /* Return BT_ADAPTER_DISABLE Method invocation context */
953         __bt_adapter_handle_pending_requests(BT_DISABLE_ADAPTER, NULL, 0);
954
955         /* Send BT Disabled event to application */
956         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
957                         g_variant_new("(i)", result));
958 }
959
960
961 static void __bt_adapter_state_set_status(bt_status_t status)
962 {
963         BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
964         adapter_state = status;
965 }
966
967 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
968 {
969         BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
970         adapter_discovery_state = status;
971 }
972
973 static void __bt_adapter_state_change_callback(int bt_status)
974 {
975         BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
976
977         switch (bt_status) {
978         case BT_DEACTIVATED:
979                 __bt_adapter_state_set_status(bt_status);
980
981                 /* Adapter is disabled, unregister event handlers */
982                 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
983                 //_bt_deinit_device_event_handler();
984
985                 /* Add Adapter disabled post processing codes */
986                 __bt_adapter_update_bt_disabled();
987                 break;
988         case BT_ACTIVATED:
989                 __bt_adapter_state_set_status(bt_status);
990                 /* Add Adapter enabled post processing codes */
991                 if (timer_id > 0) {
992                         BT_DBG("g_source is removed");
993                         g_source_remove(timer_id);
994                         timer_id = 0;
995                 }
996                 __bt_adapter_update_bt_enabled();
997                 break;
998         default:
999                 BT_ERR("Incorrect Bluetooth adapter state changed status");
1000
1001         }
1002 }
1003
1004 static int __bt_adapter_state_handle_request(gboolean enable)
1005 {
1006         int result = BLUETOOTH_ERROR_NONE;
1007         BT_DBG("");
1008
1009         switch (adapter_state) {
1010         case BT_ACTIVATING:
1011         {
1012                 BT_INFO("Adapter is currently in activating state, state [%d]",
1013                                 adapter_state);
1014                 if (enable) {
1015                         return BLUETOOTH_ERROR_IN_PROGRESS;
1016                 } else {
1017                         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1018                                         adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1019                                 /*TODO Stop Discovery*/
1020                                 if (result != OAL_STATUS_SUCCESS)
1021                                         BT_ERR("Discover stop failed: %d", result);
1022                                 __bt_adapter_update_discovery_status(FALSE);
1023                         }
1024                         result = adapter_disable();
1025                         if (result != OAL_STATUS_SUCCESS) {
1026                                 BT_ERR("adapter_enable failed: [%d]", result);
1027                                 result = BLUETOOTH_ERROR_INTERNAL;
1028                                 /*TODO: perform if anything more needs to be done to handle failure */
1029                         } else {
1030                                 /* TODO: To be handled */
1031                                 __bt_adapter_state_set_status(BT_DEACTIVATING);
1032                                 result = BLUETOOTH_ERROR_NONE;
1033                         }
1034                 }
1035                 break;
1036         }
1037         case BT_ACTIVATED:
1038         {
1039                 BT_INFO("Adapter is currently in activated state, state [%d]",
1040                                 adapter_state);
1041                 if (enable) {
1042                         return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1043                 } else {
1044                         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1045                                         adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1046                                 /*TODO Stop Discovery*/
1047                                 if (result != OAL_STATUS_SUCCESS)
1048                                         BT_ERR("Discover stop failed: %d", result);
1049                                 __bt_adapter_update_discovery_status(FALSE);
1050                         }
1051                         result = adapter_disable();
1052                         if (result != OAL_STATUS_SUCCESS) {
1053                                 BT_ERR("adapter_enable failed: [%d]", result);
1054                                 result = BLUETOOTH_ERROR_INTERNAL;
1055                                 /*TODO: perform if anything more needs to be done to handle failure */
1056                         } else {
1057                                 /* TODO: To be handled */
1058                                 __bt_adapter_state_set_status(BT_DEACTIVATING);
1059                                 result = BLUETOOTH_ERROR_NONE;
1060                         }
1061                 }
1062                 break;
1063         }
1064         case BT_DEACTIVATING:
1065         {
1066                 BT_INFO("Adapter is currently in deactivating state, state [%d]",
1067                                 adapter_state);
1068                 if (!enable) {
1069                         return BLUETOOTH_ERROR_IN_PROGRESS;
1070
1071                 } else {
1072                         result = adapter_enable();
1073                         if (result != OAL_STATUS_SUCCESS) {
1074                                 BT_ERR("adapter_enable failed: [%d]", result);
1075                                 adapter_disable();
1076                                 result = BLUETOOTH_ERROR_INTERNAL;
1077                                 /*TODO: perform if anything more needs to be done to handle failure */
1078                         } else {
1079                                 /* TODO: To be handled */
1080                                 __bt_adapter_state_set_status(BT_ACTIVATING);
1081                                 result = BLUETOOTH_ERROR_NONE;
1082                         }
1083                 }
1084                 break;
1085         }
1086         case BT_DEACTIVATED:
1087         {
1088                 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1089                                 adapter_state);
1090                 if (!enable) {
1091                         return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1092                 } else {
1093                         result = adapter_enable();
1094                         if (result != OAL_STATUS_SUCCESS) {
1095                                 BT_ERR("adapter_enable failed: [%d]", result);
1096                                 adapter_disable();
1097                                 result = BLUETOOTH_ERROR_INTERNAL;
1098                                 /*TODO: perform if anything more needs to be done to handle failure */
1099                         } else {
1100                                 /* TODO: To be handled */
1101                                 __bt_adapter_state_set_status(BT_ACTIVATING);
1102                                 result = BLUETOOTH_ERROR_NONE;
1103                         }
1104                 }
1105                 break;
1106         }
1107         }
1108         if (enable && result == BLUETOOTH_ERROR_NONE) {
1109                 /* Adapter enable request is successful, setup event handlers */
1110                 _bt_service_register_event_handler_callback(
1111                                 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1112                 _bt_device_state_handle_callback_set_request();
1113         }
1114         return result;
1115 }
1116
1117 static int __bt_adapter_state_discovery_request(gboolean enable)
1118 {
1119         int result = BLUETOOTH_ERROR_NONE;
1120
1121         BT_DBG("+");
1122         switch (adapter_discovery_state) {
1123         case ADAPTER_DISCOVERY_STARTED: {
1124                 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1125                                 adapter_discovery_state);
1126                 if (enable) {
1127                         return BLUETOOTH_ERROR_IN_PROGRESS;
1128                 } else {
1129                         result = adapter_stop_inquiry();
1130                         if (result != OAL_STATUS_SUCCESS) {
1131                                 BT_ERR("Discover stop failed: %d", result);
1132                                 result = BLUETOOTH_ERROR_INTERNAL;
1133                         } else {
1134                                 BT_ERR("Stop Discovery Triggered successfully");
1135                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1136                                 result = BLUETOOTH_ERROR_NONE;
1137                         }
1138                 }
1139                 break;
1140         }
1141         case ADAPTER_DISCOVERY_STARTING: {
1142                 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1143                                 adapter_discovery_state);
1144                 if (enable) {
1145                         return BLUETOOTH_ERROR_IN_PROGRESS;
1146                 } else {
1147                         result = adapter_stop_inquiry();
1148                         if (result != OAL_STATUS_SUCCESS) {
1149                                 BT_ERR("Discover stop failed: %d", result);
1150                                 result = BLUETOOTH_ERROR_INTERNAL;
1151                         } else {
1152                                 BT_ERR("Stop Discovery Triggered successfully");
1153                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1154                                 result = BLUETOOTH_ERROR_NONE;
1155                         }
1156                 }
1157                 break;
1158         }
1159         case ADAPTER_DISCOVERY_STOPPED: {
1160                 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1161                                 adapter_discovery_state);
1162                 if (!enable)
1163                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1164                 else {
1165                         result = adapter_start_inquiry();
1166                 if (result != OAL_STATUS_SUCCESS) {
1167                                 BT_ERR("Start Discovery failed: %d", result);
1168                                 result = BLUETOOTH_ERROR_INTERNAL;
1169                         } else {
1170                                 BT_ERR("Start Discovery Triggered successfully");
1171                         __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1172                                 result = BLUETOOTH_ERROR_NONE;
1173                         }
1174                 }
1175                 break;
1176         }
1177         case ADAPTER_DISCOVERY_STOPPING: {
1178                 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1179                                 adapter_discovery_state);
1180                 if (!enable)
1181                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1182                 else {
1183                         result = adapter_start_inquiry();
1184                         if (result != OAL_STATUS_SUCCESS) {
1185                                 BT_ERR("Start Discovery failed: %d", result);
1186                                 result = BLUETOOTH_ERROR_INTERNAL;
1187                         } else {
1188                                 BT_ERR("Start Discovery Triggered successfully");
1189                         __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1190                                 result = BLUETOOTH_ERROR_NONE;
1191                         }
1192                 }
1193                 break;
1194         }
1195         }
1196
1197         BT_DBG("-");
1198         return result;
1199 }
1200
1201 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1202 {
1203         BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1204         GVariant *param = NULL;
1205         int result = BLUETOOTH_ERROR_NONE;
1206
1207         switch (bt_discovery_status) {
1208         case ADAPTER_DISCOVERY_STOPPED:
1209         {
1210                 __bt_adapter_update_discovery_status(bt_discovery_status);
1211                 param = g_variant_new("(i)", result);
1212                 _bt_send_event(BT_ADAPTER_EVENT,
1213                                 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1214                                 param);
1215                 break;
1216         }
1217         case ADAPTER_DISCOVERY_STARTED:
1218         {
1219                 __bt_adapter_update_discovery_status(bt_discovery_status);
1220                 param = g_variant_new("(i)", result);
1221                 _bt_send_event(BT_ADAPTER_EVENT,
1222                                 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1223                                 param);
1224                 break;
1225         }
1226         default:
1227                 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1228         }
1229 }
1230
1231 #ifdef TIZEN_MOBILE
1232 static void __bt_set_visible_mode(void)
1233 {
1234         int timeout = 0;
1235
1236         if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1237                 BT_ERR("Fail to get the timeout value");
1238
1239 #ifdef TIZEN_DPM_ENABLE
1240         if (timeout == -1 ||
1241                         _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1242                 if (_bt_set_discoverable_mode(
1243                                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1244                                         timeout) != BLUETOOTH_ERROR_NONE) {
1245                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1246                                 BT_ERR("Set vconf failed");
1247                 }
1248         } else {
1249                 if (_bt_set_discoverable_mode(
1250                                         BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1251                                         timeout) != BLUETOOTH_ERROR_NONE) {
1252                         BT_ERR("Set connectable mode failed");
1253                 }
1254         }
1255 #else
1256         if (timeout == -1) {
1257                 if (_bt_set_discoverable_mode(
1258                                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1259                                         timeout) != BLUETOOTH_ERROR_NONE) {
1260                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1261                                 BT_ERR("Set vconf failed");
1262                 }
1263         } else {
1264                 if (_bt_set_discoverable_mode(
1265                                         BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1266                                         timeout) != BLUETOOTH_ERROR_NONE) {
1267                         BT_ERR("Set connectable mode failed");
1268                 }
1269         }
1270 #endif
1271 }
1272 #endif
1273
1274 static void __bt_set_local_name(void)
1275 {
1276         char *phone_name = NULL;
1277         char *ptr = NULL;
1278
1279         phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1280
1281         if (!phone_name)
1282                 return;
1283
1284         if (strlen(phone_name) != 0) {
1285                 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1286                         *ptr = '\0';
1287
1288                 _bt_set_local_name(phone_name);
1289         }
1290         free(phone_name);
1291 }