Merge "Remove flight mode implementation from core and add it to bt-service" into...
[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 <bundle.h>
28 #include <bundle_internal.h>
29 #include <eventsystem.h>
30
31 #include "alarm.h"
32
33 /*bt-service headers */
34 #include "bt-internal-types.h"
35 #include "bt-service-common.h"
36 #include "bt-service-util.h"
37 #include "bt-service-main.h"
38 #include "bt-service-core-adapter.h"
39 #include "bt-service-core-device.h"
40 #include "bt-service-event-receiver.h"
41 #include "bt-request-handler.h"
42 #include "bt-service-event.h"
43 #include "bt-service-audio-common.h"
44 #include "bt-service-core-adapter-le.h"
45 #include "bt-service-gatt.h"
46
47 #ifdef TIZEN_FEATURE_BT_DPM
48 #include "bt-service-dpm.h"
49 #endif
50 #include "bt-service-hidhost.h"
51 #include "bt-service-hiddevice.h"
52 #include "bt-service-socket.h"
53 #include "bt-service-hdp.h"
54
55 /* OAL headers */
56 #include <oal-event.h>
57 #include <oal-manager.h>
58 #include <oal-adapter-mgr.h>
59
60 #ifdef TIZEN_FEATURE_BT_PAN_NAP
61 #include "bt-service-network.h"
62 #endif
63 /*This file will contain state machines related to adapter and remote device */
64
65 #include "bt-internal-types.h"
66
67 /* Global variables */
68 typedef struct {
69         guint event_id;
70         int timeout;
71         time_t start_time;
72         gboolean alarm_init;
73         int alarm_id;
74 } bt_adapter_timer_t;
75
76 static bt_adapter_timer_t visible_timer;
77
78 typedef enum {
79         BT_ADAPTER_FLIGHT_MODE_NONE = -1,
80         BT_ADAPTER_FLIGHT_MODE_OFF,
81         BT_ADAPTER_FLIGHT_MODE_ON,
82 } bt_adapter_flightmode_req_e;
83
84 static bt_adapter_flightmode_req_e flightmode_request = BT_ADAPTER_FLIGHT_MODE_NONE;
85
86 static guint timer_id = 0;
87
88 static gboolean a2dp_init_pending = TRUE;
89
90 /* Adapter default states */
91 static bt_status_t adapter_state = BT_DEACTIVATED;
92 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
93
94 /* Forward declarations */
95 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
96 static void __bt_post_oal_init(void);
97 static void __bt_handle_oal_initialisation(oal_event_t event);
98 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
99 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
100 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
101 static void __bt_adapter_update_bt_enabled(void);
102 static void __bt_adapter_update_bt_disabled(void);
103 static void __bt_adapter_state_set_status(bt_status_t status);
104 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
105 static void __bt_adapter_state_change_callback(int bt_status);
106 static int __bt_adapter_state_handle_request(gboolean enable);
107 static int __bt_adapter_state_discovery_request(gboolean enable,
108                 unsigned short max_response, unsigned short duration, unsigned int mask,
109                 gboolean is_custom, bt_discovery_role_type_t role);
110 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
111 static gboolean __bt_is_service_request_present(int service_function);
112
113 static void __bt_set_visible_mode(void);
114 static void __bt_set_local_name(void);
115
116 static gboolean __bt_disconnect_all(void);
117
118 /* Initialize BT stack (Initialize OAL layer) */
119 int _bt_stack_init(void)
120 {
121         int ret;
122
123         BT_INFO("[bt-service] Start to initialize BT stack");
124         /* Adapter enable request is successful, setup event handlers */
125         _bt_service_register_event_handler_callback(
126                         BT_ADAPTER_MODULE, __bt_adapter_event_handler);
127
128         ret = _bt_le_init();
129         if (ret != BLUETOOTH_ERROR_NONE) {
130                 BT_ERR("_bt_le_init Failed");
131                 return ret;
132         }
133
134         ret = oal_bt_init(_bt_service_oal_event_receiver);
135
136         if (OAL_STATUS_PENDING == ret) {
137                 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
138                 return BLUETOOTH_ERROR_NONE;
139         } else if (OAL_STATUS_SUCCESS != ret) {
140                 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
141                 return BLUETOOTH_ERROR_INTERNAL;
142         }
143
144         return BLUETOOTH_ERROR_NONE;
145 }
146
147 int _bt_enable_adapter(void)
148 {
149         bt_le_status_t le_status = BT_LE_DEACTIVATED;
150
151         le_status = _bt_adapter_get_le_status();
152
153         BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
154
155         if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
156                 BT_ERR("Enabling in progress");
157                 return BLUETOOTH_ERROR_IN_PROGRESS;
158         }
159
160         if (adapter_state == BT_ACTIVATED) {
161                 BT_ERR("Already enabled");
162                 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
163         }
164
165         if (adapter_state == BT_DEACTIVATING ||
166                  adapter_state == BT_TERMINATING ||
167                   le_status == BT_LE_DEACTIVATING) {
168                 BT_ERR("Disabling in progress");
169                 return BLUETOOTH_ERROR_DEVICE_BUSY;
170         }
171
172         return __bt_adapter_state_handle_request(TRUE);
173 }
174
175 int _bt_enable_core(void)
176 {
177         /* TODO_40 : 4.0 merge  */
178         BT_INFO("Not Supported");
179         return BLUETOOTH_ERROR_NOT_SUPPORT;
180 }
181
182 int _bt_recover_adapter(void)
183 {
184         int result = BLUETOOTH_ERROR_NONE;
185
186         if (_bt_adapter_get_status() == BT_DEACTIVATING) {
187                 BT_ERR("Disabling in progress");
188                 return BLUETOOTH_ERROR_IN_PROGRESS;
189         }
190
191         if (_bt_adapter_get_status() == BT_DEACTIVATED) {
192                 BT_ERR("Already Disabled");
193                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
194         }
195
196         _bt_adapter_set_status(BT_DEACTIVATING);
197
198         result = adapter_recover();
199
200         if (result != OAL_STATUS_SUCCESS)
201                 return BLUETOOTH_ERROR_INTERNAL;
202
203         __bt_disconnect_all();
204
205         return result;
206 }
207
208 int _bt_reset_adapter(void)
209 {
210         BT_INFO("+");
211         if (OAL_STATUS_SUCCESS != adapter_reset())
212                 return BLUETOOTH_ERROR_INTERNAL;
213
214         /* TODO_40 : 4.0 merge  */
215         /* TODO Currently bt-service is not terminated in tizen next.
216            It should be handled in future patch */
217         if (_bt_adapter_get_status() == BT_DEACTIVATED)
218                 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
219
220         return BLUETOOTH_ERROR_NONE;
221 }
222
223 int _bt_check_adapter(int *status)
224 {
225         BT_INFO("+");
226         gboolean powered = FALSE;
227         BT_CHECK_PARAMETER(status, return);
228
229         *status = BT_ADAPTER_DISABLED;
230
231         if (OAL_STATUS_SUCCESS != adapter_get_powered_status(&powered))
232                 return BLUETOOTH_ERROR_INTERNAL;
233         else if (powered) {
234                 BT_INFO("Adapter is in Powered Up state");
235                 *status = BT_ADAPTER_ENABLED;
236         } else
237                 BT_INFO("Adapter is in Powered Down state");
238
239         return BLUETOOTH_ERROR_NONE;
240 }
241
242 int _bt_disable_adapter(void)
243 {
244         bt_le_status_t le_status = BT_LE_DEACTIVATED;
245
246         le_status = _bt_adapter_get_le_status();
247
248         BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
249
250         if (adapter_state == BT_DEACTIVATING ||
251                  adapter_state == BT_TERMINATING ||
252                   le_status == BT_LE_DEACTIVATING) {
253                 BT_DBG("Disabling in progress");
254                 return BLUETOOTH_ERROR_IN_PROGRESS;
255         }
256
257         if (adapter_state == BT_DEACTIVATED) {
258                 BT_DBG("Already disabled");
259                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
260         }
261
262         if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
263                 BT_ERR("Enabling in progress");
264                 return BLUETOOTH_ERROR_DEVICE_BUSY;
265         }
266
267         /* TODO: Need to add the function to disconnect all devices */
268         /* __bt_disconnect_all(); */
269
270         return __bt_adapter_state_handle_request(FALSE);
271 }
272
273 int _bt_start_discovery(unsigned short max_response,
274                 unsigned short duration, unsigned int cod_mask)
275 {
276         return __bt_adapter_state_discovery_request(TRUE, max_response, duration,
277                                 cod_mask, FALSE, 0x00);
278 }
279
280 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
281 {
282         return __bt_adapter_state_discovery_request(TRUE, 0, 0, 0, TRUE, role);
283 }
284
285 int _bt_cancel_discovery(void)
286 {
287         return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0, FALSE, 0x00);
288 }
289
290 gboolean _bt_is_discovering(void)
291 {
292         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
293                         || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
294                 return TRUE;
295         else
296                 return FALSE;
297 }
298
299 static void __bt_set_flightmode_request(int value)
300 {
301         BT_INFO("Setting flightmode request to %d", value);
302         flightmode_request = value;
303 }
304
305 static int __bt_is_flightmode_request(void)
306 {
307         return flightmode_request;
308 }
309
310 static void __bt_service_handle_flight_mode(gboolean flight_mode, bt_status_t adapter_status)
311 {
312         int flight_mode_value = 0;
313         int ps_mode_value = 0;
314
315         if (flight_mode == TRUE) {
316                 BT_INFO_C("### Flight mode on. Turn off BT");
317
318                 if (adapter_status != BT_ACTIVATED) {
319                         BT_INFO("No need to control bt status");
320                         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
321                                 BT_ERR("Fail get PS mode value");
322
323                         BT_DBG("ps_mode_value %d", ps_mode_value);
324                         if (ps_mode_value > 0) {
325                                 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
326                                         BT_ERR("Set vconf failed");
327                         }
328                         return;
329                 }
330
331                 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_ON);
332
333                 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
334                         BT_ERR("Set vconf failed");
335
336                 if (adapter_status == BT_ACTIVATED)
337                         _bt_disable_adapter();
338
339         } else {
340                 BT_INFO_C("### Flight mode off. Turn on BT");
341
342                 if (adapter_status == BT_ACTIVATED)
343                         return;
344
345                 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value))
346                         BT_ERR("Fail get flight mode value");
347
348                 BT_DBG("flight_mode_value %d", flight_mode_value);
349                 if (flight_mode_value == 0)
350                         return;
351
352                 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
353                         BT_ERR("Set vconf failed");
354
355                 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
356                         BT_ERR("Fail get PS mode value");
357
358                 BT_DBG("ps_mode_value %d", ps_mode_value);
359                 if (ps_mode_value > 0) {
360                         BT_DBG("Bt should not turn on");
361                         return;
362                 }
363
364                 if (adapter_status == BT_DEACTIVATING) {
365                         BT_INFO("BT adapter is deactivating. Turn on BT after deactivation");
366                         __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_OFF);
367                         return;
368                 }
369
370                 if (adapter_status != BT_DEACTIVATED) {
371                         BT_INFO("No need to control bt status");
372                         return;
373                 }
374                 _bt_service_initialize();
375
376                 if (adapter_status == BT_DEACTIVATED)
377                         _bt_enable_adapter();
378
379         }
380 }
381
382 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
383 {
384         int type;
385         bt_status_t adapter_status = BT_DEACTIVATED;
386         gboolean flight_mode = FALSE;
387
388         adapter_status = _bt_adapter_get_status();
389         BT_INFO("Flight mode changed. Current bt status is %d", adapter_status);
390
391         DBG_SECURE("key=%s", vconf_keynode_get_name(node));
392         type = vconf_keynode_get_type(node);
393
394         if (type == VCONF_TYPE_BOOL) {
395                 flight_mode = vconf_keynode_get_bool(node);
396                 __bt_service_handle_flight_mode(flight_mode, adapter_status);
397         } else {
398                 BT_ERR("Invaild vconf key type : %d", type);
399         }
400 }
401
402 void _bt_service_register_vconf_handler(void)
403 {
404         BT_DBG("+");
405
406         if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
407                 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
408                         BT_ERR("Unable to register key handler");
409         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
410                 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
411                         BT_ERR("Unable to register key handler");
412 }
413
414 void _bt_service_unregister_vconf_handler(void)
415 {
416         BT_DBG("+");
417
418         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
419                         (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
420
421         vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
422                         (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
423 }
424
425 int _bt_get_local_address(void)
426 {
427         int result;
428
429         BT_DBG("+");
430
431         result =  adapter_get_address();
432         if (result != OAL_STATUS_SUCCESS) {
433                 BT_ERR("adapter_get_address failed: %d", result);
434                 result = BLUETOOTH_ERROR_INTERNAL;
435         } else
436                 result = BLUETOOTH_ERROR_NONE;
437
438         BT_DBG("-");
439         return result;
440 }
441
442 int _bt_get_local_version(void)
443 {
444         int result;
445         BT_DBG("+");
446
447         result =  adapter_get_version();
448         if (result != OAL_STATUS_SUCCESS) {
449                 BT_ERR("adapter_get_version failed: %d", result);
450                 result = BLUETOOTH_ERROR_INTERNAL;
451         } else
452                 result = BLUETOOTH_ERROR_NONE;
453
454         BT_DBG("-");
455         return result;
456 }
457
458 int _bt_get_local_name(void)
459 {
460         int result;
461
462         BT_DBG("+");
463
464         result =  adapter_get_name();
465         if (result != OAL_STATUS_SUCCESS) {
466                 BT_ERR("adapter_get_name failed: %d", result);
467                 result = BLUETOOTH_ERROR_INTERNAL;
468         } else
469                 result = BLUETOOTH_ERROR_NONE;
470
471         BT_DBG("-");
472         return result;
473 }
474
475 int _bt_set_local_name(char *local_name)
476 {
477         int result = BLUETOOTH_ERROR_NONE;
478         BT_DBG("+");
479
480         retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
481
482         result =  adapter_set_name(local_name);
483         if (result != OAL_STATUS_SUCCESS) {
484                 BT_ERR("adapter_set_name 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_get_discoverable_mode(int *mode)
494 {
495         int scan_mode = 0;
496         int timeout = 0;
497
498         BT_DBG("+");
499
500         retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
501
502         adapter_is_discoverable(&scan_mode);
503         if (TRUE == scan_mode) {
504                 adapter_get_discoverable_timeout(&timeout);
505                 if (timeout > 0)
506                         *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
507                 else
508                         *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
509         } else {
510                 adapter_is_connectable(&scan_mode);
511                 if (scan_mode == TRUE)
512                         *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
513                 else {
514                         /*
515                          * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
516                          * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
517                          * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
518                          */
519                         *mode = -1;
520                 }
521         }
522
523         BT_DBG("-");
524         return BLUETOOTH_ERROR_NONE;
525 }
526
527 int _bt_get_timeout_value(int *timeout)
528 {
529         time_t current_time;
530         int time_diff;
531
532         /* Take current time */
533         time(&current_time);
534         time_diff = difftime(current_time, visible_timer.start_time);
535
536         BT_DBG("Time diff = %d\n", time_diff);
537         *timeout = visible_timer.timeout - time_diff;
538
539         return BLUETOOTH_ERROR_NONE;
540 }
541
542 static void __bt_visibility_alarm_remove()
543 {
544         if (visible_timer.event_id > 0) {
545                 g_source_remove(visible_timer.event_id);
546                 visible_timer.event_id = 0;
547         }
548
549         if (visible_timer.alarm_id > 0) {
550                 alarmmgr_remove_alarm(visible_timer.alarm_id);
551                 visible_timer.alarm_id = 0;
552         }
553 }
554
555 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
556 {
557         int result = BLUETOOTH_ERROR_NONE;
558         int timeout = 0;
559
560         BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
561
562         if (alarm_id != visible_timer.alarm_id)
563                 return 0;
564
565         if (visible_timer.event_id) {
566                 _bt_send_event(BT_ADAPTER_EVENT,
567                                 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
568                                 g_variant_new("(in)", result, timeout));
569                 g_source_remove(visible_timer.event_id);
570                 visible_timer.event_id = 0;
571                 visible_timer.timeout = 0;
572
573                 if (!TIZEN_PROFILE_WEARABLE) {
574                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
575                                 BT_ERR("Set vconf failed\n");
576                 }
577         }
578         /* Switch Off visibility in Bluez */
579         _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
580         visible_timer.alarm_id = 0;
581         return 0;
582 }
583
584 static gboolean __bt_timeout_handler(gpointer user_data)
585 {
586         int result = BLUETOOTH_ERROR_NONE;
587         time_t current_time;
588         int time_diff;
589
590         /* Take current time */
591         time(&current_time);
592         time_diff = difftime(current_time, visible_timer.start_time);
593
594         /* Send event to application */
595         _bt_send_event(BT_ADAPTER_EVENT,
596                         BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
597                         g_variant_new("(in)", result, time_diff));
598
599         if (visible_timer.timeout <= time_diff) {
600                 g_source_remove(visible_timer.event_id);
601                 visible_timer.event_id = 0;
602                 visible_timer.timeout = 0;
603
604                 if (!TIZEN_PROFILE_WEARABLE) {
605                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
606                                 BT_ERR("Set vconf failed\n");
607                 }
608
609                 return FALSE;
610         }
611
612         return TRUE;
613 }
614
615 static void __bt_visibility_alarm_create()
616 {
617         alarm_id_t alarm_id;
618         int result;
619
620         result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
621                         0, NULL, &alarm_id);
622         if (result < 0) {
623                 BT_ERR("Failed to create alarm error = %d\n", result);
624         } else {
625                 BT_DBG("Alarm created = %d\n", alarm_id);
626                 visible_timer.alarm_id = alarm_id;
627         }
628 }
629
630 int _bt_start_visibility_timer(int timeout)
631 {
632         int result;
633 #ifdef TIZEN_FEATURE_BT_DPM
634         int discoverable_state = DPM_BT_ERROR;
635 #endif
636
637         __bt_visibility_alarm_remove();
638
639         visible_timer.timeout = timeout;
640
641 #ifdef TIZEN_FEATURE_BT_DPM
642         _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
643         if (discoverable_state != DPM_RESTRICTED) {
644 #endif
645                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
646                         BT_ERR("Set vconf failed");
647 #ifdef TIZEN_FEATURE_BT_DPM
648         }
649 #endif
650
651         if (timeout <= 0)
652                 return BLUETOOTH_ERROR_NONE;
653
654         if (!visible_timer.alarm_init) {
655                 /* Set Alarm timer to switch off BT */
656                 result = alarmmgr_init("bt-service");
657                 if (result != 0)
658                         return BLUETOOTH_ERROR_INTERNAL;
659
660                 visible_timer.alarm_init = TRUE;
661         }
662
663         result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
664         if (result != 0)
665                 return BLUETOOTH_ERROR_INTERNAL;
666
667         if (!TIZEN_PROFILE_WEARABLE) {
668                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
669                         BT_ERR("Set vconf failed");
670         }
671
672         /* Take start time */
673         time(&(visible_timer.start_time));
674         visible_timer.event_id = g_timeout_add_seconds(1,
675                         __bt_timeout_handler, NULL);
676
677         visible_timer.timeout = timeout;
678
679         __bt_visibility_alarm_create();
680
681         return BLUETOOTH_ERROR_NONE;
682 }
683
684 int _bt_stop_visibility_timer(void)
685 {
686         __bt_visibility_alarm_remove();
687
688         visible_timer.timeout = 0;
689
690         if (!TIZEN_PROFILE_WEARABLE) {
691                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
692                         BT_ERR("Set vconf failed");
693         }
694
695         return BLUETOOTH_ERROR_NONE;
696 }
697
698 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
699 {
700         int result;
701         int ret = BLUETOOTH_ERROR_NONE;;
702 #ifdef TIZEN_FEATURE_BT_DPM
703         int discoverable_state = DPM_BT_ERROR;
704 #endif
705
706         BT_DBG("+");
707
708         BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
709
710 #ifdef TIZEN_FEATURE_BT_DPM
711         _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
712         if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
713                  discoverable_state == DPM_RESTRICTED) {
714                 if (headed_plugin_info->plugin_headed_enabled)
715                         headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
716                 return BLUETOOTH_ERROR_ACCESS_DENIED;
717         }
718         if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE &&
719                 discoverable_state == DPM_RESTRICTED) {
720                 if (headed_plugin_info->plugin_headed_enabled)
721                         headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
722                 return BLUETOOTH_ERROR_ACCESS_DENIED;
723         }
724 #endif
725
726         switch (discoverable_mode) {
727         case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
728                 result = adapter_set_connectable(TRUE);
729                 timeout = 0;
730                 break;
731         case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
732                 result = adapter_set_discoverable();
733                 timeout = 0;
734                 break;
735         case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
736                 result = adapter_set_discoverable();
737                 break;
738         default:
739                 return BLUETOOTH_ERROR_INVALID_PARAM;
740         }
741
742         if (result != OAL_STATUS_SUCCESS) {
743                 BT_ERR("set scan mode failed %d", result);
744                 return BLUETOOTH_ERROR_INTERNAL;
745         }
746
747         result = adapter_set_discoverable_timeout(timeout);
748         if (result != OAL_STATUS_SUCCESS) {
749                 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
750                 return BLUETOOTH_ERROR_INTERNAL;
751         }
752
753         if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
754                 ret = _bt_stop_visibility_timer();
755                 if (BLUETOOTH_ERROR_NONE != ret)
756                         BT_ERR("_bt_stop_visibility_timer failed");
757                 if (!TIZEN_PROFILE_WEARABLE) {
758                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, -1) != 0)
759                                  BT_ERR("Set vconf failed");
760                 }
761         } else if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE) {
762                 ret = _bt_start_visibility_timer(timeout);
763                 if (BLUETOOTH_ERROR_NONE != ret)
764                         BT_ERR("_bt_start_visibility_timer failed");
765
766         } else {
767                 ret = _bt_stop_visibility_timer();
768                 if (BLUETOOTH_ERROR_NONE != ret)
769                         BT_ERR("_bt_stop_visibility_timer failed");
770         }
771
772         BT_DBG("-");
773         return result;
774 }
775
776 gboolean _bt_is_connectable(void)
777 {
778         int connectable = 0;
779         int result;
780
781         BT_DBG("+");
782
783         adapter_is_connectable(&connectable);
784         if (connectable)
785                 result = TRUE;
786         else
787                 result = FALSE;
788
789         BT_DBG("Connectable: [%s]", result ? "TRUE" : "FALSE");
790         BT_DBG("-");
791         return result;
792 }
793
794 int _bt_set_connectable(gboolean connectable)
795 {
796         int result = BLUETOOTH_ERROR_NONE;
797
798         BT_DBG("+");
799         result =  adapter_set_connectable(connectable);
800         if (result != OAL_STATUS_SUCCESS) {
801                 BT_ERR("adapter_set_connectable failed: %d", result);
802                 result = BLUETOOTH_ERROR_INTERNAL;
803         } else
804                 result = BLUETOOTH_ERROR_NONE;
805
806         BT_DBG("-");
807         return result;
808 }
809
810 int _bt_get_connectable(void)
811 {
812         int result;
813
814         BT_DBG("+");
815
816         result =  adapter_get_connectable();
817         if (result != OAL_STATUS_SUCCESS) {
818                 BT_ERR("adapter_get_connectable failed: %d", result);
819                 result = BLUETOOTH_ERROR_INTERNAL;
820         } else
821                 result = BLUETOOTH_ERROR_NONE;
822
823         BT_DBG("-");
824         return result;
825 }
826
827 int _bt_is_service_used(void)
828 {
829         int result;
830
831         BT_DBG("+");
832
833         result =  adapter_get_service_uuids();
834         if (result != OAL_STATUS_SUCCESS) {
835                 BT_ERR("adapter_get_service_uuids failed: %d", result);
836                 result = BLUETOOTH_ERROR_INTERNAL;
837         } else {
838                 result = BLUETOOTH_ERROR_NONE;
839         }
840
841         BT_DBG("-");
842         return result;
843 }
844
845 int _bt_adapter_get_bonded_devices(void)
846 {
847         int result = BLUETOOTH_ERROR_NONE;
848
849         BT_DBG("+");
850         result =  adapter_get_bonded_devices();
851         if (result != OAL_STATUS_SUCCESS) {
852                 BT_ERR("adapter_get_bonded_devices failed: %d", result);
853                 result = BLUETOOTH_ERROR_INTERNAL;
854         } else
855                 result = BLUETOOTH_ERROR_NONE;
856
857         BT_DBG("-");
858         return result;
859 }
860
861 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
862 {
863         BT_DBG("+");
864         GDBusConnection *conn;
865         GDBusProxy *manager_proxy;
866         GVariant *result = NULL;
867         GVariant *result1 = NULL;
868         GVariantIter *iter = NULL;
869         GError *error = NULL;
870         char *object_path = NULL;
871         GVariantIter *interface_iter;
872         char *interface_str = NULL;
873         GDBusProxy *device_proxy = NULL;
874         gboolean is_connected = FALSE;
875
876         conn = _bt_gdbus_get_system_gconn();
877         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
878
879         manager_proxy = _bt_get_manager_proxy();
880         retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
881
882         result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
883                         NULL,
884                         G_DBUS_CALL_FLAGS_NONE,
885                         -1,
886                         NULL,
887                         NULL);
888
889         if (!result) {
890                 if (error != NULL) {
891                         BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
892                         g_clear_error(&error);
893                         error = NULL;
894                 } else
895                         BT_ERR("Failed to Failed to GetManagedObjects");
896                 return BLUETOOTH_ERROR_INTERNAL;
897         }
898
899         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
900         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
901
902         /* Parse the signature:  oa{sa{sv}}} */
903         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
904                 if (object_path == NULL)
905                         continue;
906
907                 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
908                                         &interface_str, NULL)) {
909                         if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
910                                 BT_DBG("Found a device: %s", object_path);
911                                 g_free(interface_str);
912
913                                 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
914                                                 NULL, BT_BLUEZ_NAME,
915                                                 object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
916
917                                 if (device_proxy == NULL) {
918                                         BT_DBG("Device don't have this service");
919                                         break;
920                                 }
921
922                                 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
923                                                 g_variant_new("(s)", profile_uuid),
924                                                 G_DBUS_CALL_FLAGS_NONE,
925                                                 -1,
926                                                 NULL,
927                                                 &error);
928
929                                 if (result1 == NULL) {
930                                         BT_ERR("Error occured in Proxy call");
931                                         if (error) {
932                                                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
933                                                 g_error_free(error);
934                                                 error = NULL;
935                                         }
936                                         g_object_unref(device_proxy);
937                                         break;
938                                 }
939                                 g_variant_get(result1, "(b)", &is_connected);
940
941                                 if (is_connected == TRUE) {
942                                         char address[BT_ADDRESS_STRING_SIZE];
943                                         bluetooth_device_address_t *addr = NULL;
944
945                                         _bt_convert_device_path_to_address(object_path, address);
946
947                                         addr = g_malloc0(sizeof(bluetooth_device_address_t));
948                                         _bt_convert_addr_string_to_type(addr->addr, address);
949
950                                         g_array_append_vals(*addr_list, addr,
951                                                         sizeof(bluetooth_device_address_t));
952                                 }
953
954                                 g_variant_unref(result1);
955                                 g_object_unref(device_proxy);
956
957                                 break;
958                         }
959                 }
960         }
961
962         g_variant_unref(result);
963         g_variant_iter_free(iter);
964
965         BT_DBG("-");
966         return BLUETOOTH_ERROR_NONE;
967 }
968
969 static gboolean __bt_disconnect_all(void)
970 {
971         int i;
972         gboolean ret = FALSE;
973         GArray *device_list;
974         guint size;
975         int result;
976         bluetooth_device_info_t *info;
977
978         BT_DBG("");
979         device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
980
981         BT_DBG("The length of device list after the initialisation is [%d]", device_list->len);
982
983         _bt_service_get_bonded_dev_list(&device_list);
984
985         size = (device_list->len) / sizeof(bluetooth_device_info_t);
986
987         for (i = 0; i < size; i++) {
988                 info = &g_array_index(device_list,
989                                 bluetooth_device_info_t, i);
990                 if (info->connected != BLUETOOTH_CONNECTED_LINK_NONE) {
991                         BT_DBG("Found Connected Device");
992                         result = _bt_disconnect_device(&info->device_address);
993                         if (result != BLUETOOTH_ERROR_NONE) {
994                                 BT_ERR("Cannot disconnect all the devices. bt_disconnect_device Failed");
995                                 g_array_free(device_list, TRUE);
996                                 return ret;
997                         }
998                 }
999         }
1000         ret = TRUE;
1001         g_array_free(device_list, TRUE);
1002
1003         return ret;
1004 }
1005
1006 static void __bt_handle_pending_a2dp_init(unsigned int a2dp_role)
1007 {
1008         int ret;
1009
1010         if (!a2dp_init_pending)
1011                 return;
1012
1013         BT_DBG("+");
1014         a2dp_init_pending = FALSE;
1015
1016         if (a2dp_role == BT_A2DP_SINK_ROLE) {
1017                 BT_INFO("Enable A2DP Sink role");
1018                 /* Initialize A2DP Sink */
1019                 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
1020                 if (ret != BLUETOOTH_ERROR_NONE)
1021                         BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
1022
1023                 /* Initialize AVRCP Controller */
1024                 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
1025                 if (ret != BLUETOOTH_ERROR_NONE)
1026                         BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
1027
1028                 _bt_audio_set_current_role(BLUETOOTH_A2DP_SINK);
1029                 return;
1030         }
1031
1032         BT_INFO("Enable A2DP Source role");
1033         /* Initialize A2DP Source */
1034         ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
1035         if (ret != BLUETOOTH_ERROR_NONE)
1036                 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
1037
1038         /* Initialize AVRCP Target */
1039         ret = _bt_audio_initialize(BT_AVRCP_MODULE);
1040         if (ret != BLUETOOTH_ERROR_NONE)
1041                 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
1042
1043         _bt_audio_set_current_role(BLUETOOTH_A2DP_SOURCE);
1044         BT_DBG("-");
1045 }
1046
1047 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
1048 {
1049         int result = BLUETOOTH_ERROR_NONE;
1050
1051         BT_DBG("+");
1052
1053         switch (event_type) {
1054         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1055         case OAL_EVENT_OAL_INITIALISED_FAILED:
1056                 __bt_handle_oal_initialisation(event_type);
1057                 break;
1058         case OAL_EVENT_ADAPTER_ENABLED:
1059                 __bt_adapter_state_change_callback(BT_ACTIVATED);
1060                 break;
1061         case OAL_EVENT_ADAPTER_DISABLED:
1062                 __bt_adapter_state_change_callback(BT_DEACTIVATED);
1063                 break;
1064         case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
1065                 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
1066                 break;
1067         case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
1068                 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
1069                 break;
1070         case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
1071                 bt_address_t *bd_addr = event_data;
1072                 bluetooth_device_address_t local_address;
1073
1074                 /* Copy data */
1075                 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
1076                 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
1077                                 local_address.addr[0], local_address.addr[1], local_address.addr[2],
1078                                 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
1079
1080                 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
1081                                 (void *) &local_address, sizeof(bluetooth_device_address_t));
1082                 break;
1083         }
1084         case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
1085                 char *name = event_data;
1086                 BT_DBG("Adapter Name: %s", name);
1087
1088                 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
1089                         bluetooth_device_name_t local_name;
1090
1091                         memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
1092                         g_strlcpy(local_name.name,
1093                                 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
1094                         __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
1095                                 (void *) &local_name, sizeof(bluetooth_device_name_t));
1096                 } else {
1097                         /* Send event to application */
1098                         _bt_send_event(BT_ADAPTER_EVENT,
1099                                         BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
1100                                         g_variant_new("(is)", result, name));
1101                 }
1102                 break;
1103         }
1104         case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
1105                 char *ver = event_data;
1106                 bluetooth_version_t local_version;
1107
1108                 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
1109                 g_strlcpy(local_version.version,
1110                                 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
1111                 BT_DBG("BT Version: %s", local_version.version);
1112
1113                 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
1114                                 (void *) &local_version, sizeof(bluetooth_version_t));
1115                 break;
1116         }
1117         case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
1118                 int mode = -1;
1119                 gboolean connectable = FALSE;
1120
1121                 BT_INFO("Adapter discoverable mode:"
1122                                 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
1123
1124                 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1125                         __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1126                                 (void *) &connectable, sizeof(gboolean));
1127
1128                 } else {
1129                         _bt_send_event(BT_ADAPTER_EVENT,
1130                                         BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1131                                         g_variant_new("(b)", connectable));
1132
1133                         _bt_send_event(BT_ADAPTER_EVENT,
1134                                         BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1135                                         g_variant_new("(in)", result, mode));
1136                 }
1137                 break;
1138         }
1139         case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
1140                 int mode;
1141                 gboolean connectable = TRUE;
1142
1143                 BT_INFO("Adapter discoverable mode:"
1144                                 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
1145
1146                 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1147                         __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1148                                 (void *) &connectable, sizeof(gboolean));
1149
1150                 } else {
1151
1152                         _bt_send_event(BT_ADAPTER_EVENT,
1153                                         BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1154                                         g_variant_new("(b)", connectable));
1155
1156                         mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1157                         _bt_send_event(BT_ADAPTER_EVENT,
1158                                         BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1159                                         g_variant_new("(in)", result, mode));
1160                 }
1161                 break;
1162         }
1163         case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
1164                 int mode;
1165                 gboolean connectable = TRUE;
1166
1167                 BT_INFO("Adapter discoverable mode:"
1168                                 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
1169
1170                 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1171                         __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1172                                 (void *) &connectable, sizeof(gboolean));
1173
1174                 } else {
1175                         /* Send event to application */
1176                         mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1177                         _bt_send_event(BT_ADAPTER_EVENT,
1178                                         BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1179                                         g_variant_new("(in)", result, mode));
1180                 }
1181                 break;
1182         }
1183         case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
1184                 int *timeout = event_data;
1185                 int mode;
1186
1187                 BT_INFO("Discoverable timeout: [%d]", *timeout);
1188
1189                 /* Send event to application */
1190                 _bt_get_discoverable_mode(&mode);
1191                 _bt_send_event(BT_ADAPTER_EVENT,
1192                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1193                                 g_variant_new("(in)", result, mode));
1194                 break;
1195         }
1196         case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
1197                 int count;
1198                 service_uuid_t *service_list;
1199                 event_adapter_services_t *list = event_data;
1200
1201                 count = list->num;
1202                 service_list = list->service_list;
1203                 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
1204
1205                 char uuids[512] = {0,};
1206                 char standard_prefix[2] = {0,};
1207                 int index = 0;
1208                 for (int i = 0; i < count; i++) {
1209                         if (i > 0)
1210                                 g_snprintf(uuids + (index++), 2, ",");
1211                         if (memcmp(service_list[i].uuid, standard_prefix, 2) == 0) {
1212                                 g_snprintf(uuids + index, 5, "%.2x%.2x",
1213                                         service_list[i].uuid[2], service_list[i].uuid[3]);
1214                                 index += 4;
1215                         } else {
1216                                 g_snprintf(uuids + index, 9, "%.2x%.2x..%.2x,",
1217                                         service_list[i].uuid[0], service_list[i].uuid[1], service_list[i].uuid[15]);
1218                                 index += 8;
1219                         }
1220                 }
1221                 BT_PERMANENT_LOG("UUIDs %s", uuids);
1222                 break;
1223         }
1224         case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
1225                 int i;
1226                 int count;
1227                 bluetooth_device_address_t *addr_list;
1228
1229                 event_device_list_t *bonded_device_list = event_data;
1230                 count = bonded_device_list->num;
1231
1232                 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
1233                 for (i = 0; i < count; i++) {
1234                         memcpy(addr_list[i].addr,
1235                                         bonded_device_list->devices[i].addr,
1236                                         BLUETOOTH_ADDRESS_LENGTH);
1237                 }
1238
1239                 BT_INFO("Adapter Bonded device List count: [%d]", count);
1240                 _bt_device_handle_paired_address_list(addr_list, bonded_device_list->num);
1241                 g_free(addr_list);
1242                 break;
1243         }
1244         case OAL_EVENT_ADAPTER_PROPERTY_A2DP_ROLE: {
1245                 unsigned int *a2dp_role = event_data;
1246
1247                 BT_INFO("a2dp role: [%u]", *a2dp_role);
1248
1249                 __bt_handle_pending_a2dp_init(*a2dp_role);
1250
1251                 break;
1252         }
1253         default:
1254                 BT_ERR("Unhandled event..");
1255                 break;
1256         }
1257
1258         BT_DBG("-");
1259 }
1260
1261 int _bt_init_profiles()
1262 {
1263         int ret;
1264         BT_INFO("+");
1265
1266         /*TODO: Init bluetooth profiles */
1267         ret = _bt_hidhost_initialize();
1268         if (ret != BLUETOOTH_ERROR_NONE)
1269                 BT_ERR("_bt_hidhost_initialize Failed");
1270
1271         ret = _bt_hiddevice_initialize();
1272         if (ret != BLUETOOTH_ERROR_NONE)
1273                 BT_ERR("_bt_hiddevice_initialize Failed");
1274
1275         ret = _bt_socket_init();
1276         if (ret != BLUETOOTH_ERROR_NONE)
1277                 BT_ERR("_bt_socket_init Failed");
1278
1279         /* Initialize HFP Audio Gateway */
1280         ret = _bt_audio_initialize(BT_AG_MODULE);
1281         if (ret != BLUETOOTH_ERROR_NONE)
1282                 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
1283
1284         /* Registering callback for receiving audio services searched */
1285         ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
1286         if (ret != BLUETOOTH_ERROR_NONE)
1287                 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
1288
1289         ret = _bt_hdp_init();
1290         if (ret != BLUETOOTH_ERROR_NONE)
1291                 BT_ERR("_bt_hdp_init Failed");
1292
1293         ret = _bt_gatt_init();
1294         if (ret != BLUETOOTH_ERROR_NONE)
1295                 BT_ERR("_bt_gatt_init Failed");
1296
1297         /* Initialize HF Client */
1298         ret = _bt_audio_initialize(BT_HFP_MODULE);
1299         if (ret != BLUETOOTH_ERROR_NONE)
1300                 BT_ERR("_bt_audio_initialize(BT_HFP_MODULE) Failed");
1301
1302         return BLUETOOTH_ERROR_NONE;
1303 }
1304
1305 int _bt_cleanup_profiles(void)
1306 {
1307         /* TODO: Cleanup bluetooth profiles */
1308         _bt_hidhost_deinitialize();
1309         _bt_socket_deinit();
1310 #if 0
1311         /* TODO: Cleanup bluetooth audio profiles */
1312         //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
1313         //_bt_audio_deinitialize(BT_AVRCP_MODULE);
1314         //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
1315         //_bt_audio_deinitialize(BT_AG_MODULE);
1316         //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
1317         //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
1318
1319         a2dp_init_pending = TRUE;
1320 #endif
1321         _bt_hdp_deinit();
1322         _bt_gatt_deinit();
1323
1324         return BLUETOOTH_ERROR_NONE;
1325 }
1326
1327 /* OAL post initialization handler */
1328 static void __bt_post_oal_init(void)
1329 {
1330         int ret;
1331         int status = VCONFKEY_BT_STATUS_OFF;
1332
1333         BT_DBG("OAL initialized");
1334         if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0)
1335                 BT_ERR("Fail to get the enabled value");
1336
1337 #if 0
1338         /* Update Bluetooth Status to OFF */
1339         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1340                 BT_ERR("Set vconf failed\n");
1341 #endif
1342
1343         if (status & VCONFKEY_BT_STATUS_ON) {
1344                 ret = _bt_enable_adapter();
1345                 if (ret != BLUETOOTH_ERROR_NONE)
1346                         BT_ERR("_bt_enable_adapter failed with error: %d", ret);
1347         }
1348 }
1349
1350 /* OAL initialization handler */
1351 static void __bt_handle_oal_initialisation(oal_event_t event)
1352 {
1353         BT_DBG("");
1354
1355         switch (event) {
1356         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1357                 __bt_post_oal_init();
1358                 break;
1359         case OAL_EVENT_OAL_INITIALISED_FAILED:
1360                 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1361                 g_idle_add(_bt_terminate_service, NULL);
1362                 break;
1363         default:
1364                 BT_ERR("Unknown Event");
1365                 break;
1366         }
1367 }
1368
1369 static gboolean __bt_is_service_request_present(int service_function)
1370 {
1371         GSList *l;
1372         invocation_info_t *req_info;
1373
1374         BT_DBG("+");
1375
1376         /* Get method invocation context */
1377         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1378                 req_info = l->data;
1379                 if (req_info && req_info->service_function == service_function)
1380                         return TRUE;
1381         }
1382
1383         BT_DBG("-");
1384         return FALSE;
1385 }
1386
1387 /* Internal functions of core adapter service */
1388 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1389 {
1390         GSList *l;
1391         GArray *out_param;
1392         invocation_info_t *req_info;
1393         BT_INFO("+");
1394
1395         /* Get method invocation context */
1396         for (l = _bt_get_invocation_list(); l != NULL; ) {
1397                 req_info = l->data;
1398                 l = g_slist_next(l);
1399                 if (req_info == NULL || req_info->service_function != service_function)
1400                         continue;
1401
1402                 /* Create out param */
1403                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1404
1405                 switch (service_function) {
1406                 case BT_ENABLE_ADAPTER:
1407                 case BT_DISABLE_ADAPTER: {
1408                         gboolean done = TRUE;
1409                         g_array_append_vals(out_param, &done, sizeof(gboolean));
1410                         break;
1411                 }
1412                 case BT_GET_CONNECTABLE:
1413                 case BT_GET_LOCAL_NAME:
1414                 case BT_GET_LOCAL_ADDRESS:
1415                 case BT_GET_LOCAL_VERSION:
1416                         g_array_append_vals(out_param, user_data, size);
1417                         break;
1418                 case BT_IS_SERVICE_USED: {
1419                         unsigned int i;
1420                         gboolean used = FALSE;
1421                         unsigned char *uuid;
1422                         char uuid_str[BT_UUID_STRING_SIZE];
1423                         char *request_uuid = req_info->user_data;
1424                         service_uuid_t *service_list = user_data;
1425
1426                         BT_INFO("Check for service uuid: %s", request_uuid);
1427                         for (i = 0; i < size; i++) {
1428                                 uuid = service_list[i].uuid;
1429                                 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1430                                 BT_INFO("Adapter Service: [%s]", uuid_str);
1431                                 if (strcasecmp(uuid_str, request_uuid) == 0) {
1432                                         BT_INFO("UUID matched!!");
1433                                         used = TRUE;
1434                                         break;
1435                                 }
1436                         }
1437
1438                         g_array_append_vals(out_param, &used, sizeof(gboolean));
1439                         break;
1440                 }
1441                 default:
1442                         BT_ERR("Unknown service function[%d]", service_function);
1443                 }
1444
1445                 _bt_service_method_return(req_info->context, out_param, req_info->result);
1446                 g_array_free(out_param, TRUE);
1447                 /* Now free invocation info for this request*/
1448                 _bt_free_info_from_invocation_list(req_info);
1449         }
1450 }
1451
1452 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1453 {
1454         char *phone_name = NULL;
1455         char *ptr = NULL;
1456
1457         if (node == NULL)
1458                 return;
1459
1460         if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1461                 phone_name = vconf_keynode_get_str(node);
1462
1463                 if (phone_name && strlen(phone_name) != 0) {
1464                         if (!g_utf8_validate(phone_name, -1,
1465                                         (const char **)&ptr))
1466                                 *ptr = '\0';
1467
1468                         _bt_set_local_name(phone_name);
1469                 }
1470         }
1471 }
1472
1473 /* Request return handlings */
1474 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1475 {
1476         int result = BLUETOOTH_ERROR_NONE;
1477
1478         BT_INFO("__bt_adapter_post_set_enabled>>");
1479
1480         if (TIZEN_PROFILE_TV || !headed_plugin_info->plugin_headed_enabled) {
1481                 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1482                                 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1483                         BT_ERR("Fail to set discoverable mode");
1484         } else {
1485                 __bt_set_visible_mode();
1486         }
1487
1488         /* add the vconf noti handler */
1489         if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1490                         (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1491                 BT_ERR("DEVICE_NAME key changed notification registration failed");
1492
1493         __bt_set_local_name();
1494
1495         /* Get All properties */
1496         if (OAL_STATUS_SUCCESS != adapter_get_properties())
1497                 BT_ERR("adapter_get_properties failed");
1498
1499         __bt_adapter_state_set_status(BT_ACTIVATED);
1500
1501         /*Send BT Enabled event to application */
1502         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1503                         g_variant_new("(i)", result));
1504
1505         BT_PERMANENT_LOG("BT Enabled");
1506
1507         return FALSE;
1508 }
1509
1510 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1511 {
1512         BT_INFO("_bt_adapter_post_set_disabled>>");
1513
1514         if (!TIZEN_PROFILE_TV) {
1515                 /* Add Adapter disabled post processing codes */
1516                 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1517                                 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1518                         BT_ERR("vconf_ignore_key_changed failed");
1519         }
1520
1521         /* bt-service should be terminated when BT is off */
1522         if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1523                 /* TODO: Implement to check if it is the recovery mode or not */
1524                 if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_ON) {
1525                         /* Keep bt-service to handle Flight mode OFF */
1526                         BT_DBG("Deactived due to flight mode. Keep bt-service");
1527                         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1528                         __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_NONE);
1529                 } else if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_OFF) {
1530                         BT_DBG("Durning deactivation, receive Flight mode Off. Enable BT adapter");
1531                         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1532                         _bt_service_initialize();
1533                         _bt_enable_adapter();
1534                         __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_NONE);
1535                 } else
1536                         _bt_reliable_terminate_service(NULL);
1537         }
1538
1539         return FALSE;
1540 }
1541
1542 static void __bt_adapter_update_bt_enabled(void)
1543 {
1544         BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1545         if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1546                 BT_ERR("Bluetooth profile init failed");
1547
1548         _bt_device_handle_adapter_state(TRUE);
1549
1550         /* Update Bluetooth Status to notify other modules */
1551         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1552                 BT_ERR("Set vconf failed\n");
1553
1554         if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1555                 BT_ERR("Set vconf failed\n");
1556
1557         /* TODO:Add timer function to handle any further post processing */
1558         g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1559 }
1560
1561 static void __bt_adapter_update_bt_disabled(void)
1562 {
1563         int power_off_status = 0;
1564         int ret;
1565
1566         BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1567         _bt_cleanup_profiles();
1568
1569         _bt_device_handle_adapter_state(FALSE);
1570
1571         /* Update the vconf BT status in normal Deactivation case only */
1572         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1573         BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1574
1575         /* Update Bluetooth Status to notify other modules */
1576         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1577                 BT_ERR("Set vconf failed");
1578
1579         if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1580                 BT_ERR("Set vconf failed\n");
1581
1582         /* TODO:Add timer function to handle any further post processing */
1583         g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1584
1585         /* Pending BT Disabled event before bt-service's termination */
1586 }
1587
1588 static void __bt_adapter_state_set_status(bt_status_t status)
1589 {
1590         BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1591         adapter_state = status;
1592 }
1593
1594 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1595 {
1596         BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1597         adapter_discovery_state = status;
1598 }
1599
1600 static void __bt_adapter_state_change_callback(int bt_status)
1601 {
1602         BT_INFO("BT adapter status changed [%d]", bt_status);
1603
1604         switch (bt_status) {
1605         case BT_DEACTIVATED:
1606                 __bt_adapter_state_set_status(BT_TERMINATING);
1607
1608                 /* Adapter is disabled, unregister event handlers */
1609                 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1610                 //_bt_deinit_device_event_handler();
1611
1612                 /* Add Adapter disabled post processing codes */
1613                 __bt_adapter_update_bt_disabled();
1614                 break;
1615         case BT_ACTIVATED:
1616                 /* Add Adapter enabled post processing codes */
1617                 if (timer_id > 0) {
1618                         BT_DBG("g_source is removed");
1619                         g_source_remove(timer_id);
1620                         timer_id = 0;
1621                 }
1622
1623                 /*If adapter gets activated when flight mode is ON*/
1624                 if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_ON) {
1625                         BT_DBG("Receive Flight mode On. Disable BT adapter");
1626                         _bt_disable_adapter();
1627                         return;
1628                 }
1629
1630                 __bt_adapter_update_bt_enabled();
1631                 break;
1632         default:
1633                 BT_ERR("Incorrect Bluetooth adapter state changed status");
1634
1635         }
1636 }
1637
1638 int _bt_set_adapter_request_state(int enable)
1639 {
1640         return oal_set_adapter_request_state(enable);
1641 }
1642
1643 int _bt_set_le_request_state(int enable)
1644 {
1645         return oal_set_le_request_state(enable);
1646 }
1647
1648 static int __bt_adapter_state_handle_request(gboolean enable)
1649 {
1650         int result = BLUETOOTH_ERROR_NONE;
1651
1652         switch (adapter_state) {
1653         case BT_ACTIVATING:
1654         case BT_DEACTIVATING:
1655                 BT_INFO("Should not be callded : state [%d]", adapter_state);
1656                 return BLUETOOTH_ERROR_INTERNAL;
1657                 break;
1658         case BT_ACTIVATED: {
1659                 BT_INFO("Adapter is currently in activated state, state [%d]",
1660                                 adapter_state);
1661                 if (enable) {
1662                         return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1663                 } else {
1664                         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1665                                         adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1666                                 /*TODO Stop Discovery*/
1667                                 __bt_adapter_update_discovery_status(FALSE);
1668                         }
1669                         result = adapter_disable();
1670                         if (result != OAL_STATUS_SUCCESS) {
1671                                 BT_ERR("adapter_enable failed: [%d]", result);
1672                                 result = BLUETOOTH_ERROR_INTERNAL;
1673                                 /*TODO: perform if anything more needs to be done to handle failure */
1674                         } else {
1675                                 /* TODO: To be handled */
1676                                 __bt_adapter_state_set_status(BT_DEACTIVATING);
1677                                 result = BLUETOOTH_ERROR_NONE;
1678                         }
1679                 }
1680                 break;
1681         }
1682         case BT_DEACTIVATED: {
1683                 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1684                                 adapter_state);
1685                 if (!enable) {
1686                         return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1687                 } else {
1688                         result = adapter_enable();
1689                         if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1690                                 BT_ERR("adapter_enable failed: [%d]", result);
1691                                 adapter_disable();
1692                                 result = BLUETOOTH_ERROR_INTERNAL;
1693                                 /*TODO: perform if anything more needs to be done to handle failure */
1694                         } else {
1695                                 /* TODO: To be handled */
1696                                 __bt_adapter_state_set_status(BT_ACTIVATING);
1697                                 result = BLUETOOTH_ERROR_NONE;
1698                         }
1699                 }
1700                 break;
1701         }
1702         default:
1703                 BT_ERR("Unknown state: %d", adapter_state);
1704                 break;
1705         }
1706
1707         if (enable && result == BLUETOOTH_ERROR_NONE) {
1708                 /* Adapter enable request is successful, setup event handlers */
1709                 _bt_service_register_event_handler_callback(
1710                                 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1711                 _bt_device_state_handle_callback_set_request();
1712         }
1713         return result;
1714 }
1715
1716 static int __bt_adapter_state_discovery_request(gboolean enable,
1717                 unsigned short max_response, unsigned short duration,
1718                 unsigned int mask, gboolean is_custom,
1719                 bt_discovery_role_type_t role)
1720 {
1721         int result = BLUETOOTH_ERROR_NONE;
1722
1723         BT_DBG("+");
1724         switch (adapter_discovery_state) {
1725         case ADAPTER_DISCOVERY_STARTED: {
1726                 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1727                                 adapter_discovery_state);
1728                 if (enable) {
1729                         return BLUETOOTH_ERROR_IN_PROGRESS;
1730                 } else {
1731                         result = adapter_stop_inquiry();
1732                         if (result != OAL_STATUS_SUCCESS) {
1733                                 BT_ERR("Discover stop failed: %d", result);
1734                                 result = BLUETOOTH_ERROR_INTERNAL;
1735                         } else {
1736                                 BT_ERR("Stop Discovery Triggered successfully");
1737                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1738                                 result = BLUETOOTH_ERROR_NONE;
1739                         }
1740                 }
1741                 break;
1742         }
1743         case ADAPTER_DISCOVERY_STARTING: {
1744                 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1745                                 adapter_discovery_state);
1746
1747                 result = enable ?  BLUETOOTH_ERROR_IN_PROGRESS :
1748                                         BLUETOOTH_ERROR_DEVICE_BUSY;
1749
1750                 break;
1751         }
1752         case ADAPTER_DISCOVERY_STOPPED: {
1753                 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1754                                 adapter_discovery_state);
1755                 if (!enable)
1756                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1757                 else {
1758                         BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1759
1760                         if (!is_custom)
1761                                 result = adapter_start_inquiry(duration);
1762                         else
1763                                 result = adapter_start_custom_inquiry(role);
1764
1765                         if (result != OAL_STATUS_SUCCESS) {
1766                                 BT_ERR("Start Discovery failed: %d", result);
1767                                 result = BLUETOOTH_ERROR_INTERNAL;
1768                         } else {
1769                                 BT_ERR("Start Discovery Triggered successfully");
1770                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1771                                 result = BLUETOOTH_ERROR_NONE;
1772                         }
1773                 }
1774                 break;
1775         }
1776         case ADAPTER_DISCOVERY_STOPPING: {
1777                 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1778                                 adapter_discovery_state);
1779
1780                 result = enable ?  BLUETOOTH_ERROR_DEVICE_BUSY :
1781                                         BLUETOOTH_ERROR_NOT_IN_OPERATION;
1782
1783                 break;
1784         }
1785         default:
1786                 BT_ERR("Unknown state: %d", adapter_discovery_state);
1787                 break;
1788         }
1789
1790         BT_DBG("-");
1791         return result;
1792 }
1793
1794 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1795 {
1796         BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1797         GVariant *param = NULL;
1798         int result = BLUETOOTH_ERROR_NONE;
1799
1800         switch (bt_discovery_status) {
1801         case ADAPTER_DISCOVERY_STOPPED:
1802         {
1803                 __bt_adapter_update_discovery_status(bt_discovery_status);
1804                 param = g_variant_new("(i)", result);
1805                 _bt_send_event(BT_ADAPTER_EVENT,
1806                                 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1807                                 param);
1808                 break;
1809         }
1810         case ADAPTER_DISCOVERY_STARTED:
1811         {
1812                 __bt_adapter_update_discovery_status(bt_discovery_status);
1813                 param = g_variant_new("(i)", result);
1814                 _bt_send_event(BT_ADAPTER_EVENT,
1815                                 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1816                                 param);
1817                 break;
1818         }
1819         default:
1820                 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1821         }
1822 }
1823
1824 static void __bt_set_visible_mode(void)
1825 {
1826         int timeout = 0;
1827 #ifdef TIZEN_FEATURE_BT_DPM
1828         int discoverable_state = DPM_BT_ERROR;
1829 #endif
1830
1831         if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1832                 BT_ERR("Fail to get the timeout value");
1833
1834 #ifdef TIZEN_FEATURE_BT_DPM
1835         _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1836         if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
1837                 if (_bt_set_discoverable_mode(
1838                                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1839                                         timeout) != BLUETOOTH_ERROR_NONE) {
1840                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1841                                 BT_ERR("Set vconf failed");
1842                 }
1843         } else {
1844                 if (_bt_set_discoverable_mode(
1845                                         BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1846                                         timeout) != BLUETOOTH_ERROR_NONE) {
1847                         BT_ERR("Set connectable mode failed");
1848                 }
1849         }
1850 #else
1851         if (timeout == -1) {
1852                 if (_bt_set_discoverable_mode(
1853                                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1854                                         timeout) != BLUETOOTH_ERROR_NONE) {
1855                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1856                                 BT_ERR("Set vconf failed");
1857                 }
1858         } else {
1859                 if (_bt_set_discoverable_mode(
1860                                         BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1861                                         timeout) != BLUETOOTH_ERROR_NONE) {
1862                         BT_ERR("Set connectable mode failed");
1863                 }
1864         }
1865 #endif
1866 }
1867
1868 static void __bt_set_local_name(void)
1869 {
1870         char *phone_name = NULL;
1871         char *ptr = NULL;
1872
1873         phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1874
1875         if (!phone_name)
1876                 return;
1877
1878         if (strlen(phone_name) != 0) {
1879                 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1880                         *ptr = '\0';
1881
1882                 _bt_set_local_name(phone_name);
1883         }
1884         free(phone_name);
1885 }
1886
1887 void _bt_adapter_set_status(bt_status_t status)
1888 {
1889         BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1890         adapter_state = status;
1891 }
1892
1893 bt_status_t _bt_adapter_get_status(void)
1894 {
1895         return adapter_state;
1896 }
1897
1898 void _bt_set_disabled(int result)
1899 {
1900         int power_off_status = 0;
1901         int ret;
1902         int ret_pm_ignore;
1903         int pm_ignore_mode = 0;
1904
1905         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1906         BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1907
1908         ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1909
1910         /* Update the vconf BT status in normal Deactivation case only */
1911         if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1912                 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1913
1914                 BT_DBG("Update vconf for BT normal Deactivation");
1915
1916                 if (result == BLUETOOTH_ERROR_TIMEOUT)
1917                         if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1918                                 BT_ERR("Set vconf failed");
1919
1920                 /* Update Bluetooth Status to notify other modules */
1921                 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1922                         BT_ERR("Set vconf failed");
1923
1924                 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1925                                                         EVT_VAL_BT_OFF) != ES_R_OK)
1926                         BT_ERR("Fail to set value");
1927         }
1928
1929         if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1930                 BT_ERR("Set vconf failed\n");
1931
1932         _bt_adapter_set_status(BT_DEACTIVATED);
1933         __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1934
1935         /* Send BT Disabled event to application */
1936         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1937                         g_variant_new("(i)", result));
1938
1939         BT_INFO("Adapter disabled");
1940         BT_PERMANENT_LOG("BT Disabled");
1941 }
1942