GATT-Clinet: Fix the Write and read dbus reply parameter mismatch.
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-gatt-client.c
index 71e85c5..872b21f 100644 (file)
@@ -884,6 +884,7 @@ static void __bluetooth_internal_read_cb(GObject *source_object,
        GByteArray *gp_byte_array = NULL;
        GVariantIter *iter;
        guint8 g_byte;
+       gint att_error_code;
        int ret = BLUETOOTH_ERROR_NONE;
 
        BT_DBG("+");
@@ -910,9 +911,22 @@ static void __bluetooth_internal_read_cb(GObject *source_object,
        }
 
        char_value.char_handle = user_data;
-       gp_byte_array = g_byte_array_new();
-       g_variant_get(value, "(ay)", &iter);
 
+       g_variant_get(value, "(yay)", &att_error_code, &iter);
+
+       if (att_error_code != 0) {
+               if (user_info) {
+                       _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_CHAR,
+                               att_error_code, NULL,
+                               user_info->cb, user_info->user_data);
+               }
+               g_free(char_value.char_handle);
+               g_variant_unref(value);
+               g_variant_iter_free(iter);
+               return;
+       }
+
+       gp_byte_array = g_byte_array_new();
        while (g_variant_iter_loop(iter, "y", &g_byte))
                g_byte_array_append(gp_byte_array, &g_byte, 1);
 
