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);
118 ret = oal_bt_init(_bt_service_oal_event_receiver);
120 if (OAL_STATUS_PENDING == ret) {
121 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
122 return BLUETOOTH_ERROR_NONE;
123 } else if (OAL_STATUS_SUCCESS != ret) {
124 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
125 return BLUETOOTH_ERROR_INTERNAL;
128 return BLUETOOTH_ERROR_NONE;
131 int _bt_enable_adapter(void)
133 return __bt_adapter_state_handle_request(TRUE);
136 int _bt_enable_core(void)
138 /* TODO_40 : 4.0 merge */
139 return BLUETOOTH_ERROR_NOT_SUPPORT;
142 int _bt_recover_adapter(void)
144 /* TODO_40 : 4.0 merge */
145 return BLUETOOTH_ERROR_NOT_SUPPORT;
148 int _bt_disable_adapter(void)
150 return __bt_adapter_state_handle_request(FALSE);
153 int _bt_start_discovery(unsigned short max_response,
154 unsigned short duration, unsigned int cod_mask)
156 return __bt_adapter_state_discovery_request(TRUE, max_response, duration,
157 cod_mask, FALSE, 0x00);
160 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
162 return __bt_adapter_state_discovery_request(TRUE, 0, 0, 0, TRUE, role);
165 int _bt_cancel_discovery(void)
167 return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0, FALSE, 0x00);
170 gboolean _bt_is_discovering(void)
172 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
173 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
179 int _bt_get_local_address(void)
185 result = adapter_get_address();
186 if (result != OAL_STATUS_SUCCESS) {
187 BT_ERR("adapter_get_address failed: %d", result);
188 result = BLUETOOTH_ERROR_INTERNAL;
190 result = BLUETOOTH_ERROR_NONE;
196 int _bt_get_local_version(void)
201 result = adapter_get_version();
202 if (result != OAL_STATUS_SUCCESS) {
203 BT_ERR("adapter_get_version failed: %d", result);
204 result = BLUETOOTH_ERROR_INTERNAL;
206 result = BLUETOOTH_ERROR_NONE;
212 int _bt_get_local_name(void)
218 result = adapter_get_name();
219 if (result != OAL_STATUS_SUCCESS) {
220 BT_ERR("adapter_get_name failed: %d", result);
221 result = BLUETOOTH_ERROR_INTERNAL;
223 result = BLUETOOTH_ERROR_NONE;
229 int _bt_set_local_name(char *local_name)
231 int result = BLUETOOTH_ERROR_NONE;
234 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
236 result = adapter_set_name(local_name);
237 if (result != OAL_STATUS_SUCCESS) {
238 BT_ERR("adapter_set_name failed: %d", result);
239 result = BLUETOOTH_ERROR_INTERNAL;
241 result = BLUETOOTH_ERROR_NONE;
247 int _bt_get_discoverable_mode(int *mode)
254 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
256 adapter_is_discoverable(&scan_mode);
257 if (TRUE == scan_mode) {
258 adapter_get_discoverable_timeout(&timeout);
260 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
262 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
264 adapter_is_connectable(&scan_mode);
265 if(scan_mode == TRUE)
266 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
269 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
270 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
271 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
278 return BLUETOOTH_ERROR_NONE;
281 int _bt_get_timeout_value(int *timeout)
286 /* Take current time */
288 time_diff = difftime(current_time, visible_timer.start_time);
290 BT_DBG("Time diff = %d\n", time_diff);
291 *timeout = visible_timer.timeout - time_diff;
293 return BLUETOOTH_ERROR_NONE;
296 static void __bt_visibility_alarm_remove()
298 if (visible_timer.event_id > 0) {
299 g_source_remove(visible_timer.event_id);
300 visible_timer.event_id = 0;
303 if (visible_timer.alarm_id > 0) {
304 alarmmgr_remove_alarm(visible_timer.alarm_id);
305 visible_timer.alarm_id = 0;
309 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
311 int result = BLUETOOTH_ERROR_NONE;
314 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
316 if (alarm_id != visible_timer.alarm_id)
319 if (visible_timer.event_id) {
320 _bt_send_event(BT_ADAPTER_EVENT,
321 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
322 g_variant_new("(in)", result, timeout));
323 g_source_remove(visible_timer.event_id);
324 visible_timer.event_id = 0;
325 visible_timer.timeout = 0;
327 if (!TIZEN_PROFILE_WEARABLE) {
328 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
329 BT_ERR("Set vconf failed\n");
332 /* Switch Off visibility in Bluez */
333 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
334 visible_timer.alarm_id = 0;
338 static gboolean __bt_timeout_handler(gpointer user_data)
340 int result = BLUETOOTH_ERROR_NONE;
344 /* Take current time */
346 time_diff = difftime(current_time, visible_timer.start_time);
348 /* Send event to application */
349 _bt_send_event(BT_ADAPTER_EVENT,
350 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
351 g_variant_new("(in)", result, time_diff));
353 if (visible_timer.timeout <= time_diff) {
354 g_source_remove(visible_timer.event_id);
355 visible_timer.event_id = 0;
356 visible_timer.timeout = 0;
358 if (!TIZEN_PROFILE_WEARABLE) {
359 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
360 BT_ERR("Set vconf failed\n");
369 static void __bt_visibility_alarm_create()
374 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
377 BT_ERR("Failed to create alarm error = %d\n", result);
379 BT_DBG("Alarm created = %d\n", alarm_id);
380 visible_timer.alarm_id = alarm_id;
384 static int __bt_set_visible_time(int timeout)
387 #ifdef TIZEN_FEATURE_BT_DPM
388 int discoverable_state = DPM_BT_ERROR;
391 __bt_visibility_alarm_remove();
393 visible_timer.timeout = timeout;
395 #ifdef TIZEN_FEATURE_BT_DPM
396 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
397 if (discoverable_state != DPM_RESTRICTED) {
399 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
400 BT_ERR("Set vconf failed");
401 #ifdef TIZEN_FEATURE_BT_DPM
406 return BLUETOOTH_ERROR_NONE;
408 if (!visible_timer.alarm_init) {
409 /* Set Alarm timer to switch off BT */
410 result = alarmmgr_init("bt-service");
412 return BLUETOOTH_ERROR_INTERNAL;
414 visible_timer.alarm_init = TRUE;
417 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
419 return BLUETOOTH_ERROR_INTERNAL;
421 /* Take start time */
422 time(&(visible_timer.start_time));
423 visible_timer.event_id = g_timeout_add_seconds(1,
424 __bt_timeout_handler, NULL);
426 __bt_visibility_alarm_create();
428 return BLUETOOTH_ERROR_NONE;
431 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
434 #ifdef TIZEN_FEATURE_BT_DPM
435 int discoverable_state = DPM_BT_ERROR;
440 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
442 #ifdef TIZEN_FEATURE_BT_DPM
443 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
444 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
445 discoverable_state == DPM_RESTRICTED) {
446 if (headed_plugin_info->plugin_headed_enabled)
447 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
448 return BLUETOOTH_ERROR_ACCESS_DENIED;
450 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
451 discoverable_state == DPM_RESTRICTED) {
452 if (headed_plugin_info->plugin_headed_enabled)
453 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
454 return BLUETOOTH_ERROR_ACCESS_DENIED;
458 switch (discoverable_mode) {
459 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
460 result = adapter_set_connectable(TRUE);
463 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
464 result = adapter_set_discoverable();
467 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
468 result = adapter_set_discoverable();
471 return BLUETOOTH_ERROR_INVALID_PARAM;
474 if (result != OAL_STATUS_SUCCESS) {
475 BT_ERR("set scan mode failed %d", result);
476 return BLUETOOTH_ERROR_INTERNAL;
479 result = adapter_set_discoverable_timeout(timeout);
480 if (result != OAL_STATUS_SUCCESS) {
481 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
482 return BLUETOOTH_ERROR_INTERNAL;
485 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
488 result = __bt_set_visible_time(timeout);
494 gboolean _bt_is_connectable(void)
501 adapter_is_connectable(&connectable);
507 BT_DBG("Connectable: [%s]", result ? "TRUE":"FALSE");
512 int _bt_set_connectable(gboolean connectable)
514 int result = BLUETOOTH_ERROR_NONE;
517 result = adapter_set_connectable(connectable);
518 if (result != OAL_STATUS_SUCCESS) {
519 BT_ERR("adapter_set_connectable failed: %d", result);
520 result = BLUETOOTH_ERROR_INTERNAL;
522 result = BLUETOOTH_ERROR_NONE;
528 int _bt_is_service_used(void)
534 result = adapter_get_service_uuids();
535 if (result != OAL_STATUS_SUCCESS) {
536 BT_ERR("adapter_get_service_uuids failed: %d", result);
537 result = BLUETOOTH_ERROR_INTERNAL;
539 result = BLUETOOTH_ERROR_NONE;
546 int _bt_adapter_get_bonded_devices(void)
548 int result = BLUETOOTH_ERROR_NONE;
551 result = adapter_get_bonded_devices();
552 if (result != OAL_STATUS_SUCCESS) {
553 BT_ERR("adapter_get_bonded_devices failed: %d", result);
554 result = BLUETOOTH_ERROR_INTERNAL;
556 result = BLUETOOTH_ERROR_NONE;
562 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
565 GDBusConnection *conn;
566 GDBusProxy *manager_proxy;
567 GVariant *result = NULL;
568 GVariant *result1 = NULL;
569 GVariantIter *iter = NULL;
570 GError *error = NULL;
571 char *object_path = NULL;
572 GVariantIter *interface_iter;
573 char *interface_str = NULL;
574 GDBusProxy *device_proxy = NULL;
575 gboolean is_connected = FALSE;
577 conn = _bt_gdbus_get_system_gconn();
578 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
580 manager_proxy = _bt_get_manager_proxy();
581 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
583 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
585 G_DBUS_CALL_FLAGS_NONE,
592 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
593 g_clear_error(&error);
596 BT_ERR("Failed to Failed to GetManagedObjects");
597 return BLUETOOTH_ERROR_INTERNAL;
600 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
601 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
603 /* Parse the signature: oa{sa{sv}}} */
604 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
605 if (object_path == NULL)
608 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
609 &interface_str, NULL)) {
610 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
611 BT_DBG("Found a device: %s", object_path);
612 g_free(interface_str);
614 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
616 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
618 if (device_proxy == NULL) {
619 BT_DBG("Device don't have this service");
623 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
624 g_variant_new("(s)", profile_uuid),
625 G_DBUS_CALL_FLAGS_NONE,
630 if (result1 == NULL) {
631 BT_ERR("Error occured in Proxy call");
633 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
637 g_object_unref(device_proxy);
640 g_variant_get(result1, "(b)", &is_connected);
642 if (is_connected == TRUE) {
643 char address[BT_ADDRESS_STRING_SIZE];
644 bluetooth_device_address_t *addr = NULL;
646 _bt_convert_device_path_to_address(object_path, address);
648 addr = g_malloc0(sizeof(bluetooth_device_address_t));
649 _bt_convert_addr_string_to_type(addr->addr, address);
651 g_array_append_vals(*addr_list, addr,
652 sizeof(bluetooth_device_address_t));
655 g_variant_unref(result1);
656 g_object_unref(device_proxy);
663 g_variant_unref(result);
664 g_variant_iter_free(iter);
667 return BLUETOOTH_ERROR_NONE;
670 static void __bt_handle_pending_a2dp_init(service_uuid_t *service_list, unsigned int count)
675 char uuid_str[BT_UUID_STRING_SIZE];
677 if (!a2dp_init_pending)
681 a2dp_init_pending = FALSE;
682 for (i = 0; i < count; i++) {
683 uuid = service_list[i].uuid;
684 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
685 BT_INFO("Adapter Service: [%s]", uuid_str);
686 if (!strcasecmp(uuid_str, A2DP_SINK_UUID)) {
687 BT_INFO("Enable A2DP Sink role");
688 /* Initialize A2DP Sink */
689 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
690 if (ret != BLUETOOTH_ERROR_NONE)
691 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
693 /* Initialize AVRCP Controller */
694 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
695 if (ret != BLUETOOTH_ERROR_NONE)
696 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
698 _bt_audio_set_current_role(BLUETOOTH_A2DP_SINK);
703 BT_INFO("Enable A2DP Source role by default");
704 /* Initialize A2DP Source */
705 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
706 if (ret != BLUETOOTH_ERROR_NONE)
707 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
709 /* Initialize AVRCP Target */
710 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
711 if (ret != BLUETOOTH_ERROR_NONE)
712 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
714 _bt_audio_set_current_role(BLUETOOTH_A2DP_SOURCE);
718 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
720 int result = BLUETOOTH_ERROR_NONE;
725 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
726 case OAL_EVENT_OAL_INITIALISED_FAILED:
727 __bt_handle_oal_initialisation(event_type);
729 case OAL_EVENT_ADAPTER_ENABLED:
730 __bt_adapter_state_change_callback(BT_ACTIVATED);
732 case OAL_EVENT_ADAPTER_DISABLED:
733 __bt_adapter_state_change_callback(BT_DEACTIVATED);
735 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
736 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
738 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
739 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
741 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
742 bt_address_t *bd_addr = event_data;
743 bluetooth_device_address_t local_address;
746 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
747 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
748 local_address.addr[0], local_address.addr[1], local_address.addr[2],
749 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
751 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
752 (void *) &local_address, sizeof(bluetooth_device_address_t));
755 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
756 char *name = event_data;
757 BT_DBG("Adapter Name: %s", name);
759 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
760 bluetooth_device_name_t local_name;
762 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
763 g_strlcpy(local_name.name,
764 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
765 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
766 (void *) &local_name, sizeof(bluetooth_device_name_t));
768 /* Send event to application */
769 _bt_send_event(BT_ADAPTER_EVENT,
770 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
771 g_variant_new("(is)", result, name));
775 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
776 char *ver = event_data;
777 bluetooth_version_t local_version;
779 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
780 g_strlcpy(local_version.version,
781 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
782 BT_DBG("BT Version: %s", local_version.version);
784 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
785 (void *) &local_version, sizeof(bluetooth_version_t));
788 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
790 gboolean connectable = FALSE;
792 BT_INFO("Adapter discoverable mode:"
793 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
794 _bt_send_event(BT_ADAPTER_EVENT,
795 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
796 g_variant_new("(b)", connectable));
798 _bt_send_event(BT_ADAPTER_EVENT,
799 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
800 g_variant_new("(in)", result, mode));
803 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
805 gboolean connectable = TRUE;
807 BT_INFO("Adapter discoverable mode:"
808 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
809 _bt_send_event(BT_ADAPTER_EVENT,
810 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
811 g_variant_new("(b)", connectable));
813 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
814 _bt_send_event(BT_ADAPTER_EVENT,
815 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
816 g_variant_new("(in)", result, mode));
819 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
822 BT_INFO("Adapter discoverable mode:"
823 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
825 /* Send event to application */
826 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
827 _bt_send_event(BT_ADAPTER_EVENT,
828 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
829 g_variant_new("(in)", result, mode));
833 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
834 int *timeout = event_data;
837 BT_INFO("Discoverable timeout: [%d]", *timeout);
839 /* Send event to application */
840 _bt_get_discoverable_mode(&mode);
841 _bt_send_event(BT_ADAPTER_EVENT,
842 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
843 g_variant_new("(in)", result, mode));
846 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
848 service_uuid_t *service_list;
849 event_adapter_services_t *list = event_data;
852 service_list = list->service_list;
853 __bt_handle_pending_a2dp_init(service_list, count);
854 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
857 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
860 bluetooth_device_address_t *addr_list;
862 event_device_list_t *bonded_device_list = event_data;
863 count = bonded_device_list->num;
865 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
866 for (i = 0; i < count; i++) {
867 memcpy(addr_list[i].addr,
868 bonded_device_list->devices[i].addr,
869 BLUETOOTH_ADDRESS_LENGTH);
872 BT_INFO("Adapter Bonded device List count: [%d]", count);
873 __bt_adapter_handle_pending_requests(BT_GET_BONDED_DEVICES,
874 (void *)addr_list, bonded_device_list->num);
878 BT_ERR("Unhandled event..");
885 int _bt_init_profiles()
889 /*TODO: Init bluetooth profiles */
890 ret = _bt_hidhost_initialize();
891 if (ret != BLUETOOTH_ERROR_NONE) {
892 BT_ERR("_bt_hidhost_initialize Failed");
896 ret = _bt_socket_init();
897 if (ret != BLUETOOTH_ERROR_NONE) {
898 BT_ERR("_bt_socket_init Failed");
903 * Query local adapter services and based on a2dp service uuids initialized
904 * in bluetooth stack, enable A2DP sourec or A2DP sink role.
906 ret = adapter_get_service_uuids();
907 if (ret != OAL_STATUS_SUCCESS) {
908 BT_ERR("adapter_get_service_uuids failed: %d", ret);
909 return BLUETOOTH_ERROR_INTERNAL;
911 a2dp_init_pending = TRUE;
914 /* Initialize HFP Audio Gateway */
915 ret = _bt_audio_initialize(BT_AG_MODULE);
916 if (ret != BLUETOOTH_ERROR_NONE) {
917 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
920 /* Registering callback for receiving audio services searched */
921 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
922 if (ret != BLUETOOTH_ERROR_NONE) {
923 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
927 ret = _bt_hdp_init();
928 if (ret != BLUETOOTH_ERROR_NONE) {
929 BT_ERR("_bt_hdp_init Failed");
934 if (ret != BLUETOOTH_ERROR_NONE) {
935 BT_ERR("_bt_le_init Failed");
939 ret = _bt_gatt_init();
940 if (ret != BLUETOOTH_ERROR_NONE) {
941 BT_ERR("_bt_gatt_init Failed");
945 return BLUETOOTH_ERROR_NONE;
948 int _bt_cleanup_profiles(void)
950 /* TODO: Cleanup bluetooth profiles */
951 _bt_hidhost_deinitialize();
954 /* TODO: Cleanup bluetooth audio profiles */
955 //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
956 //_bt_audio_deinitialize(BT_AVRCP_MODULE);
957 //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
958 //_bt_audio_deinitialize(BT_AG_MODULE);
959 //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
960 //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
965 return BLUETOOTH_ERROR_NONE;
968 /* OAL post initialization handler */
969 static void __bt_post_oal_init(void)
972 int status = VCONFKEY_BT_STATUS_OFF;
974 BT_DBG("OAL initialized");
975 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0) {
976 BT_ERR("Fail to get the enabled value");
980 /* Update Bluetooth Status to OFF */
981 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
982 BT_ERR("Set vconf failed\n");
985 if (status & VCONFKEY_BT_STATUS_ON) {
986 ret = _bt_enable_adapter();
987 if (ret != BLUETOOTH_ERROR_NONE)
988 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
992 /* OAL initialization handler */
993 static void __bt_handle_oal_initialisation(oal_event_t event)
998 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
999 __bt_post_oal_init();
1001 case OAL_EVENT_OAL_INITIALISED_FAILED:
1002 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1003 g_idle_add(_bt_terminate_service, NULL);
1006 BT_ERR("Unknown Event");
1011 static gboolean __bt_is_service_request_present(int service_function)
1014 invocation_info_t *req_info;
1018 /* Get method invocation context */
1019 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1021 if (req_info && req_info->service_function == service_function)
1029 /* Internal functions of core adapter service */
1030 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1034 invocation_info_t *req_info;
1037 /* Get method invocation context */
1038 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1040 if (req_info == NULL || req_info->service_function != service_function)
1043 /* Create out param */
1044 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1046 switch(service_function) {
1047 case BT_ENABLE_ADAPTER:
1048 case BT_DISABLE_ADAPTER: {
1049 gboolean done = TRUE;
1050 g_array_append_vals(out_param, &done, sizeof(gboolean));
1053 case BT_GET_LOCAL_NAME:
1054 case BT_GET_LOCAL_ADDRESS:
1055 case BT_GET_LOCAL_VERSION:
1056 g_array_append_vals(out_param, user_data, size);
1058 case BT_IS_SERVICE_USED: {
1060 gboolean used = FALSE;
1061 unsigned char *uuid;
1062 char uuid_str[BT_UUID_STRING_SIZE];
1063 char *request_uuid = req_info->user_data;
1064 service_uuid_t *service_list = user_data;
1066 BT_INFO("Check for service uuid: %s", request_uuid);
1067 for (i = 0; i < size; i++) {
1068 uuid = service_list[i].uuid;
1069 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1070 BT_INFO("Adapter Service: [%s]", uuid_str);
1071 if (strcasecmp(uuid_str, request_uuid) == 0) {
1072 BT_INFO("UUID matched!!");
1078 g_array_append_vals(out_param, &used, sizeof(gboolean));
1081 case BT_GET_BONDED_DEVICES: {
1082 bluetooth_device_address_t *addr_list = user_data;
1083 bonded_devices_req_info_t *bonded_devices_req_info;
1084 char address[BT_ADDRESS_STRING_SIZE];
1086 int res = BLUETOOTH_ERROR_NONE;
1089 * BT_GET_BONDED_DEVICES is already processed for this request,
1090 * continue for next BT_GET_BONDED_DEVICES request if any
1092 if (NULL != req_info->user_data)
1095 BT_DBG("BT_GET_BONDED_DEVICES: count = [%d]", count);
1096 /* No bonded devices, return method invocation */
1097 if (0 == count || !addr_list)
1100 /* Save address list in user data for futur reference. */
1101 bonded_devices_req_info = g_malloc0(sizeof(bonded_devices_req_info));
1102 if (!bonded_devices_req_info) {
1103 BT_ERR("Memory allocation failed");
1104 req_info->result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1109 bonded_devices_req_info->count = count;
1110 bonded_devices_req_info->addr_list = addr_list;
1111 bonded_devices_req_info->out_param = out_param;
1112 req_info->user_data = bonded_devices_req_info;
1114 while (bonded_devices_req_info->count > 0) {
1115 bonded_devices_req_info->count -= 1;
1116 res = _bt_device_get_bonded_device_info(
1117 &addr_list[bonded_devices_req_info->count]);
1118 if (BLUETOOTH_ERROR_NONE == res)
1121 _bt_convert_addr_type_to_string((char *)address,
1122 addr_list[bonded_devices_req_info->count].addr);
1123 BT_ERR("_bt_device_get_bonded_device_info Failed for [%s]", address);
1124 if (bonded_devices_req_info->count == 0) {
1125 g_free(bonded_devices_req_info->addr_list);
1126 g_free(bonded_devices_req_info);
1127 req_info->user_data = NULL;
1134 BT_ERR("Unknown service function[%d]", service_function);
1137 _bt_service_method_return(req_info->context, out_param, req_info->result);
1138 g_array_free(out_param, TRUE);
1139 /* Now free invocation info for this request*/
1140 _bt_free_info_from_invocation_list(req_info);
1144 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1146 char *phone_name = NULL;
1152 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1153 phone_name = vconf_keynode_get_str(node);
1155 if (phone_name && strlen(phone_name) != 0) {
1156 if (!g_utf8_validate(phone_name, -1,
1157 (const char **)&ptr))
1160 _bt_set_local_name(phone_name);
1165 /* Request return handlings */
1166 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1168 BT_INFO("__bt_adapter_post_set_enabled>>");
1170 if (!TIZEN_PROFILE_TV) {
1171 __bt_set_visible_mode();
1173 /* add the vconf noti handler */
1174 if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1175 (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1176 BT_ERR("DEVICE_NAME key changed notification registration failed");
1178 __bt_set_local_name();
1180 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1181 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1182 BT_ERR("Fail to set discoverable mode");
1185 /* Get All properties */
1186 if (OAL_STATUS_SUCCESS != adapter_get_properties())
1187 BT_ERR("adapter_get_properties failed");
1189 /* Add Adapter enabled post processing codes */
1193 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1195 BT_INFO("_bt_adapter_post_set_disabled>>");
1197 if (!TIZEN_PROFILE_TV) {
1198 /* Add Adapter disabled post processing codes */
1199 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1200 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1201 BT_ERR("vconf_ignore_key_changed failed");
1207 static void __bt_adapter_update_bt_enabled(void)
1209 int result = BLUETOOTH_ERROR_NONE;
1210 BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1211 if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1212 BT_ERR("Bluetooth profile init failed");
1214 /* Update Bluetooth Status to notify other modules */
1215 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1216 BT_ERR("Set vconf failed\n");
1218 /* TODO:Add timer function to handle any further post processing */
1219 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1221 /*Return BT_ADAPTER_ENABLE Method invocation context */
1222 __bt_adapter_handle_pending_requests(BT_ENABLE_ADAPTER, NULL, 0);
1223 /*Send BT Enabled event to application */
1224 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1225 g_variant_new("(i)", result));
1228 static void __bt_adapter_update_bt_disabled(void)
1230 int result = BLUETOOTH_ERROR_NONE;
1231 BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1232 _bt_cleanup_profiles();
1234 int power_off_status = 0;
1237 /* Update the vconf BT status in normal Deactivation case only */
1238 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1239 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1241 /* Update Bluetooth Status to notify other modules */
1242 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1243 BT_ERR("Set vconf failed");
1245 /* TODO:Add timer function to handle any further post processing */
1246 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1248 /* Return BT_ADAPTER_DISABLE Method invocation context */
1249 __bt_adapter_handle_pending_requests(BT_DISABLE_ADAPTER, NULL, 0);
1251 /* Send BT Disabled event to application */
1252 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1253 g_variant_new("(i)", result));
1256 static void __bt_adapter_state_set_status(bt_status_t status)
1258 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1259 adapter_state = status;
1262 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1264 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1265 adapter_discovery_state = status;
1268 static void __bt_adapter_state_change_callback(int bt_status)
1270 BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
1272 switch (bt_status) {
1273 case BT_DEACTIVATED:
1274 __bt_adapter_state_set_status(bt_status);
1276 /* Adapter is disabled, unregister event handlers */
1277 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1278 //_bt_deinit_device_event_handler();
1280 /* Add Adapter disabled post processing codes */
1281 __bt_adapter_update_bt_disabled();
1284 __bt_adapter_state_set_status(bt_status);
1285 /* Add Adapter enabled post processing codes */
1287 BT_DBG("g_source is removed");
1288 g_source_remove(timer_id);
1291 __bt_adapter_update_bt_enabled();
1294 BT_ERR("Incorrect Bluetooth adapter state changed status");
1299 static int __bt_adapter_state_handle_request(gboolean enable)
1301 int result = BLUETOOTH_ERROR_NONE;
1304 switch (adapter_state) {
1305 case BT_ACTIVATING: {
1306 BT_INFO("Adapter is currently in activating state, state [%d]",
1309 return BLUETOOTH_ERROR_IN_PROGRESS;
1311 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1312 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1313 /*TODO Stop Discovery*/
1314 if (result != OAL_STATUS_SUCCESS)
1315 BT_ERR("Discover stop failed: %d", result);
1316 __bt_adapter_update_discovery_status(FALSE);
1318 result = adapter_disable();
1319 if (result != OAL_STATUS_SUCCESS) {
1320 BT_ERR("adapter_enable failed: [%d]", result);
1321 result = BLUETOOTH_ERROR_INTERNAL;
1322 /*TODO: perform if anything more needs to be done to handle failure */
1324 /* TODO: To be handled */
1325 __bt_adapter_state_set_status(BT_DEACTIVATING);
1326 result = BLUETOOTH_ERROR_NONE;
1331 case BT_ACTIVATED: {
1332 BT_INFO("Adapter is currently in activated state, state [%d]",
1335 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1337 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1338 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1339 /*TODO Stop Discovery*/
1340 if (result != OAL_STATUS_SUCCESS)
1341 BT_ERR("Discover stop failed: %d", result);
1342 __bt_adapter_update_discovery_status(FALSE);
1344 result = adapter_disable();
1345 if (result != OAL_STATUS_SUCCESS) {
1346 BT_ERR("adapter_enable failed: [%d]", result);
1347 result = BLUETOOTH_ERROR_INTERNAL;
1348 /*TODO: perform if anything more needs to be done to handle failure */
1350 /* TODO: To be handled */
1351 __bt_adapter_state_set_status(BT_DEACTIVATING);
1352 result = BLUETOOTH_ERROR_NONE;
1357 case BT_DEACTIVATING: {
1358 BT_INFO("Adapter is currently in deactivating state, state [%d]",
1361 return BLUETOOTH_ERROR_IN_PROGRESS;
1364 result = adapter_enable();
1365 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1366 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_ACTIVATING);
1373 result = BLUETOOTH_ERROR_NONE;
1378 case BT_DEACTIVATED: {
1379 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1382 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1384 result = adapter_enable();
1385 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1386 BT_ERR("adapter_enable failed: [%d]", result);
1388 result = BLUETOOTH_ERROR_INTERNAL;
1389 /*TODO: perform if anything more needs to be done to handle failure */
1391 /* TODO: To be handled */
1392 __bt_adapter_state_set_status(BT_ACTIVATING);
1393 result = BLUETOOTH_ERROR_NONE;
1399 BT_ERR("Unknown state: %d", adapter_state);
1403 if (enable && result == BLUETOOTH_ERROR_NONE) {
1404 /* Adapter enable request is successful, setup event handlers */
1405 _bt_service_register_event_handler_callback(
1406 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1407 _bt_device_state_handle_callback_set_request();
1412 static int __bt_adapter_state_discovery_request(gboolean enable,
1413 unsigned short max_response, unsigned short duration,
1414 unsigned int mask, gboolean is_custom,
1415 bt_discovery_role_type_t role)
1417 int result = BLUETOOTH_ERROR_NONE;
1420 switch (adapter_discovery_state) {
1421 case ADAPTER_DISCOVERY_STARTED: {
1422 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1423 adapter_discovery_state);
1425 return BLUETOOTH_ERROR_IN_PROGRESS;
1427 result = adapter_stop_inquiry();
1428 if (result != OAL_STATUS_SUCCESS) {
1429 BT_ERR("Discover stop failed: %d", result);
1430 result = BLUETOOTH_ERROR_INTERNAL;
1432 BT_ERR("Stop Discovery Triggered successfully");
1433 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1434 result = BLUETOOTH_ERROR_NONE;
1439 case ADAPTER_DISCOVERY_STARTING: {
1440 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1441 adapter_discovery_state);
1443 return BLUETOOTH_ERROR_IN_PROGRESS;
1445 result = adapter_stop_inquiry();
1446 if (result != OAL_STATUS_SUCCESS) {
1447 BT_ERR("Discover stop failed: %d", result);
1448 result = BLUETOOTH_ERROR_INTERNAL;
1450 BT_ERR("Stop Discovery Triggered successfully");
1451 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1452 result = BLUETOOTH_ERROR_NONE;
1457 case ADAPTER_DISCOVERY_STOPPED: {
1458 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1459 adapter_discovery_state);
1461 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1463 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1466 result = adapter_start_inquiry(duration);
1468 result = adapter_start_custom_inquiry(role);
1470 if (result != OAL_STATUS_SUCCESS) {
1471 BT_ERR("Start Discovery failed: %d", result);
1472 result = BLUETOOTH_ERROR_INTERNAL;
1474 BT_ERR("Start Discovery Triggered successfully");
1475 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1476 result = BLUETOOTH_ERROR_NONE;
1481 case ADAPTER_DISCOVERY_STOPPING: {
1482 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1483 adapter_discovery_state);
1485 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1487 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1489 result = adapter_start_inquiry(duration);
1491 result = adapter_start_custom_inquiry(role);
1492 if (result != OAL_STATUS_SUCCESS) {
1493 BT_ERR("Start Discovery failed: %d", result);
1494 result = BLUETOOTH_ERROR_INTERNAL;
1496 BT_ERR("Start Discovery Triggered successfully");
1497 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1498 result = BLUETOOTH_ERROR_NONE;
1504 BT_ERR("Unknown state: %d", adapter_discovery_state);
1512 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1514 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1515 GVariant *param = NULL;
1516 int result = BLUETOOTH_ERROR_NONE;
1518 switch (bt_discovery_status) {
1519 case ADAPTER_DISCOVERY_STOPPED:
1521 __bt_adapter_update_discovery_status(bt_discovery_status);
1522 param = g_variant_new("(i)", result);
1523 _bt_send_event(BT_ADAPTER_EVENT,
1524 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1528 case ADAPTER_DISCOVERY_STARTED:
1530 __bt_adapter_update_discovery_status(bt_discovery_status);
1531 param = g_variant_new("(i)", result);
1532 _bt_send_event(BT_ADAPTER_EVENT,
1533 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1538 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1542 static void __bt_set_visible_mode(void)
1545 #ifdef TIZEN_FEATURE_BT_DPM
1546 int discoverable_state = DPM_BT_ERROR;
1549 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1550 BT_ERR("Fail to get the timeout value");
1552 #ifdef TIZEN_FEATURE_BT_DPM
1553 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1554 if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
1555 if (_bt_set_discoverable_mode(
1556 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1557 timeout) != BLUETOOTH_ERROR_NONE) {
1558 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1559 BT_ERR("Set vconf failed");
1562 if (_bt_set_discoverable_mode(
1563 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1564 timeout) != BLUETOOTH_ERROR_NONE) {
1565 BT_ERR("Set connectable mode failed");
1569 if (timeout == -1) {
1570 if (_bt_set_discoverable_mode(
1571 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1572 timeout) != BLUETOOTH_ERROR_NONE) {
1573 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1574 BT_ERR("Set vconf failed");
1577 if (_bt_set_discoverable_mode(
1578 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1579 timeout) != BLUETOOTH_ERROR_NONE) {
1580 BT_ERR("Set connectable mode failed");
1586 static void __bt_set_local_name(void)
1588 char *phone_name = NULL;
1591 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1596 if (strlen(phone_name) != 0) {
1597 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1600 _bt_set_local_name(phone_name);
1605 void _bt_adapter_set_status(bt_status_t status)
1607 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1608 adapter_state = status;
1611 bt_status_t _bt_adapter_get_status(void)
1613 return adapter_state;
1616 void _bt_set_disabled(int result)
1618 int power_off_status = 0;
1621 int pm_ignore_mode = 0;
1623 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1624 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1626 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1628 /* Update the vconf BT status in normal Deactivation case only */
1629 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1630 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1632 BT_DBG("Update vconf for BT normal Deactivation");
1634 if (result == BLUETOOTH_ERROR_TIMEOUT)
1635 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1636 BT_ERR("Set vconf failed");
1638 /* Update Bluetooth Status to notify other modules */
1639 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1640 BT_ERR("Set vconf failed");
1642 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1643 EVT_VAL_BT_OFF) != ES_R_OK)
1644 BT_ERR("Fail to set value");
1647 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1648 BT_ERR("Set vconf failed\n");
1650 _bt_cancel_queued_transfers();
1651 _bt_adapter_set_status(BT_DEACTIVATED);
1652 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1654 BT_INFO("Adapter disabled");