From 15c7eb79785a0707b4518cc12de95fd1dd1a7e7c Mon Sep 17 00:00:00 2001 From: "h.sandeep" Date: Fri, 17 Mar 2017 15:01:55 +0530 Subject: [PATCH] GATT-Client: Modify Read and Write procedure as per Bluez 5.43 Change-Id: I6b3967fae826dcf9cad32657611c0c7b8f3c482c Signed-off-by: h.sandeep --- bt-api/bt-gatt-client.c | 149 +++++++++++++++++++++++++++++++--------- 1 file changed, 116 insertions(+), 33 deletions(-) diff --git a/bt-api/bt-gatt-client.c b/bt-api/bt-gatt-client.c index 71e85c54..9e0f92ad 100644 --- a/bt-api/bt-gatt-client.c +++ b/bt-api/bt-gatt-client.c @@ -939,6 +939,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,18 +950,29 @@ 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_new("(a{sv})", builder), G_VARIANT_TYPE("(ay)"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, (GAsyncReadyCallback)__bluetooth_internal_read_cb, (gpointer)handle); + g_variant_builder_unref(builder); return BLUETOOTH_ERROR_NONE; } @@ -967,11 +980,13 @@ BT_EXPORT_API int bluetooth_gatt_read_characteristic_value(const char *character BT_EXPORT_API int bluetooth_gatt_set_characteristics_value( const char *char_handle, const guint8 *value, int length) { - GVariant *val; - GVariantBuilder *builder; + 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); @@ -982,19 +997,29 @@ BT_EXPORT_API int bluetooth_gatt_set_characteristics_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", builder1); + + builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + /*offset*/ + g_variant_builder_add(builder2, "{sv}", "offset", + g_variant_new_uint16(offset)); - val = g_variant_new("(ay)", builder); + /* 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_sync(conn, BT_BLUEZ_NAME, char_handle, GATT_CHAR_INTERFACE, "WriteValue", - val, + g_variant_new("(@ay@a{sv})", val, options), NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); @@ -1002,11 +1027,11 @@ BT_EXPORT_API int bluetooth_gatt_set_characteristics_value( if (error) { BT_ERR("Set value Failed: %s", error->message); g_clear_error(&error); - g_variant_builder_unref(builder); + g_variant_builder_unref(builder1); return BLUETOOTH_ERROR_INTERNAL; } - - g_variant_builder_unref(builder); + g_variant_builder_unref(builder1); + g_variant_builder_unref(builder2); BT_DBG("-"); return BLUETOOTH_ERROR_NONE; @@ -1057,9 +1082,11 @@ static void __bluetooth_internal_write_cb(GObject *source_object, 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 +1098,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 +1153,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, + g_variant_new("(@ay@a{sv})", + val, options), NULL, 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; @@ -1403,6 +1459,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 +1472,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_new("(a{sv})", builder), G_VARIANT_TYPE("(ay)"), 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 +1543,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 +1559,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; -- 2.34.1