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