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 #ifdef TIZEN_FEATURE_BT_PERMANENT_LOG
224 void _bt_start_log_dump(const char *path)
228 GError *error = NULL;
230 ret_if(path == NULL);
232 BT_INFO("Start log_dump(path = %s)", path);
234 _bt_send_dump_signal(BT_DUMP_SERVICE_START_SIGNAL);
236 proxy = __bt_get_core_proxy();
237 ret_if(proxy == NULL);
239 result = g_dbus_proxy_call_sync(proxy, "LogDump",
240 g_variant_new("(s)", path),
241 G_DBUS_CALL_FLAGS_NONE,
246 BT_ERR("Bt core call failed(Error: %s)", error->message);
247 g_clear_error(&error);
249 BT_ERR("Bt core call failed");
252 g_variant_unref(result);
255 _bt_send_dump_signal(BT_DUMP_SERVICE_FINISH_SIGNAL);
259 int _bt_check_adapter(int *status)
262 gboolean powered = FALSE;
263 BT_CHECK_PARAMETER(status, return);
265 *status = BT_ADAPTER_DISABLED;
267 if (OAL_STATUS_SUCCESS != adapter_get_powered_status(&powered))
268 return BLUETOOTH_ERROR_INTERNAL;
270 BT_INFO("Adapter is in Powered Up state");
271 *status = BT_ADAPTER_ENABLED;
273 BT_INFO("Adapter is in Powered Down state");
275 return BLUETOOTH_ERROR_NONE;
278 int _bt_disable_adapter(void)
280 bt_le_status_t le_status = BT_LE_DEACTIVATED;
282 le_status = _bt_adapter_get_le_status();
284 BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
286 if (adapter_state == BT_DEACTIVATING ||
287 adapter_state == BT_TERMINATING ||
288 le_status == BT_LE_DEACTIVATING) {
289 BT_DBG("Disabling in progress");
290 return BLUETOOTH_ERROR_IN_PROGRESS;
293 if (adapter_state == BT_DEACTIVATED) {
294 BT_DBG("Already disabled");
295 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
298 if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
299 BT_ERR("Enabling in progress");
300 return BLUETOOTH_ERROR_DEVICE_BUSY;
303 /* TODO: Need to add the function to disconnect all devices */
304 /* __bt_disconnect_all(); */
306 return __bt_adapter_state_handle_request(FALSE);
309 int _bt_start_discovery(unsigned short max_response,
310 unsigned short duration, unsigned int cod_mask)
312 return __bt_adapter_state_discovery_request(TRUE, max_response, duration,
313 cod_mask, FALSE, 0x00);
316 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
318 return __bt_adapter_state_discovery_request(TRUE, 0, 0, 0, TRUE, role);
321 int _bt_cancel_discovery(void)
323 return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0, FALSE, 0x00);
326 gboolean _bt_is_discovering(void)
328 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
329 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
335 static void __bt_set_flightmode_request(int value)
337 BT_INFO("Setting flightmode request to %d", value);
338 flightmode_request = value;
341 static int __bt_is_flightmode_request(void)
343 return flightmode_request;
346 static void __bt_service_handle_flight_mode(gboolean flight_mode, bt_status_t adapter_status)
348 int flight_mode_value = 0;
349 int ps_mode_value = 0;
351 if (flight_mode == TRUE) {
352 BT_INFO_C("### Flight mode on. Turn off BT");
354 if (adapter_status != BT_ACTIVATED) {
355 BT_INFO("No need to control bt status");
356 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
357 BT_ERR("Fail get PS mode value");
359 BT_DBG("ps_mode_value %d", ps_mode_value);
360 if (ps_mode_value > 0) {
361 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
362 BT_ERR("Set vconf failed");
367 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_ON);
369 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
370 BT_ERR("Set vconf failed");
372 if (adapter_status == BT_ACTIVATED)
373 _bt_disable_adapter();
376 BT_INFO_C("### Flight mode off. Turn on BT");
378 if (adapter_status == BT_ACTIVATED)
381 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value))
382 BT_ERR("Fail get flight mode value");
384 BT_DBG("flight_mode_value %d", flight_mode_value);
385 if (flight_mode_value == 0)
388 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
389 BT_ERR("Set vconf failed");
391 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
392 BT_ERR("Fail get PS mode value");
394 BT_DBG("ps_mode_value %d", ps_mode_value);
395 if (ps_mode_value > 0) {
396 BT_DBG("Bt should not turn on");
400 if (adapter_status == BT_DEACTIVATING) {
401 BT_INFO("BT adapter is deactivating. Turn on BT after deactivation");
402 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_OFF);
406 if (adapter_status != BT_DEACTIVATED) {
407 BT_INFO("No need to control bt status");
410 _bt_service_initialize();
412 if (adapter_status == BT_DEACTIVATED)
413 _bt_enable_adapter();
418 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
421 bt_status_t adapter_status = BT_DEACTIVATED;
422 gboolean flight_mode = FALSE;
424 adapter_status = _bt_adapter_get_status();
425 BT_INFO("Flight mode changed. Current bt status is %d", adapter_status);
427 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
428 type = vconf_keynode_get_type(node);
430 if (type == VCONF_TYPE_BOOL) {
431 flight_mode = vconf_keynode_get_bool(node);
432 __bt_service_handle_flight_mode(flight_mode, adapter_status);
434 BT_ERR("Invaild vconf key type : %d", type);
438 void _bt_service_register_vconf_handler(void)
442 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
443 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
444 BT_ERR("Unable to register key handler");
445 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
446 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
447 BT_ERR("Unable to register key handler");
450 void _bt_service_unregister_vconf_handler(void)
454 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
455 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
457 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
458 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
461 int _bt_get_local_address(void)
467 result = adapter_get_address();
468 if (result != OAL_STATUS_SUCCESS) {
469 BT_ERR("adapter_get_address failed: %d", result);
470 result = BLUETOOTH_ERROR_INTERNAL;
472 result = BLUETOOTH_ERROR_NONE;
478 int _bt_get_local_version(void)
483 result = adapter_get_version();
484 if (result != OAL_STATUS_SUCCESS) {
485 BT_ERR("adapter_get_version failed: %d", result);
486 result = BLUETOOTH_ERROR_INTERNAL;
488 result = BLUETOOTH_ERROR_NONE;
494 int _bt_get_local_name(void)
500 result = adapter_get_name();
501 if (result != OAL_STATUS_SUCCESS) {
502 BT_ERR("adapter_get_name failed: %d", result);
503 result = BLUETOOTH_ERROR_INTERNAL;
505 result = BLUETOOTH_ERROR_NONE;
511 int _bt_set_local_name(char *local_name)
513 int result = BLUETOOTH_ERROR_NONE;
516 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
518 result = adapter_set_name(local_name);
519 if (result != OAL_STATUS_SUCCESS) {
520 BT_ERR("adapter_set_name failed: %d", result);
521 result = BLUETOOTH_ERROR_INTERNAL;
523 result = BLUETOOTH_ERROR_NONE;
529 int _bt_get_discoverable_mode(int *mode)
536 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
538 adapter_is_discoverable(&scan_mode);
539 if (TRUE == scan_mode) {
540 adapter_get_discoverable_timeout(&timeout);
542 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
544 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
546 adapter_is_connectable(&scan_mode);
547 if (scan_mode == TRUE)
548 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
551 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
552 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
553 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
560 return BLUETOOTH_ERROR_NONE;
563 int _bt_get_timeout_value(int *timeout)
568 /* Take current time */
570 time_diff = difftime(current_time, visible_timer.start_time);
572 BT_DBG("Time diff = %d\n", time_diff);
573 *timeout = visible_timer.timeout - time_diff;
575 return BLUETOOTH_ERROR_NONE;
578 static void __bt_visibility_alarm_remove()
580 if (visible_timer.event_id > 0) {
581 g_source_remove(visible_timer.event_id);
582 visible_timer.event_id = 0;
585 if (visible_timer.alarm_id > 0) {
586 alarmmgr_remove_alarm(visible_timer.alarm_id);
587 visible_timer.alarm_id = 0;
591 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
593 int result = BLUETOOTH_ERROR_NONE;
596 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
598 if (alarm_id != visible_timer.alarm_id)
601 if (visible_timer.event_id) {
602 _bt_send_event(BT_ADAPTER_EVENT,
603 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
604 g_variant_new("(in)", result, timeout));
605 g_source_remove(visible_timer.event_id);
606 visible_timer.event_id = 0;
607 visible_timer.timeout = 0;
609 if (!TIZEN_PROFILE_WEARABLE) {
610 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
611 BT_ERR("Set vconf failed\n");
614 /* Switch Off visibility in Bluez */
615 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
616 visible_timer.alarm_id = 0;
620 static gboolean __bt_timeout_handler(gpointer user_data)
622 int result = BLUETOOTH_ERROR_NONE;
626 /* Take current time */
628 time_diff = difftime(current_time, visible_timer.start_time);
630 /* Send event to application */
631 _bt_send_event(BT_ADAPTER_EVENT,
632 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
633 g_variant_new("(in)", result, time_diff));
635 if (visible_timer.timeout <= time_diff) {
636 g_source_remove(visible_timer.event_id);
637 visible_timer.event_id = 0;
638 visible_timer.timeout = 0;
640 if (!TIZEN_PROFILE_WEARABLE) {
641 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
642 BT_ERR("Set vconf failed\n");
651 static void __bt_visibility_alarm_create()
656 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
659 BT_ERR("Failed to create alarm error = %d\n", result);
661 BT_DBG("Alarm created = %d\n", alarm_id);
662 visible_timer.alarm_id = alarm_id;
666 int _bt_start_visibility_timer(int timeout)
669 #ifdef TIZEN_FEATURE_BT_DPM
670 int discoverable_state = DPM_BT_ERROR;
673 __bt_visibility_alarm_remove();
675 visible_timer.timeout = timeout;
677 #ifdef TIZEN_FEATURE_BT_DPM
678 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
679 if (discoverable_state != DPM_RESTRICTED) {
681 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
682 BT_ERR("Set vconf failed");
683 #ifdef TIZEN_FEATURE_BT_DPM
688 return BLUETOOTH_ERROR_NONE;
690 if (!visible_timer.alarm_init) {
691 /* Set Alarm timer to switch off BT */
692 result = alarmmgr_init("bt-service");
694 return BLUETOOTH_ERROR_INTERNAL;
696 visible_timer.alarm_init = TRUE;
699 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
701 return BLUETOOTH_ERROR_INTERNAL;
703 if (!TIZEN_PROFILE_WEARABLE) {
704 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
705 BT_ERR("Set vconf failed");
708 /* Take start time */
709 time(&(visible_timer.start_time));
710 visible_timer.event_id = g_timeout_add_seconds(1,
711 __bt_timeout_handler, NULL);
713 visible_timer.timeout = timeout;
715 __bt_visibility_alarm_create();
717 return BLUETOOTH_ERROR_NONE;
720 int _bt_stop_visibility_timer(void)
722 __bt_visibility_alarm_remove();
724 visible_timer.timeout = 0;
726 if (!TIZEN_PROFILE_WEARABLE) {
727 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
728 BT_ERR("Set vconf failed");
731 return BLUETOOTH_ERROR_NONE;
734 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
737 int ret = BLUETOOTH_ERROR_NONE;;
738 #ifdef TIZEN_FEATURE_BT_DPM
739 int discoverable_state = DPM_BT_ERROR;
744 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
746 #ifdef TIZEN_FEATURE_BT_DPM
747 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
748 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
749 discoverable_state == DPM_RESTRICTED) {
750 if (headed_plugin_info->plugin_headed_enabled)
751 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
752 return BLUETOOTH_ERROR_ACCESS_DENIED;
754 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE &&
755 discoverable_state == DPM_RESTRICTED) {
756 if (headed_plugin_info->plugin_headed_enabled)
757 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
758 return BLUETOOTH_ERROR_ACCESS_DENIED;
762 switch (discoverable_mode) {
763 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
764 result = adapter_set_connectable(TRUE);
767 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
768 result = adapter_set_discoverable();
771 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
772 result = adapter_set_discoverable();
775 return BLUETOOTH_ERROR_INVALID_PARAM;
778 if (result != OAL_STATUS_SUCCESS) {
779 BT_ERR("set scan mode failed %d", result);
780 return BLUETOOTH_ERROR_INTERNAL;
783 result = adapter_set_discoverable_timeout(timeout);
784 if (result != OAL_STATUS_SUCCESS) {
785 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
786 return BLUETOOTH_ERROR_INTERNAL;
789 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
790 ret = _bt_stop_visibility_timer();
791 if (BLUETOOTH_ERROR_NONE != ret)
792 BT_ERR("_bt_stop_visibility_timer failed");
793 if (!TIZEN_PROFILE_WEARABLE) {
794 if (vconf_set_int(BT_FILE_VISIBLE_TIME, -1) != 0)
795 BT_ERR("Set vconf failed");
797 } else if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE) {
798 ret = _bt_start_visibility_timer(timeout);
799 if (BLUETOOTH_ERROR_NONE != ret)
800 BT_ERR("_bt_start_visibility_timer failed");
803 ret = _bt_stop_visibility_timer();
804 if (BLUETOOTH_ERROR_NONE != ret)
805 BT_ERR("_bt_stop_visibility_timer failed");
812 gboolean _bt_is_connectable(void)
819 adapter_is_connectable(&connectable);
825 BT_DBG("Connectable: [%s]", result ? "TRUE" : "FALSE");
830 int _bt_set_connectable(gboolean connectable)
832 int result = BLUETOOTH_ERROR_NONE;
835 result = adapter_set_connectable(connectable);
836 if (result != OAL_STATUS_SUCCESS) {
837 BT_ERR("adapter_set_connectable failed: %d", result);
838 result = BLUETOOTH_ERROR_INTERNAL;
840 result = BLUETOOTH_ERROR_NONE;
846 int _bt_get_connectable(void)
852 result = adapter_get_connectable();
853 if (result != OAL_STATUS_SUCCESS) {
854 BT_ERR("adapter_get_connectable failed: %d", result);
855 result = BLUETOOTH_ERROR_INTERNAL;
857 result = BLUETOOTH_ERROR_NONE;
863 int _bt_is_service_used(void)
869 result = adapter_get_service_uuids();
870 if (result != OAL_STATUS_SUCCESS) {
871 BT_ERR("adapter_get_service_uuids failed: %d", result);
872 result = BLUETOOTH_ERROR_INTERNAL;
874 result = BLUETOOTH_ERROR_NONE;
881 int _bt_adapter_get_bonded_devices(void)
883 int result = BLUETOOTH_ERROR_NONE;
886 result = adapter_get_bonded_devices();
887 if (result != OAL_STATUS_SUCCESS) {
888 BT_ERR("adapter_get_bonded_devices failed: %d", result);
889 result = BLUETOOTH_ERROR_INTERNAL;
891 result = BLUETOOTH_ERROR_NONE;
897 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
900 GDBusConnection *conn;
901 GDBusProxy *manager_proxy;
902 GVariant *result = NULL;
903 GVariant *result1 = NULL;
904 GVariantIter *iter = NULL;
905 GError *error = NULL;
906 char *object_path = NULL;
907 GVariantIter *interface_iter;
908 char *interface_str = NULL;
909 GDBusProxy *device_proxy = NULL;
910 gboolean is_connected = FALSE;
912 conn = _bt_gdbus_get_system_gconn();
913 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
915 manager_proxy = _bt_get_manager_proxy();
916 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
918 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
920 G_DBUS_CALL_FLAGS_NONE,
927 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
928 g_clear_error(&error);
931 BT_ERR("Failed to Failed to GetManagedObjects");
932 return BLUETOOTH_ERROR_INTERNAL;
935 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
936 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
938 /* Parse the signature: oa{sa{sv}}} */
939 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
940 if (object_path == NULL)
943 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
944 &interface_str, NULL)) {
945 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
946 BT_DBG("Found a device: %s", object_path);
947 g_free(interface_str);
949 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
951 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
953 if (device_proxy == NULL) {
954 BT_DBG("Device don't have this service");
958 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
959 g_variant_new("(s)", profile_uuid),
960 G_DBUS_CALL_FLAGS_NONE,
965 if (result1 == NULL) {
966 BT_ERR("Error occured in Proxy call");
968 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
972 g_object_unref(device_proxy);
975 g_variant_get(result1, "(b)", &is_connected);
977 if (is_connected == TRUE) {
978 char address[BT_ADDRESS_STRING_SIZE];
979 bluetooth_device_address_t *addr = NULL;
981 _bt_convert_device_path_to_address(object_path, address);
983 addr = g_malloc0(sizeof(bluetooth_device_address_t));
984 _bt_convert_addr_string_to_type(addr->addr, address);
986 g_array_append_vals(*addr_list, addr,
987 sizeof(bluetooth_device_address_t));
990 g_variant_unref(result1);
991 g_object_unref(device_proxy);
998 g_variant_unref(result);
999 g_variant_iter_free(iter);
1002 return BLUETOOTH_ERROR_NONE;
1005 static gboolean __bt_disconnect_all(void)
1008 gboolean ret = FALSE;
1009 GArray *device_list;
1012 bluetooth_device_info_t *info;
1015 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
1017 BT_DBG("The length of device list after the initialisation is [%d]", device_list->len);
1019 _bt_service_get_bonded_dev_list(&device_list);
1021 size = (device_list->len) / sizeof(bluetooth_device_info_t);
1023 for (i = 0; i < size; i++) {
1024 info = &g_array_index(device_list,
1025 bluetooth_device_info_t, i);
1026 if (info->connected != BLUETOOTH_CONNECTED_LINK_NONE) {
1027 BT_DBG("Found Connected Device");
1028 result = _bt_disconnect_device(&info->device_address);
1029 if (result != BLUETOOTH_ERROR_NONE) {
1030 BT_ERR("Cannot disconnect all the devices. bt_disconnect_device Failed");
1031 g_array_free(device_list, TRUE);
1037 g_array_free(device_list, TRUE);
1042 static void __bt_handle_pending_a2dp_init(unsigned int a2dp_role)
1046 if (!a2dp_init_pending)
1050 a2dp_init_pending = FALSE;
1052 if (a2dp_role == BT_A2DP_SINK_ROLE) {
1053 BT_INFO("Enable A2DP Sink role");
1054 /* Initialize A2DP Sink */
1055 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
1056 if (ret != BLUETOOTH_ERROR_NONE)
1057 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
1059 /* Initialize AVRCP Controller */
1060 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
1061 if (ret != BLUETOOTH_ERROR_NONE)
1062 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
1064 _bt_audio_set_current_role(BLUETOOTH_A2DP_SINK);
1068 BT_INFO("Enable A2DP Source role");
1069 /* Initialize A2DP Source */
1070 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
1071 if (ret != BLUETOOTH_ERROR_NONE)
1072 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
1074 /* Initialize AVRCP Target */
1075 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
1076 if (ret != BLUETOOTH_ERROR_NONE)
1077 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
1079 _bt_audio_set_current_role(BLUETOOTH_A2DP_SOURCE);
1083 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
1085 int result = BLUETOOTH_ERROR_NONE;
1089 switch (event_type) {
1090 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1091 case OAL_EVENT_OAL_INITIALISED_FAILED:
1092 __bt_handle_oal_initialisation(event_type);
1094 case OAL_EVENT_ADAPTER_ENABLED:
1095 __bt_adapter_state_change_callback(BT_ACTIVATED);
1097 case OAL_EVENT_ADAPTER_DISABLED:
1098 __bt_adapter_state_change_callback(BT_DEACTIVATED);
1100 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
1101 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
1103 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
1104 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
1106 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
1107 bt_address_t *bd_addr = event_data;
1108 bluetooth_device_address_t local_address;
1111 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
1112 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
1113 local_address.addr[0], local_address.addr[1], local_address.addr[2],
1114 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
1116 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
1117 (void *) &local_address, sizeof(bluetooth_device_address_t));
1120 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
1121 char *name = event_data;
1122 BT_DBG("Adapter Name: %s", name);
1124 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
1125 bluetooth_device_name_t local_name;
1127 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
1128 g_strlcpy(local_name.name,
1129 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
1130 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
1131 (void *) &local_name, sizeof(bluetooth_device_name_t));
1133 /* Send event to application */
1134 _bt_send_event(BT_ADAPTER_EVENT,
1135 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
1136 g_variant_new("(is)", result, name));
1140 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
1141 char *ver = event_data;
1142 bluetooth_version_t local_version;
1144 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
1145 g_strlcpy(local_version.version,
1146 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
1147 BT_DBG("BT Version: %s", local_version.version);
1149 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
1150 (void *) &local_version, sizeof(bluetooth_version_t));
1153 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
1155 gboolean connectable = FALSE;
1157 BT_INFO("Adapter discoverable mode:"
1158 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
1160 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1161 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1162 (void *) &connectable, sizeof(gboolean));
1165 _bt_send_event(BT_ADAPTER_EVENT,
1166 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1167 g_variant_new("(b)", connectable));
1169 _bt_send_event(BT_ADAPTER_EVENT,
1170 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1171 g_variant_new("(in)", result, mode));
1175 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
1177 gboolean connectable = TRUE;
1179 BT_INFO("Adapter discoverable mode:"
1180 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
1182 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1183 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1184 (void *) &connectable, sizeof(gboolean));
1188 _bt_send_event(BT_ADAPTER_EVENT,
1189 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1190 g_variant_new("(b)", connectable));
1192 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1193 _bt_send_event(BT_ADAPTER_EVENT,
1194 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1195 g_variant_new("(in)", result, mode));
1199 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
1201 gboolean connectable = TRUE;
1203 BT_INFO("Adapter discoverable mode:"
1204 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
1206 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1207 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1208 (void *) &connectable, sizeof(gboolean));
1211 /* Send event to application */
1212 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1213 _bt_send_event(BT_ADAPTER_EVENT,
1214 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1215 g_variant_new("(in)", result, mode));
1219 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
1220 int *timeout = event_data;
1223 BT_INFO("Discoverable timeout: [%d]", *timeout);
1225 /* Send event to application */
1226 _bt_get_discoverable_mode(&mode);
1227 _bt_send_event(BT_ADAPTER_EVENT,
1228 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1229 g_variant_new("(in)", result, mode));
1232 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
1234 service_uuid_t *service_list;
1235 event_adapter_services_t *list = event_data;
1238 service_list = list->service_list;
1239 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
1241 char uuids[512] = {0,};
1242 char standard_prefix[2] = {0,};
1244 for (int i = 0; i < count; i++) {
1246 g_snprintf(uuids + (index++), 2, ",");
1247 if (memcmp(service_list[i].uuid, standard_prefix, 2) == 0) {
1248 g_snprintf(uuids + index, 5, "%.2x%.2x",
1249 service_list[i].uuid[2], service_list[i].uuid[3]);
1252 g_snprintf(uuids + index, 9, "%.2x%.2x..%.2x,",
1253 service_list[i].uuid[0], service_list[i].uuid[1], service_list[i].uuid[15]);
1257 BT_PERMANENT_LOG("UUIDs %s", uuids);
1260 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
1263 bluetooth_device_address_t *addr_list;
1265 event_device_list_t *bonded_device_list = event_data;
1266 count = bonded_device_list->num;
1268 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
1269 for (i = 0; i < count; i++) {
1270 memcpy(addr_list[i].addr,
1271 bonded_device_list->devices[i].addr,
1272 BLUETOOTH_ADDRESS_LENGTH);
1275 BT_INFO("Adapter Bonded device List count: [%d]", count);
1276 _bt_device_handle_paired_address_list(addr_list, bonded_device_list->num);
1280 case OAL_EVENT_ADAPTER_PROPERTY_A2DP_ROLE: {
1281 unsigned int *a2dp_role = event_data;
1283 BT_INFO("a2dp role: [%u]", *a2dp_role);
1285 __bt_handle_pending_a2dp_init(*a2dp_role);
1290 BT_ERR("Unhandled event..");
1297 int _bt_init_profiles()
1302 /*TODO: Init bluetooth profiles */
1303 ret = _bt_hidhost_initialize();
1304 if (ret != BLUETOOTH_ERROR_NONE)
1305 BT_ERR("_bt_hidhost_initialize Failed");
1307 ret = _bt_hiddevice_initialize();
1308 if (ret != BLUETOOTH_ERROR_NONE)
1309 BT_ERR("_bt_hiddevice_initialize Failed");
1311 ret = _bt_socket_init();
1312 if (ret != BLUETOOTH_ERROR_NONE)
1313 BT_ERR("_bt_socket_init Failed");
1315 /* Initialize HFP Audio Gateway */
1316 ret = _bt_audio_initialize(BT_AG_MODULE);
1317 if (ret != BLUETOOTH_ERROR_NONE)
1318 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
1320 /* Registering callback for receiving audio services searched */
1321 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
1322 if (ret != BLUETOOTH_ERROR_NONE)
1323 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
1325 ret = _bt_hdp_init();
1326 if (ret != BLUETOOTH_ERROR_NONE)
1327 BT_ERR("_bt_hdp_init Failed");
1329 ret = _bt_gatt_init();
1330 if (ret != BLUETOOTH_ERROR_NONE)
1331 BT_ERR("_bt_gatt_init Failed");
1333 /* Initialize HF Client */
1334 ret = _bt_audio_initialize(BT_HFP_MODULE);
1335 if (ret != BLUETOOTH_ERROR_NONE)
1336 BT_ERR("_bt_audio_initialize(BT_HFP_MODULE) Failed");
1338 return BLUETOOTH_ERROR_NONE;
1341 int _bt_cleanup_profiles(void)
1343 /* TODO: Cleanup bluetooth profiles */
1344 _bt_hidhost_deinitialize();
1345 _bt_socket_deinit();
1347 /* TODO: Cleanup bluetooth audio profiles */
1348 //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
1349 //_bt_audio_deinitialize(BT_AVRCP_MODULE);
1350 //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
1351 //_bt_audio_deinitialize(BT_AG_MODULE);
1352 //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
1353 //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
1355 a2dp_init_pending = TRUE;
1360 return BLUETOOTH_ERROR_NONE;
1363 /* OAL post initialization handler */
1364 static void __bt_post_oal_init(void)
1367 int status = VCONFKEY_BT_STATUS_OFF;
1369 BT_DBG("OAL initialized");
1370 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0)
1371 BT_ERR("Fail to get the enabled value");
1374 /* Update Bluetooth Status to OFF */
1375 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1376 BT_ERR("Set vconf failed\n");
1379 if (status & VCONFKEY_BT_STATUS_ON) {
1380 ret = _bt_enable_adapter();
1381 if (ret != BLUETOOTH_ERROR_NONE)
1382 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
1386 /* OAL initialization handler */
1387 static void __bt_handle_oal_initialisation(oal_event_t event)
1392 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1393 __bt_post_oal_init();
1395 case OAL_EVENT_OAL_INITIALISED_FAILED:
1396 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1397 g_idle_add(_bt_terminate_service, NULL);
1400 BT_ERR("Unknown Event");
1405 static gboolean __bt_is_service_request_present(int service_function)
1408 invocation_info_t *req_info;
1412 /* Get method invocation context */
1413 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1415 if (req_info && req_info->service_function == service_function)
1423 /* Internal functions of core adapter service */
1424 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1428 invocation_info_t *req_info;
1431 /* Get method invocation context */
1432 for (l = _bt_get_invocation_list(); l != NULL; ) {
1434 l = g_slist_next(l);
1435 if (req_info == NULL || req_info->service_function != service_function)
1438 /* Create out param */
1439 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1441 switch (service_function) {
1442 case BT_ENABLE_ADAPTER:
1443 case BT_DISABLE_ADAPTER: {
1444 gboolean done = TRUE;
1445 g_array_append_vals(out_param, &done, sizeof(gboolean));
1448 case BT_GET_CONNECTABLE:
1449 case BT_GET_LOCAL_NAME:
1450 case BT_GET_LOCAL_ADDRESS:
1451 case BT_GET_LOCAL_VERSION:
1452 g_array_append_vals(out_param, user_data, size);
1454 case BT_IS_SERVICE_USED: {
1456 gboolean used = FALSE;
1457 unsigned char *uuid;
1458 char uuid_str[BT_UUID_STRING_SIZE];
1459 char *request_uuid = req_info->user_data;
1460 service_uuid_t *service_list = user_data;
1462 BT_INFO("Check for service uuid: %s", request_uuid);
1463 for (i = 0; i < size; i++) {
1464 uuid = service_list[i].uuid;
1465 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1466 BT_INFO("Adapter Service: [%s]", uuid_str);
1467 if (strcasecmp(uuid_str, request_uuid) == 0) {
1468 BT_INFO("UUID matched!!");
1474 g_array_append_vals(out_param, &used, sizeof(gboolean));
1478 BT_ERR("Unknown service function[%d]", service_function);
1481 _bt_service_method_return(req_info->context, out_param, req_info->result);
1482 g_array_free(out_param, TRUE);
1483 /* Now free invocation info for this request*/
1484 _bt_free_info_from_invocation_list(req_info);
1488 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1490 char *phone_name = NULL;
1496 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1497 phone_name = vconf_keynode_get_str(node);
1499 if (phone_name && strlen(phone_name) != 0) {
1500 if (!g_utf8_validate(phone_name, -1,
1501 (const char **)&ptr))
1504 _bt_set_local_name(phone_name);
1509 /* Request return handlings */
1510 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1512 int result = BLUETOOTH_ERROR_NONE;
1514 BT_INFO("__bt_adapter_post_set_enabled>>");
1516 if (TIZEN_PROFILE_TV || !headed_plugin_info->plugin_headed_enabled) {
1517 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1518 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1519 BT_ERR("Fail to set discoverable mode");
1521 __bt_set_visible_mode();
1524 /* add the vconf noti handler */
1525 if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1526 (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1527 BT_ERR("DEVICE_NAME key changed notification registration failed");
1529 __bt_set_local_name();
1531 /* Get All properties */
1532 if (OAL_STATUS_SUCCESS != adapter_get_properties())
1533 BT_ERR("adapter_get_properties failed");
1535 __bt_adapter_state_set_status(BT_ACTIVATED);
1537 /*Send BT Enabled event to application */
1538 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1539 g_variant_new("(i)", result));
1541 BT_PERMANENT_LOG("BT Enabled");
1546 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1548 BT_INFO("_bt_adapter_post_set_disabled>>");
1550 if (!TIZEN_PROFILE_TV) {
1551 /* Add Adapter disabled post processing codes */
1552 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1553 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1554 BT_ERR("vconf_ignore_key_changed failed");
1557 /* bt-service should be terminated when BT is off */
1558 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1559 /* TODO: Implement to check if it is the recovery mode or not */
1560 if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_ON) {
1561 /* Keep bt-service to handle Flight mode OFF */
1562 BT_DBG("Deactived due to flight mode. Keep bt-service");
1563 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1564 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_NONE);
1565 } else if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_OFF) {
1566 BT_DBG("Durning deactivation, receive Flight mode Off. Enable BT adapter");
1567 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1568 _bt_service_initialize();
1569 _bt_enable_adapter();
1570 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_NONE);
1572 _bt_reliable_terminate_service(NULL);
1578 static void __bt_adapter_update_bt_enabled(void)
1580 BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1581 if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1582 BT_ERR("Bluetooth profile init failed");
1584 _bt_device_handle_adapter_state(TRUE);
1586 /* Update Bluetooth Status to notify other modules */
1587 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1588 BT_ERR("Set vconf failed\n");
1590 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1591 BT_ERR("Set vconf failed\n");
1593 /* TODO:Add timer function to handle any further post processing */
1594 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1597 static void __bt_adapter_update_bt_disabled(void)
1599 int power_off_status = 0;
1602 BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1603 _bt_cleanup_profiles();
1605 _bt_device_handle_adapter_state(FALSE);
1607 #ifdef TIZEN_FEATURE_BT_AVC_TARGET
1608 /* Send the information to Absolute Volume Controller */
1609 _bt_audio_handle_adapter_disabled();
1612 /* Update the vconf BT status in normal Deactivation case only */
1613 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1614 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1616 /* Update Bluetooth Status to notify other modules */
1617 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1618 BT_ERR("Set vconf failed");
1620 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1621 BT_ERR("Set vconf failed\n");
1623 /* TODO:Add timer function to handle any further post processing */
1624 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1626 /* Pending BT Disabled event before bt-service's termination */
1629 static void __bt_adapter_state_set_status(bt_status_t status)
1631 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1632 adapter_state = status;
1635 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1637 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1638 adapter_discovery_state = status;
1641 static void __bt_adapter_state_change_callback(int bt_status)
1643 BT_INFO("BT adapter status changed [%d]", bt_status);
1645 switch (bt_status) {
1646 case BT_DEACTIVATED:
1647 __bt_adapter_state_set_status(BT_TERMINATING);
1649 /* Adapter is disabled, unregister event handlers */
1650 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1651 //_bt_deinit_device_event_handler();
1653 /* Add Adapter disabled post processing codes */
1654 __bt_adapter_update_bt_disabled();
1657 /* Add Adapter enabled post processing codes */
1659 BT_DBG("g_source is removed");
1660 g_source_remove(timer_id);
1664 /*If adapter gets activated when flight mode is ON*/
1665 if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_ON) {
1666 BT_DBG("Receive Flight mode On. Disable BT adapter");
1667 _bt_disable_adapter();
1671 __bt_adapter_update_bt_enabled();
1674 BT_ERR("Incorrect Bluetooth adapter state changed status");
1679 int _bt_set_adapter_request_state(int enable)
1681 return oal_set_adapter_request_state(enable);
1684 int _bt_set_le_request_state(int enable)
1686 return oal_set_le_request_state(enable);
1689 static int __bt_adapter_state_handle_request(gboolean enable)
1691 int result = BLUETOOTH_ERROR_NONE;
1693 switch (adapter_state) {
1695 case BT_DEACTIVATING:
1696 BT_INFO("Should not be callded : state [%d]", adapter_state);
1697 return BLUETOOTH_ERROR_INTERNAL;
1699 case BT_ACTIVATED: {
1700 BT_INFO("Adapter is currently in activated state, state [%d]",
1703 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1705 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1706 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1707 /*TODO Stop Discovery*/
1708 __bt_adapter_update_discovery_status(FALSE);
1710 result = adapter_disable();
1711 if (result != OAL_STATUS_SUCCESS) {
1712 BT_ERR("adapter_enable failed: [%d]", result);
1713 result = BLUETOOTH_ERROR_INTERNAL;
1714 /*TODO: perform if anything more needs to be done to handle failure */
1716 /* TODO: To be handled */
1717 __bt_adapter_state_set_status(BT_DEACTIVATING);
1718 result = BLUETOOTH_ERROR_NONE;
1723 case BT_DEACTIVATED: {
1724 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1727 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1729 result = adapter_enable();
1730 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1731 BT_ERR("adapter_enable failed: [%d]", result);
1733 result = BLUETOOTH_ERROR_INTERNAL;
1734 /*TODO: perform if anything more needs to be done to handle failure */
1736 /* TODO: To be handled */
1737 __bt_adapter_state_set_status(BT_ACTIVATING);
1738 result = BLUETOOTH_ERROR_NONE;
1744 BT_ERR("Unknown state: %d", adapter_state);
1748 if (enable && result == BLUETOOTH_ERROR_NONE) {
1749 /* Adapter enable request is successful, setup event handlers */
1750 _bt_service_register_event_handler_callback(
1751 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1752 _bt_device_state_handle_callback_set_request();
1757 static int __bt_adapter_state_discovery_request(gboolean enable,
1758 unsigned short max_response, unsigned short duration,
1759 unsigned int mask, gboolean is_custom,
1760 bt_discovery_role_type_t role)
1762 int result = BLUETOOTH_ERROR_NONE;
1765 switch (adapter_discovery_state) {
1766 case ADAPTER_DISCOVERY_STARTED: {
1767 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1768 adapter_discovery_state);
1770 return BLUETOOTH_ERROR_IN_PROGRESS;
1772 result = adapter_stop_inquiry();
1773 if (result != OAL_STATUS_SUCCESS) {
1774 BT_ERR("Discover stop failed: %d", result);
1775 result = BLUETOOTH_ERROR_INTERNAL;
1777 BT_ERR("Stop Discovery Triggered successfully");
1778 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1779 result = BLUETOOTH_ERROR_NONE;
1784 case ADAPTER_DISCOVERY_STARTING: {
1785 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1786 adapter_discovery_state);
1788 result = enable ? BLUETOOTH_ERROR_IN_PROGRESS :
1789 BLUETOOTH_ERROR_DEVICE_BUSY;
1793 case ADAPTER_DISCOVERY_STOPPED: {
1794 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1795 adapter_discovery_state);
1797 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1799 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1802 result = adapter_start_inquiry(duration);
1804 result = adapter_start_custom_inquiry(role);
1806 if (result != OAL_STATUS_SUCCESS) {
1807 BT_ERR("Start Discovery failed: %d", result);
1808 result = BLUETOOTH_ERROR_INTERNAL;
1810 BT_ERR("Start Discovery Triggered successfully");
1811 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1812 result = BLUETOOTH_ERROR_NONE;
1817 case ADAPTER_DISCOVERY_STOPPING: {
1818 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1819 adapter_discovery_state);
1821 result = enable ? BLUETOOTH_ERROR_DEVICE_BUSY :
1822 BLUETOOTH_ERROR_NOT_IN_OPERATION;
1827 BT_ERR("Unknown state: %d", adapter_discovery_state);
1835 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1837 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1838 GVariant *param = NULL;
1839 int result = BLUETOOTH_ERROR_NONE;
1841 switch (bt_discovery_status) {
1842 case ADAPTER_DISCOVERY_STOPPED:
1844 __bt_adapter_update_discovery_status(bt_discovery_status);
1845 param = g_variant_new("(i)", result);
1846 _bt_send_event(BT_ADAPTER_EVENT,
1847 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1851 case ADAPTER_DISCOVERY_STARTED:
1853 __bt_adapter_update_discovery_status(bt_discovery_status);
1854 param = g_variant_new("(i)", result);
1855 _bt_send_event(BT_ADAPTER_EVENT,
1856 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1861 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1865 static void __bt_set_visible_mode(void)
1868 #ifdef TIZEN_FEATURE_BT_DPM
1869 int discoverable_state = DPM_BT_ERROR;
1872 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1873 BT_ERR("Fail to get the timeout value");
1875 #ifdef TIZEN_FEATURE_BT_DPM
1876 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1877 if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
1878 if (_bt_set_discoverable_mode(
1879 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1880 timeout) != BLUETOOTH_ERROR_NONE) {
1881 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1882 BT_ERR("Set vconf failed");
1885 if (_bt_set_discoverable_mode(
1886 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1887 timeout) != BLUETOOTH_ERROR_NONE) {
1888 BT_ERR("Set connectable mode failed");
1892 if (timeout == -1) {
1893 if (_bt_set_discoverable_mode(
1894 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1895 timeout) != BLUETOOTH_ERROR_NONE) {
1896 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1897 BT_ERR("Set vconf failed");
1900 if (_bt_set_discoverable_mode(
1901 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1902 timeout) != BLUETOOTH_ERROR_NONE) {
1903 BT_ERR("Set connectable mode failed");
1909 static void __bt_set_local_name(void)
1911 char *phone_name = NULL;
1914 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1919 if (strlen(phone_name) != 0) {
1920 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1923 _bt_set_local_name(phone_name);
1928 void _bt_adapter_set_status(bt_status_t status)
1930 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1931 adapter_state = status;
1934 bt_status_t _bt_adapter_get_status(void)
1936 return adapter_state;
1939 void _bt_set_disabled(int result)
1941 int power_off_status = 0;
1944 int pm_ignore_mode = 0;
1946 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1947 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1949 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1951 /* Update the vconf BT status in normal Deactivation case only */
1952 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1953 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1955 BT_DBG("Update vconf for BT normal Deactivation");
1957 if (result == BLUETOOTH_ERROR_TIMEOUT)
1958 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1959 BT_ERR("Set vconf failed");
1961 /* Update Bluetooth Status to notify other modules */
1962 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1963 BT_ERR("Set vconf failed");
1965 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1966 EVT_VAL_BT_OFF) != ES_R_OK)
1967 BT_ERR("Fail to set value");
1970 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1971 BT_ERR("Set vconf failed\n");
1973 _bt_adapter_set_status(BT_DEACTIVATED);
1974 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1976 /* Send BT Disabled event to application */
1977 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1978 g_variant_new("(i)", result));
1980 BT_INFO("Adapter disabled");
1981 BT_PERMANENT_LOG("BT Disabled");