dba5a0629468569bf6a3b593445465e9918ca3d3
[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         case OAL_EVENT_CONTROLLER_ERROR_RECEIVED: {
1283                 uint8_t *error_code = event_data;
1284
1285                 BT_INFO("OAL_EVENT_CONTROLLER_ERROR_RECEIVED: [%d]", *error_code);
1286
1287 #ifdef TIZEN_FEATURE_BT_PERMANENT_LOG
1288                 _bt_start_log_dump("syslog");
1289 #endif
1290
1291 #ifdef TIZEN_FEATURE_BT_VENDOR_OPERATION
1292                 _bt_recover_adapter();
1293 #endif
1294                 break;
1295         }
1296         default:
1297                 BT_ERR("Unhandled event..");
1298                 break;
1299         }
1300
1301         BT_DBG("-");
1302 }
1303
1304 int _bt_init_profiles()
1305 {
1306         int ret;
1307         BT_INFO("+");
1308
1309         /*TODO: Init bluetooth profiles */
1310         ret = _bt_hidhost_initialize();
1311         if (ret != BLUETOOTH_ERROR_NONE)
1312                 BT_ERR("_bt_hidhost_initialize Failed");
1313
1314         ret = _bt_hiddevice_initialize();
1315         if (ret != BLUETOOTH_ERROR_NONE)
1316                 BT_ERR("_bt_hiddevice_initialize Failed");
1317
1318         ret = _bt_socket_init();
1319         if (ret != BLUETOOTH_ERROR_NONE)
1320                 BT_ERR("_bt_socket_init Failed");
1321
1322         /* Initialize HFP Audio Gateway */
1323         ret = _bt_audio_initialize(BT_AG_MODULE);
1324         if (ret != BLUETOOTH_ERROR_NONE)
1325                 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
1326
1327         /* Registering callback for receiving audio services searched */
1328         ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
1329         if (ret != BLUETOOTH_ERROR_NONE)
1330                 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
1331
1332         ret = _bt_hdp_init();
1333         if (ret != BLUETOOTH_ERROR_NONE)
1334                 BT_ERR("_bt_hdp_init Failed");
1335
1336         ret = _bt_gatt_init();
1337         if (ret != BLUETOOTH_ERROR_NONE)
1338                 BT_ERR("_bt_gatt_init Failed");
1339
1340         /* Initialize HF Client */
1341         ret = _bt_audio_initialize(BT_HFP_MODULE);
1342         if (ret != BLUETOOTH_ERROR_NONE)
1343                 BT_ERR("_bt_audio_initialize(BT_HFP_MODULE) Failed");
1344
1345         return BLUETOOTH_ERROR_NONE;
1346 }
1347
1348 int _bt_cleanup_profiles(void)
1349 {
1350         /* TODO: Cleanup bluetooth profiles */
1351         _bt_hidhost_deinitialize();
1352         _bt_socket_deinit();
1353 #if 0
1354         /* TODO: Cleanup bluetooth audio profiles */
1355         //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
1356         //_bt_audio_deinitialize(BT_AVRCP_MODULE);
1357         //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
1358         //_bt_audio_deinitialize(BT_AG_MODULE);
1359         //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
1360         //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
1361
1362         a2dp_init_pending = TRUE;
1363 #endif
1364         _bt_hdp_deinit();
1365         _bt_gatt_deinit();
1366
1367         return BLUETOOTH_ERROR_NONE;
1368 }
1369
1370 /* OAL post initialization handler */
1371 static void __bt_post_oal_init(void)
1372 {
1373         int ret;
1374         int status = VCONFKEY_BT_STATUS_OFF;
1375
1376         BT_DBG("OAL initialized");
1377         if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0)
1378                 BT_ERR("Fail to get the enabled value");
1379
1380 #if 0
1381         /* Update Bluetooth Status to OFF */
1382         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1383                 BT_ERR("Set vconf failed\n");
1384 #endif
1385
1386         if (status & VCONFKEY_BT_STATUS_ON) {
1387                 ret = _bt_enable_adapter();
1388                 if (ret != BLUETOOTH_ERROR_NONE)
1389                         BT_ERR("_bt_enable_adapter failed with error: %d", ret);
1390         }
1391 }
1392
1393 /* OAL initialization handler */
1394 static void __bt_handle_oal_initialisation(oal_event_t event)
1395 {
1396         BT_DBG("");
1397
1398         switch (event) {
1399         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1400                 __bt_post_oal_init();
1401                 break;
1402         case OAL_EVENT_OAL_INITIALISED_FAILED:
1403                 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1404                 g_idle_add(_bt_terminate_service, NULL);
1405                 break;
1406         default:
1407                 BT_ERR("Unknown Event");
1408                 break;
1409         }
1410 }
1411
1412 static gboolean __bt_is_service_request_present(int service_function)
1413 {
1414         GSList *l;
1415         invocation_info_t *req_info;
1416
1417         BT_DBG("+");
1418
1419         /* Get method invocation context */
1420         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1421                 req_info = l->data;
1422                 if (req_info && req_info->service_function == service_function)
1423                         return TRUE;
1424         }
1425
1426         BT_DBG("-");
1427         return FALSE;
1428 }
1429
1430 /* Internal functions of core adapter service */
1431 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1432 {
1433         GSList *l;
1434         GArray *out_param;
1435         invocation_info_t *req_info;
1436         BT_INFO("+");
1437
1438         /* Get method invocation context */
1439         for (l = _bt_get_invocation_list(); l != NULL; ) {
1440                 req_info = l->data;
1441                 l = g_slist_next(l);
1442                 if (req_info == NULL || req_info->service_function != service_function)
1443                         continue;
1444
1445                 /* Create out param */
1446                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1447
1448                 switch (service_function) {
1449                 case BT_ENABLE_ADAPTER:
1450                 case BT_DISABLE_ADAPTER: {
1451                         gboolean done = TRUE;
1452                         g_array_append_vals(out_param, &done, sizeof(gboolean));
1453                         break;
1454                 }
1455                 case BT_GET_CONNECTABLE:
1456                 case BT_GET_LOCAL_NAME:
1457                 case BT_GET_LOCAL_ADDRESS:
1458                 case BT_GET_LOCAL_VERSION:
1459                         g_array_append_vals(out_param, user_data, size);
1460                         break;
1461                 case BT_IS_SERVICE_USED: {
1462                         unsigned int i;
1463                         gboolean used = FALSE;
1464                         unsigned char *uuid;
1465                         char uuid_str[BT_UUID_STRING_SIZE];
1466                         char *request_uuid = req_info->user_data;
1467                         service_uuid_t *service_list = user_data;
1468
1469                         BT_INFO("Check for service uuid: %s", request_uuid);
1470                         for (i = 0; i < size; i++) {
1471                                 uuid = service_list[i].uuid;
1472                                 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1473                                 BT_INFO("Adapter Service: [%s]", uuid_str);
1474                                 if (strcasecmp(uuid_str, request_uuid) == 0) {
1475                                         BT_INFO("UUID matched!!");
1476                                         used = TRUE;
1477                                         break;
1478                                 }
1479                         }
1480
1481                         g_array_append_vals(out_param, &used, sizeof(gboolean));
1482                         break;
1483                 }
1484                 default:
1485                         BT_ERR("Unknown service function[%d]", service_function);
1486                 }
1487
1488                 _bt_service_method_return(req_info->context, out_param, req_info->result);
1489                 g_array_free(out_param, TRUE);
1490                 /* Now free invocation info for this request*/
1491                 _bt_free_info_from_invocation_list(req_info);
1492         }
1493 }
1494
1495 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1496 {
1497         char *phone_name = NULL;
1498         char *ptr = NULL;
1499
1500         if (node == NULL)
1501                 return;
1502
1503         if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1504                 phone_name = vconf_keynode_get_str(node);
1505
1506                 if (phone_name && strlen(phone_name) != 0) {
1507                         if (!g_utf8_validate(phone_name, -1,
1508                                         (const char **)&ptr))
1509                                 *ptr = '\0';
1510
1511                         _bt_set_local_name(phone_name);
1512                 }
1513         }
1514 }
1515
1516 /* Request return handlings */
1517 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1518 {
1519         int result = BLUETOOTH_ERROR_NONE;
1520
1521         BT_INFO("__bt_adapter_post_set_enabled>>");
1522
1523         if (TIZEN_PROFILE_TV || !headed_plugin_info->plugin_headed_enabled) {
1524                 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1525                                 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1526                         BT_ERR("Fail to set discoverable mode");
1527         } else {
1528                 __bt_set_visible_mode();
1529         }
1530
1531         /* add the vconf noti handler */
1532         if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1533                         (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1534                 BT_ERR("DEVICE_NAME key changed notification registration failed");
1535
1536         __bt_set_local_name();
1537
1538         /* Get All properties */
1539         if (OAL_STATUS_SUCCESS != adapter_get_properties())
1540                 BT_ERR("adapter_get_properties failed");
1541
1542         __bt_adapter_state_set_status(BT_ACTIVATED);
1543
1544         /*Send BT Enabled event to application */
1545         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1546                         g_variant_new("(i)", result));
1547
1548         BT_PERMANENT_LOG("BT Enabled");
1549
1550         return FALSE;
1551 }
1552
1553 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1554 {
1555         BT_INFO("_bt_adapter_post_set_disabled>>");
1556
1557         if (!TIZEN_PROFILE_TV) {
1558                 /* Add Adapter disabled post processing codes */
1559                 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1560                                 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1561                         BT_ERR("vconf_ignore_key_changed failed");
1562         }
1563
1564         /* bt-service should be terminated when BT is off */
1565         if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1566                 /* TODO: Implement to check if it is the recovery mode or not */
1567                 if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_ON) {
1568                         /* Keep bt-service to handle Flight mode OFF */
1569                         BT_DBG("Deactived due to flight mode. Keep bt-service");
1570                         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1571                         __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_NONE);
1572                 } else if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_OFF) {
1573                         BT_DBG("Durning deactivation, receive Flight mode Off. Enable BT adapter");
1574                         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1575                         _bt_service_initialize();
1576                         _bt_enable_adapter();
1577                         __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_NONE);
1578                 } else
1579                         _bt_reliable_terminate_service(NULL);
1580         } else {
1581                 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1582         }
1583
1584         return FALSE;
1585 }
1586
1587 static void __bt_adapter_update_bt_enabled(void)
1588 {
1589         BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1590         if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1591                 BT_ERR("Bluetooth profile init failed");
1592
1593         _bt_device_handle_adapter_state(TRUE);
1594
1595         /* Update Bluetooth Status to notify other modules */
1596         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1597                 BT_ERR("Set vconf failed\n");
1598
1599         if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1600                 BT_ERR("Set vconf failed\n");
1601
1602         /* TODO:Add timer function to handle any further post processing */
1603         g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1604 }
1605
1606 static void __bt_adapter_update_bt_disabled(void)
1607 {
1608         int power_off_status = 0;
1609         int ret;
1610
1611         BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1612         _bt_cleanup_profiles();
1613
1614         _bt_device_handle_adapter_state(FALSE);
1615
1616 #ifdef TIZEN_FEATURE_BT_AVC_TARGET
1617         /* Send the information to Absolute Volume Controller */
1618         _bt_audio_handle_adapter_disabled();
1619 #endif
1620
1621         /* Update the vconf BT status in normal Deactivation case only */
1622         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1623         BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1624
1625         /* Update Bluetooth Status to notify other modules */
1626         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1627                 BT_ERR("Set vconf failed");
1628
1629         if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1630                 BT_ERR("Set vconf failed\n");
1631
1632         /* TODO:Add timer function to handle any further post processing */
1633         g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1634
1635         /* Pending BT Disabled event before bt-service's termination */
1636 }
1637
1638 static void __bt_adapter_state_set_status(bt_status_t status)
1639 {
1640         BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1641         adapter_state = status;
1642 }
1643
1644 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1645 {
1646         BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1647         adapter_discovery_state = status;
1648 }
1649
1650 static void __bt_adapter_state_change_callback(int bt_status)
1651 {
1652         BT_INFO("BT adapter status changed [%d]", bt_status);
1653
1654         switch (bt_status) {
1655         case BT_DEACTIVATED:
1656                 __bt_adapter_state_set_status(BT_TERMINATING);
1657
1658                 /* Adapter is disabled, unregister event handlers */
1659                 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1660                 //_bt_deinit_device_event_handler();
1661
1662                 /* Add Adapter disabled post processing codes */
1663                 __bt_adapter_update_bt_disabled();
1664                 break;
1665         case BT_ACTIVATED:
1666                 /* Add Adapter enabled post processing codes */
1667                 if (timer_id > 0) {
1668                         BT_DBG("g_source is removed");
1669                         g_source_remove(timer_id);
1670                         timer_id = 0;
1671                 }
1672
1673                 /*If adapter gets activated when flight mode is ON*/
1674                 if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_ON) {
1675                         BT_DBG("Receive Flight mode On. Disable BT adapter");
1676                         _bt_disable_adapter();
1677                         return;
1678                 }
1679
1680                 __bt_adapter_update_bt_enabled();
1681                 break;
1682         default:
1683                 BT_ERR("Incorrect Bluetooth adapter state changed status");
1684
1685         }
1686 }
1687
1688 int _bt_set_adapter_request_state(int enable)
1689 {
1690         return oal_set_adapter_request_state(enable);
1691 }
1692
1693 int _bt_set_le_request_state(int enable)
1694 {
1695         return oal_set_le_request_state(enable);
1696 }
1697
1698 static int __bt_adapter_state_handle_request(gboolean enable)
1699 {
1700         int result = BLUETOOTH_ERROR_NONE;
1701
1702         switch (adapter_state) {
1703         case BT_ACTIVATING:
1704         case BT_DEACTIVATING:
1705                 BT_INFO("Should not be callded : state [%d]", adapter_state);
1706                 return BLUETOOTH_ERROR_INTERNAL;
1707                 break;
1708         case BT_ACTIVATED: {
1709                 BT_INFO("Adapter is currently in activated state, state [%d]",
1710                                 adapter_state);
1711                 if (enable) {
1712                         return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1713                 } else {
1714                         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1715                                         adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1716                                 /*TODO Stop Discovery*/
1717                                 __bt_adapter_update_discovery_status(FALSE);
1718                         }
1719                         result = adapter_disable();
1720                         if (result != OAL_STATUS_SUCCESS) {
1721                                 BT_ERR("adapter_enable failed: [%d]", result);
1722                                 result = BLUETOOTH_ERROR_INTERNAL;
1723                                 /*TODO: perform if anything more needs to be done to handle failure */
1724                         } else {
1725                                 /* TODO: To be handled */
1726                                 __bt_adapter_state_set_status(BT_DEACTIVATING);
1727                                 result = BLUETOOTH_ERROR_NONE;
1728                         }
1729                 }
1730                 break;
1731         }
1732         case BT_DEACTIVATED: {
1733                 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1734                                 adapter_state);
1735                 if (!enable) {
1736                         return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1737                 } else {
1738                         result = adapter_enable();
1739                         if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1740                                 BT_ERR("adapter_enable failed: [%d]", result);
1741                                 adapter_disable();
1742                                 result = BLUETOOTH_ERROR_INTERNAL;
1743                                 /*TODO: perform if anything more needs to be done to handle failure */
1744                         } else {
1745                                 /* TODO: To be handled */
1746                                 __bt_adapter_state_set_status(BT_ACTIVATING);
1747                                 result = BLUETOOTH_ERROR_NONE;
1748                         }
1749                 }
1750                 break;
1751         }
1752         default:
1753                 BT_ERR("Unknown state: %d", adapter_state);
1754                 break;
1755         }
1756
1757         if (enable && result == BLUETOOTH_ERROR_NONE) {
1758                 /* Adapter enable request is successful, setup event handlers */
1759                 _bt_service_register_event_handler_callback(
1760                                 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1761                 _bt_device_state_handle_callback_set_request();
1762         }
1763         return result;
1764 }
1765
1766 static int __bt_adapter_state_discovery_request(gboolean enable,
1767                 unsigned short max_response, unsigned short duration,
1768                 unsigned int mask, gboolean is_custom,
1769                 bt_discovery_role_type_t role)
1770 {
1771         int result = BLUETOOTH_ERROR_NONE;
1772
1773         BT_DBG("+");
1774         switch (adapter_discovery_state) {
1775         case ADAPTER_DISCOVERY_STARTED: {
1776                 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1777                                 adapter_discovery_state);
1778                 if (enable) {
1779                         return BLUETOOTH_ERROR_IN_PROGRESS;
1780                 } else {
1781                         result = adapter_stop_inquiry();
1782                         if (result != OAL_STATUS_SUCCESS) {
1783                                 BT_ERR("Discover stop failed: %d", result);
1784                                 result = BLUETOOTH_ERROR_INTERNAL;
1785                         } else {
1786                                 BT_ERR("Stop Discovery Triggered successfully");
1787                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1788                                 result = BLUETOOTH_ERROR_NONE;
1789                         }
1790                 }
1791                 break;
1792         }
1793         case ADAPTER_DISCOVERY_STARTING: {
1794                 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1795                                 adapter_discovery_state);
1796
1797                 result = enable ?  BLUETOOTH_ERROR_IN_PROGRESS :
1798                                         BLUETOOTH_ERROR_DEVICE_BUSY;
1799
1800                 break;
1801         }
1802         case ADAPTER_DISCOVERY_STOPPED: {
1803                 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1804                                 adapter_discovery_state);
1805                 if (!enable)
1806                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1807                 else {
1808                         BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1809
1810                         if (!is_custom)
1811                                 result = adapter_start_inquiry(duration);
1812                         else
1813                                 result = adapter_start_custom_inquiry(role);
1814
1815                         if (result != OAL_STATUS_SUCCESS) {
1816                                 BT_ERR("Start Discovery failed: %d", result);
1817                                 result = BLUETOOTH_ERROR_INTERNAL;
1818                         } else {
1819                                 BT_ERR("Start Discovery Triggered successfully");
1820                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1821                                 result = BLUETOOTH_ERROR_NONE;
1822                         }
1823                 }
1824                 break;
1825         }
1826         case ADAPTER_DISCOVERY_STOPPING: {
1827                 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1828                                 adapter_discovery_state);
1829
1830                 result = enable ?  BLUETOOTH_ERROR_DEVICE_BUSY :
1831                                         BLUETOOTH_ERROR_NOT_IN_OPERATION;
1832
1833                 break;
1834         }
1835         default:
1836                 BT_ERR("Unknown state: %d", adapter_discovery_state);
1837                 break;
1838         }
1839
1840         BT_DBG("-");
1841         return result;
1842 }
1843
1844 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1845 {
1846         BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1847         GVariant *param = NULL;
1848         int result = BLUETOOTH_ERROR_NONE;
1849
1850         switch (bt_discovery_status) {
1851         case ADAPTER_DISCOVERY_STOPPED:
1852         {
1853                 __bt_adapter_update_discovery_status(bt_discovery_status);
1854                 param = g_variant_new("(i)", result);
1855                 _bt_send_event(BT_ADAPTER_EVENT,
1856                                 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1857                                 param);
1858                 break;
1859         }
1860         case ADAPTER_DISCOVERY_STARTED:
1861         {
1862                 __bt_adapter_update_discovery_status(bt_discovery_status);
1863                 param = g_variant_new("(i)", result);
1864                 _bt_send_event(BT_ADAPTER_EVENT,
1865                                 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1866                                 param);
1867                 break;
1868         }
1869         default:
1870                 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1871         }
1872 }
1873
1874 static void __bt_set_visible_mode(void)
1875 {
1876         int timeout = 0;
1877 #ifdef TIZEN_FEATURE_BT_DPM
1878         int discoverable_state = DPM_BT_ERROR;
1879 #endif
1880
1881         if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1882                 BT_ERR("Fail to get the timeout value");
1883
1884 #ifdef TIZEN_FEATURE_BT_DPM
1885         _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1886         if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
1887                 if (_bt_set_discoverable_mode(
1888                                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1889                                         timeout) != BLUETOOTH_ERROR_NONE) {
1890                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1891                                 BT_ERR("Set vconf failed");
1892                 }
1893         }
1894 #else
1895         if (timeout == -1) {
1896                 if (_bt_set_discoverable_mode(
1897                                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1898                                         timeout) != BLUETOOTH_ERROR_NONE) {
1899                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1900                                 BT_ERR("Set vconf failed");
1901                 }
1902         }
1903 #endif
1904 }
1905
1906 static void __bt_set_local_name(void)
1907 {
1908         char *phone_name = NULL;
1909         char *ptr = NULL;
1910
1911         phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1912
1913         if (!phone_name)
1914                 return;
1915
1916         if (strlen(phone_name) != 0) {
1917                 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1918                         *ptr = '\0';
1919
1920                 _bt_set_local_name(phone_name);
1921         }
1922         free(phone_name);
1923 }
1924
1925 void _bt_adapter_set_status(bt_status_t status)
1926 {
1927         BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1928         adapter_state = status;
1929 }
1930
1931 bt_status_t _bt_adapter_get_status(void)
1932 {
1933         return adapter_state;
1934 }
1935
1936 void _bt_set_disabled(int result)
1937 {
1938         int power_off_status = 0;
1939         int ret;
1940         int ret_pm_ignore;
1941         int pm_ignore_mode = 0;
1942
1943         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1944         BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1945
1946         ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1947
1948         /* Update the vconf BT status in normal Deactivation case only */
1949         if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1950                 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1951
1952                 BT_DBG("Update vconf for BT normal Deactivation");
1953
1954                 if (result == BLUETOOTH_ERROR_TIMEOUT)
1955                         if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1956                                 BT_ERR("Set vconf failed");
1957
1958                 /* Update Bluetooth Status to notify other modules */
1959                 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1960                         BT_ERR("Set vconf failed");
1961
1962                 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1963                                                         EVT_VAL_BT_OFF) != ES_R_OK)
1964                         BT_ERR("Fail to set value");
1965         }
1966
1967         if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1968                 BT_ERR("Set vconf failed\n");
1969
1970         _bt_adapter_set_status(BT_DEACTIVATED);
1971         __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1972
1973         /* Send BT Disabled event to application */
1974         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1975                         g_variant_new("(i)", result));
1976
1977         BT_INFO("Adapter disabled");
1978         BT_PERMANENT_LOG("BT Disabled");
1979 }
1980