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 #ifdef TIZEN_DPM_ENABLE
45 #include "bt-service-dpm.h"
49 #include <oal-event.h>
50 #include <oal-manager.h>
51 #include <oal-adapter-mgr.h>
53 #define BT_ENABLE_TIMEOUT 20000 /* 20 seconds */
55 /*This file will contain state machines related to adapter and remote device */
57 /* Global variables */
66 static bt_adapter_timer_t visible_timer;
68 static guint timer_id = 0;
70 /* Adapter default states */
71 static bt_status_t adapter_state = BT_DEACTIVATED;
72 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
74 /* Forward declarations */
75 static void __bt_adapter_event_handler(int event_type, gpointer event_data);
76 static void __bt_post_oal_init(void);
77 static void __bt_handle_oal_initialisation(oal_event_t event);
78 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size);
79 static gboolean __bt_adapter_post_set_enabled(gpointer user_data);
80 static gboolean __bt_adapter_post_set_disabled(gpointer user_data);
81 static void __bt_adapter_update_bt_enabled(void);
82 static void __bt_adapter_update_bt_disabled(void);
83 static void __bt_adapter_state_set_status(bt_status_t status);
84 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status);
85 static void __bt_adapter_state_change_callback(int bt_status);
86 static int __bt_adapter_state_handle_request(gboolean enable);
87 static int __bt_adapter_state_discovery_request(gboolean enable);
88 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
90 /* Initialize BT stack (Initialize OAL layer) */
91 int _bt_stack_init(void)
95 BT_INFO("[bt-service] Start to initialize BT stack");
96 /* Adapter enable request is successful, setup event handlers */
97 _bt_service_register_event_handler_callback(
98 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
100 ret = oal_bt_init(_bt_service_oal_event_receiver);
102 if (OAL_STATUS_PENDING == ret) {
103 BT_INFO("OAL Initialisation Pending, Profiles Init will be done once oal initialised...");
104 return BLUETOOTH_ERROR_NONE;
105 } else if (OAL_STATUS_SUCCESS != ret) {
106 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
107 return BLUETOOTH_ERROR_INTERNAL;
110 __bt_post_oal_init();
111 return BLUETOOTH_ERROR_NONE;
114 int _bt_enable_adapter(void)
116 return __bt_adapter_state_handle_request(TRUE);
119 int _bt_disable_adapter(void)
121 return __bt_adapter_state_handle_request(FALSE);
125 int _bt_start_discovery(void)
127 return __bt_adapter_state_discovery_request(TRUE);
130 int _bt_cancel_discovery(void)
132 return __bt_adapter_state_discovery_request(FALSE);
135 gboolean _bt_is_discovering(void)
137 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED
138 || adapter_discovery_state == ADAPTER_DISCOVERY_STARTING)
144 int _bt_get_local_address(void)
150 result = adapter_get_address();
151 if (result != OAL_STATUS_SUCCESS) {
152 BT_ERR("adapter_get_address failed: %d", result);
153 result = BLUETOOTH_ERROR_INTERNAL;
155 result = BLUETOOTH_ERROR_NONE;
161 int _bt_get_local_version(void)
166 result = adapter_get_version();
167 if (result != OAL_STATUS_SUCCESS) {
168 BT_ERR("adapter_get_address failed: %d", result);
169 result = BLUETOOTH_ERROR_INTERNAL;
171 result = BLUETOOTH_ERROR_NONE;
177 int _bt_get_local_name(void)
183 result = adapter_get_name();
184 if (result != OAL_STATUS_SUCCESS) {
185 BT_ERR("adapter_get_name failed: %d", result);
186 result = BLUETOOTH_ERROR_INTERNAL;
188 result = BLUETOOTH_ERROR_NONE;
194 int _bt_set_local_name(char *local_name)
196 int result = BLUETOOTH_ERROR_NONE;
199 retv_if(NULL == local_name, BLUETOOTH_ERROR_INVALID_PARAM);
201 result = adapter_set_name(local_name);
202 if (result != OAL_STATUS_SUCCESS) {
203 BT_ERR("adapter_set_name failed: %d", result);
204 result = BLUETOOTH_ERROR_INTERNAL;
206 result = BLUETOOTH_ERROR_NONE;
212 int _bt_get_discoverable_mode(int *mode)
219 retv_if(NULL == mode, BLUETOOTH_ERROR_INVALID_PARAM);
221 adapter_is_discoverable(&scan_mode);
222 if (TRUE == scan_mode) {
223 adapter_get_discoverable_timeout(&timeout);
225 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
227 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
229 adapter_is_connectable(&scan_mode);
230 if(scan_mode == TRUE)
231 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
234 * TODO: NON CONNECTABLE is not defined in bluetooth_discoverable_mode_t.
235 * After adding BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE, set mode as
236 * BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE. Until then return -1.
243 return BLUETOOTH_ERROR_NONE;
246 int _bt_get_timeout_value(int *timeout)
251 /* Take current time */
253 time_diff = difftime(current_time, visible_timer.start_time);
255 BT_DBG("Time diff = %d\n", time_diff);
256 *timeout = visible_timer.timeout - time_diff;
258 return BLUETOOTH_ERROR_NONE;
261 static void __bt_visibility_alarm_remove()
263 if (visible_timer.event_id > 0) {
264 g_source_remove(visible_timer.event_id);
265 visible_timer.event_id = 0;
268 if (visible_timer.alarm_id > 0) {
269 alarmmgr_remove_alarm(visible_timer.alarm_id);
270 visible_timer.alarm_id = 0;
274 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
276 int result = BLUETOOTH_ERROR_NONE;
279 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
281 if (alarm_id != visible_timer.alarm_id)
284 if (visible_timer.event_id) {
285 _bt_send_event(BT_ADAPTER_EVENT,
286 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
287 g_variant_new("(in)", result, timeout));
288 g_source_remove(visible_timer.event_id);
289 visible_timer.event_id = 0;
290 visible_timer.timeout = 0;
292 #ifndef TIZEN_WEARABLE
293 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
294 BT_ERR("Set vconf failed\n");
297 /* Switch Off visibility in Bluez */
298 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
299 visible_timer.alarm_id = 0;
303 static gboolean __bt_timeout_handler(gpointer user_data)
305 int result = BLUETOOTH_ERROR_NONE;
309 /* Take current time */
311 time_diff = difftime(current_time, visible_timer.start_time);
313 /* Send event to application */
314 _bt_send_event(BT_ADAPTER_EVENT,
315 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
316 g_variant_new("(in)", result, time_diff));
318 if (visible_timer.timeout <= time_diff) {
319 g_source_remove(visible_timer.event_id);
320 visible_timer.event_id = 0;
321 visible_timer.timeout = 0;
323 #ifndef TIZEN_WEARABLE
324 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
325 BT_ERR("Set vconf failed\n");
333 static void __bt_visibility_alarm_create()
338 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
341 BT_ERR("Failed to create alarm error = %d\n", result);
343 BT_DBG("Alarm created = %d\n", alarm_id);
344 visible_timer.alarm_id = alarm_id;
348 static int __bt_set_visible_time(int timeout)
352 __bt_visibility_alarm_remove();
354 visible_timer.timeout = timeout;
356 #ifndef TIZEN_WEARABLE
357 #ifdef TIZEN_DPM_ENABLE
358 if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
360 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
361 BT_ERR("Set vconf failed");
362 #ifdef TIZEN_DPM_ENABLE
368 return BLUETOOTH_ERROR_NONE;
370 if (!visible_timer.alarm_init) {
371 /* Set Alarm timer to switch off BT */
372 result = alarmmgr_init("bt-service");
374 return BLUETOOTH_ERROR_INTERNAL;
376 visible_timer.alarm_init = TRUE;
379 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
381 return BLUETOOTH_ERROR_INTERNAL;
383 /* Take start time */
384 time(&(visible_timer.start_time));
385 visible_timer.event_id = g_timeout_add_seconds(1,
386 __bt_timeout_handler, NULL);
388 __bt_visibility_alarm_create();
390 return BLUETOOTH_ERROR_NONE;
393 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
399 BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
401 #ifdef TIZEN_DPM_ENABLE
402 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
403 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
404 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
405 return BLUETOOTH_ERROR_ACCESS_DENIED;
407 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
408 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
409 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
410 return BLUETOOTH_ERROR_ACCESS_DENIED;
414 switch (discoverable_mode) {
415 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
416 result = adapter_set_connectable(TRUE);
419 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
420 result = adapter_set_discoverable();
423 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
424 result = adapter_set_discoverable();
427 return BLUETOOTH_ERROR_INVALID_PARAM;
430 if (result != OAL_STATUS_SUCCESS) {
431 BT_ERR("set scan mode failed %d", result);
432 return BLUETOOTH_ERROR_INTERNAL;
435 result = adapter_set_discoverable_timeout(timeout);
436 if (result != OAL_STATUS_SUCCESS) {
437 BT_ERR("adapter_set_discoverable_timeout failed %d", result);
438 return BLUETOOTH_ERROR_INTERNAL;
441 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
444 result = __bt_set_visible_time(timeout);
450 gboolean _bt_is_connectable(void)
457 adapter_is_connectable(&connectable);
463 BT_DBG("Connectable: [%s]", result ? "TRUE":"FALSE");
468 int _bt_set_connectable(gboolean connectable)
470 int result = BLUETOOTH_ERROR_NONE;
473 result = adapter_set_connectable(connectable);
474 if (result != OAL_STATUS_SUCCESS) {
475 BT_ERR("adapter_get_address failed: %d", result);
476 result = BLUETOOTH_ERROR_INTERNAL;
478 result = BLUETOOTH_ERROR_NONE;
484 int _bt_is_service_used(void)
490 result = adapter_get_service_uuids();
491 if (result != OAL_STATUS_SUCCESS) {
492 BT_ERR("adapter_get_service_uuids failed: %d", result);
493 result = BLUETOOTH_ERROR_INTERNAL;
495 result = BLUETOOTH_ERROR_NONE;
502 int _bt_adapter_get_bonded_devices(void)
504 int result = BLUETOOTH_ERROR_NONE;
507 result = adapter_get_bonded_devices();
508 if (result != OAL_STATUS_SUCCESS) {
509 BT_ERR("adapter_get_bonded_devices failed: %d", result);
510 result = BLUETOOTH_ERROR_INTERNAL;
512 result = BLUETOOTH_ERROR_NONE;
518 static void __bt_adapter_event_handler(int event_type, gpointer event_data)
523 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
524 case OAL_EVENT_OAL_INITIALISED_FAILED:
525 __bt_handle_oal_initialisation(event_type);
527 case OAL_EVENT_ADAPTER_ENABLED:
528 __bt_adapter_state_change_callback(BT_ACTIVATED);
530 case OAL_EVENT_ADAPTER_DISABLED:
531 __bt_adapter_state_change_callback(BT_DEACTIVATED);
533 case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
534 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STARTED);
536 case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
537 __bt_adapter_discovery_state_change_callback(ADAPTER_DISCOVERY_STOPPED);
539 case OAL_EVENT_ADAPTER_PROPERTY_ADDRESS: {
540 bt_address_t *bd_addr = event_data;
541 bluetooth_device_address_t local_address;
544 memcpy(local_address.addr, bd_addr->addr, BT_ADDRESS_LENGTH_MAX);
545 BT_DBG("Adapter address: [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]",
546 local_address.addr[0], local_address.addr[1], local_address.addr[2],
547 local_address.addr[3], local_address.addr[4], local_address.addr[5]);
549 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_ADDRESS,
550 (void *) &local_address, sizeof(bluetooth_device_address_t));
553 case OAL_EVENT_ADAPTER_PROPERTY_NAME: {
554 char *name = event_data;
555 bluetooth_device_name_t local_name;
557 memset(&local_name, 0x00, sizeof(bluetooth_device_name_t));
559 g_strlcpy(local_name.name,
560 (const gchar *)name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
561 BT_DBG("Adapter Name: %s", local_name.name);
563 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_NAME,
564 (void *) &local_name, sizeof(bluetooth_device_name_t));
567 case OAL_EVENT_ADAPTER_PROPERTY_VERSION: {
568 char *ver = event_data;
569 bluetooth_version_t local_version;
571 memset(&local_version, 0x00, sizeof(bluetooth_version_t));
572 g_strlcpy(local_version.version,
573 (const gchar *)ver, BLUETOOTH_VERSION_LENGTH_MAX);
574 BT_DBG("BT Version: %s", local_version.version);
576 __bt_adapter_handle_pending_requests(BT_GET_LOCAL_VERSION,
577 (void *) &local_version, sizeof(bluetooth_version_t));
580 case OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE: {
581 gboolean connectable = FALSE;
583 BT_INFO("Adapter discoverable mode:"
584 " BLUETOOTH_DISCOVERABLE_MODE_NON_CONNECTABLE");
585 _bt_send_event(BT_ADAPTER_EVENT,
586 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
587 g_variant_new("(b)", connectable));
590 case OAL_EVENT_ADAPTER_MODE_CONNECTABLE: {
591 gboolean connectable = TRUE;
593 BT_INFO("Adapter discoverable mode:"
594 " BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE");
595 _bt_send_event(BT_ADAPTER_EVENT,
596 BLUETOOTH_EVENT_CONNECTABLE_CHANGED,
597 g_variant_new("(b)", connectable));
600 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE: {
601 BT_INFO("Adapter discoverable mode:"
602 " BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE");
605 case OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT: {
606 int *timeout = event_data;
608 BT_INFO("Discoverable timeout: [%d]", *timeout);
611 case OAL_EVENT_ADAPTER_PROPERTY_SERVICES: {
613 service_uuid_t *service_list;
614 event_adapter_services_t *list = event_data;
617 service_list = list->service_list;
618 __bt_adapter_handle_pending_requests(BT_IS_SERVICE_USED, service_list, count);
621 case OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST: {
624 bluetooth_device_address_t *addr_list;
626 event_device_list_t *bonded_device_list = event_data;
627 count = bonded_device_list->num;
629 addr_list = g_malloc0(count * sizeof(bluetooth_device_address_t));
630 for (i = 0; i < count; i++) {
631 memcpy(addr_list[i].addr,
632 bonded_device_list->devices[i].addr,
633 BLUETOOTH_ADDRESS_LENGTH);
636 __bt_adapter_handle_pending_requests(BT_GET_BONDED_DEVICES,
637 (void *)addr_list, bonded_device_list->num);
641 BT_ERR("Unhandled event..");
648 /* OAL post initialization handler */
649 static void __bt_post_oal_init(void)
651 BT_DBG("OAL initialized, Init profiles..");
656 /* OAL initialization handler */
657 static void __bt_handle_oal_initialisation(oal_event_t event)
662 case OAL_EVENT_OAL_INITIALISED_SUCCESS:
663 __bt_post_oal_init();
665 case OAL_EVENT_OAL_INITIALISED_FAILED:
666 BT_ERR("OAL Initialisation Failed, terminate bt-service daemon..");
667 g_idle_add(_bt_terminate_service, NULL);
670 BT_ERR("Unknown Event");
675 /* Internal functions of core adapter service */
676 static void __bt_adapter_handle_pending_requests(int service_function, void *user_data, unsigned int size)
680 invocation_info_t *req_info;
683 /* Get method invocation context */
684 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
686 if (req_info == NULL || req_info->service_function != service_function)
689 /* Create out param */
690 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
692 switch(service_function) {
693 case BT_ENABLE_ADAPTER:
694 case BT_DISABLE_ADAPTER: {
695 gboolean done = TRUE;
696 g_array_append_vals(out_param, &done, sizeof(gboolean));
699 case BT_GET_LOCAL_NAME:
700 case BT_GET_LOCAL_ADDRESS:
701 case BT_GET_LOCAL_VERSION:
702 g_array_append_vals(out_param, user_data, size);
704 case BT_IS_SERVICE_USED: {
706 gboolean used = FALSE;
708 char uuid_str[BT_UUID_STRING_SIZE];
709 char *request_uuid = req_info->user_data;
710 service_uuid_t *service_list = user_data;
712 BT_INFO("Check for service uuid: %s", request_uuid);
713 for (i = 0; i < size; i++) {
714 uuid = service_list[i].uuid;
715 _bt_service_convert_uuid_type_to_string(uuid_str, uuid);
716 BT_INFO("Adapter Service: [%s]", uuid_str);
717 if (strcasecmp(uuid_str, request_uuid) == 0) {
718 BT_INFO("UUID matched!!");
724 g_array_append_vals(out_param, &used, sizeof(gboolean));
727 case BT_GET_BONDED_DEVICES: {
728 bluetooth_device_address_t *addr_list = user_data;
729 bonded_devices_req_info_t *bonded_devices_req_info;
730 char address[BT_ADDRESS_STRING_SIZE];
732 int res = BLUETOOTH_ERROR_NONE;
735 * BT_GET_BONDED_DEVICES is already processed for this request,
736 * continue for next BT_GET_BONDED_DEVICES request if any
738 if (NULL != req_info->user_data)
741 BT_DBG("BT_GET_BONDED_DEVICES: count = [%d]", count);
742 /* No bonded devices, return method invocation */
743 if (0 == count || !addr_list)
746 /* Save address list in user data for futur reference. */
747 bonded_devices_req_info = g_malloc0(sizeof(bonded_devices_req_info));
748 if (!bonded_devices_req_info) {
749 BT_ERR("Memory allocation failed");
750 req_info->result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
755 bonded_devices_req_info->count = count;
756 bonded_devices_req_info->addr_list = addr_list;
757 bonded_devices_req_info->out_param = out_param;
758 req_info->user_data = bonded_devices_req_info;
760 while (bonded_devices_req_info->count > 0) {
761 bonded_devices_req_info->count -= 1;
762 res = _bt_device_get_bonded_device_info(
763 &addr_list[bonded_devices_req_info->count]);
764 if (BLUETOOTH_ERROR_NONE == res)
767 _bt_convert_addr_type_to_string((char *)address,
768 addr_list[bonded_devices_req_info->count].addr);
769 BT_ERR("_bt_device_get_bonded_device_info Failed for [%s]", address);
770 if (bonded_devices_req_info->count == 0) {
771 g_free(bonded_devices_req_info->addr_list);
772 g_free(bonded_devices_req_info);
773 req_info->user_data = NULL;
780 BT_ERR("Unknown service function[%d]", service_function);
783 _bt_service_method_return(req_info->context, out_param, req_info->result);
784 g_array_free(out_param, TRUE);
785 /* Now free invocation info for this request*/
786 _bt_free_info_from_invocation_list(req_info);
790 /* Request return handlings */
791 static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
793 BT_INFO("__bt_adapter_post_set_enabled>>");
794 /*TODO Get All properties */
795 /* Add Adapter enabled post processing codes */
799 static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
801 BT_INFO("_bt_adapter_post_set_disabled>>");
802 /* Add Adapter disabled post processing codes */
806 static void __bt_adapter_update_bt_enabled(void)
808 int result = BLUETOOTH_ERROR_NONE;
809 BT_INFO("_bt_adapter_update_bt_enabled>>");
810 /* Update Bluetooth Status to notify other modules */
811 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
812 BT_ERR("Set vconf failed\n");
814 /* TODO:Add timer function to handle any further post processing */
815 g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
817 /*Return BT_ADAPTER_ENABLE Method invocation context */
818 __bt_adapter_handle_pending_requests(BT_ENABLE_ADAPTER, NULL, 0);
819 /*Send BT Enabled event to application */
820 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
821 g_variant_new("(i)", result));
824 static void __bt_adapter_update_bt_disabled(void)
826 int result = BLUETOOTH_ERROR_NONE;
827 BT_INFO("_bt_adapter_update_bt_disabled>>");
829 int power_off_status = 0;
832 /* Update the vconf BT status in normal Deactivation case only */
833 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
834 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
836 /* TODO:Add timer function to handle any further post processing */
837 g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
839 /* Return BT_ADAPTER_DISABLE Method invocation context */
840 __bt_adapter_handle_pending_requests(BT_DISABLE_ADAPTER, NULL, 0);
842 /* Send BT Disabled event to application */
843 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
844 g_variant_new("(i)", result));
848 static void __bt_adapter_state_set_status(bt_status_t status)
850 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
851 adapter_state = status;
854 static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t status)
856 BT_INFO("adapter_discovery_status changed [%d] -> [%d]", adapter_discovery_state, status);
857 adapter_discovery_state = status;
860 static void __bt_adapter_state_change_callback(int bt_status)
862 BT_INFO("__bt_adapter_state_change_callback: status [%d]", bt_status);
866 __bt_adapter_state_set_status(bt_status);
868 /* Adapter is disabled, unregister event handlers */
869 _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
870 //_bt_deinit_device_event_handler();
872 /* Add Adapter disabled post processing codes */
873 __bt_adapter_update_bt_disabled();
876 __bt_adapter_state_set_status(bt_status);
877 /* Add Adapter enabled post processing codes */
879 BT_DBG("g_source is removed");
880 g_source_remove(timer_id);
883 __bt_adapter_update_bt_enabled();
886 BT_ERR("Incorrect Bluetooth adapter state changed status");
891 static int __bt_adapter_state_handle_request(gboolean enable)
893 int result = BLUETOOTH_ERROR_NONE;
896 switch (adapter_state) {
899 BT_INFO("Adapter is currently in activating state, state [%d]",
902 return BLUETOOTH_ERROR_IN_PROGRESS;
904 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
905 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
906 /*TODO Stop Discovery*/
907 if (result != OAL_STATUS_SUCCESS)
908 BT_ERR("Discover stop failed: %d", result);
909 __bt_adapter_update_discovery_status(FALSE);
911 result = adapter_disable();
912 if (result != OAL_STATUS_SUCCESS) {
913 BT_ERR("adapter_enable failed: [%d]", result);
914 result = BLUETOOTH_ERROR_INTERNAL;
915 /*TODO: perform if anything more needs to be done to handle failure */
917 /* TODO: To be handled */
918 __bt_adapter_state_set_status(BT_DEACTIVATING);
919 result = BLUETOOTH_ERROR_NONE;
926 BT_INFO("Adapter is currently in activated state, state [%d]",
929 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
931 if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
932 adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
933 /*TODO Stop Discovery*/
934 if (result != OAL_STATUS_SUCCESS)
935 BT_ERR("Discover stop failed: %d", result);
936 __bt_adapter_update_discovery_status(FALSE);
938 result = adapter_disable();
939 if (result != OAL_STATUS_SUCCESS) {
940 BT_ERR("adapter_enable failed: [%d]", result);
941 result = BLUETOOTH_ERROR_INTERNAL;
942 /*TODO: perform if anything more needs to be done to handle failure */
944 /* TODO: To be handled */
945 __bt_adapter_state_set_status(BT_DEACTIVATING);
946 result = BLUETOOTH_ERROR_NONE;
951 case BT_DEACTIVATING:
953 BT_INFO("Adapter is currently in deactivating state, state [%d]",
956 return BLUETOOTH_ERROR_IN_PROGRESS;
959 result = adapter_enable();
960 if (result != OAL_STATUS_SUCCESS) {
961 BT_ERR("adapter_enable failed: [%d]", result);
963 result = BLUETOOTH_ERROR_INTERNAL;
964 /*TODO: perform if anything more needs to be done to handle failure */
966 /* TODO: To be handled */
967 __bt_adapter_state_set_status(BT_ACTIVATING);
968 result = BLUETOOTH_ERROR_NONE;
975 BT_INFO("Adapter is currently in deactivated state, state [%d]",
978 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
980 result = adapter_enable();
981 if (result != OAL_STATUS_SUCCESS) {
982 BT_ERR("adapter_enable failed: [%d]", result);
984 result = BLUETOOTH_ERROR_INTERNAL;
985 /*TODO: perform if anything more needs to be done to handle failure */
987 /* TODO: To be handled */
988 __bt_adapter_state_set_status(BT_ACTIVATING);
989 result = BLUETOOTH_ERROR_NONE;
995 if (enable && result == BLUETOOTH_ERROR_NONE) {
996 /* Adapter enable request is successful, setup event handlers */
997 _bt_service_register_event_handler_callback(
998 BT_ADAPTER_MODULE, __bt_adapter_event_handler);
999 _bt_device_state_handle_callback_set_request();
1004 static int __bt_adapter_state_discovery_request(gboolean enable)
1006 int result = BLUETOOTH_ERROR_NONE;
1009 switch (adapter_discovery_state) {
1010 case ADAPTER_DISCOVERY_STARTED: {
1011 BT_INFO("Adapter is currently in discovery started state, state [%d]",
1012 adapter_discovery_state);
1014 return BLUETOOTH_ERROR_IN_PROGRESS;
1016 result = adapter_stop_inquiry();
1017 if (result != OAL_STATUS_SUCCESS) {
1018 BT_ERR("Discover stop failed: %d", result);
1019 result = BLUETOOTH_ERROR_INTERNAL;
1021 BT_ERR("Stop Discovery Triggered successfully");
1022 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1023 result = BLUETOOTH_ERROR_NONE;
1028 case ADAPTER_DISCOVERY_STARTING: {
1029 BT_INFO("Adapter is currently in discovery starting state, state [%d]",
1030 adapter_discovery_state);
1032 return BLUETOOTH_ERROR_IN_PROGRESS;
1034 result = adapter_stop_inquiry();
1035 if (result != OAL_STATUS_SUCCESS) {
1036 BT_ERR("Discover stop failed: %d", result);
1037 result = BLUETOOTH_ERROR_INTERNAL;
1039 BT_ERR("Stop Discovery Triggered successfully");
1040 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
1041 result = BLUETOOTH_ERROR_NONE;
1046 case ADAPTER_DISCOVERY_STOPPED: {
1047 BT_INFO("Adapter is currently in discovery stopped state, state [%d]",
1048 adapter_discovery_state);
1050 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1052 result = adapter_start_inquiry();
1053 if (result != OAL_STATUS_SUCCESS) {
1054 BT_ERR("Start Discovery failed: %d", result);
1055 result = BLUETOOTH_ERROR_INTERNAL;
1057 BT_ERR("Start Discovery Triggered successfully");
1058 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1059 result = BLUETOOTH_ERROR_NONE;
1064 case ADAPTER_DISCOVERY_STOPPING: {
1065 BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
1066 adapter_discovery_state);
1068 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1070 result = adapter_start_inquiry();
1071 if (result != OAL_STATUS_SUCCESS) {
1072 BT_ERR("Start Discovery failed: %d", result);
1073 result = BLUETOOTH_ERROR_INTERNAL;
1075 BT_ERR("Start Discovery Triggered successfully");
1076 __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
1077 result = BLUETOOTH_ERROR_NONE;
1088 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status)
1090 BT_INFO("__bt_adapter_discovery_state_change_callback: status [%d]", bt_discovery_status);
1091 GVariant *param = NULL;
1092 int result = BLUETOOTH_ERROR_NONE;
1094 switch (bt_discovery_status) {
1095 case ADAPTER_DISCOVERY_STOPPED:
1097 __bt_adapter_update_discovery_status(bt_discovery_status);
1098 param = g_variant_new("(i)", result);
1099 _bt_send_event(BT_ADAPTER_EVENT,
1100 BLUETOOTH_EVENT_DISCOVERY_FINISHED,
1104 case ADAPTER_DISCOVERY_STARTED:
1106 __bt_adapter_update_discovery_status(bt_discovery_status);
1107 param = g_variant_new("(i)", result);
1108 _bt_send_event(BT_ADAPTER_EVENT,
1109 BLUETOOTH_EVENT_DISCOVERY_STARTED,
1114 BT_ERR("Incorrect Bluetooth adapter Discovery state changed status");