@@ -939,6 +953,8 @@ BT_EXPORT_API int bluetooth_gatt_read_characteristic_value(const char *character
 {
        GDBusConnection *conn;
        char *handle;
+       GVariantBuilder *builder = NULL;
+       guint16 offset = 0;
 
        BT_CHECK_PARAMETER(characteristic, return);
        BT_CHECK_ENABLED(return);
@@ -948,67 +964,30 @@ BT_EXPORT_API int bluetooth_gatt_read_characteristic_value(const char *character
 
        handle = g_strdup(characteristic);
 
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+       /*offset*/
+       g_variant_builder_add(builder, "{sv}", "offset",
+               g_variant_new("q", offset));
+
+       /* Device Object path*/
+//     g_variant_builder_add(builder, "{sv}", "device",
+//     g_variant_new_object("o", NULL));
+
        g_dbus_connection_call(conn,
                        BT_BLUEZ_NAME,
                        characteristic,
                        GATT_CHAR_INTERFACE,
                        "ReadValue",
-                       NULL,
-                       G_VARIANT_TYPE("(ay)"),
+                       g_variant_new("(a{sv})", builder),
+                       G_VARIANT_TYPE("(yay)"),
                        G_DBUS_CALL_FLAGS_NONE,
                        -1,
                        NULL,
                        (GAsyncReadyCallback)__bluetooth_internal_read_cb,
                        (gpointer)handle);
-
-       return BLUETOOTH_ERROR_NONE;
-}
-
-BT_EXPORT_API int bluetooth_gatt_set_characteristics_value(
-               const char *char_handle, const guint8 *value, int length)
-{
-       GVariant *val;
-       GVariantBuilder *builder;
-       GError *error = NULL;
-       GDBusConnection *conn;
-       int i = 0;
-
-       BT_DBG("+");
-       BT_CHECK_PARAMETER(char_handle, return);
-       BT_CHECK_PARAMETER(value, return);
-       retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
-       BT_CHECK_ENABLED(return);
-
-       conn = _bt_gdbus_get_system_gconn();
-       retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
-
-       for (i = 0; i < length; i++)
-               g_variant_builder_add(builder, "y", value[i]);
-
-       val = g_variant_new("(ay)", builder);
-
-       g_dbus_connection_call_sync(conn,
-                       BT_BLUEZ_NAME,
-                       char_handle,
-                       GATT_CHAR_INTERFACE,
-                       "WriteValue",
-                       val,
-                       NULL,
-                       G_DBUS_CALL_FLAGS_NONE,
-                       -1, NULL, &error);
-
-       if (error) {
-               BT_ERR("Set value Failed: %s", error->message);
-               g_clear_error(&error);
-               g_variant_builder_unref(builder);
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
        g_variant_builder_unref(builder);
 
-       BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
 }
 
@@ -1054,12 +1033,78 @@ static void __bluetooth_internal_write_cb(GObject *source_object,
        return;
 }
 
+BT_EXPORT_API int bluetooth_gatt_set_characteristics_value(
+               const char *char_handle, const guint8 *value, int length)
+{
+       GVariant *val, *options;
+       GVariantBuilder *builder1;
+       GVariantBuilder *builder2;
+       GError *error = NULL;
+       GDBusConnection *conn;
+       int i = 0;
+       guint16 offset = 0;
+
+       BT_DBG("+");
+       BT_CHECK_PARAMETER(char_handle, return);
+       BT_CHECK_PARAMETER(value, return);
+       retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
+       BT_CHECK_ENABLED(return);
+
+       conn = _bt_gdbus_get_system_gconn();
+       retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
+       for (i = 0; i < length; i++)
+               g_variant_builder_add(builder1, "y", value[i]);
+
+       val = g_variant_new("ay", builder1);
+
+       builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+       /*offset*/
+       g_variant_builder_add(builder2, "{sv}", "offset",
+                               g_variant_new_uint16(offset));
+
+       /* Device Object path*/
+//     g_variant_builder_add(builder2, "{sv}", "device",
+//     g_variant_new_object("o", NULL));
+
+       options = g_variant_new("a{sv}", builder2);
+
+       g_dbus_connection_call(conn,
+                               BT_BLUEZ_NAME,
+                               char_handle,
+                               GATT_CHAR_INTERFACE,
+                               "WriteValue",
+                               g_variant_new("(@ay@a{sv})",
+                               val, options),
+                               G_VARIANT_TYPE("(y)"),
+                               G_DBUS_CALL_FLAGS_NONE,
+                               -1, NULL,
+                               (GAsyncReadyCallback)__bluetooth_internal_write_cb,
+                               NULL);
+
+
+       if (error) {
+               BT_ERR("Set value Failed: %s", error->message);
+               g_clear_error(&error);
+               g_variant_builder_unref(builder1);
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+       g_variant_builder_unref(builder1);
+       g_variant_builder_unref(builder2);
+
+       BT_DBG("-");
+       return BLUETOOTH_ERROR_NONE;
+}
+
 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value_by_type(
                const char *char_handle, const guint8 *value, int length, guint8 write_type)
 {
-       GVariant *val;
-       GVariantBuilder *builder;
+       GVariant *val, *options;
+       GVariantBuilder *builder1;
+       GVariantBuilder *builder2;
        GDBusConnection *conn;
+       guint16 offset = 0;
        int i = 0;
        int ret = BLUETOOTH_ERROR_NONE;
 
@@ -1071,34 +1116,50 @@ BT_EXPORT_API int bluetooth_gatt_set_characteristics_value_by_type(
        conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
+       builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
 
        for (i = 0; i < length; i++)
-               g_variant_builder_add(builder, "y", value[i]);
+               g_variant_builder_add(builder1, "y", value[i]);
+
+       val = g_variant_new("ay", builder1);
+
+       builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+       /*offset*/
+       g_variant_builder_add(builder2, "{sv}", "offset",
+                       g_variant_new_uint16(offset));
+
+       /* Device Object path*/
+//     g_variant_builder_add(builder2, "{sv}", "device",
+//     g_variant_new_object("o", NULL));
+
+       options = g_variant_new("a{sv}", builder2);
 
-       val = g_variant_new("ay", builder);
        g_dbus_connection_call(conn,
                        BT_BLUEZ_NAME,
                        char_handle,
                        GATT_CHAR_INTERFACE,
                        "WriteValuebyType",
-                       g_variant_new("(y@ay)", write_type, val),
+                       g_variant_new("(y@ay@a{sv})",
+                       write_type, val, options),
                        G_VARIANT_TYPE("(y)"),
                        G_DBUS_CALL_FLAGS_NONE,
                        -1, NULL,
                        (GAsyncReadyCallback)__bluetooth_internal_write_cb,
                        NULL);
 
-       g_variant_builder_unref(builder);
+       g_variant_builder_unref(builder1);
+       g_variant_builder_unref(builder2);
        return ret;
 }
 
 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value_request(
                        const char *char_handle, const guint8 *value, int length)
 {
-       GVariant *val;
+       GVariant *val, *options;
        GDBusConnection *conn;
-       GVariantBuilder *builder;
+       GVariantBuilder *builder1;
+       GVariantBuilder *builder2;
+       guint offset = 0;
        int i;
 
        BT_DBG("+");
@@ -1110,28 +1171,41 @@ BT_EXPORT_API int bluetooth_gatt_set_characteristics_value_request(
        conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
+       builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
 
        for (i = 0; i < length; i++) {
-               g_variant_builder_add(builder, "y", value[i]);
+               g_variant_builder_add(builder1, "y", value[i]);
                BT_DBG("value [] = %d", value[i]);
        }
 
-       val = g_variant_new("(ay)", builder);
+       val = g_variant_new("ay", builder1);
+
+       builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+       /*offset*/
+       g_variant_builder_add(builder2, "{sv}", "offset",
+                               g_variant_new_uint16(offset));
+
+       /* Device Object path*/
+//     g_variant_builder_add(builder2, "{sv}", "device",
+//     g_variant_new_object("o", NULL));
+
+       options = g_variant_new("a{sv}", builder2);
 
        g_dbus_connection_call(conn,
                                BT_BLUEZ_NAME,
                                char_handle,
                                GATT_CHAR_INTERFACE,
                                "WriteValue",
-                               val,
-                               NULL,
+                               g_variant_new("(@ay@a{sv})",
+                               val, options),
+                               G_VARIANT_TYPE("(y)"),
                                G_DBUS_CALL_FLAGS_NONE,
                                -1, NULL,
                                (GAsyncReadyCallback)__bluetooth_internal_write_cb,
                                NULL);
 
-       g_variant_builder_unref(builder);
+       g_variant_builder_unref(builder1);
+       g_variant_builder_unref(builder2);
 
        BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
@@ -1355,6 +1429,7 @@ static void __bluetooth_internal_read_desc_cb(GObject *source_object,
        GByteArray *gp_byte_array = NULL;
        GVariantIter *iter;
        guint8 g_byte;
+       gint att_error_code;
 
        BT_DBG("+");
        user_info = _bt_get_user_data(BT_COMMON);
@@ -1375,9 +1450,21 @@ static void __bluetooth_internal_read_desc_cb(GObject *source_object,
                return;
        }
 
-       gp_byte_array = g_byte_array_new();
-       g_variant_get(value, "(ay)", &iter);
+       g_variant_get(value, "(yay)", &att_error_code, &iter);
 
+       if (att_error_code != 0) {
+               if (user_info) {
+                       _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_DESC,
+                               att_error_code, NULL,
+                               user_info->cb, user_info->user_data);
+               }
+               g_free(char_value.handle);
+               g_variant_unref(value);
+               g_variant_iter_free(iter);
+               return;
+       }
+
+       gp_byte_array = g_byte_array_new();
        while (g_variant_iter_loop(iter, "y",  &g_byte))
                g_byte_array_append(gp_byte_array, &g_byte, 1);
 
@@ -1403,6 +1490,8 @@ static void __bluetooth_internal_read_desc_cb(GObject *source_object,
 BT_EXPORT_API int bluetooth_gatt_read_descriptor_value(const char *char_descriptor)
 {
        GDBusConnection *conn;
+       GVariantBuilder *builder;
+       guint offset = 0;
        char *handle;
 
        BT_DBG("+");
@@ -1414,18 +1503,28 @@ BT_EXPORT_API int bluetooth_gatt_read_descriptor_value(const char *char_descript
 
        handle = g_strdup(char_descriptor);
 
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+       /*offset*/
+       g_variant_builder_add(builder, "{sv}", "offset",
+                               g_variant_new("q", offset));
+       /* Device Object path*/
+//  g_variant_builder_add(builder, "{sv}", "device",
+//  g_variant_new("o", serv_info->serv_path));
+
        g_dbus_connection_call(conn,
                        BT_BLUEZ_NAME,
                        char_descriptor,
                        GATT_DESC_INTERFACE,
                        "ReadValue",
-                       NULL,
-                       G_VARIANT_TYPE("(ay)"),
+                       g_variant_new("(a{sv})", builder),
+                       G_VARIANT_TYPE("(yay)"),
                        G_DBUS_CALL_FLAGS_NONE,
                        -1,
                        NULL,
                        (GAsyncReadyCallback)__bluetooth_internal_read_desc_cb,
                        (gpointer)handle);
+       g_variant_builder_unref(builder);
 
        BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
@@ -1475,9 +1574,11 @@ static void __bluetooth_internal_write_desc_cb(GObject *source_object,
 BT_EXPORT_API int bluetooth_gatt_write_descriptor_value(
                        const char *desc_handle, const guint8 *value, int length)
 {
-       GVariant *val;
+       GVariant *val, *options;
        GDBusConnection *conn;
-       GVariantBuilder *builder;
+       GVariantBuilder *builder1;
+       GVariantBuilder *builder2;
+       guint offset = 0;
        int i;
 
        BT_DBG("+");
@@ -1489,26 +1590,39 @@ BT_EXPORT_API int bluetooth_gatt_write_descriptor_value(
        conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
+       builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
 
        for (i = 0; i < length; i++)
-               g_variant_builder_add(builder, "y", value[i]);
+               g_variant_builder_add(builder1, "y", value[i]);
 
-       val = g_variant_new("(ay)", builder);
+       val = g_variant_new("ay", builder1);
+
+       builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+       /*offset*/
+       g_variant_builder_add(builder2, "{sv}", "offset",
+                               g_variant_new_uint16(offset));
+
+       /* Device Object path*/
+//     g_variant_builder_add(builder2, "{sv}", "device",
+//     g_variant_new_object("o", NULL));
+
+       options = g_variant_new("a{sv}", builder2);
 
        g_dbus_connection_call(conn,
                                BT_BLUEZ_NAME,
                                desc_handle,
                                GATT_DESC_INTERFACE,
                                "WriteValue",
-                               val,
+                               g_variant_new("(@ay@a{sv})",
+                               val, options),
                                G_VARIANT_TYPE("(y)"),
                                G_DBUS_CALL_FLAGS_NONE,
                                -1, NULL,
                                (GAsyncReadyCallback)__bluetooth_internal_write_desc_cb,
                                NULL);
 
-       g_variant_builder_unref(builder);
+       g_variant_builder_unref(builder1);
+       g_variant_builder_unref(builder2);
 
        BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;