Remove compiler warnings
[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 <syspopup_caller.h>
28 #include <aul.h>
29 #include <eventsystem.h>
30 #include <bundle_internal.h>
31
32 #include "alarm.h"
33
34 /*bt-service headers */
35 #include "bt-internal-types.h"
36 #include "bt-service-common.h"
37 #include "bt-service-util.h"
38 #include "bt-service-main.h"
39 #include "bt-service-core-adapter.h"
40 #include "bt-service-core-device.h"
41 #include "bt-service-event-receiver.h"
42 #include "bt-request-handler.h"
43 #include "bt-service-event.h"
44 #include "bt-service-audio-common.h"
45 #include "bt-service-core-adapter-le.h"
46 #include "bt-service-gatt.h"
47
48 #ifdef TIZEN_DPM_ENABLE
49 #include "bt-service-dpm.h"
50 #endif
51 #include "bt-service-hidhost.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 /*This file will contain state machines related to adapter and remote device */
61
62 /* Global variables */
63 typedef struct {
64         guint event_id;
65         int timeout;
66         time_t start_time;
67         gboolean alarm_init;
68         int alarm_id;
69 } bt_adapter_timer_t;
70
71 static bt_adapter_timer_t visible_timer;
72
73 static guint timer_id = 0;
74
75 /* Adapter default states */
76 static bt_status_t adapter_state = BT_DEACTIVATED;
77 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
78
79 /* Forward declarations */
80 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
81 static void __bt_post_oal_init(void);
82 static void __bt_handle_oal_initialisation(oal_event_t event);
83 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
84 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
85 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
86 static void __bt_adapter_update_bt_enabled(void);
87 static void __bt_adapter_update_bt_disabled(void);
88 static void __bt_adapter_state_set_status(bt_status_t status);
89 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
90 static void __bt_adapter_state_change_callback(int bt_status);
91 static int __bt_adapter_state_handle_request(gboolean enable);
92 static int __bt_adapter_state_discovery_request(gboolean enable,
93                 unsigned short max_response, unsigned short duration, unsigned int mask);
94 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
95 static gboolean __bt_is_service_request_present(int service_function);
96 #ifndef TIZEN_TV
97 static void __bt_set_visible_mode(void);
98 static void __bt_set_local_name(void);
99 #endif
100
101 /* Initialize BT stack (Initialize OAL layer) */
102 int _bt_stack_init(void)
103 {
104         int ret;
105
106         BT_INFO("[bt-service] Start to initialize BT stack");
107         /* Adapter enable request is successful, setup event handlers */
108         _bt_service_register_event_handler_callback(
109                         BT_ADAPTER_MODULE, __bt_adapter_event_handler);
110
111         ret = oal_bt_init(_bt_service_oal_event_receiver);
112
113         if (OAL_STATUS_PENDING == ret) {
114                 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
115                 return BLUETOOTH_ERROR_NONE;
116         } else if (OAL_STATUS_SUCCESS != ret) {
117                 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
118                 return BLUETOOTH_ERROR_INTERNAL;
119         }
120
121         return BLUETOOTH_ERROR_NONE;
122 }
123
124 int _bt_enable_adapter(void)
125 {
126         return __bt_adapter_state_handle_request(TRUE);
127 }
128
129 int _bt_disable_adapter(void)
130 {
131         return __bt_adapter_state_handle_request(FALSE);
132 }
133
134
135 int _bt_start_discovery(unsigned short max_response,
136                 unsigned short duration, unsigned int cod_mask)
137 {
138         return __bt_adapter_state_discovery_request(TRUE, max_response, duration, cod_mask);
139 }
140
141 int _bt_cancel_discovery(void)
142 {
143         return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0);
144 }
145
146 gboolean _bt_is_discovering(void)
147 {
148         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
149                         || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
150                 return TRUE;
151         else
152                 return FALSE;
153 }
154
155 int _bt_get_local_address(void)
156 {
157         int result;
158
159         BT_DBG("+");
160
161         result =  adapter_get_address();
162         if (result != OAL_STATUS_SUCCESS) {
163                 BT_ERR("adapter_get_address failed: %d", result);
164                 result = BLUETOOTH_ERROR_INTERNAL;
165         } else
166                 result = BLUETOOTH_ERROR_NONE;
167
168         BT_DBG("-");
169         return result;
170 }
171
172 int _bt_get_local_version(void)
173 {
174         int result;
175         BT_DBG("+");
176
177         result =  adapter_get_version();
178         if (result != OAL_STATUS_SUCCESS) {
179                 BT_ERR("adapter_get_version failed: %d", result);
180                 result = BLUETOOTH_ERROR_INTERNAL;
181         } else
182                 result = BLUETOOTH_ERROR_NONE;
183
184         BT_DBG("-");
185         return result;
186 }
187
188 int _bt_get_local_name(void)
189 {
190         int result;
191
192         BT_DBG("+");
193
194         result =  adapter_get_name();
195         if (result != OAL_STATUS_SUCCESS) {
196                 BT_ERR("adapter_get_name failed: %d", result);
197                 result = BLUETOOTH_ERROR_INTERNAL;
198         } else
199                 result = BLUETOOTH_ERROR_NONE;
200
201         BT_DBG("-");
202         return result;
203 }
204
205 int _bt_set_local_name(char *local_name)
206 {
207         int result = BLUETOOTH_ERROR_NONE;
208         BT_DBG("+");
209
210         retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
211
212         result =  adapter_set_name(local_name);
213         if (result != OAL_STATUS_SUCCESS) {
214                 BT_ERR("adapter_set_name failed: %d", result);
215                 result = BLUETOOTH_ERROR_INTERNAL;
216         } else
217                 result = BLUETOOTH_ERROR_NONE;
218
219         BT_DBG("-");
220         return result;
221 }
222
223 int _bt_get_discoverable_mode(int *mode)
224 {
225         int scan_mode = 0;
226         int timeout = 0;
227
228         BT_DBG("+");
229
230         retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
231
232         adapter_is_discoverable(&scan_mode);
233         if (TRUE == scan_mode) {
234                 adapter_get_discoverable_timeout(&timeout);
235                 if (timeout > 0)
236                         *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
237                 else
238                         *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
239         } else {
240                 adapter_is_connectable(&scan_mode);
241                 if(scan_mode == TRUE)
242                         *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
243                 else {
244                         /*
245                          * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
246                          * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
247                          * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
248                          */
249                         *mode = -1;
250                 }
251         }
252
253         BT_DBG("-");
254         return BLUETOOTH_ERROR_NONE;
255 }
256
257 int _bt_get_timeout_value(int *timeout)
258 {
259         time_t current_time;
260         int time_diff;
261
262         /* Take current time */
263         time(&current_time);
264         time_diff = difftime(current_time, visible_timer.start_time);
265
266         BT_DBG("Time diff = %d\n", time_diff);
267         *timeout = visible_timer.timeout - time_diff;
268
269         return BLUETOOTH_ERROR_NONE;
270 }
271
272 static void __bt_visibility_alarm_remove()
273 {
274         if (visible_timer.event_id > 0) {
275                 g_source_remove(visible_timer.event_id);
276                 visible_timer.event_id = 0;
277         }
278
279         if (visible_timer.alarm_id > 0) {
280                 alarmmgr_remove_alarm(visible_timer.alarm_id);
281                 visible_timer.alarm_id = 0;
282         }
283 }
284
285 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
286 {
287         int result = BLUETOOTH_ERROR_NONE;
288         int timeout = 0;
289
290         BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
291
292         if (alarm_id != visible_timer.alarm_id)
293                 return 0;
294
295         if (visible_timer.event_id) {
296                 _bt_send_event(BT_ADAPTER_EVENT,
297                                 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
298                                 g_variant_new("(in)", result, timeout));
299                 g_source_remove(visible_timer.event_id);
300                 visible_timer.event_id = 0;
301                 visible_timer.timeout = 0;
302
303 #ifndef TIZEN_PROFILE_WEARABLE
304                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
305                         BT_ERR("Set vconf failed\n");
306 #endif
307         }
308         /* Switch Off visibility in Bluez */
309         _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
310         visible_timer.alarm_id = 0;
311         return 0;
312 }
313
314 static gboolean __bt_timeout_handler(gpointer user_data)
315 {
316         int result = BLUETOOTH_ERROR_NONE;
317         time_t current_time;
318         int time_diff;
319
320         /* Take current time */
321         time(&current_time);
322         time_diff = difftime(current_time, visible_timer.start_time);
323
324         /* Send event to application */
325         _bt_send_event(BT_ADAPTER_EVENT,
326                         BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
327                         g_variant_new("(in)", result, time_diff));
328
329         if (visible_timer.timeout <= time_diff) {
330                 g_source_remove(visible_timer.event_id);
331                 visible_timer.event_id = 0;
332                 visible_timer.timeout = 0;
333
334 #ifndef TIZEN_PROFILE_WEARABLE
335                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
336                         BT_ERR("Set vconf failed\n");
337 #endif
338                 return FALSE;
339         }
340
341         return TRUE;
342 }
343
344 static void __bt_visibility_alarm_create()
345 {
346         alarm_id_t alarm_id;
347         int result;
348
349         result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
350                         0, NULL, &alarm_id);
351         if (result < 0) {
352                 BT_ERR("Failed to create alarm error = %d\n", result);
353         } else {
354                 BT_DBG("Alarm created = %d\n", alarm_id);
355                 visible_timer.alarm_id = alarm_id;
356         }
357 }
358
359 static int __bt_set_visible_time(int timeout)
360 {
361         int result;
362
363         __bt_visibility_alarm_remove();
364
365         visible_timer.timeout = timeout;
366
367 #ifndef TIZEN_PROFILE_WEARABLE
368 #ifdef TIZEN_DPM_ENABLE
369         if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
370 #endif
371                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
372                         BT_ERR("Set vconf failed");
373 #ifdef TIZEN_DPM_ENABLE
374         }
375 #endif
376 #endif
377
378         if (timeout <= 0)
379                 return BLUETOOTH_ERROR_NONE;
380
381         if (!visible_timer.alarm_init) {
382                 /* Set Alarm timer to switch off BT */
383                 result = alarmmgr_init("bt-service");
384                 if (result != 0)
385                         return BLUETOOTH_ERROR_INTERNAL;
386
387                 visible_timer.alarm_init = TRUE;
388         }
389
390         result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
391         if (result != 0)
392                 return BLUETOOTH_ERROR_INTERNAL;
393
394         /* Take start time */
395         time(&(visible_timer.start_time));
396         visible_timer.event_id = g_timeout_add_seconds(1,
397                         __bt_timeout_handler, NULL);
398
399         __bt_visibility_alarm_create();
400
401         return BLUETOOTH_ERROR_NONE;
402 }
403
404 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
405 {
406         int result;
407
408         BT_DBG("+");
409
410         BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
411
412 #ifdef TIZEN_DPM_ENABLE
413         if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
414                         _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
415                 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
416                 return BLUETOOTH_ERROR_ACCESS_DENIED;
417         }
418         if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
419                         _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
420                 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
421                 return BLUETOOTH_ERROR_ACCESS_DENIED;
422         }
423 #endif
424
425         switch (discoverable_mode) {
426         case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
427                 result = adapter_set_connectable(TRUE);
428                 timeout = 0;
429                 break;
430         case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
431                 result = adapter_set_discoverable();
432                 timeout = 0;
433                 break;
434         case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
435                 result = adapter_set_discoverable();
436                 break;
437         default:
438                 return BLUETOOTH_ERROR_INVALID_PARAM;
439         }
440
441         if (result != OAL_STATUS_SUCCESS) {
442                 BT_ERR("set scan mode failed %d", result);
443                 return BLUETOOTH_ERROR_INTERNAL;
444         }
445
446         result = adapter_set_discoverable_timeout(timeout);
447         if (result != OAL_STATUS_SUCCESS) {
448                 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
449                 return BLUETOOTH_ERROR_INTERNAL;
450         }
451
452         if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
453                 timeout = -1;
454
455         result = __bt_set_visible_time(timeout);
456
457         BT_DBG("-");
458         return result;
459 }
460
461 gboolean _bt_is_connectable(void)
462 {
463         int connectable = 0;
464         int result;
465
466         BT_DBG("+");
467
468         adapter_is_connectable(&connectable);
469         if (connectable)
470                 result = TRUE;
471         else
472                 result = FALSE;
473
474         BT_DBG("Connectable: [%s]", result ? "TRUE":"FALSE");
475         BT_DBG("-");
476         return result;
477 }
478
479 int _bt_set_connectable(gboolean connectable)
480 {
481         int result = BLUETOOTH_ERROR_NONE;
482
483         BT_DBG("+");
484         result =  adapter_set_connectable(connectable);
485         if (result != OAL_STATUS_SUCCESS) {
486                 BT_ERR("adapter_set_connectable failed: %d", result);
487                 result = BLUETOOTH_ERROR_INTERNAL;
488         } else
489                 result = BLUETOOTH_ERROR_NONE;
490
491         BT_DBG("-");
492         return result;
493 }
494
495 int _bt_is_service_used(void)
496 {
497         int result;
498
499         BT_DBG("+");
500
501         result =  adapter_get_service_uuids();
502         if (result != OAL_STATUS_SUCCESS) {
503                 BT_ERR("adapter_get_service_uuids failed: %d", result);
504                 result = BLUETOOTH_ERROR_INTERNAL;
505         } else {
506                 result = BLUETOOTH_ERROR_NONE;
507         }
508
509         BT_DBG("-");
510         return result;
511 }
512
513 int _bt_adapter_get_bonded_devices(void)
514 {
515         int result = BLUETOOTH_ERROR_NONE;
516
517         BT_DBG("+");
518         result =  adapter_get_bonded_devices();
519         if (result != OAL_STATUS_SUCCESS) {
520                 BT_ERR("adapter_get_bonded_devices 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 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
530 {
531         int result = BLUETOOTH_ERROR_NONE;
532
533         BT_DBG("+");
534
535         switch(event_type) {
536         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
537         case OAL_EVENT_OAL_INITIALISED_FAILED:
538                 __bt_handle_oal_initialisation(event_type);
539                 break;
540         case OAL_EVENT_ADAPTER_ENABLED:
541                 __bt_adapter_state_change_callback(BT_ACTIVATED);
542                 break;
543         case OAL_EVENT_ADAPTER_DISABLED:
544                 __bt_adapter_state_change_callback(BT_DEACTIVATED);
545                 break;
546         case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
547                 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
548                 break;
549         case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
550                 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
551                 break;
552         case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
553                 bt_address_t *bd_addr = event_data;
554                 bluetooth_device_address_t local_address;
555
556                 /* Copy data */
557                 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
558                 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
559                                 local_address.addr[0], local_address.addr[1], local_address.addr[2],
560                                 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
561
562                 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
563                                 (void *) &local_address, sizeof(bluetooth_device_address_t));
564                 break;
565         }
566         case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
567                 char *name = event_data;
568                 BT_DBG("Adapter Name: %s", name);
569
570                 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
571                         bluetooth_device_name_t local_name;
572
573                         memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
574                         g_strlcpy(local_name.name,
575                                 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
576                         __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
577                                 (void *) &local_name, sizeof(bluetooth_device_name_t));
578                 } else {
579                         /* Send event to application */
580                         _bt_send_event(BT_ADAPTER_EVENT,
581                                         BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
582                                         g_variant_new("(is)", result, name));
583                 }
584                 break;
585         }
586         case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
587                 char *ver = event_data;
588                 bluetooth_version_t local_version;
589
590                 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
591                 g_strlcpy(local_version.version,
592                                 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
593                 BT_DBG("BT Version: %s", local_version.version);
594
595                 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
596                                 (void *) &local_version, sizeof(bluetooth_version_t));
597                 break;
598         }
599         case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
600                 int mode = -1;
601                 gboolean connectable = FALSE;
602
603                 BT_INFO("Adapter discoverable mode:"
604                                 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
605                 _bt_send_event(BT_ADAPTER_EVENT,
606                                 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
607                                 g_variant_new("(b)", connectable));
608
609                 _bt_send_event(BT_ADAPTER_EVENT,
610                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
611                                 g_variant_new("(in)", result, mode));
612                 break;
613         }
614         case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
615                 int mode;
616                 gboolean connectable = TRUE;
617
618                 BT_INFO("Adapter discoverable mode:"
619                                 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
620                 _bt_send_event(BT_ADAPTER_EVENT,
621                                 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
622                                 g_variant_new("(b)", connectable));
623
624                 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
625                 _bt_send_event(BT_ADAPTER_EVENT,
626                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
627                                 g_variant_new("(in)", result, mode));
628                 break;
629         }
630         case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
631                 int mode;
632
633                 BT_INFO("Adapter discoverable mode:"
634                                 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
635
636                 /* Send event to application */
637                 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
638                 _bt_send_event(BT_ADAPTER_EVENT,
639                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
640                                 g_variant_new("(in)", result, mode));
641
642                 break;
643         }
644         case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
645                 int *timeout = event_data;
646                 int mode;
647
648                 BT_INFO("Discoverable timeout: [%d]", *timeout);
649
650                 /* Send event to application */
651                 _bt_get_discoverable_mode(&mode);
652                 _bt_send_event(BT_ADAPTER_EVENT,
653                                 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
654                                 g_variant_new("(in)", result, mode));
655                 break;
656         }
657         case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
658                 int count;
659                 service_uuid_t *service_list;
660                 event_adapter_services_t *list = event_data;
661
662                 count = list->num;
663                 service_list = list->service_list;
664                 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
665                 break;
666         }
667         case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
668                 int i;
669                 int count;
670                 bluetooth_device_address_t *addr_list;
671
672                 event_device_list_t *bonded_device_list = event_data;
673                 count = bonded_device_list->num;
674
675                 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
676                 for (i = 0; i < count; i++) {
677                         memcpy(addr_list[i].addr,
678                                         bonded_device_list->devices[i].addr,
679                                         BLUETOOTH_ADDRESS_LENGTH);
680                 }
681
682                 BT_INFO("Adapter Bonded device List count: [%d]", count);
683                 __bt_adapter_handle_pending_requests(BT_GET_BONDED_DEVICES,
684                                 (void *)addr_list, bonded_device_list->num);
685                 break;
686         }
687         default:
688                 BT_ERR("Unhandled event..");
689                 break;
690         }
691
692         BT_DBG("-");
693 }
694
695 int _bt_init_profiles()
696 {
697         int ret;
698
699         /*TODO: Init bluetooth profiles */
700         ret = _bt_hidhost_initialize();
701         if (ret != BLUETOOTH_ERROR_NONE) {
702                 BT_ERR("_bt_hidhost_initialize Failed");
703                 return ret;
704         }
705         ret = _bt_socket_init();
706         if (ret != BLUETOOTH_ERROR_NONE) {
707                 BT_ERR("_bt_socket_init Failed");
708                 return ret;
709         }
710         /* Initialize A2DP Source */
711         ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
712         if (ret != BLUETOOTH_ERROR_NONE) {
713                 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
714                 return ret;
715         }
716
717         /* Initialize AVRCP Target */
718         ret = _bt_audio_initialize(BT_AVRCP_MODULE);
719         if (ret != BLUETOOTH_ERROR_NONE) {
720                 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
721                 return ret;
722         }
723
724         /* Initialize A2DP Sink */
725         ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
726         if (ret != BLUETOOTH_ERROR_NONE) {
727                 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
728                 return ret;
729         }
730         /* Initialize HFP Audio Gateway */
731         ret = _bt_audio_initialize(BT_AG_MODULE);
732         if (ret != BLUETOOTH_ERROR_NONE) {
733                 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
734                 return ret;
735         }
736         /* Initialize AVRCP Controller */
737         ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
738         if (ret != BLUETOOTH_ERROR_NONE) {
739                 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
740                 return ret;
741         }
742         /* Registering callback for receiving audio services searched */
743         ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
744         if (ret != BLUETOOTH_ERROR_NONE) {
745                 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
746                 return ret;
747         }
748
749         ret = _bt_hdp_init();
750         if (ret != BLUETOOTH_ERROR_NONE) {
751                 BT_ERR("_bt_hdp_init Failed");
752                 return ret;
753         }
754
755         ret = _bt_le_init();
756         if (ret != BLUETOOTH_ERROR_NONE) {
757                 BT_ERR("_bt_le_init Failed");
758                 return ret;
759         }
760
761         ret = _bt_gatt_init();
762         if (ret != BLUETOOTH_ERROR_NONE) {
763                 BT_ERR("_bt_gatt_init Failed");
764                 return ret;
765         }
766
767         return BLUETOOTH_ERROR_NONE;
768 }
769
770 int _bt_cleanup_profiles(void)
771 {
772         /* TODO: Cleanup bluetooth profiles */
773         _bt_hidhost_deinitialize();
774         _bt_socket_deinit();
775 #if 0
776         /* TODO: Cleanup bluetooth audio profiles */
777         //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
778         //_bt_audio_deinitialize(BT_AVRCP_MODULE);
779         //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
780         //_bt_audio_deinitialize(BT_AG_MODULE);
781         //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
782         //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
783 #endif
784         _bt_hdp_deinit();
785
786         return BLUETOOTH_ERROR_NONE;
787 }
788
789 /* OAL post initialization handler */
790 static void __bt_post_oal_init(void)
791 {
792         int ret;
793         int status = VCONFKEY_BT_STATUS_OFF;
794
795         BT_DBG("OAL initialized, Init profiles..");
796         ret = _bt_init_profiles();
797         if (ret != BLUETOOTH_ERROR_NONE)
798                 BT_ERR("Bluetooth profile init error: %d", ret);
799
800         if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0) {
801                 BT_ERR("Fail to get the enabled value");
802         }
803
804         /* Update Bluetooth Status to OFF */
805         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
806                 BT_ERR("Set vconf failed\n");
807
808         if (status & VCONFKEY_BT_STATUS_ON) {
809                 ret = _bt_enable_adapter();
810                 if (ret != BLUETOOTH_ERROR_NONE)
811                         BT_ERR("_bt_enable_adapter failed with error: %d", ret);
812         }
813 }
814
815 /* OAL initialization handler */
816 static void __bt_handle_oal_initialisation(oal_event_t event)
817 {
818         BT_DBG("");
819
820         switch(event) {
821         case OAL_EVENT_OAL_INITIALISED_SUCCESS:
822                 __bt_post_oal_init();
823                 break;
824         case OAL_EVENT_OAL_INITIALISED_FAILED:
825                 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
826                 g_idle_add(_bt_terminate_service, NULL);
827                 break;
828         default:
829                 BT_ERR("Unknown Event");
830                 break;
831         }
832 }
833
834 static gboolean __bt_is_service_request_present(int service_function)
835 {
836         GSList *l;
837         invocation_info_t *req_info;
838
839         BT_DBG("+");
840
841         /* Get method invocation context */
842         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
843                 req_info = l->data;
844                 if (req_info && req_info->service_function == service_function)
845                         return TRUE;
846         }
847
848         BT_DBG("-");
849         return FALSE;
850 }
851
852 /* Internal functions of core adapter service */
853 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
854 {
855         GSList *l;
856         GArray *out_param;
857         invocation_info_t *req_info;
858         BT_INFO("+");
859
860         /* Get method invocation context */
861         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
862                 req_info = l->data;
863                 if (req_info == NULL || req_info->service_function != service_function)
864                         continue;
865
866                 /* Create out param */
867                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
868
869                 switch(service_function) {
870                 case BT_ENABLE_ADAPTER:
871                 case BT_DISABLE_ADAPTER: {
872                         gboolean done = TRUE;
873                         g_array_append_vals(out_param, &done, sizeof(gboolean));
874                         break;
875                 }
876                 case BT_GET_LOCAL_NAME:
877                 case BT_GET_LOCAL_ADDRESS:
878                 case BT_GET_LOCAL_VERSION:
879                         g_array_append_vals(out_param, user_data, size);
880                         break;
881                 case BT_IS_SERVICE_USED: {
882                         unsigned int i;
883                         gboolean used = FALSE;
884                         unsigned char *uuid;
885                         char uuid_str[BT_UUID_STRING_SIZE];
886                         char *request_uuid = req_info->user_data;
887                         service_uuid_t *service_list = user_data;
888
889                         BT_INFO("Check for service uuid: %s", request_uuid);
890                         for (i = 0; i < size; i++) {
891                                 uuid = service_list[i].uuid;
892                                 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
893                                 BT_INFO("Adapter Service: [%s]", uuid_str);
894                                 if (strcasecmp(uuid_str, request_uuid) == 0) {
895                                         BT_INFO("UUID matched!!");
896                                         used = TRUE;
897                                         break;
898                                 }
899                         }
900
901                         g_array_append_vals(out_param, &used, sizeof(gboolean));
902                         break;
903                 }
904                 case BT_GET_BONDED_DEVICES: {
905                         bluetooth_device_address_t *addr_list = user_data;
906                         bonded_devices_req_info_t *bonded_devices_req_info;
907                         char address[BT_ADDRESS_STRING_SIZE];
908                         int count = size;
909                         int res = BLUETOOTH_ERROR_NONE;
910
911                         /*
912                          * BT_GET_BONDED_DEVICES is already processed for this request,
913                          * continue for next BT_GET_BONDED_DEVICES request if any
914                          */
915                         if (NULL != req_info->user_data)
916                                 continue;
917
918                         BT_DBG("BT_GET_BONDED_DEVICES: count = [%d]", count);
919                         /* No bonded devices, return method invocation */
920                         if (0 == count || !addr_list)
921                                 break;
922
923                         /* Save address list in user data  for futur reference. */
924                         bonded_devices_req_info = g_malloc0(sizeof(bonded_devices_req_info));
925                         if (!bonded_devices_req_info) {
926                                 BT_ERR("Memory allocation failed");
927                                 req_info->result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
928                                 g_free(addr_list);
929                                 break;
930                         }
931
932                         bonded_devices_req_info->count = count;
933                         bonded_devices_req_info->addr_list = addr_list;
934                         bonded_devices_req_info->out_param = out_param;
935                         req_info->user_data = bonded_devices_req_info;
936
937                         while (bonded_devices_req_info->count > 0) {
938                                 bonded_devices_req_info->count -= 1;
939                                 res = _bt_device_get_bonded_device_info(
940                                                 &addr_list[bonded_devices_req_info->count]);
941                                 if (BLUETOOTH_ERROR_NONE == res)
942                                         return;
943                                 else {
944                                         _bt_convert_addr_type_to_string((char *)address,
945                                                         addr_list[bonded_devices_req_info->count].addr);
946                                         BT_ERR("_bt_device_get_bonded_device_info Failed for [%s]", address);
947                                         if (bonded_devices_req_info->count == 0) {
948                                                 g_free(bonded_devices_req_info->addr_list);
949                                                 g_free(bonded_devices_req_info);
950                                                 req_info->user_data = NULL;
951                                         }
952                                 }
953                         }
954                         break;
955                 }
956                 default:
957                         BT_ERR("Unknown service function[%d]", service_function);
958                 }
959
960                 _bt_service_method_return(req_info->context, out_param, req_info->result);
961                 g_array_free(out_param, TRUE);
962                 /* Now free invocation info for this request*/
963                 _bt_free_info_from_invocation_list(req_info);
964         }
965 }
966
967 #ifndef TIZEN_TV
968 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
969 {
970         char *phone_name = NULL;
971         char *ptr = NULL;
972
973         if (node == NULL)
974                 return;
975
976         if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
977                 phone_name = vconf_keynode_get_str(node);
978
979                 if (phone_name && strlen(phone_name) != 0) {
980                         if (!g_utf8_validate(phone_name, -1,
981                                         (const char **)&ptr))
982                                 *ptr = '\0';
983
984                         _bt_set_local_name(phone_name);
985                 }
986         }
987 }
988 #endif
989
990 /* Request return handlings */
991 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
992 {
993         BT_INFO("__bt_adapter_post_set_enabled>>");
994
995 #ifndef TIZEN_TV
996         __bt_set_visible_mode();
997
998         /* add the vconf noti handler */
999         if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1000                         (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1001                 BT_ERR("DEVICE_NAME key changed notification registration failed");
1002
1003         __bt_set_local_name();
1004 #else
1005         if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1006                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1007                 BT_ERR("Fail to set discoverable mode");
1008 #endif
1009
1010         /* Get All properties */
1011         if (OAL_STATUS_SUCCESS != adapter_get_properties())
1012                 BT_ERR("adapter_get_properties failed");
1013
1014         /* Add Adapter enabled post processing codes */
1015         return FALSE;
1016 }
1017
1018 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1019 {
1020         BT_INFO("_bt_adapter_post_set_disabled>>");
1021
1022 #ifndef TIZEN_TV
1023         /* Add Adapter disabled post processing codes */
1024         if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1025                         (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1026                 BT_ERR("vconf_ignore_key_changed failed");
1027 #endif
1028
1029         return FALSE;
1030 }
1031
1032 static void __bt_adapter_update_bt_enabled(void)
1033 {
1034         int result = BLUETOOTH_ERROR_NONE;
1035         BT_INFO("_bt_adapter_update_bt_enabled>>");
1036         /* Update Bluetooth Status to notify other modules */
1037         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1038                 BT_ERR("Set vconf failed\n");
1039
1040         /* TODO:Add timer function to handle any further post processing */
1041         g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1042
1043         /*Return BT_ADAPTER_ENABLE Method invocation context */
1044         __bt_adapter_handle_pending_requests(BT_ENABLE_ADAPTER, NULL, 0);
1045         /*Send BT Enabled event to application */
1046         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1047                         g_variant_new("(i)", result));
1048 }
1049
1050 static void __bt_adapter_update_bt_disabled(void)
1051 {
1052         int result = BLUETOOTH_ERROR_NONE;
1053         BT_INFO("_bt_adapter_update_bt_disabled>>");
1054
1055         int power_off_status = 0;
1056         int ret;
1057
1058         /* Update the vconf BT status in normal Deactivation case only */
1059         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1060         BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1061
1062         /* Update Bluetooth Status to notify other modules */
1063         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1064                 BT_ERR("Set vconf failed");
1065
1066         /* TODO:Add timer function to handle any further post processing */
1067         g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1068
1069         /* Return BT_ADAPTER_DISABLE Method invocation context */
1070         __bt_adapter_handle_pending_requests(BT_DISABLE_ADAPTER, NULL, 0);
1071
1072         /* Send BT Disabled event to application */
1073         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1074                         g_variant_new("(i)", result));
1075 }
1076
1077 static void __bt_adapter_state_set_status(bt_status_t status)
1078 {
1079         BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1080         adapter_state = status;
1081 }
1082
1083 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1084 {
1085         BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1086         adapter_discovery_state = status;
1087 }
1088
1089 static void __bt_adapter_state_change_callback(int bt_status)
1090 {
1091         BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
1092
1093         switch (bt_status) {
1094         case BT_DEACTIVATED:
1095                 __bt_adapter_state_set_status(bt_status);
1096
1097                 /* Adapter is disabled, unregister event handlers */
1098                 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1099                 //_bt_deinit_device_event_handler();
1100
1101                 /* Add Adapter disabled post processing codes */
1102                 __bt_adapter_update_bt_disabled();
1103                 break;
1104         case BT_ACTIVATED:
1105                 __bt_adapter_state_set_status(bt_status);
1106                 /* Add Adapter enabled post processing codes */
1107                 if (timer_id > 0) {
1108                         BT_DBG("g_source is removed");
1109                         g_source_remove(timer_id);
1110                         timer_id = 0;
1111                 }
1112                 __bt_adapter_update_bt_enabled();
1113                 break;
1114         default:
1115                 BT_ERR("Incorrect Bluetooth adapter state changed status");
1116
1117         }
1118 }
1119
1120 static int __bt_adapter_state_handle_request(gboolean enable)
1121 {
1122         int result = BLUETOOTH_ERROR_NONE;
1123         BT_DBG("");
1124
1125         switch (adapter_state) {
1126         case BT_ACTIVATING: {
1127                 BT_INFO("Adapter is currently in activating state, state [%d]",
1128                                 adapter_state);
1129                 if (enable) {
1130                         return BLUETOOTH_ERROR_IN_PROGRESS;
1131                 } else {
1132                         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1133                                         adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1134                                 /*TODO Stop Discovery*/
1135                                 if (result != OAL_STATUS_SUCCESS)
1136                                         BT_ERR("Discover stop failed: %d", result);
1137                                 __bt_adapter_update_discovery_status(FALSE);
1138                         }
1139                         result = adapter_disable();
1140                         if (result != OAL_STATUS_SUCCESS) {
1141                                 BT_ERR("adapter_enable failed: [%d]", result);
1142                                 result = BLUETOOTH_ERROR_INTERNAL;
1143                                 /*TODO: perform if anything more needs to be done to handle failure */
1144                         } else {
1145                                 /* TODO: To be handled */
1146                                 __bt_adapter_state_set_status(BT_DEACTIVATING);
1147                                 result = BLUETOOTH_ERROR_NONE;
1148                         }
1149                 }
1150                 break;
1151         }
1152         case BT_ACTIVATED: {
1153                 BT_INFO("Adapter is currently in activated state, state [%d]",
1154                                 adapter_state);
1155                 if (enable) {
1156                         return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1157                 } else {
1158                         if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1159                                         adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1160                                 /*TODO Stop Discovery*/
1161                                 if (result != OAL_STATUS_SUCCESS)
1162                                         BT_ERR("Discover stop failed: %d", result);
1163                                 __bt_adapter_update_discovery_status(FALSE);
1164                         }
1165                         result = adapter_disable();
1166                         if (result != OAL_STATUS_SUCCESS) {
1167                                 BT_ERR("adapter_enable failed: [%d]", result);
1168                                 result = BLUETOOTH_ERROR_INTERNAL;
1169                                 /*TODO: perform if anything more needs to be done to handle failure */
1170                         } else {
1171                                 /* TODO: To be handled */
1172                                 __bt_adapter_state_set_status(BT_DEACTIVATING);
1173                                 result = BLUETOOTH_ERROR_NONE;
1174                         }
1175                 }
1176                 break;
1177         }
1178         case BT_DEACTIVATING: {
1179                 BT_INFO("Adapter is currently in deactivating state, state [%d]",
1180                                 adapter_state);
1181                 if (!enable) {
1182                         return BLUETOOTH_ERROR_IN_PROGRESS;
1183
1184                 } else {
1185                         result = adapter_enable();
1186                         if (result != OAL_STATUS_SUCCESS) {
1187                                 BT_ERR("adapter_enable failed: [%d]", result);
1188                                 adapter_disable();
1189                                 result = BLUETOOTH_ERROR_INTERNAL;
1190                                 /*TODO: perform if anything more needs to be done to handle failure */
1191                         } else {
1192                                 /* TODO: To be handled */
1193                                 __bt_adapter_state_set_status(BT_ACTIVATING);
1194                                 result = BLUETOOTH_ERROR_NONE;
1195                         }
1196                 }
1197                 break;
1198         }
1199         case BT_DEACTIVATED: {
1200                 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1201                                 adapter_state);
1202                 if (!enable) {
1203                         return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1204                 } else {
1205                         result = adapter_enable();
1206                         if (result != OAL_STATUS_SUCCESS) {
1207                                 BT_ERR("adapter_enable failed: [%d]", result);
1208                                 adapter_disable();
1209                                 result = BLUETOOTH_ERROR_INTERNAL;
1210                                 /*TODO: perform if anything more needs to be done to handle failure */
1211                         } else {
1212                                 /* TODO: To be handled */
1213                                 __bt_adapter_state_set_status(BT_ACTIVATING);
1214                                 result = BLUETOOTH_ERROR_NONE;
1215                         }
1216                 }
1217                 break;
1218         }
1219         default:
1220                 BT_ERR("Unknown state: %d", adapter_state);
1221                 break;
1222         }
1223
1224         if (enable && result == BLUETOOTH_ERROR_NONE) {
1225                 /* Adapter enable request is successful, setup event handlers */
1226                 _bt_service_register_event_handler_callback(
1227                                 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1228                 _bt_device_state_handle_callback_set_request();
1229         }
1230         return result;
1231 }
1232
1233 static int __bt_adapter_state_discovery_request(gboolean enable,
1234                 unsigned short max_response, unsigned short duration, unsigned int mask)
1235 {
1236         int result = BLUETOOTH_ERROR_NONE;
1237
1238         BT_DBG("+");
1239         switch (adapter_discovery_state) {
1240         case ADAPTER_DISCOVERY_STARTED: {
1241                 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1242                                 adapter_discovery_state);
1243                 if (enable) {
1244                         return BLUETOOTH_ERROR_IN_PROGRESS;
1245                 } else {
1246                         result = adapter_stop_inquiry();
1247                         if (result != OAL_STATUS_SUCCESS) {
1248                                 BT_ERR("Discover stop failed: %d", result);
1249                                 result = BLUETOOTH_ERROR_INTERNAL;
1250                         } else {
1251                                 BT_ERR("Stop Discovery Triggered successfully");
1252                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1253                                 result = BLUETOOTH_ERROR_NONE;
1254                         }
1255                 }
1256                 break;
1257         }
1258         case ADAPTER_DISCOVERY_STARTING: {
1259                 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1260                                 adapter_discovery_state);
1261                 if (enable) {
1262                         return BLUETOOTH_ERROR_IN_PROGRESS;
1263                 } else {
1264                         result = adapter_stop_inquiry();
1265                         if (result != OAL_STATUS_SUCCESS) {
1266                                 BT_ERR("Discover stop failed: %d", result);
1267                                 result = BLUETOOTH_ERROR_INTERNAL;
1268                         } else {
1269                                 BT_ERR("Stop Discovery Triggered successfully");
1270                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1271                                 result = BLUETOOTH_ERROR_NONE;
1272                         }
1273                 }
1274                 break;
1275         }
1276         case ADAPTER_DISCOVERY_STOPPED: {
1277                 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1278                                 adapter_discovery_state);
1279                 if (!enable)
1280                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1281                 else {
1282                         BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1283                         result = adapter_start_inquiry(duration);
1284                         if (result != OAL_STATUS_SUCCESS) {
1285                                 BT_ERR("Start Discovery failed: %d", result);
1286                                 result = BLUETOOTH_ERROR_INTERNAL;
1287                         } else {
1288                                 BT_ERR("Start Discovery Triggered successfully");
1289                                 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1290                                 result = BLUETOOTH_ERROR_NONE;
1291                         }
1292                 }
1293                 break;
1294         }
1295         case ADAPTER_DISCOVERY_STOPPING: {
1296                 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1297                                 adapter_discovery_state);
1298                 if (!enable)
1299                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1300                 else {
1301                         BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1302                         result = adapter_start_inquiry(duration);
1303                         if (result != OAL_STATUS_SUCCESS) {
1304                                 BT_ERR("Start Discovery failed: %d", result);
1305                                 result = BLUETOOTH_ERROR_INTERNAL;
1306                         } else {
1307                                 BT_ERR("Start Discovery Triggered successfully");
1308                         __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1309                                 result = BLUETOOTH_ERROR_NONE;
1310                         }
1311                 }
1312                 break;
1313         }
1314         default:
1315                 BT_ERR("Unknown state: %d", adapter_discovery_state);
1316                 break;
1317         }
1318
1319         BT_DBG("-");
1320         return result;
1321 }
1322
1323 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1324 {
1325         BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1326         GVariant *param = NULL;
1327         int result = BLUETOOTH_ERROR_NONE;
1328
1329         switch (bt_discovery_status) {
1330         case ADAPTER_DISCOVERY_STOPPED:
1331         {
1332                 __bt_adapter_update_discovery_status(bt_discovery_status);
1333                 param = g_variant_new("(i)", result);
1334                 _bt_send_event(BT_ADAPTER_EVENT,
1335                                 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1336                                 param);
1337                 break;
1338         }
1339         case ADAPTER_DISCOVERY_STARTED:
1340         {
1341                 __bt_adapter_update_discovery_status(bt_discovery_status);
1342                 param = g_variant_new("(i)", result);
1343                 _bt_send_event(BT_ADAPTER_EVENT,
1344                                 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1345                                 param);
1346                 break;
1347         }
1348         default:
1349                 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1350         }
1351 }
1352
1353 #ifndef TIZEN_TV
1354 static void __bt_set_visible_mode(void)
1355 {
1356         int timeout = 0;
1357
1358         if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1359                 BT_ERR("Fail to get the timeout value");
1360
1361 #ifdef TIZEN_DPM_ENABLE
1362         if (timeout == -1 ||
1363                         _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1364                 if (_bt_set_discoverable_mode(
1365                                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1366                                         timeout) != BLUETOOTH_ERROR_NONE) {
1367                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1368                                 BT_ERR("Set vconf failed");
1369                 }
1370         } else {
1371                 if (_bt_set_discoverable_mode(
1372                                         BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1373                                         timeout) != BLUETOOTH_ERROR_NONE) {
1374                         BT_ERR("Set connectable mode failed");
1375                 }
1376         }
1377 #else
1378         if (timeout == -1) {
1379                 if (_bt_set_discoverable_mode(
1380                                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1381                                         timeout) != BLUETOOTH_ERROR_NONE) {
1382                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1383                                 BT_ERR("Set vconf failed");
1384                 }
1385         } else {
1386                 if (_bt_set_discoverable_mode(
1387                                         BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1388                                         timeout) != BLUETOOTH_ERROR_NONE) {
1389                         BT_ERR("Set connectable mode failed");
1390                 }
1391         }
1392 #endif
1393 }
1394
1395 static void __bt_set_local_name(void)
1396 {
1397         char *phone_name = NULL;
1398         char *ptr = NULL;
1399
1400         phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1401
1402         if (!phone_name)
1403                 return;
1404
1405         if (strlen(phone_name) != 0) {
1406                 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1407                         *ptr = '\0';
1408
1409                 _bt_set_local_name(phone_name);
1410         }
1411         free(phone_name);
1412 }
1413 #endif