[FRWK] Implement 'Reset Adapter' API
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / adapter / bt-service-core-adapter.c
index 0714f98..3958cd9 100644 (file)
@@ -44,7 +44,7 @@
 #include "bt-service-core-adapter-le.h"
 #include "bt-service-gatt.h"
 
-#ifdef TIZEN_DPM_ENABLE
+#ifdef TIZEN_FEATURE_BT_DPM
 #include "bt-service-dpm.h"
 #endif
 #include "bt-service-hidhost.h"
@@ -97,7 +97,8 @@ static void __bt_adapter_update_discovery_status(bt_adapter_discovery_state_t st
 static void __bt_adapter_state_change_callback(int bt_status);
 static int __bt_adapter_state_handle_request(gboolean enable);
 static int __bt_adapter_state_discovery_request(gboolean enable,
-               unsigned short max_response, unsigned short duration, unsigned int mask);
+               unsigned short max_response, unsigned short duration, unsigned int mask,
+               gboolean is_custom, bt_discovery_role_type_t role);
 static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status);
 static gboolean __bt_is_service_request_present(int service_function);
 
@@ -135,15 +136,32 @@ int _bt_enable_adapter(void)
 int _bt_enable_core(void)
 {
        /* TODO_40 : 4.0 merge  */
+       BT_INFO("Not Supported");
        return BLUETOOTH_ERROR_NOT_SUPPORT;
 }
 
 int _bt_recover_adapter(void)
 {
        /* TODO_40 : 4.0 merge  */
+       BT_INFO("Not Supported");
        return BLUETOOTH_ERROR_NOT_SUPPORT;
 }
 
