2 * Copyright (c) 2015 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Anupam Roy <anupam.r@samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <vconf-internal-keys.h>
28 #include <bundle_internal.h>
29 #include <eventsystem.h>
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"
47 #ifdef TIZEN_FEATURE_BT_DPM
48 #include "bt-service-dpm.h"
50 #include "bt-service-hidhost.h"
51 #include "bt-service-hiddevice.h"
52 #include "bt-service-socket.h"
53 #include "bt-service-hdp.h"
56 #include <oal-event.h>
57 #include <oal-manager.h>
58 #include <oal-adapter-mgr.h>
60 #ifdef TIZEN_FEATURE_BT_PAN_NAP
61 #include "bt-service-network.h"
63 /*This file will contain state machines related to adapter and remote device */
65 #include "bt-internal-types.h"
67 /* Global variables */
76 static bt_adapter_timer_t visible_timer;
78 static guint timer_id = 0;
80 static gboolean a2dp_init_pending = TRUE;
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;
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);
105 static void __bt_set_visible_mode(void);
106 static void __bt_set_local_name(void);
108 static gboolean __bt_disconnect_all(void);
110 /* Initialize BT stack (Initialize OAL layer) */
111 int _bt_stack_init(void)
115 BT_INFO("[bt-service] Start to initialize BT stack");
116 /* Adapter enable request is successful, setup event handlers */
117 _bt_service_register_event_handler_callback(
118 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
121 if (ret != BLUETOOTH_ERROR_NONE) {
122 BT_ERR("_bt_le_init Failed");
126 ret = oal_bt_init(_bt_service_oal_event_receiver);
128 if (OAL_STATUS_PENDING == ret) {
129 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
130 return BLUETOOTH_ERROR_NONE;
131 } else if (OAL_STATUS_SUCCESS != ret) {
132 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
133 return BLUETOOTH_ERROR_INTERNAL;
136 return BLUETOOTH_ERROR_NONE;
139 int _bt_enable_adapter(void)
141 bt_le_status_t le_status = BT_LE_DEACTIVATED;
143 le_status = _bt_adapter_get_le_status();
145 BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
147 if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
148 BT_ERR("Enabling in progress");
149 return BLUETOOTH_ERROR_IN_PROGRESS;
152 if (adapter_state == BT_ACTIVATED) {
153 BT_ERR("Already enabled");
154 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
157 if (adapter_state == BT_DEACTIVATING ||
158 adapter_state == BT_TERMINATING ||
159 le_status == BT_LE_DEACTIVATING) {
160 BT_ERR("Disabling in progress");
161 return BLUETOOTH_ERROR_DEVICE_BUSY;
164 return __bt_adapter_state_handle_request(TRUE);
167 int _bt_enable_core(void)
169 /* TODO_40 : 4.0 merge */
170 BT_INFO("Not Supported");
171 return BLUETOOTH_ERROR_NOT_SUPPORT;
174 int _bt_recover_adapter(void)
176 int result = BLUETOOTH_ERROR_NONE;
178 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
179 BT_ERR("Disabling in progress");
180 return BLUETOOTH_ERROR_IN_PROGRESS;
183 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
184 BT_ERR("Already Disabled");
185 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
188 _bt_adapter_set_status(BT_DEACTIVATING);
190 result = adapter_recover();
192 if (result != OAL_STATUS_SUCCESS)
193 return BLUETOOTH_ERROR_INTERNAL;
195 __bt_disconnect_all();
200 int _bt_reset_adapter(void)
203 if (OAL_STATUS_SUCCESS != adapter_reset())
204 return BLUETOOTH_ERROR_INTERNAL;
206 /* TODO_40 : 4.0 merge */
207 /* TODO Currently bt-service is not terminated in tizen next.
208 It should be handled in future patch */
209 if (_bt_adapter_get_status() == BT_DEACTIVATED)
210 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
212 return BLUETOOTH_ERROR_NONE;
215 int _bt_check_adapter(int *status)
218 gboolean powered = FALSE;
219 BT_CHECK_PARAMETER(status, return);
221 *status = BT_ADAPTER_DISABLED;
223 if (OAL_STATUS_SUCCESS != adapter_get_powered_status(&powered))
224 return BLUETOOTH_ERROR_INTERNAL;
226 BT_INFO("Adapter is in Powered Up state");
227 *status = BT_ADAPTER_ENABLED;
229 BT_INFO("Adapter is in Powered Down state");
231 return BLUETOOTH_ERROR_NONE;
234 int _bt_disable_adapter(void)
236 bt_le_status_t le_status = BT_LE_DEACTIVATED;
238 le_status = _bt_adapter_get_le_status();
240 BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
242 if (adapter_state == BT_DEACTIVATING ||
243 adapter_state == BT_TERMINATING ||
244 le_status == BT_LE_DEACTIVATING) {
245 BT_DBG("Disabling in progress");
246 return BLUETOOTH_ERROR_IN_PROGRESS;
249 if (adapter_state == BT_DEACTIVATED) {
250 BT_DBG("Already disabled");
251 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
254 if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
255 BT_ERR("Enabling in progress");
256 return BLUETOOTH_ERROR_DEVICE_BUSY;
259 /* TODO: Need to add the function to disconnect all devices */
260 /* __bt_disconnect_all(); */
262 return __bt_adapter_state_handle_request(FALSE);
265 int _bt_start_discovery(unsigned short max_response,
266 unsigned short duration, unsigned int cod_mask)
268 return __bt_adapter_state_discovery_request(TRUE, max_response, duration,
269 cod_mask, FALSE, 0x00);
272 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
274 return __bt_adapter_state_discovery_request(TRUE, 0, 0, 0, TRUE, role);
277 int _bt_cancel_discovery(void)
279 return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0, FALSE, 0x00);
282 gboolean _bt_is_discovering(void)
284 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
285 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
291 int _bt_get_local_address(void)
297 result = adapter_get_address();
298 if (result != OAL_STATUS_SUCCESS) {
299 BT_ERR("adapter_get_address failed: %d", result);
300 result = BLUETOOTH_ERROR_INTERNAL;
302 result = BLUETOOTH_ERROR_NONE;
308 int _bt_get_local_version(void)
313 result = adapter_get_version();
314 if (result != OAL_STATUS_SUCCESS) {
315 BT_ERR("adapter_get_version failed: %d", result);
316 result = BLUETOOTH_ERROR_INTERNAL;
318 result = BLUETOOTH_ERROR_NONE;
324 int _bt_get_local_name(void)
330 result = adapter_get_name();
331 if (result != OAL_STATUS_SUCCESS) {
332 BT_ERR("adapter_get_name failed: %d", result);
333 result = BLUETOOTH_ERROR_INTERNAL;
335 result = BLUETOOTH_ERROR_NONE;
341 int _bt_set_local_name(char *local_name)
343 int result = BLUETOOTH_ERROR_NONE;
346 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
348 result = adapter_set_name(local_name);
349 if (result != OAL_STATUS_SUCCESS) {
350 BT_ERR("adapter_set_name failed: %d", result);
351 result = BLUETOOTH_ERROR_INTERNAL;
353 result = BLUETOOTH_ERROR_NONE;
359 int _bt_get_discoverable_mode(int *mode)
366 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
368 adapter_is_discoverable(&scan_mode);
369 if (TRUE == scan_mode) {
370 adapter_get_discoverable_timeout(&timeout);
372 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
374 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
376 adapter_is_connectable(&scan_mode);
377 if (scan_mode == TRUE)
378 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
381 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
382 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
383 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
390 return BLUETOOTH_ERROR_NONE;
393 int _bt_get_timeout_value(int *timeout)
398 /* Take current time */
400 time_diff = difftime(current_time, visible_timer.start_time);
402 BT_DBG("Time diff = %d\n", time_diff);
403 *timeout = visible_timer.timeout - time_diff;
405 return BLUETOOTH_ERROR_NONE;
408 static void __bt_visibility_alarm_remove()
410 if (visible_timer.event_id > 0) {
411 g_source_remove(visible_timer.event_id);
412 visible_timer.event_id = 0;
415 if (visible_timer.alarm_id > 0) {
416 alarmmgr_remove_alarm(visible_timer.alarm_id);
417 visible_timer.alarm_id = 0;
421 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
423 int result = BLUETOOTH_ERROR_NONE;
426 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
428 if (alarm_id != visible_timer.alarm_id)
431 if (visible_timer.event_id) {
432 _bt_send_event(BT_ADAPTER_EVENT,
433 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
434 g_variant_new("(in)", result, timeout));
435 g_source_remove(visible_timer.event_id);
436 visible_timer.event_id = 0;
437 visible_timer.timeout = 0;
439 if (!TIZEN_PROFILE_WEARABLE) {
440 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
441 BT_ERR("Set vconf failed\n");
444 /* Switch Off visibility in Bluez */
445 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
446 visible_timer.alarm_id = 0;
450 static gboolean __bt_timeout_handler(gpointer user_data)
452 int result = BLUETOOTH_ERROR_NONE;
456 /* Take current time */
458 time_diff = difftime(current_time, visible_timer.start_time);
460 /* Send event to application */
461 _bt_send_event(BT_ADAPTER_EVENT,
462 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
463 g_variant_new("(in)", result, time_diff));
465 if (visible_timer.timeout <= time_diff) {
466 g_source_remove(visible_timer.event_id);
467 visible_timer.event_id = 0;
468 visible_timer.timeout = 0;
470 if (!TIZEN_PROFILE_WEARABLE) {
471 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
472 BT_ERR("Set vconf failed\n");
481 static void __bt_visibility_alarm_create()
486 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
489 BT_ERR("Failed to create alarm error = %d\n", result);
491 BT_DBG("Alarm created = %d\n", alarm_id);
492 visible_timer.alarm_id = alarm_id;
496 int _bt_start_visibility_timer(int timeout)
499 #ifdef TIZEN_FEATURE_BT_DPM
500 int discoverable_state = DPM_BT_ERROR;
503 __bt_visibility_alarm_remove();
505 visible_timer.timeout = timeout;
507 #ifdef TIZEN_FEATURE_BT_DPM
508 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
509 if (discoverable_state != DPM_RESTRICTED) {
511 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
512 BT_ERR("Set vconf failed");
513 #ifdef TIZEN_FEATURE_BT_DPM
518 return BLUETOOTH_ERROR_NONE;
520 if (!visible_timer.alarm_init) {
521 /* Set Alarm timer to switch off BT */
522 result = alarmmgr_init("bt-service");
524 return BLUETOOTH_ERROR_INTERNAL;
526 visible_timer.alarm_init = TRUE;
529 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
531 return BLUETOOTH_ERROR_INTERNAL;
533 if (!TIZEN_PROFILE_WEARABLE) {
534 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
535 BT_ERR("Set vconf failed");
538 /* Take start time */
539 time(&(visible_timer.start_time));
540 visible_timer.event_id = g_timeout_add_seconds(1,
541 __bt_timeout_handler, NULL);
543 visible_timer.timeout = timeout;
545 __bt_visibility_alarm_create();
547 return BLUETOOTH_ERROR_NONE;
550 int _bt_stop_visibility_timer(void)
552 __bt_visibility_alarm_remove();
554 visible_timer.timeout = 0;
556 if (!TIZEN_PROFILE_WEARABLE) {
557 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
558 BT_ERR("Set vconf failed");
561 return BLUETOOTH_ERROR_NONE;
564 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
567 int ret = BLUETOOTH_ERROR_NONE;;
568 #ifdef TIZEN_FEATURE_BT_DPM
569 int discoverable_state = DPM_BT_ERROR;
574 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
576 #ifdef TIZEN_FEATURE_BT_DPM
577 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
578 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
579 discoverable_state == DPM_RESTRICTED) {
580 if (headed_plugin_info->plugin_headed_enabled)
581 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
582 return BLUETOOTH_ERROR_ACCESS_DENIED;
584 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE &&
585 discoverable_state == DPM_RESTRICTED) {
586 if (headed_plugin_info->plugin_headed_enabled)
587 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
588 return BLUETOOTH_ERROR_ACCESS_DENIED;
592 switch (discoverable_mode) {
593 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
594 result = adapter_set_connectable(TRUE);
597 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
598 result = adapter_set_discoverable();
601 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
602 result = adapter_set_discoverable();
605 return BLUETOOTH_ERROR_INVALID_PARAM;
608 if (result != OAL_STATUS_SUCCESS) {
609 BT_ERR("set scan mode failed %d", result);
610 return BLUETOOTH_ERROR_INTERNAL;
613 result = adapter_set_discoverable_timeout(timeout);
614 if (result != OAL_STATUS_SUCCESS) {
615 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
616 return BLUETOOTH_ERROR_INTERNAL;
619 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
620 ret = _bt_stop_visibility_timer();
621 if (BLUETOOTH_ERROR_NONE != ret)
622 BT_ERR("_bt_stop_visibility_timer failed");
623 if (!TIZEN_PROFILE_WEARABLE) {
624 if (vconf_set_int(BT_FILE_VISIBLE_TIME, -1) != 0)
625 BT_ERR("Set vconf failed");
627 } else if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE) {
628 ret = _bt_start_visibility_timer(timeout);
629 if (BLUETOOTH_ERROR_NONE != ret)
630 BT_ERR("_bt_start_visibility_timer failed");
633 ret = _bt_stop_visibility_timer();
634 if (BLUETOOTH_ERROR_NONE != ret)
635 BT_ERR("_bt_stop_visibility_timer failed");
642 gboolean _bt_is_connectable(void)
649 adapter_is_connectable(&connectable);
655 BT_DBG("Connectable: [%s]", result ? "TRUE" : "FALSE");
660 int _bt_set_connectable(gboolean connectable)
662 int result = BLUETOOTH_ERROR_NONE;
665 result = adapter_set_connectable(connectable);
666 if (result != OAL_STATUS_SUCCESS) {
667 BT_ERR("adapter_set_connectable failed: %d", result);
668 result = BLUETOOTH_ERROR_INTERNAL;
670 result = BLUETOOTH_ERROR_NONE;
676 int _bt_get_connectable(void)
682 result = adapter_get_connectable();
683 if (result != OAL_STATUS_SUCCESS) {
684 BT_ERR("adapter_get_connectable failed: %d", result);
685 result = BLUETOOTH_ERROR_INTERNAL;
687 result = BLUETOOTH_ERROR_NONE;
693 int _bt_is_service_used(void)
699 result = adapter_get_service_uuids();
700 if (result != OAL_STATUS_SUCCESS) {
701 BT_ERR("adapter_get_service_uuids failed: %d", result);
702 result = BLUETOOTH_ERROR_INTERNAL;
704 result = BLUETOOTH_ERROR_NONE;
711 int _bt_adapter_get_bonded_devices(void)
713 int result = BLUETOOTH_ERROR_NONE;
716 result = adapter_get_bonded_devices();
717 if (result != OAL_STATUS_SUCCESS) {
718 BT_ERR("adapter_get_bonded_devices failed: %d", result);
719 result = BLUETOOTH_ERROR_INTERNAL;
721 result = BLUETOOTH_ERROR_NONE;
727 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
730 GDBusConnection *conn;
731 GDBusProxy *manager_proxy;
732 GVariant *result = NULL;
733 GVariant *result1 = NULL;
734 GVariantIter *iter = NULL;
735 GError *error = NULL;
736 char *object_path = NULL;
737 GVariantIter *interface_iter;
738 char *interface_str = NULL;
739 GDBusProxy *device_proxy = NULL;
740 gboolean is_connected = FALSE;
742 conn = _bt_gdbus_get_system_gconn();
743 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
745 manager_proxy = _bt_get_manager_proxy();
746 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
748 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
750 G_DBUS_CALL_FLAGS_NONE,
757 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
758 g_clear_error(&error);
761 BT_ERR("Failed to Failed to GetManagedObjects");
762 return BLUETOOTH_ERROR_INTERNAL;
765 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
766 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
768 /* Parse the signature: oa{sa{sv}}} */
769 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
770 if (object_path == NULL)
773 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
774 &interface_str, NULL)) {
775 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
776 BT_DBG("Found a device: %s", object_path);
777 g_free(interface_str);
779 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
781 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
783 if (device_proxy == NULL) {
784 BT_DBG("Device don't have this service");
788 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
789 g_variant_new("(s)", profile_uuid),
790 G_DBUS_CALL_FLAGS_NONE,
795 if (result1 == NULL) {
796 BT_ERR("Error occured in Proxy call");
798 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
802 g_object_unref(device_proxy);
805 g_variant_get(result1, "(b)", &is_connected);
807 if (is_connected == TRUE) {
808 char address[BT_ADDRESS_STRING_SIZE];
809 bluetooth_device_address_t *addr = NULL;
811 _bt_convert_device_path_to_address(object_path, address);
813 addr = g_malloc0(sizeof(bluetooth_device_address_t));
814 _bt_convert_addr_string_to_type(addr->addr, address);
816 g_array_append_vals(*addr_list, addr,
817 sizeof(bluetooth_device_address_t));
820 g_variant_unref(result1);
821 g_object_unref(device_proxy);
828 g_variant_unref(result);
829 g_variant_iter_free(iter);
832 return BLUETOOTH_ERROR_NONE;
835 static gboolean __bt_disconnect_all(void)
838 gboolean ret = FALSE;
842 bluetooth_device_info_t *info;
845 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
847 BT_DBG("The length of device list after the initialisation is [%d]", device_list->len);
849 _bt_service_get_bonded_dev_list(&device_list);
851 size = (device_list->len) / sizeof(bluetooth_device_info_t);
853 for (i = 0; i < size; i++) {
854 info = &g_array_index(device_list,
855 bluetooth_device_info_t, i);
856 if (info->connected != BLUETOOTH_CONNECTED_LINK_NONE) {
857 BT_DBG("Found Connected Device");
858 result = _bt_disconnect_device(&info->device_address);
859 if (result != BLUETOOTH_ERROR_NONE) {
860 BT_ERR("Cannot disconnect all the devices. bt_disconnect_device Failed");
861 g_array_free(device_list, TRUE);
867 g_array_free(device_list, TRUE);
872 static void __bt_handle_pending_a2dp_init(unsigned int a2dp_role)
876 if (!a2dp_init_pending)
880 a2dp_init_pending = FALSE;
882 if (a2dp_role == BT_A2DP_SINK_ROLE) {
883 BT_INFO("Enable A2DP Sink role");
884 /* Initialize A2DP Sink */
885 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
886 if (ret != BLUETOOTH_ERROR_NONE)
887 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
889 /* Initialize AVRCP Controller */
890 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
891 if (ret != BLUETOOTH_ERROR_NONE)
892 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
894 _bt_audio_set_current_role(BLUETOOTH_A2DP_SINK);
898 BT_INFO("Enable A2DP Source role");
899 /* Initialize A2DP Source */
900 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
901 if (ret != BLUETOOTH_ERROR_NONE)
902 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
904 /* Initialize AVRCP Target */
905 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
906 if (ret != BLUETOOTH_ERROR_NONE)
907 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
909 _bt_audio_set_current_role(BLUETOOTH_A2DP_SOURCE);
913 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
915 int result = BLUETOOTH_ERROR_NONE;
919 switch (event_type) {
920 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
921 case OAL_EVENT_OAL_INITIALISED_FAILED:
922 __bt_handle_oal_initialisation(event_type);
924 case OAL_EVENT_ADAPTER_ENABLED:
925 __bt_adapter_state_change_callback(BT_ACTIVATED);
927 case OAL_EVENT_ADAPTER_DISABLED:
928 __bt_adapter_state_change_callback(BT_DEACTIVATED);
930 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
931 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
933 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
934 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
936 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
937 bt_address_t *bd_addr = event_data;
938 bluetooth_device_address_t local_address;
941 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
942 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
943 local_address.addr[0], local_address.addr[1], local_address.addr[2],
944 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
946 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
947 (void *) &local_address, sizeof(bluetooth_device_address_t));
950 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
951 char *name = event_data;
952 BT_DBG("Adapter Name: %s", name);
954 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
955 bluetooth_device_name_t local_name;
957 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
958 g_strlcpy(local_name.name,
959 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
960 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
961 (void *) &local_name, sizeof(bluetooth_device_name_t));
963 /* Send event to application */
964 _bt_send_event(BT_ADAPTER_EVENT,
965 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
966 g_variant_new("(is)", result, name));
970 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
971 char *ver = event_data;
972 bluetooth_version_t local_version;
974 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
975 g_strlcpy(local_version.version,
976 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
977 BT_DBG("BT Version: %s", local_version.version);
979 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
980 (void *) &local_version, sizeof(bluetooth_version_t));
983 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
985 gboolean connectable = FALSE;
987 BT_INFO("Adapter discoverable mode:"
988 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
990 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
991 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
992 (void *) &connectable, sizeof(gboolean));
995 _bt_send_event(BT_ADAPTER_EVENT,
996 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
997 g_variant_new("(b)", connectable));
999 _bt_send_event(BT_ADAPTER_EVENT,
1000 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1001 g_variant_new("(in)", result, mode));
1005 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
1007 gboolean connectable = TRUE;
1009 BT_INFO("Adapter discoverable mode:"
1010 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
1012 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1013 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1014 (void *) &connectable, sizeof(gboolean));
1018 _bt_send_event(BT_ADAPTER_EVENT,
1019 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1020 g_variant_new("(b)", connectable));
1022 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1023 _bt_send_event(BT_ADAPTER_EVENT,
1024 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1025 g_variant_new("(in)", result, mode));
1029 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
1031 gboolean connectable = TRUE;
1033 BT_INFO("Adapter discoverable mode:"
1034 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
1036 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1037 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1038 (void *) &connectable, sizeof(gboolean));
1041 /* Send event to application */
1042 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1043 _bt_send_event(BT_ADAPTER_EVENT,
1044 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1045 g_variant_new("(in)", result, mode));
1049 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
1050 int *timeout = event_data;
1053 BT_INFO("Discoverable timeout: [%d]", *timeout);
1055 /* Send event to application */
1056 _bt_get_discoverable_mode(&mode);
1057 _bt_send_event(BT_ADAPTER_EVENT,
1058 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1059 g_variant_new("(in)", result, mode));
1062 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
1064 service_uuid_t *service_list;
1065 event_adapter_services_t *list = event_data;
1068 service_list = list->service_list;
1069 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
1071 char uuids[512] = {0,};
1072 char standard_prefix[2] = {0,};
1074 for (int i = 0; i < count; i++) {
1076 g_snprintf(uuids + (index++), 2, ",");
1077 if (memcmp(service_list[i].uuid, standard_prefix, 2) == 0) {
1078 g_snprintf(uuids + index, 5, "%.2x%.2x",
1079 service_list[i].uuid[2], service_list[i].uuid[3]);
1082 g_snprintf(uuids + index, 9, "%.2x%.2x..%.2x,",
1083 service_list[i].uuid[0], service_list[i].uuid[1], service_list[i].uuid[15]);
1087 BT_PERMANENT_LOG("UUIDs %s", uuids);
1090 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
1093 bluetooth_device_address_t *addr_list;
1095 event_device_list_t *bonded_device_list = event_data;
1096 count = bonded_device_list->num;
1098 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
1099 for (i = 0; i < count; i++) {
1100 memcpy(addr_list[i].addr,
1101 bonded_device_list->devices[i].addr,
1102 BLUETOOTH_ADDRESS_LENGTH);
1105 BT_INFO("Adapter Bonded device List count: [%d]", count);
1106 _bt_device_handle_paired_address_list(addr_list, bonded_device_list->num);
1110 case OAL_EVENT_ADAPTER_PROPERTY_A2DP_ROLE: {
1111 unsigned int *a2dp_role = event_data;
1113 BT_INFO("a2dp role: [%u]", *a2dp_role);
1115 __bt_handle_pending_a2dp_init(*a2dp_role);
1120 BT_ERR("Unhandled event..");
1127 int _bt_init_profiles()
1132 /*TODO: Init bluetooth profiles */
1133 ret = _bt_hidhost_initialize();
1134 if (ret != BLUETOOTH_ERROR_NONE)
1135 BT_ERR("_bt_hidhost_initialize Failed");
1137 ret = _bt_hiddevice_initialize();
1138 if (ret != BLUETOOTH_ERROR_NONE)
1139 BT_ERR("_bt_hiddevice_initialize Failed");
1141 ret = _bt_socket_init();
1142 if (ret != BLUETOOTH_ERROR_NONE)
1143 BT_ERR("_bt_socket_init Failed");
1145 /* Initialize HFP Audio Gateway */
1146 ret = _bt_audio_initialize(BT_AG_MODULE);
1147 if (ret != BLUETOOTH_ERROR_NONE)
1148 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
1150 /* Registering callback for receiving audio services searched */
1151 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
1152 if (ret != BLUETOOTH_ERROR_NONE)
1153 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
1155 ret = _bt_hdp_init();
1156 if (ret != BLUETOOTH_ERROR_NONE)
1157 BT_ERR("_bt_hdp_init Failed");
1159 ret = _bt_gatt_init();
1160 if (ret != BLUETOOTH_ERROR_NONE)
1161 BT_ERR("_bt_gatt_init Failed");
1163 /* Initialize HF Client */
1164 ret = _bt_audio_initialize(BT_HFP_MODULE);
1165 if (ret != BLUETOOTH_ERROR_NONE)
1166 BT_ERR("_bt_audio_initialize(BT_HFP_MODULE) Failed");
1168 return BLUETOOTH_ERROR_NONE;
1171 int _bt_cleanup_profiles(void)
1173 /* TODO: Cleanup bluetooth profiles */
1174 _bt_hidhost_deinitialize();
1175 _bt_socket_deinit();
1177 /* TODO: Cleanup bluetooth audio profiles */
1178 //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
1179 //_bt_audio_deinitialize(BT_AVRCP_MODULE);
1180 //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
1181 //_bt_audio_deinitialize(BT_AG_MODULE);
1182 //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
1183 //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
1185 a2dp_init_pending = TRUE;
1190 return BLUETOOTH_ERROR_NONE;
1193 /* OAL post initialization handler */
1194 static void __bt_post_oal_init(void)
1197 int status = VCONFKEY_BT_STATUS_OFF;
1199 BT_DBG("OAL initialized");
1200 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0)
1201 BT_ERR("Fail to get the enabled value");
1204 /* Update Bluetooth Status to OFF */
1205 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1206 BT_ERR("Set vconf failed\n");
1209 if (status & VCONFKEY_BT_STATUS_ON) {
1210 ret = _bt_enable_adapter();
1211 if (ret != BLUETOOTH_ERROR_NONE)
1212 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
1216 /* OAL initialization handler */
1217 static void __bt_handle_oal_initialisation(oal_event_t event)
1222 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1223 __bt_post_oal_init();
1225 case OAL_EVENT_OAL_INITIALISED_FAILED:
1226 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1227 g_idle_add(_bt_terminate_service, NULL);
1230 BT_ERR("Unknown Event");
1235 static gboolean __bt_is_service_request_present(int service_function)
1238 invocation_info_t *req_info;
1242 /* Get method invocation context */
1243 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1245 if (req_info && req_info->service_function == service_function)
1253 /* Internal functions of core adapter service */
1254 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1258 invocation_info_t *req_info;
1261 /* Get method invocation context */
1262 for (l = _bt_get_invocation_list(); l != NULL; ) {
1264 l = g_slist_next(l);
1265 if (req_info == NULL || req_info->service_function != service_function)
1268 /* Create out param */
1269 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1271 switch (service_function) {
1272 case BT_ENABLE_ADAPTER:
1273 case BT_DISABLE_ADAPTER: {
1274 gboolean done = TRUE;
1275 g_array_append_vals(out_param, &done, sizeof(gboolean));
1278 case BT_GET_CONNECTABLE:
1279 case BT_GET_LOCAL_NAME:
1280 case BT_GET_LOCAL_ADDRESS:
1281 case BT_GET_LOCAL_VERSION:
1282 g_array_append_vals(out_param, user_data, size);
1284 case BT_IS_SERVICE_USED: {
1286 gboolean used = FALSE;
1287 unsigned char *uuid;
1288 char uuid_str[BT_UUID_STRING_SIZE];
1289 char *request_uuid = req_info->user_data;
1290 service_uuid_t *service_list = user_data;
1292 BT_INFO("Check for service uuid: %s", request_uuid);
1293 for (i = 0; i < size; i++) {
1294 uuid = service_list[i].uuid;
1295 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1296 BT_INFO("Adapter Service: [%s]", uuid_str);
1297 if (strcasecmp(uuid_str, request_uuid) == 0) {
1298 BT_INFO("UUID matched!!");
1304 g_array_append_vals(out_param, &used, sizeof(gboolean));
1308 BT_ERR("Unknown service function[%d]", service_function);
1311 _bt_service_method_return(req_info->context, out_param, req_info->result);
1312 g_array_free(out_param, TRUE);
1313 /* Now free invocation info for this request*/
1314 _bt_free_info_from_invocation_list(req_info);
1318 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1320 char *phone_name = NULL;
1326 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1327 phone_name = vconf_keynode_get_str(node);
1329 if (phone_name && strlen(phone_name) != 0) {
1330 if (!g_utf8_validate(phone_name, -1,
1331 (const char **)&ptr))
1334 _bt_set_local_name(phone_name);
1339 /* Request return handlings */
1340 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1342 int result = BLUETOOTH_ERROR_NONE;
1344 BT_INFO("__bt_adapter_post_set_enabled>>");
1346 if (TIZEN_PROFILE_TV || !headed_plugin_info->plugin_headed_enabled) {
1347 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1348 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1349 BT_ERR("Fail to set discoverable mode");
1351 __bt_set_visible_mode();
1354 /* add the vconf noti handler */
1355 if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1356 (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1357 BT_ERR("DEVICE_NAME key changed notification registration failed");
1359 __bt_set_local_name();
1361 /* Get All properties */
1362 if (OAL_STATUS_SUCCESS != adapter_get_properties())
1363 BT_ERR("adapter_get_properties failed");
1365 __bt_adapter_state_set_status(BT_ACTIVATED);
1367 /*Send BT Enabled event to application */
1368 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1369 g_variant_new("(i)", result));
1371 BT_PERMANENT_LOG("BT Enabled");
1376 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1378 BT_INFO("_bt_adapter_post_set_disabled>>");
1380 if (!TIZEN_PROFILE_TV) {
1381 /* Add Adapter disabled post processing codes */
1382 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1383 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1384 BT_ERR("vconf_ignore_key_changed failed");
1387 /* bt-service should be terminated when BT is off */
1388 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1389 /* TODO: Implement to check if it is the recovery mode or not */
1390 _bt_reliable_terminate_service(NULL);
1392 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1398 static void __bt_adapter_update_bt_enabled(void)
1400 BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1401 if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1402 BT_ERR("Bluetooth profile init failed");
1404 _bt_device_handle_adapter_state(TRUE);
1406 /* Update Bluetooth Status to notify other modules */
1407 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1408 BT_ERR("Set vconf failed\n");
1410 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1411 BT_ERR("Set vconf failed\n");
1413 /* TODO:Add timer function to handle any further post processing */
1414 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1417 static void __bt_adapter_update_bt_disabled(void)
1419 int power_off_status = 0;
1422 BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1423 _bt_cleanup_profiles();
1425 _bt_device_handle_adapter_state(FALSE);
1427 /* Update the vconf BT status in normal Deactivation case only */
1428 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1429 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1431 /* Update Bluetooth Status to notify other modules */
1432 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1433 BT_ERR("Set vconf failed");
1435 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1436 BT_ERR("Set vconf failed\n");
1438 /* TODO:Add timer function to handle any further post processing */
1439 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1441 /* Pending BT Disabled event before bt-service's termination */
1444 static void __bt_adapter_state_set_status(bt_status_t status)
1446 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1447 adapter_state = status;
1450 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1452 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1453 adapter_discovery_state = status;
1456 static void __bt_adapter_state_change_callback(int bt_status)
1458 BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
1460 switch (bt_status) {
1461 case BT_DEACTIVATED:
1462 __bt_adapter_state_set_status(BT_TERMINATING);
1464 /* Adapter is disabled, unregister event handlers */
1465 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1466 //_bt_deinit_device_event_handler();
1468 /* Add Adapter disabled post processing codes */
1469 __bt_adapter_update_bt_disabled();
1472 /* Add Adapter enabled post processing codes */
1474 BT_DBG("g_source is removed");
1475 g_source_remove(timer_id);
1478 __bt_adapter_update_bt_enabled();
1481 BT_ERR("Incorrect Bluetooth adapter state changed status");
1486 int _bt_set_adapter_request_state(int enable)
1488 return oal_set_adapter_request_state(enable);
1491 int _bt_set_le_request_state(int enable)
1493 return oal_set_le_request_state(enable);
1496 static int __bt_adapter_state_handle_request(gboolean enable)
1498 int result = BLUETOOTH_ERROR_NONE;
1500 switch (adapter_state) {
1502 case BT_DEACTIVATING:
1503 BT_INFO("Should not be callded : state [%d]", adapter_state);
1504 return BLUETOOTH_ERROR_INTERNAL;
1506 case BT_ACTIVATED: {
1507 BT_INFO("Adapter is currently in activated state, state [%d]",
1510 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1512 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1513 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1514 /*TODO Stop Discovery*/
1515 __bt_adapter_update_discovery_status(FALSE);
1517 result = adapter_disable();
1518 if (result != OAL_STATUS_SUCCESS) {
1519 BT_ERR("adapter_enable failed: [%d]", result);
1520 result = BLUETOOTH_ERROR_INTERNAL;
1521 /*TODO: perform if anything more needs to be done to handle failure */
1523 /* TODO: To be handled */
1524 __bt_adapter_state_set_status(BT_DEACTIVATING);
1525 result = BLUETOOTH_ERROR_NONE;
1530 case BT_DEACTIVATED: {
1531 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1534 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1536 result = adapter_enable();
1537 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1538 BT_ERR("adapter_enable failed: [%d]", result);
1540 result = BLUETOOTH_ERROR_INTERNAL;
1541 /*TODO: perform if anything more needs to be done to handle failure */
1543 /* TODO: To be handled */
1544 __bt_adapter_state_set_status(BT_ACTIVATING);
1545 result = BLUETOOTH_ERROR_NONE;
1551 BT_ERR("Unknown state: %d", adapter_state);
1555 if (enable && result == BLUETOOTH_ERROR_NONE) {
1556 /* Adapter enable request is successful, setup event handlers */
1557 _bt_service_register_event_handler_callback(
1558 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1559 _bt_device_state_handle_callback_set_request();
1564 static int __bt_adapter_state_discovery_request(gboolean enable,
1565 unsigned short max_response, unsigned short duration,
1566 unsigned int mask, gboolean is_custom,
1567 bt_discovery_role_type_t role)
1569 int result = BLUETOOTH_ERROR_NONE;
1572 switch (adapter_discovery_state) {
1573 case ADAPTER_DISCOVERY_STARTED: {
1574 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1575 adapter_discovery_state);
1577 return BLUETOOTH_ERROR_IN_PROGRESS;
1579 result = adapter_stop_inquiry();
1580 if (result != OAL_STATUS_SUCCESS) {
1581 BT_ERR("Discover stop failed: %d", result);
1582 result = BLUETOOTH_ERROR_INTERNAL;
1584 BT_ERR("Stop Discovery Triggered successfully");
1585 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1586 result = BLUETOOTH_ERROR_NONE;
1591 case ADAPTER_DISCOVERY_STARTING: {
1592 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1593 adapter_discovery_state);
1595 result = enable ? BLUETOOTH_ERROR_IN_PROGRESS :
1596 BLUETOOTH_ERROR_DEVICE_BUSY;
1600 case ADAPTER_DISCOVERY_STOPPED: {
1601 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1602 adapter_discovery_state);
1604 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1606 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1609 result = adapter_start_inquiry(duration);
1611 result = adapter_start_custom_inquiry(role);
1613 if (result != OAL_STATUS_SUCCESS) {
1614 BT_ERR("Start Discovery failed: %d", result);
1615 result = BLUETOOTH_ERROR_INTERNAL;
1617 BT_ERR("Start Discovery Triggered successfully");
1618 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1619 result = BLUETOOTH_ERROR_NONE;
1624 case ADAPTER_DISCOVERY_STOPPING: {
1625 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1626 adapter_discovery_state);
1628 result = enable ? BLUETOOTH_ERROR_DEVICE_BUSY :
1629 BLUETOOTH_ERROR_NOT_IN_OPERATION;
1634 BT_ERR("Unknown state: %d", adapter_discovery_state);
1642 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1644 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1645 GVariant *param = NULL;
1646 int result = BLUETOOTH_ERROR_NONE;
1648 switch (bt_discovery_status) {
1649 case ADAPTER_DISCOVERY_STOPPED:
1651 __bt_adapter_update_discovery_status(bt_discovery_status);
1652 param = g_variant_new("(i)", result);
1653 _bt_send_event(BT_ADAPTER_EVENT,
1654 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1658 case ADAPTER_DISCOVERY_STARTED:
1660 __bt_adapter_update_discovery_status(bt_discovery_status);
1661 param = g_variant_new("(i)", result);
1662 _bt_send_event(BT_ADAPTER_EVENT,
1663 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1668 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1672 static void __bt_set_visible_mode(void)
1675 #ifdef TIZEN_FEATURE_BT_DPM
1676 int discoverable_state = DPM_BT_ERROR;
1679 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1680 BT_ERR("Fail to get the timeout value");
1682 #ifdef TIZEN_FEATURE_BT_DPM
1683 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1684 if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
1685 if (_bt_set_discoverable_mode(
1686 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1687 timeout) != BLUETOOTH_ERROR_NONE) {
1688 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1689 BT_ERR("Set vconf failed");
1692 if (_bt_set_discoverable_mode(
1693 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1694 timeout) != BLUETOOTH_ERROR_NONE) {
1695 BT_ERR("Set connectable mode failed");
1699 if (timeout == -1) {
1700 if (_bt_set_discoverable_mode(
1701 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1702 timeout) != BLUETOOTH_ERROR_NONE) {
1703 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1704 BT_ERR("Set vconf failed");
1707 if (_bt_set_discoverable_mode(
1708 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1709 timeout) != BLUETOOTH_ERROR_NONE) {
1710 BT_ERR("Set connectable mode failed");
1716 static void __bt_set_local_name(void)
1718 char *phone_name = NULL;
1721 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1726 if (strlen(phone_name) != 0) {
1727 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1730 _bt_set_local_name(phone_name);
1735 void _bt_adapter_set_status(bt_status_t status)
1737 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1738 adapter_state = status;
1741 bt_status_t _bt_adapter_get_status(void)
1743 return adapter_state;
1746 void _bt_set_disabled(int result)
1748 int power_off_status = 0;
1751 int pm_ignore_mode = 0;
1753 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1754 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1756 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1758 /* Update the vconf BT status in normal Deactivation case only */
1759 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1760 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1762 BT_DBG("Update vconf for BT normal Deactivation");
1764 if (result == BLUETOOTH_ERROR_TIMEOUT)
1765 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1766 BT_ERR("Set vconf failed");
1768 /* Update Bluetooth Status to notify other modules */
1769 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1770 BT_ERR("Set vconf failed");
1772 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1773 EVT_VAL_BT_OFF) != ES_R_OK)
1774 BT_ERR("Fail to set value");
1777 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1778 BT_ERR("Set vconf failed\n");
1780 _bt_adapter_set_status(BT_DEACTIVATED);
1781 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1783 /* Send BT Disabled event to application */
1784 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1785 g_variant_new("(i)", result));
1787 BT_INFO("Adapter disabled");
1788 BT_PERMANENT_LOG("BT Disabled");