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