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"
46 #ifdef TIZEN_FEATURE_BT_OBEX
47 #include "bt-service-avrcp.h"
48 #include "bt-service-opp-client.h"
49 #include "bt-service-obex-server.h"
51 #ifdef TIZEN_DPM_ENABLE
52 #include "bt-service-dpm.h"
54 #include "bt-service-hidhost.h"
55 #include "bt-service-socket.h"
56 #include "bt-service-hdp.h"
59 #include <oal-event.h>
60 #include <oal-manager.h>
61 #include <oal-adapter-mgr.h>
63 #ifdef TIZEN_FEATURE_BT_PAN_NAP
64 #include "bt-service-network.h"
66 /*This file will contain state machines related to adapter and remote device */
68 #include "bt-internal-types.h"
70 /* Global variables */
79 #ifdef TIZEN_FEATURE_BT_OBEX
82 bt_set_alarm_cb callback;
87 gboolean is_alarm_initialized;
89 } bt_service_alarm_mgr_t;
91 static bt_service_alarm_mgr_t alarm_mgr = {0, };
92 static gboolean is_recovery_mode = FALSE;
93 static gboolean is_discovering;
94 static void *adapter_agent = NULL;
95 static guint le_timer_id = 0;
96 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
97 static uint status_reg_id;
98 static void alarm_data_free(void *data);
101 static bt_adapter_timer_t visible_timer;
103 static guint timer_id = 0;
106 /* Adapter default states */
107 static bt_status_t adapter_state = BT_DEACTIVATED;
108 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
110 /* Forward declarations */
111 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
112 static void __bt_post_oal_init(void);
113 static void __bt_handle_oal_initialisation(oal_event_t event);
114 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
115 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
116 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
117 static void __bt_adapter_update_bt_enabled(void);
118 static void __bt_adapter_update_bt_disabled(void);
119 static void __bt_adapter_state_set_status(bt_status_t status);
120 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
121 static void __bt_adapter_state_change_callback(int bt_status);
122 static int __bt_adapter_state_handle_request(gboolean enable);
123 static int __bt_adapter_state_discovery_request(gboolean enable,
124 unsigned short max_response, unsigned short duration, unsigned int mask);
125 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
126 static gboolean __bt_is_service_request_present(int service_function);
128 static void __bt_set_visible_mode(void);
129 static void __bt_set_local_name(void);
131 /* Initialize BT stack (Initialize OAL layer) */
132 int _bt_stack_init(void)
136 BT_INFO("[bt-service] Start to initialize BT stack");
137 /* Adapter enable request is successful, setup event handlers */
138 _bt_service_register_event_handler_callback(
139 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
141 ret = oal_bt_init(_bt_service_oal_event_receiver);
143 if (OAL_STATUS_PENDING == ret) {
144 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
145 return BLUETOOTH_ERROR_NONE;
146 } else if (OAL_STATUS_SUCCESS != ret) {
147 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
148 return BLUETOOTH_ERROR_INTERNAL;
151 return BLUETOOTH_ERROR_NONE;
154 int _bt_enable_adapter(void)
156 return __bt_adapter_state_handle_request(TRUE);
159 int _bt_enable_core(void)
161 /* TODO_40 : 4.0 merge */
162 return BLUETOOTH_ERROR_NOT_SUPPORT;
165 int _bt_enable_adapter_le(void)
167 /* TODO_40 : 4.0 merge */
168 return BLUETOOTH_ERROR_NOT_SUPPORT;
171 int _bt_recover_adapter(void)
173 /* TODO_40 : 4.0 merge */
174 return BLUETOOTH_ERROR_NOT_SUPPORT;
177 int _bt_disable_adapter(void)
179 return __bt_adapter_state_handle_request(FALSE);
181 #ifdef TIZEN_FEATURE_BT_OBEX
182 static void alarm_data_free(void *data)
184 bt_service_alarm_t *p_data = (bt_service_alarm_t *)data;
189 int _bt_start_discovery(unsigned short max_response,
190 unsigned short duration, unsigned int cod_mask)
192 return __bt_adapter_state_discovery_request(TRUE, max_response, duration, cod_mask);
195 int _bt_cancel_discovery(void)
197 return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0);
200 gboolean _bt_is_discovering(void)
202 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
203 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
208 #ifdef TIZEN_FEATURE_BT_OBEX
209 void _bt_adapter_set_le_status(bt_le_status_t status)
211 BT_INFO("adapter_le_status changed [%d] -> [%d]", adapter_le_status, status);
212 adapter_le_status = status;
215 bt_le_status_t _bt_adapter_get_le_status(void)
217 return adapter_le_status;
221 int _bt_get_local_address(void)
227 result = adapter_get_address();
228 if (result != OAL_STATUS_SUCCESS) {
229 BT_ERR("adapter_get_address failed: %d", result);
230 result = BLUETOOTH_ERROR_INTERNAL;
232 result = BLUETOOTH_ERROR_NONE;
238 int _bt_get_local_version(void)
243 result = adapter_get_version();
244 if (result != OAL_STATUS_SUCCESS) {
245 BT_ERR("adapter_get_version failed: %d", result);
246 result = BLUETOOTH_ERROR_INTERNAL;
248 result = BLUETOOTH_ERROR_NONE;
254 int _bt_get_local_name(void)
260 result = adapter_get_name();
261 if (result != OAL_STATUS_SUCCESS) {
262 BT_ERR("adapter_get_name failed: %d", result);
263 result = BLUETOOTH_ERROR_INTERNAL;
265 result = BLUETOOTH_ERROR_NONE;
271 int _bt_set_local_name(char *local_name)
273 int result = BLUETOOTH_ERROR_NONE;
276 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
278 result = adapter_set_name(local_name);
279 if (result != OAL_STATUS_SUCCESS) {
280 BT_ERR("adapter_set_name failed: %d", result);
281 result = BLUETOOTH_ERROR_INTERNAL;
283 result = BLUETOOTH_ERROR_NONE;
289 int _bt_get_discoverable_mode(int *mode)
296 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
298 adapter_is_discoverable(&scan_mode);
299 if (TRUE == scan_mode) {
300 adapter_get_discoverable_timeout(&timeout);
302 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
304 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
306 adapter_is_connectable(&scan_mode);
307 if(scan_mode == TRUE)
308 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
311 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
312 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
313 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
320 return BLUETOOTH_ERROR_NONE;
323 int _bt_get_timeout_value(int *timeout)
328 /* Take current time */
330 time_diff = difftime(current_time, visible_timer.start_time);
332 BT_DBG("Time diff = %d\n", time_diff);
333 *timeout = visible_timer.timeout - time_diff;
335 return BLUETOOTH_ERROR_NONE;
338 static void __bt_visibility_alarm_remove()
340 if (visible_timer.event_id > 0) {
341 g_source_remove(visible_timer.event_id);
342 visible_timer.event_id = 0;
345 if (visible_timer.alarm_id > 0) {
346 alarmmgr_remove_alarm(visible_timer.alarm_id);
347 visible_timer.alarm_id = 0;
351 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
353 int result = BLUETOOTH_ERROR_NONE;
356 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
358 if (alarm_id != visible_timer.alarm_id)
361 if (visible_timer.event_id) {
362 _bt_send_event(BT_ADAPTER_EVENT,
363 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
364 g_variant_new("(in)", result, timeout));
365 g_source_remove(visible_timer.event_id);
366 visible_timer.event_id = 0;
367 visible_timer.timeout = 0;
369 if (!TIZEN_PROFILE_WEARABLE) {
370 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
371 BT_ERR("Set vconf failed\n");
374 /* Switch Off visibility in Bluez */
375 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
376 visible_timer.alarm_id = 0;
380 static gboolean __bt_timeout_handler(gpointer user_data)
382 int result = BLUETOOTH_ERROR_NONE;
386 /* Take current time */
388 time_diff = difftime(current_time, visible_timer.start_time);
390 /* Send event to application */
391 _bt_send_event(BT_ADAPTER_EVENT,
392 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
393 g_variant_new("(in)", result, time_diff));
395 if (visible_timer.timeout <= time_diff) {
396 g_source_remove(visible_timer.event_id);
397 visible_timer.event_id = 0;
398 visible_timer.timeout = 0;
400 if (!TIZEN_PROFILE_WEARABLE) {
401 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
402 BT_ERR("Set vconf failed\n");
411 static void __bt_visibility_alarm_create()
416 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
419 BT_ERR("Failed to create alarm error = %d\n", result);
421 BT_DBG("Alarm created = %d\n", alarm_id);
422 visible_timer.alarm_id = alarm_id;
426 static int __bt_set_visible_time(int timeout)
430 __bt_visibility_alarm_remove();
432 visible_timer.timeout = timeout;
434 #ifdef TIZEN_DPM_ENABLE
435 if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
437 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
438 BT_ERR("Set vconf failed");
439 #ifdef TIZEN_DPM_ENABLE
444 return BLUETOOTH_ERROR_NONE;
446 if (!visible_timer.alarm_init) {
447 /* Set Alarm timer to switch off BT */
448 result = alarmmgr_init("bt-service");
450 return BLUETOOTH_ERROR_INTERNAL;
452 visible_timer.alarm_init = TRUE;
455 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
457 return BLUETOOTH_ERROR_INTERNAL;
459 /* Take start time */
460 time(&(visible_timer.start_time));
461 visible_timer.event_id = g_timeout_add_seconds(1,
462 __bt_timeout_handler, NULL);
464 __bt_visibility_alarm_create();
466 return BLUETOOTH_ERROR_NONE;
469 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
475 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
477 #ifdef TIZEN_DPM_ENABLE
478 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
479 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
480 if (headed_plugin_info->plugin_headed_enabled)
481 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
482 return BLUETOOTH_ERROR_ACCESS_DENIED;
484 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
485 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
486 if (headed_plugin_info->plugin_headed_enabled)
487 headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
488 return BLUETOOTH_ERROR_ACCESS_DENIED;
492 switch (discoverable_mode) {
493 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
494 result = adapter_set_connectable(TRUE);
497 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
498 result = adapter_set_discoverable();
501 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
502 result = adapter_set_discoverable();
505 return BLUETOOTH_ERROR_INVALID_PARAM;
508 if (result != OAL_STATUS_SUCCESS) {
509 BT_ERR("set scan mode failed %d", result);
510 return BLUETOOTH_ERROR_INTERNAL;
513 result = adapter_set_discoverable_timeout(timeout);
514 if (result != OAL_STATUS_SUCCESS) {
515 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
516 return BLUETOOTH_ERROR_INTERNAL;
519 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
522 result = __bt_set_visible_time(timeout);
528 gboolean _bt_is_connectable(void)
535 adapter_is_connectable(&connectable);
541 BT_DBG("Connectable: [%s]", result ? "TRUE":"FALSE");
546 int _bt_set_connectable(gboolean connectable)
548 int result = BLUETOOTH_ERROR_NONE;
551 result = adapter_set_connectable(connectable);
552 if (result != OAL_STATUS_SUCCESS) {
553 BT_ERR("adapter_set_connectable failed: %d", result);
554 result = BLUETOOTH_ERROR_INTERNAL;
556 result = BLUETOOTH_ERROR_NONE;
562 int _bt_is_service_used(void)
568 result = adapter_get_service_uuids();
569 if (result != OAL_STATUS_SUCCESS) {
570 BT_ERR("adapter_get_service_uuids failed: %d", result);
571 result = BLUETOOTH_ERROR_INTERNAL;
573 result = BLUETOOTH_ERROR_NONE;
580 int _bt_adapter_get_bonded_devices(void)
582 int result = BLUETOOTH_ERROR_NONE;
585 result = adapter_get_bonded_devices();
586 if (result != OAL_STATUS_SUCCESS) {
587 BT_ERR("adapter_get_bonded_devices failed: %d", result);
588 result = BLUETOOTH_ERROR_INTERNAL;
590 result = BLUETOOTH_ERROR_NONE;
596 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
598 int result = BLUETOOTH_ERROR_NONE;
603 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
604 case OAL_EVENT_OAL_INITIALISED_FAILED:
605 __bt_handle_oal_initialisation(event_type);
607 case OAL_EVENT_ADAPTER_ENABLED:
608 __bt_adapter_state_change_callback(BT_ACTIVATED);
610 case OAL_EVENT_ADAPTER_DISABLED:
611 __bt_adapter_state_change_callback(BT_DEACTIVATED);
613 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
614 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
616 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
617 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
619 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
620 bt_address_t *bd_addr = event_data;
621 bluetooth_device_address_t local_address;
624 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
625 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
626 local_address.addr[0], local_address.addr[1], local_address.addr[2],
627 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
629 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
630 (void *) &local_address, sizeof(bluetooth_device_address_t));
633 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
634 char *name = event_data;
635 BT_DBG("Adapter Name: %s", name);
637 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
638 bluetooth_device_name_t local_name;
640 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
641 g_strlcpy(local_name.name,
642 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
643 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
644 (void *) &local_name, sizeof(bluetooth_device_name_t));
646 /* Send event to application */
647 _bt_send_event(BT_ADAPTER_EVENT,
648 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
649 g_variant_new("(is)", result, name));
653 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
654 char *ver = event_data;
655 bluetooth_version_t local_version;
657 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
658 g_strlcpy(local_version.version,
659 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
660 BT_DBG("BT Version: %s", local_version.version);
662 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
663 (void *) &local_version, sizeof(bluetooth_version_t));
666 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
668 gboolean connectable = FALSE;
670 BT_INFO("Adapter discoverable mode:"
671 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
672 _bt_send_event(BT_ADAPTER_EVENT,
673 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
674 g_variant_new("(b)", connectable));
676 _bt_send_event(BT_ADAPTER_EVENT,
677 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
678 g_variant_new("(in)", result, mode));
681 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
683 gboolean connectable = TRUE;
685 BT_INFO("Adapter discoverable mode:"
686 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
687 _bt_send_event(BT_ADAPTER_EVENT,
688 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
689 g_variant_new("(b)", connectable));
691 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
692 _bt_send_event(BT_ADAPTER_EVENT,
693 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
694 g_variant_new("(in)", result, mode));
697 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
700 BT_INFO("Adapter discoverable mode:"
701 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
703 /* Send event to application */
704 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
705 _bt_send_event(BT_ADAPTER_EVENT,
706 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
707 g_variant_new("(in)", result, mode));
711 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
712 int *timeout = event_data;
715 BT_INFO("Discoverable timeout: [%d]", *timeout);
717 /* Send event to application */
718 _bt_get_discoverable_mode(&mode);
719 _bt_send_event(BT_ADAPTER_EVENT,
720 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
721 g_variant_new("(in)", result, mode));
724 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
726 service_uuid_t *service_list;
727 event_adapter_services_t *list = event_data;
730 service_list = list->service_list;
731 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
734 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
737 bluetooth_device_address_t *addr_list;
739 event_device_list_t *bonded_device_list = event_data;
740 count = bonded_device_list->num;
742 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
743 for (i = 0; i < count; i++) {
744 memcpy(addr_list[i].addr,
745 bonded_device_list->devices[i].addr,
746 BLUETOOTH_ADDRESS_LENGTH);
749 BT_INFO("Adapter Bonded device List count: [%d]", count);
750 __bt_adapter_handle_pending_requests(BT_GET_BONDED_DEVICES,
751 (void *)addr_list, bonded_device_list->num);
755 BT_ERR("Unhandled event..");
762 int _bt_init_profiles()
766 /*TODO: Init bluetooth profiles */
767 ret = _bt_hidhost_initialize();
768 if (ret != BLUETOOTH_ERROR_NONE) {
769 BT_ERR("_bt_hidhost_initialize Failed");
772 ret = _bt_socket_init();
773 if (ret != BLUETOOTH_ERROR_NONE) {
774 BT_ERR("_bt_socket_init Failed");
777 /* Initialize A2DP Source */
778 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
779 if (ret != BLUETOOTH_ERROR_NONE) {
780 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
784 /* Initialize AVRCP Target */
785 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
786 if (ret != BLUETOOTH_ERROR_NONE) {
787 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
791 /* Initialize A2DP Sink */
792 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
793 if (ret != BLUETOOTH_ERROR_NONE) {
794 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
797 /* Initialize HFP Audio Gateway */
798 ret = _bt_audio_initialize(BT_AG_MODULE);
799 if (ret != BLUETOOTH_ERROR_NONE) {
800 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
803 /* Initialize AVRCP Controller */
804 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
805 if (ret != BLUETOOTH_ERROR_NONE) {
806 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
809 /* Registering callback for receiving audio services searched */
810 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
811 if (ret != BLUETOOTH_ERROR_NONE) {
812 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
816 ret = _bt_hdp_init();
817 if (ret != BLUETOOTH_ERROR_NONE) {
818 BT_ERR("_bt_hdp_init Failed");
823 if (ret != BLUETOOTH_ERROR_NONE) {
824 BT_ERR("_bt_le_init Failed");
828 ret = _bt_gatt_init();
829 if (ret != BLUETOOTH_ERROR_NONE) {
830 BT_ERR("_bt_gatt_init Failed");
834 return BLUETOOTH_ERROR_NONE;
837 int _bt_cleanup_profiles(void)
839 /* TODO: Cleanup bluetooth profiles */
840 _bt_hidhost_deinitialize();
843 /* TODO: Cleanup bluetooth audio profiles */
844 //_bt_audio_deinitialize(BT_A2DP_SOURCE_MODULE);
845 //_bt_audio_deinitialize(BT_AVRCP_MODULE);
846 //_bt_audio_deinitialize(BT_A2DP_SINK_MODULE);
847 //_bt_audio_deinitialize(BT_AG_MODULE);
848 //_bt_audio_deinitialize(BT_AVRCP_CTRL_MODULE);
849 //_bt_audio_deinitialize(BT_AUDIO_ALL_MODULE);
854 return BLUETOOTH_ERROR_NONE;
857 /* OAL post initialization handler */
858 static void __bt_post_oal_init(void)
861 int status = VCONFKEY_BT_STATUS_OFF;
863 BT_DBG("OAL initialized, Init profiles..");
864 ret = _bt_init_profiles();
865 if (ret != BLUETOOTH_ERROR_NONE)
866 BT_ERR("Bluetooth profile init error: %d", ret);
868 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0) {
869 BT_ERR("Fail to get the enabled value");
872 /* Update Bluetooth Status to OFF */
873 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
874 BT_ERR("Set vconf failed\n");
876 if (status & VCONFKEY_BT_STATUS_ON) {
877 ret = _bt_enable_adapter();
878 if (ret != BLUETOOTH_ERROR_NONE)
879 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
883 /* OAL initialization handler */
884 static void __bt_handle_oal_initialisation(oal_event_t event)
889 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
890 __bt_post_oal_init();
892 case OAL_EVENT_OAL_INITIALISED_FAILED:
893 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
894 g_idle_add(_bt_terminate_service, NULL);
897 BT_ERR("Unknown Event");
902 static gboolean __bt_is_service_request_present(int service_function)
905 invocation_info_t *req_info;
909 /* Get method invocation context */
910 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
912 if (req_info && req_info->service_function == service_function)
920 /* Internal functions of core adapter service */
921 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
925 invocation_info_t *req_info;
928 /* Get method invocation context */
929 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
931 if (req_info == NULL || req_info->service_function != service_function)
934 /* Create out param */
935 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
937 switch(service_function) {
938 case BT_ENABLE_ADAPTER:
939 case BT_DISABLE_ADAPTER: {
940 gboolean done = TRUE;
941 g_array_append_vals(out_param, &done, sizeof(gboolean));
944 case BT_GET_LOCAL_NAME:
945 case BT_GET_LOCAL_ADDRESS:
946 case BT_GET_LOCAL_VERSION:
947 g_array_append_vals(out_param, user_data, size);
949 case BT_IS_SERVICE_USED: {
951 gboolean used = FALSE;
953 char uuid_str[BT_UUID_STRING_SIZE];
954 char *request_uuid = req_info->user_data;
955 service_uuid_t *service_list = user_data;
957 BT_INFO("Check for service uuid: %s", request_uuid);
958 for (i = 0; i < size; i++) {
959 uuid = service_list[i].uuid;
960 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
961 BT_INFO("Adapter Service: [%s]", uuid_str);
962 if (strcasecmp(uuid_str, request_uuid) == 0) {
963 BT_INFO("UUID matched!!");
969 g_array_append_vals(out_param, &used, sizeof(gboolean));
972 case BT_GET_BONDED_DEVICES: {
973 bluetooth_device_address_t *addr_list = user_data;
974 bonded_devices_req_info_t *bonded_devices_req_info;
975 char address[BT_ADDRESS_STRING_SIZE];
977 int res = BLUETOOTH_ERROR_NONE;
980 * BT_GET_BONDED_DEVICES is already processed for this request,
981 * continue for next BT_GET_BONDED_DEVICES request if any
983 if (NULL != req_info->user_data)
986 BT_DBG("BT_GET_BONDED_DEVICES: count = [%d]", count);
987 /* No bonded devices, return method invocation */
988 if (0 == count || !addr_list)
991 /* Save address list in user data for futur reference. */
992 bonded_devices_req_info = g_malloc0(sizeof(bonded_devices_req_info));
993 if (!bonded_devices_req_info) {
994 BT_ERR("Memory allocation failed");
995 req_info->result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1000 bonded_devices_req_info->count = count;
1001 bonded_devices_req_info->addr_list = addr_list;
1002 bonded_devices_req_info->out_param = out_param;
1003 req_info->user_data = bonded_devices_req_info;
1005 while (bonded_devices_req_info->count > 0) {
1006 bonded_devices_req_info->count -= 1;
1007 res = _bt_device_get_bonded_device_info(
1008 &addr_list[bonded_devices_req_info->count]);
1009 if (BLUETOOTH_ERROR_NONE == res)
1012 _bt_convert_addr_type_to_string((char *)address,
1013 addr_list[bonded_devices_req_info->count].addr);
1014 BT_ERR("_bt_device_get_bonded_device_info Failed for [%s]", address);
1015 if (bonded_devices_req_info->count == 0) {
1016 g_free(bonded_devices_req_info->addr_list);
1017 g_free(bonded_devices_req_info);
1018 req_info->user_data = NULL;
1025 BT_ERR("Unknown service function[%d]", service_function);
1028 _bt_service_method_return(req_info->context, out_param, req_info->result);
1029 g_array_free(out_param, TRUE);
1030 /* Now free invocation info for this request*/
1031 _bt_free_info_from_invocation_list(req_info);
1035 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
1037 char *phone_name = NULL;
1043 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
1044 phone_name = vconf_keynode_get_str(node);
1046 if (phone_name && strlen(phone_name) != 0) {
1047 if (!g_utf8_validate(phone_name, -1,
1048 (const char **)&ptr))
1051 _bt_set_local_name(phone_name);
1056 /* Request return handlings */
1057 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
1059 BT_INFO("__bt_adapter_post_set_enabled>>");
1061 if (!TIZEN_PROFILE_TV) {
1062 __bt_set_visible_mode();
1064 /* add the vconf noti handler */
1065 if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1066 (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
1067 BT_ERR("DEVICE_NAME key changed notification registration failed");
1069 __bt_set_local_name();
1071 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
1072 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
1073 BT_ERR("Fail to set discoverable mode");
1076 /* Get All properties */
1077 if (OAL_STATUS_SUCCESS != adapter_get_properties())
1078 BT_ERR("adapter_get_properties failed");
1080 /* Add Adapter enabled post processing codes */
1084 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
1086 BT_INFO("_bt_adapter_post_set_disabled>>");
1088 if (!TIZEN_PROFILE_TV) {
1089 /* Add Adapter disabled post processing codes */
1090 if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1091 (vconf_callback_fn)__bt_phone_name_changed_cb) != 0)
1092 BT_ERR("vconf_ignore_key_changed failed");
1098 static void __bt_adapter_update_bt_enabled(void)
1100 int result = BLUETOOTH_ERROR_NONE;
1101 BT_INFO("_bt_adapter_update_bt_enabled>>");
1102 /* Update Bluetooth Status to notify other modules */
1103 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1104 BT_ERR("Set vconf failed\n");
1106 /* TODO:Add timer function to handle any further post processing */
1107 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
1109 /*Return BT_ADAPTER_ENABLE Method invocation context */
1110 __bt_adapter_handle_pending_requests(BT_ENABLE_ADAPTER, NULL, 0);
1111 /*Send BT Enabled event to application */
1112 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1113 g_variant_new("(i)", result));
1116 static void __bt_adapter_update_bt_disabled(void)
1118 int result = BLUETOOTH_ERROR_NONE;
1119 BT_INFO("_bt_adapter_update_bt_disabled>>");
1121 int power_off_status = 0;
1124 /* Update the vconf BT status in normal Deactivation case only */
1125 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1126 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1128 /* Update Bluetooth Status to notify other modules */
1129 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1130 BT_ERR("Set vconf failed");
1132 /* TODO:Add timer function to handle any further post processing */
1133 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1135 /* Return BT_ADAPTER_DISABLE Method invocation context */
1136 __bt_adapter_handle_pending_requests(BT_DISABLE_ADAPTER, NULL, 0);
1138 /* Send BT Disabled event to application */
1139 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1140 g_variant_new("(i)", result));
1143 static void __bt_adapter_state_set_status(bt_status_t status)
1145 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1146 adapter_state = status;
1149 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1151 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1152 adapter_discovery_state = status;
1155 static void __bt_adapter_state_change_callback(int bt_status)
1157 BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
1159 switch (bt_status) {
1160 case BT_DEACTIVATED:
1161 __bt_adapter_state_set_status(bt_status);
1163 /* Adapter is disabled, unregister event handlers */
1164 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1165 //_bt_deinit_device_event_handler();
1167 /* Add Adapter disabled post processing codes */
1168 __bt_adapter_update_bt_disabled();
1171 __bt_adapter_state_set_status(bt_status);
1172 /* Add Adapter enabled post processing codes */
1174 BT_DBG("g_source is removed");
1175 g_source_remove(timer_id);
1178 __bt_adapter_update_bt_enabled();
1181 BT_ERR("Incorrect Bluetooth adapter state changed status");
1186 static int __bt_adapter_state_handle_request(gboolean enable)
1188 int result = BLUETOOTH_ERROR_NONE;
1191 switch (adapter_state) {
1192 case BT_ACTIVATING: {
1193 BT_INFO("Adapter is currently in activating state, state [%d]",
1196 return BLUETOOTH_ERROR_IN_PROGRESS;
1198 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1199 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1200 /*TODO Stop Discovery*/
1201 if (result != OAL_STATUS_SUCCESS)
1202 BT_ERR("Discover stop failed: %d", result);
1203 __bt_adapter_update_discovery_status(FALSE);
1205 result = adapter_disable();
1206 if (result != OAL_STATUS_SUCCESS) {
1207 BT_ERR("adapter_enable failed: [%d]", result);
1208 result = BLUETOOTH_ERROR_INTERNAL;
1209 /*TODO: perform if anything more needs to be done to handle failure */
1211 /* TODO: To be handled */
1212 __bt_adapter_state_set_status(BT_DEACTIVATING);
1213 result = BLUETOOTH_ERROR_NONE;
1218 case BT_ACTIVATED: {
1219 BT_INFO("Adapter is currently in activated state, state [%d]",
1222 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1224 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1225 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1226 /*TODO Stop Discovery*/
1227 if (result != OAL_STATUS_SUCCESS)
1228 BT_ERR("Discover stop failed: %d", result);
1229 __bt_adapter_update_discovery_status(FALSE);
1231 result = adapter_disable();
1232 if (result != OAL_STATUS_SUCCESS) {
1233 BT_ERR("adapter_enable failed: [%d]", result);
1234 result = BLUETOOTH_ERROR_INTERNAL;
1235 /*TODO: perform if anything more needs to be done to handle failure */
1237 /* TODO: To be handled */
1238 __bt_adapter_state_set_status(BT_DEACTIVATING);
1239 result = BLUETOOTH_ERROR_NONE;
1244 case BT_DEACTIVATING: {
1245 BT_INFO("Adapter is currently in deactivating state, state [%d]",
1248 return BLUETOOTH_ERROR_IN_PROGRESS;
1251 result = adapter_enable();
1252 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1253 BT_ERR("adapter_enable failed: [%d]", result);
1255 result = BLUETOOTH_ERROR_INTERNAL;
1256 /*TODO: perform if anything more needs to be done to handle failure */
1258 /* TODO: To be handled */
1259 __bt_adapter_state_set_status(BT_ACTIVATING);
1260 result = BLUETOOTH_ERROR_NONE;
1265 case BT_DEACTIVATED: {
1266 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1269 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1271 result = adapter_enable();
1272 if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
1273 BT_ERR("adapter_enable failed: [%d]", result);
1275 result = BLUETOOTH_ERROR_INTERNAL;
1276 /*TODO: perform if anything more needs to be done to handle failure */
1278 /* TODO: To be handled */
1279 __bt_adapter_state_set_status(BT_ACTIVATING);
1280 result = BLUETOOTH_ERROR_NONE;
1286 BT_ERR("Unknown state: %d", adapter_state);
1290 if (enable && result == BLUETOOTH_ERROR_NONE) {
1291 /* Adapter enable request is successful, setup event handlers */
1292 _bt_service_register_event_handler_callback(
1293 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1294 _bt_device_state_handle_callback_set_request();
1299 static int __bt_adapter_state_discovery_request(gboolean enable,
1300 unsigned short max_response, unsigned short duration, unsigned int mask)
1302 int result = BLUETOOTH_ERROR_NONE;
1305 switch (adapter_discovery_state) {
1306 case ADAPTER_DISCOVERY_STARTED: {
1307 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1308 adapter_discovery_state);
1310 return BLUETOOTH_ERROR_IN_PROGRESS;
1312 result = adapter_stop_inquiry();
1313 if (result != OAL_STATUS_SUCCESS) {
1314 BT_ERR("Discover stop failed: %d", result);
1315 result = BLUETOOTH_ERROR_INTERNAL;
1317 BT_ERR("Stop Discovery Triggered successfully");
1318 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1319 result = BLUETOOTH_ERROR_NONE;
1324 case ADAPTER_DISCOVERY_STARTING: {
1325 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1326 adapter_discovery_state);
1328 return BLUETOOTH_ERROR_IN_PROGRESS;
1330 result = adapter_stop_inquiry();
1331 if (result != OAL_STATUS_SUCCESS) {
1332 BT_ERR("Discover stop failed: %d", result);
1333 result = BLUETOOTH_ERROR_INTERNAL;
1335 BT_ERR("Stop Discovery Triggered successfully");
1336 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1337 result = BLUETOOTH_ERROR_NONE;
1342 case ADAPTER_DISCOVERY_STOPPED: {
1343 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1344 adapter_discovery_state);
1346 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1348 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1349 result = adapter_start_inquiry(duration);
1350 if (result != OAL_STATUS_SUCCESS) {
1351 BT_ERR("Start Discovery failed: %d", result);
1352 result = BLUETOOTH_ERROR_INTERNAL;
1354 BT_ERR("Start Discovery Triggered successfully");
1355 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1356 result = BLUETOOTH_ERROR_NONE;
1361 case ADAPTER_DISCOVERY_STOPPING: {
1362 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1363 adapter_discovery_state);
1365 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1367 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1368 result = adapter_start_inquiry(duration);
1369 if (result != OAL_STATUS_SUCCESS) {
1370 BT_ERR("Start Discovery failed: %d", result);
1371 result = BLUETOOTH_ERROR_INTERNAL;
1373 BT_ERR("Start Discovery Triggered successfully");
1374 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1375 result = BLUETOOTH_ERROR_NONE;
1381 BT_ERR("Unknown state: %d", adapter_discovery_state);
1389 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1391 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1392 GVariant *param = NULL;
1393 int result = BLUETOOTH_ERROR_NONE;
1395 switch (bt_discovery_status) {
1396 case ADAPTER_DISCOVERY_STOPPED:
1398 __bt_adapter_update_discovery_status(bt_discovery_status);
1399 param = g_variant_new("(i)", result);
1400 _bt_send_event(BT_ADAPTER_EVENT,
1401 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1405 case ADAPTER_DISCOVERY_STARTED:
1407 __bt_adapter_update_discovery_status(bt_discovery_status);
1408 param = g_variant_new("(i)", result);
1409 _bt_send_event(BT_ADAPTER_EVENT,
1410 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1415 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1419 static void __bt_set_visible_mode(void)
1423 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1424 BT_ERR("Fail to get the timeout value");
1426 #ifdef TIZEN_DPM_ENABLE
1427 if (timeout == -1 ||
1428 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1429 if (_bt_set_discoverable_mode(
1430 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1431 timeout) != BLUETOOTH_ERROR_NONE) {
1432 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1433 BT_ERR("Set vconf failed");
1436 if (_bt_set_discoverable_mode(
1437 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1438 timeout) != BLUETOOTH_ERROR_NONE) {
1439 BT_ERR("Set connectable mode failed");
1443 if (timeout == -1) {
1444 if (_bt_set_discoverable_mode(
1445 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1446 timeout) != BLUETOOTH_ERROR_NONE) {
1447 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1448 BT_ERR("Set vconf failed");
1451 if (_bt_set_discoverable_mode(
1452 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1453 timeout) != BLUETOOTH_ERROR_NONE) {
1454 BT_ERR("Set connectable mode failed");
1460 static void __bt_set_local_name(void)
1462 char *phone_name = NULL;
1465 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1470 if (strlen(phone_name) != 0) {
1471 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1474 _bt_set_local_name(phone_name);
1479 void _bt_set_discovery_status(gboolean mode)
1481 is_discovering = mode;
1484 int _bt_check_adapter(int *status)
1486 if (!TIZEN_PROFILE_TV) {
1487 char *adapter_path = NULL;
1489 BT_CHECK_PARAMETER(status, return);
1491 *status = BT_ADAPTER_DISABLED;
1493 adapter_path = _bt_get_adapter_path();
1496 if (adapter_path != NULL)
1497 *status = BT_ADAPTER_ENABLED;
1499 g_free(adapter_path);
1500 return BLUETOOTH_ERROR_NONE;
1503 GError *error = NULL;
1506 gboolean powered = FALSE;
1508 BT_CHECK_PARAMETER(status, return);
1510 *status = BT_ADAPTER_DISABLED;
1512 proxy = _bt_get_adapter_properties_proxy();
1513 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1515 result = g_dbus_proxy_call_sync(proxy,
1517 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1519 G_DBUS_CALL_FLAGS_NONE,
1525 BT_ERR("Failed to get local address");
1526 if (error != NULL) {
1527 BT_ERR("Failed to get local address (Error: %s)", error->message);
1528 g_clear_error(&error);
1530 return BLUETOOTH_ERROR_INTERNAL;
1533 g_variant_get(result, "(v)", &temp);
1534 powered = g_variant_get_boolean(temp);
1535 BT_DBG("powered: %d", powered);
1538 *status = BT_ADAPTER_ENABLED;
1540 g_variant_unref(result);
1541 g_variant_unref(temp);
1542 return BLUETOOTH_ERROR_NONE;
1546 static int __bt_set_enabled(void)
1548 int adapter_status = BT_ADAPTER_DISABLED;
1549 int result = BLUETOOTH_ERROR_NONE;
1552 BT_DBG("g_source is removed");
1553 g_source_remove(timer_id);
1557 _bt_check_adapter(&adapter_status);
1559 if (adapter_status == BT_ADAPTER_DISABLED) {
1560 BT_ERR("Bluetoothd is not running");
1561 return BLUETOOTH_ERROR_INTERNAL;
1564 if (TIZEN_PROFILE_MOBILE || TIZEN_PROFILE_IVI) {
1565 /* BT setting UI will control Mobile's visible mode. So in the FRWK...set the visible mode as off: */
1566 if (_bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0) != BLUETOOTH_ERROR_NONE)
1567 BT_ERR("Set connectable mode failed");
1568 } else if (TIZEN_PROFILE_TV) {
1569 if (_bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0) != BLUETOOTH_ERROR_NONE)
1570 BT_ERR("Fail to set discoverable mode");
1573 /* Update Bluetooth Status to notify other modules */
1574 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
1575 BT_ERR("Set vconf failed\n");
1577 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1578 BT_ERR("Set vconf failed\n");
1580 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1581 EVT_VAL_BT_ON) != ES_R_OK)
1582 BT_ERR("Fail to set value");
1584 /* Send enabled event to API */
1585 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1586 g_variant_new("(i)", result));
1588 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1589 _bt_audio_start_auto_connect(FALSE);
1592 if (!TIZEN_PROFILE_TV)
1593 __bt_set_local_name();
1595 _bt_set_discovery_status(FALSE);
1597 return BLUETOOTH_ERROR_NONE;
1600 void _bt_set_disabled(int result)
1602 int power_off_status = 0;
1605 int pm_ignore_mode = 0;
1607 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1608 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1610 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
1612 /* Update the vconf BT status in normal Deactivation case only */
1613 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
1614 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
1616 BT_DBG("Update vconf for BT normal Deactivation");
1618 if (result == BLUETOOTH_ERROR_TIMEOUT)
1619 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
1620 BT_ERR("Set vconf failed");
1622 /* Update Bluetooth Status to notify other modules */
1623 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1624 BT_ERR("Set vconf failed");
1626 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1627 EVT_VAL_BT_OFF) != ES_R_OK)
1628 BT_ERR("Fail to set value");
1631 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
1632 BT_ERR("Set vconf failed\n");
1634 _bt_cancel_queued_transfers();
1635 // _bt_adapter_set_status(BT_DEACTIVATED);
1636 _bt_set_discovery_status(FALSE);
1638 if (!TIZEN_FEATURE_BT_USB_DONGLE &&
1639 _bt_adapter_get_le_status() != BT_LE_DEACTIVATED) {
1640 /* Send disabled event */
1641 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1642 g_variant_new("(i)", result));
1645 BT_INFO("Adapter disabled");
1649 void _bt_adapter_set_status(bt_status_t status)
1651 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_status, status);
1652 adapter_status = status;
1656 bt_status_t _bt_adapter_get_status(void)
1658 return adapter_state;
1661 void *_bt_get_adapter_agent(void)
1663 return adapter_agent;
1667 static int __bt_set_le_enabled(void)
1670 int result = BLUETOOTH_ERROR_NONE;
1673 /* Update Bluetooth Status to notify other modules */
1674 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
1675 BT_ERR("Set vconf failed\n");
1677 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1678 EVT_VAL_BT_LE_ON) != ES_R_OK)
1679 BT_ERR("Fail to set value");
1681 /* Send enabled event to API */
1683 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
1684 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
1686 status = _bt_adapter_get_status();
1687 if (status == BT_DEACTIVATED) {
1688 BT_INFO("BREDR is off, turn off PSCAN");
1689 _bt_set_connectable(FALSE);
1691 if (le_timer_id > 0) {
1692 g_source_remove(le_timer_id);
1696 /* Send enabled event to API */
1697 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_ENABLED,
1698 g_variant_new("(i)", result));
1700 if (!TIZEN_PROFILE_TV)
1701 __bt_set_local_name();
1704 return BLUETOOTH_ERROR_NONE;
1707 static void __bt_state_event_handler(const char *event_name, bundle *data, void *user_data)
1709 const char *bt_status = NULL;
1710 const char *bt_le_status = NULL;
1711 BT_DBG("bt state set event(%s) received", event_name);
1713 bt_status = bundle_get_val(data, EVT_KEY_BT_STATE);
1714 BT_DBG("bt_state: (%s)", bt_status);
1716 bt_le_status = bundle_get_val(data, EVT_KEY_BT_LE_STATE);
1717 BT_DBG("bt_state: (%s)", bt_le_status);
1721 void _bt_handle_adapter_added(void)
1725 bt_le_status_t le_status;
1728 status = _bt_adapter_get_status();
1729 le_status = _bt_adapter_get_le_status();
1730 BT_DBG("status : %d", status);
1731 BT_DBG("le_status : %d", le_status);
1733 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1734 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
1735 if (!adapter_agent) {
1736 BT_ERR("Fail to register agent");
1740 if (adapter_agent == NULL) {
1741 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
1742 if (!adapter_agent) {
1743 BT_ERR("Fail to register agent");
1749 if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
1750 BT_ERR("Fail to register media player");
1752 #ifdef TIZEN_FEATURE_BT_OBEX
1753 if (_bt_register_obex_server() != BLUETOOTH_ERROR_NONE)
1754 BT_ERR("Fail to init obex server");
1757 #ifdef TIZEN_FEATURE_BT_PAN_NAP
1758 if (_bt_network_activate() != BLUETOOTH_ERROR_NONE)
1759 BT_ERR("Fail to activate network");
1762 /* add the vconf noti handler */
1763 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1764 __bt_phone_name_changed_cb, NULL);
1766 BT_ERR("Unable to register key handler");
1768 if (le_status == BT_LE_ACTIVATING ||
1769 status == BT_ACTIVATING) {
1770 __bt_set_le_enabled();
1771 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1774 if (status == BT_ACTIVATING) {
1776 //_bt_adapter_set_status(BT_ACTIVATED);
1780 if (eventsystem_register_event(SYS_EVENT_BT_STATE, &status_reg_id,
1781 (eventsystem_handler)__bt_state_event_handler, NULL) != ES_R_OK) {
1782 BT_ERR("Fail to register system event");
1786 void _bt_handle_adapter_removed(void)
1790 //_bt_adapter_set_status(BT_DEACTIVATED);
1792 __bt_visibility_alarm_remove();
1794 if (alarm_mgr.is_alarm_initialized == TRUE) {
1796 alarm_mgr.is_alarm_initialized = FALSE;
1797 g_list_free_full(alarm_mgr.g_alarm_list, alarm_data_free);
1798 alarm_mgr.g_alarm_list = NULL;
1801 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
1802 _bt_audio_stop_auto_connect();
1805 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1806 (vconf_callback_fn)__bt_phone_name_changed_cb);
1808 ERR("vconf_ignore_key_changed failed\n");
1810 /* unregister all the services/servers/profiles registered on bluez-adapter
1811 once adapter is removed, reinitializing of the state-varaibles becomes
1813 if (_bt_unregister_obex_server() != BLUETOOTH_ERROR_NONE)
1814 BT_ERR("Fail to unregister obex server");
1816 if (_bt_unregister_media_player() != BLUETOOTH_ERROR_NONE)
1817 BT_ERR("Fail to unregister media player");
1819 /* Other unregister APIs should be placed here */
1821 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
1822 _bt_destroy_agent(adapter_agent);
1823 adapter_agent = NULL;
1825 if (is_recovery_mode == TRUE) {
1826 /* Send disabled event */
1827 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1829 /* Will recover BT by bt-core, so set the mode as activating */
1830 // _bt_adapter_set_status(BT_ACTIVATING);
1831 is_recovery_mode = FALSE;
1833 _bt_reliable_terminate_service(NULL);
1836 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1839 if (eventsystem_unregister_event(status_reg_id) != ES_R_OK)
1840 BT_ERR("Fail to unregister system event");