Merge "Add the plugin for Headed/Headless device" into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-device.c
index 206af08..896a228 100644 (file)
@@ -19,7 +19,6 @@
 #include <gio/gio.h>
 #include <dlog.h>
 #include <string.h>
-#include <syspopup_caller.h>
 #include <vconf.h>
 #include <bundle_internal.h>
 
@@ -37,6 +36,7 @@
 #include "bt-service-network.h"
 #include "bt-service-adapter.h"
 #include "bt-service-gap-agent.h"
+#include "bt-service-pbap.h"
 
 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
@@ -191,63 +191,6 @@ done:
        BT_DBG("-");
 } */
 
-#ifdef TIZEN_PROFILE_WEARABLE
-static gboolean __bt_syspopup_timer_cb(gpointer user_data)
-{
-       int ret;
-       bundle *b;
-       retv_if(user_data == NULL, FALSE);
-
-       b = (bundle *)user_data;
-
-       ret = syspopup_launch("bt-syspopup", b);
-       if (ret < 0) {
-               BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
-       } else {
-               BT_DBG("Hurray!!! Finally Popup launched");
-               bundle_free(b);
-       }
-       return (ret < 0) ? TRUE : FALSE;
-}
-
-static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
-{
-       BT_DBG("+");
-       int ret = 0;
-       bundle *b = NULL;
-       GDBusConnection *conn;
-
-       conn = _bt_gdbus_get_system_gconn();
-       if (conn == NULL)
-               return FALSE;
-
-       b = bundle_create();
-       if (b == NULL)
-               return FALSE;
-
-       bundle_add(b, "event-type", "unable-to-pairing");
-
-       if (result == BLUETOOTH_ERROR_TIMEOUT)
-               bundle_add(b, "error", "timeout");
-       else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
-               bundle_add(b, "error", "authfailed");
-       else
-               bundle_add(b, "error", "error");
-
-       ret = syspopup_launch("bt-syspopup", b);
-       if (0 > ret) {
-               BT_ERR("Popup launch failed...retry %d \n", ret);
-               g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
-                               b);
-       } else {
-               bundle_free(b);
-       }
-
-       BT_DBG("-");
-       return TRUE;
-}
-#endif
-
 gboolean _bt_is_device_creating(void)
 {
        return is_device_creating;
@@ -316,8 +259,12 @@ static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
        ret_if(value == NULL);
        ret_if(info == NULL);
 
-       info->uuid_count = g_variant_get_size(value);
-       info->uuids = g_variant_dup_strv(value, &info->uuid_count);
+       gsize uuid_count = 0;
+
+       info->uuids = g_variant_dup_strv(value, &uuid_count);
+       info->uuid_count = (unsigned int)uuid_count;
+
+       BT_DBG("uuid count : %d", uuid_count);
 }
 
 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
@@ -558,12 +505,6 @@ static gboolean __ignore_auto_pairing_request(const char *address)
        }
 
        buffer = g_malloc0(sizeof(char) * size);
-       /* Fix : NULL_RETURNS */
-       if (buffer == NULL) {
-               BT_ERR("Memory allocation error\n");
-               fclose(fp);
-               return FALSE;
-       }
        result = fread((char *)buffer, 1, size, fp);
        fclose(fp);
        if (result != size) {
@@ -718,7 +659,8 @@ static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
        GVariant *param;
 
        /* Terminate ALL system popup */
-       syspopup_destroy_all();
+       if (headed_plugin_info->plugin_headed_enabled)
+               headed_plugin_info->headed_plugin->bt_destroy_popup_all();
 
        reply = g_dbus_proxy_call_finish(proxy, res, &err);
        if (reply)
@@ -799,17 +741,19 @@ static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                        result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
                        result == BLUETOOTH_ERROR_TIMEOUT ||
                        result == BLUETOOTH_ERROR_HOST_DOWN) {
-#ifdef TIZEN_PROFILE_WEARABLE
-               int is_sw_running = 0;
+               if (TIZEN_PROFILE_WEARABLE) {
+                       int is_sw_running = 0;
 
-               if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
-                       BT_ERR("vconf_get_int for setup wizard state failed");
+                       if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
+                               BT_ERR("vconf_get_int for setup wizard state failed");
 
-               if (!is_sw_running)
-                       __bt_launch_unable_to_pairing_syspopup(result);
-               else
-                       BT_ERR("Unable to pair");
-#endif
+                       if (!is_sw_running) {
+                               if (headed_plugin_info->plugin_headed_enabled)
+                                       headed_plugin_info->headed_plugin->bt_launch_unable_to_pairing_syspopup(result);
+                       } else {
+                               BT_ERR("Unable to pair");
+                       }
+               }
                bonding_info->result = result;
        }
 
