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;
78 static guint le_timer_id = 0;
80 static gboolean a2dp_init_pending = FALSE;
81 static GDBusProxy *core_proxy = NULL;
83 #define BT_CORE_NAME "org.projectx.bt_core"
84 #define BT_CORE_PATH "/org/projectx/bt_core"
85 #define BT_CORE_INTERFACE "org.projectx.btcore"
87 /* Adapter default states */
88 static bt_status_t adapter_state = BT_DEACTIVATED;
89 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
91 /* Forward declarations */
92 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
93 static void __bt_post_oal_init(void);
94 static void __bt_handle_oal_initialisation(oal_event_t event);
95 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
96 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
97 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
98 static void __bt_adapter_update_bt_enabled(void);
99 static void __bt_adapter_update_bt_disabled(void);
100 static void __bt_adapter_state_set_status(bt_status_t status);
101 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
102 static void __bt_adapter_state_change_callback(int bt_status);
103 static int __bt_adapter_state_handle_request(gboolean enable);
104 static int __bt_adapter_state_discovery_request(gboolean enable,
105 unsigned short max_response, unsigned short duration, unsigned int mask,
106 gboolean is_custom, bt_discovery_role_type_t role);
107 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
108 static gboolean __bt_is_service_request_present(int service_function);
110 static void __bt_set_visible_mode(void);
111 static void __bt_set_local_name(void);
112 void _bt_adapter_start_enable_timer(void);
113 void _bt_adapter_start_le_enable_timer(void);
115 GDBusProxy *_bt_init_core_proxy(void)
118 GDBusConnection *conn;
120 conn = _bt_gdbus_get_system_gconn();
124 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
139 static GDBusProxy *__bt_get_core_proxy(void)
141 return (core_proxy) ? core_proxy : _bt_init_core_proxy();
145 /* Initialize BT stack (Initialize OAL layer) */
146 int _bt_stack_init(void)
150 BT_INFO("[bt-service] Start to initialize BT stack");
151 /* Adapter enable request is successful, setup event handlers */
152 _bt_service_register_event_handler_callback(
153 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
156 if (ret != BLUETOOTH_ERROR_NONE) {
157 BT_ERR("_bt_le_init Failed");
161 ret = oal_bt_init(_bt_service_oal_event_receiver);
163 if (OAL_STATUS_PENDING == ret) {
164 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
165 return BLUETOOTH_ERROR_NONE;
166 } else if (OAL_STATUS_SUCCESS != ret) {
167 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
168 return BLUETOOTH_ERROR_INTERNAL;
171 return BLUETOOTH_ERROR_NONE;
174 int _bt_enable_adapter(void)
176 return __bt_adapter_state_handle_request(TRUE);
179 int _bt_enable_core(void)
181 BT_INFO("Implemented");
184 GError *error = NULL;
186 proxy = __bt_get_core_proxy();
187 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
189 /* Clean up the process */
190 result = g_dbus_proxy_call_sync(proxy,
193 G_DBUS_CALL_FLAGS_NONE,
200 BT_ERR("Bt core call failed(Error: %s)", error->message);
201 g_clear_error(&error);
203 BT_ERR("Bt core call failed");
204 return BLUETOOTH_ERROR_INTERNAL;
207 g_variant_unref(result);
208 return BLUETOOTH_ERROR_NONE;
211 int _bt_recover_adapter(void)
213 /* TODO_40 : 4.0 merge */
214 BT_INFO("Not Supported");
215 return BLUETOOTH_ERROR_NOT_SUPPORT;
218 int _bt_reset_adapter(void)
221 if (OAL_STATUS_SUCCESS != adapter_reset())
222 return BLUETOOTH_ERROR_INTERNAL;
224 /* TODO_40 : 4.0 merge */
225 /* TODO Currently bt-service is not terminated in tizen next.
226 It should be handled in future patch */
227 if (_bt_adapter_get_status() == BT_DEACTIVATED)
228 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
230 return BLUETOOTH_ERROR_NONE;
233 void _bt_set_le_disabled(int result)
235 int power_off_status;
238 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
239 BT_DBG("ret : %d", ret);
240 BT_DBG("power_off_status : %d", power_off_status);
242 /* Update Bluetooth Status to notify other modules */
243 BT_DBG("Update vconf for BT LE normal Deactivation");
244 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
245 BT_ERR("Set vconf failed\n");
246 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
248 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
249 EVT_VAL_BT_LE_OFF) != ES_R_OK)
250 BT_ERR("Fail to set value");
252 /* Send disabled event */
253 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
254 g_variant_new("(i)", result));
257 int _bt_check_adapter(int *status)
260 gboolean powered = FALSE;
261 BT_CHECK_PARAMETER(status, return);
263 *status = BT_ADAPTER_DISABLED;
265 if (OAL_STATUS_SUCCESS != adapter_get_powered_status(&powered))
266 return BLUETOOTH_ERROR_INTERNAL;
268 BT_INFO("Adapter is in Powered Up state");
269 *status = BT_ADAPTER_ENABLED;
271 BT_INFO("Adapter is in Powered Down state");
273 return BLUETOOTH_ERROR_NONE;
276 int _bt_disable_adapter(void)
278 return __bt_adapter_state_handle_request(FALSE);
281 int _bt_start_discovery(unsigned short max_response,
282 unsigned short duration, unsigned int cod_mask)
284 return __bt_adapter_state_discovery_request(TRUE, max_response, duration,
285 cod_mask, FALSE, 0x00);
288 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
290 return __bt_adapter_state_discovery_request(TRUE, 0, 0, 0, TRUE, role);
293 int _bt_cancel_discovery(void)
295 return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0, FALSE, 0x00);
298 gboolean _bt_is_discovering(void)
300 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
301 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
307 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
309 gboolean flight_mode = FALSE;
312 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
313 type = vconf_keynode_get_type(node);
314 if (type == VCONF_TYPE_BOOL) {
315 flight_mode = vconf_keynode_get_bool(node);
316 if (flight_mode != TRUE) {
317 BT_ERR("Ignore the event");
321 BT_ERR("Flight Mode == TRUE");
324 BT_ERR("Invaild vconf key type : %d", type);
331 void _bt_service_register_vconf_handler(void)
335 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
336 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
337 BT_ERR("Unable to register key handler");
338 BT_DBG("Telephony is disabled");
339 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
340 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
341 BT_ERR("Unable to register key handler");
344 void _bt_service_unregister_vconf_handler(void)
348 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
349 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
351 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
352 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
355 static gboolean __bt_enable_timeout_cb(gpointer user_data)
359 GError *error = NULL;
363 retv_if(_bt_adapter_get_status() == BT_ACTIVATED, FALSE);
365 BT_ERR("EnableAdapter is failed");
367 proxy = __bt_get_core_proxy();
371 /* Clean up the process */
372 result = g_dbus_proxy_call_sync(proxy,
375 G_DBUS_CALL_FLAGS_NONE,
382 BT_ERR("Bt core call failed(Error: %s)", error->message);
383 g_clear_error(&error);
385 BT_ERR("Bt core call failed");
390 g_variant_unref(result);
391 _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
393 #ifndef USB_BLUETOOTH
394 _bt_terminate_service(NULL);
400 void _bt_adapter_start_enable_timer(void)
403 g_source_remove(timer_id);
407 timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
408 __bt_enable_timeout_cb, NULL);
413 static gboolean __bt_enable_le_timeout_cb(gpointer user_data)
417 GError *error = NULL;
421 retv_if(_bt_adapter_get_le_status() == BT_LE_ACTIVATED, FALSE);
423 BT_ERR("EnableAdapterLE is failed");
425 proxy = __bt_get_core_proxy();
429 /* Clean up the process */
430 result = g_dbus_proxy_call_sync(proxy,
433 G_DBUS_CALL_FLAGS_NONE,
440 BT_ERR("Bt core call failed(Error: %s)", error->message);
441 g_clear_error(&error);
443 BT_ERR("Bt core call failed");
447 g_variant_unref(result);
448 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
450 _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
452 if (_bt_adapter_get_status() == BT_DEACTIVATED)
453 _bt_terminate_service(NULL);
458 void _bt_adapter_start_le_enable_timer(void)
460 if (le_timer_id > 0) {
461 g_source_remove(le_timer_id);
465 le_timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
466 __bt_enable_le_timeout_cb, NULL);
471 int _bt_get_local_address(void)
477 result = adapter_get_address();
478 if (result != OAL_STATUS_SUCCESS) {
479 BT_ERR("adapter_get_address failed: %d", result);
480 result = BLUETOOTH_ERROR_INTERNAL;
482 result = BLUETOOTH_ERROR_NONE;
488 int _bt_get_local_version(void)
493 result = adapter_get_version();
494 if (result != OAL_STATUS_SUCCESS) {
495 BT_ERR("adapter_get_version failed: %d", result);
496 result = BLUETOOTH_ERROR_INTERNAL;
498 result = BLUETOOTH_ERROR_NONE;
504 int _bt_get_local_name(void)
510 result = adapter_get_name();
511 if (result != OAL_STATUS_SUCCESS) {
512 BT_ERR("adapter_get_name failed: %d", result);
513 result = BLUETOOTH_ERROR_INTERNAL;
515 result = BLUETOOTH_ERROR_NONE;
521 int _bt_set_local_name(char *local_name)
523 int result = BLUETOOTH_ERROR_NONE;
526 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
528 result = adapter_set_name(local_name);
529 if (result != OAL_STATUS_SUCCESS) {
530 BT_ERR("adapter_set_name failed: %d", result);
531 result = BLUETOOTH_ERROR_INTERNAL;
533 result = BLUETOOTH_ERROR_NONE;
539 int _bt_get_discoverable_mode(int *mode)
546 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
548 adapter_is_discoverable(&scan_mode);
549 if (TRUE == scan_mode) {
550 adapter_get_discoverable_timeout(&timeout);
552 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
554 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
556 adapter_is_connectable(&scan_mode);
557 if (scan_mode == TRUE)
558 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
561 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
562 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
563 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
570 return BLUETOOTH_ERROR_NONE;
573 int _bt_get_timeout_value(int *timeout)
578 /* Take current time */
580 time_diff = difftime(current_time, visible_timer.start_time);
582 BT_DBG("Time diff = %d\n", time_diff);
583 *timeout = visible_timer.timeout - time_diff;
585 return BLUETOOTH_ERROR_NONE;
588 static void __bt_visibility_alarm_remove()
590 if (visible_timer.event_id > 0) {
591 g_source_remove(visible_timer.event_id);
592 visible_timer.event_id = 0;
595 if (visible_timer.alarm_id > 0) {
596 alarmmgr_remove_alarm(visible_timer.alarm_id);
597 visible_timer.alarm_id = 0;
601 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
603 int result = BLUETOOTH_ERROR_NONE;
606 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
608 if (alarm_id != visible_timer.alarm_id)
611 if (visible_timer.event_id) {
612 _bt_send_event(BT_ADAPTER_EVENT,
613 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
614 g_variant_new("(in)", result, timeout));
615 g_source_remove(visible_timer.event_id);
616 visible_timer.event_id = 0;
617 visible_timer.timeout = 0;
619 if (!TIZEN_PROFILE_WEARABLE) {
620 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
621 BT_ERR("Set vconf failed\n");
624 /* Switch Off visibility in Bluez */
625 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
626 visible_timer.alarm_id = 0;
630 static gboolean __bt_timeout_handler(gpointer user_data)
632 int result = BLUETOOTH_ERROR_NONE;
636 /* Take current time */
638 time_diff = difftime(current_time, visible_timer.start_time);
640 /* Send event to application */
641 _bt_send_event(BT_ADAPTER_EVENT,
642 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
643 g_variant_new("(in)", result, time_diff));
645 if (visible_timer.timeout <= time_diff) {
646 g_source_remove(visible_timer.event_id);
647 visible_timer.event_id = 0;
648 visible_timer.timeout = 0;
650 if (!TIZEN_PROFILE_WEARABLE) {
651 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
652 BT_ERR("Set vconf failed\n");
661 static void __bt_visibility_alarm_create()
666 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
669 BT_ERR("Failed to create alarm error = %d\n", result);
671 BT_DBG("Alarm created = %d\n", alarm_id);
672 visible_timer.alarm_id = alarm_id;
676 int _bt_start_visibility_timer(int timeout)
679 #ifdef TIZEN_FEATURE_BT_DPM
680 int discoverable_state = DPM_BT_ERROR;
683 __bt_visibility_alarm_remove();
685 visible_timer.timeout = timeout;
687 #ifdef TIZEN_FEATURE_BT_DPM
688 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
689 if (discoverable_state != DPM_RESTRICTED) {
691 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
692 BT_ERR("Set vconf failed");
693 #ifdef TIZEN_FEATURE_BT_DPM
698 return BLUETOOTH_ERROR_NONE;
700 if (!visible_timer.alarm_init) {
701 /* Set Alarm timer to switch off BT */
702 result = alarmmgr_init("bt-service");
704 return BLUETOOTH_ERROR_INTERNAL;
706 visible_timer.alarm_init = TRUE;
709 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
711 return BLUETOOTH_ERROR_INTERNAL;
713 if (!TIZEN_PROFILE_WEARABLE) {
714 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
715 BT_ERR("Set vconf failed");
718 /* Take start time */
719 time(&(visible_timer.start_time));
720 visible_timer.event_id = g_timeout_add_seconds(1,
721 __bt_timeout_handler, NULL);
723 visible_timer.timeout = timeout;
725 __bt_visibility_alarm_create();
727 return BLUETOOTH_ERROR_NONE;
730 int _bt_stop_visibility_timer(void)
732 __bt_visibility_alarm_remove();
734 visible_timer.timeout = 0;
736 if (!TIZEN_PROFILE_WEARABLE) {
737 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
738 BT_ERR("Set vconf failed");
741 return BLUETOOTH_ERROR_NONE;
744 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
747 int ret = BLUETOOTH_ERROR_NONE;;
748 #ifdef TIZEN_FEATURE_BT_DPM
749 int discoverable_state = DPM_BT_ERROR;
754 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
756 #ifdef TIZEN_FEATURE_BT_DPM
757 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
758 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
759 discoverable_state == DPM_RESTRICTED) {
760 if (headed_plugin_info->plugin_headed_enabled)
761 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
762 return BLUETOOTH_ERROR_ACCESS_DENIED;
764 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE &&
765 discoverable_state == DPM_RESTRICTED) {
766 if (headed_plugin_info->plugin_headed_enabled)
767 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
768 return BLUETOOTH_ERROR_ACCESS_DENIED;
772 switch (discoverable_mode) {
773 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
774 result = adapter_set_connectable(TRUE);
777 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
778 result = adapter_set_discoverable();
781 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
782 result = adapter_set_discoverable();
785 return BLUETOOTH_ERROR_INVALID_PARAM;
788 if (result != OAL_STATUS_SUCCESS) {
789 BT_ERR("set scan mode failed %d", result);
790 return BLUETOOTH_ERROR_INTERNAL;
793 result = adapter_set_discoverable_timeout(timeout);
794 if (result != OAL_STATUS_SUCCESS) {
795 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
796 return BLUETOOTH_ERROR_INTERNAL;
799 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
800 ret = _bt_stop_visibility_timer();
801 if (BLUETOOTH_ERROR_NONE != ret)
802 BT_ERR("_bt_stop_visibility_timer failed");
803 if (!TIZEN_PROFILE_WEARABLE) {
804 if (vconf_set_int(BT_FILE_VISIBLE_TIME, -1) != 0)
805 BT_ERR("Set vconf failed");
807 } else if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE) {
808 ret = _bt_start_visibility_timer(timeout);
809 if (BLUETOOTH_ERROR_NONE != ret)
810 BT_ERR("_bt_start_visibility_timer failed");
813 ret = _bt_stop_visibility_timer();
814 if (BLUETOOTH_ERROR_NONE != ret)
815 BT_ERR("_bt_stop_visibility_timer failed");
822 gboolean _bt_is_connectable(void)
829 adapter_is_connectable(&connectable);
835 BT_DBG("Connectable: [%s]", result ? "TRUE" : "FALSE");
840 int _bt_set_connectable(gboolean connectable)
842 int result = BLUETOOTH_ERROR_NONE;
845 result = adapter_set_connectable(connectable);
846 if (result != OAL_STATUS_SUCCESS) {
847 BT_ERR("adapter_set_connectable failed: %d", result);
848 result = BLUETOOTH_ERROR_INTERNAL;
850 result = BLUETOOTH_ERROR_NONE;
856 int _bt_is_service_used(void)
862 result = adapter_get_service_uuids();
863 if (result != OAL_STATUS_SUCCESS) {
864 BT_ERR("adapter_get_service_uuids failed: %d", result);
865 result = BLUETOOTH_ERROR_INTERNAL;
867 result = BLUETOOTH_ERROR_NONE;
874 int _bt_adapter_get_bonded_devices(void)
876 int result = BLUETOOTH_ERROR_NONE;
879 result = adapter_get_bonded_devices();
880 if (result != OAL_STATUS_SUCCESS) {
881 BT_ERR("adapter_get_bonded_devices failed: %d", result);
882 result = BLUETOOTH_ERROR_INTERNAL;
884 result = BLUETOOTH_ERROR_NONE;
890 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
893 GDBusConnection *conn;
894 GDBusProxy *manager_proxy;
895 GVariant *result = NULL;
896 GVariant *result1 = NULL;
897 GVariantIter *iter = NULL;
898 GError *error = NULL;
899 char *object_path = NULL;
900 GVariantIter *interface_iter;
901 char *interface_str = NULL;
902 GDBusProxy *device_proxy = NULL;
903 gboolean is_connected = FALSE;
905 conn = _bt_gdbus_get_system_gconn();
906 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
908 manager_proxy = _bt_get_manager_proxy();
909 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
911 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
913 G_DBUS_CALL_FLAGS_NONE,
920 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
921 g_clear_error(&error);
924 BT_ERR("Failed to Failed to GetManagedObjects");
925 return BLUETOOTH_ERROR_INTERNAL;
928 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
929 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
931 /* Parse the signature: oa{sa{sv}}} */
932 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
933 if (object_path == NULL)
936 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
937 &interface_str, NULL)) {
938 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
939 BT_DBG("Found a device: %s", object_path);
940 g_free(interface_str);
942 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
944 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
946 if (device_proxy == NULL) {
947 BT_DBG("Device don't have this service");
951 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
952 g_variant_new("(s)", profile_uuid),
953 G_DBUS_CALL_FLAGS_NONE,
958 if (result1 == NULL) {
959 BT_ERR("Error occured in Proxy call");
961 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
965 g_object_unref(device_proxy);
968 g_variant_get(result1, "(b)", &is_connected);
970 if (is_connected == TRUE) {
971 char address[BT_ADDRESS_STRING_SIZE];
972 bluetooth_device_address_t *addr = NULL;
974 _bt_convert_device_path_to_address(object_path, address);
976 addr = g_malloc0(sizeof(bluetooth_device_address_t));
977 _bt_convert_addr_string_to_type(addr->addr, address);
979 g_array_append_vals(*addr_list, addr,
980 sizeof(bluetooth_device_address_t));
983 g_variant_unref(result1);
984 g_object_unref(device_proxy);
991 g_variant_unref(result);
992 g_variant_iter_free(iter);
995 return BLUETOOTH_ERROR_NONE;
998 static void __bt_handle_pending_a2dp_init(service_uuid_t *service_list, unsigned int count)
1002 unsigned char *uuid;
1003 char uuid_str[BT_UUID_STRING_SIZE];
1005 if (!a2dp_init_pending)
1009 a2dp_init_pending = FALSE;
1010 for (i = 0; i < count; i++) {
1011 uuid = service_list[i].uuid;
1012 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1013 BT_INFO("Adapter Service: [%s]", uuid_str);
1014 if (!strcasecmp(uuid_str, A2DP_SINK_UUID)) {
1015 BT_INFO("Enable A2DP Sink role");
1016 /* Initialize A2DP Sink */
1017 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
1018 if (ret != BLUETOOTH_ERROR_NONE)
1019 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
1021 /* Initialize AVRCP Controller */
1022 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
1023 if (ret != BLUETOOTH_ERROR_NONE)
1024 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
1026 _bt_audio_set_current_role(BLUETOOTH_A2DP_SINK);
1031 BT_INFO("Enable A2DP Source role by default");
1032 /* Initialize A2DP Source */
1033 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
1034 if (ret != BLUETOOTH_ERROR_NONE)
1035 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
1037 /* Initialize AVRCP Target */
1038 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
1039 if (ret != BLUETOOTH_ERROR_NONE)
1040 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
1042 _bt_audio_set_current_role(BLUETOOTH_A2DP_SOURCE);
1046 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
1048 int result = BLUETOOTH_ERROR_NONE;
1052 switch (event_type) {
1053 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1054 case OAL_EVENT_OAL_INITIALISED_FAILED:
1055 __bt_handle_oal_initialisation(event_type);
1057 case OAL_EVENT_ADAPTER_ENABLED:
1058 __bt_adapter_state_change_callback(BT_ACTIVATED);
1060 case OAL_EVENT_ADAPTER_DISABLED:
1061 __bt_adapter_state_change_callback(BT_DEACTIVATED);
1063 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
1064 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
1066 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
1067 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
1069 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
1070 bt_address_t *bd_addr = event_data;
1071 bluetooth_device_address_t local_address;
1074 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
1075 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
1076 local_address.addr[0], local_address.addr[1], local_address.addr[2],
1077 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
1079 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
1080 (void *) &local_address, sizeof(bluetooth_device_address_t));
1083 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
1084 char *name = event_data;
1085 BT_DBG("Adapter Name: %s", name);
1087 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
1088 bluetooth_device_name_t local_name;
1090 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
1091 g_strlcpy(local_name.name,
1092 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
1093 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
1094 (void *) &local_name, sizeof(bluetooth_device_name_t));
1096 /* Send event to application */
1097 _bt_send_event(BT_ADAPTER_EVENT,
1098 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
1099 g_variant_new("(is)", result, name));
1103 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
1104 char *ver = event_data;
1105 bluetooth_version_t local_version;
1107 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
1108 g_strlcpy(local_version.version,
1109 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
1110 BT_DBG("BT Version: %s", local_version.version);
1112 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
1113 (void *) &local_version, sizeof(bluetooth_version_t));
1116 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
1118 gboolean connectable = FALSE;
1120 BT_INFO("Adapter discoverable mode:"
1121 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
1122 _bt_send_event(BT_ADAPTER_EVENT,
1123 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1124 g_variant_new("(b)", connectable));
1126 _bt_send_event(BT_ADAPTER_EVENT,
1127 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1128 g_variant_new("(in)", result, mode));
1131 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
1133 gboolean connectable = TRUE;
1135 BT_INFO("Adapter discoverable mode:"
1136 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
1137 _bt_send_event(BT_ADAPTER_EVENT,
1138 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1139 g_variant_new("(b)", connectable));
1141 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1142 _bt_send_event(BT_ADAPTER_EVENT,
1143 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1144 g_variant_new("(in)", result, mode));
1147 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
1150 BT_INFO("Adapter discoverable mode:"
1151 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
1153 /* Send event to application */
1154 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1155 _bt_send_event(BT_ADAPTER_EVENT,
1156 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1157 g_variant_new("(in)", result, mode));
1161 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
1162 int *timeout = event_data;
1165 BT_INFO("Discoverable timeout: [%d]", *timeout);
1167 /* Send event to application */
1168 _bt_get_discoverable_mode(&mode);
1169 _bt_send_event(BT_ADAPTER_EVENT,
1170 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1171 g_variant_new("(in)", result, mode));
1174 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
1176 service_uuid_t *service_list;
1177 event_adapter_services_t *list = event_data;
1180 service_list = list->service_list;
1181 __bt_handle_pending_a2dp_init(service_list, count);
1182 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
1185 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
1188 bluetooth_device_address_t *addr_list;
1190 event_device_list_t *bonded_device_list = event_data;
1191 count = bonded_device_list->num;
1193 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
1194 for (i = 0; i < count; i++) {
1195 memcpy(addr_list[i].addr,
1196 bonded_device_list->devices[i].addr,
1197 BLUETOOTH_ADDRESS_LENGTH);
1200 BT_INFO("Adapter Bonded device List count: [%d]", count);
1201 _bt_device_handle_paired_address_list(addr_list, bonded_device_list->num);
1206 BT_ERR("Unhandled event..");
1213 int _bt_init_profiles()
1218 /*TODO: Init bluetooth profiles */
1219 ret = _bt_hidhost_initialize();
1220 if (ret != BLUETOOTH_ERROR_NONE)
1221 BT_ERR("_bt_hidhost_initialize Failed");
1223 ret = _bt_socket_init();
1224 if (ret != BLUETOOTH_ERROR_NONE)
1225 BT_ERR("_bt_socket_init Failed");
1228 * Query local adapter services and based on a2dp service uuids initialized
1229 * in bluetooth stack, enable A2DP sourec or A2DP sink role.
1231 ret = adapter_get_service_uuids();
1232 if (ret != OAL_STATUS_SUCCESS)
1233 BT_ERR("adapter_get_service_uuids failed: %d", ret);
1235 a2dp_init_pending = TRUE;
1238 /* Initialize HFP Audio Gateway */
1239 ret = _bt_audio_initialize(BT_AG_MODULE);
1240 if (ret != BLUETOOTH_ERROR_NONE)
1241 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
1243 /* Registering callback for receiving audio services searched */
1244 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
1245 if (ret != BLUETOOTH_ERROR_NONE)
1246 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
1248 ret = _bt_hdp_init();
1249 if (ret != BLUETOOTH_ERROR_NONE)
1250 BT_ERR("_bt_hdp_init Failed");
1252 ret = _bt_gatt_init();
1253 if (ret != BLUETOOTH_ERROR_NONE)
1254 BT_ERR("_bt_gatt_init Failed");
1256 return BLUETOOTH_ERROR_NONE;
1259 int _bt_cleanup_profiles(void)
1261 /* TODO: Cleanup bluetooth profiles */
1262 _bt_hidhost_deinitialize();
1263 _bt_socket_deinit();
1265 /* TODO: Cleanup bluetooth audio profiles */
1266 //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
1267 //_bt_audio_deinitialize(BT_AVRCP_MODULE);
1268 //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
1269 //_bt_audio_deinitialize(BT_AG_MODULE);
1270 //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
1271 //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
1276 return BLUETOOTH_ERROR_NONE;
1279 /* OAL post initialization handler */
1280 static void __bt_post_oal_init(void)
1283 int status = VCONFKEY_BT_STATUS_OFF;
1285 BT_DBG("OAL initialized");
1286 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0)
1287 BT_ERR("Fail to get the enabled value");
1290 /* Update Bluetooth Status to OFF */
1291 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1292 BT_ERR("Set vconf failed\n");
1295 if (status & VCONFKEY_BT_STATUS_ON) {
1296 ret = _bt_enable_adapter();
1297 if (ret != BLUETOOTH_ERROR_NONE)
1298 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
1302 /* OAL initialization handler */
1303 static void __bt_handle_oal_initialisation(oal_event_t event)
1308 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1309 __bt_post_oal_init();
1311 case OAL_EVENT_OAL_INITIALISED_FAILED:
1312 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1313 g_idle_add(_bt_terminate_service, NULL);
1316 BT_ERR("Unknown Event");
1321 static gboolean __bt_is_service_request_present(int service_function)
1324 invocation_info_t *req_info;
1328 /* Get method invocation context */
1329 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1331 if (req_info && req_info->service_function == service_function)
1339 /* Internal functions of core adapter service */
1340 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1344 invocation_info_t *req_info;
1347 /* Get method invocation context */
1348 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1350 if (req_info == NULL || req_info->service_function != service_function)
1353 /* Create out param */
1354 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1356 switch (service_function) {
1357 case BT_ENABLE_ADAPTER:
1358 case BT_DISABLE_ADAPTER: {
1359 gboolean done = TRUE;
1360 g_array_append_vals(out_param, &done, sizeof(gboolean));
1363 case BT_GET_LOCAL_NAME:
1364 case BT_GET_LOCAL_ADDRESS:
1365 case BT_GET_LOCAL_VERSION:
1366 g_array_append_vals(out_param, user_data, size);
1368 case BT_IS_SERVICE_USED: {
1370 gboolean used = FALSE;
1371 unsigned char *uuid;
1372 char uuid_str[BT_UUID_STRING_SIZE];
1373 char *request_uuid = req_info->user_data;
1374 service_uuid_t *service_list = user_data;
1376 BT_INFO("Check for service uuid: %s", request_uuid);
1377 for (i = 0; i < size; i++) {
1378 uuid = service_list[i].uuid;
1379 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1380 BT_INFO("Adapter Service: [%s]", uuid_str);
1381 if (strcasecmp(uuid_str, request_uuid) == 0) {
1382 BT_INFO("UUID matched!!");
1388 g_array_append_vals(out_param, &used, sizeof(gboolean));
1392 BT_ERR("Unknown service function[%d]", service_function);
1395 _bt_service_method_return(req_info->context, out_param, req_info->result);
1396 g_array_free(out_param, TRUE);
1397 /* Now free invocation info for this request*/
1398 _bt_free_info_from_invocation_list(req_info);
1402 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1404 char *phone_name = NULL;
1410 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1411 phone_name = vconf_keynode_get_str(node);
1413 if (phone_name && strlen(phone_name) != 0) {
1414 if (!g_utf8_validate(phone_name, -1,
1415 (const char **)&ptr))
1418 _bt_set_local_name(phone_name);
1423 /* Request return handlings */
1424 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1426 BT_INFO("__bt_adapter_post_set_enabled>>");
1428 if (TIZEN_PROFILE_TV || !headed_plugin_info->plugin_headed_enabled) {
1429 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1430 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1431 BT_ERR("Fail to set discoverable mode");
1433 __bt_set_visible_mode();
1435 /* add the vconf noti handler */
1436 if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1437 (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1438 BT_ERR("DEVICE_NAME key changed notification registration failed");
1440 __bt_set_local_name();
1443 /* Get All properties */
1444 if (OAL_STATUS_SUCCESS != adapter_get_properties())
1445 BT_ERR("adapter_get_properties failed");
1447 /* Add Adapter enabled post processing codes */
1451 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1453 BT_INFO("_bt_adapter_post_set_disabled>>");
1455 if (!TIZEN_PROFILE_TV) {
1456 /* Add Adapter disabled post processing codes */
1457 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1458 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1459 BT_ERR("vconf_ignore_key_changed failed");
1462 /* bt-service should be terminated when BT is off */
1463 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1464 /* TODO: Implement to check if it is the recovery mode or not */
1465 _bt_reliable_terminate_service(NULL);
1471 static void __bt_adapter_update_bt_enabled(void)
1473 int result = BLUETOOTH_ERROR_NONE;
1475 BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1476 if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1477 BT_ERR("Bluetooth profile init failed");
1479 _bt_device_handle_adapter_state(TRUE);
1481 /* Update Bluetooth Status to notify other modules */
1482 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1483 BT_ERR("Set vconf failed\n");
1485 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1486 BT_ERR("Set vconf failed\n");
1488 /* TODO:Add timer function to handle any further post processing */
1489 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1491 /*Return BT_ADAPTER_ENABLE Method invocation context */
1492 __bt_adapter_handle_pending_requests(BT_ENABLE_ADAPTER, NULL, 0);
1493 /*Send BT Enabled event to application */
1494 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1495 g_variant_new("(i)", result));
1498 static void __bt_adapter_update_bt_disabled(void)
1500 int result = BLUETOOTH_ERROR_NONE;
1501 int power_off_status = 0;
1504 BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1505 _bt_cleanup_profiles();
1507 _bt_device_handle_adapter_state(FALSE);
1509 /* Update the vconf BT status in normal Deactivation case only */
1510 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1511 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1513 /* Update Bluetooth Status to notify other modules */
1514 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1515 BT_ERR("Set vconf failed");
1517 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1518 BT_ERR("Set vconf failed\n");
1520 /* TODO:Add timer function to handle any further post processing */
1521 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1523 /* Return BT_ADAPTER_DISABLE Method invocation context */
1524 __bt_adapter_handle_pending_requests(BT_DISABLE_ADAPTER, NULL, 0);
1526 /* Send BT Disabled event to application */
1527 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1528 g_variant_new("(i)", result));
1531 static void __bt_adapter_state_set_status(bt_status_t status)
1533 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1534 adapter_state = status;
1537 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1539 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1540 adapter_discovery_state = status;
1543 static void __bt_adapter_state_change_callback(int bt_status)
1545 BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
1547 switch (bt_status) {
1548 case BT_DEACTIVATED:
1549 __bt_adapter_state_set_status(bt_status);
1551 /* Adapter is disabled, unregister event handlers */
1552 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1553 //_bt_deinit_device_event_handler();
1555 /* Add Adapter disabled post processing codes */
1556 __bt_adapter_update_bt_disabled();
1559 __bt_adapter_state_set_status(bt_status);
1560 /* Add Adapter enabled post processing codes */
1562 BT_DBG("g_source is removed");
1563 g_source_remove(timer_id);
1566 __bt_adapter_update_bt_enabled();
1569 BT_ERR("Incorrect Bluetooth adapter state changed status");
1574 static int __bt_adapter_state_handle_request(gboolean enable)
1576 int result = BLUETOOTH_ERROR_NONE;
1579 switch (adapter_state) {
1580 case BT_ACTIVATING: {
1581 BT_INFO("Adapter is currently in activating state, state [%d]",
1584 return BLUETOOTH_ERROR_IN_PROGRESS;
1586 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1587 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1588 /*TODO Stop Discovery*/
1589 __bt_adapter_update_discovery_status(FALSE);
1591 result = adapter_disable();
1592 if (result != OAL_STATUS_SUCCESS) {
1593 BT_ERR("adapter_enable failed: [%d]", result);
1594 result = BLUETOOTH_ERROR_INTERNAL;
1595 /*TODO: perform if anything more needs to be done to handle failure */
1597 /* TODO: To be handled */
1598 __bt_adapter_state_set_status(BT_DEACTIVATING);
1599 result = BLUETOOTH_ERROR_NONE;
1604 case BT_ACTIVATED: {
1605 BT_INFO("Adapter is currently in activated state, state [%d]",
1608 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1610 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1611 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1612 /*TODO Stop Discovery*/
1613 __bt_adapter_update_discovery_status(FALSE);
1615 result = adapter_disable();
1616 if (result != OAL_STATUS_SUCCESS) {
1617 BT_ERR("adapter_enable failed: [%d]", result);
1618 result = BLUETOOTH_ERROR_INTERNAL;
1619 /*TODO: perform if anything more needs to be done to handle failure */
1621 /* TODO: To be handled */
1622 __bt_adapter_state_set_status(BT_DEACTIVATING);
1623 result = BLUETOOTH_ERROR_NONE;
1628 case BT_DEACTIVATING: {
1629 BT_INFO("Adapter is currently in deactivating state, state [%d]",
1632 return BLUETOOTH_ERROR_IN_PROGRESS;
1635 result = adapter_enable();
1636 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1637 BT_ERR("adapter_enable failed: [%d]", result);
1639 result = BLUETOOTH_ERROR_INTERNAL;
1640 /*TODO: perform if anything more needs to be done to handle failure */
1642 /* TODO: To be handled */
1643 __bt_adapter_state_set_status(BT_ACTIVATING);
1644 result = BLUETOOTH_ERROR_NONE;
1649 case BT_DEACTIVATED: {
1650 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1653 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1655 result = adapter_enable();
1656 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1657 BT_ERR("adapter_enable failed: [%d]", result);
1659 result = BLUETOOTH_ERROR_INTERNAL;
1660 /*TODO: perform if anything more needs to be done to handle failure */
1662 /* TODO: To be handled */
1663 __bt_adapter_state_set_status(BT_ACTIVATING);
1664 result = BLUETOOTH_ERROR_NONE;
1670 BT_ERR("Unknown state: %d", adapter_state);
1674 if (enable && result == BLUETOOTH_ERROR_NONE) {
1675 /* Adapter enable request is successful, setup event handlers */
1676 _bt_service_register_event_handler_callback(
1677 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1678 _bt_device_state_handle_callback_set_request();
1683 static int __bt_adapter_state_discovery_request(gboolean enable,
1684 unsigned short max_response, unsigned short duration,
1685 unsigned int mask, gboolean is_custom,
1686 bt_discovery_role_type_t role)
1688 int result = BLUETOOTH_ERROR_NONE;
1691 switch (adapter_discovery_state) {
1692 case ADAPTER_DISCOVERY_STARTED: {
1693 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1694 adapter_discovery_state);
1696 return BLUETOOTH_ERROR_IN_PROGRESS;
1698 result = adapter_stop_inquiry();
1699 if (result != OAL_STATUS_SUCCESS) {
1700 BT_ERR("Discover stop failed: %d", result);
1701 result = BLUETOOTH_ERROR_INTERNAL;
1703 BT_ERR("Stop Discovery Triggered successfully");
1704 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1705 result = BLUETOOTH_ERROR_NONE;
1710 case ADAPTER_DISCOVERY_STARTING: {
1711 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1712 adapter_discovery_state);
1714 return BLUETOOTH_ERROR_IN_PROGRESS;
1716 result = adapter_stop_inquiry();
1717 if (result != OAL_STATUS_SUCCESS) {
1718 BT_ERR("Discover stop failed: %d", result);
1719 result = BLUETOOTH_ERROR_INTERNAL;
1721 BT_ERR("Stop Discovery Triggered successfully");
1722 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1723 result = BLUETOOTH_ERROR_NONE;
1728 case ADAPTER_DISCOVERY_STOPPED: {
1729 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1730 adapter_discovery_state);
1732 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1734 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1737 result = adapter_start_inquiry(duration);
1739 result = adapter_start_custom_inquiry(role);
1741 if (result != OAL_STATUS_SUCCESS) {
1742 BT_ERR("Start Discovery failed: %d", result);
1743 result = BLUETOOTH_ERROR_INTERNAL;
1745 BT_ERR("Start Discovery Triggered successfully");
1746 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1747 result = BLUETOOTH_ERROR_NONE;
1752 case ADAPTER_DISCOVERY_STOPPING: {
1753 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1754 adapter_discovery_state);
1756 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1758 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1760 result = adapter_start_inquiry(duration);
1762 result = adapter_start_custom_inquiry(role);
1763 if (result != OAL_STATUS_SUCCESS) {
1764 BT_ERR("Start Discovery failed: %d", result);
1765 result = BLUETOOTH_ERROR_INTERNAL;
1767 BT_ERR("Start Discovery Triggered successfully");
1768 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1769 result = BLUETOOTH_ERROR_NONE;
1775 BT_ERR("Unknown state: %d", adapter_discovery_state);
1783 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1785 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1786 GVariant *param = NULL;
1787 int result = BLUETOOTH_ERROR_NONE;
1789 switch (bt_discovery_status) {
1790 case ADAPTER_DISCOVERY_STOPPED:
1792 __bt_adapter_update_discovery_status(bt_discovery_status);
1793 param = g_variant_new("(i)", result);
1794 _bt_send_event(BT_ADAPTER_EVENT,
1795 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1799 case ADAPTER_DISCOVERY_STARTED:
1801 __bt_adapter_update_discovery_status(bt_discovery_status);
1802 param = g_variant_new("(i)", result);
1803 _bt_send_event(BT_ADAPTER_EVENT,
1804 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1809 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1813 static void __bt_set_visible_mode(void)
1816 #ifdef TIZEN_FEATURE_BT_DPM
1817 int discoverable_state = DPM_BT_ERROR;
1820 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1821 BT_ERR("Fail to get the timeout value");
1823 #ifdef TIZEN_FEATURE_BT_DPM
1824 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1825 if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
1826 if (_bt_set_discoverable_mode(
1827 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1828 timeout) != BLUETOOTH_ERROR_NONE) {
1829 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1830 BT_ERR("Set vconf failed");
1833 if (_bt_set_discoverable_mode(
1834 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1835 timeout) != BLUETOOTH_ERROR_NONE) {
1836 BT_ERR("Set connectable mode failed");
1840 if (timeout == -1) {
1841 if (_bt_set_discoverable_mode(
1842 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1843 timeout) != BLUETOOTH_ERROR_NONE) {
1844 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1845 BT_ERR("Set vconf failed");
1848 if (_bt_set_discoverable_mode(
1849 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1850 timeout) != BLUETOOTH_ERROR_NONE) {
1851 BT_ERR("Set connectable mode failed");
1857 static void __bt_set_local_name(void)
1859 char *phone_name = NULL;
1862 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1867 if (strlen(phone_name) != 0) {
1868 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1871 _bt_set_local_name(phone_name);
1876 void _bt_adapter_set_status(bt_status_t status)
1878 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1879 adapter_state = status;
1882 bt_status_t _bt_adapter_get_status(void)
1884 return adapter_state;
1887 void _bt_set_disabled(int result)
1889 int power_off_status = 0;
1892 int pm_ignore_mode = 0;
1894 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1895 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1897 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1899 /* Update the vconf BT status in normal Deactivation case only */
1900 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1901 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1903 BT_DBG("Update vconf for BT normal Deactivation");
1905 if (result == BLUETOOTH_ERROR_TIMEOUT)
1906 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1907 BT_ERR("Set vconf failed");
1909 /* Update Bluetooth Status to notify other modules */
1910 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1911 BT_ERR("Set vconf failed");
1913 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1914 EVT_VAL_BT_OFF) != ES_R_OK)
1915 BT_ERR("Fail to set value");
1918 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1919 BT_ERR("Set vconf failed\n");
1921 _bt_adapter_set_status(BT_DEACTIVATED);
1922 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1924 BT_INFO("Adapter disabled");