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 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
94 static gboolean __bt_is_service_request_present(int service_function);
96 static void __bt_set_visible_mode(void);
98 static void __bt_set_local_name(void);
100 /* Initialize BT stack (Initialize OAL layer) */
101 int _bt_stack_init(void)
105 BT_INFO("[bt-service] Start to initialize BT stack");
106 /* Adapter enable request is successful, setup event handlers */
107 _bt_service_register_event_handler_callback(
108 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
110 ret = oal_bt_init(_bt_service_oal_event_receiver);
112 if (OAL_STATUS_PENDING == ret) {
113 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
114 return BLUETOOTH_ERROR_NONE;
115 } else if (OAL_STATUS_SUCCESS != ret) {
116 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
117 return BLUETOOTH_ERROR_INTERNAL;
120 return BLUETOOTH_ERROR_NONE;
123 int _bt_enable_adapter(void)
125 return __bt_adapter_state_handle_request(TRUE);
128 int _bt_disable_adapter(void)
130 return __bt_adapter_state_handle_request(FALSE);
134 int _bt_start_discovery(void)
136 return __bt_adapter_state_discovery_request(TRUE);
139 int _bt_cancel_discovery(void)
141 return __bt_adapter_state_discovery_request(FALSE);
144 gboolean _bt_is_discovering(void)
146 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
147 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
153 int _bt_get_local_address(void)
159 result = adapter_get_address();
160 if (result != OAL_STATUS_SUCCESS) {
161 BT_ERR("adapter_get_address failed: %d", result);
162 result = BLUETOOTH_ERROR_INTERNAL;
164 result = BLUETOOTH_ERROR_NONE;
170 int _bt_get_local_version(void)
175 result = adapter_get_version();
176 if (result != OAL_STATUS_SUCCESS) {
177 BT_ERR("adapter_get_version failed: %d", result);
178 result = BLUETOOTH_ERROR_INTERNAL;
180 result = BLUETOOTH_ERROR_NONE;
186 int _bt_get_local_name(void)
192 result = adapter_get_name();
193 if (result != OAL_STATUS_SUCCESS) {
194 BT_ERR("adapter_get_name failed: %d", result);
195 result = BLUETOOTH_ERROR_INTERNAL;
197 result = BLUETOOTH_ERROR_NONE;
203 int _bt_set_local_name(char *local_name)
205 int result = BLUETOOTH_ERROR_NONE;
208 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
210 result = adapter_set_name(local_name);
211 if (result != OAL_STATUS_SUCCESS) {
212 BT_ERR("adapter_set_name failed: %d", result);
213 result = BLUETOOTH_ERROR_INTERNAL;
215 result = BLUETOOTH_ERROR_NONE;
221 int _bt_get_discoverable_mode(int *mode)
228 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
230 adapter_is_discoverable(&scan_mode);
231 if (TRUE == scan_mode) {
232 adapter_get_discoverable_timeout(&timeout);
234 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
236 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
238 adapter_is_connectable(&scan_mode);
239 if(scan_mode == TRUE)
240 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
243 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
244 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
245 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then set -1.
252 return BLUETOOTH_ERROR_NONE;
255 int _bt_get_timeout_value(int *timeout)
260 /* Take current time */
262 time_diff = difftime(current_time, visible_timer.start_time);
264 BT_DBG("Time diff = %d\n", time_diff);
265 *timeout = visible_timer.timeout - time_diff;
267 return BLUETOOTH_ERROR_NONE;
270 static void __bt_visibility_alarm_remove()
272 if (visible_timer.event_id > 0) {
273 g_source_remove(visible_timer.event_id);
274 visible_timer.event_id = 0;
277 if (visible_timer.alarm_id > 0) {
278 alarmmgr_remove_alarm(visible_timer.alarm_id);
279 visible_timer.alarm_id = 0;
283 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
285 int result = BLUETOOTH_ERROR_NONE;
288 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
290 if (alarm_id != visible_timer.alarm_id)
293 if (visible_timer.event_id) {
294 _bt_send_event(BT_ADAPTER_EVENT,
295 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
296 g_variant_new("(in)", result, timeout));
297 g_source_remove(visible_timer.event_id);
298 visible_timer.event_id = 0;
299 visible_timer.timeout = 0;
301 #ifndef TIZEN_PROFILE_WEARABLE
302 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
303 BT_ERR("Set vconf failed\n");
306 /* Switch Off visibility in Bluez */
307 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
308 visible_timer.alarm_id = 0;
312 static gboolean __bt_timeout_handler(gpointer user_data)
314 int result = BLUETOOTH_ERROR_NONE;
318 /* Take current time */
320 time_diff = difftime(current_time, visible_timer.start_time);
322 /* Send event to application */
323 _bt_send_event(BT_ADAPTER_EVENT,
324 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
325 g_variant_new("(in)", result, time_diff));
327 if (visible_timer.timeout <= time_diff) {
328 g_source_remove(visible_timer.event_id);
329 visible_timer.event_id = 0;
330 visible_timer.timeout = 0;
332 #ifndef TIZEN_PROFILE_WEARABLE
333 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
334 BT_ERR("Set vconf failed\n");
342 static void __bt_visibility_alarm_create()
347 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
350 BT_ERR("Failed to create alarm error = %d\n", result);
352 BT_DBG("Alarm created = %d\n", alarm_id);
353 visible_timer.alarm_id = alarm_id;
357 static int __bt_set_visible_time(int timeout)
361 __bt_visibility_alarm_remove();
363 visible_timer.timeout = timeout;
365 #ifndef TIZEN_PROFILE_WEARABLE
366 #ifdef TIZEN_DPM_ENABLE
367 if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
369 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
370 BT_ERR("Set vconf failed");
371 #ifdef TIZEN_DPM_ENABLE
377 return BLUETOOTH_ERROR_NONE;
379 if (!visible_timer.alarm_init) {
380 /* Set Alarm timer to switch off BT */
381 result = alarmmgr_init("bt-service");
383 return BLUETOOTH_ERROR_INTERNAL;
385 visible_timer.alarm_init = TRUE;
388 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
390 return BLUETOOTH_ERROR_INTERNAL;
392 /* Take start time */
393 time(&(visible_timer.start_time));
394 visible_timer.event_id = g_timeout_add_seconds(1,
395 __bt_timeout_handler, NULL);
397 __bt_visibility_alarm_create();
399 return BLUETOOTH_ERROR_NONE;
402 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
408 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
410 #ifdef TIZEN_DPM_ENABLE
411 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
412 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
413 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
414 return BLUETOOTH_ERROR_ACCESS_DENIED;
416 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
417 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
418 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
419 return BLUETOOTH_ERROR_ACCESS_DENIED;
423 switch (discoverable_mode) {
424 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
425 result = adapter_set_connectable(TRUE);
428 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
429 result = adapter_set_discoverable();
432 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
433 result = adapter_set_discoverable();
436 return BLUETOOTH_ERROR_INVALID_PARAM;
439 if (result != OAL_STATUS_SUCCESS) {
440 BT_ERR("set scan mode failed %d", result);
441 return BLUETOOTH_ERROR_INTERNAL;
444 result = adapter_set_discoverable_timeout(timeout);
445 if (result != OAL_STATUS_SUCCESS) {
446 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
447 return BLUETOOTH_ERROR_INTERNAL;
450 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
453 result = __bt_set_visible_time(timeout);
459 gboolean _bt_is_connectable(void)
466 adapter_is_connectable(&connectable);
472 BT_DBG("Connectable: [%s]", result ? "TRUE":"FALSE");
477 int _bt_set_connectable(gboolean connectable)
479 int result = BLUETOOTH_ERROR_NONE;
482 result = adapter_set_connectable(connectable);
483 if (result != OAL_STATUS_SUCCESS) {
484 BT_ERR("adapter_set_connectable failed: %d", result);
485 result = BLUETOOTH_ERROR_INTERNAL;
487 result = BLUETOOTH_ERROR_NONE;
493 int _bt_is_service_used(void)
499 result = adapter_get_service_uuids();
500 if (result != OAL_STATUS_SUCCESS) {
501 BT_ERR("adapter_get_service_uuids failed: %d", result);
502 result = BLUETOOTH_ERROR_INTERNAL;
504 result = BLUETOOTH_ERROR_NONE;
511 int _bt_adapter_get_bonded_devices(void)
513 int result = BLUETOOTH_ERROR_NONE;
516 result = adapter_get_bonded_devices();
517 if (result != OAL_STATUS_SUCCESS) {
518 BT_ERR("adapter_get_bonded_devices failed: %d", result);
519 result = BLUETOOTH_ERROR_INTERNAL;
521 result = BLUETOOTH_ERROR_NONE;
527 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
529 int result = BLUETOOTH_ERROR_NONE;
534 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
535 case OAL_EVENT_OAL_INITIALISED_FAILED:
536 __bt_handle_oal_initialisation(event_type);
538 case OAL_EVENT_ADAPTER_ENABLED:
539 __bt_adapter_state_change_callback(BT_ACTIVATED);
541 case OAL_EVENT_ADAPTER_DISABLED:
542 __bt_adapter_state_change_callback(BT_DEACTIVATED);
544 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
545 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
547 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
548 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
550 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
551 bt_address_t *bd_addr = event_data;
552 bluetooth_device_address_t local_address;
555 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
556 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
557 local_address.addr[0], local_address.addr[1], local_address.addr[2],
558 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
560 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
561 (void *) &local_address, sizeof(bluetooth_device_address_t));
564 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
565 char *name = event_data;
566 BT_DBG("Adapter Name: %s", name);
568 if (__bt_is_service_request_present(BT_GET_LOCAL_NAME)) {
569 bluetooth_device_name_t local_name;
571 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
572 g_strlcpy(local_name.name,
573 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
574 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
575 (void *) &local_name, sizeof(bluetooth_device_name_t));
577 /* Send event to application */
578 _bt_send_event(BT_ADAPTER_EVENT,
579 BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
580 g_variant_new("(is)", result, name));
584 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
585 char *ver = event_data;
586 bluetooth_version_t local_version;
588 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
589 g_strlcpy(local_version.version,
590 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
591 BT_DBG("BT Version: %s", local_version.version);
593 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
594 (void *) &local_version, sizeof(bluetooth_version_t));
597 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
599 gboolean connectable = FALSE;
601 BT_INFO("Adapter discoverable mode:"
602 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
603 _bt_send_event(BT_ADAPTER_EVENT,
604 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
605 g_variant_new("(b)", connectable));
607 _bt_send_event(BT_ADAPTER_EVENT,
608 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
609 g_variant_new("(in)", result, mode));
612 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
614 gboolean connectable = TRUE;
616 BT_INFO("Adapter discoverable mode:"
617 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
618 _bt_send_event(BT_ADAPTER_EVENT,
619 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
620 g_variant_new("(b)", connectable));
622 mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
623 _bt_send_event(BT_ADAPTER_EVENT,
624 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
625 g_variant_new("(in)", result, mode));
628 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
631 BT_INFO("Adapter discoverable mode:"
632 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
634 /* Send event to application */
635 mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
636 _bt_send_event(BT_ADAPTER_EVENT,
637 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
638 g_variant_new("(in)", result, mode));
642 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
643 int *timeout = event_data;
646 BT_INFO("Discoverable timeout: [%d]", *timeout);
648 /* Send event to application */
649 _bt_get_discoverable_mode(&mode);
650 _bt_send_event(BT_ADAPTER_EVENT,
651 BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
652 g_variant_new("(in)", result, mode));
655 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
657 service_uuid_t *service_list;
658 event_adapter_services_t *list = event_data;
661 service_list = list->service_list;
662 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
665 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
668 bluetooth_device_address_t *addr_list;
670 event_device_list_t *bonded_device_list = event_data;
671 count = bonded_device_list->num;
673 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
674 for (i = 0; i < count; i++) {
675 memcpy(addr_list[i].addr,
676 bonded_device_list->devices[i].addr,
677 BLUETOOTH_ADDRESS_LENGTH);
680 BT_INFO("Adapter Bonded device List count: [%d]", count);
681 __bt_adapter_handle_pending_requests(BT_GET_BONDED_DEVICES,
682 (void *)addr_list, bonded_device_list->num);
686 BT_ERR("Unhandled event..");
693 static int __bt_init_profiles()
697 /*TODO: Init bluetooth profiles */
698 ret = _bt_hidhost_initialize();
699 if (ret != BLUETOOTH_ERROR_NONE) {
700 BT_ERR("_bt_hidhost_initialize Failed");
703 ret = _bt_socket_init();
704 if (ret != BLUETOOTH_ERROR_NONE) {
705 BT_ERR("_bt_socket_init Failed");
708 /* Initialize A2DP Source */
709 ret = _bt_audio_initialize(BT_A2DP_SOURCE_MODULE);
710 if (ret != BLUETOOTH_ERROR_NONE) {
711 BT_ERR("_bt_audio_initialize(BT_A2DP_SOURCE_MODULE) Failed");
715 /* Initialize AVRCP Target */
716 ret = _bt_audio_initialize(BT_AVRCP_MODULE);
717 if (ret != BLUETOOTH_ERROR_NONE) {
718 BT_ERR("_bt_audio_initialize(BT_AVRCP_MODULE) Failed");
722 /* Initialize A2DP Sink */
723 ret = _bt_audio_initialize(BT_A2DP_SINK_MODULE);
724 if (ret != BLUETOOTH_ERROR_NONE) {
725 BT_ERR("_bt_audio_initialize(BT_A2DP_SINK_MODULE) Failed");
728 /* Initialize HFP Audio Gateway */
729 ret = _bt_audio_initialize(BT_AG_MODULE);
730 if (ret != BLUETOOTH_ERROR_NONE) {
731 BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
734 /* Initialize AVRCP Controller */
735 ret = _bt_audio_initialize(BT_AVRCP_CTRL_MODULE);
736 if (ret != BLUETOOTH_ERROR_NONE) {
737 BT_ERR("_bt_audio_initialize(BT_AVRCP_CTRL_MODULE) Failed");
740 /* Registering callback for receiving audio services searched */
741 ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
742 if (ret != BLUETOOTH_ERROR_NONE) {
743 BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
747 ret = _bt_hdp_init();
748 if (ret != BLUETOOTH_ERROR_NONE) {
749 BT_ERR("_bt_hdp_init Failed");
754 if (ret != BLUETOOTH_ERROR_NONE) {
755 BT_ERR("_bt_le_init Failed");
759 ret = _bt_gatt_init();
760 if (ret != BLUETOOTH_ERROR_NONE) {
761 BT_ERR("_bt_gatt_init Failed");
765 return BLUETOOTH_ERROR_NONE;
768 /* OAL post initialization handler */
769 static void __bt_post_oal_init(void)
772 int status = VCONFKEY_BT_STATUS_OFF;
774 BT_DBG("OAL initialized, Init profiles..");
775 ret = __bt_init_profiles();
776 if (ret != BLUETOOTH_ERROR_NONE)
777 BT_ERR("Bluetooth profile init error: %d", ret);
779 if (vconf_get_int(VCONFKEY_BT_STATUS, &status) != 0) {
780 BT_ERR("Fail to get the enabled value");
783 /* Update Bluetooth Status to OFF */
784 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
785 BT_ERR("Set vconf failed\n");
787 if (status & VCONFKEY_BT_STATUS_ON) {
788 ret = _bt_enable_adapter();
789 if (ret != BLUETOOTH_ERROR_NONE)
790 BT_ERR("_bt_enable_adapter failed with error: %d", ret);
796 /* OAL initialization handler */
797 static void __bt_handle_oal_initialisation(oal_event_t event)
802 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
803 __bt_post_oal_init();
805 case OAL_EVENT_OAL_INITIALISED_FAILED:
806 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
807 g_idle_add(_bt_terminate_service, NULL);
810 BT_ERR("Unknown Event");
815 static gboolean __bt_is_service_request_present(int service_function)
818 invocation_info_t *req_info;
822 /* Get method invocation context */
823 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
825 if (req_info && req_info->service_function == service_function)
833 /* Internal functions of core adapter service */
834 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
838 invocation_info_t *req_info;
841 /* Get method invocation context */
842 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
844 if (req_info == NULL || req_info->service_function != service_function)
847 /* Create out param */
848 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
850 switch(service_function) {
851 case BT_ENABLE_ADAPTER:
852 case BT_DISABLE_ADAPTER: {
853 gboolean done = TRUE;
854 g_array_append_vals(out_param, &done, sizeof(gboolean));
857 case BT_GET_LOCAL_NAME:
858 case BT_GET_LOCAL_ADDRESS:
859 case BT_GET_LOCAL_VERSION:
860 g_array_append_vals(out_param, user_data, size);
862 case BT_IS_SERVICE_USED: {
864 gboolean used = FALSE;
866 char uuid_str[BT_UUID_STRING_SIZE];
867 char *request_uuid = req_info->user_data;
868 service_uuid_t *service_list = user_data;
870 BT_INFO("Check for service uuid: %s", request_uuid);
871 for (i = 0; i < size; i++) {
872 uuid = service_list[i].uuid;
873 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
874 BT_INFO("Adapter Service: [%s]", uuid_str);
875 if (strcasecmp(uuid_str, request_uuid) == 0) {
876 BT_INFO("UUID matched!!");
882 g_array_append_vals(out_param, &used, sizeof(gboolean));
885 case BT_GET_BONDED_DEVICES: {
886 bluetooth_device_address_t *addr_list = user_data;
887 bonded_devices_req_info_t *bonded_devices_req_info;
888 char address[BT_ADDRESS_STRING_SIZE];
890 int res = BLUETOOTH_ERROR_NONE;
893 * BT_GET_BONDED_DEVICES is already processed for this request,
894 * continue for next BT_GET_BONDED_DEVICES request if any
896 if (NULL != req_info->user_data)
899 BT_DBG("Total num of bonded devices = [%d]", count);
900 /* No bonded devices, return method invocation */
901 if (0 == count || !addr_list)
904 /* Save address list in user data for futur reference. */
905 bonded_devices_req_info = g_malloc0(sizeof(bonded_devices_req_info));
906 if (!bonded_devices_req_info) {
907 BT_ERR("Memory allocation failed");
908 req_info->result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
913 bonded_devices_req_info->count = count;
914 bonded_devices_req_info->addr_list = addr_list;
915 bonded_devices_req_info->out_param = out_param;
916 req_info->user_data = bonded_devices_req_info;
918 while (bonded_devices_req_info->count > 0) {
919 bonded_devices_req_info->count -= 1;
920 res = _bt_device_get_bonded_device_info(
921 &addr_list[bonded_devices_req_info->count]);
922 if (BLUETOOTH_ERROR_NONE == res)
925 _bt_convert_addr_type_to_string((char *)address,
926 addr_list[bonded_devices_req_info->count].addr);
927 BT_ERR("_bt_device_get_bonded_device_info Failed for [%s]", address);
928 if (bonded_devices_req_info->count == 0) {
929 g_free(bonded_devices_req_info->addr_list);
930 g_free(bonded_devices_req_info);
931 req_info->user_data = NULL;
938 BT_ERR("Unknown service function[%d]", service_function);
941 _bt_service_method_return(req_info->context, out_param, req_info->result);
942 g_array_free(out_param, TRUE);
943 /* Now free invocation info for this request*/
944 _bt_free_info_from_invocation_list(req_info);
948 /* Request return handlings */
949 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
951 BT_INFO("__bt_adapter_post_set_enabled>>");
954 __bt_set_visible_mode();
957 if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
958 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
959 BT_ERR("Fail to set discoverable mode");
962 __bt_set_local_name();
964 /* Get All properties */
965 if (OAL_STATUS_SUCCESS != adapter_get_properties())
966 BT_ERR("adapter_get_properties failed");
968 /* Add Adapter enabled post processing codes */
972 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
974 BT_INFO("_bt_adapter_post_set_disabled>>");
975 /* Add Adapter disabled post processing codes */
979 static void __bt_adapter_update_bt_enabled(void)
981 int result = BLUETOOTH_ERROR_NONE;
982 BT_INFO("_bt_adapter_update_bt_enabled>>");
983 /* Update Bluetooth Status to notify other modules */
984 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
985 BT_ERR("Set vconf failed\n");
987 /* TODO:Add timer function to handle any further post processing */
988 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
990 /*Return BT_ADAPTER_ENABLE Method invocation context */
991 __bt_adapter_handle_pending_requests(BT_ENABLE_ADAPTER, NULL, 0);
992 /*Send BT Enabled event to application */
993 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
994 g_variant_new("(i)", result));
997 static void __bt_adapter_update_bt_disabled(void)
999 int result = BLUETOOTH_ERROR_NONE;
1000 BT_INFO("_bt_adapter_update_bt_disabled>>");
1002 int power_off_status = 0;
1005 /* Update the vconf BT status in normal Deactivation case only */
1006 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
1007 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
1009 /* Update Bluetooth Status to notify other modules */
1010 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1011 BT_ERR("Set vconf failed\n");
1013 /* TODO:Add timer function to handle any further post processing */
1014 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
1016 /* Return BT_ADAPTER_DISABLE Method invocation context */
1017 __bt_adapter_handle_pending_requests(BT_DISABLE_ADAPTER, NULL, 0);
1019 /* Send BT Disabled event to application */
1020 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
1021 g_variant_new("(i)", result));
1025 static void __bt_adapter_state_set_status(bt_status_t status)
1027 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
1028 adapter_state = status;
1031 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
1033 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
1034 adapter_discovery_state = status;
1037 static void __bt_adapter_state_change_callback(int bt_status)
1039 BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
1041 switch (bt_status) {
1042 case BT_DEACTIVATED:
1043 __bt_adapter_state_set_status(bt_status);
1045 /* Adapter is disabled, unregister event handlers */
1046 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
1047 //_bt_deinit_device_event_handler();
1049 /* Add Adapter disabled post processing codes */
1050 __bt_adapter_update_bt_disabled();
1053 __bt_adapter_state_set_status(bt_status);
1054 /* Add Adapter enabled post processing codes */
1056 BT_DBG("g_source is removed");
1057 g_source_remove(timer_id);
1060 __bt_adapter_update_bt_enabled();
1063 BT_ERR("Incorrect Bluetooth adapter state changed status");
1068 static int __bt_adapter_state_handle_request(gboolean enable)
1070 int result = BLUETOOTH_ERROR_NONE;
1073 switch (adapter_state) {
1076 BT_INFO("Adapter is currently in activating state, state [%d]",
1079 return BLUETOOTH_ERROR_IN_PROGRESS;
1081 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1082 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1083 /*TODO Stop Discovery*/
1084 if (result != OAL_STATUS_SUCCESS)
1085 BT_ERR("Discover stop failed: %d", result);
1086 __bt_adapter_update_discovery_status(FALSE);
1088 result = adapter_disable();
1089 if (result != OAL_STATUS_SUCCESS) {
1090 BT_ERR("adapter_enable failed: [%d]", result);
1091 result = BLUETOOTH_ERROR_INTERNAL;
1092 /*TODO: perform if anything more needs to be done to handle failure */
1094 /* TODO: To be handled */
1095 __bt_adapter_state_set_status(BT_DEACTIVATING);
1096 result = BLUETOOTH_ERROR_NONE;
1103 BT_INFO("Adapter is currently in activated state, state [%d]",
1106 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1108 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
1109 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
1110 /*TODO Stop Discovery*/
1111 if (result != OAL_STATUS_SUCCESS)
1112 BT_ERR("Discover stop failed: %d", result);
1113 __bt_adapter_update_discovery_status(FALSE);
1115 result = adapter_disable();
1116 if (result != OAL_STATUS_SUCCESS) {
1117 BT_ERR("adapter_enable failed: [%d]", result);
1118 result = BLUETOOTH_ERROR_INTERNAL;
1119 /*TODO: perform if anything more needs to be done to handle failure */
1121 /* TODO: To be handled */
1122 __bt_adapter_state_set_status(BT_DEACTIVATING);
1123 result = BLUETOOTH_ERROR_NONE;
1128 case BT_DEACTIVATING:
1130 BT_INFO("Adapter is currently in deactivating state, state [%d]",
1133 return BLUETOOTH_ERROR_IN_PROGRESS;
1136 result = adapter_enable();
1137 if (result != OAL_STATUS_SUCCESS) {
1138 BT_ERR("adapter_enable failed: [%d]", result);
1140 result = BLUETOOTH_ERROR_INTERNAL;
1141 /*TODO: perform if anything more needs to be done to handle failure */
1143 /* TODO: To be handled */
1144 __bt_adapter_state_set_status(BT_ACTIVATING);
1145 result = BLUETOOTH_ERROR_NONE;
1150 case BT_DEACTIVATED:
1152 BT_INFO("Adapter is currently in deactivated state, state [%d]",
1155 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1157 result = adapter_enable();
1158 if (result != OAL_STATUS_SUCCESS) {
1159 BT_ERR("adapter_enable failed: [%d]", result);
1161 result = BLUETOOTH_ERROR_INTERNAL;
1162 /*TODO: perform if anything more needs to be done to handle failure */
1164 /* TODO: To be handled */
1165 __bt_adapter_state_set_status(BT_ACTIVATING);
1166 result = BLUETOOTH_ERROR_NONE;
1172 if (enable && result == BLUETOOTH_ERROR_NONE) {
1173 /* Adapter enable request is successful, setup event handlers */
1174 _bt_service_register_event_handler_callback(
1175 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
1176 _bt_device_state_handle_callback_set_request();
1181 static int __bt_adapter_state_discovery_request(gboolean enable)
1183 int result = BLUETOOTH_ERROR_NONE;
1186 switch (adapter_discovery_state) {
1187 case ADAPTER_DISCOVERY_STARTED: {
1188 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1189 adapter_discovery_state);
1191 return BLUETOOTH_ERROR_IN_PROGRESS;
1193 result = adapter_stop_inquiry();
1194 if (result != OAL_STATUS_SUCCESS) {
1195 BT_ERR("Discover stop failed: %d", result);
1196 result = BLUETOOTH_ERROR_INTERNAL;
1198 BT_ERR("Stop Discovery Triggered successfully");
1199 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1200 result = BLUETOOTH_ERROR_NONE;
1205 case ADAPTER_DISCOVERY_STARTING: {
1206 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1207 adapter_discovery_state);
1209 return BLUETOOTH_ERROR_IN_PROGRESS;
1211 result = adapter_stop_inquiry();
1212 if (result != OAL_STATUS_SUCCESS) {
1213 BT_ERR("Discover stop failed: %d", result);
1214 result = BLUETOOTH_ERROR_INTERNAL;
1216 BT_ERR("Stop Discovery Triggered successfully");
1217 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1218 result = BLUETOOTH_ERROR_NONE;
1223 case ADAPTER_DISCOVERY_STOPPED: {
1224 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1225 adapter_discovery_state);
1227 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1229 result = adapter_start_inquiry();
1230 if (result != OAL_STATUS_SUCCESS) {
1231 BT_ERR("Start Discovery failed: %d", result);
1232 result = BLUETOOTH_ERROR_INTERNAL;
1234 BT_ERR("Start Discovery Triggered successfully");
1235 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1236 result = BLUETOOTH_ERROR_NONE;
1241 case ADAPTER_DISCOVERY_STOPPING: {
1242 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1243 adapter_discovery_state);
1245 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1247 result = adapter_start_inquiry();
1248 if (result != OAL_STATUS_SUCCESS) {
1249 BT_ERR("Start Discovery failed: %d", result);
1250 result = BLUETOOTH_ERROR_INTERNAL;
1252 BT_ERR("Start Discovery Triggered successfully");
1253 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1254 result = BLUETOOTH_ERROR_NONE;
1265 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1267 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1268 GVariant *param = NULL;
1269 int result = BLUETOOTH_ERROR_NONE;
1271 switch (bt_discovery_status) {
1272 case ADAPTER_DISCOVERY_STOPPED:
1274 __bt_adapter_update_discovery_status(bt_discovery_status);
1275 param = g_variant_new("(i)", result);
1276 _bt_send_event(BT_ADAPTER_EVENT,
1277 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1281 case ADAPTER_DISCOVERY_STARTED:
1283 __bt_adapter_update_discovery_status(bt_discovery_status);
1284 param = g_variant_new("(i)", result);
1285 _bt_send_event(BT_ADAPTER_EVENT,
1286 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1291 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");
1296 static void __bt_set_visible_mode(void)
1300 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
1301 BT_ERR("Fail to get the timeout value");
1303 #ifdef TIZEN_DPM_ENABLE
1304 if (timeout == -1 ||
1305 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1306 if (_bt_set_discoverable_mode(
1307 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1308 timeout) != BLUETOOTH_ERROR_NONE) {
1309 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1310 BT_ERR("Set vconf failed");
1313 if (_bt_set_discoverable_mode(
1314 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1315 timeout) != BLUETOOTH_ERROR_NONE) {
1316 BT_ERR("Set connectable mode failed");
1320 if (timeout == -1) {
1321 if (_bt_set_discoverable_mode(
1322 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
1323 timeout) != BLUETOOTH_ERROR_NONE) {
1324 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
1325 BT_ERR("Set vconf failed");
1328 if (_bt_set_discoverable_mode(
1329 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
1330 timeout) != BLUETOOTH_ERROR_NONE) {
1331 BT_ERR("Set connectable mode failed");
1338 static void __bt_set_local_name(void)
1340 char *phone_name = NULL;
1343 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1348 if (strlen(phone_name) != 0) {
1349 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
1352 _bt_set_local_name(phone_name);