[FRWK] Implement 'Reset Adapter' API
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / adapter / bt-service-core-adapter.c
index f146ab3..3958cd9 100644 (file)
@@ -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)
@@ -552,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;
@@ -861,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();
@@ -1293,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;
 
@@ -1342,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;
@@ -1361,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;