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_recover_adapter(void)
177 int result = BLUETOOTH_ERROR_NONE;
179 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
180 BT_ERR("Disabling in progress");
181 return BLUETOOTH_ERROR_IN_PROGRESS;
184 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
185 BT_ERR("Already Disabled");
186 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
189 _bt_adapter_set_status(BT_DEACTIVATING);
191 result = adapter_recover();
193 if (result != OAL_STATUS_SUCCESS)
194 return BLUETOOTH_ERROR_INTERNAL;
196 __bt_disconnect_all();
201 int _bt_reset_adapter(void)
204 if (OAL_STATUS_SUCCESS != adapter_reset())
205 return BLUETOOTH_ERROR_INTERNAL;
207 /* TODO_40 : 4.0 merge */
208 /* TODO Currently bt-service is not terminated in tizen next.
209 It should be handled in future patch */
210 if (_bt_adapter_get_status() == BT_DEACTIVATED)
211 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
213 return BLUETOOTH_ERROR_NONE;
216 #ifdef TIZEN_FEATURE_BT_PERMANENT_LOG
217 void _bt_start_log_dump(const char *path)
221 GError *error = NULL;
223 ret_if(path == NULL);
225 BT_INFO("Start log_dump(path = %s)", path);
227 _bt_send_dump_signal(BT_DUMP_SERVICE_START_SIGNAL);
229 proxy = __bt_get_core_proxy();
230 ret_if(proxy == NULL);
232 result = g_dbus_proxy_call_sync(proxy, "LogDump",
233 g_variant_new("(s)", path),
234 G_DBUS_CALL_FLAGS_NONE,
239 BT_ERR("Bt core call failed(Error: %s)", error->message);
240 g_clear_error(&error);
242 BT_ERR("Bt core call failed");
245 g_variant_unref(result);
248 _bt_send_dump_signal(BT_DUMP_SERVICE_FINISH_SIGNAL);
252 int _bt_check_adapter(int *status)
255 gboolean powered = FALSE;
256 BT_CHECK_PARAMETER(status, return);
258 *status = BT_ADAPTER_DISABLED;
260 if (OAL_STATUS_SUCCESS != adapter_get_powered_status(&powered))
261 return BLUETOOTH_ERROR_INTERNAL;
263 BT_INFO("Adapter is in Powered Up state");
264 *status = BT_ADAPTER_ENABLED;
266 BT_INFO("Adapter is in Powered Down state");
268 return BLUETOOTH_ERROR_NONE;
271 int _bt_disable_adapter(void)
273 bt_le_status_t le_status = BT_LE_DEACTIVATED;
275 le_status = _bt_adapter_get_le_status();
277 BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
279 if (adapter_state == BT_DEACTIVATING ||
280 adapter_state == BT_TERMINATING ||
281 le_status == BT_LE_DEACTIVATING) {
282 BT_DBG("Disabling in progress");
283 return BLUETOOTH_ERROR_IN_PROGRESS;
286 if (adapter_state == BT_DEACTIVATED) {
287 BT_DBG("Already disabled");
288 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
291 if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
292 BT_ERR("Enabling in progress");
293 return BLUETOOTH_ERROR_DEVICE_BUSY;
296 /* TODO: Need to add the function to disconnect all devices */
297 /* __bt_disconnect_all(); */
299 return __bt_adapter_state_handle_request(FALSE);
302 int _bt_start_discovery(unsigned short max_response,
303 unsigned short duration, unsigned int cod_mask)
305 return __bt_adapter_state_discovery_request(TRUE, max_response, duration,
306 cod_mask, FALSE, 0x00);
309 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
311 return __bt_adapter_state_discovery_request(TRUE, 0, 0, 0, TRUE, role);
314 int _bt_cancel_discovery(void)
316 return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0, FALSE, 0x00);
319 gboolean _bt_is_discovering(void)
321 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
322 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
328 static void __bt_set_flightmode_request(int value)
330 BT_INFO("Setting flightmode request to %d", value);
331 flightmode_request = value;
334 static int __bt_is_flightmode_request(void)
336 return flightmode_request;
339 static void __bt_service_handle_flight_mode(gboolean flight_mode, bt_status_t adapter_status)
341 int flight_mode_value = 0;
342 int ps_mode_value = 0;
344 if (flight_mode == TRUE) {
345 BT_INFO_C("### Flight mode on. Turn off BT");
347 if (adapter_status != BT_ACTIVATED) {
348 BT_INFO("No need to control bt status");
349 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
350 BT_ERR("Fail get PS mode value");
352 BT_DBG("ps_mode_value %d", ps_mode_value);
353 if (ps_mode_value > 0) {
354 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
355 BT_ERR("Set vconf failed");
360 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_ON);
362 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
363 BT_ERR("Set vconf failed");
365 if (adapter_status == BT_ACTIVATED)
366 _bt_disable_adapter();
369 BT_INFO_C("### Flight mode off. Turn on BT");
371 if (adapter_status == BT_ACTIVATED)
374 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value))
375 BT_ERR("Fail get flight mode value");
377 BT_DBG("flight_mode_value %d", flight_mode_value);
378 if (flight_mode_value == 0)
381 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
382 BT_ERR("Set vconf failed");
384 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
385 BT_ERR("Fail get PS mode value");
387 BT_DBG("ps_mode_value %d", ps_mode_value);
388 if (ps_mode_value > 0) {
389 BT_DBG("Bt should not turn on");
393 if (adapter_status == BT_DEACTIVATING) {
394 BT_INFO("BT adapter is deactivating. Turn on BT after deactivation");
395 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_OFF);
399 if (adapter_status != BT_DEACTIVATED) {
400 BT_INFO("No need to control bt status");
403 _bt_service_initialize();
405 if (adapter_status == BT_DEACTIVATED)
406 _bt_enable_adapter();
411 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
414 bt_status_t adapter_status = BT_DEACTIVATED;
415 gboolean flight_mode = FALSE;
417 adapter_status = _bt_adapter_get_status();
418 BT_INFO("Flight mode changed. Current bt status is %d", adapter_status);
420 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
421 type = vconf_keynode_get_type(node);
423 if (type == VCONF_TYPE_BOOL) {
424 flight_mode = vconf_keynode_get_bool(node);
425 __bt_service_handle_flight_mode(flight_mode, adapter_status);
427 BT_ERR("Invaild vconf key type : %d", type);
431 void _bt_service_register_vconf_handler(void)
435 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
436 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
437 BT_ERR("Unable to register key handler");
438 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
439 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
440 BT_ERR("Unable to register key handler");
443 void _bt_service_unregister_vconf_handler(void)
447 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
448 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
450 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
451 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
454 int _bt_get_local_address(void)
460 result = adapter_get_address();
461 if (result != OAL_STATUS_SUCCESS) {
462 BT_ERR("adapter_get_address failed: %d", result);
463 result = BLUETOOTH_ERROR_INTERNAL;
465 result = BLUETOOTH_ERROR_NONE;
471 int _bt_get_local_version(void)
476 result = adapter_get_version();
477 if (result != OAL_STATUS_SUCCESS) {
478 BT_ERR("adapter_get_version failed: %d", result);
479 result = BLUETOOTH_ERROR_INTERNAL;
481 result = BLUETOOTH_ERROR_NONE;
487 int _bt_get_local_name(void)
493 result = adapter_get_name();
494 if (result != OAL_STATUS_SUCCESS) {
495 BT_ERR("adapter_get_name failed: %d", result);
496 result = BLUETOOTH_ERROR_INTERNAL;
498 result = BLUETOOTH_ERROR_NONE;
504 int _bt_set_local_name(char *local_name)
506 int result = BLUETOOTH_ERROR_NONE;
509 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
511 result = adapter_set_name(local_name);
512 if (result != OAL_STATUS_SUCCESS) {
513 BT_ERR("adapter_set_name failed: %d", result);
514 result = BLUETOOTH_ERROR_INTERNAL;
516 result = BLUETOOTH_ERROR_NONE;
522 int _bt_get_discoverable_mode(int *mode)
529 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
531 adapter_is_discoverable(&scan_mode);
532 if (TRUE == scan_mode) {
533 adapter_get_discoverable_timeout(&timeout);
535 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
537 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
539 adapter_is_connectable(&scan_mode);
540 if (scan_mode == TRUE)
541 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
544 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
545 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
546 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
553 return BLUETOOTH_ERROR_NONE;
556 int _bt_get_timeout_value(int *timeout)
561 /* Take current time */
563 time_diff = difftime(current_time, visible_timer.start_time);
565 BT_DBG("Time diff = %d\n", time_diff);
566 *timeout = visible_timer.timeout - time_diff;
568 return BLUETOOTH_ERROR_NONE;
571 static void __bt_visibility_alarm_remove()
573 if (visible_timer.event_id > 0) {
574 g_source_remove(visible_timer.event_id);
575 visible_timer.event_id = 0;
578 if (visible_timer.alarm_id > 0) {
579 alarmmgr_remove_alarm(visible_timer.alarm_id);
580 visible_timer.alarm_id = 0;
584 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
586 int result = BLUETOOTH_ERROR_NONE;
589 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
591 if (alarm_id != visible_timer.alarm_id)
594 if (visible_timer.event_id) {
595 _bt_send_event(BT_ADAPTER_EVENT,
596 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
597 g_variant_new("(in)", result, timeout));
598 g_source_remove(visible_timer.event_id);
599 visible_timer.event_id = 0;
600 visible_timer.timeout = 0;
602 if (!TIZEN_PROFILE_WEARABLE) {
603 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
604 BT_ERR("Set vconf failed\n");
607 /* Switch Off visibility in Bluez */
608 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
609 visible_timer.alarm_id = 0;
613 static gboolean __bt_timeout_handler(gpointer user_data)
615 int result = BLUETOOTH_ERROR_NONE;
619 /* Take current time */
621 time_diff = difftime(current_time, visible_timer.start_time);
623 /* Send event to application */
624 _bt_send_event(BT_ADAPTER_EVENT,
625 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
626 g_variant_new("(in)", result, time_diff));
628 if (visible_timer.timeout <= time_diff) {
629 g_source_remove(visible_timer.event_id);
630 visible_timer.event_id = 0;
631 visible_timer.timeout = 0;
633 if (!TIZEN_PROFILE_WEARABLE) {
634 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
635 BT_ERR("Set vconf failed\n");
644 static void __bt_visibility_alarm_create()
649 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
652 BT_ERR("Failed to create alarm error = %d\n", result);
654 BT_DBG("Alarm created = %d\n", alarm_id);
655 visible_timer.alarm_id = alarm_id;
659 int _bt_start_visibility_timer(int timeout)
662 #ifdef TIZEN_FEATURE_BT_DPM
663 int discoverable_state = DPM_BT_ERROR;
666 __bt_visibility_alarm_remove();
668 visible_timer.timeout = timeout;
670 #ifdef TIZEN_FEATURE_BT_DPM
671 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
672 if (discoverable_state != DPM_RESTRICTED) {
674 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
675 BT_ERR("Set vconf failed");
676 #ifdef TIZEN_FEATURE_BT_DPM
681 return BLUETOOTH_ERROR_NONE;
683 if (!visible_timer.alarm_init) {
684 /* Set Alarm timer to switch off BT */
685 result = alarmmgr_init("bt-service");
687 return BLUETOOTH_ERROR_INTERNAL;
689 visible_timer.alarm_init = TRUE;
692 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
694 return BLUETOOTH_ERROR_INTERNAL;
696 if (!TIZEN_PROFILE_WEARABLE) {
697 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
698 BT_ERR("Set vconf failed");
701 /* Take start time */
702 time(&(visible_timer.start_time));
703 visible_timer.event_id = g_timeout_add_seconds(1,
704 __bt_timeout_handler, NULL);
706 visible_timer.timeout = timeout;
708 __bt_visibility_alarm_create();
710 return BLUETOOTH_ERROR_NONE;
713 int _bt_stop_visibility_timer(void)
715 __bt_visibility_alarm_remove();
717 visible_timer.timeout = 0;
719 if (!TIZEN_PROFILE_WEARABLE) {
720 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
721 BT_ERR("Set vconf failed");
724 return BLUETOOTH_ERROR_NONE;
727 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
730 int ret = BLUETOOTH_ERROR_NONE;;
731 #ifdef TIZEN_FEATURE_BT_DPM
732 int discoverable_state = DPM_BT_ERROR;
737 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
739 #ifdef TIZEN_FEATURE_BT_DPM
740 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
741 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
742 discoverable_state == DPM_RESTRICTED) {
743 if (headed_plugin_info->plugin_headed_enabled)
744 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
745 return BLUETOOTH_ERROR_ACCESS_DENIED;
747 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE &&
748 discoverable_state == DPM_RESTRICTED) {
749 if (headed_plugin_info->plugin_headed_enabled)
750 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
751 return BLUETOOTH_ERROR_ACCESS_DENIED;
755 switch (discoverable_mode) {
756 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
757 result = adapter_set_connectable(TRUE);
760 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
761 result = adapter_set_discoverable();
764 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
765 result = adapter_set_discoverable();
768 return BLUETOOTH_ERROR_INVALID_PARAM;
771 if (result != OAL_STATUS_SUCCESS) {
772 BT_ERR("set scan mode failed %d", result);
773 return BLUETOOTH_ERROR_INTERNAL;
776 result = adapter_set_discoverable_timeout(timeout);
777 if (result != OAL_STATUS_SUCCESS) {
778 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
779 return BLUETOOTH_ERROR_INTERNAL;
782 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
783 ret = _bt_stop_visibility_timer();
784 if (BLUETOOTH_ERROR_NONE != ret)
785 BT_ERR("_bt_stop_visibility_timer failed");
786 if (!TIZEN_PROFILE_WEARABLE) {
787 if (vconf_set_int(BT_FILE_VISIBLE_TIME, -1) != 0)
788 BT_ERR("Set vconf failed");
790 } else if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE) {
791 ret = _bt_start_visibility_timer(timeout);
792 if (BLUETOOTH_ERROR_NONE != ret)
793 BT_ERR("_bt_start_visibility_timer failed");
796 ret = _bt_stop_visibility_timer();
797 if (BLUETOOTH_ERROR_NONE != ret)
798 BT_ERR("_bt_stop_visibility_timer failed");
805 gboolean _bt_is_connectable(void)
812 adapter_is_connectable(&connectable);
818 BT_DBG("Connectable: [%s]", result ? "TRUE" : "FALSE");
823 int _bt_set_connectable(gboolean connectable)
825 int result = BLUETOOTH_ERROR_NONE;
828 result = adapter_set_connectable(connectable);
829 if (result != OAL_STATUS_SUCCESS) {
830 BT_ERR("adapter_set_connectable failed: %d", result);
831 result = BLUETOOTH_ERROR_INTERNAL;
833 result = BLUETOOTH_ERROR_NONE;
839 int _bt_get_connectable(void)
845 result = adapter_get_connectable();
846 if (result != OAL_STATUS_SUCCESS) {
847 BT_ERR("adapter_get_connectable failed: %d", result);
848 result = BLUETOOTH_ERROR_INTERNAL;
850 result = BLUETOOTH_ERROR_NONE;
856 int _bt_is_service_used(void)
862 result = adapter_get_service_uuids();
863 if (result != OAL_STATUS_SUCCESS) {
864 BT_ERR("adapter_get_service_uuids failed: %d", result);
865 result = BLUETOOTH_ERROR_INTERNAL;
867 result = BLUETOOTH_ERROR_NONE;
874 int _bt_adapter_get_bonded_devices(void)
876 int result = BLUETOOTH_ERROR_NONE;
879 result = adapter_get_bonded_devices();
880 if (result != OAL_STATUS_SUCCESS) {
881 BT_ERR("adapter_get_bonded_devices failed: %d", result);
882 result = BLUETOOTH_ERROR_INTERNAL;
884 result = BLUETOOTH_ERROR_NONE;
890 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
893 GDBusConnection *conn;
894 GDBusProxy *manager_proxy;
895 GVariant *result = NULL;
896 GVariant *result1 = NULL;
897 GVariantIter *iter = NULL;
898 GError *error = NULL;
899 char *object_path = NULL;
900 GVariantIter *interface_iter;
901 char *interface_str = NULL;
902 GDBusProxy *device_proxy = NULL;
903 gboolean is_connected = FALSE;
905 conn = _bt_gdbus_get_system_gconn();
906 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
908 manager_proxy = _bt_get_manager_proxy();
909 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
911 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
913 G_DBUS_CALL_FLAGS_NONE,
920 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
921 g_clear_error(&error);
924 BT_ERR("Failed to Failed to GetManagedObjects");
925 return BLUETOOTH_ERROR_INTERNAL;
928 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
929 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
931 /* Parse the signature: oa{sa{sv}}} */
932 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
933 if (object_path == NULL)
936 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
937 &interface_str, NULL)) {
938 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
939 BT_DBG("Found a device: %s", object_path);
940 g_free(interface_str);
942 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
944 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
946 if (device_proxy == NULL) {
947 BT_DBG("Device don't have this service");
951 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
952 g_variant_new("(s)", profile_uuid),
953 G_DBUS_CALL_FLAGS_NONE,
958 if (result1 == NULL) {
959 BT_ERR("Error occured in Proxy call");
961 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
965 g_object_unref(device_proxy);
968 g_variant_get(result1, "(b)", &is_connected);
970 if (is_connected == TRUE) {
971 char address[BT_ADDRESS_STRING_SIZE];
972 bluetooth_device_address_t *addr = NULL;
974 _bt_convert_device_path_to_address(object_path, address);
976 addr = g_malloc0(sizeof(bluetooth_device_address_t));
977 _bt_convert_addr_string_to_type(addr->addr, address);
979 g_array_append_vals(*addr_list, addr,
980 sizeof(bluetooth_device_address_t));
983 g_variant_unref(result1);
984 g_object_unref(device_proxy);
991 g_variant_unref(result);
992 g_variant_iter_free(iter);
995 return BLUETOOTH_ERROR_NONE;
998 static gboolean __bt_disconnect_all(void)
1001 gboolean ret = FALSE;
1002 GArray *device_list;
1005 bluetooth_device_info_t *info;
1008 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
1010 BT_DBG("The length of device list after the initialisation is [%d]", device_list->len);
1012 _bt_service_get_bonded_dev_list(&device_list);
1014 size = (device_list->len) / sizeof(bluetooth_device_info_t);
1016 for (i = 0; i < size; i++) {
1017 info = &g_array_index(device_list,
1018 bluetooth_device_info_t, i);
1019 if (info->connected != BLUETOOTH_CONNECTED_LINK_NONE) {
1020 BT_DBG("Found Connected Device");
1021 result = _bt_disconnect_device(&info->device_address);
1022 if (result != BLUETOOTH_ERROR_NONE) {
1023 BT_ERR("Cannot disconnect all the devices. bt_disconnect_device Failed");
1024 g_array_free(device_list, TRUE);
1030 g_array_free(device_list, TRUE);
1035 static void __bt_handle_pending_a2dp_init(unsigned int a2dp_role)
1039 if (!a2dp_init_pending)
1043 a2dp_init_pending = FALSE;
1045 if (a2dp_role == BT_A2DP_SINK_ROLE) {
1046 BT_INFO("Enable A2DP Sink role");
1047 /* Initialize A2DP Sink */
1048 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
1049 if (ret != BLUETOOTH_ERROR_NONE)
1050 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
1052 /* Initialize AVRCP Controller */
1053 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
1054 if (ret != BLUETOOTH_ERROR_NONE)
1055 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
1057 _bt_audio_set_current_role(BLUETOOTH_A2DP_SINK);
1061 BT_INFO("Enable A2DP Source role");
1062 /* Initialize A2DP Source */
1063 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
1064 if (ret != BLUETOOTH_ERROR_NONE)
1065 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
1067 /* Initialize AVRCP Target */
1068 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
1069 if (ret != BLUETOOTH_ERROR_NONE)
1070 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
1072 _bt_audio_set_current_role(BLUETOOTH_A2DP_SOURCE);
1076 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
1078 int result = BLUETOOTH_ERROR_NONE;
1082 switch (event_type) {
1083 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1084 case OAL_EVENT_OAL_INITIALISED_FAILED:
1085 __bt_handle_oal_initialisation(event_type);
1087 case OAL_EVENT_ADAPTER_ENABLED:
1088 __bt_adapter_state_change_callback(BT_ACTIVATED);
1090 case OAL_EVENT_ADAPTER_DISABLED:
1091 __bt_adapter_state_change_callback(BT_DEACTIVATED);
1093 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
1094 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
1096 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
1097 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
1099 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
1100 bt_address_t *bd_addr = event_data;
1101 bluetooth_device_address_t local_address;
1104 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
1105 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
1106 local_address.addr[0], local_address.addr[1], local_address.addr[2],
1107 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
1109 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
1110 (void *) &local_address, sizeof(bluetooth_device_address_t));
1113 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
1114 char *name = event_data;
1115 BT_DBG("Adapter Name: %s", name);
1117 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
1118 bluetooth_device_name_t local_name;
1120 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
1121 g_strlcpy(local_name.name,
1122 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
1123 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
1124 (void *) &local_name, sizeof(bluetooth_device_name_t));
1126 /* Send event to application */
1127 _bt_send_event(BT_ADAPTER_EVENT,
1128 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
1129 g_variant_new("(is)", result, name));
1133 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
1134 char *ver = event_data;
1135 bluetooth_version_t local_version;
1137 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
1138 g_strlcpy(local_version.version,
1139 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
1140 BT_DBG("BT Version: %s", local_version.version);
1142 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
1143 (void *) &local_version, sizeof(bluetooth_version_t));
1146 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
1148 gboolean connectable = FALSE;
1150 BT_INFO("Adapter discoverable mode:"
1151 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
1153 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1154 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1155 (void *) &connectable, sizeof(gboolean));
1158 _bt_send_event(BT_ADAPTER_EVENT,
1159 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1160 g_variant_new("(b)", connectable));
1162 _bt_send_event(BT_ADAPTER_EVENT,
1163 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1164 g_variant_new("(in)", result, mode));
1168 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
1170 gboolean connectable = TRUE;
1172 BT_INFO("Adapter discoverable mode:"
1173 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
1175 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1176 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1177 (void *) &connectable, sizeof(gboolean));
1181 _bt_send_event(BT_ADAPTER_EVENT,
1182 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
1183 g_variant_new("(b)", connectable));
1185 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1186 _bt_send_event(BT_ADAPTER_EVENT,
1187 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1188 g_variant_new("(in)", result, mode));
1192 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
1194 gboolean connectable = TRUE;
1196 BT_INFO("Adapter discoverable mode:"
1197 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
1199 if (__bt_is_service_request_present(BT_GET_CONNECTABLE)) {
1200 __bt_adapter_handle_pending_requests(BT_GET_CONNECTABLE,
1201 (void *) &connectable, sizeof(gboolean));
1204 /* Send event to application */
1205 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1206 _bt_send_event(BT_ADAPTER_EVENT,
1207 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1208 g_variant_new("(in)", result, mode));
1212 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
1213 int *timeout = event_data;
1216 BT_INFO("Discoverable timeout: [%d]", *timeout);
1218 /* Send event to application */
1219 _bt_get_discoverable_mode(&mode);
1220 _bt_send_event(BT_ADAPTER_EVENT,
1221 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
1222 g_variant_new("(in)", result, mode));
1225 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
1227 service_uuid_t *service_list;
1228 event_adapter_services_t *list = event_data;
1231 service_list = list->service_list;
1232 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
1234 char uuids[512] = {0,};
1235 char standard_prefix[2] = {0,};
1237 for (int i = 0; i < count; i++) {
1239 g_snprintf(uuids + (index++), 2, ",");
1240 if (memcmp(service_list[i].uuid, standard_prefix, 2) == 0) {
1241 g_snprintf(uuids + index, 5, "%.2x%.2x",
1242 service_list[i].uuid[2], service_list[i].uuid[3]);
1245 g_snprintf(uuids + index, 9, "%.2x%.2x..%.2x,",
1246 service_list[i].uuid[0], service_list[i].uuid[1], service_list[i].uuid[15]);
1250 BT_PERMANENT_LOG("UUIDs %s", uuids);
1253 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
1256 bluetooth_device_address_t *addr_list;
1258 event_device_list_t *bonded_device_list = event_data;
1259 count = bonded_device_list->num;
1261 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
1262 for (i = 0; i < count; i++) {
1263 memcpy(addr_list[i].addr,
1264 bonded_device_list->devices[i].addr,
1265 BLUETOOTH_ADDRESS_LENGTH);
1268 BT_INFO("Adapter Bonded device List count: [%d]", count);
1269 _bt_device_handle_paired_address_list(addr_list, bonded_device_list->num);
1273 case OAL_EVENT_ADAPTER_PROPERTY_A2DP_ROLE: {
1274 unsigned int *a2dp_role = event_data;
1276 BT_INFO("a2dp role: [%u]", *a2dp_role);
1278 __bt_handle_pending_a2dp_init(*a2dp_role);
1282 case OAL_EVENT_CONTROLLER_ERROR_RECEIVED: {
1283 uint8_t *error_code = event_data;
1285 BT_INFO("OAL_EVENT_CONTROLLER_ERROR_RECEIVED: [%d]", *error_code);
1286 #ifdef TIZEN_FEATURE_BT_PERMANENT_LOG
1287 _bt_start_log_dump("syslog");
1289 _bt_recover_adapter();
1293 BT_ERR("Unhandled event..");
1300 int _bt_init_profiles()
1305 /*TODO: Init bluetooth profiles */
1306 ret = _bt_hidhost_initialize();
1307 if (ret != BLUETOOTH_ERROR_NONE)
1308 BT_ERR("_bt_hidhost_initialize Failed");
1310 ret = _bt_hiddevice_initialize();
1311 if (ret != BLUETOOTH_ERROR_NONE)
1312 BT_ERR("_bt_hiddevice_initialize Failed");
1314 ret = _bt_socket_init();
1315 if (ret != BLUETOOTH_ERROR_NONE)
1316 BT_ERR("_bt_socket_init Failed");
1318 /* Initialize HFP Audio Gateway */
1319 ret = _bt_audio_initialize(BT_AG_MODULE);
1320 if (ret != BLUETOOTH_ERROR_NONE)
1321 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
1323 /* Registering callback for receiving audio services searched */
1324 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
1325 if (ret != BLUETOOTH_ERROR_NONE)
1326 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
1328 ret = _bt_hdp_init();
1329 if (ret != BLUETOOTH_ERROR_NONE)
1330 BT_ERR("_bt_hdp_init Failed");
1332 ret = _bt_gatt_init();
1333 if (ret != BLUETOOTH_ERROR_NONE)
1334 BT_ERR("_bt_gatt_init Failed");
1336 /* Initialize HF Client */
1337 ret = _bt_audio_initialize(BT_HFP_MODULE);
1338 if (ret != BLUETOOTH_ERROR_NONE)
1339 BT_ERR("_bt_audio_initialize(BT_HFP_MODULE) Failed");
1341 return BLUETOOTH_ERROR_NONE;
1344 int _bt_cleanup_profiles(void)
1346 /* TODO: Cleanup bluetooth profiles */
1347 _bt_hidhost_deinitialize();
1348 _bt_socket_deinit();
1350 /* TODO: Cleanup bluetooth audio profiles */
1351 //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
1352 //_bt_audio_deinitialize(BT_AVRCP_MODULE);
1353 //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
1354 //_bt_audio_deinitialize(BT_AG_MODULE);
1355 //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
1356 //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
1358 a2dp_init_pending = TRUE;
1363 return BLUETOOTH_ERROR_NONE;
1366 /* OAL post initialization handler */
1367 static void __bt_post_oal_init(void)
1370 int status = VCONFKEY_BT_STATUS_OFF;
1372 BT_DBG("OAL initialized");
1373 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0)
1374 BT_ERR("Fail to get the enabled value");
1377 /* Update Bluetooth Status to OFF */
1378 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1379 BT_ERR("Set vconf failed\n");
1382 if (status & VCONFKEY_BT_STATUS_ON) {
1383 ret = _bt_enable_adapter();
1384 if (ret != BLUETOOTH_ERROR_NONE)
1385 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
1389 /* OAL initialization handler */
1390 static void __bt_handle_oal_initialisation(oal_event_t event)
1395 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
1396 __bt_post_oal_init();
1398 case OAL_EVENT_OAL_INITIALISED_FAILED:
1399 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
1400 g_idle_add(_bt_terminate_service, NULL);
1403 BT_ERR("Unknown Event");
1408 static gboolean __bt_is_service_request_present(int service_function)
1411 invocation_info_t *req_info;
1415 /* Get method invocation context */
1416 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1418 if (req_info && req_info->service_function == service_function)
1426 /* Internal functions of core adapter service */
1427 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
1431 invocation_info_t *req_info;
1434 /* Get method invocation context */
1435 for (l = _bt_get_invocation_list(); l != NULL; ) {
1437 l = g_slist_next(l);
1438 if (req_info == NULL || req_info->service_function != service_function)
1441 /* Create out param */
1442 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1444 switch (service_function) {
1445 case BT_ENABLE_ADAPTER:
1446 case BT_DISABLE_ADAPTER: {
1447 gboolean done = TRUE;
1448 g_array_append_vals(out_param, &done, sizeof(gboolean));
1451 case BT_GET_CONNECTABLE:
1452 case BT_GET_LOCAL_NAME:
1453 case BT_GET_LOCAL_ADDRESS:
1454 case BT_GET_LOCAL_VERSION:
1455 g_array_append_vals(out_param, user_data, size);
1457 case BT_IS_SERVICE_USED: {
1459 gboolean used = FALSE;
1460 unsigned char *uuid;
1461 char uuid_str[BT_UUID_STRING_SIZE];
1462 char *request_uuid = req_info->user_data;
1463 service_uuid_t *service_list = user_data;
1465 BT_INFO("Check for service uuid: %s", request_uuid);
1466 for (i = 0; i < size; i++) {
1467 uuid = service_list[i].uuid;
1468 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
1469 BT_INFO("Adapter Service: [%s]", uuid_str);
1470 if (strcasecmp(uuid_str, request_uuid) == 0) {
1471 BT_INFO("UUID matched!!");
1477 g_array_append_vals(out_param, &used, sizeof(gboolean));
1481 BT_ERR("Unknown service function[%d]", service_function);
1484 _bt_service_method_return(req_info->context, out_param, req_info->result);
1485 g_array_free(out_param, TRUE);
1486 /* Now free invocation info for this request*/
1487 _bt_free_info_from_invocation_list(req_info);
1491 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1493 char *phone_name = NULL;
1499 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1500 phone_name = vconf_keynode_get_str(node);
1502 if (phone_name && strlen(phone_name) != 0) {
1503 if (!g_utf8_validate(phone_name, -1,
1504 (const char **)&ptr))
1507 _bt_set_local_name(phone_name);
1512 /* Request return handlings */
1513 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1515 int result = BLUETOOTH_ERROR_NONE;
1517 BT_INFO("__bt_adapter_post_set_enabled>>");
1519 if (TIZEN_PROFILE_TV || !headed_plugin_info->plugin_headed_enabled) {
1520 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1521 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1522 BT_ERR("Fail to set discoverable mode");
1524 __bt_set_visible_mode();
1527 /* add the vconf noti handler */
1528 if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1529 (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1530 BT_ERR("DEVICE_NAME key changed notification registration failed");
1532 __bt_set_local_name();
1534 /* Get All properties */
1535 if (OAL_STATUS_SUCCESS != adapter_get_properties())
1536 BT_ERR("adapter_get_properties failed");
1538 __bt_adapter_state_set_status(BT_ACTIVATED);
1540 /*Send BT Enabled event to application */
1541 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1542 g_variant_new("(i)", result));
1544 BT_PERMANENT_LOG("BT Enabled");
1549 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1551 BT_INFO("_bt_adapter_post_set_disabled>>");
1553 if (!TIZEN_PROFILE_TV) {
1554 /* Add Adapter disabled post processing codes */
1555 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1556 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1557 BT_ERR("vconf_ignore_key_changed failed");
1560 /* bt-service should be terminated when BT is off */
1561 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1562 /* TODO: Implement to check if it is the recovery mode or not */
1563 if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_ON) {
1564 /* Keep bt-service to handle Flight mode OFF */
1565 BT_DBG("Deactived due to flight mode. Keep bt-service");
1566 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1567 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_NONE);
1568 } else if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_OFF) {
1569 BT_DBG("Durning deactivation, receive Flight mode Off. Enable BT adapter");
1570 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1571 _bt_service_initialize();
1572 _bt_enable_adapter();
1573 __bt_set_flightmode_request(BT_ADAPTER_FLIGHT_MODE_NONE);
1575 _bt_reliable_terminate_service(NULL);
1577 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1583 static void __bt_adapter_update_bt_enabled(void)
1585 BT_INFO("_bt_adapter_update_bt_enabled >> Init profiles...");
1586 if (BLUETOOTH_ERROR_NONE != _bt_init_profiles())
1587 BT_ERR("Bluetooth profile init failed");
1589 _bt_device_handle_adapter_state(TRUE);
1591 /* Update Bluetooth Status to notify other modules */
1592 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1593 BT_ERR("Set vconf failed\n");
1595 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1596 BT_ERR("Set vconf failed\n");
1598 /* TODO:Add timer function to handle any further post processing */
1599 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1602 static void __bt_adapter_update_bt_disabled(void)
1604 int power_off_status = 0;
1607 BT_INFO("_bt_adapter_update_bt_disabled >> Cleanup profiles...");
1608 _bt_cleanup_profiles();
1610 _bt_device_handle_adapter_state(FALSE);
1612 #ifdef TIZEN_FEATURE_BT_AVC_TARGET
1613 /* Send the information to Absolute Volume Controller */
1614 _bt_audio_handle_adapter_disabled();
1617 /* Update the vconf BT status in normal Deactivation case only */
1618 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1619 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1621 /* Update Bluetooth Status to notify other modules */
1622 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1623 BT_ERR("Set vconf failed");
1625 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1626 BT_ERR("Set vconf failed\n");
1628 /* TODO:Add timer function to handle any further post processing */
1629 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1631 /* Pending BT Disabled event before bt-service's termination */
1634 static void __bt_adapter_state_set_status(bt_status_t status)
1636 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1637 adapter_state = status;
1640 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1642 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1643 adapter_discovery_state = status;
1646 static void __bt_adapter_state_change_callback(int bt_status)
1648 BT_INFO("BT adapter status changed [%d]", bt_status);
1650 switch (bt_status) {
1651 case BT_DEACTIVATED:
1652 __bt_adapter_state_set_status(BT_TERMINATING);
1654 /* Adapter is disabled, unregister event handlers */
1655 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1656 //_bt_deinit_device_event_handler();
1658 /* Add Adapter disabled post processing codes */
1659 __bt_adapter_update_bt_disabled();
1662 /* Add Adapter enabled post processing codes */
1664 BT_DBG("g_source is removed");
1665 g_source_remove(timer_id);
1669 /*If adapter gets activated when flight mode is ON*/
1670 if (__bt_is_flightmode_request() == BT_ADAPTER_FLIGHT_MODE_ON) {
1671 BT_DBG("Receive Flight mode On. Disable BT adapter");
1672 _bt_disable_adapter();
1676 __bt_adapter_update_bt_enabled();
1679 BT_ERR("Incorrect Bluetooth adapter state changed status");
1684 int _bt_set_adapter_request_state(int enable)
1686 return oal_set_adapter_request_state(enable);
1689 int _bt_set_le_request_state(int enable)
1691 return oal_set_le_request_state(enable);
1694 static int __bt_adapter_state_handle_request(gboolean enable)
1696 int result = BLUETOOTH_ERROR_NONE;
1698 switch (adapter_state) {
1700 case BT_DEACTIVATING:
1701 BT_INFO("Should not be callded : state [%d]", adapter_state);
1702 return BLUETOOTH_ERROR_INTERNAL;
1704 case BT_ACTIVATED: {
1705 BT_INFO("Adapter is currently in activated state, state [%d]",
1708 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1710 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1711 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1712 /*TODO Stop Discovery*/
1713 __bt_adapter_update_discovery_status(FALSE);
1715 result = adapter_disable();
1716 if (result != OAL_STATUS_SUCCESS) {
1717 BT_ERR("adapter_enable failed: [%d]", result);
1718 result = BLUETOOTH_ERROR_INTERNAL;
1719 /*TODO: perform if anything more needs to be done to handle failure */
1721 /* TODO: To be handled */
1722 __bt_adapter_state_set_status(BT_DEACTIVATING);
1723 result = BLUETOOTH_ERROR_NONE;
1728 case BT_DEACTIVATED: {
1729 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1732 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1734 result = adapter_enable();
1735 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1736 BT_ERR("adapter_enable failed: [%d]", result);
1738 result = BLUETOOTH_ERROR_INTERNAL;
1739 /*TODO: perform if anything more needs to be done to handle failure */
1741 /* TODO: To be handled */
1742 __bt_adapter_state_set_status(BT_ACTIVATING);
1743 result = BLUETOOTH_ERROR_NONE;
1749 BT_ERR("Unknown state: %d", adapter_state);
1753 if (enable && result == BLUETOOTH_ERROR_NONE) {
1754 /* Adapter enable request is successful, setup event handlers */
1755 _bt_service_register_event_handler_callback(
1756 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1757 _bt_device_state_handle_callback_set_request();
1762 static int __bt_adapter_state_discovery_request(gboolean enable,
1763 unsigned short max_response, unsigned short duration,
1764 unsigned int mask, gboolean is_custom,
1765 bt_discovery_role_type_t role)
1767 int result = BLUETOOTH_ERROR_NONE;
1770 switch (adapter_discovery_state) {
1771 case ADAPTER_DISCOVERY_STARTED: {
1772 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1773 adapter_discovery_state);
1775 return BLUETOOTH_ERROR_IN_PROGRESS;
1777 result = adapter_stop_inquiry();
1778 if (result != OAL_STATUS_SUCCESS) {
1779 BT_ERR("Discover stop failed: %d", result);
1780 result = BLUETOOTH_ERROR_INTERNAL;
1782 BT_ERR("Stop Discovery Triggered successfully");
1783 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1784 result = BLUETOOTH_ERROR_NONE;
1789 case ADAPTER_DISCOVERY_STARTING: {
1790 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1791 adapter_discovery_state);
1793 result = enable ? BLUETOOTH_ERROR_IN_PROGRESS :
1794 BLUETOOTH_ERROR_DEVICE_BUSY;
1798 case ADAPTER_DISCOVERY_STOPPED: {
1799 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1800 adapter_discovery_state);
1802 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1804 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1807 result = adapter_start_inquiry(duration);
1809 result = adapter_start_custom_inquiry(role);
1811 if (result != OAL_STATUS_SUCCESS) {
1812 BT_ERR("Start Discovery failed: %d", result);
1813 result = BLUETOOTH_ERROR_INTERNAL;
1815 BT_ERR("Start Discovery Triggered successfully");
1816 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1817 result = BLUETOOTH_ERROR_NONE;
1822 case ADAPTER_DISCOVERY_STOPPING: {
1823 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1824 adapter_discovery_state);
1826 result = enable ? BLUETOOTH_ERROR_DEVICE_BUSY :
1827 BLUETOOTH_ERROR_NOT_IN_OPERATION;
1832 BT_ERR("Unknown state: %d", adapter_discovery_state);
1840 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1842 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1843 GVariant *param = NULL;
1844 int result = BLUETOOTH_ERROR_NONE;
1846 switch (bt_discovery_status) {
1847 case ADAPTER_DISCOVERY_STOPPED:
1849 __bt_adapter_update_discovery_status(bt_discovery_status);
1850 param = g_variant_new("(i)", result);
1851 _bt_send_event(BT_ADAPTER_EVENT,
1852 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1856 case ADAPTER_DISCOVERY_STARTED:
1858 __bt_adapter_update_discovery_status(bt_discovery_status);
1859 param = g_variant_new("(i)", result);
1860 _bt_send_event(BT_ADAPTER_EVENT,
1861 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1866 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1870 static void __bt_set_visible_mode(void)
1873 #ifdef TIZEN_FEATURE_BT_DPM
1874 int discoverable_state = DPM_BT_ERROR;
1877 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1878 BT_ERR("Fail to get the timeout value");
1880 #ifdef TIZEN_FEATURE_BT_DPM
1881 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1882 if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
1883 if (_bt_set_discoverable_mode(
1884 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1885 timeout) != BLUETOOTH_ERROR_NONE) {
1886 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1887 BT_ERR("Set vconf failed");
1891 if (timeout == -1) {
1892 if (_bt_set_discoverable_mode(
1893 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1894 timeout) != BLUETOOTH_ERROR_NONE) {
1895 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1896 BT_ERR("Set vconf failed");
1902 static void __bt_set_local_name(void)
1904 char *phone_name = NULL;
1907 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1912 if (strlen(phone_name) != 0) {
1913 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1916 _bt_set_local_name(phone_name);
1921 void _bt_adapter_set_status(bt_status_t status)
1923 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1924 adapter_state = status;
1927 bt_status_t _bt_adapter_get_status(void)
1929 return adapter_state;
1932 void _bt_set_disabled(int result)
1934 int power_off_status = 0;
1937 int pm_ignore_mode = 0;
1939 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1940 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1942 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1944 /* Update the vconf BT status in normal Deactivation case only */
1945 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1946 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1948 BT_DBG("Update vconf for BT normal Deactivation");
1950 if (result == BLUETOOTH_ERROR_TIMEOUT)
1951 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1952 BT_ERR("Set vconf failed");
1954 /* Update Bluetooth Status to notify other modules */
1955 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1956 BT_ERR("Set vconf failed");
1958 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1959 EVT_VAL_BT_OFF) != ES_R_OK)
1960 BT_ERR("Fail to set value");
1963 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1964 BT_ERR("Set vconf failed\n");
1966 _bt_adapter_set_status(BT_DEACTIVATED);
1967 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
1969 /* Send BT Disabled event to application */
1970 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1971 g_variant_new("(i)", result));
1973 BT_INFO("Adapter disabled");
1974 BT_PERMANENT_LOG("BT Disabled");