@@ -1141,12 +1085,6 @@ int _bt_unbond_device(int request_id,
 
        /* allocate user data so that it can be retrieved in callback */
        unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
-       /* Fix : NULL_RETURNS */
-       if (unbonding_info == NULL) {
-               BT_ERR("Memory not allocated !");
-               return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
-       }
-
        unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
        unbonding_info->req_id = request_id;
 
@@ -1644,6 +1582,7 @@ int _bt_is_device_connected(bluetooth_device_address_t *device_address,
 {
        char *object_path = NULL;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+       char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
        GDBusProxy *device_proxy = NULL;
        GDBusProxy *adapter_proxy = NULL;
        GDBusConnection *conn;
@@ -1656,8 +1595,12 @@ int _bt_is_device_connected(bluetooth_device_address_t *device_address,
        retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
        retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
 
+       _bt_convert_addr_type_to_string(address, device_address->addr);
+       _bt_convert_addr_string_to_secure_string(secure_address, address);
+
        *is_connected = FALSE;
-       BT_DBG("connection_type: %d", connection_type);
+       BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
+
        if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
                return _bt_rfcomm_is_device_connected(device_address,
                                                is_connected);
@@ -1721,15 +1664,18 @@ int _bt_is_device_connected(bluetooth_device_address_t *device_address,
        } else {
                uuid = _bt_get_profile_uuid128(connection_type);
                if (uuid == NULL) {
-                       BT_ERR("uuid is NULL");
+                       BT_ERR("connection_type: %d, uuid is NULL", connection_type);
                        return BLUETOOTH_ERROR_INTERNAL;
                }
-
-               BT_DBG("uuid: %s", uuid);
+               BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
 
                object_path = _bt_get_device_object_path(address);
-               retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
-               BT_DBG("object_path: %s", object_path);
+               if (!object_path) {
+                       BT_ERR("object_path is NULL");
+                       g_free(uuid);
+                       return BLUETOOTH_ERROR_NOT_PAIRED;
+               }
+
                device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
                                                                        NULL, BT_BLUEZ_NAME,
                                                                        object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
@@ -1895,6 +1841,7 @@ static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_ad
        gboolean auto_connect)
 {
        char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
+       char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
        gchar *device_path = NULL;
        GDBusProxy *device_proxy = NULL;
        GDBusConnection *conn;
@@ -1922,12 +1869,6 @@ static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_ad
        retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        func_data = g_malloc0(sizeof(bt_function_data_t));
-       if (func_data == NULL) {
-               BT_ERR("Unable to allocate memory for address");
-               ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
-               goto fail;
-       }
-
        func_data->address = g_strdup(device_address);
        if (func_data->address == NULL) {
                BT_ERR("Unable to allocate memory for address");
@@ -1935,6 +1876,9 @@ static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_ad
                goto fail;
        }
 
+       _bt_convert_addr_string_to_secure_string(secure_address, device_address);
+       BT_INFO("Connect LE [%s]", secure_address);
+
        func_data->req_id = req_id;
 
        g_dbus_proxy_call(device_proxy, "ConnectLE",
@@ -1949,10 +1893,10 @@ static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_ad
 fail:
        if (device_proxy)
                g_object_unref(device_proxy);
-       if (func_data) {
-               g_free(func_data->address);
-               g_free(func_data);
-       }
+
+       g_free(func_data->address);
+       g_free(func_data);
+
        return ret;
 }
 
@@ -2011,6 +1955,7 @@ int _bt_disconnect_le_device(int req_id,
                const bluetooth_device_address_t *bd_addr)
 {
        char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
+       char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
        gchar *device_path;
        GDBusProxy *device_proxy;
        GDBusConnection *conn;
@@ -2040,12 +1985,6 @@ int _bt_disconnect_le_device(int req_id,
        retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        func_data = g_malloc0(sizeof(bt_function_data_t));
-       if (func_data == NULL) {
-               BT_ERR("Unable to allocate memory for address");
-               ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
-               goto fail;
-       }
-
        func_data->address = g_strdup(device_address);
        if (func_data->address == NULL) {
                BT_ERR("Unable to allocate memory for address");
@@ -2053,6 +1992,9 @@ int _bt_disconnect_le_device(int req_id,
                goto fail;
        }
 
+       _bt_convert_addr_string_to_secure_string(secure_address, device_address);
+       BT_INFO("Disconnect LE [%s]", secure_address);
+
        func_data->req_id = req_id;
 
        g_dbus_proxy_call(device_proxy, "DisconnectLE",
@@ -2066,10 +2008,10 @@ int _bt_disconnect_le_device(int req_id,
 fail:
        if (device_proxy)
                g_object_unref(device_proxy);
-       if (func_data) {
-               g_free(func_data->address);
-               g_free(func_data);
-       }
+
+       g_free(func_data->address);
+       g_free(func_data);
+
        return ret;
 }
 
@@ -2121,9 +2063,18 @@ int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
                g_object_unref(device_proxy);
                return BLUETOOTH_ERROR_INTERNAL;
        }
-
        g_object_unref(device_proxy);
 
+       /* IPSP daemon launch */
+       GDBusProxy *ipsp_proxy;
+
+       ipsp_proxy = _bt_get_ipsp_proxy();
+       retv_if(ipsp_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       g_dbus_proxy_call(ipsp_proxy, "EnableIpsp",
+                       NULL, G_DBUS_CALL_FLAGS_NONE,
+                       -1, NULL, NULL, NULL);
+
        return ret;
 }
 
@@ -3097,12 +3048,6 @@ int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_addre
        g_free(device_path);
        retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       if (device_path == NULL) {
-               BT_DBG("device_path NULL");
-               ret = BLUETOOTH_ERROR_INTERNAL;
-               goto fail;
-       }
-
        att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
        att_mtu_req_info->addr = (char *)g_strdup(address);
        att_mtu_req_info->req_id = request_id;
@@ -3115,7 +3060,7 @@ int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_addre
                                NULL,
                                (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
                                NULL);
-fail:
+
        return ret;
 }