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