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-socket.h"
52 #include "bt-service-hdp.h"
55 #include <oal-event.h>
56 #include <oal-manager.h>
57 #include <oal-adapter-mgr.h>
59 #ifdef TIZEN_FEATURE_BT_PAN_NAP
60 #include "bt-service-network.h"
62 /*This file will contain state machines related to adapter and remote device */
64 #include "bt-internal-types.h"
66 /* Global variables */
75 static bt_adapter_timer_t visible_timer;
77 static guint timer_id = 0;
79 static gboolean a2dp_init_pending = FALSE;
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 /* Initialize BT stack (Initialize OAL layer) */
109 int _bt_stack_init(void)
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);
119 if (ret != BLUETOOTH_ERROR_NONE) {
120 BT_ERR("_bt_le_init Failed");
124 ret = oal_bt_init(_bt_service_oal_event_receiver);
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;
134 return BLUETOOTH_ERROR_NONE;
137 int _bt_enable_adapter(void)
139 return __bt_adapter_state_handle_request(TRUE);
142 int _bt_enable_core(void)
144 /* TODO_40 : 4.0 merge */
145 BT_INFO("Not Supported");
146 return BLUETOOTH_ERROR_NOT_SUPPORT;
149 int _bt_recover_adapter(void)
151 /* TODO_40 : 4.0 merge */
152 BT_INFO("Not Supported");
153 return BLUETOOTH_ERROR_NOT_SUPPORT;
156 int _bt_reset_adapter(void)
159 if (OAL_STATUS_SUCCESS != adapter_reset())
160 return BLUETOOTH_ERROR_INTERNAL;
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);
168 return BLUETOOTH_ERROR_NONE;
171 int _bt_check_adapter(int *status)
174 gboolean powered = FALSE;
175 BT_CHECK_PARAMETER(status, return);
177 *status = BT_ADAPTER_DISABLED;
179 if (OAL_STATUS_SUCCESS != adapter_get_powered_status(&powered))
180 return BLUETOOTH_ERROR_INTERNAL;
182 BT_INFO("Adapter is in Powered Up state");
183 *status = BT_ADAPTER_ENABLED;
185 BT_INFO("Adapter is in Powered Down state");
187 return BLUETOOTH_ERROR_NONE;
190 int _bt_disable_adapter(void)
192 return __bt_adapter_state_handle_request(FALSE);
195 int _bt_start_discovery(unsigned short max_response,
196 unsigned short duration, unsigned int cod_mask)
198 return __bt_adapter_state_discovery_request(TRUE, max_response, duration,
199 cod_mask, FALSE, 0x00);
202 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
204 return __bt_adapter_state_discovery_request(TRUE, 0, 0, 0, TRUE, role);
207 int _bt_cancel_discovery(void)
209 return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0, FALSE, 0x00);
212 gboolean _bt_is_discovering(void)
214 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
215 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
221 int _bt_get_local_address(void)
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;
232 result = BLUETOOTH_ERROR_NONE;
238 int _bt_get_local_version(void)
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;
248 result = BLUETOOTH_ERROR_NONE;
254 int _bt_get_local_name(void)
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;
265 result = BLUETOOTH_ERROR_NONE;
271 int _bt_set_local_name(char *local_name)
273 int result = BLUETOOTH_ERROR_NONE;
276 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
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;
283 result = BLUETOOTH_ERROR_NONE;
289 int _bt_get_discoverable_mode(int *mode)
296 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
298 adapter_is_discoverable(&scan_mode);
299 if (TRUE == scan_mode) {
300 adapter_get_discoverable_timeout(&timeout);
302 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
304 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
306 adapter_is_connectable(&scan_mode);
307 if (scan_mode == TRUE)
308 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
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.
320 return BLUETOOTH_ERROR_NONE;
323 int _bt_get_timeout_value(int *timeout)
328 /* Take current time */
330 time_diff = difftime(current_time, visible_timer.start_time);
332 BT_DBG("Time diff = %d\n", time_diff);
333 *timeout = visible_timer.timeout - time_diff;
335 return BLUETOOTH_ERROR_NONE;
338 static void __bt_visibility_alarm_remove()
340 if (visible_timer.event_id > 0) {
341 g_source_remove(visible_timer.event_id);
342 visible_timer.event_id = 0;
345 if (visible_timer.alarm_id > 0) {
346 alarmmgr_remove_alarm(visible_timer.alarm_id);
347 visible_timer.alarm_id = 0;
351 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
353 int result = BLUETOOTH_ERROR_NONE;
356 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
358 if (alarm_id != visible_timer.alarm_id)
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;
369 if (!TIZEN_PROFILE_WEARABLE) {
370 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
371 BT_ERR("Set vconf failed\n");
374 /* Switch Off visibility in Bluez */
375 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
376 visible_timer.alarm_id = 0;
380 static gboolean __bt_timeout_handler(gpointer user_data)
382 int result = BLUETOOTH_ERROR_NONE;
386 /* Take current time */
388 time_diff = difftime(current_time, visible_timer.start_time);
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));
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;
400 if (!TIZEN_PROFILE_WEARABLE) {
401 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
402 BT_ERR("Set vconf failed\n");
411 static void __bt_visibility_alarm_create()
416 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
419 BT_ERR("Failed to create alarm error = %d\n", result);
421 BT_DBG("Alarm created = %d\n", alarm_id);
422 visible_timer.alarm_id = alarm_id;
426 int _bt_start_visibility_timer(int timeout)
429 #ifdef TIZEN_FEATURE_BT_DPM
430 int discoverable_state = DPM_BT_ERROR;
433 __bt_visibility_alarm_remove();
435 visible_timer.timeout = timeout;
437 #ifdef TIZEN_FEATURE_BT_DPM
438 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
439 if (discoverable_state != DPM_RESTRICTED) {
441 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
442 BT_ERR("Set vconf failed");
443 #ifdef TIZEN_FEATURE_BT_DPM
448 return BLUETOOTH_ERROR_NONE;
450 if (!visible_timer.alarm_init) {
451 /* Set Alarm timer to switch off BT */
452 result = alarmmgr_init("bt-service");
454 return BLUETOOTH_ERROR_INTERNAL;
456 visible_timer.alarm_init = TRUE;
459 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
461 return BLUETOOTH_ERROR_INTERNAL;
463 if (!TIZEN_PROFILE_WEARABLE) {
464 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
465 BT_ERR("Set vconf failed");
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);
473 visible_timer.timeout = timeout;
475 __bt_visibility_alarm_create();
477 return BLUETOOTH_ERROR_NONE;
480 int _bt_stop_visibility_timer(void)
482 __bt_visibility_alarm_remove();
484 visible_timer.timeout = 0;
486 if (!TIZEN_PROFILE_WEARABLE) {
487 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
488 BT_ERR("Set vconf failed");
491 return BLUETOOTH_ERROR_NONE;
494 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
497 int ret = BLUETOOTH_ERROR_NONE;;
498 #ifdef TIZEN_FEATURE_BT_DPM
499 int discoverable_state = DPM_BT_ERROR;
504 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
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)
511 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
512 return BLUETOOTH_ERROR_ACCESS_DENIED;
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;
522 switch (discoverable_mode) {
523 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
524 result = adapter_set_connectable(TRUE);
527 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
528 result = adapter_set_discoverable();
531 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
532 result = adapter_set_discoverable();
535 return BLUETOOTH_ERROR_INVALID_PARAM;
538 if (result != OAL_STATUS_SUCCESS) {
539 BT_ERR("set scan mode failed %d", result);
540 return BLUETOOTH_ERROR_INTERNAL;
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;
549 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
550 ret = _bt_stop_visibility_timer();
551 if (BLUETOOTH_ERROR_NONE != ret)
552 BT_ERR("_bt_stop_visibility_timer failed");
553 if (!TIZEN_PROFILE_WEARABLE) {
554 if (vconf_set_int(BT_FILE_VISIBLE_TIME, -1) != 0)
555 BT_ERR("Set vconf failed");
557 } else if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE) {
558 ret = _bt_start_visibility_timer(timeout);
559 if (BLUETOOTH_ERROR_NONE != ret)
560 BT_ERR("_bt_start_visibility_timer failed");
563 ret = _bt_stop_visibility_timer();
564 if (BLUETOOTH_ERROR_NONE != ret)
565 BT_ERR("_bt_stop_visibility_timer failed");
572 gboolean _bt_is_connectable(void)
579 adapter_is_connectable(&connectable);
585 BT_DBG("Connectable: [%s]", result ? "TRUE" : "FALSE");
590 int _bt_set_connectable(gboolean connectable)
592 int result = BLUETOOTH_ERROR_NONE;
595 result = adapter_set_connectable(connectable);
596 if (result != OAL_STATUS_SUCCESS) {
597 BT_ERR("adapter_set_connectable failed: %d", result);
598 result = BLUETOOTH_ERROR_INTERNAL;
600 result = BLUETOOTH_ERROR_NONE;
606 int _bt_is_service_used(void)
612 result = adapter_get_service_uuids();
613 if (result != OAL_STATUS_SUCCESS) {
614 BT_ERR("adapter_get_service_uuids failed: %d", result);
615 result = BLUETOOTH_ERROR_INTERNAL;
617 result = BLUETOOTH_ERROR_NONE;
624 int _bt_adapter_get_bonded_devices(void)
626 int result = BLUETOOTH_ERROR_NONE;
629 result = adapter_get_bonded_devices();
630 if (result != OAL_STATUS_SUCCESS) {
631 BT_ERR("adapter_get_bonded_devices failed: %d", result);
632 result = BLUETOOTH_ERROR_INTERNAL;
634 result = BLUETOOTH_ERROR_NONE;
640 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
643 GDBusConnection *conn;
644 GDBusProxy *manager_proxy;
645 GVariant *result = NULL;
646 GVariant *result1 = NULL;
647 GVariantIter *iter = NULL;
648 GError *error = NULL;
649 char *object_path = NULL;
650 GVariantIter *interface_iter;
651 char *interface_str = NULL;
652 GDBusProxy *device_proxy = NULL;
653 gboolean is_connected = FALSE;
655 conn = _bt_gdbus_get_system_gconn();
656 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
658 manager_proxy = _bt_get_manager_proxy();
659 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
661 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
663 G_DBUS_CALL_FLAGS_NONE,
670 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
671 g_clear_error(&error);
674 BT_ERR("Failed to Failed to GetManagedObjects");
675 return BLUETOOTH_ERROR_INTERNAL;
678 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
679 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
681 /* Parse the signature: oa{sa{sv}}} */
682 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
683 if (object_path == NULL)
686 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
687 &interface_str, NULL)) {
688 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
689 BT_DBG("Found a device: %s", object_path);
690 g_free(interface_str);
692 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
694 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
696 if (device_proxy == NULL) {
697 BT_DBG("Device don't have this service");
701 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
702 g_variant_new("(s)", profile_uuid),
703 G_DBUS_CALL_FLAGS_NONE,
708 if (result1 == NULL) {
709 BT_ERR("Error occured in Proxy call");
711 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
715 g_object_unref(device_proxy);
718 g_variant_get(result1, "(b)", &is_connected);
720 if (is_connected == TRUE) {
721 char address[BT_ADDRESS_STRING_SIZE];
722 bluetooth_device_address_t *addr = NULL;
724 _bt_convert_device_path_to_address(object_path, address);
726 addr = g_malloc0(sizeof(bluetooth_device_address_t));
727 _bt_convert_addr_string_to_type(addr->addr, address);
729 g_array_append_vals(*addr_list, addr,
730 sizeof(bluetooth_device_address_t));
733 g_variant_unref(result1);
734 g_object_unref(device_proxy);
741 g_variant_unref(result);
742 g_variant_iter_free(iter);
745 return BLUETOOTH_ERROR_NONE;
748 static void __bt_handle_pending_a2dp_init(service_uuid_t *service_list, unsigned int count)
753 char uuid_str[BT_UUID_STRING_SIZE];
755 if (!a2dp_init_pending)
759 a2dp_init_pending = FALSE;
760 for (i = 0; i < count; i++) {
761 uuid = service_list[i].uuid;
762 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
763 BT_INFO("Adapter Service: [%s]", uuid_str);
764 if (!strcasecmp(uuid_str, A2DP_SINK_UUID)) {
765 BT_INFO("Enable A2DP Sink role");
766 /* Initialize A2DP Sink */
767 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
768 if (ret != BLUETOOTH_ERROR_NONE)
769 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
771 /* Initialize AVRCP Controller */
772 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
773 if (ret != BLUETOOTH_ERROR_NONE)
774 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
776 _bt_audio_set_current_role(BLUETOOTH_A2DP_SINK);
781 BT_INFO("Enable A2DP Source role by default");
782 /* Initialize A2DP Source */
783 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
784 if (ret != BLUETOOTH_ERROR_NONE)
785 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
787 /* Initialize AVRCP Target */
788 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
789 if (ret != BLUETOOTH_ERROR_NONE)
790 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
792 _bt_audio_set_current_role(BLUETOOTH_A2DP_SOURCE);
796 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
798 int result = BLUETOOTH_ERROR_NONE;
802 switch (event_type) {
803 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
804 case OAL_EVENT_OAL_INITIALISED_FAILED:
805 __bt_handle_oal_initialisation(event_type);
807 case OAL_EVENT_ADAPTER_ENABLED:
808 __bt_adapter_state_change_callback(BT_ACTIVATED);
810 case OAL_EVENT_ADAPTER_DISABLED:
811 __bt_adapter_state_change_callback(BT_DEACTIVATED);
813 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
814 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
816 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
817 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
819 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
820 bt_address_t *bd_addr = event_data;
821 bluetooth_device_address_t local_address;
824 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
825 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
826 local_address.addr[0], local_address.addr[1], local_address.addr[2],
827 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
829 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
830 (void *) &local_address, sizeof(bluetooth_device_address_t));
833 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
834 char *name = event_data;
835 BT_DBG("Adapter Name: %s", name);
837 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
838 bluetooth_device_name_t local_name;
840 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
841 g_strlcpy(local_name.name,
842 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
843 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
844 (void *) &local_name, sizeof(bluetooth_device_name_t));
846 /* Send event to application */
847 _bt_send_event(BT_ADAPTER_EVENT,
848 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
849 g_variant_new("(is)", result, name));
853 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
854 char *ver = event_data;
855 bluetooth_version_t local_version;
857 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
858 g_strlcpy(local_version.version,
859 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
860 BT_DBG("BT Version: %s", local_version.version);
862 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
863 (void *) &local_version, sizeof(bluetooth_version_t));
866 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
868 gboolean connectable = FALSE;
870 BT_INFO("Adapter discoverable mode:"
871 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
872 _bt_send_event(BT_ADAPTER_EVENT,
873 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
874 g_variant_new("(b)", connectable));
876 _bt_send_event(BT_ADAPTER_EVENT,
877 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
878 g_variant_new("(in)", result, mode));
881 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
883 gboolean connectable = TRUE;
885 BT_INFO("Adapter discoverable mode:"
886 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
887 _bt_send_event(BT_ADAPTER_EVENT,
888 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
889 g_variant_new("(b)", connectable));
891 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
892 _bt_send_event(BT_ADAPTER_EVENT,
893 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
894 g_variant_new("(in)", result, mode));
897 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
900 BT_INFO("Adapter discoverable mode:"
901 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
903 /* Send event to application */
904 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
905 _bt_send_event(BT_ADAPTER_EVENT,
906 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
907 g_variant_new("(in)", result, mode));
911 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
912 int *timeout = event_data;
915 BT_INFO("Discoverable timeout: [%d]", *timeout);
917 /* Send event to application */
918 _bt_get_discoverable_mode(&mode);
919 _bt_send_event(BT_ADAPTER_EVENT,
920 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
921 g_variant_new("(in)", result, mode));
924 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
926 service_uuid_t *service_list;
927 event_adapter_services_t *list = event_data;
930 service_list = list->service_list;
931 __bt_handle_pending_a2dp_init(service_list, count);
932 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
935 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
938 bluetooth_device_address_t *addr_list;
940 event_device_list_t *bonded_device_list = event_data;
941 count = bonded_device_list->num;
943 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
944 for (i = 0; i < count; i++) {
945 memcpy(addr_list[i].addr,
946 bonded_device_list->devices[i].addr,
947 BLUETOOTH_ADDRESS_LENGTH);
950 BT_INFO("Adapter Bonded device List count: [%d]", count);
951 _bt_device_handle_paired_address_list(addr_list, bonded_device_list->num);
956 BT_ERR("Unhandled event..");
963 int _bt_init_profiles()
968 /*TODO: Init bluetooth profiles */
969 ret = _bt_hidhost_initialize();
970 if (ret != BLUETOOTH_ERROR_NONE)
971 BT_ERR("_bt_hidhost_initialize Failed");
973 ret = _bt_socket_init();
974 if (ret != BLUETOOTH_ERROR_NONE)
975 BT_ERR("_bt_socket_init Failed");
978 * Query local adapter services and based on a2dp service uuids initialized
979 * in bluetooth stack, enable A2DP sourec or A2DP sink role.
981 ret = adapter_get_service_uuids();
982 if (ret != OAL_STATUS_SUCCESS)
983 BT_ERR("adapter_get_service_uuids failed: %d", ret);
985 a2dp_init_pending = TRUE;
988 /* Initialize HFP Audio Gateway */
989 ret = _bt_audio_initialize(BT_AG_MODULE);
990 if (ret != BLUETOOTH_ERROR_NONE)
991 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
993 /* Registering callback for receiving audio services searched */
994 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
995 if (ret != BLUETOOTH_ERROR_NONE)
996 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
998 ret = _bt_hdp_init();
999 if (ret != BLUETOOTH_ERROR_NONE)
1000 BT_ERR("_bt_hdp_init Failed");
1002 ret = _bt_gatt_init();
1003 if (ret != BLUETOOTH_ERROR_NONE)
1004 BT_ERR("_bt_gatt_init Failed");
1006 return BLUETOOTH_ERROR_NONE;
1009 int _bt_cleanup_profiles(void)
1011 /* TODO: Cleanup bluetooth profiles */
1012 _bt_hidhost_deinitialize();
1013 _bt_socket_deinit();
1015 /* TODO: Cleanup bluetooth audio profiles */
1016 //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
1017 //_bt_audio_deinitialize(BT_AVRCP_MODULE);
1018 //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
1019 //_bt_audio_deinitialize(BT_AG_MODULE);
1020 //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
1021 //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
1026 return BLUETOOTH_ERROR_NONE;
1029 /* OAL post initialization handler */
1030 static void __bt_post_oal_init(void)
1033 int status = VCONFKEY_BT_STATUS_OFF;
1035 BT_DBG("OAL initialized");
1036 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0)
1037 BT_ERR("Fail to get the enabled value");
1040 /* Update Bluetooth Status to OFF */
1041 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1042 BT_ERR("Set vconf failed\n");
1045 if (status & VCONFKEY_BT_STATUS_ON) {
1046 ret = _bt_enable_adapter();
1047 if (ret != BLUETOOTH_ERROR_NONE)
1048 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
1052 /* OAL initialization handler */
1053 static void __bt_handle_oal_initialisation(oal_event_t event)
1058 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1059 __bt_post_oal_init();
1061 case OAL_EVENT_OAL_INITIALISED_FAILED:
1062 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1063 g_idle_add(_bt_terminate_service, NULL);
1066 BT_ERR("Unknown Event");
1071 static gboolean __bt_is_service_request_present(int service_function)
1074 invocation_info_t *req_info;
1078 /* Get method invocation context */
1079 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1081 if (req_info && req_info->service_function == service_function)
1089 /* Internal functions of core adapter service */
1090 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1094 invocation_info_t *req_info;
1097 /* Get method invocation context */
1098 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1100 if (req_info == NULL || req_info->service_function != service_function)
1103 /* Create out param */
1104 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1106 switch (service_function) {
1107 case BT_ENABLE_ADAPTER:
1108 case BT_DISABLE_ADAPTER: {
1109 gboolean done = TRUE;
1110 g_array_append_vals(out_param, &done, sizeof(gboolean));
1113 case BT_GET_LOCAL_NAME:
1114 case BT_GET_LOCAL_ADDRESS:
1115 case BT_GET_LOCAL_VERSION:
1116 g_array_append_vals(out_param, user_data, size);
1118 case BT_IS_SERVICE_USED: {
1120 gboolean used = FALSE;
1121 unsigned char *uuid;
1122 char uuid_str[BT_UUID_STRING_SIZE];
1123 char *request_uuid = req_info->user_data;
1124 service_uuid_t *service_list = user_data;
1126 BT_INFO("Check for service uuid: %s", request_uuid);
1127 for (i = 0; i < size; i++) {
1128 uuid = service_list[i].uuid;
1129 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1130 BT_INFO("Adapter Service: [%s]", uuid_str);
1131 if (strcasecmp(uuid_str, request_uuid) == 0) {
1132 BT_INFO("UUID matched!!");
1138 g_array_append_vals(out_param, &used, sizeof(gboolean));
1142 BT_ERR("Unknown service function[%d]", service_function);
1145 _bt_service_method_return(req_info->context, out_param, req_info->result);
1146 g_array_free(out_param, TRUE);
1147 /* Now free invocation info for this request*/
1148 _bt_free_info_from_invocation_list(req_info);
1152 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1154 char *phone_name = NULL;
1160 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1161 phone_name = vconf_keynode_get_str(node);
1163 if (phone_name && strlen(phone_name) != 0) {
1164 if (!g_utf8_validate(phone_name, -1,
1165 (const char **)&ptr))
1168 _bt_set_local_name(phone_name);
1173 /* Request return handlings */
1174 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1176 BT_INFO("__bt_adapter_post_set_enabled>>");
1178 if (TIZEN_PROFILE_TV || !headed_plugin_info->plugin_headed_enabled) {
1179 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1180 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1181 BT_ERR("Fail to set discoverable mode");
1183 __bt_set_visible_mode();
1185 /* add the vconf noti handler */
1186 if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1187 (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1188 BT_ERR("DEVICE_NAME key changed notification registration failed");
1190 __bt_set_local_name();
1193 /* Get All properties */
1194 if (OAL_STATUS_SUCCESS != adapter_get_properties())
1195 BT_ERR("adapter_get_properties failed");
1197 /* Add Adapter enabled post processing codes */
1201 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1203 BT_INFO("_bt_adapter_post_set_disabled>>");
1205 if (!TIZEN_PROFILE_TV) {
1206 /* Add Adapter disabled post processing codes */
1207 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1208 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1209 BT_ERR("vconf_ignore_key_changed failed");
1212 /* bt-service should be terminated when BT is off */
1213 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1214 /* TODO: Implement to check if it is the recovery mode or not */
1215 _bt_reliable_terminate_service(NULL);
1221 static void __bt_adapter_update_bt_enabled(void)
1223 int result = BLUETOOTH_ERROR_NONE;
1225 BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1226 if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1227 BT_ERR("Bluetooth profile init failed");
1229 _bt_device_handle_adapter_state(TRUE);
1231 /* Update Bluetooth Status to notify other modules */
1232 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1233 BT_ERR("Set vconf failed\n");
1235 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1236 BT_ERR("Set vconf failed\n");
1238 /* TODO:Add timer function to handle any further post processing */
1239 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1241 /*Return BT_ADAPTER_ENABLE Method invocation context */
1242 __bt_adapter_handle_pending_requests(BT_ENABLE_ADAPTER, NULL, 0);
1243 /*Send BT Enabled event to application */
1244 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1245 g_variant_new("(i)", result));
1248 static void __bt_adapter_update_bt_disabled(void)
1250 int result = BLUETOOTH_ERROR_NONE;
1251 int power_off_status = 0;
1254 BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1255 _bt_cleanup_profiles();
1257 _bt_device_handle_adapter_state(FALSE);
1259 /* Update the vconf BT status in normal Deactivation case only */
1260 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1261 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1263 /* Update Bluetooth Status to notify other modules */
1264 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1265 BT_ERR("Set vconf failed");
1267 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1268 BT_ERR("Set vconf failed\n");
1270 /* TODO:Add timer function to handle any further post processing */
1271 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1273 /* Return BT_ADAPTER_DISABLE Method invocation context */
1274 __bt_adapter_handle_pending_requests(BT_DISABLE_ADAPTER, NULL, 0);
1276 /* Send BT Disabled event to application */
1277 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1278 g_variant_new("(i)", result));
1281 static void __bt_adapter_state_set_status(bt_status_t status)
1283 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1284 adapter_state = status;
1287 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1289 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1290 adapter_discovery_state = status;
1293 static void __bt_adapter_state_change_callback(int bt_status)
1295 BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
1297 switch (bt_status) {
1298 case BT_DEACTIVATED:
1299 __bt_adapter_state_set_status(bt_status);
1301 /* Adapter is disabled, unregister event handlers */
1302 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1303 //_bt_deinit_device_event_handler();
1305 /* Add Adapter disabled post processing codes */
1306 __bt_adapter_update_bt_disabled();
1309 __bt_adapter_state_set_status(bt_status);
1310 /* Add Adapter enabled post processing codes */
1312 BT_DBG("g_source is removed");
1313 g_source_remove(timer_id);
1316 __bt_adapter_update_bt_enabled();
1319 BT_ERR("Incorrect Bluetooth adapter state changed status");
1324 static int __bt_adapter_state_handle_request(gboolean enable)
1326 int result = BLUETOOTH_ERROR_NONE;
1329 switch (adapter_state) {
1330 case BT_ACTIVATING: {
1331 BT_INFO("Adapter is currently in activating state, state [%d]",
1334 return BLUETOOTH_ERROR_IN_PROGRESS;
1336 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1337 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1338 /*TODO Stop Discovery*/
1339 __bt_adapter_update_discovery_status(FALSE);
1341 result = adapter_disable();
1342 if (result != OAL_STATUS_SUCCESS) {
1343 BT_ERR("adapter_enable failed: [%d]", result);
1344 result = BLUETOOTH_ERROR_INTERNAL;
1345 /*TODO: perform if anything more needs to be done to handle failure */
1347 /* TODO: To be handled */
1348 __bt_adapter_state_set_status(BT_DEACTIVATING);
1349 result = BLUETOOTH_ERROR_NONE;
1354 case BT_ACTIVATED: {
1355 BT_INFO("Adapter is currently in activated state, state [%d]",
1358 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1360 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1361 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1362 /*TODO Stop Discovery*/
1363 __bt_adapter_update_discovery_status(FALSE);
1365 result = adapter_disable();
1366 if (result != OAL_STATUS_SUCCESS) {
1367 BT_ERR("adapter_enable failed: [%d]", result);
1368 result = BLUETOOTH_ERROR_INTERNAL;
1369 /*TODO: perform if anything more needs to be done to handle failure */
1371 /* TODO: To be handled */
1372 __bt_adapter_state_set_status(BT_DEACTIVATING);
1373 result = BLUETOOTH_ERROR_NONE;
1378 case BT_DEACTIVATING: {
1379 BT_INFO("Adapter is currently in deactivating state, state [%d]",
1382 return BLUETOOTH_ERROR_IN_PROGRESS;
1385 result = adapter_enable();
1386 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1387 BT_ERR("adapter_enable failed: [%d]", result);
1389 result = BLUETOOTH_ERROR_INTERNAL;
1390 /*TODO: perform if anything more needs to be done to handle failure */
1392 /* TODO: To be handled */
1393 __bt_adapter_state_set_status(BT_ACTIVATING);
1394 result = BLUETOOTH_ERROR_NONE;
1399 case BT_DEACTIVATED: {
1400 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1403 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1405 result = adapter_enable();
1406 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1407 BT_ERR("adapter_enable failed: [%d]", result);
1409 result = BLUETOOTH_ERROR_INTERNAL;
1410 /*TODO: perform if anything more needs to be done to handle failure */
1412 /* TODO: To be handled */
1413 __bt_adapter_state_set_status(BT_ACTIVATING);
1414 result = BLUETOOTH_ERROR_NONE;
1420 BT_ERR("Unknown state: %d", adapter_state);
1424 if (enable && result == BLUETOOTH_ERROR_NONE) {
1425 /* Adapter enable request is successful, setup event handlers */
1426 _bt_service_register_event_handler_callback(
1427 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1428 _bt_device_state_handle_callback_set_request();
1433 static int __bt_adapter_state_discovery_request(gboolean enable,
1434 unsigned short max_response, unsigned short duration,
1435 unsigned int mask, gboolean is_custom,
1436 bt_discovery_role_type_t role)
1438 int result = BLUETOOTH_ERROR_NONE;
1441 switch (adapter_discovery_state) {
1442 case ADAPTER_DISCOVERY_STARTED: {
1443 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1444 adapter_discovery_state);
1446 return BLUETOOTH_ERROR_IN_PROGRESS;
1448 result = adapter_stop_inquiry();
1449 if (result != OAL_STATUS_SUCCESS) {
1450 BT_ERR("Discover stop failed: %d", result);
1451 result = BLUETOOTH_ERROR_INTERNAL;
1453 BT_ERR("Stop Discovery Triggered successfully");
1454 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1455 result = BLUETOOTH_ERROR_NONE;
1460 case ADAPTER_DISCOVERY_STARTING: {
1461 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1462 adapter_discovery_state);
1464 return BLUETOOTH_ERROR_IN_PROGRESS;
1466 result = adapter_stop_inquiry();
1467 if (result != OAL_STATUS_SUCCESS) {
1468 BT_ERR("Discover stop failed: %d", result);
1469 result = BLUETOOTH_ERROR_INTERNAL;
1471 BT_ERR("Stop Discovery Triggered successfully");
1472 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1473 result = BLUETOOTH_ERROR_NONE;
1478 case ADAPTER_DISCOVERY_STOPPED: {
1479 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1480 adapter_discovery_state);
1482 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1484 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1487 result = adapter_start_inquiry(duration);
1489 result = adapter_start_custom_inquiry(role);
1491 if (result != OAL_STATUS_SUCCESS) {
1492 BT_ERR("Start Discovery failed: %d", result);
1493 result = BLUETOOTH_ERROR_INTERNAL;
1495 BT_ERR("Start Discovery Triggered successfully");
1496 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1497 result = BLUETOOTH_ERROR_NONE;
1502 case ADAPTER_DISCOVERY_STOPPING: {
1503 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1504 adapter_discovery_state);
1506 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1508 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1510 result = adapter_start_inquiry(duration);
1512 result = adapter_start_custom_inquiry(role);
1513 if (result != OAL_STATUS_SUCCESS) {
1514 BT_ERR("Start Discovery failed: %d", result);
1515 result = BLUETOOTH_ERROR_INTERNAL;
1517 BT_ERR("Start Discovery Triggered successfully");
1518 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1519 result = BLUETOOTH_ERROR_NONE;
1525 BT_ERR("Unknown state: %d", adapter_discovery_state);
1533 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1535 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1536 GVariant *param = NULL;
1537 int result = BLUETOOTH_ERROR_NONE;
1539 switch (bt_discovery_status) {
1540 case ADAPTER_DISCOVERY_STOPPED:
1542 __bt_adapter_update_discovery_status(bt_discovery_status);
1543 param = g_variant_new("(i)", result);
1544 _bt_send_event(BT_ADAPTER_EVENT,
1545 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1549 case ADAPTER_DISCOVERY_STARTED:
1551 __bt_adapter_update_discovery_status(bt_discovery_status);
1552 param = g_variant_new("(i)", result);
1553 _bt_send_event(BT_ADAPTER_EVENT,
1554 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1559 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1563 static void __bt_set_visible_mode(void)
1566 #ifdef TIZEN_FEATURE_BT_DPM
1567 int discoverable_state = DPM_BT_ERROR;
1570 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1571 BT_ERR("Fail to get the timeout value");
1573 #ifdef TIZEN_FEATURE_BT_DPM
1574 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1575 if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
1576 if (_bt_set_discoverable_mode(
1577 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1578 timeout) != BLUETOOTH_ERROR_NONE) {
1579 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1580 BT_ERR("Set vconf failed");
1583 if (_bt_set_discoverable_mode(
1584 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1585 timeout) != BLUETOOTH_ERROR_NONE) {
1586 BT_ERR("Set connectable mode failed");
1590 if (timeout == -1) {
1591 if (_bt_set_discoverable_mode(
1592 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1593 timeout) != BLUETOOTH_ERROR_NONE) {
1594 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1595 BT_ERR("Set vconf failed");
1598 if (_bt_set_discoverable_mode(
1599 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1600 timeout) != BLUETOOTH_ERROR_NONE) {
1601 BT_ERR("Set connectable mode failed");
1607 static void __bt_set_local_name(void)
1609 char *phone_name = NULL;
1612 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1617 if (strlen(phone_name) != 0) {
1618 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1621 _bt_set_local_name(phone_name);
1626 void _bt_adapter_set_status(bt_status_t status)
1628 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1629 adapter_state = status;
1632 bt_status_t _bt_adapter_get_status(void)
1634 return adapter_state;
1637 void _bt_set_disabled(int result)
1639 int power_off_status = 0;
1642 int pm_ignore_mode = 0;
1644 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1645 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1647 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1649 /* Update the vconf BT status in normal Deactivation case only */
1650 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1651 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1653 BT_DBG("Update vconf for BT normal Deactivation");
1655 if (result == BLUETOOTH_ERROR_TIMEOUT)
1656 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1657 BT_ERR("Set vconf failed");
1659 /* Update Bluetooth Status to notify other modules */
1660 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1661 BT_ERR("Set vconf failed");
1663 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1664 EVT_VAL_BT_OFF) != ES_R_OK)
1665 BT_ERR("Fail to set value");
1668 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1669 BT_ERR("Set vconf failed\n");
1671 _bt_adapter_set_status(BT_DEACTIVATED);
1672 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1674 BT_INFO("Adapter disabled");