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