2020-09-24 18:21:59

by Stotland, Inga

[permalink] [raw]
Subject: [PATCH BlueZ] 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(-)

diff --git a/src/shared/io-ell.c b/src/shared/io-ell.c
index c4a115478..485c613e0 100644
--- a/src/shared/io-ell.c
+++ b/src/shared/io-ell.c
@@ -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,
--
2.26.2


2020-09-24 23:25:35

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [PATCH BlueZ] shared/io-ell: Fix ELL io wrapper

Hi Inga,

On Thu, Sep 24, 2020 at 11:20 AM Inga Stotland <[email protected]> wrote:
>
> 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(-)
>
> diff --git a/src/shared/io-ell.c b/src/shared/io-ell.c
> index c4a115478..485c613e0 100644
> --- a/src/shared/io-ell.c
> +++ b/src/shared/io-ell.c
> @@ -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,
> --
> 2.26.2
>

Applied, thanks.

--
Luiz Augusto von Dentz