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;
79 BT_ADAPTER_FLIGHT_MODE_NONE = -1,
80 BT_ADAPTER_FLIGHT_MODE_OFF,
81 BT_ADAPTER_FLIGHT_MODE_ON,
82 } bt_adapter_flightmode_req_e;
84 static bt_adapter_flightmode_req_e flightmode_request = BT_ADAPTER_FLIGHT_MODE_NONE;
86 static guint timer_id = 0;
88 static gboolean a2dp_init_pending = TRUE;
90 /* Adapter default states */
91 static bt_status_t adapter_state = BT_DEACTIVATED;
92 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
94 /* Forward declarations */
95 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
96 static void __bt_post_oal_init(void);
97 static void __bt_handle_oal_initialisation(oal_event_t event);
98 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
99 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
100 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
101 static void __bt_adapter_update_bt_enabled(void);
102 static void __bt_adapter_update_bt_disabled(void);
103 static void __bt_adapter_state_set_status(bt_status_t status);
104 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
105 static void __bt_adapter_state_change_callback(int bt_status);
106 static int __bt_adapter_state_handle_request(gboolean enable);
107 static int __bt_adapter_state_discovery_request(gboolean enable,
108 unsigned short max_response, unsigned short duration, unsigned int mask,
109 gboolean is_custom, bt_discovery_role_type_t role);
110 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
111 static gboolean __bt_is_service_request_present(int service_function);
113 static void __bt_set_visible_mode(void);
114 static void __bt_set_local_name(void);
116 static gboolean __bt_disconnect_all(void);
118 /* Initialize BT stack (Initialize OAL layer) */
119 int _bt_stack_init(void)
123 BT_INFO("[bt-service] Start to initialize BT stack");
124 /* Adapter enable request is successful, setup event handlers */
125 _bt_service_register_event_handler_callback(
126 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
129 if (ret != BLUETOOTH_ERROR_NONE) {
130 BT_ERR("_bt_le_init Failed");
134 ret = oal_bt_init(_bt_service_oal_event_receiver);
136 if (OAL_STATUS_PENDING == ret) {
137 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
138 return BLUETOOTH_ERROR_NONE;
139 } else if (OAL_STATUS_SUCCESS != ret) {
140 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
141 return BLUETOOTH_ERROR_INTERNAL;
144 return BLUETOOTH_ERROR_NONE;
147 int _bt_enable_adapter(void)
149 bt_le_status_t le_status = BT_LE_DEACTIVATED;
151 le_status = _bt_adapter_get_le_status();
153 BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
155 if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
156 BT_ERR("Enabling in progress");
157 return BLUETOOTH_ERROR_IN_PROGRESS;
160 if (adapter_state == BT_ACTIVATED) {
161 BT_ERR("Already enabled");
162 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
165 if (adapter_state == BT_DEACTIVATING ||
166 adapter_state == BT_TERMINATING ||
167 le_status == BT_LE_DEACTIVATING) {
168 BT_ERR("Disabling in progress");
169 return BLUETOOTH_ERROR_DEVICE_BUSY;
172 return __bt_adapter_state_handle_request(TRUE);
175 int _bt_enable_core(void)
177 /* TODO_40 : 4.0 merge */
178 BT_INFO("Not Supported");
179 return BLUETOOTH_ERROR_NOT_SUPPORT;
182 int _bt_recover_adapter(void)
184 int result = BLUETOOTH_ERROR_NONE;
186 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
187 BT_ERR("Disabling in progress");
188 return BLUETOOTH_ERROR_IN_PROGRESS;
191 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
192 BT_ERR("Already Disabled");
193 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
196 _bt_adapter_set_status(BT_DEACTIVATING);
198 result = adapter_recover();
200 if (result != OAL_STATUS_SUCCESS)
201 return BLUETOOTH_ERROR_INTERNAL;
203 __bt_disconnect_all();
208 int _bt_reset_adapter(void)
211 if (OAL_STATUS_SUCCESS != adapter_reset())
212 return BLUETOOTH_ERROR_INTERNAL;
214 /* TODO_40 : 4.0 merge */
215 /* TODO Currently bt-service is not terminated in tizen next.
216 It should be handled in future patch */
217 if (_bt_adapter_get_status() == BT_DEACTIVATED)
218 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
220 return BLUETOOTH_ERROR_NONE;
223 int _bt_check_adapter(int *status)
226 gboolean powered = FALSE;
227 BT_CHECK_PARAMETER(status, return);
229 *status = BT_ADAPTER_DISABLED;
231 if (OAL_STATUS_SUCCESS != adapter_get_powered_status(&powered))
232 return BLUETOOTH_ERROR_INTERNAL;
234 BT_INFO("Adapter is in Powered Up state");
235 *status = BT_ADAPTER_ENABLED;
237 BT_INFO("Adapter is in Powered Down state");
239 return BLUETOOTH_ERROR_NONE;
242 int _bt_disable_adapter(void)
244 bt_le_status_t le_status = BT_LE_DEACTIVATED;
246 le_status = _bt_adapter_get_le_status();
248 BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
250 if (adapter_state == BT_DEACTIVATING ||
251 adapter_state == BT_TERMINATING ||
252 le_status == BT_LE_DEACTIVATING) {
253 BT_DBG("Disabling in progress");
254 return BLUETOOTH_ERROR_IN_PROGRESS;
257 if (adapter_state == BT_DEACTIVATED) {
258 BT_DBG("Already disabled");
259 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
262 if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
263 BT_ERR("Enabling in progress");
264 return BLUETOOTH_ERROR_DEVICE_BUSY;
267 /* TODO: Need to add the function to disconnect all devices */
268 /* __bt_disconnect_all(); */
270 return __bt_adapter_state_handle_request(FALSE);
273 int _bt_start_discovery(unsigned short max_response,
274 unsigned short duration, unsigned int cod_mask)
276 return __bt_adapter_state_discovery_request(TRUE, max_response, duration,
277 cod_mask, FALSE, 0x00);
280 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
282 return __bt_adapter_state_discovery_request(TRUE, 0, 0, 0, TRUE, role);
285 int _bt_cancel_discovery(void)
287 return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0, FALSE, 0x00);
290 gboolean _bt_is_discovering(void)
292 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
293 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
299 static void __bt_set_flightmode_request(int value)
301 BT_INFO("Setting flightmode request to %d", value);
302 flightmode_request = value;
305 static int __bt_is_flightmode_request(void)
307 return flightmode_request;
310 static void __bt_service_handle_flight_mode(gboolean flight_mode, bt_status_t adapter_status)
312 int flight_mode_value = 0;
313 int ps_mode_value = 0;
315 if (flight_mode == TRUE) {
316 BT_INFO_C("### Flight mode on. Turn off BT");
318 if (adapter_status != BT_ACTIVATED) {
319 BT_INFO("No need to control bt status");
320 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
321 BT_ERR("Fail get PS mode value");
323 BT_DBG("ps_mode_value %d", ps_mode_value);
324 if (ps_mode_value > 0) {
325 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
326 BT_ERR("Set vconf failed");
331 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_ON);
333 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
334 BT_ERR("Set vconf failed");
336 if (adapter_status == BT_ACTIVATED)
337 _bt_disable_adapter();
340 BT_INFO_C("### Flight mode off. Turn on BT");
342 if (adapter_status == BT_ACTIVATED)
345 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value))
346 BT_ERR("Fail get flight mode value");
348 BT_DBG("flight_mode_value %d", flight_mode_value);
349 if (flight_mode_value == 0)
352 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
353 BT_ERR("Set vconf failed");
355 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
356 BT_ERR("Fail get PS mode value");
358 BT_DBG("ps_mode_value %d", ps_mode_value);
359 if (ps_mode_value > 0) {
360 BT_DBG("Bt should not turn on");
364 if (adapter_status == BT_DEACTIVATING) {
365 BT_INFO("BT adapter is deactivating. Turn on BT after deactivation");
366 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_OFF);
370 if (adapter_status != BT_DEACTIVATED) {
371 BT_INFO("No need to control bt status");
374 _bt_service_initialize();
376 if (adapter_status == BT_DEACTIVATED)
377 _bt_enable_adapter();
382 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
385 bt_status_t adapter_status = BT_DEACTIVATED;
386 gboolean flight_mode = FALSE;
388 adapter_status = _bt_adapter_get_status();
389 BT_INFO("Flight mode changed. Current bt status is %d", adapter_status);
391 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
392 type = vconf_keynode_get_type(node);
394 if (type == VCONF_TYPE_BOOL) {
395 flight_mode = vconf_keynode_get_bool(node);
396 __bt_service_handle_flight_mode(flight_mode, adapter_status);
398 BT_ERR("Invaild vconf key type : %d", type);
402 void _bt_service_register_vconf_handler(void)
406 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
407 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
408 BT_ERR("Unable to register key handler");
409 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
410 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
411 BT_ERR("Unable to register key handler");
414 void _bt_service_unregister_vconf_handler(void)
418 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
419 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
421 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
422 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
425 int _bt_get_local_address(void)
431 result = adapter_get_address();
432 if (result != OAL_STATUS_SUCCESS) {
433 BT_ERR("adapter_get_address failed: %d", result);
434 result = BLUETOOTH_ERROR_INTERNAL;
436 result = BLUETOOTH_ERROR_NONE;
442 int _bt_get_local_version(void)
447 result = adapter_get_version();
448 if (result != OAL_STATUS_SUCCESS) {
449 BT_ERR("adapter_get_version failed: %d", result);
450 result = BLUETOOTH_ERROR_INTERNAL;
452 result = BLUETOOTH_ERROR_NONE;
458 int _bt_get_local_name(void)
464 result = adapter_get_name();
465 if (result != OAL_STATUS_SUCCESS) {
466 BT_ERR("adapter_get_name failed: %d", result);
467 result = BLUETOOTH_ERROR_INTERNAL;
469 result = BLUETOOTH_ERROR_NONE;
475 int _bt_set_local_name(char *local_name)
477 int result = BLUETOOTH_ERROR_NONE;
480 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
482 result = adapter_set_name(local_name);
483 if (result != OAL_STATUS_SUCCESS) {
484 BT_ERR("adapter_set_name failed: %d", result);
485 result = BLUETOOTH_ERROR_INTERNAL;
487 result = BLUETOOTH_ERROR_NONE;
493 int _bt_get_discoverable_mode(int *mode)
500 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
502 adapter_is_discoverable(&scan_mode);
503 if (TRUE == scan_mode) {
504 adapter_get_discoverable_timeout(&timeout);
506 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
508 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
510 adapter_is_connectable(&scan_mode);
511 if (scan_mode == TRUE)
512 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
515 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
516 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
517 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
524 return BLUETOOTH_ERROR_NONE;
527 int _bt_get_timeout_value(int *timeout)
532 /* Take current time */
534 time_diff = difftime(current_time, visible_timer.start_time);
536 BT_DBG("Time diff = %d\n", time_diff);
537 *timeout = visible_timer.timeout - time_diff;
539 return BLUETOOTH_ERROR_NONE;
542 static void __bt_visibility_alarm_remove()
544 if (visible_timer.event_id > 0) {
545 g_source_remove(visible_timer.event_id);
546 visible_timer.event_id = 0;
549 if (visible_timer.alarm_id > 0) {
550 alarmmgr_remove_alarm(visible_timer.alarm_id);
551 visible_timer.alarm_id = 0;
555 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
557 int result = BLUETOOTH_ERROR_NONE;
560 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
562 if (alarm_id != visible_timer.alarm_id)
565 if (visible_timer.event_id) {
566 _bt_send_event(BT_ADAPTER_EVENT,
567 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
568 g_variant_new("(in)", result, timeout));
569 g_source_remove(visible_timer.event_id);
570 visible_timer.event_id = 0;
571 visible_timer.timeout = 0;
573 if (!TIZEN_PROFILE_WEARABLE) {
574 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
575 BT_ERR("Set vconf failed\n");
578 /* Switch Off visibility in Bluez */
579 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
580 visible_timer.alarm_id = 0;
584 static gboolean __bt_timeout_handler(gpointer user_data)
586 int result = BLUETOOTH_ERROR_NONE;
590 /* Take current time */
592 time_diff = difftime(current_time, visible_timer.start_time);
594 /* Send event to application */
595 _bt_send_event(BT_ADAPTER_EVENT,
596 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
597 g_variant_new("(in)", result, time_diff));
599 if (visible_timer.timeout <= time_diff) {
600 g_source_remove(visible_timer.event_id);
601 visible_timer.event_id = 0;
602 visible_timer.timeout = 0;
604 if (!TIZEN_PROFILE_WEARABLE) {
605 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
606 BT_ERR("Set vconf failed\n");
615 static void __bt_visibility_alarm_create()
620 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
623 BT_ERR("Failed to create alarm error = %d\n", result);
625 BT_DBG("Alarm created = %d\n", alarm_id);
626 visible_timer.alarm_id = alarm_id;
630 int _bt_start_visibility_timer(int timeout)
633 #ifdef TIZEN_FEATURE_BT_DPM
634 int discoverable_state = DPM_BT_ERROR;
637 __bt_visibility_alarm_remove();
639 visible_timer.timeout = timeout;
641 #ifdef TIZEN_FEATURE_BT_DPM
642 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
643 if (discoverable_state != DPM_RESTRICTED) {
645 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
646 BT_ERR("Set vconf failed");
647 #ifdef TIZEN_FEATURE_BT_DPM
652 return BLUETOOTH_ERROR_NONE;
654 if (!visible_timer.alarm_init) {
655 /* Set Alarm timer to switch off BT */
656 result = alarmmgr_init("bt-service");
658 return BLUETOOTH_ERROR_INTERNAL;
660 visible_timer.alarm_init = TRUE;
663 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
665 return BLUETOOTH_ERROR_INTERNAL;
667 if (!TIZEN_PROFILE_WEARABLE) {
668 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
669 BT_ERR("Set vconf failed");
672 /* Take start time */
673 time(&(visible_timer.start_time));
674 visible_timer.event_id = g_timeout_add_seconds(1,
675 __bt_timeout_handler, NULL);
677 visible_timer.timeout = timeout;
679 __bt_visibility_alarm_create();
681 return BLUETOOTH_ERROR_NONE;
684 int _bt_stop_visibility_timer(void)
686 __bt_visibility_alarm_remove();
688 visible_timer.timeout = 0;
690 if (!TIZEN_PROFILE_WEARABLE) {
691 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
692 BT_ERR("Set vconf failed");
695 return BLUETOOTH_ERROR_NONE;
698 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
701 int ret = BLUETOOTH_ERROR_NONE;;
702 #ifdef TIZEN_FEATURE_BT_DPM
703 int discoverable_state = DPM_BT_ERROR;
708 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
710 #ifdef TIZEN_FEATURE_BT_DPM
711 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
712 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
713 discoverable_state == DPM_RESTRICTED) {
714 if (headed_plugin_info->plugin_headed_enabled)
715 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
716 return BLUETOOTH_ERROR_ACCESS_DENIED;
718 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE &&
719 discoverable_state == DPM_RESTRICTED) {
720 if (headed_plugin_info->plugin_headed_enabled)
721 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
722 return BLUETOOTH_ERROR_ACCESS_DENIED;
726 switch (discoverable_mode) {
727 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
728 result = adapter_set_connectable(TRUE);
731 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
732 result = adapter_set_discoverable();
735 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
736 result = adapter_set_discoverable();
739 return BLUETOOTH_ERROR_INVALID_PARAM;
742 if (result != OAL_STATUS_SUCCESS) {
743 BT_ERR("set scan mode failed %d", result);
744 return BLUETOOTH_ERROR_INTERNAL;
747 result = adapter_set_discoverable_timeout(timeout);
748 if (result != OAL_STATUS_SUCCESS) {
749 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
750 return BLUETOOTH_ERROR_INTERNAL;
753 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
754 ret = _bt_stop_visibility_timer();
755 if (BLUETOOTH_ERROR_NONE != ret)
756 BT_ERR("_bt_stop_visibility_timer failed");
757 if (!TIZEN_PROFILE_WEARABLE) {
758 if (vconf_set_int(BT_FILE_VISIBLE_TIME, -1) != 0)
759 BT_ERR("Set vconf failed");
761 } else if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE) {
762 ret = _bt_start_visibility_timer(timeout);
763 if (BLUETOOTH_ERROR_NONE != ret)
764 BT_ERR("_bt_start_visibility_timer failed");
767 ret = _bt_stop_visibility_timer();
768 if (BLUETOOTH_ERROR_NONE != ret)
769 BT_ERR("_bt_stop_visibility_timer failed");
776 gboolean _bt_is_connectable(void)
783 adapter_is_connectable(&connectable);
789 BT_DBG("Connectable: [%s]", result ? "TRUE" : "FALSE");
794 int _bt_set_connectable(gboolean connectable)
796 int result = BLUETOOTH_ERROR_NONE;
799 result = adapter_set_connectable(connectable);
800 if (result != OAL_STATUS_SUCCESS) {
801 BT_ERR("adapter_set_connectable failed: %d", result);
802 result = BLUETOOTH_ERROR_INTERNAL;
804 result = BLUETOOTH_ERROR_NONE;
810 int _bt_get_connectable(void)
816 result = adapter_get_connectable();
817 if (result != OAL_STATUS_SUCCESS) {
818 BT_ERR("adapter_get_connectable failed: %d", result);
819 result = BLUETOOTH_ERROR_INTERNAL;
821 result = BLUETOOTH_ERROR_NONE;
827 int _bt_is_service_used(void)
833 result = adapter_get_service_uuids();
834 if (result != OAL_STATUS_SUCCESS) {
835 BT_ERR("adapter_get_service_uuids failed: %d", result);
836 result = BLUETOOTH_ERROR_INTERNAL;
838 result = BLUETOOTH_ERROR_NONE;
845 int _bt_adapter_get_bonded_devices(void)
847 int result = BLUETOOTH_ERROR_NONE;
850 result = adapter_get_bonded_devices();
851 if (result != OAL_STATUS_SUCCESS) {
852 BT_ERR("adapter_get_bonded_devices failed: %d", result);
853 result = BLUETOOTH_ERROR_INTERNAL;
855 result = BLUETOOTH_ERROR_NONE;
861 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
864 GDBusConnection *conn;
865 GDBusProxy *manager_proxy;
866 GVariant *result = NULL;
867 GVariant *result1 = NULL;
868 GVariantIter *iter = NULL;
869 GError *error = NULL;
870 char *object_path = NULL;
871 GVariantIter *interface_iter;
872 char *interface_str = NULL;
873 GDBusProxy *device_proxy = NULL;
874 gboolean is_connected = FALSE;
876 conn = _bt_gdbus_get_system_gconn();
877 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
879 manager_proxy = _bt_get_manager_proxy();
880 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
882 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
884 G_DBUS_CALL_FLAGS_NONE,
891 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
892 g_clear_error(&error);
895 BT_ERR("Failed to Failed to GetManagedObjects");
896 return BLUETOOTH_ERROR_INTERNAL;
899 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
900 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
902 /* Parse the signature: oa{sa{sv}}} */
903 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
904 if (object_path == NULL)
907 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
908 &interface_str, NULL)) {
909 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
910 BT_DBG("Found a device: %s", object_path);
911 g_free(interface_str);
913 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
915 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
917 if (device_proxy == NULL) {
918 BT_DBG("Device don't have this service");
922 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
923 g_variant_new("(s)", profile_uuid),
924 G_DBUS_CALL_FLAGS_NONE,
929 if (result1 == NULL) {
930 BT_ERR("Error occured in Proxy call");
932 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
936 g_object_unref(device_proxy);
939 g_variant_get(result1, "(b)", &is_connected);
941 if (is_connected == TRUE) {
942 char address[BT_ADDRESS_STRING_SIZE];
943 bluetooth_device_address_t *addr = NULL;
945 _bt_convert_device_path_to_address(object_path, address);
947 addr = g_malloc0(sizeof(bluetooth_device_address_t));
948 _bt_convert_addr_string_to_type(addr->addr, address);
950 g_array_append_vals(*addr_list, addr,
951 sizeof(bluetooth_device_address_t));
954 g_variant_unref(result1);
955 g_object_unref(device_proxy);
962 g_variant_unref(result);
963 g_variant_iter_free(iter);
966 return BLUETOOTH_ERROR_NONE;
969 static gboolean __bt_disconnect_all(void)
972 gboolean ret = FALSE;
976 bluetooth_device_info_t *info;
979 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
981 BT_DBG("The length of device list after the initialisation is [%d]", device_list->len);
983 _bt_service_get_bonded_dev_list(&device_list);
985 size = (device_list->len) / sizeof(bluetooth_device_info_t);
987 for (i = 0; i < size; i++) {
988 info = &g_array_index(device_list,
989 bluetooth_device_info_t, i);
990 if (info->connected != BLUETOOTH_CONNECTED_LINK_NONE) {
991 BT_DBG("Found Connected Device");
992 result = _bt_disconnect_device(&info->device_address);
993 if (result != BLUETOOTH_ERROR_NONE) {
994 BT_ERR("Cannot disconnect all the devices. bt_disconnect_device Failed");
995 g_array_free(device_list, TRUE);
1001 g_array_free(device_list, TRUE);
1006 static void __bt_handle_pending_a2dp_init(unsigned int a2dp_role)
1010 if (!a2dp_init_pending)
1014 a2dp_init_pending = FALSE;
1016 if (a2dp_role == BT_A2DP_SINK_ROLE) {
1017 BT_INFO("Enable A2DP Sink role");
1018 /* Initialize A2DP Sink */
1019 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
1020 if (ret != BLUETOOTH_ERROR_NONE)
1021 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
1023 /* Initialize AVRCP Controller */
1024 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
1025 if (ret != BLUETOOTH_ERROR_NONE)
1026 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
1028 _bt_audio_set_current_role(BLUETOOTH_A2DP_SINK);
1032 BT_INFO("Enable A2DP Source role");
1033 /* Initialize A2DP Source */
1034 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
1035 if (ret != BLUETOOTH_ERROR_NONE)
1036 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
1038 /* Initialize AVRCP Target */
1039 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
1040 if (ret != BLUETOOTH_ERROR_NONE)
1041 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
1043 _bt_audio_set_current_role(BLUETOOTH_A2DP_SOURCE);
1047 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
1049 int result = BLUETOOTH_ERROR_NONE;
1053 switch (event_type) {
1054 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1055 case OAL_EVENT_OAL_INITIALISED_FAILED:
1056 __bt_handle_oal_initialisation(event_type);
1058 case OAL_EVENT_ADAPTER_ENABLED:
1059 __bt_adapter_state_change_callback(BT_ACTIVATED);
1061 case OAL_EVENT_ADAPTER_DISABLED:
1062 __bt_adapter_state_change_callback(BT_DEACTIVATED);
1064 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
1065 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
1067 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
1068 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
1070 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
1071 bt_address_t *bd_addr = event_data;
1072 bluetooth_device_address_t local_address;
1075 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
1076 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
1077 local_address.addr[0], local_address.addr[1], local_address.addr[2],
1078 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
1080 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
1081 (void *) &local_address, sizeof(bluetooth_device_address_t));
1084 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
1085 char *name = event_data;
1086 BT_DBG("Adapter Name: %s", name);
1088 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
1089 bluetooth_device_name_t local_name;
1091 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
1092 g_strlcpy(local_name.name,
1093 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
1094 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
1095 (void *) &local_name, sizeof(bluetooth_device_name_t));
1097 /* Send event to application */
1098 _bt_send_event(BT_ADAPTER_EVENT,
1099 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
1100 g_variant_new("(is)", result, name));
1104 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
1105 char *ver = event_data;
1106 bluetooth_version_t local_version;
1108 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
1109 g_strlcpy(local_version.version,
1110 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
1111 BT_DBG("BT Version: %s", local_version.version);
1113 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
1114 (void *) &local_version, sizeof(bluetooth_version_t));
1117 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
1119 gboolean connectable = FALSE;
1121 BT_INFO("Adapter discoverable mode:"
1122 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
1124 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1125 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1126 (void *) &connectable, sizeof(gboolean));
1129 _bt_send_event(BT_ADAPTER_EVENT,
1130 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1131 g_variant_new("(b)", connectable));
1133 _bt_send_event(BT_ADAPTER_EVENT,
1134 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1135 g_variant_new("(in)", result, mode));
1139 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
1141 gboolean connectable = TRUE;
1143 BT_INFO("Adapter discoverable mode:"
1144 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
1146 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1147 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1148 (void *) &connectable, sizeof(gboolean));
1152 _bt_send_event(BT_ADAPTER_EVENT,
1153 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1154 g_variant_new("(b)", connectable));
1156 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1157 _bt_send_event(BT_ADAPTER_EVENT,
1158 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1159 g_variant_new("(in)", result, mode));
1163 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
1165 gboolean connectable = TRUE;
1167 BT_INFO("Adapter discoverable mode:"
1168 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
1170 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1171 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1172 (void *) &connectable, sizeof(gboolean));
1175 /* Send event to application */
1176 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1177 _bt_send_event(BT_ADAPTER_EVENT,
1178 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1179 g_variant_new("(in)", result, mode));
1183 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
1184 int *timeout = event_data;
1187 BT_INFO("Discoverable timeout: [%d]", *timeout);
1189 /* Send event to application */
1190 _bt_get_discoverable_mode(&mode);
1191 _bt_send_event(BT_ADAPTER_EVENT,
1192 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1193 g_variant_new("(in)", result, mode));
1196 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
1198 service_uuid_t *service_list;
1199 event_adapter_services_t *list = event_data;
1202 service_list = list->service_list;
1203 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
1205 char uuids[512] = {0,};
1206 char standard_prefix[2] = {0,};
1208 for (int i = 0; i < count; i++) {
1210 g_snprintf(uuids + (index++), 2, ",");
1211 if (memcmp(service_list[i].uuid, standard_prefix, 2) == 0) {
1212 g_snprintf(uuids + index, 5, "%.2x%.2x",
1213 service_list[i].uuid[2], service_list[i].uuid[3]);
1216 g_snprintf(uuids + index, 9, "%.2x%.2x..%.2x,",
1217 service_list[i].uuid[0], service_list[i].uuid[1], service_list[i].uuid[15]);
1221 BT_PERMANENT_LOG("UUIDs %s", uuids);
1224 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
1227 bluetooth_device_address_t *addr_list;
1229 event_device_list_t *bonded_device_list = event_data;
1230 count = bonded_device_list->num;
1232 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
1233 for (i = 0; i < count; i++) {
1234 memcpy(addr_list[i].addr,
1235 bonded_device_list->devices[i].addr,
1236 BLUETOOTH_ADDRESS_LENGTH);
1239 BT_INFO("Adapter Bonded device List count: [%d]", count);
1240 _bt_device_handle_paired_address_list(addr_list, bonded_device_list->num);
1244 case OAL_EVENT_ADAPTER_PROPERTY_A2DP_ROLE: {
1245 unsigned int *a2dp_role = event_data;
1247 BT_INFO("a2dp role: [%u]", *a2dp_role);
1249 __bt_handle_pending_a2dp_init(*a2dp_role);
1254 BT_ERR("Unhandled event..");
1261 int _bt_init_profiles()
1266 /*TODO: Init bluetooth profiles */
1267 ret = _bt_hidhost_initialize();
1268 if (ret != BLUETOOTH_ERROR_NONE)
1269 BT_ERR("_bt_hidhost_initialize Failed");
1271 ret = _bt_hiddevice_initialize();
1272 if (ret != BLUETOOTH_ERROR_NONE)
1273 BT_ERR("_bt_hiddevice_initialize Failed");
1275 ret = _bt_socket_init();
1276 if (ret != BLUETOOTH_ERROR_NONE)
1277 BT_ERR("_bt_socket_init Failed");
1279 /* Initialize HFP Audio Gateway */
1280 ret = _bt_audio_initialize(BT_AG_MODULE);
1281 if (ret != BLUETOOTH_ERROR_NONE)
1282 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
1284 /* Registering callback for receiving audio services searched */
1285 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
1286 if (ret != BLUETOOTH_ERROR_NONE)
1287 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
1289 ret = _bt_hdp_init();
1290 if (ret != BLUETOOTH_ERROR_NONE)
1291 BT_ERR("_bt_hdp_init Failed");
1293 ret = _bt_gatt_init();
1294 if (ret != BLUETOOTH_ERROR_NONE)
1295 BT_ERR("_bt_gatt_init Failed");
1297 /* Initialize HF Client */
1298 ret = _bt_audio_initialize(BT_HFP_MODULE);
1299 if (ret != BLUETOOTH_ERROR_NONE)
1300 BT_ERR("_bt_audio_initialize(BT_HFP_MODULE) Failed");
1302 return BLUETOOTH_ERROR_NONE;
1305 int _bt_cleanup_profiles(void)
1307 /* TODO: Cleanup bluetooth profiles */
1308 _bt_hidhost_deinitialize();
1309 _bt_socket_deinit();
1311 /* TODO: Cleanup bluetooth audio profiles */
1312 //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
1313 //_bt_audio_deinitialize(BT_AVRCP_MODULE);
1314 //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
1315 //_bt_audio_deinitialize(BT_AG_MODULE);
1316 //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
1317 //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
1319 a2dp_init_pending = TRUE;
1324 return BLUETOOTH_ERROR_NONE;
1327 /* OAL post initialization handler */
1328 static void __bt_post_oal_init(void)
1331 int status = VCONFKEY_BT_STATUS_OFF;
1333 BT_DBG("OAL initialized");
1334 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0)
1335 BT_ERR("Fail to get the enabled value");
1338 /* Update Bluetooth Status to OFF */
1339 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1340 BT_ERR("Set vconf failed\n");
1343 if (status & VCONFKEY_BT_STATUS_ON) {
1344 ret = _bt_enable_adapter();
1345 if (ret != BLUETOOTH_ERROR_NONE)
1346 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
1350 /* OAL initialization handler */
1351 static void __bt_handle_oal_initialisation(oal_event_t event)
1356 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1357 __bt_post_oal_init();
1359 case OAL_EVENT_OAL_INITIALISED_FAILED:
1360 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1361 g_idle_add(_bt_terminate_service, NULL);
1364 BT_ERR("Unknown Event");
1369 static gboolean __bt_is_service_request_present(int service_function)
1372 invocation_info_t *req_info;
1376 /* Get method invocation context */
1377 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1379 if (req_info && req_info->service_function == service_function)
1387 /* Internal functions of core adapter service */
1388 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1392 invocation_info_t *req_info;
1395 /* Get method invocation context */
1396 for (l = _bt_get_invocation_list(); l != NULL; ) {
1398 l = g_slist_next(l);
1399 if (req_info == NULL || req_info->service_function != service_function)
1402 /* Create out param */
1403 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1405 switch (service_function) {
1406 case BT_ENABLE_ADAPTER:
1407 case BT_DISABLE_ADAPTER: {
1408 gboolean done = TRUE;
1409 g_array_append_vals(out_param, &done, sizeof(gboolean));
1412 case BT_GET_CONNECTABLE:
1413 case BT_GET_LOCAL_NAME:
1414 case BT_GET_LOCAL_ADDRESS:
1415 case BT_GET_LOCAL_VERSION:
1416 g_array_append_vals(out_param, user_data, size);
1418 case BT_IS_SERVICE_USED: {
1420 gboolean used = FALSE;
1421 unsigned char *uuid;
1422 char uuid_str[BT_UUID_STRING_SIZE];
1423 char *request_uuid = req_info->user_data;
1424 service_uuid_t *service_list = user_data;
1426 BT_INFO("Check for service uuid: %s", request_uuid);
1427 for (i = 0; i < size; i++) {
1428 uuid = service_list[i].uuid;
1429 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1430 BT_INFO("Adapter Service: [%s]", uuid_str);
1431 if (strcasecmp(uuid_str, request_uuid) == 0) {
1432 BT_INFO("UUID matched!!");
1438 g_array_append_vals(out_param, &used, sizeof(gboolean));
1442 BT_ERR("Unknown service function[%d]", service_function);
1445 _bt_service_method_return(req_info->context, out_param, req_info->result);
1446 g_array_free(out_param, TRUE);
1447 /* Now free invocation info for this request*/
1448 _bt_free_info_from_invocation_list(req_info);
1452 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1454 char *phone_name = NULL;
1460 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1461 phone_name = vconf_keynode_get_str(node);
1463 if (phone_name && strlen(phone_name) != 0) {
1464 if (!g_utf8_validate(phone_name, -1,
1465 (const char **)&ptr))
1468 _bt_set_local_name(phone_name);
1473 /* Request return handlings */
1474 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1476 int result = BLUETOOTH_ERROR_NONE;
1478 BT_INFO("__bt_adapter_post_set_enabled>>");
1480 if (TIZEN_PROFILE_TV || !headed_plugin_info->plugin_headed_enabled) {
1481 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1482 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1483 BT_ERR("Fail to set discoverable mode");
1485 __bt_set_visible_mode();
1488 /* add the vconf noti handler */
1489 if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1490 (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1491 BT_ERR("DEVICE_NAME key changed notification registration failed");
1493 __bt_set_local_name();
1495 /* Get All properties */
1496 if (OAL_STATUS_SUCCESS != adapter_get_properties())
1497 BT_ERR("adapter_get_properties failed");
1499 __bt_adapter_state_set_status(BT_ACTIVATED);
1501 /*Send BT Enabled event to application */
1502 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1503 g_variant_new("(i)", result));
1505 BT_PERMANENT_LOG("BT Enabled");
1510 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1512 BT_INFO("_bt_adapter_post_set_disabled>>");
1514 if (!TIZEN_PROFILE_TV) {
1515 /* Add Adapter disabled post processing codes */
1516 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1517 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1518 BT_ERR("vconf_ignore_key_changed failed");
1521 /* bt-service should be terminated when BT is off */
1522 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1523 /* TODO: Implement to check if it is the recovery mode or not */
1524 if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_ON) {
1525 /* Keep bt-service to handle Flight mode OFF */
1526 BT_DBG("Deactived due to flight mode. Keep bt-service");
1527 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1528 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_NONE);
1529 } else if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_OFF) {
1530 BT_DBG("Durning deactivation, receive Flight mode Off. Enable BT adapter");
1531 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1532 _bt_service_initialize();
1533 _bt_enable_adapter();
1534 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_NONE);
1536 _bt_reliable_terminate_service(NULL);
1542 static void __bt_adapter_update_bt_enabled(void)
1544 BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1545 if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1546 BT_ERR("Bluetooth profile init failed");
1548 _bt_device_handle_adapter_state(TRUE);
1550 /* Update Bluetooth Status to notify other modules */
1551 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1552 BT_ERR("Set vconf failed\n");
1554 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1555 BT_ERR("Set vconf failed\n");
1557 /* TODO:Add timer function to handle any further post processing */
1558 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1561 static void __bt_adapter_update_bt_disabled(void)
1563 int power_off_status = 0;
1566 BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1567 _bt_cleanup_profiles();
1569 _bt_device_handle_adapter_state(FALSE);
1571 /* Update the vconf BT status in normal Deactivation case only */
1572 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1573 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1575 /* Update Bluetooth Status to notify other modules */
1576 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1577 BT_ERR("Set vconf failed");
1579 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1580 BT_ERR("Set vconf failed\n");
1582 /* TODO:Add timer function to handle any further post processing */
1583 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1585 /* Pending BT Disabled event before bt-service's termination */
1588 static void __bt_adapter_state_set_status(bt_status_t status)
1590 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1591 adapter_state = status;
1594 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1596 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1597 adapter_discovery_state = status;
1600 static void __bt_adapter_state_change_callback(int bt_status)
1602 BT_INFO("BT adapter status changed [%d]", bt_status);
1604 switch (bt_status) {
1605 case BT_DEACTIVATED:
1606 __bt_adapter_state_set_status(BT_TERMINATING);
1608 /* Adapter is disabled, unregister event handlers */
1609 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1610 //_bt_deinit_device_event_handler();
1612 /* Add Adapter disabled post processing codes */
1613 __bt_adapter_update_bt_disabled();
1616 /* Add Adapter enabled post processing codes */
1618 BT_DBG("g_source is removed");
1619 g_source_remove(timer_id);
1623 /*If adapter gets activated when flight mode is ON*/
1624 if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_ON) {
1625 BT_DBG("Receive Flight mode On. Disable BT adapter");
1626 _bt_disable_adapter();
1630 __bt_adapter_update_bt_enabled();
1633 BT_ERR("Incorrect Bluetooth adapter state changed status");
1638 int _bt_set_adapter_request_state(int enable)
1640 return oal_set_adapter_request_state(enable);
1643 int _bt_set_le_request_state(int enable)
1645 return oal_set_le_request_state(enable);
1648 static int __bt_adapter_state_handle_request(gboolean enable)
1650 int result = BLUETOOTH_ERROR_NONE;
1652 switch (adapter_state) {
1654 case BT_DEACTIVATING:
1655 BT_INFO("Should not be callded : state [%d]", adapter_state);
1656 return BLUETOOTH_ERROR_INTERNAL;
1658 case BT_ACTIVATED: {
1659 BT_INFO("Adapter is currently in activated state, state [%d]",
1662 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1664 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1665 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1666 /*TODO Stop Discovery*/
1667 __bt_adapter_update_discovery_status(FALSE);
1669 result = adapter_disable();
1670 if (result != OAL_STATUS_SUCCESS) {
1671 BT_ERR("adapter_enable failed: [%d]", result);
1672 result = BLUETOOTH_ERROR_INTERNAL;
1673 /*TODO: perform if anything more needs to be done to handle failure */
1675 /* TODO: To be handled */
1676 __bt_adapter_state_set_status(BT_DEACTIVATING);
1677 result = BLUETOOTH_ERROR_NONE;
1682 case BT_DEACTIVATED: {
1683 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1686 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1688 result = adapter_enable();
1689 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1690 BT_ERR("adapter_enable failed: [%d]", result);
1692 result = BLUETOOTH_ERROR_INTERNAL;
1693 /*TODO: perform if anything more needs to be done to handle failure */
1695 /* TODO: To be handled */
1696 __bt_adapter_state_set_status(BT_ACTIVATING);
1697 result = BLUETOOTH_ERROR_NONE;
1703 BT_ERR("Unknown state: %d", adapter_state);
1707 if (enable && result == BLUETOOTH_ERROR_NONE) {
1708 /* Adapter enable request is successful, setup event handlers */
1709 _bt_service_register_event_handler_callback(
1710 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1711 _bt_device_state_handle_callback_set_request();
1716 static int __bt_adapter_state_discovery_request(gboolean enable,
1717 unsigned short max_response, unsigned short duration,
1718 unsigned int mask, gboolean is_custom,
1719 bt_discovery_role_type_t role)
1721 int result = BLUETOOTH_ERROR_NONE;
1724 switch (adapter_discovery_state) {
1725 case ADAPTER_DISCOVERY_STARTED: {
1726 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1727 adapter_discovery_state);
1729 return BLUETOOTH_ERROR_IN_PROGRESS;
1731 result = adapter_stop_inquiry();
1732 if (result != OAL_STATUS_SUCCESS) {
1733 BT_ERR("Discover stop failed: %d", result);
1734 result = BLUETOOTH_ERROR_INTERNAL;
1736 BT_ERR("Stop Discovery Triggered successfully");
1737 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1738 result = BLUETOOTH_ERROR_NONE;
1743 case ADAPTER_DISCOVERY_STARTING: {
1744 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1745 adapter_discovery_state);
1747 result = enable ? BLUETOOTH_ERROR_IN_PROGRESS :
1748 BLUETOOTH_ERROR_DEVICE_BUSY;
1752 case ADAPTER_DISCOVERY_STOPPED: {
1753 BT_INFO("Adapter is currently in discovery stopped 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);
1761 result = adapter_start_inquiry(duration);
1763 result = adapter_start_custom_inquiry(role);
1765 if (result != OAL_STATUS_SUCCESS) {
1766 BT_ERR("Start Discovery failed: %d", result);
1767 result = BLUETOOTH_ERROR_INTERNAL;
1769 BT_ERR("Start Discovery Triggered successfully");
1770 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1771 result = BLUETOOTH_ERROR_NONE;
1776 case ADAPTER_DISCOVERY_STOPPING: {
1777 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1778 adapter_discovery_state);
1780 result = enable ? BLUETOOTH_ERROR_DEVICE_BUSY :
1781 BLUETOOTH_ERROR_NOT_IN_OPERATION;
1786 BT_ERR("Unknown state: %d", adapter_discovery_state);
1794 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1796 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1797 GVariant *param = NULL;
1798 int result = BLUETOOTH_ERROR_NONE;
1800 switch (bt_discovery_status) {
1801 case ADAPTER_DISCOVERY_STOPPED:
1803 __bt_adapter_update_discovery_status(bt_discovery_status);
1804 param = g_variant_new("(i)", result);
1805 _bt_send_event(BT_ADAPTER_EVENT,
1806 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1810 case ADAPTER_DISCOVERY_STARTED:
1812 __bt_adapter_update_discovery_status(bt_discovery_status);
1813 param = g_variant_new("(i)", result);
1814 _bt_send_event(BT_ADAPTER_EVENT,
1815 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1820 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1824 static void __bt_set_visible_mode(void)
1827 #ifdef TIZEN_FEATURE_BT_DPM
1828 int discoverable_state = DPM_BT_ERROR;
1831 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1832 BT_ERR("Fail to get the timeout value");
1834 #ifdef TIZEN_FEATURE_BT_DPM
1835 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1836 if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
1837 if (_bt_set_discoverable_mode(
1838 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1839 timeout) != BLUETOOTH_ERROR_NONE) {
1840 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1841 BT_ERR("Set vconf failed");
1844 if (_bt_set_discoverable_mode(
1845 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1846 timeout) != BLUETOOTH_ERROR_NONE) {
1847 BT_ERR("Set connectable mode failed");
1851 if (timeout == -1) {
1852 if (_bt_set_discoverable_mode(
1853 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1854 timeout) != BLUETOOTH_ERROR_NONE) {
1855 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1856 BT_ERR("Set vconf failed");
1859 if (_bt_set_discoverable_mode(
1860 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1861 timeout) != BLUETOOTH_ERROR_NONE) {
1862 BT_ERR("Set connectable mode failed");
1868 static void __bt_set_local_name(void)
1870 char *phone_name = NULL;
1873 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1878 if (strlen(phone_name) != 0) {
1879 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1882 _bt_set_local_name(phone_name);
1887 void _bt_adapter_set_status(bt_status_t status)
1889 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1890 adapter_state = status;
1893 bt_status_t _bt_adapter_get_status(void)
1895 return adapter_state;
1898 void _bt_set_disabled(int result)
1900 int power_off_status = 0;
1903 int pm_ignore_mode = 0;
1905 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1906 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1908 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1910 /* Update the vconf BT status in normal Deactivation case only */
1911 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1912 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1914 BT_DBG("Update vconf for BT normal Deactivation");
1916 if (result == BLUETOOTH_ERROR_TIMEOUT)
1917 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1918 BT_ERR("Set vconf failed");
1920 /* Update Bluetooth Status to notify other modules */
1921 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1922 BT_ERR("Set vconf failed");
1924 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1925 EVT_VAL_BT_OFF) != ES_R_OK)
1926 BT_ERR("Fail to set value");
1929 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1930 BT_ERR("Set vconf failed\n");
1932 _bt_adapter_set_status(BT_DEACTIVATED);
1933 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1935 /* Send BT Disabled event to application */
1936 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1937 g_variant_new("(i)", result));
1939 BT_INFO("Adapter disabled");
1940 BT_PERMANENT_LOG("BT Disabled");