Code optimize
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-device.c
index 206af08..98da9f8 100644 (file)
@@ -455,7 +455,7 @@ bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
                        g_variant_unref(tmp_value);
                }
 
-               tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
+               tmp_value = g_variant_lookup_value(value, "LegacyManufacturerDataLen", G_VARIANT_TYPE_UINT16);
                if (tmp_value) {
                        dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
                        if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
@@ -466,7 +466,7 @@ bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
                } else
                        dev_info->manufacturer_data_len = 0;
 
-               tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
+               tmp_value = g_variant_lookup_value(value, "LegacyManufacturerData", G_VARIANT_TYPE_ARRAY);
                if (tmp_value) {
                        if ((dev_info->manufacturer_data_len == 0) ||
                                        dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
@@ -558,12 +558,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) {
@@ -1141,12 +1135,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;
 
@@ -1582,7 +1570,6 @@ int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
        GDBusProxy *device_proxy;
        GError *error = NULL;
        GVariant *value;
-       GVariant *tmp_value;
        GDBusConnection *conn;
        GVariant *result = NULL;
        int ret = BLUETOOTH_ERROR_NONE;
@@ -1603,8 +1590,9 @@ int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
        g_free(object_path);
        retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
-                       g_variant_new("(s)", BT_DEVICE_INTERFACE),
+       result = g_dbus_proxy_call_sync(device_proxy, "Get",
+                       g_variant_new("(ss)", BT_DEVICE_INTERFACE,
+                               "GattConnected"),
                        G_DBUS_CALL_FLAGS_NONE,
                        -1,
                        NULL,
@@ -1618,21 +1606,12 @@ int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       g_variant_get(result , "(@a{sv})", &value);
+       g_variant_get(result, "(v)", &value);
+       *is_connected = g_variant_get_boolean(value);
        g_variant_unref(result);
 
-       tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
-       if (tmp_value == NULL) {
-               g_object_unref(device_proxy);
-               g_variant_unref(value);
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
-       *is_connected = g_variant_get_boolean(tmp_value);
-
        BT_DBG("gatt is connected : %d", *is_connected);
 
-       g_variant_unref(tmp_value);
        g_variant_unref(value);
        g_object_unref(device_proxy);
 
@@ -1644,6 +1623,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 +1636,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,11 +1705,10 @@ 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);
@@ -1778,7 +1761,6 @@ int _bt_get_connected_link(bluetooth_device_address_t *device_address,
        GDBusProxy *device_proxy;
        GError *error = NULL;
        GDBusConnection *conn;
-       GVariant *tmp_value = NULL;
        GVariant *value = NULL;
        GVariant *result = NULL;
 
@@ -1801,33 +1783,24 @@ int _bt_get_connected_link(bluetooth_device_address_t *device_address,
                return BLUETOOTH_ERROR_NONE;
        }
 
-       result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
-                                       g_variant_new("(s)", BT_DEVICE_INTERFACE),
-                                       G_DBUS_CALL_FLAGS_NONE,
-                                       -1,
-                                       NULL,
-                                       &error);
+       result = g_dbus_proxy_call_sync(device_proxy, "Get",
+                       g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Connected"),
+                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
 
        if (error != NULL) {
                BT_ERR("Error occured in Proxy call [%s]\n", error->message);
                g_error_free(error);
                g_object_unref(device_proxy);
                return BLUETOOTH_ERROR_INTERNAL;
+       } else {
+               g_variant_get(result, "(v)", &value);
+               *connected = g_variant_get_byte(value);
+               g_variant_unref(value);
+               g_variant_unref(result);
        }
 
-       g_variant_get(result , "(@a{sv})", &value);
-       g_variant_unref(result);
-
-       tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
-       if (tmp_value != NULL) {
-               *connected = g_variant_get_byte(tmp_value);
-               g_variant_unref(tmp_value);
-               g_object_unref(device_proxy);
-               return BLUETOOTH_ERROR_NONE;
-       } else  {
-               BT_ERR("g_variant value is NULL");
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
+       g_object_unref(device_proxy);
+       return BLUETOOTH_ERROR_NONE;
 }
 
 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
@@ -1895,6 +1868,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 +1896,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 +1903,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 +1920,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 +1982,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 +2012,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 +2019,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 +2035,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;
 }
 
@@ -2236,47 +2205,6 @@ int _bt_connect_profile(char *address, char *uuid,
        return BLUETOOTH_ERROR_NONE;
 }
 
-int _bt_disconnect_all(char *address)
-{
-       int ret = BLUETOOTH_ERROR_NONE;
-       char *object_path;
-       GDBusProxy *proxy;
-       GDBusConnection *conn;
-       GVariant *result = NULL;
-       GError *err = NULL;
-
-       BT_DBG("");
-       conn = _bt_gdbus_get_system_gconn();
-       retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       object_path = _bt_get_device_object_path(address);
-       retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
-                                       NULL, BT_BLUEZ_NAME,
-                                       object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
-       g_free(object_path);
-       retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       result = g_dbus_proxy_call_sync(proxy, "Disconnect",
-               NULL,
-               G_DBUS_CALL_FLAGS_NONE,
-               -1, NULL,
-               &err);
-
-       if (err != NULL) {
-               BT_ERR("Dbus Call Error:[%s]", err->message);
-               g_error_free(err);
-               ret = BLUETOOTH_ERROR_INTERNAL;
-       }
-
-       g_object_unref(proxy);
-       if (result)
-               g_variant_unref(result);
-
-       return ret;
-}
-
 int _bt_disconnect_profile(char *address, char *uuid,
                                                void *cb, gpointer func_data)
 {
@@ -3128,10 +3056,8 @@ int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
        GDBusProxy *device_proxy;
        GError *error = NULL;
        GVariant *value;
-       GVariant *tmp_value;
        GDBusConnection *conn;
        GVariant *result = NULL;
-       int ret = BLUETOOTH_ERROR_NONE;
 
        BT_CHECK_PARAMETER(device_address, return);
 
@@ -3149,40 +3075,24 @@ int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
        g_free(object_path);
        retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
-                       g_variant_new("(s)", BT_DEVICE_INTERFACE),
-                       G_DBUS_CALL_FLAGS_NONE,
-                       -1,
-                       NULL,
-                       &error);
-       if (result == NULL) {
-               if (error != NULL) {
-                       BT_ERR("Error occured in Proxy call [%s]\n", error->message);
-                       g_error_free(error);
-               }
+       result = g_dbus_proxy_call_sync(device_proxy, "Get",
+                       g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
+                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
+       if (error) {
+               ERR("DBus Error : %s", error->message);
+               g_clear_error(&error);
                g_object_unref(device_proxy);
                return BLUETOOTH_ERROR_INTERNAL;
-       }
-
-       g_variant_get(result , "(@a{sv})", &value);
-       g_variant_unref(result);
-
-       tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
-       if (tmp_value == NULL) {
-               g_object_unref(device_proxy);
+       } else {
+               g_variant_get(result, "(v)", &value);
+               *mtu = g_variant_get_uint16(value);
+               BT_DBG("ATT MTU : %d", *mtu);
                g_variant_unref(value);
-               return BLUETOOTH_ERROR_INTERNAL;
+               g_variant_unref(result);
        }
 
-       *mtu = g_variant_get_uint16(tmp_value);
-
-       BT_DBG("ATT MTU : %d", *mtu);
-
-       g_variant_unref(tmp_value);
-       g_variant_unref(value);
        g_object_unref(device_proxy);
-
-       return ret;
+       return BLUETOOTH_ERROR_NONE;
 }
 
 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
@@ -3226,7 +3136,7 @@ int _bt_get_device_ida(bluetooth_device_address_t *device_address,
        if (result == NULL) {
                BT_ERR("Failed to get device ID address");
                if (error != NULL) {
-                       BT_ERR("Error occured in Proxy call [%s]\n", error->message);
+                       BT_ERR("Error occured in Proxy call [%s]", error->message);
                        g_error_free(error);
                }
                g_object_unref(device_proxy);