From baefc1803bcd883f7b1e6e98ffb05da72fa7337c Mon Sep 17 00:00:00 2001 From: Sebastian Urban Date: Sat, 12 Jun 2021 11:56:01 +0200 Subject: [PATCH] gatt-database: No multiple calls to AcquireWrite This checks if an outstanding call to AcquireWrite is already in progress. If so, the write request is placed into the queue, but AcquireWrite is not called again. When a response to AcquireWrite is received, acquire_write_reply sends all queued writes over the acquired socket. Making multiple simultaneous calls to AcquireWrite makes no sense, as this would open multiple socket pairs and only the last returned socket would be used for further writes. Signed-off-by: Anuj Jain Signed-off-by: Ayush Garg --- src/gatt-database.c | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/gatt-database.c b/src/gatt-database.c index 9327f9a..1da4d64 100644 --- a/src/gatt-database.c +++ b/src/gatt-database.c @@ -2867,6 +2867,26 @@ static struct pending_op *send_write(struct btd_device *device, return NULL; } +static void flush_pending_write(void *data, void *user_data) +{ + GDBusProxy *proxy = user_data; + struct pending_op *op = data; + + if (g_dbus_proxy_method_call(proxy, "WriteValue", write_setup_cb, + write_reply_cb, + op, pending_op_free) == TRUE) + return; + + pending_op_free(op); +} + +static void flush_pending_writes(GDBusProxy *proxy, + struct queue *owner_queue) +{ + queue_foreach(owner_queue, flush_pending_write, proxy); + queue_remove_all(owner_queue, NULL, NULL, NULL); +} + static bool sock_hup(struct io *io, void *user_data) { struct external_chrc *chrc = user_data; @@ -2982,22 +3002,19 @@ static void acquire_write_reply(DBusMessage *message, void *user_data) chrc->write_io = sock_io_new(fd, chrc); - if (sock_io_send(chrc->write_io, op->data.iov_base, - op->data.iov_len) < 0) - goto retry; + while ((op = queue_peek_head(chrc->pending_writes)) != NULL) { + if (sock_io_send(chrc->write_io, op->data.iov_base, + op->data.iov_len) < 0) + goto retry; - gatt_db_attribute_write_result(op->attrib, op->id, 0); + gatt_db_attribute_write_result(op->attrib, op->id, 0); + pending_op_free(op); + } return; retry: - send_write(op->device, op->attrib, chrc->proxy, NULL, op->id, -#ifdef TIZEN_FEATURE_BLUEZ_MODIFY - op->data.iov_base, op->data.iov_len, 0, 0, -#else - op->data.iov_base, op->data.iov_len, 0, -#endif - op->link_type, false, false); + flush_pending_writes(chrc->proxy, chrc->pending_writes); } static void acquire_write_setup(DBusMessageIter *iter, void *user_data) @@ -3025,6 +3042,7 @@ static struct pending_op *acquire_write(struct external_chrc *chrc, uint8_t link_type) { struct pending_op *op; + bool acquiring = !queue_isempty(chrc->pending_writes); #ifdef TIZEN_FEATURE_BLUEZ_MODIFY op = pending_write_new(device, chrc->pending_writes, attrib, id, value, len, 0, 0, @@ -3033,10 +3051,13 @@ static struct pending_op *acquire_write(struct external_chrc *chrc, #endif link_type, false, false); + if (acquiring) + return op; + if (g_dbus_proxy_method_call(chrc->proxy, "AcquireWrite", acquire_write_setup, acquire_write_reply, - op, pending_op_free)) + op, NULL)) return op; pending_op_free(op); -- 2.7.4