+int _bt_reset_adapter(void)
+{
+       BT_INFO("+");
+       if (OAL_STATUS_SUCCESS != adapter_reset())
+               return BLUETOOTH_ERROR_INTERNAL;
+
+       /* TODO_40 : 4.0 merge  */
+       /* TODO Currently bt-service is not terminated in tizen next.
+           It should be handled in future patch */
+       if (_bt_adapter_get_status() == BT_DEACTIVATED)
+               g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
 int _bt_disable_adapter(void)
 {
        return __bt_adapter_state_handle_request(FALSE);
@@ -152,12 +170,18 @@ int _bt_disable_adapter(void)
 int _bt_start_discovery(unsigned short max_response,
                unsigned short duration, unsigned int cod_mask)
 {
-       return __bt_adapter_state_discovery_request(TRUE, max_response, duration, cod_mask);
+       return __bt_adapter_state_discovery_request(TRUE, max_response, duration,
+                               cod_mask, FALSE, 0x00);
+}
+
+int _bt_start_custom_discovery(bt_discovery_role_type_t role)
+{
+       return __bt_adapter_state_discovery_request(TRUE, 0, 0, 0, TRUE, role);
 }
 
 int _bt_cancel_discovery(void)
 {
-       return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0);
+       return __bt_adapter_state_discovery_request(FALSE, 0, 0, 0, FALSE, 0x00);
 }
 
 gboolean _bt_is_discovering(void)
@@ -377,17 +401,21 @@ static void __bt_visibility_alarm_create()
 static int __bt_set_visible_time(int timeout)
 {
        int result;
+#ifdef TIZEN_FEATURE_BT_DPM
+       int discoverable_state = DPM_BT_ERROR;
+#endif
 
        __bt_visibility_alarm_remove();
 
        visible_timer.timeout = timeout;
 
-#ifdef TIZEN_DPM_ENABLE
-       if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
+#ifdef TIZEN_FEATURE_BT_DPM
+       _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
+       if (discoverable_state != DPM_RESTRICTED) {
 #endif
                if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
                        BT_ERR("Set vconf failed");
-#ifdef TIZEN_DPM_ENABLE
+#ifdef TIZEN_FEATURE_BT_DPM
        }
 #endif
 
@@ -420,20 +448,24 @@ static int __bt_set_visible_time(int timeout)
 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
 {
        int result;
+#ifdef TIZEN_FEATURE_BT_DPM
+       int discoverable_state = DPM_BT_ERROR;
+#endif
 
        BT_DBG("+");
 
        BT_INFO("discoverable_mode: %d, timeout: %d", discoverable_mode, timeout);
 
-#ifdef TIZEN_DPM_ENABLE
+#ifdef TIZEN_FEATURE_BT_DPM
+       _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
        if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
-                       _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
+                discoverable_state == DPM_RESTRICTED) {
                if (headed_plugin_info->plugin_headed_enabled)
                        headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
                return BLUETOOTH_ERROR_ACCESS_DENIED;
        }
        if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
-                       _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
+               discoverable_state == DPM_RESTRICTED) {
                if (headed_plugin_info->plugin_headed_enabled)
                        headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
                return BLUETOOTH_ERROR_ACCESS_DENIED;
@@ -544,6 +576,114 @@ int _bt_adapter_get_bonded_devices(void)
        return result;
 }
 
+int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
+{
+       BT_DBG("+");
+       GDBusConnection *conn;
+       GDBusProxy *manager_proxy;
+       GVariant *result = NULL;
+       GVariant *result1 = NULL;
+       GVariantIter *iter = NULL;
+       GError *error = NULL;
+       char *object_path = NULL;
+       GVariantIter *interface_iter;
+       char *interface_str = NULL;
+       GDBusProxy *device_proxy = NULL;
+       gboolean is_connected = FALSE;
+
+       conn = _bt_gdbus_get_system_gconn();
+       retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       manager_proxy = _bt_get_manager_proxy();
+       retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
+                       NULL,
+                       G_DBUS_CALL_FLAGS_NONE,
+                       -1,
+                       NULL,
+                       NULL);
+
+       if (!result) {
+               if (error != NULL) {
+                       BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
+                       g_clear_error(&error);
+                       error = NULL;
+               } else
+                       BT_ERR("Failed to Failed to GetManagedObjects");
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
+       g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
+
+       /* Parse the signature:  oa{sa{sv}}} */
+        while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
+                if (object_path == NULL)
+                        continue;
+
+                while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
+                                &interface_str, NULL)) {
+                       if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
+                               BT_DBG("Found a device: %s", object_path);
+                               g_free(interface_str);
+
+                               device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                                               NULL, BT_BLUEZ_NAME,
+                                               object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
+
+                               if (device_proxy == NULL) {
+                                       BT_DBG("Device don't have this service");
+                                       break;
+                               }
+
+                               result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
+                                               g_variant_new("(s)", profile_uuid),
+                                               G_DBUS_CALL_FLAGS_NONE,
+                                               -1,
+                                               NULL,
+                                               &error);
+
+                               if (result1 == NULL) {
+                                       BT_ERR("Error occured in Proxy call");
+                                       if (error) {
+                                               BT_ERR("Error occured in Proxy call [%s]\n", error->message);
+                                               g_error_free(error);
+                                               error = NULL;
+                                       }
+                                       g_object_unref(device_proxy);
+                                       break;
+                               }
+                               g_variant_get(result1, "(b)", &is_connected);
+
+                               if (is_connected == TRUE) {
+                                       char address[BT_ADDRESS_STRING_SIZE];
+                                       bluetooth_device_address_t *addr = NULL;
+
+                                       _bt_convert_device_path_to_address(object_path, address);
+
+                                       addr = g_malloc0(sizeof(bluetooth_device_address_t));
+                                       _bt_convert_addr_string_to_type(addr->addr, address);
+
+                                       g_array_append_vals(*addr_list, addr,
+                                                       sizeof(bluetooth_device_address_t));
+                               }
+
+                               g_variant_unref(result1);
+                               g_object_unref(device_proxy);
+
+                               break;
+                       }
+                }
+        }
+
+        g_variant_unref(result);
+        g_variant_iter_free(iter);
+
+        BT_DBG("-");
+        return BLUETOOTH_ERROR_NONE;
+}
+
 static void __bt_handle_pending_a2dp_init(service_uuid_t *service_list, unsigned int count)
 {
        int ret;
@@ -853,9 +993,11 @@ static void __bt_post_oal_init(void)
                BT_ERR("Fail to get the enabled value");
        }
 
