Skip to contentSkip to conversation

shared/io-ell: Fix ELL io wrapper

PATCH Applied Inga Stotland 2 participants 2 messages Sep 24, 2020 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 shared/io-ell: Fix ELL io wrapper

This modifies the internal io structure inside io-ell to retain correct user data associated with write and read handlers and to return these data with the corresponding callbacks.

---
 src/shared/io-ell.c | 54 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 50 insertions(+), 4 deletions(-)
src/shared/io-ell.c +50 -4
index c4a115478..485c613e0 100644
@@ -22,8 +22,48 @@
 
 struct io {
 	struct l_io *l_io;
+	io_callback_func_t read_cb;
+	io_destroy_func_t read_destroy;
+	void *read_data;
+	io_callback_func_t write_cb;
+	io_destroy_func_t write_destroy;
+	void *write_data;
 };
 
+static bool read_callback(struct l_io *l_io, void *user_data)
+{
+	struct io *io = user_data;
+	bool result = false;
+
+	if (!io)
+		return false;
+
+	if (io->read_cb)
+		result = io->read_cb(io, io->read_data);
+
+	if (io->read_destroy)
+		io->read_destroy(io->read_data);
+
+	return result;
+}
+
+static bool write_callback(struct l_io *l_io, void *user_data)
+{
+	struct io *io = user_data;
+	bool result = false;
+
+	if (!io)
+		return false;
+
+	if (io->write_cb)
+		result = io->write_cb(io, io->write_data);
+
+	if (io->write_destroy)
+		io->write_destroy(io->write_data);
+
+	return result;
+}
+
 struct io *io_new(int fd)
 {
 	struct io *io;
@@ -80,8 +120,11 @@ bool io_set_read_handler(struct io *io, io_callback_func_t callback,
 	if (!io || !io->l_io)
 		return false;
 
-	return l_io_set_read_handler(io->l_io, (l_io_read_cb_t) callback,
-							user_data, destroy);
+	io->read_cb = callback;
+	io->read_data = user_data;
+	io->read_destroy = destroy;
+
+	return l_io_set_read_handler(io->l_io, read_callback, io, NULL);
 }
 
 bool io_set_write_handler(struct io *io, io_callback_func_t callback,
@@ -90,8 +133,11 @@ bool io_set_write_handler(struct io *io, io_callback_func_t callback,
 	if (!io || !io->l_io)
 		return false;
 
-	return l_io_set_write_handler(io->l_io, (l_io_write_cb_t) callback,
-							user_data, destroy);
+	io->write_cb = callback;
+	io->write_data = user_data;
+	io->write_destroy = destroy;
+
+	return l_io_set_write_handler(io->l_io, write_callback, io, NULL);
 }
 
 bool io_set_disconnect_handler(struct io *io, io_callback_func_t callback,
Signature · 1 line
2.26.2
maintainer Applied
#1 lore raw

Hi Inga,

Quoted 90 lines · Inga Stotland

This modifies the internal io structure inside io-ell to retain correct user data associated with write and read handlers and to return these data with the corresponding callbacks.

---
 src/shared/io-ell.c | 54 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 50 insertions(+), 4 deletions(-)
src/shared/io-ell.c +50 -5
index c4a115478..485c613e0 100644
@@ -22,8 +22,48 @@
 
 struct io {
        struct l_io *l_io;
+       io_callback_func_t read_cb;
+       io_destroy_func_t read_destroy;
+       void *read_data;
+       io_callback_func_t write_cb;
+       io_destroy_func_t write_destroy;
+       void *write_data;
 };
 
+static bool read_callback(struct l_io *l_io, void *user_data)
+{
+       struct io *io = user_data;
+       bool result = false;
+
+       if (!io)
+               return false;
+
+       if (io->read_cb)
+               result = io->read_cb(io, io->read_data);
+
+       if (io->read_destroy)
+               io->read_destroy(io->read_data);
+
+       return result;
+}
+
+static bool write_callback(struct l_io *l_io, void *user_data)
+{
+       struct io *io = user_data;
+       bool result = false;
+
+       if (!io)
+               return false;
+
+       if (io->write_cb)
+               result = io->write_cb(io, io->write_data);
+
+       if (io->write_destroy)
+               io->write_destroy(io->write_data);
+
+       return result;
+}
+
 struct io *io_new(int fd)
 {
        struct io *io;
@@ -80,8 +120,11 @@ bool io_set_read_handler(struct io *io, io_callback_func_t callback,
        if (!io || !io->l_io)
                return false;
 
-       return l_io_set_read_handler(io->l_io, (l_io_read_cb_t) callback,
-                                                       user_data, destroy);
+       io->read_cb = callback;
+       io->read_data = user_data;
+       io->read_destroy = destroy;
+
+       return l_io_set_read_handler(io->l_io, read_callback, io, NULL);
 }
 
 bool io_set_write_handler(struct io *io, io_callback_func_t callback,
@@ -90,8 +133,11 @@ bool io_set_write_handler(struct io *io, io_callback_func_t callback,
        if (!io || !io->l_io)
                return false;
 
-       return l_io_set_write_handler(io->l_io, (l_io_write_cb_t) callback,
-                                                       user_data, destroy);
+       io->write_cb = callback;
+       io->write_data = user_data;
+       io->write_destroy = destroy;
+
+       return l_io_set_write_handler(io->l_io, write_callback, io, NULL);
 }
 
 bool io_set_disconnect_handler(struct io *io, io_callback_func_t callback,
--
Quoted 2 lines

2.26.2

Applied, thanks.

Signature · 1 line
Luiz Augusto von Dentz