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