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-mesh-main.h"
39 #include "bt-service-core-adapter.h"
40 #include "bt-service-core-device.h"
41 #include "bt-service-event-receiver.h"
42 #include "bt-request-handler.h"
43 #include "bt-service-event.h"
44 #include "bt-service-audio-common.h"
45 #include "bt-service-core-adapter-le.h"
46 #include "bt-service-gatt.h"
47 #include "bt-service-dpm.h"
48 #include "bt-service-hidhost.h"
49 #include "bt-service-hiddevice.h"
50 #include "bt-service-socket.h"
51 #include "bt-service-hdp.h"
52 #include "bt-service-tds.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;
78 BT_ADAPTER_FLIGHT_MODE_NONE = -1,
79 BT_ADAPTER_FLIGHT_MODE_OFF,
80 BT_ADAPTER_FLIGHT_MODE_ON,
81 } bt_adapter_flightmode_req_e;
83 static bt_adapter_flightmode_req_e flightmode_request = BT_ADAPTER_FLIGHT_MODE_NONE;
85 static guint timer_id = 0;
87 static gboolean a2dp_init_pending = TRUE;
89 /* Adapter default states */
90 static bt_status_t adapter_state = BT_DEACTIVATED;
91 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
93 /* Forward declarations */
94 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
95 static void __bt_post_oal_init(void);
96 static void __bt_handle_oal_initialisation(oal_event_t event);
97 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
98 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
99 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
100 static void __bt_adapter_update_bt_enabled(void);
101 static void __bt_adapter_update_bt_disabled(void);
102 static void __bt_adapter_state_set_status(bt_status_t status);
103 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
104 static void __bt_adapter_state_change_callback(int bt_status);
105 static int __bt_adapter_state_handle_request(gboolean enable);
106 static int __bt_adapter_state_discovery_request(gboolean enable,
107 unsigned short max_response, unsigned short duration, unsigned int mask,
108 gboolean is_custom, bt_discovery_role_type_t role);
109 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
110 static gboolean __bt_is_service_request_present(int service_function);
112 static void __bt_set_visible_mode(void);
113 static void __bt_set_local_name(void);
115 static gboolean __bt_disconnect_all(void);
117 /* Initialize BT stack (Initialize OAL layer) */
118 int _bt_stack_init(void)
122 BT_INFO("[bt-service] Start to initialize BT stack");
123 /* Adapter enable request is successful, setup event handlers */
124 _bt_service_register_event_handler_callback(
125 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
128 if (ret != BLUETOOTH_ERROR_NONE) {
129 BT_ERR("_bt_le_init Failed");
133 ret = oal_bt_init(_bt_service_oal_event_receiver);
135 if (OAL_STATUS_PENDING == ret) {
136 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
137 return BLUETOOTH_ERROR_NONE;
138 } else if (OAL_STATUS_SUCCESS != ret) {
139 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
140 return _bt_convert_oal_status_to_bt_error(ret);
143 return BLUETOOTH_ERROR_NONE;
146 int _bt_enable_adapter(void)
148 bt_le_status_t le_status = BT_LE_DEACTIVATED;
150 le_status = _bt_adapter_get_le_status();
152 BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
154 if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
155 BT_ERR("Enabling in progress");
156 return BLUETOOTH_ERROR_IN_PROGRESS;
159 if (adapter_state == BT_ACTIVATED) {
160 BT_ERR("Already enabled");
161 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
164 if (adapter_state == BT_DEACTIVATING ||
165 adapter_state == BT_TERMINATING ||
166 le_status == BT_LE_DEACTIVATING) {
167 BT_ERR("Disabling in progress");
168 return BLUETOOTH_ERROR_DEVICE_BUSY;
171 return __bt_adapter_state_handle_request(TRUE);
174 int _bt_recover_adapter(void)
176 int result = BLUETOOTH_ERROR_NONE;
178 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
179 BT_ERR("Disabling in progress");
180 return BLUETOOTH_ERROR_IN_PROGRESS;
183 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
184 BT_ERR("Already Disabled");
185 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
188 _bt_adapter_set_status(BT_DEACTIVATING);
190 result = adapter_recover();
192 if (result != OAL_STATUS_SUCCESS)
193 return _bt_convert_oal_status_to_bt_error(result);
195 __bt_disconnect_all();
200 int _bt_reset_adapter(void)
204 result = adapter_reset();
205 if (result != OAL_STATUS_SUCCESS)
206 return _bt_convert_oal_status_to_bt_error(result);
208 /* TODO_40 : 4.0 merge */
209 /* TODO Currently bt-service is not terminated in tizen next.
210 It should be handled in future patch */
211 if (_bt_adapter_get_status() == BT_DEACTIVATED)
212 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
214 return BLUETOOTH_ERROR_NONE;
217 #ifdef TIZEN_FEATURE_BT_PERMANENT_LOG
218 void _bt_start_log_dump(const char *path)
222 GError *error = NULL;
224 ret_if(path == NULL);
226 BT_INFO("Start log_dump(path = %s)", path);
228 _bt_send_dump_signal(BT_DUMP_SERVICE_START_SIGNAL);
230 proxy = __bt_get_core_proxy();
231 ret_if(proxy == NULL);
233 result = g_dbus_proxy_call_sync(proxy, "LogDump",
234 g_variant_new("(s)", path),
235 G_DBUS_CALL_FLAGS_NONE,
240 BT_ERR("Bt core call failed(Error: %s)", error->message);
241 g_clear_error(&error);
243 BT_ERR("Bt core call failed");
246 g_variant_unref(result);
249 _bt_send_dump_signal(BT_DUMP_SERVICE_FINISH_SIGNAL);
253 int _bt_check_adapter(int *status)
256 gboolean powered = FALSE;
258 BT_CHECK_PARAMETER(status, return);
260 *status = BT_ADAPTER_DISABLED;
262 result = adapter_get_powered_status(&powered);
263 if (result != OAL_STATUS_SUCCESS)
264 return _bt_convert_oal_status_to_bt_error(result);
266 BT_INFO("Adapter is in Powered Up state");
267 *status = BT_ADAPTER_ENABLED;
269 BT_INFO("Adapter is in Powered Down state");
271 return BLUETOOTH_ERROR_NONE;
274 int _bt_disable_adapter(void)
276 bt_le_status_t le_status = BT_LE_DEACTIVATED;
278 le_status = _bt_adapter_get_le_status();
280 BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
282 if (adapter_state == BT_DEACTIVATING ||
283 adapter_state == BT_TERMINATING ||
284 le_status == BT_LE_DEACTIVATING) {
285 BT_DBG("Disabling in progress");
286 return BLUETOOTH_ERROR_IN_PROGRESS;
289 if (adapter_state == BT_DEACTIVATED) {
290 BT_DBG("Already disabled");
291 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
294 if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
295 BT_ERR("Enabling in progress");
296 return BLUETOOTH_ERROR_DEVICE_BUSY;
299 /* TODO: Need to add the function to disconnect all devices */
300 /* __bt_disconnect_all(); */
302 return __bt_adapter_state_handle_request(FALSE);
305 int _bt_start_discovery(unsigned short max_response,
306 unsigned short duration, unsigned int cod_mask)
308 return __bt_adapter_state_discovery_request(TRUE, max_response, duration,
309 cod_mask, FALSE, 0x00);
312 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
314 return __bt_adapter_state_discovery_request(TRUE, 0, 0, 0, TRUE, role);
317 int _bt_cancel_discovery(void)
319 return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0, FALSE, 0x00);
322 gboolean _bt_is_discovering(void)
324 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
325 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
331 static void __bt_set_flightmode_request(int value)
333 BT_INFO("Setting flightmode request to %d", value);
334 flightmode_request = value;
337 static int __bt_is_flightmode_request(void)
339 return flightmode_request;
342 static void __bt_service_handle_flight_mode(gboolean flight_mode, bt_status_t adapter_status)
344 int flight_mode_value = 0;
345 int ps_mode_value = 0;
347 if (flight_mode == TRUE) {
348 BT_INFO_C("### Flight mode on. Turn off BT");
350 if (adapter_status != BT_ACTIVATED) {
351 BT_INFO("No need to control bt status");
352 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
353 BT_ERR("Fail get PS mode value");
355 BT_DBG("ps_mode_value %d", ps_mode_value);
356 if (ps_mode_value > 0) {
357 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
358 BT_ERR("Set vconf failed");
363 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_ON);
365 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
366 BT_ERR("Set vconf failed");
368 if (adapter_status == BT_ACTIVATED)
369 _bt_disable_adapter();
372 BT_INFO_C("### Flight mode off. Turn on BT");
374 if (adapter_status == BT_ACTIVATED)
377 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value))
378 BT_ERR("Fail get flight mode value");
380 BT_DBG("flight_mode_value %d", flight_mode_value);
381 if (flight_mode_value == 0)
384 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
385 BT_ERR("Set vconf failed");
387 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
388 BT_ERR("Fail get PS mode value");
390 BT_DBG("ps_mode_value %d", ps_mode_value);
391 if (ps_mode_value > 0) {
392 BT_DBG("Bt should not turn on");
396 if (adapter_status == BT_DEACTIVATING) {
397 BT_INFO("BT adapter is deactivating. Turn on BT after deactivation");
398 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_OFF);
402 if (adapter_status != BT_DEACTIVATED) {
403 BT_INFO("No need to control bt status");
406 _bt_service_initialize();
408 if (adapter_status == BT_DEACTIVATED)
409 _bt_enable_adapter();
414 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
417 bt_status_t adapter_status = BT_DEACTIVATED;
418 gboolean flight_mode = FALSE;
420 adapter_status = _bt_adapter_get_status();
421 BT_INFO("Flight mode changed. Current bt status is %d", adapter_status);
423 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
424 type = vconf_keynode_get_type(node);
426 if (type == VCONF_TYPE_BOOL) {
427 flight_mode = vconf_keynode_get_bool(node);
428 __bt_service_handle_flight_mode(flight_mode, adapter_status);
430 BT_ERR("Invaild vconf key type : %d", type);
434 void _bt_service_register_vconf_handler(void)
438 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
439 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
440 BT_ERR("Unable to register key handler");
441 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
442 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
443 BT_ERR("Unable to register key handler");
446 void _bt_service_unregister_vconf_handler(void)
450 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
451 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
453 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
454 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
457 static gboolean __bt_enable_timeout_cb(gpointer user_data)
461 _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
462 #ifndef USB_BLUETOOTH
463 _bt_terminate_service(NULL);
468 void _bt_adapter_start_enable_timer(void)
471 g_source_remove(timer_id);
475 timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
476 __bt_enable_timeout_cb, NULL);
481 int _bt_get_local_address(void)
487 result = adapter_get_address();
488 if (result != OAL_STATUS_SUCCESS) {
489 BT_ERR("adapter_get_address failed: %d", result);
490 result = _bt_convert_oal_status_to_bt_error(result);
492 result = BLUETOOTH_ERROR_NONE;
498 int _bt_get_local_version(void)
503 result = adapter_get_version();
504 if (result != OAL_STATUS_SUCCESS) {
505 BT_ERR("adapter_get_version failed: %d", result);
506 result = _bt_convert_oal_status_to_bt_error(result);
508 result = BLUETOOTH_ERROR_NONE;
514 int _bt_get_local_name(void)
520 result = adapter_get_name();
521 if (result != OAL_STATUS_SUCCESS) {
522 BT_ERR("adapter_get_name failed: %d", result);
523 result = _bt_convert_oal_status_to_bt_error(result);
525 result = BLUETOOTH_ERROR_NONE;
531 int _bt_set_local_name(char *local_name)
533 int result = BLUETOOTH_ERROR_NONE;
536 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
538 result = adapter_set_name(local_name);
539 if (result != OAL_STATUS_SUCCESS) {
540 BT_ERR("adapter_set_name failed: %d", result);
541 result = _bt_convert_oal_status_to_bt_error(result);
543 result = BLUETOOTH_ERROR_NONE;
549 int _bt_get_discoverable_mode(int *mode)
556 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
558 adapter_is_discoverable(&scan_mode);
559 if (TRUE == scan_mode) {
560 adapter_get_discoverable_timeout(&timeout);
562 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
564 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
566 adapter_is_connectable(&scan_mode);
567 if (scan_mode == TRUE)
568 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
571 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
572 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
573 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
580 return BLUETOOTH_ERROR_NONE;
583 int _bt_get_timeout_value(int *timeout)
588 /* Take current time */
590 time_diff = difftime(current_time, visible_timer.start_time);
592 BT_DBG("Time diff = %d\n", time_diff);
593 *timeout = visible_timer.timeout - time_diff;
595 return BLUETOOTH_ERROR_NONE;
598 int _bt_get_energy_info(unsigned int *tx_time, unsigned int *rx_time,
599 unsigned int *idle_time, unsigned int *energy_used)
604 BT_CHECK_PARAMETER(tx_time, return);
605 BT_CHECK_PARAMETER(rx_time, return);
606 BT_CHECK_PARAMETER(idle_time, return);
607 BT_CHECK_PARAMETER(energy_used, return);
614 result = adapter_get_energy_info(tx_time, rx_time, idle_time, energy_used);
615 if (result != OAL_STATUS_SUCCESS)
616 return _bt_convert_oal_status_to_bt_error(result);
618 return BLUETOOTH_ERROR_NONE;
622 static void __bt_visibility_alarm_remove()
624 if (visible_timer.event_id > 0) {
625 g_source_remove(visible_timer.event_id);
626 visible_timer.event_id = 0;
629 if (visible_timer.alarm_id > 0) {
630 alarmmgr_remove_alarm(visible_timer.alarm_id);
631 visible_timer.alarm_id = 0;
635 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
637 int result = BLUETOOTH_ERROR_NONE;
640 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
642 if (alarm_id != visible_timer.alarm_id)
645 if (visible_timer.event_id) {
646 _bt_send_event(BT_ADAPTER_EVENT,
647 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
648 g_variant_new("(in)", result, timeout));
649 g_source_remove(visible_timer.event_id);
650 visible_timer.event_id = 0;
651 visible_timer.timeout = 0;
653 if (!TIZEN_PROFILE_WEARABLE) {
654 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
655 BT_ERR("Set vconf failed\n");
658 /* Switch Off visibility in Bluez */
659 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
660 visible_timer.alarm_id = 0;
664 static gboolean __bt_timeout_handler(gpointer user_data)
666 int result = BLUETOOTH_ERROR_NONE;
670 /* Take current time */
672 time_diff = difftime(current_time, visible_timer.start_time);
674 /* Send event to application */
675 _bt_send_event(BT_ADAPTER_EVENT,
676 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
677 g_variant_new("(in)", result, time_diff));
679 if (visible_timer.timeout <= time_diff) {
680 g_source_remove(visible_timer.event_id);
681 visible_timer.event_id = 0;
682 visible_timer.timeout = 0;
684 if (!TIZEN_PROFILE_WEARABLE) {
685 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
686 BT_ERR("Set vconf failed\n");
695 static void __bt_visibility_alarm_create()
700 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
703 BT_ERR("Failed to create alarm error = %d\n", result);
705 BT_DBG("Alarm created = %d\n", alarm_id);
706 visible_timer.alarm_id = alarm_id;
710 int _bt_start_visibility_timer(int timeout)
713 int discoverable_state = DPM_BT_ERROR;
715 __bt_visibility_alarm_remove();
717 visible_timer.timeout = timeout;
719 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
720 if (discoverable_state != DPM_RESTRICTED) {
721 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
722 BT_ERR("Set vconf failed");
726 return BLUETOOTH_ERROR_NONE;
728 if (!visible_timer.alarm_init) {
729 /* Set Alarm timer to switch off BT */
730 result = alarmmgr_init("bt-service");
732 return BLUETOOTH_ERROR_INTERNAL;
734 visible_timer.alarm_init = TRUE;
737 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
739 return BLUETOOTH_ERROR_INTERNAL;
741 if (!TIZEN_PROFILE_WEARABLE) {
742 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
743 BT_ERR("Set vconf failed");
746 /* Take start time */
747 time(&(visible_timer.start_time));
748 visible_timer.event_id = g_timeout_add_seconds(1,
749 __bt_timeout_handler, NULL);
751 visible_timer.timeout = timeout;
753 __bt_visibility_alarm_create();
755 return BLUETOOTH_ERROR_NONE;
758 int _bt_stop_visibility_timer(void)
760 __bt_visibility_alarm_remove();
762 visible_timer.timeout = 0;
764 if (!TIZEN_PROFILE_WEARABLE) {
765 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
766 BT_ERR("Set vconf failed");
769 return BLUETOOTH_ERROR_NONE;
772 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
775 int ret = BLUETOOTH_ERROR_NONE;;
776 int discoverable_state = DPM_BT_ERROR;
780 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
782 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
783 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
784 discoverable_state == DPM_RESTRICTED) {
785 if (headed_plugin_info->plugin_headed_enabled)
786 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
787 return BLUETOOTH_ERROR_ACCESS_DENIED;
789 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE &&
790 discoverable_state == DPM_RESTRICTED) {
791 if (headed_plugin_info->plugin_headed_enabled)
792 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
793 return BLUETOOTH_ERROR_ACCESS_DENIED;
796 switch (discoverable_mode) {
797 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
798 result = adapter_set_connectable(TRUE);
801 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
802 result = adapter_set_discoverable();
805 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
806 result = adapter_set_discoverable();
809 return BLUETOOTH_ERROR_INVALID_PARAM;
812 if (result != OAL_STATUS_SUCCESS) {
813 BT_ERR("set scan mode failed %d", result);
814 return _bt_convert_oal_status_to_bt_error(result);
817 result = adapter_set_discoverable_timeout(timeout);
818 if (result != OAL_STATUS_SUCCESS) {
819 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
820 return _bt_convert_oal_status_to_bt_error(result);
823 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
824 ret = _bt_stop_visibility_timer();
825 if (BLUETOOTH_ERROR_NONE != ret)
826 BT_ERR("_bt_stop_visibility_timer failed");
827 if (!TIZEN_PROFILE_WEARABLE) {
828 if (vconf_set_int(BT_FILE_VISIBLE_TIME, -1) != 0)
829 BT_ERR("Set vconf failed");
831 } else if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE) {
832 ret = _bt_start_visibility_timer(timeout);
833 if (BLUETOOTH_ERROR_NONE != ret)
834 BT_ERR("_bt_start_visibility_timer failed");
837 ret = _bt_stop_visibility_timer();
838 if (BLUETOOTH_ERROR_NONE != ret)
839 BT_ERR("_bt_stop_visibility_timer failed");
846 gboolean _bt_is_connectable(void)
853 adapter_is_connectable(&connectable);
859 BT_DBG("Connectable: [%s]", result ? "TRUE" : "FALSE");
864 int _bt_set_connectable(gboolean connectable)
866 int result = BLUETOOTH_ERROR_NONE;
869 result = adapter_set_connectable(connectable);
870 if (result != OAL_STATUS_SUCCESS) {
871 BT_ERR("adapter_set_connectable failed: %d", result);
872 result = _bt_convert_oal_status_to_bt_error(result);
874 result = BLUETOOTH_ERROR_NONE;
880 int _bt_get_connectable(void)
886 result = adapter_get_connectable();
887 if (result != OAL_STATUS_SUCCESS) {
888 BT_ERR("adapter_get_connectable failed: %d", result);
889 result = _bt_convert_oal_status_to_bt_error(result);
891 result = BLUETOOTH_ERROR_NONE;
897 int _bt_is_service_used(void)
903 result = adapter_get_service_uuids();
904 if (result != OAL_STATUS_SUCCESS) {
905 BT_ERR("adapter_get_service_uuids failed: %d", result);
906 result = _bt_convert_oal_status_to_bt_error(result);
908 result = BLUETOOTH_ERROR_NONE;
915 int _bt_adapter_get_bonded_devices(void)
917 int result = BLUETOOTH_ERROR_NONE;
920 result = adapter_get_bonded_devices();
921 if (result != OAL_STATUS_SUCCESS) {
922 BT_ERR("adapter_get_bonded_devices failed: %d", result);
923 result = _bt_convert_oal_status_to_bt_error(result);
925 result = BLUETOOTH_ERROR_NONE;
931 int _bt_get_profile_connected_devices(const char *profile_uuid)
933 int result = BLUETOOTH_ERROR_NONE;
936 result = adapter_get_profile_connected_devices(profile_uuid);
937 if (result != OAL_STATUS_SUCCESS) {
938 BT_ERR("adapter_get_profile_connected_devices failed: %d", result);
939 result = _bt_convert_oal_status_to_bt_error(result);
941 result = BLUETOOTH_ERROR_NONE;
947 static gboolean __bt_disconnect_all(void)
950 gboolean ret = FALSE;
954 bluetooth_device_info_t *info;
957 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
959 BT_DBG("The length of device list after the initialisation is [%d]", device_list->len);
961 _bt_service_get_bonded_dev_list(&device_list);
963 size = (device_list->len) / sizeof(bluetooth_device_info_t);
965 for (i = 0; i < size; i++) {
966 info = &g_array_index(device_list,
967 bluetooth_device_info_t, i);
968 if (info->connected != BLUETOOTH_CONNECTED_LINK_NONE) {
969 BT_DBG("Found Connected Device");
970 result = _bt_disconnect_device(&info->device_address);
971 if (result != BLUETOOTH_ERROR_NONE) {
972 BT_ERR("Cannot disconnect all the devices. bt_disconnect_device Failed");
973 g_array_free(device_list, TRUE);
979 g_array_free(device_list, TRUE);
984 static void __bt_handle_pending_a2dp_init(unsigned int a2dp_role)
988 if (!a2dp_init_pending)
992 a2dp_init_pending = FALSE;
994 if (a2dp_role == BT_A2DP_SINK_ROLE) {
995 BT_INFO("Enable A2DP Sink role");
996 /* Initialize A2DP Sink */
997 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
998 if (ret != BLUETOOTH_ERROR_NONE)
999 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
1001 /* Initialize AVRCP Controller */
1002 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
1003 if (ret != BLUETOOTH_ERROR_NONE)
1004 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
1006 _bt_audio_set_current_role(BLUETOOTH_A2DP_SINK);
1010 BT_INFO("Enable A2DP Source role");
1011 /* Initialize A2DP Source */
1012 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
1013 if (ret != BLUETOOTH_ERROR_NONE)
1014 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
1016 /* Initialize AVRCP Target */
1017 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
1018 if (ret != BLUETOOTH_ERROR_NONE)
1019 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
1021 _bt_audio_set_current_role(BLUETOOTH_A2DP_SOURCE);
1025 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
1027 int result = BLUETOOTH_ERROR_NONE;
1031 switch (event_type) {
1032 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1033 case OAL_EVENT_OAL_INITIALISED_FAILED:
1034 __bt_handle_oal_initialisation(event_type);
1036 case OAL_EVENT_ADAPTER_ENABLED:
1037 __bt_adapter_state_change_callback(BT_ACTIVATED);
1039 case OAL_EVENT_ADAPTER_DISABLED:
1040 __bt_adapter_state_change_callback(BT_DEACTIVATED);
1042 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
1043 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
1045 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
1046 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
1048 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
1049 bt_address_t *bd_addr = event_data;
1050 bluetooth_device_address_t local_address;
1053 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
1054 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
1055 local_address.addr[0], local_address.addr[1], local_address.addr[2],
1056 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
1058 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
1059 (void *) &local_address, sizeof(bluetooth_device_address_t));
1062 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
1063 char *name = event_data;
1064 BT_DBG("Adapter Name: %s", name);
1066 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
1067 bluetooth_device_name_t local_name;
1069 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
1070 g_strlcpy(local_name.name,
1071 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
1072 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
1073 (void *) &local_name, sizeof(bluetooth_device_name_t));
1075 /* Send event to application */
1076 _bt_send_event(BT_ADAPTER_EVENT,
1077 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
1078 g_variant_new("(is)", result, name));
1082 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
1083 char *ver = event_data;
1084 bluetooth_version_t local_version;
1086 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
1087 g_strlcpy(local_version.version,
1088 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
1089 BT_DBG("BT Version: %s", local_version.version);
1091 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
1092 (void *) &local_version, sizeof(bluetooth_version_t));
1095 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
1097 gboolean connectable = FALSE;
1099 BT_INFO("Adapter discoverable mode:"
1100 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
1102 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1103 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1104 (void *) &connectable, sizeof(gboolean));
1107 _bt_send_event(BT_ADAPTER_EVENT,
1108 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1109 g_variant_new("(b)", connectable));
1111 _bt_send_event(BT_ADAPTER_EVENT,
1112 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1113 g_variant_new("(in)", result, mode));
1117 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
1119 gboolean connectable = TRUE;
1121 BT_INFO("Adapter discoverable mode:"
1122 " BLUETOOTH_DISCOVERABLE_MODE_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));
1130 _bt_send_event(BT_ADAPTER_EVENT,
1131 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1132 g_variant_new("(b)", connectable));
1134 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1135 _bt_send_event(BT_ADAPTER_EVENT,
1136 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1137 g_variant_new("(in)", result, mode));
1141 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
1143 gboolean connectable = TRUE;
1145 BT_INFO("Adapter discoverable mode:"
1146 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
1148 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1149 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1150 (void *) &connectable, sizeof(gboolean));
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_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
1183 char uuids[512] = {0,};
1184 char standard_prefix[2] = {0,};
1186 for (int i = 0; i < count; i++) {
1188 g_snprintf(uuids + (index++), 2, ",");
1189 if (memcmp(service_list[i].uuid, standard_prefix, 2) == 0) {
1190 g_snprintf(uuids + index, 5, "%.2x%.2x",
1191 service_list[i].uuid[2], service_list[i].uuid[3]);
1194 g_snprintf(uuids + index, 9, "%.2x%.2x..%.2x,",
1195 service_list[i].uuid[0], service_list[i].uuid[1], service_list[i].uuid[15]);
1199 BT_PERMANENT_LOG("UUIDs %s", uuids);
1202 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
1205 bluetooth_device_address_t *addr_list;
1207 event_device_list_t *bonded_device_list = event_data;
1208 count = bonded_device_list->num;
1210 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
1211 for (i = 0; i < count; i++) {
1212 memcpy(addr_list[i].addr,
1213 bonded_device_list->devices[i].addr,
1214 BLUETOOTH_ADDRESS_LENGTH);
1217 BT_INFO("Adapter Bonded device List count: [%d]", count);
1218 _bt_device_handle_paired_address_list(addr_list, bonded_device_list->num);
1222 case OAL_EVENT_ADAPTER_PROPERTY_A2DP_ROLE: {
1223 unsigned int *a2dp_role = event_data;
1225 BT_INFO("a2dp role: [%u]", *a2dp_role);
1227 __bt_handle_pending_a2dp_init(*a2dp_role);
1231 case OAL_EVENT_CONTROLLER_ERROR_RECEIVED: {
1232 uint8_t *error_code = event_data;
1234 BT_INFO("OAL_EVENT_CONTROLLER_ERROR_RECEIVED: [%d]", *error_code);
1236 #ifdef TIZEN_FEATURE_BT_PERMANENT_LOG
1237 _bt_start_log_dump("syslog");
1240 #ifdef TIZEN_FEATURE_BT_VENDOR_OPERATION
1241 _bt_recover_adapter();
1245 case OAL_EVENT_ADAPTER_PROFILE_CONNECTED_DEVICES:
1246 BT_DBG("OAL_EVENT_ADAPTER_PROFILE_CONNECTED_DEVICES");
1247 __bt_adapter_handle_pending_requests(BT_GET_PROFILE_CONNECTED_DEVICES,
1248 event_data, sizeof(event_adapter_profile_connected_devices));
1251 BT_ERR("Unhandled event..");
1258 int _bt_init_profiles()
1263 /*TODO: Init bluetooth profiles */
1264 ret = _bt_hidhost_initialize();
1265 if (ret != BLUETOOTH_ERROR_NONE)
1266 BT_ERR("_bt_hidhost_initialize Failed");
1268 ret = _bt_hiddevice_initialize();
1269 if (ret != BLUETOOTH_ERROR_NONE)
1270 BT_ERR("_bt_hiddevice_initialize Failed");
1272 ret = _bt_socket_init();
1273 if (ret != BLUETOOTH_ERROR_NONE)
1274 BT_ERR("_bt_socket_init Failed");
1276 /* Initialize HFP Audio Gateway */
1277 ret = _bt_audio_initialize(BT_AG_MODULE);
1278 if (ret != BLUETOOTH_ERROR_NONE)
1279 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
1281 /* Registering callback for receiving audio services searched */
1282 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
1283 if (ret != BLUETOOTH_ERROR_NONE)
1284 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
1286 ret = _bt_hdp_init();
1287 if (ret != BLUETOOTH_ERROR_NONE)
1288 BT_ERR("_bt_hdp_init Failed");
1290 ret = _bt_gatt_init();
1291 if (ret != BLUETOOTH_ERROR_NONE)
1292 BT_ERR("_bt_gatt_init Failed");
1294 ret = _bt_tds_init();
1295 if (ret != BLUETOOTH_ERROR_NONE)
1296 BT_ERR("_bt_tds_init Failed");
1298 /* Initialize HF Client */
1299 ret = _bt_audio_initialize(BT_HFP_MODULE);
1300 if (ret != BLUETOOTH_ERROR_NONE)
1301 BT_ERR("_bt_audio_initialize(BT_HFP_MODULE) Failed");
1303 return BLUETOOTH_ERROR_NONE;
1306 int _bt_cleanup_profiles(void)
1308 /* TODO: Cleanup bluetooth profiles */
1309 _bt_hidhost_deinitialize();
1310 _bt_socket_deinit();
1312 /* TODO: Cleanup bluetooth audio profiles
1313 //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
1314 //_bt_audio_deinitialize(BT_AVRCP_MODULE);
1315 //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
1316 //_bt_audio_deinitialize(BT_AG_MODULE);
1317 //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
1318 //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
1320 a2dp_init_pending = TRUE;
1325 return BLUETOOTH_ERROR_NONE;
1328 /* OAL post initialization handler */
1329 static void __bt_post_oal_init(void)
1332 int status = VCONFKEY_BT_STATUS_OFF;
1334 BT_DBG("OAL initialized");
1335 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0)
1336 BT_ERR("Fail to get the enabled value");
1338 if (status & VCONFKEY_BT_STATUS_ON) {
1339 ret = _bt_enable_adapter();
1340 if (ret != BLUETOOTH_ERROR_NONE)
1341 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
1345 /* OAL initialization handler */
1346 static void __bt_handle_oal_initialisation(oal_event_t event)
1351 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1352 __bt_post_oal_init();
1354 case OAL_EVENT_OAL_INITIALISED_FAILED:
1355 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1356 g_idle_add(_bt_terminate_service, NULL);
1359 BT_ERR("Unknown Event");
1364 static gboolean __bt_is_service_request_present(int service_function)
1367 invocation_info_t *req_info;
1371 /* Get method invocation context */
1372 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1374 if (req_info && req_info->service_function == service_function)
1382 /* Internal functions of core adapter service */
1383 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1387 invocation_info_t *req_info;
1390 /* Get method invocation context */
1391 for (l = _bt_get_invocation_list(); l != NULL; ) {
1393 l = g_slist_next(l);
1394 if (req_info == NULL || req_info->service_function != service_function)
1397 /* Create out param */
1398 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1400 switch (service_function) {
1401 case BT_ENABLE_ADAPTER:
1402 case BT_DISABLE_ADAPTER: {
1403 gboolean done = TRUE;
1404 g_array_append_vals(out_param, &done, sizeof(gboolean));
1407 case BT_GET_CONNECTABLE:
1408 case BT_GET_LOCAL_NAME:
1409 case BT_GET_LOCAL_ADDRESS:
1410 case BT_GET_LOCAL_VERSION:
1411 g_array_append_vals(out_param, user_data, size);
1413 case BT_IS_SERVICE_USED: {
1415 gboolean used = FALSE;
1416 unsigned char *uuid;
1417 char uuid_str[BT_UUID_STRING_SIZE];
1418 char *request_uuid = req_info->user_data;
1419 service_uuid_t *service_list = user_data;
1421 BT_INFO("Check for service uuid: %s", request_uuid);
1422 for (i = 0; i < size; i++) {
1423 uuid = service_list[i].uuid;
1424 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1425 BT_INFO("Adapter Service: [%s]", uuid_str);
1426 if (strcasecmp(uuid_str, request_uuid) == 0) {
1427 BT_INFO("UUID matched!!");
1433 g_array_append_vals(out_param, &used, sizeof(gboolean));
1436 case BT_GET_PROFILE_CONNECTED_DEVICES: {
1437 event_adapter_profile_connected_devices *event_data = user_data;
1438 bluetooth_device_address_t addr;
1440 for (i = 0; i < event_data->count; i++) {
1441 memcpy(&addr, &event_data->addr_list[i], sizeof(bluetooth_device_address_t));
1442 g_array_append_vals(out_param, &addr, sizeof(bluetooth_device_address_t));
1447 BT_ERR("Unknown service function[%d]", service_function);
1450 _bt_service_method_return(req_info->context, out_param, req_info->result);
1451 g_array_free(out_param, TRUE);
1452 /* Now free invocation info for this request*/
1453 _bt_free_info_from_invocation_list(req_info);
1457 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1459 char *phone_name = NULL;
1465 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1466 phone_name = vconf_keynode_get_str(node);
1468 if (phone_name && strlen(phone_name) != 0) {
1469 if (!g_utf8_validate(phone_name, -1,
1470 (const char **)&ptr))
1473 _bt_set_local_name(phone_name);
1478 /* Request return handlings */
1479 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1481 int result = BLUETOOTH_ERROR_NONE;
1483 BT_INFO("__bt_adapter_post_set_enabled>>");
1485 if (TIZEN_PROFILE_TV || TIZEN_FEATURE_ROBOT_REFERENCE ||
1486 !headed_plugin_info->plugin_headed_enabled) {
1487 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1488 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1489 BT_ERR("Fail to set discoverable mode");
1490 } else if (TIZEN_PROFILE_IOT || TIZEN_PROFILE_COMMON) {
1491 /* Tizen IOT Speaker - The default visible mode is off */
1492 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1493 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0))
1494 BT_ERR("Fail to set discoverable mode");
1496 __bt_set_visible_mode();
1499 /* add the vconf noti handler */
1500 if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1501 (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1502 BT_ERR("DEVICE_NAME key changed notification registration failed");
1504 __bt_set_local_name();
1506 /* Get All properties */
1507 if (OAL_STATUS_SUCCESS != adapter_get_properties())
1508 BT_ERR("adapter_get_properties failed");
1510 __bt_adapter_state_set_status(BT_ACTIVATED);
1512 /*Send BT Enabled event to application */
1513 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1514 g_variant_new("(i)", result));
1516 BT_PERMANENT_LOG("BT Enabled");
1521 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1523 BT_INFO("_bt_adapter_post_set_disabled>>");
1525 if (!TIZEN_PROFILE_TV) {
1526 /* Add Adapter disabled post processing codes */
1527 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1528 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1529 BT_ERR("vconf_ignore_key_changed failed");
1532 /* bt-service should be terminated when BT is off */
1533 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1534 /* TODO: Implement to check if it is the recovery mode or not */
1535 if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_ON) {
1536 /* Keep bt-service to handle Flight mode OFF */
1537 BT_DBG("Deactived due to flight mode. Keep bt-service");
1538 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1539 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_NONE);
1540 } else if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_OFF) {
1541 BT_DBG("Durning deactivation, receive Flight mode Off. Enable BT adapter");
1542 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1543 _bt_service_initialize();
1544 _bt_enable_adapter();
1545 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_NONE);
1547 _bt_reliable_terminate_service(NULL);
1549 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1555 static void __bt_adapter_update_bt_enabled(void)
1557 BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1558 if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1559 BT_ERR("Bluetooth profile init failed");
1561 _bt_device_state_handle_callback_set_request();
1563 _bt_device_handle_adapter_state(TRUE);
1565 /* Update Bluetooth Status to notify other modules */
1566 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1567 BT_ERR("Set vconf failed\n");
1569 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1570 BT_ERR("Set vconf failed\n");
1572 /* TODO:Add timer function to handle any further post processing */
1573 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1576 static void __bt_adapter_update_bt_disabled(void)
1578 int power_off_status = 0;
1581 BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1582 _bt_cleanup_profiles();
1584 _bt_device_handle_adapter_state(FALSE);
1586 /* Send the information to Absolute Volume Controller */
1587 _bt_audio_handle_adapter_disabled();
1589 /* Update the vconf BT status in normal Deactivation case only */
1590 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1591 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1593 /* Update Bluetooth Status to notify other modules */
1594 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1595 BT_ERR("Set vconf failed");
1597 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1598 BT_ERR("Set vconf failed\n");
1600 /* TODO:Add timer function to handle any further post processing */
1601 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1603 /* Pending BT Disabled event before bt-service's termination */
1606 static void __bt_adapter_state_set_status(bt_status_t status)
1608 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1609 adapter_state = status;
1612 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1614 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1615 adapter_discovery_state = status;
1618 static void __bt_adapter_state_change_callback(int bt_status)
1620 BT_INFO("BT adapter status changed [%d]", bt_status);
1622 switch (bt_status) {
1623 case BT_DEACTIVATED:
1624 __bt_adapter_state_set_status(BT_TERMINATING);
1626 /* Adapter is disabled, unregister event handlers */
1627 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1628 //_bt_deinit_device_event_handler();
1630 /* Add Adapter disabled post processing codes */
1631 __bt_adapter_update_bt_disabled();
1634 /* Add Adapter enabled post processing codes */
1636 BT_DBG("g_source is removed");
1637 g_source_remove(timer_id);
1641 /*If adapter gets activated when flight mode is ON*/
1642 if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_ON) {
1643 BT_DBG("Receive Flight mode On. Disable BT adapter");
1644 _bt_disable_adapter();
1648 __bt_adapter_update_bt_enabled();
1651 BT_ERR("Incorrect Bluetooth adapter state changed status");
1656 int _bt_set_adapter_request_state(int enable)
1658 return oal_set_adapter_request_state(enable);
1661 int _bt_set_le_request_state(int enable)
1663 return oal_set_le_request_state(enable);
1666 static int __bt_adapter_state_handle_request(gboolean enable)
1668 int result = BLUETOOTH_ERROR_NONE;
1670 switch (adapter_state) {
1672 case BT_DEACTIVATING:
1673 BT_INFO("Should not be callded : state [%d]", adapter_state);
1674 return BLUETOOTH_ERROR_INTERNAL;
1676 case BT_ACTIVATED: {
1677 BT_INFO("Adapter is currently in activated state, state [%d]",
1680 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1682 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1683 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1684 /*TODO Stop Discovery*/
1685 __bt_adapter_update_discovery_status(FALSE);
1687 result = adapter_disable();
1688 if (result != OAL_STATUS_SUCCESS) {
1689 BT_ERR("adapter_disable failed: [%d]", result);
1690 result = _bt_convert_oal_status_to_bt_error(result);
1691 /*TODO: perform if anything more needs to be done to handle failure */
1693 /* TODO: To be handled */
1694 __bt_adapter_state_set_status(BT_DEACTIVATING);
1695 result = BLUETOOTH_ERROR_NONE;
1700 case BT_DEACTIVATED: {
1701 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1704 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1706 result = adapter_enable();
1707 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1708 BT_ERR("adapter_enable failed: [%d]", result);
1710 result = _bt_convert_oal_status_to_bt_error(result);
1711 /*TODO: perform if anything more needs to be done to handle failure */
1713 /* TODO: To be handled */
1714 __bt_adapter_state_set_status(BT_ACTIVATING);
1715 result = BLUETOOTH_ERROR_NONE;
1721 BT_ERR("Unknown state: %d", adapter_state);
1725 if (enable && result == BLUETOOTH_ERROR_NONE) {
1726 /* Adapter enable request is successful, setup event handlers */
1727 _bt_service_register_event_handler_callback(
1728 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1733 static int __bt_adapter_state_discovery_request(gboolean enable,
1734 unsigned short max_response, unsigned short duration,
1735 unsigned int mask, gboolean is_custom,
1736 bt_discovery_role_type_t role)
1738 int result = BLUETOOTH_ERROR_NONE;
1741 switch (adapter_discovery_state) {
1742 case ADAPTER_DISCOVERY_STARTED: {
1743 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1744 adapter_discovery_state);
1746 return BLUETOOTH_ERROR_IN_PROGRESS;
1748 result = adapter_stop_inquiry();
1749 if (result != OAL_STATUS_SUCCESS) {
1750 BT_ERR("Discover stop failed: %d", result);
1751 result = _bt_convert_oal_status_to_bt_error(result);
1753 BT_ERR("Stop Discovery Triggered successfully");
1754 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1755 result = BLUETOOTH_ERROR_NONE;
1760 case ADAPTER_DISCOVERY_STARTING: {
1761 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1762 adapter_discovery_state);
1764 result = enable ? BLUETOOTH_ERROR_IN_PROGRESS :
1765 BLUETOOTH_ERROR_DEVICE_BUSY;
1769 case ADAPTER_DISCOVERY_STOPPED: {
1770 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1771 adapter_discovery_state);
1773 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1775 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1778 result = adapter_start_inquiry(duration);
1780 result = adapter_start_custom_inquiry(role);
1782 if (result != OAL_STATUS_SUCCESS) {
1783 BT_ERR("Start Discovery failed: %d", result);
1784 result = _bt_convert_oal_status_to_bt_error(result);
1786 BT_ERR("Start Discovery Triggered successfully");
1787 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1788 result = BLUETOOTH_ERROR_NONE;
1793 case ADAPTER_DISCOVERY_STOPPING: {
1794 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1795 adapter_discovery_state);
1797 result = enable ? BLUETOOTH_ERROR_DEVICE_BUSY :
1798 BLUETOOTH_ERROR_NOT_IN_OPERATION;
1803 BT_ERR("Unknown state: %d", adapter_discovery_state);
1811 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1813 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1814 GVariant *param = NULL;
1815 int result = BLUETOOTH_ERROR_NONE;
1817 switch (bt_discovery_status) {
1818 case ADAPTER_DISCOVERY_STOPPED:
1820 __bt_adapter_update_discovery_status(bt_discovery_status);
1821 param = g_variant_new("(i)", result);
1822 _bt_send_event(BT_ADAPTER_EVENT,
1823 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1827 case ADAPTER_DISCOVERY_STARTED:
1829 __bt_adapter_update_discovery_status(bt_discovery_status);
1830 param = g_variant_new("(i)", result);
1831 _bt_send_event(BT_ADAPTER_EVENT,
1832 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1837 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1841 static void __bt_set_visible_mode(void)
1844 int discoverable_state = DPM_BT_ERROR;
1846 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1847 BT_ERR("Fail to get the timeout value");
1849 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1850 if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
1851 if (_bt_set_discoverable_mode(
1852 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1853 timeout) != BLUETOOTH_ERROR_NONE) {
1854 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1855 BT_ERR("Set vconf failed");
1860 static void __bt_set_local_name(void)
1862 char *phone_name = NULL;
1865 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1870 if (strlen(phone_name) != 0) {
1871 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1874 _bt_set_local_name(phone_name);
1879 void _bt_adapter_set_status(bt_status_t status)
1881 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1882 adapter_state = status;
1885 bt_status_t _bt_adapter_get_status(void)
1887 return adapter_state;
1890 void _bt_set_disabled(int result)
1892 int power_off_status = 0;
1895 int pm_ignore_mode = 0;
1897 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1898 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1900 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1902 /* Update the vconf BT status in normal Deactivation case only */
1903 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1904 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1906 BT_DBG("Update vconf for BT normal Deactivation");
1908 if (result == BLUETOOTH_ERROR_TIMEOUT)
1909 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1910 BT_ERR("Set vconf failed");
1912 /* Update Bluetooth Status to notify other modules */
1913 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1914 BT_ERR("Set vconf failed");
1916 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1917 EVT_VAL_BT_OFF) != ES_R_OK)
1918 BT_ERR("Fail to set value");
1921 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1922 BT_ERR("Set vconf failed\n");
1924 _bt_adapter_set_status(BT_DEACTIVATED);
1925 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1927 /* Send BT Disabled event to application */
1928 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1929 g_variant_new("(i)", result));
1931 BT_INFO("Adapter disabled");
1932 BT_PERMANENT_LOG("BT Disabled");