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;
81 /* Adapter default states */
82 static bt_status_t adapter_state = BT_DEACTIVATED;
83 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
85 /* Forward declarations */
86 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
87 static void __bt_post_oal_init(void);
88 static void __bt_handle_oal_initialisation(oal_event_t event);
89 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
90 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
91 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
92 static void __bt_adapter_update_bt_enabled(void);
93 static void __bt_adapter_update_bt_disabled(void);
94 static void __bt_adapter_state_set_status(bt_status_t status);
95 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
96 static void __bt_adapter_state_change_callback(int bt_status);
97 static int __bt_adapter_state_handle_request(gboolean enable);
98 static int __bt_adapter_state_discovery_request(gboolean enable,
99 unsigned short max_response, unsigned short duration, unsigned int mask,
100 gboolean is_custom, bt_discovery_role_type_t role);
101 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
102 static gboolean __bt_is_service_request_present(int service_function);
104 static void __bt_set_visible_mode(void);
105 static void __bt_set_local_name(void);
106 int set_adapter_request_state(int enable);
107 int set_le_request_state(int enable);
109 /* Initialize BT stack (Initialize OAL layer) */
110 int _bt_stack_init(void)
114 BT_INFO("[bt-service] Start to initialize BT stack");
115 /* Adapter enable request is successful, setup event handlers */
116 _bt_service_register_event_handler_callback(
117 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
120 if (ret != BLUETOOTH_ERROR_NONE) {
121 BT_ERR("_bt_le_init Failed");
125 ret = oal_bt_init(_bt_service_oal_event_receiver);
127 if (OAL_STATUS_PENDING == ret) {
128 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
129 return BLUETOOTH_ERROR_NONE;
130 } else if (OAL_STATUS_SUCCESS != ret) {
131 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
132 return BLUETOOTH_ERROR_INTERNAL;
135 return BLUETOOTH_ERROR_NONE;
138 int _bt_enable_adapter(void)
140 return __bt_adapter_state_handle_request(TRUE);
143 int _bt_enable_core(void)
145 /* TODO_40 : 4.0 merge */
146 BT_INFO("Not Supported");
147 return BLUETOOTH_ERROR_NOT_SUPPORT;
150 int _bt_recover_adapter(void)
152 /* TODO_40 : 4.0 merge */
153 BT_INFO("Not Supported");
154 return BLUETOOTH_ERROR_NOT_SUPPORT;
157 int _bt_reset_adapter(void)
160 if (OAL_STATUS_SUCCESS != adapter_reset())
161 return BLUETOOTH_ERROR_INTERNAL;
163 /* TODO_40 : 4.0 merge */
164 /* TODO Currently bt-service is not terminated in tizen next.
165 It should be handled in future patch */
166 if (_bt_adapter_get_status() == BT_DEACTIVATED)
167 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
169 return BLUETOOTH_ERROR_NONE;
172 int _bt_check_adapter(int *status)
175 gboolean powered = FALSE;
176 BT_CHECK_PARAMETER(status, return);
178 *status = BT_ADAPTER_DISABLED;
180 if (OAL_STATUS_SUCCESS != adapter_get_powered_status(&powered))
181 return BLUETOOTH_ERROR_INTERNAL;
183 BT_INFO("Adapter is in Powered Up state");
184 *status = BT_ADAPTER_ENABLED;
186 BT_INFO("Adapter is in Powered Down state");
188 return BLUETOOTH_ERROR_NONE;
191 int _bt_disable_adapter(void)
193 return __bt_adapter_state_handle_request(FALSE);
196 int _bt_start_discovery(unsigned short max_response,
197 unsigned short duration, unsigned int cod_mask)
199 return __bt_adapter_state_discovery_request(TRUE, max_response, duration,
200 cod_mask, FALSE, 0x00);
203 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
205 return __bt_adapter_state_discovery_request(TRUE, 0, 0, 0, TRUE, role);
208 int _bt_cancel_discovery(void)
210 return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0, FALSE, 0x00);
213 gboolean _bt_is_discovering(void)
215 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
216 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
222 int _bt_get_local_address(void)
228 result = adapter_get_address();
229 if (result != OAL_STATUS_SUCCESS) {
230 BT_ERR("adapter_get_address failed: %d", result);
231 result = BLUETOOTH_ERROR_INTERNAL;
233 result = BLUETOOTH_ERROR_NONE;
239 int _bt_get_local_version(void)
244 result = adapter_get_version();
245 if (result != OAL_STATUS_SUCCESS) {
246 BT_ERR("adapter_get_version failed: %d", result);
247 result = BLUETOOTH_ERROR_INTERNAL;
249 result = BLUETOOTH_ERROR_NONE;
255 int _bt_get_local_name(void)
261 result = adapter_get_name();
262 if (result != OAL_STATUS_SUCCESS) {
263 BT_ERR("adapter_get_name failed: %d", result);
264 result = BLUETOOTH_ERROR_INTERNAL;
266 result = BLUETOOTH_ERROR_NONE;
272 int _bt_set_local_name(char *local_name)
274 int result = BLUETOOTH_ERROR_NONE;
277 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
279 result = adapter_set_name(local_name);
280 if (result != OAL_STATUS_SUCCESS) {
281 BT_ERR("adapter_set_name failed: %d", result);
282 result = BLUETOOTH_ERROR_INTERNAL;
284 result = BLUETOOTH_ERROR_NONE;
290 int _bt_get_discoverable_mode(int *mode)
297 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
299 adapter_is_discoverable(&scan_mode);
300 if (TRUE == scan_mode) {
301 adapter_get_discoverable_timeout(&timeout);
303 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
305 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
307 adapter_is_connectable(&scan_mode);
308 if (scan_mode == TRUE)
309 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
312 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
313 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
314 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
321 return BLUETOOTH_ERROR_NONE;
324 int _bt_get_timeout_value(int *timeout)
329 /* Take current time */
331 time_diff = difftime(current_time, visible_timer.start_time);
333 BT_DBG("Time diff = %d\n", time_diff);
334 *timeout = visible_timer.timeout - time_diff;
336 return BLUETOOTH_ERROR_NONE;
339 static void __bt_visibility_alarm_remove()
341 if (visible_timer.event_id > 0) {
342 g_source_remove(visible_timer.event_id);
343 visible_timer.event_id = 0;
346 if (visible_timer.alarm_id > 0) {
347 alarmmgr_remove_alarm(visible_timer.alarm_id);
348 visible_timer.alarm_id = 0;
352 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
354 int result = BLUETOOTH_ERROR_NONE;
357 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
359 if (alarm_id != visible_timer.alarm_id)
362 if (visible_timer.event_id) {
363 _bt_send_event(BT_ADAPTER_EVENT,
364 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
365 g_variant_new("(in)", result, timeout));
366 g_source_remove(visible_timer.event_id);
367 visible_timer.event_id = 0;
368 visible_timer.timeout = 0;
370 if (!TIZEN_PROFILE_WEARABLE) {
371 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
372 BT_ERR("Set vconf failed\n");
375 /* Switch Off visibility in Bluez */
376 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
377 visible_timer.alarm_id = 0;
381 static gboolean __bt_timeout_handler(gpointer user_data)
383 int result = BLUETOOTH_ERROR_NONE;
387 /* Take current time */
389 time_diff = difftime(current_time, visible_timer.start_time);
391 /* Send event to application */
392 _bt_send_event(BT_ADAPTER_EVENT,
393 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
394 g_variant_new("(in)", result, time_diff));
396 if (visible_timer.timeout <= time_diff) {
397 g_source_remove(visible_timer.event_id);
398 visible_timer.event_id = 0;
399 visible_timer.timeout = 0;
401 if (!TIZEN_PROFILE_WEARABLE) {
402 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
403 BT_ERR("Set vconf failed\n");
412 static void __bt_visibility_alarm_create()
417 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
420 BT_ERR("Failed to create alarm error = %d\n", result);
422 BT_DBG("Alarm created = %d\n", alarm_id);
423 visible_timer.alarm_id = alarm_id;
427 int _bt_start_visibility_timer(int timeout)
430 #ifdef TIZEN_FEATURE_BT_DPM
431 int discoverable_state = DPM_BT_ERROR;
434 __bt_visibility_alarm_remove();
436 visible_timer.timeout = timeout;
438 #ifdef TIZEN_FEATURE_BT_DPM
439 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
440 if (discoverable_state != DPM_RESTRICTED) {
442 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
443 BT_ERR("Set vconf failed");
444 #ifdef TIZEN_FEATURE_BT_DPM
449 return BLUETOOTH_ERROR_NONE;
451 if (!visible_timer.alarm_init) {
452 /* Set Alarm timer to switch off BT */
453 result = alarmmgr_init("bt-service");
455 return BLUETOOTH_ERROR_INTERNAL;
457 visible_timer.alarm_init = TRUE;
460 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
462 return BLUETOOTH_ERROR_INTERNAL;
464 if (!TIZEN_PROFILE_WEARABLE) {
465 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
466 BT_ERR("Set vconf failed");
469 /* Take start time */
470 time(&(visible_timer.start_time));
471 visible_timer.event_id = g_timeout_add_seconds(1,
472 __bt_timeout_handler, NULL);
474 visible_timer.timeout = timeout;
476 __bt_visibility_alarm_create();
478 return BLUETOOTH_ERROR_NONE;
481 int _bt_stop_visibility_timer(void)
483 __bt_visibility_alarm_remove();
485 visible_timer.timeout = 0;
487 if (!TIZEN_PROFILE_WEARABLE) {
488 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
489 BT_ERR("Set vconf failed");
492 return BLUETOOTH_ERROR_NONE;
495 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
498 int ret = BLUETOOTH_ERROR_NONE;;
499 #ifdef TIZEN_FEATURE_BT_DPM
500 int discoverable_state = DPM_BT_ERROR;
505 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
507 #ifdef TIZEN_FEATURE_BT_DPM
508 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
509 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
510 discoverable_state == DPM_RESTRICTED) {
511 if (headed_plugin_info->plugin_headed_enabled)
512 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
513 return BLUETOOTH_ERROR_ACCESS_DENIED;
515 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE &&
516 discoverable_state == DPM_RESTRICTED) {
517 if (headed_plugin_info->plugin_headed_enabled)
518 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
519 return BLUETOOTH_ERROR_ACCESS_DENIED;
523 switch (discoverable_mode) {
524 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
525 result = adapter_set_connectable(TRUE);
528 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
529 result = adapter_set_discoverable();
532 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
533 result = adapter_set_discoverable();
536 return BLUETOOTH_ERROR_INVALID_PARAM;
539 if (result != OAL_STATUS_SUCCESS) {
540 BT_ERR("set scan mode failed %d", result);
541 return BLUETOOTH_ERROR_INTERNAL;
544 result = adapter_set_discoverable_timeout(timeout);
545 if (result != OAL_STATUS_SUCCESS) {
546 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
547 return BLUETOOTH_ERROR_INTERNAL;
550 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
551 ret = _bt_stop_visibility_timer();
552 if (BLUETOOTH_ERROR_NONE != ret)
553 BT_ERR("_bt_stop_visibility_timer failed");
554 if (!TIZEN_PROFILE_WEARABLE) {
555 if (vconf_set_int(BT_FILE_VISIBLE_TIME, -1) != 0)
556 BT_ERR("Set vconf failed");
558 } else if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE) {
559 ret = _bt_start_visibility_timer(timeout);
560 if (BLUETOOTH_ERROR_NONE != ret)
561 BT_ERR("_bt_start_visibility_timer failed");
564 ret = _bt_stop_visibility_timer();
565 if (BLUETOOTH_ERROR_NONE != ret)
566 BT_ERR("_bt_stop_visibility_timer failed");
573 gboolean _bt_is_connectable(void)
580 adapter_is_connectable(&connectable);
586 BT_DBG("Connectable: [%s]", result ? "TRUE" : "FALSE");
591 int _bt_set_connectable(gboolean connectable)
593 int result = BLUETOOTH_ERROR_NONE;
596 result = adapter_set_connectable(connectable);
597 if (result != OAL_STATUS_SUCCESS) {
598 BT_ERR("adapter_set_connectable failed: %d", result);
599 result = BLUETOOTH_ERROR_INTERNAL;
601 result = BLUETOOTH_ERROR_NONE;
607 int _bt_is_service_used(void)
613 result = adapter_get_service_uuids();
614 if (result != OAL_STATUS_SUCCESS) {
615 BT_ERR("adapter_get_service_uuids failed: %d", result);
616 result = BLUETOOTH_ERROR_INTERNAL;
618 result = BLUETOOTH_ERROR_NONE;
625 int _bt_adapter_get_bonded_devices(void)
627 int result = BLUETOOTH_ERROR_NONE;
630 result = adapter_get_bonded_devices();
631 if (result != OAL_STATUS_SUCCESS) {
632 BT_ERR("adapter_get_bonded_devices failed: %d", result);
633 result = BLUETOOTH_ERROR_INTERNAL;
635 result = BLUETOOTH_ERROR_NONE;
641 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
644 GDBusConnection *conn;
645 GDBusProxy *manager_proxy;
646 GVariant *result = NULL;
647 GVariant *result1 = NULL;
648 GVariantIter *iter = NULL;
649 GError *error = NULL;
650 char *object_path = NULL;
651 GVariantIter *interface_iter;
652 char *interface_str = NULL;
653 GDBusProxy *device_proxy = NULL;
654 gboolean is_connected = FALSE;
656 conn = _bt_gdbus_get_system_gconn();
657 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
659 manager_proxy = _bt_get_manager_proxy();
660 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
662 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
664 G_DBUS_CALL_FLAGS_NONE,
671 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
672 g_clear_error(&error);
675 BT_ERR("Failed to Failed to GetManagedObjects");
676 return BLUETOOTH_ERROR_INTERNAL;
679 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
680 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
682 /* Parse the signature: oa{sa{sv}}} */
683 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
684 if (object_path == NULL)
687 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
688 &interface_str, NULL)) {
689 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
690 BT_DBG("Found a device: %s", object_path);
691 g_free(interface_str);
693 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
695 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
697 if (device_proxy == NULL) {
698 BT_DBG("Device don't have this service");
702 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
703 g_variant_new("(s)", profile_uuid),
704 G_DBUS_CALL_FLAGS_NONE,
709 if (result1 == NULL) {
710 BT_ERR("Error occured in Proxy call");
712 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
716 g_object_unref(device_proxy);
719 g_variant_get(result1, "(b)", &is_connected);
721 if (is_connected == TRUE) {
722 char address[BT_ADDRESS_STRING_SIZE];
723 bluetooth_device_address_t *addr = NULL;
725 _bt_convert_device_path_to_address(object_path, address);
727 addr = g_malloc0(sizeof(bluetooth_device_address_t));
728 _bt_convert_addr_string_to_type(addr->addr, address);
730 g_array_append_vals(*addr_list, addr,
731 sizeof(bluetooth_device_address_t));
734 g_variant_unref(result1);
735 g_object_unref(device_proxy);
742 g_variant_unref(result);
743 g_variant_iter_free(iter);
746 return BLUETOOTH_ERROR_NONE;
749 static void __bt_handle_pending_a2dp_init(service_uuid_t *service_list, unsigned int count)
754 char uuid_str[BT_UUID_STRING_SIZE];
756 if (!a2dp_init_pending)
760 a2dp_init_pending = FALSE;
761 for (i = 0; i < count; i++) {
762 uuid = service_list[i].uuid;
763 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
764 BT_INFO("Adapter Service: [%s]", uuid_str);
765 if (!strcasecmp(uuid_str, A2DP_SINK_UUID)) {
766 BT_INFO("Enable A2DP Sink role");
767 /* Initialize A2DP Sink */
768 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
769 if (ret != BLUETOOTH_ERROR_NONE)
770 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
772 /* Initialize AVRCP Controller */
773 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
774 if (ret != BLUETOOTH_ERROR_NONE)
775 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
777 _bt_audio_set_current_role(BLUETOOTH_A2DP_SINK);
782 BT_INFO("Enable A2DP Source role by default");
783 /* Initialize A2DP Source */
784 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
785 if (ret != BLUETOOTH_ERROR_NONE)
786 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
788 /* Initialize AVRCP Target */
789 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
790 if (ret != BLUETOOTH_ERROR_NONE)
791 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
793 _bt_audio_set_current_role(BLUETOOTH_A2DP_SOURCE);
797 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
799 int result = BLUETOOTH_ERROR_NONE;
803 switch (event_type) {
804 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
805 case OAL_EVENT_OAL_INITIALISED_FAILED:
806 __bt_handle_oal_initialisation(event_type);
808 case OAL_EVENT_ADAPTER_ENABLED:
809 __bt_adapter_state_change_callback(BT_ACTIVATED);
811 case OAL_EVENT_ADAPTER_DISABLED:
812 __bt_adapter_state_change_callback(BT_DEACTIVATED);
814 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
815 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
817 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
818 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
820 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
821 bt_address_t *bd_addr = event_data;
822 bluetooth_device_address_t local_address;
825 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
826 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
827 local_address.addr[0], local_address.addr[1], local_address.addr[2],
828 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
830 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
831 (void *) &local_address, sizeof(bluetooth_device_address_t));
834 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
835 char *name = event_data;
836 BT_DBG("Adapter Name: %s", name);
838 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
839 bluetooth_device_name_t local_name;
841 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
842 g_strlcpy(local_name.name,
843 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
844 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
845 (void *) &local_name, sizeof(bluetooth_device_name_t));
847 /* Send event to application */
848 _bt_send_event(BT_ADAPTER_EVENT,
849 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
850 g_variant_new("(is)", result, name));
854 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
855 char *ver = event_data;
856 bluetooth_version_t local_version;
858 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
859 g_strlcpy(local_version.version,
860 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
861 BT_DBG("BT Version: %s", local_version.version);
863 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
864 (void *) &local_version, sizeof(bluetooth_version_t));
867 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
869 gboolean connectable = FALSE;
871 BT_INFO("Adapter discoverable mode:"
872 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
873 _bt_send_event(BT_ADAPTER_EVENT,
874 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
875 g_variant_new("(b)", connectable));
877 _bt_send_event(BT_ADAPTER_EVENT,
878 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
879 g_variant_new("(in)", result, mode));
882 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
884 gboolean connectable = TRUE;
886 BT_INFO("Adapter discoverable mode:"
887 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
888 _bt_send_event(BT_ADAPTER_EVENT,
889 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
890 g_variant_new("(b)", connectable));
892 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
893 _bt_send_event(BT_ADAPTER_EVENT,
894 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
895 g_variant_new("(in)", result, mode));
898 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
901 BT_INFO("Adapter discoverable mode:"
902 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
904 /* Send event to application */
905 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
906 _bt_send_event(BT_ADAPTER_EVENT,
907 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
908 g_variant_new("(in)", result, mode));
912 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
913 int *timeout = event_data;
916 BT_INFO("Discoverable timeout: [%d]", *timeout);
918 /* Send event to application */
919 _bt_get_discoverable_mode(&mode);
920 _bt_send_event(BT_ADAPTER_EVENT,
921 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
922 g_variant_new("(in)", result, mode));
925 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
927 service_uuid_t *service_list;
928 event_adapter_services_t *list = event_data;
931 service_list = list->service_list;
932 __bt_handle_pending_a2dp_init(service_list, count);
933 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
936 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
939 bluetooth_device_address_t *addr_list;
941 event_device_list_t *bonded_device_list = event_data;
942 count = bonded_device_list->num;
944 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
945 for (i = 0; i < count; i++) {
946 memcpy(addr_list[i].addr,
947 bonded_device_list->devices[i].addr,
948 BLUETOOTH_ADDRESS_LENGTH);
951 BT_INFO("Adapter Bonded device List count: [%d]", count);
952 _bt_device_handle_paired_address_list(addr_list, bonded_device_list->num);
957 BT_ERR("Unhandled event..");
964 int _bt_init_profiles()
969 /*TODO: Init bluetooth profiles */
970 ret = _bt_hidhost_initialize();
971 if (ret != BLUETOOTH_ERROR_NONE)
972 BT_ERR("_bt_hidhost_initialize Failed");
974 ret = _bt_socket_init();
975 if (ret != BLUETOOTH_ERROR_NONE)
976 BT_ERR("_bt_socket_init Failed");
979 * Query local adapter services and based on a2dp service uuids initialized
980 * in bluetooth stack, enable A2DP sourec or A2DP sink role.
982 ret = adapter_get_service_uuids();
983 if (ret != OAL_STATUS_SUCCESS)
984 BT_ERR("adapter_get_service_uuids failed: %d", ret);
986 a2dp_init_pending = TRUE;
989 /* Initialize HFP Audio Gateway */
990 ret = _bt_audio_initialize(BT_AG_MODULE);
991 if (ret != BLUETOOTH_ERROR_NONE)
992 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
994 /* Registering callback for receiving audio services searched */
995 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
996 if (ret != BLUETOOTH_ERROR_NONE)
997 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
999 ret = _bt_hdp_init();
1000 if (ret != BLUETOOTH_ERROR_NONE)
1001 BT_ERR("_bt_hdp_init Failed");
1003 ret = _bt_gatt_init();
1004 if (ret != BLUETOOTH_ERROR_NONE)
1005 BT_ERR("_bt_gatt_init Failed");
1007 return BLUETOOTH_ERROR_NONE;
1010 int _bt_cleanup_profiles(void)
1012 /* TODO: Cleanup bluetooth profiles */
1013 _bt_hidhost_deinitialize();
1014 _bt_socket_deinit();
1016 /* TODO: Cleanup bluetooth audio profiles */
1017 //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
1018 //_bt_audio_deinitialize(BT_AVRCP_MODULE);
1019 //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
1020 //_bt_audio_deinitialize(BT_AG_MODULE);
1021 //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
1022 //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
1027 return BLUETOOTH_ERROR_NONE;
1030 /* OAL post initialization handler */
1031 static void __bt_post_oal_init(void)
1034 int status = VCONFKEY_BT_STATUS_OFF;
1036 BT_DBG("OAL initialized");
1037 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0)
1038 BT_ERR("Fail to get the enabled value");
1041 /* Update Bluetooth Status to OFF */
1042 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1043 BT_ERR("Set vconf failed\n");
1046 if (status & VCONFKEY_BT_STATUS_ON) {
1047 ret = _bt_enable_adapter();
1048 if (ret != BLUETOOTH_ERROR_NONE)
1049 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
1053 /* OAL initialization handler */
1054 static void __bt_handle_oal_initialisation(oal_event_t event)
1059 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1060 __bt_post_oal_init();
1062 case OAL_EVENT_OAL_INITIALISED_FAILED:
1063 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1064 g_idle_add(_bt_terminate_service, NULL);
1067 BT_ERR("Unknown Event");
1072 static gboolean __bt_is_service_request_present(int service_function)
1075 invocation_info_t *req_info;
1079 /* Get method invocation context */
1080 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1082 if (req_info && req_info->service_function == service_function)
1090 /* Internal functions of core adapter service */
1091 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1095 invocation_info_t *req_info;
1098 /* Get method invocation context */
1099 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1101 if (req_info == NULL || req_info->service_function != service_function)
1104 /* Create out param */
1105 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1107 switch (service_function) {
1108 case BT_ENABLE_ADAPTER:
1109 case BT_DISABLE_ADAPTER: {
1110 gboolean done = TRUE;
1111 g_array_append_vals(out_param, &done, sizeof(gboolean));
1114 case BT_GET_LOCAL_NAME:
1115 case BT_GET_LOCAL_ADDRESS:
1116 case BT_GET_LOCAL_VERSION:
1117 g_array_append_vals(out_param, user_data, size);
1119 case BT_IS_SERVICE_USED: {
1121 gboolean used = FALSE;
1122 unsigned char *uuid;
1123 char uuid_str[BT_UUID_STRING_SIZE];
1124 char *request_uuid = req_info->user_data;
1125 service_uuid_t *service_list = user_data;
1127 BT_INFO("Check for service uuid: %s", request_uuid);
1128 for (i = 0; i < size; i++) {
1129 uuid = service_list[i].uuid;
1130 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1131 BT_INFO("Adapter Service: [%s]", uuid_str);
1132 if (strcasecmp(uuid_str, request_uuid) == 0) {
1133 BT_INFO("UUID matched!!");
1139 g_array_append_vals(out_param, &used, sizeof(gboolean));
1143 BT_ERR("Unknown service function[%d]", service_function);
1146 _bt_service_method_return(req_info->context, out_param, req_info->result);
1147 g_array_free(out_param, TRUE);
1148 /* Now free invocation info for this request*/
1149 _bt_free_info_from_invocation_list(req_info);
1153 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1155 char *phone_name = NULL;
1161 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1162 phone_name = vconf_keynode_get_str(node);
1164 if (phone_name && strlen(phone_name) != 0) {
1165 if (!g_utf8_validate(phone_name, -1,
1166 (const char **)&ptr))
1169 _bt_set_local_name(phone_name);
1174 /* Request return handlings */
1175 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1177 BT_INFO("__bt_adapter_post_set_enabled>>");
1179 if (TIZEN_PROFILE_TV || !headed_plugin_info->plugin_headed_enabled) {
1180 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1181 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1182 BT_ERR("Fail to set discoverable mode");
1184 __bt_set_visible_mode();
1187 /* add the vconf noti handler */
1188 if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1189 (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1190 BT_ERR("DEVICE_NAME key changed notification registration failed");
1192 __bt_set_local_name();
1194 /* Get All properties */
1195 if (OAL_STATUS_SUCCESS != adapter_get_properties())
1196 BT_ERR("adapter_get_properties failed");
1198 /* Add Adapter enabled post processing codes */
1202 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1204 BT_INFO("_bt_adapter_post_set_disabled>>");
1206 if (!TIZEN_PROFILE_TV) {
1207 /* Add Adapter disabled post processing codes */
1208 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1209 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1210 BT_ERR("vconf_ignore_key_changed failed");
1213 /* bt-service should be terminated when BT is off */
1214 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1215 /* TODO: Implement to check if it is the recovery mode or not */
1216 _bt_reliable_terminate_service(NULL);
1222 static void __bt_adapter_update_bt_enabled(void)
1224 int result = BLUETOOTH_ERROR_NONE;
1226 BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1227 if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1228 BT_ERR("Bluetooth profile init failed");
1230 _bt_device_handle_adapter_state(TRUE);
1232 /* Update Bluetooth Status to notify other modules */
1233 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1234 BT_ERR("Set vconf failed\n");
1236 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1237 BT_ERR("Set vconf failed\n");
1239 /* TODO:Add timer function to handle any further post processing */
1240 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1242 /*Return BT_ADAPTER_ENABLE Method invocation context */
1243 __bt_adapter_handle_pending_requests(BT_ENABLE_ADAPTER, NULL, 0);
1244 /*Send BT Enabled event to application */
1245 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1246 g_variant_new("(i)", result));
1249 static void __bt_adapter_update_bt_disabled(void)
1251 int result = BLUETOOTH_ERROR_NONE;
1252 int power_off_status = 0;
1255 BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1256 _bt_cleanup_profiles();
1258 _bt_device_handle_adapter_state(FALSE);
1260 /* Update the vconf BT status in normal Deactivation case only */
1261 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1262 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1264 /* Update Bluetooth Status to notify other modules */
1265 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1266 BT_ERR("Set vconf failed");
1268 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1269 BT_ERR("Set vconf failed\n");
1271 /* TODO:Add timer function to handle any further post processing */
1272 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1274 /* Return BT_ADAPTER_DISABLE Method invocation context */
1275 __bt_adapter_handle_pending_requests(BT_DISABLE_ADAPTER, NULL, 0);
1277 /* Send BT Disabled event to application */
1278 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1279 g_variant_new("(i)", result));
1282 static void __bt_adapter_state_set_status(bt_status_t status)
1284 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1285 adapter_state = status;
1288 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1290 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1291 adapter_discovery_state = status;
1294 static void __bt_adapter_state_change_callback(int bt_status)
1296 BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
1298 switch (bt_status) {
1299 case BT_DEACTIVATED:
1300 __bt_adapter_state_set_status(bt_status);
1302 /* Adapter is disabled, unregister event handlers */
1303 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1304 //_bt_deinit_device_event_handler();
1306 /* Add Adapter disabled post processing codes */
1307 __bt_adapter_update_bt_disabled();
1310 __bt_adapter_state_set_status(bt_status);
1311 /* Add Adapter enabled post processing codes */
1313 BT_DBG("g_source is removed");
1314 g_source_remove(timer_id);
1317 __bt_adapter_update_bt_enabled();
1320 BT_ERR("Incorrect Bluetooth adapter state changed status");
1325 int set_adapter_request_state(int enable)
1327 return oal_set_adapter_request_state(enable);
1330 int set_le_request_state(int enable)
1332 return oal_set_le_request_state(enable);
1335 static int __bt_adapter_state_handle_request(gboolean enable)
1337 int result = BLUETOOTH_ERROR_NONE;
1340 switch (adapter_state) {
1341 case BT_ACTIVATING: {
1342 BT_INFO("Adapter is currently in activating state, state [%d]",
1345 return BLUETOOTH_ERROR_IN_PROGRESS;
1347 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1348 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1349 /*TODO Stop Discovery*/
1350 __bt_adapter_update_discovery_status(FALSE);
1352 result = adapter_disable();
1353 if (result != OAL_STATUS_SUCCESS) {
1354 BT_ERR("adapter_enable failed: [%d]", result);
1355 result = BLUETOOTH_ERROR_INTERNAL;
1356 /*TODO: perform if anything more needs to be done to handle failure */
1358 /* TODO: To be handled */
1359 __bt_adapter_state_set_status(BT_DEACTIVATING);
1360 result = BLUETOOTH_ERROR_NONE;
1365 case BT_ACTIVATED: {
1366 BT_INFO("Adapter is currently in activated state, state [%d]",
1369 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1371 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1372 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1373 /*TODO Stop Discovery*/
1374 __bt_adapter_update_discovery_status(FALSE);
1376 result = adapter_disable();
1377 if (result != OAL_STATUS_SUCCESS) {
1378 BT_ERR("adapter_enable failed: [%d]", result);
1379 result = BLUETOOTH_ERROR_INTERNAL;
1380 /*TODO: perform if anything more needs to be done to handle failure */
1382 /* TODO: To be handled */
1383 __bt_adapter_state_set_status(BT_DEACTIVATING);
1384 result = BLUETOOTH_ERROR_NONE;
1389 case BT_DEACTIVATING: {
1390 BT_INFO("Adapter is currently in deactivating state, state [%d]",
1393 return BLUETOOTH_ERROR_IN_PROGRESS;
1396 result = adapter_enable();
1397 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1398 BT_ERR("adapter_enable failed: [%d]", result);
1400 result = BLUETOOTH_ERROR_INTERNAL;
1401 /*TODO: perform if anything more needs to be done to handle failure */
1403 /* TODO: To be handled */
1404 __bt_adapter_state_set_status(BT_ACTIVATING);
1405 result = BLUETOOTH_ERROR_NONE;
1410 case BT_DEACTIVATED: {
1411 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1414 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1416 result = adapter_enable();
1417 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1418 BT_ERR("adapter_enable failed: [%d]", result);
1420 result = BLUETOOTH_ERROR_INTERNAL;
1421 /*TODO: perform if anything more needs to be done to handle failure */
1423 /* TODO: To be handled */
1424 __bt_adapter_state_set_status(BT_ACTIVATING);
1425 result = BLUETOOTH_ERROR_NONE;
1431 BT_ERR("Unknown state: %d", adapter_state);
1435 if (enable && result == BLUETOOTH_ERROR_NONE) {
1436 /* Adapter enable request is successful, setup event handlers */
1437 _bt_service_register_event_handler_callback(
1438 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1439 _bt_device_state_handle_callback_set_request();
1444 static int __bt_adapter_state_discovery_request(gboolean enable,
1445 unsigned short max_response, unsigned short duration,
1446 unsigned int mask, gboolean is_custom,
1447 bt_discovery_role_type_t role)
1449 int result = BLUETOOTH_ERROR_NONE;
1452 switch (adapter_discovery_state) {
1453 case ADAPTER_DISCOVERY_STARTED: {
1454 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1455 adapter_discovery_state);
1457 return BLUETOOTH_ERROR_IN_PROGRESS;
1459 result = adapter_stop_inquiry();
1460 if (result != OAL_STATUS_SUCCESS) {
1461 BT_ERR("Discover stop failed: %d", result);
1462 result = BLUETOOTH_ERROR_INTERNAL;
1464 BT_ERR("Stop Discovery Triggered successfully");
1465 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1466 result = BLUETOOTH_ERROR_NONE;
1471 case ADAPTER_DISCOVERY_STARTING: {
1472 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1473 adapter_discovery_state);
1475 return BLUETOOTH_ERROR_IN_PROGRESS;
1477 result = adapter_stop_inquiry();
1478 if (result != OAL_STATUS_SUCCESS) {
1479 BT_ERR("Discover stop failed: %d", result);
1480 result = BLUETOOTH_ERROR_INTERNAL;
1482 BT_ERR("Stop Discovery Triggered successfully");
1483 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1484 result = BLUETOOTH_ERROR_NONE;
1489 case ADAPTER_DISCOVERY_STOPPED: {
1490 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1491 adapter_discovery_state);
1493 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1495 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1498 result = adapter_start_inquiry(duration);
1500 result = adapter_start_custom_inquiry(role);
1502 if (result != OAL_STATUS_SUCCESS) {
1503 BT_ERR("Start Discovery failed: %d", result);
1504 result = BLUETOOTH_ERROR_INTERNAL;
1506 BT_ERR("Start Discovery Triggered successfully");
1507 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1508 result = BLUETOOTH_ERROR_NONE;
1513 case ADAPTER_DISCOVERY_STOPPING: {
1514 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1515 adapter_discovery_state);
1517 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1519 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1521 result = adapter_start_inquiry(duration);
1523 result = adapter_start_custom_inquiry(role);
1524 if (result != OAL_STATUS_SUCCESS) {
1525 BT_ERR("Start Discovery failed: %d", result);
1526 result = BLUETOOTH_ERROR_INTERNAL;
1528 BT_ERR("Start Discovery Triggered successfully");
1529 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1530 result = BLUETOOTH_ERROR_NONE;
1536 BT_ERR("Unknown state: %d", adapter_discovery_state);
1544 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1546 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1547 GVariant *param = NULL;
1548 int result = BLUETOOTH_ERROR_NONE;
1550 switch (bt_discovery_status) {
1551 case ADAPTER_DISCOVERY_STOPPED:
1553 __bt_adapter_update_discovery_status(bt_discovery_status);
1554 param = g_variant_new("(i)", result);
1555 _bt_send_event(BT_ADAPTER_EVENT,
1556 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1560 case ADAPTER_DISCOVERY_STARTED:
1562 __bt_adapter_update_discovery_status(bt_discovery_status);
1563 param = g_variant_new("(i)", result);
1564 _bt_send_event(BT_ADAPTER_EVENT,
1565 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1570 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1574 static void __bt_set_visible_mode(void)
1577 #ifdef TIZEN_FEATURE_BT_DPM
1578 int discoverable_state = DPM_BT_ERROR;
1581 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1582 BT_ERR("Fail to get the timeout value");
1584 #ifdef TIZEN_FEATURE_BT_DPM
1585 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1586 if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
1587 if (_bt_set_discoverable_mode(
1588 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1589 timeout) != BLUETOOTH_ERROR_NONE) {
1590 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1591 BT_ERR("Set vconf failed");
1594 if (_bt_set_discoverable_mode(
1595 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1596 timeout) != BLUETOOTH_ERROR_NONE) {
1597 BT_ERR("Set connectable mode failed");
1601 if (timeout == -1) {
1602 if (_bt_set_discoverable_mode(
1603 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1604 timeout) != BLUETOOTH_ERROR_NONE) {
1605 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1606 BT_ERR("Set vconf failed");
1609 if (_bt_set_discoverable_mode(
1610 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1611 timeout) != BLUETOOTH_ERROR_NONE) {
1612 BT_ERR("Set connectable mode failed");
1618 static void __bt_set_local_name(void)
1620 char *phone_name = NULL;
1623 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1628 if (strlen(phone_name) != 0) {
1629 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1632 _bt_set_local_name(phone_name);
1637 void _bt_adapter_set_status(bt_status_t status)
1639 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1640 adapter_state = status;
1643 bt_status_t _bt_adapter_get_status(void)
1645 return adapter_state;
1648 void _bt_set_disabled(int result)
1650 int power_off_status = 0;
1653 int pm_ignore_mode = 0;
1655 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1656 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1658 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1660 /* Update the vconf BT status in normal Deactivation case only */
1661 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1662 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1664 BT_DBG("Update vconf for BT normal Deactivation");
1666 if (result == BLUETOOTH_ERROR_TIMEOUT)
1667 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1668 BT_ERR("Set vconf failed");
1670 /* Update Bluetooth Status to notify other modules */
1671 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1672 BT_ERR("Set vconf failed");
1674 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1675 EVT_VAL_BT_OFF) != ES_R_OK)
1676 BT_ERR("Fail to set value");
1679 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1680 BT_ERR("Set vconf failed\n");
1682 _bt_adapter_set_status(BT_DEACTIVATED);
1683 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1685 BT_INFO("Adapter disabled");