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