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>
27 #include <syspopup_caller.h>
29 #include <eventsystem.h>
30 #include <bundle_internal.h>
34 /*bt-service headers */
35 #include "bt-internal-types.h"
36 #include "bt-service-common.h"
37 #include "bt-service-util.h"
38 #include "bt-service-main.h"
39 #include "bt-service-core-adapter.h"
40 #include "bt-service-core-device.h"
41 #include "bt-service-event-receiver.h"
42 #include "bt-request-handler.h"
43 #include "bt-service-event.h"
44 #include "bt-service-audio-common.h"
45 #include "bt-service-core-adapter-le.h"
46 #include "bt-service-gatt.h"
48 #ifdef TIZEN_DPM_ENABLE
49 #include "bt-service-dpm.h"
51 #include "bt-service-hidhost.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 /*This file will contain state machines related to adapter and remote device */
62 /* Global variables */
71 static bt_adapter_timer_t visible_timer;
73 static guint timer_id = 0;
75 /* Adapter default states */
76 static bt_status_t adapter_state = BT_DEACTIVATED;
77 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
79 /* Forward declarations */
80 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
81 static void __bt_post_oal_init(void);
82 static void __bt_handle_oal_initialisation(oal_event_t event);
83 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
84 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
85 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
86 static void __bt_adapter_update_bt_enabled(void);
87 static void __bt_adapter_update_bt_disabled(void);
88 static void __bt_adapter_state_set_status(bt_status_t status);
89 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
90 static void __bt_adapter_state_change_callback(int bt_status);
91 static int __bt_adapter_state_handle_request(gboolean enable);
92 static int __bt_adapter_state_discovery_request(gboolean enable,
93 unsigned short max_response, unsigned short duration, unsigned int mask);
94 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
95 static gboolean __bt_is_service_request_present(int service_function);
97 static void __bt_set_visible_mode(void);
99 static void __bt_set_local_name(void);
101 /* Initialize BT stack (Initialize OAL layer) */
102 int _bt_stack_init(void)
106 BT_INFO("[bt-service] Start to initialize BT stack");
107 /* Adapter enable request is successful, setup event handlers */
108 _bt_service_register_event_handler_callback(
109 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
111 ret = oal_bt_init(_bt_service_oal_event_receiver);
113 if (OAL_STATUS_PENDING == ret) {
114 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
115 return BLUETOOTH_ERROR_NONE;
116 } else if (OAL_STATUS_SUCCESS != ret) {
117 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
118 return BLUETOOTH_ERROR_INTERNAL;
121 return BLUETOOTH_ERROR_NONE;
124 int _bt_enable_adapter(void)
126 return __bt_adapter_state_handle_request(TRUE);
129 int _bt_disable_adapter(void)
131 return __bt_adapter_state_handle_request(FALSE);
135 int _bt_start_discovery(unsigned short max_response,
136 unsigned short duration, unsigned int cod_mask)
138 return __bt_adapter_state_discovery_request(TRUE, max_response, duration, cod_mask);
141 int _bt_cancel_discovery(void)
143 return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0);
146 gboolean _bt_is_discovering(void)
148 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
149 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
155 int _bt_get_local_address(void)
161 result = adapter_get_address();
162 if (result != OAL_STATUS_SUCCESS) {
163 BT_ERR("adapter_get_address failed: %d", result);
164 result = BLUETOOTH_ERROR_INTERNAL;
166 result = BLUETOOTH_ERROR_NONE;
172 int _bt_get_local_version(void)
177 result = adapter_get_version();
178 if (result != OAL_STATUS_SUCCESS) {
179 BT_ERR("adapter_get_version failed: %d", result);
180 result = BLUETOOTH_ERROR_INTERNAL;
182 result = BLUETOOTH_ERROR_NONE;
188 int _bt_get_local_name(void)
194 result = adapter_get_name();
195 if (result != OAL_STATUS_SUCCESS) {
196 BT_ERR("adapter_get_name failed: %d", result);
197 result = BLUETOOTH_ERROR_INTERNAL;
199 result = BLUETOOTH_ERROR_NONE;
205 int _bt_set_local_name(char *local_name)
207 int result = BLUETOOTH_ERROR_NONE;
210 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
212 result = adapter_set_name(local_name);
213 if (result != OAL_STATUS_SUCCESS) {
214 BT_ERR("adapter_set_name failed: %d", result);
215 result = BLUETOOTH_ERROR_INTERNAL;
217 result = BLUETOOTH_ERROR_NONE;
223 int _bt_get_discoverable_mode(int *mode)
230 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
232 adapter_is_discoverable(&scan_mode);
233 if (TRUE == scan_mode) {
234 adapter_get_discoverable_timeout(&timeout);
236 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
238 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
240 adapter_is_connectable(&scan_mode);
241 if(scan_mode == TRUE)
242 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
245 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
246 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
247 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
254 return BLUETOOTH_ERROR_NONE;
257 int _bt_get_timeout_value(int *timeout)
262 /* Take current time */
264 time_diff = difftime(current_time, visible_timer.start_time);
266 BT_DBG("Time diff = %d\n", time_diff);
267 *timeout = visible_timer.timeout - time_diff;
269 return BLUETOOTH_ERROR_NONE;
272 static void __bt_visibility_alarm_remove()
274 if (visible_timer.event_id > 0) {
275 g_source_remove(visible_timer.event_id);
276 visible_timer.event_id = 0;
279 if (visible_timer.alarm_id > 0) {
280 alarmmgr_remove_alarm(visible_timer.alarm_id);
281 visible_timer.alarm_id = 0;
285 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
287 int result = BLUETOOTH_ERROR_NONE;
290 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
292 if (alarm_id != visible_timer.alarm_id)
295 if (visible_timer.event_id) {
296 _bt_send_event(BT_ADAPTER_EVENT,
297 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
298 g_variant_new("(in)", result, timeout));
299 g_source_remove(visible_timer.event_id);
300 visible_timer.event_id = 0;
301 visible_timer.timeout = 0;
303 #ifndef TIZEN_PROFILE_WEARABLE
304 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
305 BT_ERR("Set vconf failed\n");
308 /* Switch Off visibility in Bluez */
309 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
310 visible_timer.alarm_id = 0;
314 static gboolean __bt_timeout_handler(gpointer user_data)
316 int result = BLUETOOTH_ERROR_NONE;
320 /* Take current time */
322 time_diff = difftime(current_time, visible_timer.start_time);
324 /* Send event to application */
325 _bt_send_event(BT_ADAPTER_EVENT,
326 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
327 g_variant_new("(in)", result, time_diff));
329 if (visible_timer.timeout <= time_diff) {
330 g_source_remove(visible_timer.event_id);
331 visible_timer.event_id = 0;
332 visible_timer.timeout = 0;
334 #ifndef TIZEN_PROFILE_WEARABLE
335 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
336 BT_ERR("Set vconf failed\n");
344 static void __bt_visibility_alarm_create()
349 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
352 BT_ERR("Failed to create alarm error = %d\n", result);
354 BT_DBG("Alarm created = %d\n", alarm_id);
355 visible_timer.alarm_id = alarm_id;
359 static int __bt_set_visible_time(int timeout)
363 __bt_visibility_alarm_remove();
365 visible_timer.timeout = timeout;
367 #ifndef TIZEN_PROFILE_WEARABLE
368 #ifdef TIZEN_DPM_ENABLE
369 if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
371 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
372 BT_ERR("Set vconf failed");
373 #ifdef TIZEN_DPM_ENABLE
379 return BLUETOOTH_ERROR_NONE;
381 if (!visible_timer.alarm_init) {
382 /* Set Alarm timer to switch off BT */
383 result = alarmmgr_init("bt-service");
385 return BLUETOOTH_ERROR_INTERNAL;
387 visible_timer.alarm_init = TRUE;
390 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
392 return BLUETOOTH_ERROR_INTERNAL;
394 /* Take start time */
395 time(&(visible_timer.start_time));
396 visible_timer.event_id = g_timeout_add_seconds(1,
397 __bt_timeout_handler, NULL);
399 __bt_visibility_alarm_create();
401 return BLUETOOTH_ERROR_NONE;
404 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
410 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
412 #ifdef TIZEN_DPM_ENABLE
413 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
414 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
415 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
416 return BLUETOOTH_ERROR_ACCESS_DENIED;
418 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
419 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
420 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
421 return BLUETOOTH_ERROR_ACCESS_DENIED;
425 switch (discoverable_mode) {
426 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
427 result = adapter_set_connectable(TRUE);
430 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
431 result = adapter_set_discoverable();
434 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
435 result = adapter_set_discoverable();
438 return BLUETOOTH_ERROR_INVALID_PARAM;
441 if (result != OAL_STATUS_SUCCESS) {
442 BT_ERR("set scan mode failed %d", result);
443 return BLUETOOTH_ERROR_INTERNAL;
446 result = adapter_set_discoverable_timeout(timeout);
447 if (result != OAL_STATUS_SUCCESS) {
448 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
449 return BLUETOOTH_ERROR_INTERNAL;
452 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
455 result = __bt_set_visible_time(timeout);
461 gboolean _bt_is_connectable(void)
468 adapter_is_connectable(&connectable);
474 BT_DBG("Connectable: [%s]", result ? "TRUE":"FALSE");
479 int _bt_set_connectable(gboolean connectable)
481 int result = BLUETOOTH_ERROR_NONE;
484 result = adapter_set_connectable(connectable);
485 if (result != OAL_STATUS_SUCCESS) {
486 BT_ERR("adapter_set_connectable failed: %d", result);
487 result = BLUETOOTH_ERROR_INTERNAL;
489 result = BLUETOOTH_ERROR_NONE;
495 int _bt_is_service_used(void)
501 result = adapter_get_service_uuids();
502 if (result != OAL_STATUS_SUCCESS) {
503 BT_ERR("adapter_get_service_uuids failed: %d", result);
504 result = BLUETOOTH_ERROR_INTERNAL;
506 result = BLUETOOTH_ERROR_NONE;
513 int _bt_adapter_get_bonded_devices(void)
515 int result = BLUETOOTH_ERROR_NONE;
518 result = adapter_get_bonded_devices();
519 if (result != OAL_STATUS_SUCCESS) {
520 BT_ERR("adapter_get_bonded_devices failed: %d", result);
521 result = BLUETOOTH_ERROR_INTERNAL;
523 result = BLUETOOTH_ERROR_NONE;
529 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
531 int result = BLUETOOTH_ERROR_NONE;
536 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
537 case OAL_EVENT_OAL_INITIALISED_FAILED:
538 __bt_handle_oal_initialisation(event_type);
540 case OAL_EVENT_ADAPTER_ENABLED:
541 __bt_adapter_state_change_callback(BT_ACTIVATED);
543 case OAL_EVENT_ADAPTER_DISABLED:
544 __bt_adapter_state_change_callback(BT_DEACTIVATED);
546 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
547 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
549 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
550 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
552 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
553 bt_address_t *bd_addr = event_data;
554 bluetooth_device_address_t local_address;
557 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
558 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
559 local_address.addr[0], local_address.addr[1], local_address.addr[2],
560 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
562 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
563 (void *) &local_address, sizeof(bluetooth_device_address_t));
566 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
567 char *name = event_data;
568 BT_DBG("Adapter Name: %s", name);
570 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
571 bluetooth_device_name_t local_name;
573 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
574 g_strlcpy(local_name.name,
575 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
576 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
577 (void *) &local_name, sizeof(bluetooth_device_name_t));
579 /* Send event to application */
580 _bt_send_event(BT_ADAPTER_EVENT,
581 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
582 g_variant_new("(is)", result, name));
586 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
587 char *ver = event_data;
588 bluetooth_version_t local_version;
590 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
591 g_strlcpy(local_version.version,
592 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
593 BT_DBG("BT Version: %s", local_version.version);
595 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
596 (void *) &local_version, sizeof(bluetooth_version_t));
599 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
601 gboolean connectable = FALSE;
603 BT_INFO("Adapter discoverable mode:"
604 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
605 _bt_send_event(BT_ADAPTER_EVENT,
606 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
607 g_variant_new("(b)", connectable));
609 _bt_send_event(BT_ADAPTER_EVENT,
610 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
611 g_variant_new("(in)", result, mode));
614 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
616 gboolean connectable = TRUE;
618 BT_INFO("Adapter discoverable mode:"
619 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
620 _bt_send_event(BT_ADAPTER_EVENT,
621 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
622 g_variant_new("(b)", connectable));
624 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
625 _bt_send_event(BT_ADAPTER_EVENT,
626 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
627 g_variant_new("(in)", result, mode));
630 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
633 BT_INFO("Adapter discoverable mode:"
634 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
636 /* Send event to application */
637 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
638 _bt_send_event(BT_ADAPTER_EVENT,
639 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
640 g_variant_new("(in)", result, mode));
644 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
645 int *timeout = event_data;
648 BT_INFO("Discoverable timeout: [%d]", *timeout);
650 /* Send event to application */
651 _bt_get_discoverable_mode(&mode);
652 _bt_send_event(BT_ADAPTER_EVENT,
653 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
654 g_variant_new("(in)", result, mode));
657 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
659 service_uuid_t *service_list;
660 event_adapter_services_t *list = event_data;
663 service_list = list->service_list;
664 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
667 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
670 bluetooth_device_address_t *addr_list;
672 event_device_list_t *bonded_device_list = event_data;
673 count = bonded_device_list->num;
675 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
676 for (i = 0; i < count; i++) {
677 memcpy(addr_list[i].addr,
678 bonded_device_list->devices[i].addr,
679 BLUETOOTH_ADDRESS_LENGTH);
682 BT_INFO("Adapter Bonded device List count: [%d]", count);
683 __bt_adapter_handle_pending_requests(BT_GET_BONDED_DEVICES,
684 (void *)addr_list, bonded_device_list->num);
688 BT_ERR("Unhandled event..");
695 static int __bt_init_profiles()
699 /*TODO: Init bluetooth profiles */
700 ret = _bt_hidhost_initialize();
701 if (ret != BLUETOOTH_ERROR_NONE) {
702 BT_ERR("_bt_hidhost_initialize Failed");
705 ret = _bt_socket_init();
706 if (ret != BLUETOOTH_ERROR_NONE) {
707 BT_ERR("_bt_socket_init Failed");
710 /* Initialize A2DP Source */
711 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
712 if (ret != BLUETOOTH_ERROR_NONE) {
713 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
717 /* Initialize AVRCP Target */
718 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
719 if (ret != BLUETOOTH_ERROR_NONE) {
720 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
724 /* Initialize A2DP Sink */
725 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
726 if (ret != BLUETOOTH_ERROR_NONE) {
727 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
730 /* Initialize HFP Audio Gateway */
731 ret = _bt_audio_initialize(BT_AG_MODULE);
732 if (ret != BLUETOOTH_ERROR_NONE) {
733 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
736 /* Initialize AVRCP Controller */
737 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
738 if (ret != BLUETOOTH_ERROR_NONE) {
739 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
742 /* Registering callback for receiving audio services searched */
743 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
744 if (ret != BLUETOOTH_ERROR_NONE) {
745 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
749 ret = _bt_hdp_init();
750 if (ret != BLUETOOTH_ERROR_NONE) {
751 BT_ERR("_bt_hdp_init Failed");
756 if (ret != BLUETOOTH_ERROR_NONE) {
757 BT_ERR("_bt_le_init Failed");
761 ret = _bt_gatt_init();
762 if (ret != BLUETOOTH_ERROR_NONE) {
763 BT_ERR("_bt_gatt_init Failed");
767 return BLUETOOTH_ERROR_NONE;
770 /* OAL post initialization handler */
771 static void __bt_post_oal_init(void)
774 int status = VCONFKEY_BT_STATUS_OFF;
776 BT_DBG("OAL initialized, Init profiles..");
777 ret = __bt_init_profiles();
778 if (ret != BLUETOOTH_ERROR_NONE)
779 BT_ERR("Bluetooth profile init error: %d", ret);
781 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0) {
782 BT_ERR("Fail to get the enabled value");
785 /* Update Bluetooth Status to OFF */
786 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
787 BT_ERR("Set vconf failed\n");
789 if (status & VCONFKEY_BT_STATUS_ON) {
790 ret = _bt_enable_adapter();
791 if (ret != BLUETOOTH_ERROR_NONE)
792 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
798 /* OAL initialization handler */
799 static void __bt_handle_oal_initialisation(oal_event_t event)
804 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
805 __bt_post_oal_init();
807 case OAL_EVENT_OAL_INITIALISED_FAILED:
808 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
809 g_idle_add(_bt_terminate_service, NULL);
812 BT_ERR("Unknown Event");
817 static gboolean __bt_is_service_request_present(int service_function)
820 invocation_info_t *req_info;
824 /* Get method invocation context */
825 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
827 if (req_info && req_info->service_function == service_function)
835 /* Internal functions of core adapter service */
836 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
840 invocation_info_t *req_info;
843 /* Get method invocation context */
844 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
846 if (req_info == NULL || req_info->service_function != service_function)
849 /* Create out param */
850 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
852 switch(service_function) {
853 case BT_ENABLE_ADAPTER:
854 case BT_DISABLE_ADAPTER: {
855 gboolean done = TRUE;
856 g_array_append_vals(out_param, &done, sizeof(gboolean));
859 case BT_GET_LOCAL_NAME:
860 case BT_GET_LOCAL_ADDRESS:
861 case BT_GET_LOCAL_VERSION:
862 g_array_append_vals(out_param, user_data, size);
864 case BT_IS_SERVICE_USED: {
866 gboolean used = FALSE;
868 char uuid_str[BT_UUID_STRING_SIZE];
869 char *request_uuid = req_info->user_data;
870 service_uuid_t *service_list = user_data;
872 BT_INFO("Check for service uuid: %s", request_uuid);
873 for (i = 0; i < size; i++) {
874 uuid = service_list[i].uuid;
875 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
876 BT_INFO("Adapter Service: [%s]", uuid_str);
877 if (strcasecmp(uuid_str, request_uuid) == 0) {
878 BT_INFO("UUID matched!!");
884 g_array_append_vals(out_param, &used, sizeof(gboolean));
887 case BT_GET_BONDED_DEVICES: {
888 bluetooth_device_address_t *addr_list = user_data;
889 bonded_devices_req_info_t *bonded_devices_req_info;
890 char address[BT_ADDRESS_STRING_SIZE];
892 int res = BLUETOOTH_ERROR_NONE;
895 * BT_GET_BONDED_DEVICES is already processed for this request,
896 * continue for next BT_GET_BONDED_DEVICES request if any
898 if (NULL != req_info->user_data)
901 BT_DBG("Total num of bonded devices = [%d]", count);
902 /* No bonded devices, return method invocation */
903 if (0 == count || !addr_list)
906 /* Save address list in user data for futur reference. */
907 bonded_devices_req_info = g_malloc0(sizeof(bonded_devices_req_info));
908 if (!bonded_devices_req_info) {
909 BT_ERR("Memory allocation failed");
910 req_info->result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
915 bonded_devices_req_info->count = count;
916 bonded_devices_req_info->addr_list = addr_list;
917 bonded_devices_req_info->out_param = out_param;
918 req_info->user_data = bonded_devices_req_info;
920 while (bonded_devices_req_info->count > 0) {
921 bonded_devices_req_info->count -= 1;
922 res = _bt_device_get_bonded_device_info(
923 &addr_list[bonded_devices_req_info->count]);
924 if (BLUETOOTH_ERROR_NONE == res)
927 _bt_convert_addr_type_to_string((char *)address,
928 addr_list[bonded_devices_req_info->count].addr);
929 BT_ERR("_bt_device_get_bonded_device_info Failed for [%s]", address);
930 if (bonded_devices_req_info->count == 0) {
931 g_free(bonded_devices_req_info->addr_list);
932 g_free(bonded_devices_req_info);
933 req_info->user_data = NULL;
940 BT_ERR("Unknown service function[%d]", service_function);
943 _bt_service_method_return(req_info->context, out_param, req_info->result);
944 g_array_free(out_param, TRUE);
945 /* Now free invocation info for this request*/
946 _bt_free_info_from_invocation_list(req_info);
950 /* Request return handlings */
951 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
953 BT_INFO("__bt_adapter_post_set_enabled>>");
956 __bt_set_visible_mode();
959 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
960 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
961 BT_ERR("Fail to set discoverable mode");
964 __bt_set_local_name();
966 /* Get All properties */
967 if (OAL_STATUS_SUCCESS != adapter_get_properties())
968 BT_ERR("adapter_get_properties failed");
970 /* Add Adapter enabled post processing codes */
974 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
976 BT_INFO("_bt_adapter_post_set_disabled>>");
977 /* Add Adapter disabled post processing codes */
981 static void __bt_adapter_update_bt_enabled(void)
983 int result = BLUETOOTH_ERROR_NONE;
984 BT_INFO("_bt_adapter_update_bt_enabled>>");
985 /* Update Bluetooth Status to notify other modules */
986 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
987 BT_ERR("Set vconf failed\n");
989 /* TODO:Add timer function to handle any further post processing */
990 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
992 /*Return BT_ADAPTER_ENABLE Method invocation context */
993 __bt_adapter_handle_pending_requests(BT_ENABLE_ADAPTER, NULL, 0);
994 /*Send BT Enabled event to application */
995 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
996 g_variant_new("(i)", result));
999 static void __bt_adapter_update_bt_disabled(void)
1001 int result = BLUETOOTH_ERROR_NONE;
1002 BT_INFO("_bt_adapter_update_bt_disabled>>");
1004 int power_off_status = 0;
1007 /* Update the vconf BT status in normal Deactivation case only */
1008 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1009 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1011 /* Update Bluetooth Status to notify other modules */
1012 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1013 BT_ERR("Set vconf failed\n");
1015 /* TODO:Add timer function to handle any further post processing */
1016 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1018 /* Return BT_ADAPTER_DISABLE Method invocation context */
1019 __bt_adapter_handle_pending_requests(BT_DISABLE_ADAPTER, NULL, 0);
1021 /* Send BT Disabled event to application */
1022 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1023 g_variant_new("(i)", result));
1027 static void __bt_adapter_state_set_status(bt_status_t status)
1029 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1030 adapter_state = status;
1033 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1035 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1036 adapter_discovery_state = status;
1039 static void __bt_adapter_state_change_callback(int bt_status)
1041 BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
1043 switch (bt_status) {
1044 case BT_DEACTIVATED:
1045 __bt_adapter_state_set_status(bt_status);
1047 /* Adapter is disabled, unregister event handlers */
1048 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1049 //_bt_deinit_device_event_handler();
1051 /* Add Adapter disabled post processing codes */
1052 __bt_adapter_update_bt_disabled();
1055 __bt_adapter_state_set_status(bt_status);
1056 /* Add Adapter enabled post processing codes */
1058 BT_DBG("g_source is removed");
1059 g_source_remove(timer_id);
1062 __bt_adapter_update_bt_enabled();
1065 BT_ERR("Incorrect Bluetooth adapter state changed status");
1070 static int __bt_adapter_state_handle_request(gboolean enable)
1072 int result = BLUETOOTH_ERROR_NONE;
1075 switch (adapter_state) {
1078 BT_INFO("Adapter is currently in activating state, state [%d]",
1081 return BLUETOOTH_ERROR_IN_PROGRESS;
1083 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1084 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1085 /*TODO Stop Discovery*/
1086 if (result != OAL_STATUS_SUCCESS)
1087 BT_ERR("Discover stop failed: %d", result);
1088 __bt_adapter_update_discovery_status(FALSE);
1090 result = adapter_disable();
1091 if (result != OAL_STATUS_SUCCESS) {
1092 BT_ERR("adapter_enable failed: [%d]", result);
1093 result = BLUETOOTH_ERROR_INTERNAL;
1094 /*TODO: perform if anything more needs to be done to handle failure */
1096 /* TODO: To be handled */
1097 __bt_adapter_state_set_status(BT_DEACTIVATING);
1098 result = BLUETOOTH_ERROR_NONE;
1105 BT_INFO("Adapter is currently in activated state, state [%d]",
1108 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1110 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1111 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1112 /*TODO Stop Discovery*/
1113 if (result != OAL_STATUS_SUCCESS)
1114 BT_ERR("Discover stop failed: %d", result);
1115 __bt_adapter_update_discovery_status(FALSE);
1117 result = adapter_disable();
1118 if (result != OAL_STATUS_SUCCESS) {
1119 BT_ERR("adapter_enable failed: [%d]", result);
1120 result = BLUETOOTH_ERROR_INTERNAL;
1121 /*TODO: perform if anything more needs to be done to handle failure */
1123 /* TODO: To be handled */
1124 __bt_adapter_state_set_status(BT_DEACTIVATING);
1125 result = BLUETOOTH_ERROR_NONE;
1130 case BT_DEACTIVATING:
1132 BT_INFO("Adapter is currently in deactivating state, state [%d]",
1135 return BLUETOOTH_ERROR_IN_PROGRESS;
1138 result = adapter_enable();
1139 if (result != OAL_STATUS_SUCCESS) {
1140 BT_ERR("adapter_enable failed: [%d]", result);
1142 result = BLUETOOTH_ERROR_INTERNAL;
1143 /*TODO: perform if anything more needs to be done to handle failure */
1145 /* TODO: To be handled */
1146 __bt_adapter_state_set_status(BT_ACTIVATING);
1147 result = BLUETOOTH_ERROR_NONE;
1152 case BT_DEACTIVATED:
1154 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1157 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1159 result = adapter_enable();
1160 if (result != OAL_STATUS_SUCCESS) {
1161 BT_ERR("adapter_enable failed: [%d]", result);
1163 result = BLUETOOTH_ERROR_INTERNAL;
1164 /*TODO: perform if anything more needs to be done to handle failure */
1166 /* TODO: To be handled */
1167 __bt_adapter_state_set_status(BT_ACTIVATING);
1168 result = BLUETOOTH_ERROR_NONE;
1174 if (enable && result == BLUETOOTH_ERROR_NONE) {
1175 /* Adapter enable request is successful, setup event handlers */
1176 _bt_service_register_event_handler_callback(
1177 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1178 _bt_device_state_handle_callback_set_request();
1183 static int __bt_adapter_state_discovery_request(gboolean enable,
1184 unsigned short max_response, unsigned short duration, unsigned int mask)
1186 int result = BLUETOOTH_ERROR_NONE;
1189 switch (adapter_discovery_state) {
1190 case ADAPTER_DISCOVERY_STARTED: {
1191 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1192 adapter_discovery_state);
1194 return BLUETOOTH_ERROR_IN_PROGRESS;
1196 result = adapter_stop_inquiry();
1197 if (result != OAL_STATUS_SUCCESS) {
1198 BT_ERR("Discover stop failed: %d", result);
1199 result = BLUETOOTH_ERROR_INTERNAL;
1201 BT_ERR("Stop Discovery Triggered successfully");
1202 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1203 result = BLUETOOTH_ERROR_NONE;
1208 case ADAPTER_DISCOVERY_STARTING: {
1209 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1210 adapter_discovery_state);
1212 return BLUETOOTH_ERROR_IN_PROGRESS;
1214 result = adapter_stop_inquiry();
1215 if (result != OAL_STATUS_SUCCESS) {
1216 BT_ERR("Discover stop failed: %d", result);
1217 result = BLUETOOTH_ERROR_INTERNAL;
1219 BT_ERR("Stop Discovery Triggered successfully");
1220 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1221 result = BLUETOOTH_ERROR_NONE;
1226 case ADAPTER_DISCOVERY_STOPPED: {
1227 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1228 adapter_discovery_state);
1230 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1232 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1233 result = adapter_start_inquiry(duration);
1234 if (result != OAL_STATUS_SUCCESS) {
1235 BT_ERR("Start Discovery failed: %d", result);
1236 result = BLUETOOTH_ERROR_INTERNAL;
1238 BT_ERR("Start Discovery Triggered successfully");
1239 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1240 result = BLUETOOTH_ERROR_NONE;
1245 case ADAPTER_DISCOVERY_STOPPING: {
1246 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1247 adapter_discovery_state);
1249 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1251 BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
1252 result = adapter_start_inquiry(duration);
1253 if (result != OAL_STATUS_SUCCESS) {
1254 BT_ERR("Start Discovery failed: %d", result);
1255 result = BLUETOOTH_ERROR_INTERNAL;
1257 BT_ERR("Start Discovery Triggered successfully");
1258 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1259 result = BLUETOOTH_ERROR_NONE;
1270 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1272 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1273 GVariant *param = NULL;
1274 int result = BLUETOOTH_ERROR_NONE;
1276 switch (bt_discovery_status) {
1277 case ADAPTER_DISCOVERY_STOPPED:
1279 __bt_adapter_update_discovery_status(bt_discovery_status);
1280 param = g_variant_new("(i)", result);
1281 _bt_send_event(BT_ADAPTER_EVENT,
1282 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1286 case ADAPTER_DISCOVERY_STARTED:
1288 __bt_adapter_update_discovery_status(bt_discovery_status);
1289 param = g_variant_new("(i)", result);
1290 _bt_send_event(BT_ADAPTER_EVENT,
1291 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1296 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1301 static void __bt_set_visible_mode(void)
1305 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1306 BT_ERR("Fail to get the timeout value");
1308 #ifdef TIZEN_DPM_ENABLE
1309 if (timeout == -1 ||
1310 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1311 if (_bt_set_discoverable_mode(
1312 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1313 timeout) != BLUETOOTH_ERROR_NONE) {
1314 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1315 BT_ERR("Set vconf failed");
1318 if (_bt_set_discoverable_mode(
1319 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1320 timeout) != BLUETOOTH_ERROR_NONE) {
1321 BT_ERR("Set connectable mode failed");
1325 if (timeout == -1) {
1326 if (_bt_set_discoverable_mode(
1327 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1328 timeout) != BLUETOOTH_ERROR_NONE) {
1329 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1330 BT_ERR("Set vconf failed");
1333 if (_bt_set_discoverable_mode(
1334 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1335 timeout) != BLUETOOTH_ERROR_NONE) {
1336 BT_ERR("Set connectable mode failed");
1343 static void __bt_set_local_name(void)
1345 char *phone_name = NULL;
1348 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1353 if (strlen(phone_name) != 0) {
1354 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1357 _bt_set_local_name(phone_name);