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