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