2 * Copyright (c) 2015 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Anupam Roy <anupam.r@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include <vconf-internal-keys.h>
28 #include <bundle_internal.h>
29 #include <eventsystem.h>
33 /*bt-service headers */
34 #include "bt-internal-types.h"
35 #include "bt-service-common.h"
36 #include "bt-service-util.h"
37 #include "bt-service-main.h"
38 #include "bt-service-core-adapter.h"
39 #include "bt-service-core-device.h"
40 #include "bt-service-event-receiver.h"
41 #include "bt-request-handler.h"
42 #include "bt-service-event.h"
43 #include "bt-service-audio-common.h"
44 #include "bt-service-core-adapter-le.h"
45 #include "bt-service-gatt.h"
47 #ifdef TIZEN_FEATURE_BT_DPM
48 #include "bt-service-dpm.h"
50 #include "bt-service-hidhost.h"
51 #include "bt-service-hiddevice.h"
52 #include "bt-service-socket.h"
53 #include "bt-service-hdp.h"
56 #include <oal-event.h>
57 #include <oal-manager.h>
58 #include <oal-adapter-mgr.h>
60 #ifdef TIZEN_FEATURE_BT_PAN_NAP
61 #include "bt-service-network.h"
63 /*This file will contain state machines related to adapter and remote device */
65 #include "bt-internal-types.h"
67 /* Global variables */
76 static bt_adapter_timer_t visible_timer;
78 static guint timer_id = 0;
80 static gboolean a2dp_init_pending = TRUE;
82 /* Adapter default states */
83 static bt_status_t adapter_state = BT_DEACTIVATED;
84 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
86 /* Forward declarations */
87 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
88 static void __bt_post_oal_init(void);
89 static void __bt_handle_oal_initialisation(oal_event_t event);
90 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
91 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
92 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
93 static void __bt_adapter_update_bt_enabled(void);
94 static void __bt_adapter_update_bt_disabled(void);
95 static void __bt_adapter_state_set_status(bt_status_t status);
96 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
97 static void __bt_adapter_state_change_callback(int bt_status);
98 static int __bt_adapter_state_handle_request(gboolean enable);
99 static int __bt_adapter_state_discovery_request(gboolean enable,
100 unsigned short max_response, unsigned short duration, unsigned int mask,
101 gboolean is_custom, bt_discovery_role_type_t role);
102 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
103 static gboolean __bt_is_service_request_present(int service_function);
105 static void __bt_set_visible_mode(void);
106 static void __bt_set_local_name(void);
108 /* 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 bt_le_status_t le_status = BT_LE_DEACTIVATED;
141 le_status = _bt_adapter_get_le_status();
143 BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
145 if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
146 BT_ERR("Enabling in progress");
147 return BLUETOOTH_ERROR_IN_PROGRESS;
150 if (adapter_state == BT_ACTIVATED) {
151 BT_ERR("Already enabled");
152 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
155 if (adapter_state == BT_DEACTIVATING ||
156 adapter_state == BT_TERMINATING ||
157 le_status == BT_LE_DEACTIVATING) {
158 BT_ERR("Disabling in progress");
159 return BLUETOOTH_ERROR_DEVICE_BUSY;
162 return __bt_adapter_state_handle_request(TRUE);
165 int _bt_enable_core(void)
167 /* TODO_40 : 4.0 merge */
168 BT_INFO("Not Supported");
169 return BLUETOOTH_ERROR_NOT_SUPPORT;
172 int _bt_recover_adapter(void)
174 /* TODO_40 : 4.0 merge */
175 BT_INFO("Not Supported");
176 return BLUETOOTH_ERROR_NOT_SUPPORT;
179 int _bt_reset_adapter(void)
182 if (OAL_STATUS_SUCCESS != adapter_reset())
183 return BLUETOOTH_ERROR_INTERNAL;
185 /* TODO_40 : 4.0 merge */
186 /* TODO Currently bt-service is not terminated in tizen next.
187 It should be handled in future patch */
188 if (_bt_adapter_get_status() == BT_DEACTIVATED)
189 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
191 return BLUETOOTH_ERROR_NONE;
194 int _bt_check_adapter(int *status)
197 gboolean powered = FALSE;
198 BT_CHECK_PARAMETER(status, return);
200 *status = BT_ADAPTER_DISABLED;
202 if (OAL_STATUS_SUCCESS != adapter_get_powered_status(&powered))
203 return BLUETOOTH_ERROR_INTERNAL;
205 BT_INFO("Adapter is in Powered Up state");
206 *status = BT_ADAPTER_ENABLED;
208 BT_INFO("Adapter is in Powered Down state");
210 return BLUETOOTH_ERROR_NONE;
213 int _bt_disable_adapter(void)
215 bt_le_status_t le_status = BT_LE_DEACTIVATED;
217 le_status = _bt_adapter_get_le_status();
219 BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
221 if (adapter_state == BT_DEACTIVATING ||
222 adapter_state == BT_TERMINATING ||
223 le_status == BT_LE_DEACTIVATING) {
224 BT_DBG("Disabling in progress");
225 return BLUETOOTH_ERROR_IN_PROGRESS;
228 if (adapter_state == BT_DEACTIVATED) {
229 BT_DBG("Already disabled");
230 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
233 if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
234 BT_ERR("Enabling in progress");
235 return BLUETOOTH_ERROR_DEVICE_BUSY;
238 /* TODO: Need to add the function to disconnect all devices */
239 /* __bt_disconnect_all(); */
241 return __bt_adapter_state_handle_request(FALSE);
244 int _bt_start_discovery(unsigned short max_response,
245 unsigned short duration, unsigned int cod_mask)
247 return __bt_adapter_state_discovery_request(TRUE, max_response, duration,
248 cod_mask, FALSE, 0x00);
251 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
253 return __bt_adapter_state_discovery_request(TRUE, 0, 0, 0, TRUE, role);
256 int _bt_cancel_discovery(void)
258 return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0, FALSE, 0x00);
261 gboolean _bt_is_discovering(void)
263 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
264 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
270 int _bt_get_local_address(void)
276 result = adapter_get_address();
277 if (result != OAL_STATUS_SUCCESS) {
278 BT_ERR("adapter_get_address failed: %d", result);
279 result = BLUETOOTH_ERROR_INTERNAL;
281 result = BLUETOOTH_ERROR_NONE;
287 int _bt_get_local_version(void)
292 result = adapter_get_version();
293 if (result != OAL_STATUS_SUCCESS) {
294 BT_ERR("adapter_get_version failed: %d", result);
295 result = BLUETOOTH_ERROR_INTERNAL;
297 result = BLUETOOTH_ERROR_NONE;
303 int _bt_get_local_name(void)
309 result = adapter_get_name();
310 if (result != OAL_STATUS_SUCCESS) {
311 BT_ERR("adapter_get_name failed: %d", result);
312 result = BLUETOOTH_ERROR_INTERNAL;
314 result = BLUETOOTH_ERROR_NONE;
320 int _bt_set_local_name(char *local_name)
322 int result = BLUETOOTH_ERROR_NONE;
325 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
327 result = adapter_set_name(local_name);
328 if (result != OAL_STATUS_SUCCESS) {
329 BT_ERR("adapter_set_name failed: %d", result);
330 result = BLUETOOTH_ERROR_INTERNAL;
332 result = BLUETOOTH_ERROR_NONE;
338 int _bt_get_discoverable_mode(int *mode)
345 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
347 adapter_is_discoverable(&scan_mode);
348 if (TRUE == scan_mode) {
349 adapter_get_discoverable_timeout(&timeout);
351 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
353 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
355 adapter_is_connectable(&scan_mode);
356 if (scan_mode == TRUE)
357 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
360 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
361 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
362 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
369 return BLUETOOTH_ERROR_NONE;
372 int _bt_get_timeout_value(int *timeout)
377 /* Take current time */
379 time_diff = difftime(current_time, visible_timer.start_time);
381 BT_DBG("Time diff = %d\n", time_diff);
382 *timeout = visible_timer.timeout - time_diff;
384 return BLUETOOTH_ERROR_NONE;
387 static void __bt_visibility_alarm_remove()
389 if (visible_timer.event_id > 0) {
390 g_source_remove(visible_timer.event_id);
391 visible_timer.event_id = 0;
394 if (visible_timer.alarm_id > 0) {
395 alarmmgr_remove_alarm(visible_timer.alarm_id);
396 visible_timer.alarm_id = 0;
400 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
402 int result = BLUETOOTH_ERROR_NONE;
405 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
407 if (alarm_id != visible_timer.alarm_id)
410 if (visible_timer.event_id) {
411 _bt_send_event(BT_ADAPTER_EVENT,
412 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
413 g_variant_new("(in)", result, timeout));
414 g_source_remove(visible_timer.event_id);
415 visible_timer.event_id = 0;
416 visible_timer.timeout = 0;
418 if (!TIZEN_PROFILE_WEARABLE) {
419 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
420 BT_ERR("Set vconf failed\n");
423 /* Switch Off visibility in Bluez */
424 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
425 visible_timer.alarm_id = 0;
429 static gboolean __bt_timeout_handler(gpointer user_data)
431 int result = BLUETOOTH_ERROR_NONE;
435 /* Take current time */
437 time_diff = difftime(current_time, visible_timer.start_time);
439 /* Send event to application */
440 _bt_send_event(BT_ADAPTER_EVENT,
441 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
442 g_variant_new("(in)", result, time_diff));
444 if (visible_timer.timeout <= time_diff) {
445 g_source_remove(visible_timer.event_id);
446 visible_timer.event_id = 0;
447 visible_timer.timeout = 0;
449 if (!TIZEN_PROFILE_WEARABLE) {
450 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
451 BT_ERR("Set vconf failed\n");
460 static void __bt_visibility_alarm_create()
465 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
468 BT_ERR("Failed to create alarm error = %d\n", result);
470 BT_DBG("Alarm created = %d\n", alarm_id);
471 visible_timer.alarm_id = alarm_id;
475 int _bt_start_visibility_timer(int timeout)
478 #ifdef TIZEN_FEATURE_BT_DPM
479 int discoverable_state = DPM_BT_ERROR;
482 __bt_visibility_alarm_remove();
484 visible_timer.timeout = timeout;
486 #ifdef TIZEN_FEATURE_BT_DPM
487 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
488 if (discoverable_state != DPM_RESTRICTED) {
490 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
491 BT_ERR("Set vconf failed");
492 #ifdef TIZEN_FEATURE_BT_DPM
497 return BLUETOOTH_ERROR_NONE;
499 if (!visible_timer.alarm_init) {
500 /* Set Alarm timer to switch off BT */
501 result = alarmmgr_init("bt-service");
503 return BLUETOOTH_ERROR_INTERNAL;
505 visible_timer.alarm_init = TRUE;
508 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
510 return BLUETOOTH_ERROR_INTERNAL;
512 if (!TIZEN_PROFILE_WEARABLE) {
513 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
514 BT_ERR("Set vconf failed");
517 /* Take start time */
518 time(&(visible_timer.start_time));
519 visible_timer.event_id = g_timeout_add_seconds(1,
520 __bt_timeout_handler, NULL);
522 visible_timer.timeout = timeout;
524 __bt_visibility_alarm_create();
526 return BLUETOOTH_ERROR_NONE;
529 int _bt_stop_visibility_timer(void)
531 __bt_visibility_alarm_remove();
533 visible_timer.timeout = 0;
535 if (!TIZEN_PROFILE_WEARABLE) {
536 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
537 BT_ERR("Set vconf failed");
540 return BLUETOOTH_ERROR_NONE;
543 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
546 int ret = BLUETOOTH_ERROR_NONE;;
547 #ifdef TIZEN_FEATURE_BT_DPM
548 int discoverable_state = DPM_BT_ERROR;
553 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
555 #ifdef TIZEN_FEATURE_BT_DPM
556 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
557 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
558 discoverable_state == DPM_RESTRICTED) {
559 if (headed_plugin_info->plugin_headed_enabled)
560 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
561 return BLUETOOTH_ERROR_ACCESS_DENIED;
563 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE &&
564 discoverable_state == DPM_RESTRICTED) {
565 if (headed_plugin_info->plugin_headed_enabled)
566 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
567 return BLUETOOTH_ERROR_ACCESS_DENIED;
571 switch (discoverable_mode) {
572 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
573 result = adapter_set_connectable(TRUE);
576 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
577 result = adapter_set_discoverable();
580 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
581 result = adapter_set_discoverable();
584 return BLUETOOTH_ERROR_INVALID_PARAM;
587 if (result != OAL_STATUS_SUCCESS) {
588 BT_ERR("set scan mode failed %d", result);
589 return BLUETOOTH_ERROR_INTERNAL;
592 result = adapter_set_discoverable_timeout(timeout);
593 if (result != OAL_STATUS_SUCCESS) {
594 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
595 return BLUETOOTH_ERROR_INTERNAL;
598 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
599 ret = _bt_stop_visibility_timer();
600 if (BLUETOOTH_ERROR_NONE != ret)
601 BT_ERR("_bt_stop_visibility_timer failed");
602 if (!TIZEN_PROFILE_WEARABLE) {
603 if (vconf_set_int(BT_FILE_VISIBLE_TIME, -1) != 0)
604 BT_ERR("Set vconf failed");
606 } else if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE) {
607 ret = _bt_start_visibility_timer(timeout);
608 if (BLUETOOTH_ERROR_NONE != ret)
609 BT_ERR("_bt_start_visibility_timer failed");
612 ret = _bt_stop_visibility_timer();
613 if (BLUETOOTH_ERROR_NONE != ret)
614 BT_ERR("_bt_stop_visibility_timer failed");
621 gboolean _bt_is_connectable(void)
628 adapter_is_connectable(&connectable);
634 BT_DBG("Connectable: [%s]", result ? "TRUE" : "FALSE");
639 int _bt_set_connectable(gboolean connectable)
641 int result = BLUETOOTH_ERROR_NONE;
644 result = adapter_set_connectable(connectable);
645 if (result != OAL_STATUS_SUCCESS) {
646 BT_ERR("adapter_set_connectable failed: %d", result);
647 result = BLUETOOTH_ERROR_INTERNAL;
649 result = BLUETOOTH_ERROR_NONE;
655 int _bt_is_service_used(void)
661 result = adapter_get_service_uuids();
662 if (result != OAL_STATUS_SUCCESS) {
663 BT_ERR("adapter_get_service_uuids failed: %d", result);
664 result = BLUETOOTH_ERROR_INTERNAL;
666 result = BLUETOOTH_ERROR_NONE;
673 int _bt_adapter_get_bonded_devices(void)
675 int result = BLUETOOTH_ERROR_NONE;
678 result = adapter_get_bonded_devices();
679 if (result != OAL_STATUS_SUCCESS) {
680 BT_ERR("adapter_get_bonded_devices failed: %d", result);
681 result = BLUETOOTH_ERROR_INTERNAL;
683 result = BLUETOOTH_ERROR_NONE;
689 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
692 GDBusConnection *conn;
693 GDBusProxy *manager_proxy;
694 GVariant *result = NULL;
695 GVariant *result1 = NULL;
696 GVariantIter *iter = NULL;
697 GError *error = NULL;
698 char *object_path = NULL;
699 GVariantIter *interface_iter;
700 char *interface_str = NULL;
701 GDBusProxy *device_proxy = NULL;
702 gboolean is_connected = FALSE;
704 conn = _bt_gdbus_get_system_gconn();
705 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
707 manager_proxy = _bt_get_manager_proxy();
708 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
710 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
712 G_DBUS_CALL_FLAGS_NONE,
719 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
720 g_clear_error(&error);
723 BT_ERR("Failed to Failed to GetManagedObjects");
724 return BLUETOOTH_ERROR_INTERNAL;
727 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
728 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
730 /* Parse the signature: oa{sa{sv}}} */
731 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
732 if (object_path == NULL)
735 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
736 &interface_str, NULL)) {
737 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
738 BT_DBG("Found a device: %s", object_path);
739 g_free(interface_str);
741 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
743 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
745 if (device_proxy == NULL) {
746 BT_DBG("Device don't have this service");
750 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
751 g_variant_new("(s)", profile_uuid),
752 G_DBUS_CALL_FLAGS_NONE,
757 if (result1 == NULL) {
758 BT_ERR("Error occured in Proxy call");
760 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
764 g_object_unref(device_proxy);
767 g_variant_get(result1, "(b)", &is_connected);
769 if (is_connected == TRUE) {
770 char address[BT_ADDRESS_STRING_SIZE];
771 bluetooth_device_address_t *addr = NULL;
773 _bt_convert_device_path_to_address(object_path, address);
775 addr = g_malloc0(sizeof(bluetooth_device_address_t));
776 _bt_convert_addr_string_to_type(addr->addr, address);
778 g_array_append_vals(*addr_list, addr,
779 sizeof(bluetooth_device_address_t));
782 g_variant_unref(result1);
783 g_object_unref(device_proxy);
790 g_variant_unref(result);
791 g_variant_iter_free(iter);
794 return BLUETOOTH_ERROR_NONE;
797 static void __bt_handle_pending_a2dp_init(unsigned int a2dp_role)
801 if (!a2dp_init_pending)
805 a2dp_init_pending = FALSE;
807 if (a2dp_role == BT_A2DP_SINK_ROLE) {
808 BT_INFO("Enable A2DP Sink role");
809 /* Initialize A2DP Sink */
810 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
811 if (ret != BLUETOOTH_ERROR_NONE)
812 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
814 /* Initialize AVRCP Controller */
815 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
816 if (ret != BLUETOOTH_ERROR_NONE)
817 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
819 _bt_audio_set_current_role(BLUETOOTH_A2DP_SINK);
823 BT_INFO("Enable A2DP Source role");
824 /* Initialize A2DP Source */
825 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
826 if (ret != BLUETOOTH_ERROR_NONE)
827 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
829 /* Initialize AVRCP Target */
830 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
831 if (ret != BLUETOOTH_ERROR_NONE)
832 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
834 _bt_audio_set_current_role(BLUETOOTH_A2DP_SOURCE);
838 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
840 int result = BLUETOOTH_ERROR_NONE;
844 switch (event_type) {
845 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
846 case OAL_EVENT_OAL_INITIALISED_FAILED:
847 __bt_handle_oal_initialisation(event_type);
849 case OAL_EVENT_ADAPTER_ENABLED:
850 __bt_adapter_state_change_callback(BT_ACTIVATED);
852 case OAL_EVENT_ADAPTER_DISABLED:
853 __bt_adapter_state_change_callback(BT_DEACTIVATED);
855 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
856 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
858 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
859 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
861 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
862 bt_address_t *bd_addr = event_data;
863 bluetooth_device_address_t local_address;
866 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
867 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
868 local_address.addr[0], local_address.addr[1], local_address.addr[2],
869 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
871 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
872 (void *) &local_address, sizeof(bluetooth_device_address_t));
875 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
876 char *name = event_data;
877 BT_DBG("Adapter Name: %s", name);
879 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
880 bluetooth_device_name_t local_name;
882 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
883 g_strlcpy(local_name.name,
884 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
885 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
886 (void *) &local_name, sizeof(bluetooth_device_name_t));
888 /* Send event to application */
889 _bt_send_event(BT_ADAPTER_EVENT,
890 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
891 g_variant_new("(is)", result, name));
895 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
896 char *ver = event_data;
897 bluetooth_version_t local_version;
899 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
900 g_strlcpy(local_version.version,
901 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
902 BT_DBG("BT Version: %s", local_version.version);
904 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
905 (void *) &local_version, sizeof(bluetooth_version_t));
908 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
910 gboolean connectable = FALSE;
912 BT_INFO("Adapter discoverable mode:"
913 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
914 _bt_send_event(BT_ADAPTER_EVENT,
915 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
916 g_variant_new("(b)", connectable));
918 _bt_send_event(BT_ADAPTER_EVENT,
919 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
920 g_variant_new("(in)", result, mode));
923 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
925 gboolean connectable = TRUE;
927 BT_INFO("Adapter discoverable mode:"
928 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
929 _bt_send_event(BT_ADAPTER_EVENT,
930 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
931 g_variant_new("(b)", connectable));
933 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
934 _bt_send_event(BT_ADAPTER_EVENT,
935 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
936 g_variant_new("(in)", result, mode));
939 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
942 BT_INFO("Adapter discoverable mode:"
943 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
945 /* Send event to application */
946 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
947 _bt_send_event(BT_ADAPTER_EVENT,
948 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
949 g_variant_new("(in)", result, mode));
953 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
954 int *timeout = event_data;
957 BT_INFO("Discoverable timeout: [%d]", *timeout);
959 /* Send event to application */
960 _bt_get_discoverable_mode(&mode);
961 _bt_send_event(BT_ADAPTER_EVENT,
962 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
963 g_variant_new("(in)", result, mode));
966 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
968 service_uuid_t *service_list;
969 event_adapter_services_t *list = event_data;
972 service_list = list->service_list;
973 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
976 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
979 bluetooth_device_address_t *addr_list;
981 event_device_list_t *bonded_device_list = event_data;
982 count = bonded_device_list->num;
984 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
985 for (i = 0; i < count; i++) {
986 memcpy(addr_list[i].addr,
987 bonded_device_list->devices[i].addr,
988 BLUETOOTH_ADDRESS_LENGTH);
991 BT_INFO("Adapter Bonded device List count: [%d]", count);
992 _bt_device_handle_paired_address_list(addr_list, bonded_device_list->num);
996 case OAL_EVENT_ADAPTER_PROPERTY_A2DP_ROLE: {
997 unsigned int *a2dp_role = event_data;
999 BT_INFO("a2dp role: [%u]", *a2dp_role);
1001 __bt_handle_pending_a2dp_init(*a2dp_role);
1006 BT_ERR("Unhandled event..");
1013 int _bt_init_profiles()
1018 /*TODO: Init bluetooth profiles */
1019 ret = _bt_hidhost_initialize();
1020 if (ret != BLUETOOTH_ERROR_NONE)
1021 BT_ERR("_bt_hidhost_initialize Failed");
1023 ret = _bt_hiddevice_initialize();
1024 if (ret != BLUETOOTH_ERROR_NONE)
1025 BT_ERR("_bt_hiddevice_initialize Failed");
1027 ret = _bt_socket_init();
1028 if (ret != BLUETOOTH_ERROR_NONE)
1029 BT_ERR("_bt_socket_init Failed");
1031 /* Initialize HFP Audio Gateway */
1032 ret = _bt_audio_initialize(BT_AG_MODULE);
1033 if (ret != BLUETOOTH_ERROR_NONE)
1034 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
1036 /* Registering callback for receiving audio services searched */
1037 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
1038 if (ret != BLUETOOTH_ERROR_NONE)
1039 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
1041 ret = _bt_hdp_init();
1042 if (ret != BLUETOOTH_ERROR_NONE)
1043 BT_ERR("_bt_hdp_init Failed");
1045 ret = _bt_gatt_init();
1046 if (ret != BLUETOOTH_ERROR_NONE)
1047 BT_ERR("_bt_gatt_init Failed");
1049 /* Initialize HF Client */
1050 ret = _bt_audio_initialize(BT_HFP_MODULE);
1051 if (ret != BLUETOOTH_ERROR_NONE)
1052 BT_ERR("_bt_audio_initialize(BT_HFP_MODULE) Failed");
1054 return BLUETOOTH_ERROR_NONE;
1057 int _bt_cleanup_profiles(void)
1059 /* TODO: Cleanup bluetooth profiles */
1060 _bt_hidhost_deinitialize();
1061 _bt_socket_deinit();
1063 /* TODO: Cleanup bluetooth audio profiles */
1064 //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
1065 //_bt_audio_deinitialize(BT_AVRCP_MODULE);
1066 //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
1067 //_bt_audio_deinitialize(BT_AG_MODULE);
1068 //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
1069 //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
1071 a2dp_init_pending = TRUE;
1076 return BLUETOOTH_ERROR_NONE;
1079 /* OAL post initialization handler */
1080 static void __bt_post_oal_init(void)
1083 int status = VCONFKEY_BT_STATUS_OFF;
1085 BT_DBG("OAL initialized");
1086 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0)
1087 BT_ERR("Fail to get the enabled value");
1090 /* Update Bluetooth Status to OFF */
1091 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1092 BT_ERR("Set vconf failed\n");
1095 if (status & VCONFKEY_BT_STATUS_ON) {
1096 ret = _bt_enable_adapter();
1097 if (ret != BLUETOOTH_ERROR_NONE)
1098 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
1102 /* OAL initialization handler */
1103 static void __bt_handle_oal_initialisation(oal_event_t event)
1108 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1109 __bt_post_oal_init();
1111 case OAL_EVENT_OAL_INITIALISED_FAILED:
1112 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1113 g_idle_add(_bt_terminate_service, NULL);
1116 BT_ERR("Unknown Event");
1121 static gboolean __bt_is_service_request_present(int service_function)
1124 invocation_info_t *req_info;
1128 /* Get method invocation context */
1129 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1131 if (req_info && req_info->service_function == service_function)
1139 /* Internal functions of core adapter service */
1140 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1144 invocation_info_t *req_info;
1147 /* Get method invocation context */
1148 for (l = _bt_get_invocation_list(); l != NULL; ) {
1150 l = g_slist_next(l);
1151 if (req_info == NULL || req_info->service_function != service_function)
1154 /* Create out param */
1155 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1157 switch (service_function) {
1158 case BT_ENABLE_ADAPTER:
1159 case BT_DISABLE_ADAPTER: {
1160 gboolean done = TRUE;
1161 g_array_append_vals(out_param, &done, sizeof(gboolean));
1164 case BT_GET_LOCAL_NAME:
1165 case BT_GET_LOCAL_ADDRESS:
1166 case BT_GET_LOCAL_VERSION:
1167 g_array_append_vals(out_param, user_data, size);
1169 case BT_IS_SERVICE_USED: {
1171 gboolean used = FALSE;
1172 unsigned char *uuid;
1173 char uuid_str[BT_UUID_STRING_SIZE];
1174 char *request_uuid = req_info->user_data;
1175 service_uuid_t *service_list = user_data;
1177 BT_INFO("Check for service uuid: %s", request_uuid);
1178 for (i = 0; i < size; i++) {
1179 uuid = service_list[i].uuid;
1180 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1181 BT_INFO("Adapter Service: [%s]", uuid_str);
1182 if (strcasecmp(uuid_str, request_uuid) == 0) {
1183 BT_INFO("UUID matched!!");
1189 g_array_append_vals(out_param, &used, sizeof(gboolean));
1193 BT_ERR("Unknown service function[%d]", service_function);
1196 _bt_service_method_return(req_info->context, out_param, req_info->result);
1197 g_array_free(out_param, TRUE);
1198 /* Now free invocation info for this request*/
1199 _bt_free_info_from_invocation_list(req_info);
1203 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1205 char *phone_name = NULL;
1211 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1212 phone_name = vconf_keynode_get_str(node);
1214 if (phone_name && strlen(phone_name) != 0) {
1215 if (!g_utf8_validate(phone_name, -1,
1216 (const char **)&ptr))
1219 _bt_set_local_name(phone_name);
1224 /* Request return handlings */
1225 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1227 BT_INFO("__bt_adapter_post_set_enabled>>");
1229 if (TIZEN_PROFILE_TV || !headed_plugin_info->plugin_headed_enabled) {
1230 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1231 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1232 BT_ERR("Fail to set discoverable mode");
1234 __bt_set_visible_mode();
1237 /* add the vconf noti handler */
1238 if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1239 (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1240 BT_ERR("DEVICE_NAME key changed notification registration failed");
1242 __bt_set_local_name();
1244 /* Get All properties */
1245 if (OAL_STATUS_SUCCESS != adapter_get_properties())
1246 BT_ERR("adapter_get_properties failed");
1248 /* Add Adapter enabled post processing codes */
1252 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1254 BT_INFO("_bt_adapter_post_set_disabled>>");
1256 if (!TIZEN_PROFILE_TV) {
1257 /* Add Adapter disabled post processing codes */
1258 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1259 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1260 BT_ERR("vconf_ignore_key_changed failed");
1263 /* bt-service should be terminated when BT is off */
1264 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1265 /* TODO: Implement to check if it is the recovery mode or not */
1266 _bt_reliable_terminate_service(NULL);
1272 static void __bt_adapter_update_bt_enabled(void)
1274 int result = BLUETOOTH_ERROR_NONE;
1276 BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1277 if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1278 BT_ERR("Bluetooth profile init failed");
1280 _bt_device_handle_adapter_state(TRUE);
1282 /* Update Bluetooth Status to notify other modules */
1283 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1284 BT_ERR("Set vconf failed\n");
1286 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1287 BT_ERR("Set vconf failed\n");
1289 /* TODO:Add timer function to handle any further post processing */
1290 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1292 /*Send BT Enabled event to application */
1293 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1294 g_variant_new("(i)", result));
1297 static void __bt_adapter_update_bt_disabled(void)
1299 int result = BLUETOOTH_ERROR_NONE;
1300 int power_off_status = 0;
1303 BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1304 _bt_cleanup_profiles();
1306 _bt_device_handle_adapter_state(FALSE);
1308 /* Update the vconf BT status in normal Deactivation case only */
1309 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1310 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1312 /* Update Bluetooth Status to notify other modules */
1313 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1314 BT_ERR("Set vconf failed");
1316 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1317 BT_ERR("Set vconf failed\n");
1319 /* TODO:Add timer function to handle any further post processing */
1320 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1322 /* Send BT Disabled event to application */
1323 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1324 g_variant_new("(i)", result));
1327 static void __bt_adapter_state_set_status(bt_status_t status)
1329 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1330 adapter_state = status;
1333 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1335 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1336 adapter_discovery_state = status;
1339 static void __bt_adapter_state_change_callback(int bt_status)
1341 BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
1343 switch (bt_status) {
1344 case BT_DEACTIVATED:
1345 __bt_adapter_state_set_status(BT_TERMINATING);
1347 /* Adapter is disabled, unregister event handlers */
1348 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1349 //_bt_deinit_device_event_handler();
1351 /* Add Adapter disabled post processing codes */
1352 __bt_adapter_update_bt_disabled();
1355 __bt_adapter_state_set_status(bt_status);
1356 /* Add Adapter enabled post processing codes */
1358 BT_DBG("g_source is removed");
1359 g_source_remove(timer_id);
1362 __bt_adapter_update_bt_enabled();
1365 BT_ERR("Incorrect Bluetooth adapter state changed status");
1370 int _bt_set_adapter_request_state(int enable)
1372 return oal_set_adapter_request_state(enable);
1375 int _bt_set_le_request_state(int enable)
1377 return oal_set_le_request_state(enable);
1380 static int __bt_adapter_state_handle_request(gboolean enable)
1382 int result = BLUETOOTH_ERROR_NONE;
1385 switch (adapter_state) {
1387 case BT_DEACTIVATING:
1388 BT_INFO("Should not be callded : state [%d]", adapter_state);
1389 return BLUETOOTH_ERROR_INTERNAL;
1391 case BT_ACTIVATED: {
1392 BT_INFO("Adapter is currently in activated state, state [%d]",
1395 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1397 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1398 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1399 /*TODO Stop Discovery*/
1400 __bt_adapter_update_discovery_status(FALSE);
1402 result = adapter_disable();
1403 if (result != OAL_STATUS_SUCCESS) {
1404 BT_ERR("adapter_enable failed: [%d]", result);
1405 result = BLUETOOTH_ERROR_INTERNAL;
1406 /*TODO: perform if anything more needs to be done to handle failure */
1408 /* TODO: To be handled */
1409 __bt_adapter_state_set_status(BT_DEACTIVATING);
1410 result = BLUETOOTH_ERROR_NONE;
1415 case BT_DEACTIVATED: {
1416 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1419 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1421 result = adapter_enable();
1422 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1423 BT_ERR("adapter_enable failed: [%d]", result);
1425 result = BLUETOOTH_ERROR_INTERNAL;
1426 /*TODO: perform if anything more needs to be done to handle failure */
1428 /* TODO: To be handled */
1429 __bt_adapter_state_set_status(BT_ACTIVATING);
1430 result = BLUETOOTH_ERROR_NONE;
1436 BT_ERR("Unknown state: %d", adapter_state);
1440 if (enable && result == BLUETOOTH_ERROR_NONE) {
1441 /* Adapter enable request is successful, setup event handlers */
1442 _bt_service_register_event_handler_callback(
1443 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1444 _bt_device_state_handle_callback_set_request();
1449 static int __bt_adapter_state_discovery_request(gboolean enable,
1450 unsigned short max_response, unsigned short duration,
1451 unsigned int mask, gboolean is_custom,
1452 bt_discovery_role_type_t role)
1454 int result = BLUETOOTH_ERROR_NONE;
1457 switch (adapter_discovery_state) {
1458 case ADAPTER_DISCOVERY_STARTED: {
1459 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1460 adapter_discovery_state);
1462 return BLUETOOTH_ERROR_IN_PROGRESS;
1464 result = adapter_stop_inquiry();
1465 if (result != OAL_STATUS_SUCCESS) {
1466 BT_ERR("Discover stop failed: %d", result);
1467 result = BLUETOOTH_ERROR_INTERNAL;
1469 BT_ERR("Stop Discovery Triggered successfully");
1470 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1471 result = BLUETOOTH_ERROR_NONE;
1476 case ADAPTER_DISCOVERY_STARTING: {
1477 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1478 adapter_discovery_state);
1480 result = enable ? BLUETOOTH_ERROR_IN_PROGRESS :
1481 BLUETOOTH_ERROR_DEVICE_BUSY;
1485 case ADAPTER_DISCOVERY_STOPPED: {
1486 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1487 adapter_discovery_state);
1489 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1491 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1494 result = adapter_start_inquiry(duration);
1496 result = adapter_start_custom_inquiry(role);
1498 if (result != OAL_STATUS_SUCCESS) {
1499 BT_ERR("Start Discovery failed: %d", result);
1500 result = BLUETOOTH_ERROR_INTERNAL;
1502 BT_ERR("Start Discovery Triggered successfully");
1503 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1504 result = BLUETOOTH_ERROR_NONE;
1509 case ADAPTER_DISCOVERY_STOPPING: {
1510 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1511 adapter_discovery_state);
1513 result = enable ? BLUETOOTH_ERROR_DEVICE_BUSY :
1514 BLUETOOTH_ERROR_NOT_IN_OPERATION;
1519 BT_ERR("Unknown state: %d", adapter_discovery_state);
1527 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1529 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1530 GVariant *param = NULL;
1531 int result = BLUETOOTH_ERROR_NONE;
1533 switch (bt_discovery_status) {
1534 case ADAPTER_DISCOVERY_STOPPED:
1536 __bt_adapter_update_discovery_status(bt_discovery_status);
1537 param = g_variant_new("(i)", result);
1538 _bt_send_event(BT_ADAPTER_EVENT,
1539 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1543 case ADAPTER_DISCOVERY_STARTED:
1545 __bt_adapter_update_discovery_status(bt_discovery_status);
1546 param = g_variant_new("(i)", result);
1547 _bt_send_event(BT_ADAPTER_EVENT,
1548 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1553 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1557 static void __bt_set_visible_mode(void)
1560 #ifdef TIZEN_FEATURE_BT_DPM
1561 int discoverable_state = DPM_BT_ERROR;
1564 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1565 BT_ERR("Fail to get the timeout value");
1567 #ifdef TIZEN_FEATURE_BT_DPM
1568 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1569 if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
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");
1584 if (timeout == -1) {
1585 if (_bt_set_discoverable_mode(
1586 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1587 timeout) != BLUETOOTH_ERROR_NONE) {
1588 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1589 BT_ERR("Set vconf failed");
1592 if (_bt_set_discoverable_mode(
1593 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1594 timeout) != BLUETOOTH_ERROR_NONE) {
1595 BT_ERR("Set connectable mode failed");
1601 static void __bt_set_local_name(void)
1603 char *phone_name = NULL;
1606 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1611 if (strlen(phone_name) != 0) {
1612 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1615 _bt_set_local_name(phone_name);
1620 void _bt_adapter_set_status(bt_status_t status)
1622 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1623 adapter_state = status;
1626 bt_status_t _bt_adapter_get_status(void)
1628 return adapter_state;
1631 void _bt_set_disabled(int result)
1633 int power_off_status = 0;
1636 int pm_ignore_mode = 0;
1638 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1639 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1641 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1643 /* Update the vconf BT status in normal Deactivation case only */
1644 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1645 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1647 BT_DBG("Update vconf for BT normal Deactivation");
1649 if (result == BLUETOOTH_ERROR_TIMEOUT)
1650 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1651 BT_ERR("Set vconf failed");
1653 /* Update Bluetooth Status to notify other modules */
1654 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1655 BT_ERR("Set vconf failed");
1657 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1658 EVT_VAL_BT_OFF) != ES_R_OK)
1659 BT_ERR("Fail to set value");
1662 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1663 BT_ERR("Set vconf failed\n");
1665 _bt_adapter_set_status(BT_DEACTIVATED);
1666 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1668 BT_INFO("Adapter disabled");