+#if 0
        /* Update Bluetooth Status to OFF */
        if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
                BT_ERR("Set vconf failed\n");
+#endif
 
        if (status & VCONFKEY_BT_STATUS_ON) {
                ret = _bt_enable_adapter();
@@ -1285,7 +1427,9 @@ static int __bt_adapter_state_handle_request(gboolean enable)
 }
 
 static int __bt_adapter_state_discovery_request(gboolean enable,
-               unsigned short max_response, unsigned short duration, unsigned int mask)
+               unsigned short max_response, unsigned short duration,
+               unsigned int mask, gboolean is_custom,
+               bt_discovery_role_type_t role)
 {
        int result = BLUETOOTH_ERROR_NONE;
 
@@ -1334,7 +1478,12 @@ static int __bt_adapter_state_discovery_request(gboolean enable,
                        return BLUETOOTH_ERROR_NOT_IN_OPERATION;
                else {
                        BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
-                       result = adapter_start_inquiry(duration);
+
+                       if (!is_custom)
+                               result = adapter_start_inquiry(duration);
+                       else
+                               result = adapter_start_custom_inquiry(role);
+
                        if (result != OAL_STATUS_SUCCESS) {
                                BT_ERR("Start Discovery failed: %d", result);
                                result = BLUETOOTH_ERROR_INTERNAL;
@@ -1353,7 +1502,10 @@ static int __bt_adapter_state_discovery_request(gboolean enable,
                        return BLUETOOTH_ERROR_NOT_IN_OPERATION;
                else {
                        BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
-                       result = adapter_start_inquiry(duration);
+                       if (!is_custom)
+                               result = adapter_start_inquiry(duration);
+                       else
+                               result = adapter_start_custom_inquiry(role);
                        if (result != OAL_STATUS_SUCCESS) {
                                BT_ERR("Start Discovery failed: %d", result);
                                result = BLUETOOTH_ERROR_INTERNAL;
@@ -1407,13 +1559,16 @@ static void __bt_adapter_discovery_state_change_callback(int bt_discovery_status
 static void __bt_set_visible_mode(void)
 {
        int timeout = 0;
+#ifdef TIZEN_FEATURE_BT_DPM
+       int discoverable_state = DPM_BT_ERROR;
+#endif
 
        if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
                BT_ERR("Fail to get the timeout value");
 
-#ifdef TIZEN_DPM_ENABLE
-       if (timeout == -1 ||
-                       _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
+#ifdef TIZEN_FEATURE_BT_DPM
+       _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
+       if (timeout == -1 || discoverable_state == DPM_RESTRICTED) {
                if (_bt_set_discoverable_mode(
                                        BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
                                        timeout) != BLUETOOTH_ERROR_NONE) {
@@ -1464,7 +1619,55 @@ static void __bt_set_local_name(void)
        free(phone_name);
 }
 
+void _bt_adapter_set_status(bt_status_t status)
+{
+       BT_INFO("adapter_status changed [%d] -> [%d]", adapter_state, status);
+       adapter_state = status;
+}
+
 bt_status_t _bt_adapter_get_status(void)
 {
        return adapter_state;
 }
+
+void _bt_set_disabled(int result)
+{
+       int power_off_status = 0;
+       int ret;
+       int ret_pm_ignore;
+       int pm_ignore_mode = 0;
+
+       ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
+       BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
+
+       ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
+
+       /* Update the vconf BT status in normal Deactivation case only */
+       if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
+               ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
+
+               BT_DBG("Update vconf for BT normal Deactivation");
+
+               if (result == BLUETOOTH_ERROR_TIMEOUT)
+                       if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
+                               BT_ERR("Set vconf failed");
+
+               /* Update Bluetooth Status to notify other modules */
+               if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
+                       BT_ERR("Set vconf failed");
+
+               if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
+                                                       EVT_VAL_BT_OFF) != ES_R_OK)
+                       BT_ERR("Fail to set value");
+       }
+
+       if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
+               BT_ERR("Set vconf failed\n");
+
+       _bt_cancel_queued_transfers();
+       _bt_adapter_set_status(BT_DEACTIVATED);
+       __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);
+
+       BT_INFO("Adapter disabled");
+}
+