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_is_service_used(void)
682 result = adapter_get_service_uuids();
683 if (result != OAL_STATUS_SUCCESS) {
684 BT_ERR("adapter_get_service_uuids failed: %d", result);
685 result = BLUETOOTH_ERROR_INTERNAL;
687 result = BLUETOOTH_ERROR_NONE;
694 int _bt_adapter_get_bonded_devices(void)
696 int result = BLUETOOTH_ERROR_NONE;
699 result = adapter_get_bonded_devices();
700 if (result != OAL_STATUS_SUCCESS) {
701 BT_ERR("adapter_get_bonded_devices failed: %d", result);
702 result = BLUETOOTH_ERROR_INTERNAL;
704 result = BLUETOOTH_ERROR_NONE;
710 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
713 GDBusConnection *conn;
714 GDBusProxy *manager_proxy;
715 GVariant *result = NULL;
716 GVariant *result1 = NULL;
717 GVariantIter *iter = NULL;
718 GError *error = NULL;
719 char *object_path = NULL;
720 GVariantIter *interface_iter;
721 char *interface_str = NULL;
722 GDBusProxy *device_proxy = NULL;
723 gboolean is_connected = FALSE;
725 conn = _bt_gdbus_get_system_gconn();
726 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
728 manager_proxy = _bt_get_manager_proxy();
729 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
731 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
733 G_DBUS_CALL_FLAGS_NONE,
740 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
741 g_clear_error(&error);
744 BT_ERR("Failed to Failed to GetManagedObjects");
745 return BLUETOOTH_ERROR_INTERNAL;
748 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
749 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
751 /* Parse the signature: oa{sa{sv}}} */
752 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
753 if (object_path == NULL)
756 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
757 &interface_str, NULL)) {
758 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
759 BT_DBG("Found a device: %s", object_path);
760 g_free(interface_str);
762 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
764 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
766 if (device_proxy == NULL) {
767 BT_DBG("Device don't have this service");
771 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
772 g_variant_new("(s)", profile_uuid),
773 G_DBUS_CALL_FLAGS_NONE,
778 if (result1 == NULL) {
779 BT_ERR("Error occured in Proxy call");
781 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
785 g_object_unref(device_proxy);
788 g_variant_get(result1, "(b)", &is_connected);
790 if (is_connected == TRUE) {
791 char address[BT_ADDRESS_STRING_SIZE];
792 bluetooth_device_address_t *addr = NULL;
794 _bt_convert_device_path_to_address(object_path, address);
796 addr = g_malloc0(sizeof(bluetooth_device_address_t));
797 _bt_convert_addr_string_to_type(addr->addr, address);
799 g_array_append_vals(*addr_list, addr,
800 sizeof(bluetooth_device_address_t));
803 g_variant_unref(result1);
804 g_object_unref(device_proxy);
811 g_variant_unref(result);
812 g_variant_iter_free(iter);
815 return BLUETOOTH_ERROR_NONE;
818 static gboolean __bt_disconnect_all(void)
821 gboolean ret = FALSE;
825 bluetooth_device_info_t *info;
828 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
830 BT_DBG("The length of device list after the initialisation is [%d]", device_list->len);
832 _bt_service_get_bonded_dev_list(&device_list);
834 size = (device_list->len) / sizeof(bluetooth_device_info_t);
836 for (i = 0; i < size; i++) {
837 info = &g_array_index(device_list,
838 bluetooth_device_info_t, i);
839 if (info->connected != BLUETOOTH_CONNECTED_LINK_NONE) {
840 BT_DBG("Found Connected Device");
841 result = _bt_disconnect_device(&info->device_address);
842 if (result != BLUETOOTH_ERROR_NONE) {
843 BT_ERR("Cannot disconnect all the devices. bt_disconnect_device Failed");
844 g_array_free(device_list, TRUE);
850 g_array_free(device_list, TRUE);
855 static void __bt_handle_pending_a2dp_init(unsigned int a2dp_role)
859 if (!a2dp_init_pending)
863 a2dp_init_pending = FALSE;
865 if (a2dp_role == BT_A2DP_SINK_ROLE) {
866 BT_INFO("Enable A2DP Sink role");
867 /* Initialize A2DP Sink */
868 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
869 if (ret != BLUETOOTH_ERROR_NONE)
870 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
872 /* Initialize AVRCP Controller */
873 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
874 if (ret != BLUETOOTH_ERROR_NONE)
875 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
877 _bt_audio_set_current_role(BLUETOOTH_A2DP_SINK);
881 BT_INFO("Enable A2DP Source role");
882 /* Initialize A2DP Source */
883 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
884 if (ret != BLUETOOTH_ERROR_NONE)
885 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
887 /* Initialize AVRCP Target */
888 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
889 if (ret != BLUETOOTH_ERROR_NONE)
890 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
892 _bt_audio_set_current_role(BLUETOOTH_A2DP_SOURCE);
896 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
898 int result = BLUETOOTH_ERROR_NONE;
902 switch (event_type) {
903 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
904 case OAL_EVENT_OAL_INITIALISED_FAILED:
905 __bt_handle_oal_initialisation(event_type);
907 case OAL_EVENT_ADAPTER_ENABLED:
908 __bt_adapter_state_change_callback(BT_ACTIVATED);
910 case OAL_EVENT_ADAPTER_DISABLED:
911 __bt_adapter_state_change_callback(BT_DEACTIVATED);
913 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
914 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
916 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
917 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
919 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
920 bt_address_t *bd_addr = event_data;
921 bluetooth_device_address_t local_address;
924 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
925 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
926 local_address.addr[0], local_address.addr[1], local_address.addr[2],
927 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
929 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
930 (void *) &local_address, sizeof(bluetooth_device_address_t));
933 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
934 char *name = event_data;
935 BT_DBG("Adapter Name: %s", name);
937 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
938 bluetooth_device_name_t local_name;
940 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
941 g_strlcpy(local_name.name,
942 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
943 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
944 (void *) &local_name, sizeof(bluetooth_device_name_t));
946 /* Send event to application */
947 _bt_send_event(BT_ADAPTER_EVENT,
948 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
949 g_variant_new("(is)", result, name));
953 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
954 char *ver = event_data;
955 bluetooth_version_t local_version;
957 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
958 g_strlcpy(local_version.version,
959 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
960 BT_DBG("BT Version: %s", local_version.version);
962 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
963 (void *) &local_version, sizeof(bluetooth_version_t));
966 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
968 gboolean connectable = FALSE;
970 BT_INFO("Adapter discoverable mode:"
971 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
972 _bt_send_event(BT_ADAPTER_EVENT,
973 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
974 g_variant_new("(b)", connectable));
976 _bt_send_event(BT_ADAPTER_EVENT,
977 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
978 g_variant_new("(in)", result, mode));
981 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
983 gboolean connectable = TRUE;
985 BT_INFO("Adapter discoverable mode:"
986 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
987 _bt_send_event(BT_ADAPTER_EVENT,
988 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
989 g_variant_new("(b)", connectable));
991 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
992 _bt_send_event(BT_ADAPTER_EVENT,
993 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
994 g_variant_new("(in)", result, mode));
997 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
1000 BT_INFO("Adapter discoverable mode:"
1001 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
1003 /* Send event to application */
1004 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1005 _bt_send_event(BT_ADAPTER_EVENT,
1006 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1007 g_variant_new("(in)", result, mode));
1011 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
1012 int *timeout = event_data;
1015 BT_INFO("Discoverable timeout: [%d]", *timeout);
1017 /* Send event to application */
1018 _bt_get_discoverable_mode(&mode);
1019 _bt_send_event(BT_ADAPTER_EVENT,
1020 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1021 g_variant_new("(in)", result, mode));
1024 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
1026 service_uuid_t *service_list;
1027 event_adapter_services_t *list = event_data;
1030 service_list = list->service_list;
1031 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
1034 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
1037 bluetooth_device_address_t *addr_list;
1039 event_device_list_t *bonded_device_list = event_data;
1040 count = bonded_device_list->num;
1042 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
1043 for (i = 0; i < count; i++) {
1044 memcpy(addr_list[i].addr,
1045 bonded_device_list->devices[i].addr,
1046 BLUETOOTH_ADDRESS_LENGTH);
1049 BT_INFO("Adapter Bonded device List count: [%d]", count);
1050 _bt_device_handle_paired_address_list(addr_list, bonded_device_list->num);
1054 case OAL_EVENT_ADAPTER_PROPERTY_A2DP_ROLE: {
1055 unsigned int *a2dp_role = event_data;
1057 BT_INFO("a2dp role: [%u]", *a2dp_role);
1059 __bt_handle_pending_a2dp_init(*a2dp_role);
1064 BT_ERR("Unhandled event..");
1071 int _bt_init_profiles()
1076 /*TODO: Init bluetooth profiles */
1077 ret = _bt_hidhost_initialize();
1078 if (ret != BLUETOOTH_ERROR_NONE)
1079 BT_ERR("_bt_hidhost_initialize Failed");
1081 ret = _bt_hiddevice_initialize();
1082 if (ret != BLUETOOTH_ERROR_NONE)
1083 BT_ERR("_bt_hiddevice_initialize Failed");
1085 ret = _bt_socket_init();
1086 if (ret != BLUETOOTH_ERROR_NONE)
1087 BT_ERR("_bt_socket_init Failed");
1089 /* Initialize HFP Audio Gateway */
1090 ret = _bt_audio_initialize(BT_AG_MODULE);
1091 if (ret != BLUETOOTH_ERROR_NONE)
1092 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
1094 /* Registering callback for receiving audio services searched */
1095 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
1096 if (ret != BLUETOOTH_ERROR_NONE)
1097 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
1099 ret = _bt_hdp_init();
1100 if (ret != BLUETOOTH_ERROR_NONE)
1101 BT_ERR("_bt_hdp_init Failed");
1103 ret = _bt_gatt_init();
1104 if (ret != BLUETOOTH_ERROR_NONE)
1105 BT_ERR("_bt_gatt_init Failed");
1107 /* Initialize HF Client */
1108 ret = _bt_audio_initialize(BT_HFP_MODULE);
1109 if (ret != BLUETOOTH_ERROR_NONE)
1110 BT_ERR("_bt_audio_initialize(BT_HFP_MODULE) Failed");
1112 return BLUETOOTH_ERROR_NONE;
1115 int _bt_cleanup_profiles(void)
1117 /* TODO: Cleanup bluetooth profiles */
1118 _bt_hidhost_deinitialize();
1119 _bt_socket_deinit();
1121 /* TODO: Cleanup bluetooth audio profiles */
1122 //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
1123 //_bt_audio_deinitialize(BT_AVRCP_MODULE);
1124 //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
1125 //_bt_audio_deinitialize(BT_AG_MODULE);
1126 //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
1127 //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
1129 a2dp_init_pending = TRUE;
1134 return BLUETOOTH_ERROR_NONE;
1137 /* OAL post initialization handler */
1138 static void __bt_post_oal_init(void)
1141 int status = VCONFKEY_BT_STATUS_OFF;
1143 BT_DBG("OAL initialized");
1144 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0)
1145 BT_ERR("Fail to get the enabled value");
1148 /* Update Bluetooth Status to OFF */
1149 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1150 BT_ERR("Set vconf failed\n");
1153 if (status & VCONFKEY_BT_STATUS_ON) {
1154 ret = _bt_enable_adapter();
1155 if (ret != BLUETOOTH_ERROR_NONE)
1156 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
1160 /* OAL initialization handler */
1161 static void __bt_handle_oal_initialisation(oal_event_t event)
1166 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1167 __bt_post_oal_init();
1169 case OAL_EVENT_OAL_INITIALISED_FAILED:
1170 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1171 g_idle_add(_bt_terminate_service, NULL);
1174 BT_ERR("Unknown Event");
1179 static gboolean __bt_is_service_request_present(int service_function)
1182 invocation_info_t *req_info;
1186 /* Get method invocation context */
1187 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1189 if (req_info && req_info->service_function == service_function)
1197 /* Internal functions of core adapter service */
1198 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1202 invocation_info_t *req_info;
1205 /* Get method invocation context */
1206 for (l = _bt_get_invocation_list(); l != NULL; ) {
1208 l = g_slist_next(l);
1209 if (req_info == NULL || req_info->service_function != service_function)
1212 /* Create out param */
1213 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1215 switch (service_function) {
1216 case BT_ENABLE_ADAPTER:
1217 case BT_DISABLE_ADAPTER: {
1218 gboolean done = TRUE;
1219 g_array_append_vals(out_param, &done, sizeof(gboolean));
1222 case BT_GET_LOCAL_NAME:
1223 case BT_GET_LOCAL_ADDRESS:
1224 case BT_GET_LOCAL_VERSION:
1225 g_array_append_vals(out_param, user_data, size);
1227 case BT_IS_SERVICE_USED: {
1229 gboolean used = FALSE;
1230 unsigned char *uuid;
1231 char uuid_str[BT_UUID_STRING_SIZE];
1232 char *request_uuid = req_info->user_data;
1233 service_uuid_t *service_list = user_data;
1235 BT_INFO("Check for service uuid: %s", request_uuid);
1236 for (i = 0; i < size; i++) {
1237 uuid = service_list[i].uuid;
1238 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1239 BT_INFO("Adapter Service: [%s]", uuid_str);
1240 if (strcasecmp(uuid_str, request_uuid) == 0) {
1241 BT_INFO("UUID matched!!");
1247 g_array_append_vals(out_param, &used, sizeof(gboolean));
1251 BT_ERR("Unknown service function[%d]", service_function);
1254 _bt_service_method_return(req_info->context, out_param, req_info->result);
1255 g_array_free(out_param, TRUE);
1256 /* Now free invocation info for this request*/
1257 _bt_free_info_from_invocation_list(req_info);
1261 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1263 char *phone_name = NULL;
1269 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1270 phone_name = vconf_keynode_get_str(node);
1272 if (phone_name && strlen(phone_name) != 0) {
1273 if (!g_utf8_validate(phone_name, -1,
1274 (const char **)&ptr))
1277 _bt_set_local_name(phone_name);
1282 /* Request return handlings */
1283 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1285 int result = BLUETOOTH_ERROR_NONE;
1287 BT_INFO("__bt_adapter_post_set_enabled>>");
1289 if (TIZEN_PROFILE_TV || !headed_plugin_info->plugin_headed_enabled) {
1290 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1291 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1292 BT_ERR("Fail to set discoverable mode");
1294 __bt_set_visible_mode();
1297 /* add the vconf noti handler */
1298 if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1299 (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1300 BT_ERR("DEVICE_NAME key changed notification registration failed");
1302 __bt_set_local_name();
1304 /* Get All properties */
1305 if (OAL_STATUS_SUCCESS != adapter_get_properties())
1306 BT_ERR("adapter_get_properties failed");
1308 __bt_adapter_state_set_status(BT_ACTIVATED);
1310 /*Send BT Enabled event to application */
1311 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1312 g_variant_new("(i)", result));
1317 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1319 BT_INFO("_bt_adapter_post_set_disabled>>");
1321 if (!TIZEN_PROFILE_TV) {
1322 /* Add Adapter disabled post processing codes */
1323 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1324 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1325 BT_ERR("vconf_ignore_key_changed failed");
1328 /* bt-service should be terminated when BT is off */
1329 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1330 /* TODO: Implement to check if it is the recovery mode or not */
1331 _bt_reliable_terminate_service(NULL);
1337 static void __bt_adapter_update_bt_enabled(void)
1339 BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1340 if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1341 BT_ERR("Bluetooth profile init failed");
1343 _bt_device_handle_adapter_state(TRUE);
1345 /* Update Bluetooth Status to notify other modules */
1346 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1347 BT_ERR("Set vconf failed\n");
1349 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1350 BT_ERR("Set vconf failed\n");
1352 /* TODO:Add timer function to handle any further post processing */
1353 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1356 static void __bt_adapter_update_bt_disabled(void)
1358 int power_off_status = 0;
1361 BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1362 _bt_cleanup_profiles();
1364 _bt_device_handle_adapter_state(FALSE);
1366 /* Update the vconf BT status in normal Deactivation case only */
1367 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1368 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1370 /* Update Bluetooth Status to notify other modules */
1371 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1372 BT_ERR("Set vconf failed");
1374 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1375 BT_ERR("Set vconf failed\n");
1377 /* TODO:Add timer function to handle any further post processing */
1378 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1380 /* Pending BT Disabled event before bt-service's termination */
1383 static void __bt_adapter_state_set_status(bt_status_t status)
1385 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1386 adapter_state = status;
1389 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1391 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1392 adapter_discovery_state = status;
1395 static void __bt_adapter_state_change_callback(int bt_status)
1397 BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
1399 switch (bt_status) {
1400 case BT_DEACTIVATED:
1401 __bt_adapter_state_set_status(BT_TERMINATING);
1403 /* Adapter is disabled, unregister event handlers */
1404 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1405 //_bt_deinit_device_event_handler();
1407 /* Add Adapter disabled post processing codes */
1408 __bt_adapter_update_bt_disabled();
1411 /* Add Adapter enabled post processing codes */
1413 BT_DBG("g_source is removed");
1414 g_source_remove(timer_id);
1417 __bt_adapter_update_bt_enabled();
1420 BT_ERR("Incorrect Bluetooth adapter state changed status");
1425 int _bt_set_adapter_request_state(int enable)
1427 return oal_set_adapter_request_state(enable);
1430 int _bt_set_le_request_state(int enable)
1432 return oal_set_le_request_state(enable);
1435 static int __bt_adapter_state_handle_request(gboolean enable)
1437 int result = BLUETOOTH_ERROR_NONE;
1440 switch (adapter_state) {
1442 case BT_DEACTIVATING:
1443 BT_INFO("Should not be callded : state [%d]", adapter_state);
1444 return BLUETOOTH_ERROR_INTERNAL;
1446 case BT_ACTIVATED: {
1447 BT_INFO("Adapter is currently in activated state, state [%d]",
1450 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1452 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1453 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1454 /*TODO Stop Discovery*/
1455 __bt_adapter_update_discovery_status(FALSE);
1457 result = adapter_disable();
1458 if (result != OAL_STATUS_SUCCESS) {
1459 BT_ERR("adapter_enable failed: [%d]", result);
1460 result = BLUETOOTH_ERROR_INTERNAL;
1461 /*TODO: perform if anything more needs to be done to handle failure */
1463 /* TODO: To be handled */
1464 __bt_adapter_state_set_status(BT_DEACTIVATING);
1465 result = BLUETOOTH_ERROR_NONE;
1470 case BT_DEACTIVATED: {
1471 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1474 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1476 result = adapter_enable();
1477 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1478 BT_ERR("adapter_enable failed: [%d]", result);
1480 result = BLUETOOTH_ERROR_INTERNAL;
1481 /*TODO: perform if anything more needs to be done to handle failure */
1483 /* TODO: To be handled */
1484 __bt_adapter_state_set_status(BT_ACTIVATING);
1485 result = BLUETOOTH_ERROR_NONE;
1491 BT_ERR("Unknown state: %d", adapter_state);
1495 if (enable && result == BLUETOOTH_ERROR_NONE) {
1496 /* Adapter enable request is successful, setup event handlers */
1497 _bt_service_register_event_handler_callback(
1498 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1499 _bt_device_state_handle_callback_set_request();
1504 static int __bt_adapter_state_discovery_request(gboolean enable,
1505 unsigned short max_response, unsigned short duration,
1506 unsigned int mask, gboolean is_custom,
1507 bt_discovery_role_type_t role)
1509 int result = BLUETOOTH_ERROR_NONE;
1512 switch (adapter_discovery_state) {
1513 case ADAPTER_DISCOVERY_STARTED: {
1514 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1515 adapter_discovery_state);
1517 return BLUETOOTH_ERROR_IN_PROGRESS;
1519 result = adapter_stop_inquiry();
1520 if (result != OAL_STATUS_SUCCESS) {
1521 BT_ERR("Discover stop failed: %d", result);
1522 result = BLUETOOTH_ERROR_INTERNAL;
1524 BT_ERR("Stop Discovery Triggered successfully");
1525 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1526 result = BLUETOOTH_ERROR_NONE;
1531 case ADAPTER_DISCOVERY_STARTING: {
1532 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1533 adapter_discovery_state);
1535 result = enable ? BLUETOOTH_ERROR_IN_PROGRESS :
1536 BLUETOOTH_ERROR_DEVICE_BUSY;
1540 case ADAPTER_DISCOVERY_STOPPED: {
1541 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1542 adapter_discovery_state);
1544 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1546 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1549 result = adapter_start_inquiry(duration);
1551 result = adapter_start_custom_inquiry(role);
1553 if (result != OAL_STATUS_SUCCESS) {
1554 BT_ERR("Start Discovery failed: %d", result);
1555 result = BLUETOOTH_ERROR_INTERNAL;
1557 BT_ERR("Start Discovery Triggered successfully");
1558 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1559 result = BLUETOOTH_ERROR_NONE;
1564 case ADAPTER_DISCOVERY_STOPPING: {
1565 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1566 adapter_discovery_state);
1568 result = enable ? BLUETOOTH_ERROR_DEVICE_BUSY :
1569 BLUETOOTH_ERROR_NOT_IN_OPERATION;
1574 BT_ERR("Unknown state: %d", adapter_discovery_state);
1582 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1584 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1585 GVariant *param = NULL;
1586 int result = BLUETOOTH_ERROR_NONE;
1588 switch (bt_discovery_status) {
1589 case ADAPTER_DISCOVERY_STOPPED:
1591 __bt_adapter_update_discovery_status(bt_discovery_status);
1592 param = g_variant_new("(i)", result);
1593 _bt_send_event(BT_ADAPTER_EVENT,
1594 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1598 case ADAPTER_DISCOVERY_STARTED:
1600 __bt_adapter_update_discovery_status(bt_discovery_status);
1601 param = g_variant_new("(i)", result);
1602 _bt_send_event(BT_ADAPTER_EVENT,
1603 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1608 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1612 static void __bt_set_visible_mode(void)
1615 #ifdef TIZEN_FEATURE_BT_DPM
1616 int discoverable_state = DPM_BT_ERROR;
1619 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1620 BT_ERR("Fail to get the timeout value");
1622 #ifdef TIZEN_FEATURE_BT_DPM
1623 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1624 if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
1625 if (_bt_set_discoverable_mode(
1626 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1627 timeout) != BLUETOOTH_ERROR_NONE) {
1628 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1629 BT_ERR("Set vconf failed");
1632 if (_bt_set_discoverable_mode(
1633 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1634 timeout) != BLUETOOTH_ERROR_NONE) {
1635 BT_ERR("Set connectable mode failed");
1639 if (timeout == -1) {
1640 if (_bt_set_discoverable_mode(
1641 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1642 timeout) != BLUETOOTH_ERROR_NONE) {
1643 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1644 BT_ERR("Set vconf failed");
1647 if (_bt_set_discoverable_mode(
1648 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1649 timeout) != BLUETOOTH_ERROR_NONE) {
1650 BT_ERR("Set connectable mode failed");
1656 static void __bt_set_local_name(void)
1658 char *phone_name = NULL;
1661 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1666 if (strlen(phone_name) != 0) {
1667 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1670 _bt_set_local_name(phone_name);
1675 void _bt_adapter_set_status(bt_status_t status)
1677 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1678 adapter_state = status;
1681 bt_status_t _bt_adapter_get_status(void)
1683 return adapter_state;
1686 void _bt_set_disabled(int result)
1688 int power_off_status = 0;
1691 int pm_ignore_mode = 0;
1693 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1694 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1696 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1698 /* Update the vconf BT status in normal Deactivation case only */
1699 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1700 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1702 BT_DBG("Update vconf for BT normal Deactivation");
1704 if (result == BLUETOOTH_ERROR_TIMEOUT)
1705 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1706 BT_ERR("Set vconf failed");
1708 /* Update Bluetooth Status to notify other modules */
1709 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1710 BT_ERR("Set vconf failed");
1712 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1713 EVT_VAL_BT_OFF) != ES_R_OK)
1714 BT_ERR("Fail to set value");
1717 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1718 BT_ERR("Set vconf failed\n");
1720 _bt_adapter_set_status(BT_DEACTIVATED);
1721 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1723 /* Send BT Disabled event to application */
1724 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1725 g_variant_new("(i)", result));
1727 BT_INFO("Adapter disabled");