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