2014-03-05 09:22:37

by Lukasz Rymanowski

[permalink] [raw]
Subject: [PATCH v2 1/5] monitor: Allow to modify epoll even events are the same

This is epsecially needed when we want to modify timer.
---
monitor/mainloop.c | 3 ---
1 file changed, 3 deletions(-)

diff --git a/monitor/mainloop.c b/monitor/mainloop.c
index 8c94bfe..7f97e65 100644
--- a/monitor/mainloop.c
+++ b/monitor/mainloop.c
@@ -222,9 +222,6 @@ int mainloop_modify_fd(int fd, uint32_t events)
if (!data)
return -ENXIO;

- if (data->events == events)
- return 0;
-
memset(&ev, 0, sizeof(ev));
ev.events = events;
ev.data.ptr = data;
--
1.8.4



2014-03-05 13:39:18

by Anderson Lizardo

[permalink] [raw]
Subject: Re: [PATCH v2 2/5] monitor: Use miliseconds instead of seconds in the timeout

Hi Lukasz,

On Wed, Mar 5, 2014 at 5:22 AM, Lukasz Rymanowski
<[email protected]> wrote:
> -static inline int timeout_set(int fd, unsigned int seconds)
> +static inline int timeout_set(int fd, unsigned int msec)
> {
> struct itimerspec itimer;
> + unsigned int sec = msec / 1000;
>
> memset(&itimer, 0, sizeof(itimer));
> itimer.it_interval.tv_sec = 0;
> itimer.it_interval.tv_nsec = 0;
> - itimer.it_value.tv_sec = seconds;
> - itimer.it_value.tv_nsec = 0;
> + itimer.it_value.tv_sec = sec;
> + itimer.it_value.tv_nsec = (msec - (sec * 1000)) * 1000;

Would you not be multiplying by 1000000 (i.e. 10^6) instead?

Also, why not use:

itimer.it_value.tv_nsec = (msec % 1000) * 1000000;

>
> return timerfd_settime(fd, 0, &itimer, NULL);
> }

Best Regards,
--
Anderson Lizardo
http://www.indt.org/?lang=en
INdT - Manaus - Brazil

2014-03-05 13:24:25

by Szymon Janc

[permalink] [raw]
Subject: Re: [PATCH v2 1/5] monitor: Allow to modify epoll even events are the same

Hi Ɓukasz,

On Wednesday 05 of March 2014 10:22:37 Lukasz Rymanowski wrote:
> This is epsecially needed when we want to modify timer.
> ---
> monitor/mainloop.c | 3 ---
> 1 file changed, 3 deletions(-)
>
> diff --git a/monitor/mainloop.c b/monitor/mainloop.c
> index 8c94bfe..7f97e65 100644
> --- a/monitor/mainloop.c
> +++ b/monitor/mainloop.c
> @@ -222,9 +222,6 @@ int mainloop_modify_fd(int fd, uint32_t events)
> if (!data)
> return -ENXIO;
>
> - if (data->events == events)
> - return 0;
> -
> memset(&ev, 0, sizeof(ev));
> ev.events = events;
> ev.data.ptr = data;
>

All patches applied. Thanks.

--
Best regards,
Szymon Janc

2014-03-05 09:22:41

by Lukasz Rymanowski

[permalink] [raw]
Subject: [PATCH v2 5/5] shared: Add timeout handling with mainloop support

---
src/shared/timeout-mainloop.c | 78 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+)
create mode 100644 src/shared/timeout-mainloop.c

diff --git a/src/shared/timeout-mainloop.c b/src/shared/timeout-mainloop.c
new file mode 100644
index 0000000..bdc527a
--- /dev/null
+++ b/src/shared/timeout-mainloop.c
@@ -0,0 +1,78 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2014 Intel Corporation. All rights reserved.
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ */
+
+#include <stdlib.h>
+
+#include "timeout.h"
+
+#include "monitor/mainloop.h"
+
+struct timeout_data {
+ int id;
+ timeout_func_t func;
+ timeout_destroy_func_t destroy;
+ unsigned int timeout;
+ void *user_data;
+};
+
+static void timeout_callback(int id, void *user_data)
+{
+ struct timeout_data *data = user_data;
+
+ if (data->func(data->user_data) &&
+ !mainloop_modify_timeout(data->id, data->timeout))
+ return;
+
+ mainloop_remove_timeout(data->id);
+}
+
+static void timeout_destroy(void *user_data)
+{
+ struct timeout_data *data = user_data;
+
+ if (data->destroy)
+ data->destroy(data->user_data);
+
+ free(data);
+}
+
+int timeout_add(unsigned int t, timeout_func_t func, void *user_data,
+ timeout_destroy_func_t destroy)
+{
+ struct timeout_data *data = malloc(sizeof(*data));
+
+ data->func = func;
+ data->user_data = user_data;
+ data->timeout = t;
+
+ data->id = mainloop_add_timeout(t, timeout_callback, data,
+ timeout_destroy);
+ if (!data->id) {
+ free(data);
+ return 0;
+ }
+
+ return data->id;
+}
+
+void timeout_remove(unsigned int id)
+{
+ if (id)
+ mainloop_remove_timeout(id);
+}
--
1.8.4


2014-03-05 09:22:40

by Lukasz Rymanowski

[permalink] [raw]
Subject: [PATCH v2 4/5] shared: Add timeout handling with Glib support

---
src/shared/timeout-glib.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 73 insertions(+)
create mode 100644 src/shared/timeout-glib.c

diff --git a/src/shared/timeout-glib.c b/src/shared/timeout-glib.c
new file mode 100644
index 0000000..c045efc
--- /dev/null
+++ b/src/shared/timeout-glib.c
@@ -0,0 +1,73 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2014 Intel Corporation. All rights reserved.
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ */
+
+#include "timeout.h"
+
+#include <glib.h>
+
+struct timeout_data {
+ timeout_func_t func;
+ timeout_destroy_func_t destroy;
+ void *user_data;
+};
+
+static gboolean timeout_callback(gpointer user_data)
+{
+ struct timeout_data *data = user_data;
+
+ if (data->func(data->user_data))
+ return TRUE;
+
+ return FALSE;
+}
+
+static void timeout_destroy(gpointer user_data)
+{
+ struct timeout_data *data = user_data;
+
+ if (data->destroy)
+ data->destroy(data->user_data);
+
+ g_free(data);
+}
+
+int timeout_add(unsigned int ms, timeout_func_t func, void *user_data,
+ timeout_destroy_func_t destroy)
+{
+ guint id;
+ struct timeout_data *data = g_malloc0(sizeof(*data));
+
+ data->func = func;
+ data->destroy = destroy;
+ data->user_data = user_data;
+
+ id = g_timeout_add_full(G_PRIORITY_DEFAULT, ms, timeout_callback, data,
+ timeout_destroy);
+ if (!id)
+ g_free(data);
+
+ return id;
+}
+
+void timeout_remove(unsigned int id)
+{
+ GSource *source = g_main_context_find_source_by_id(NULL, id);
+ if (source)
+ g_source_destroy(source);
+}
--
1.8.4


2014-03-05 09:22:38

by Lukasz Rymanowski

[permalink] [raw]
Subject: [PATCH v2 2/5] monitor: Use miliseconds instead of seconds in the timeout

---
monitor/mainloop.c | 19 ++++++++++---------
monitor/mainloop.h | 4 ++--
tools/3dsp.c | 2 +-
tools/btinfo.c | 2 +-
tools/ibeacon.c | 2 +-
5 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/monitor/mainloop.c b/monitor/mainloop.c
index 7f97e65..8d4b391 100644
--- a/monitor/mainloop.c
+++ b/monitor/mainloop.c
@@ -287,20 +287,21 @@ static void timeout_callback(int fd, uint32_t events, void *user_data)
data->callback(data->fd, data->user_data);
}

-static inline int timeout_set(int fd, unsigned int seconds)
+static inline int timeout_set(int fd, unsigned int msec)
{
struct itimerspec itimer;
+ unsigned int sec = msec / 1000;

memset(&itimer, 0, sizeof(itimer));
itimer.it_interval.tv_sec = 0;
itimer.it_interval.tv_nsec = 0;
- itimer.it_value.tv_sec = seconds;
- itimer.it_value.tv_nsec = 0;
+ itimer.it_value.tv_sec = sec;
+ itimer.it_value.tv_nsec = (msec - (sec * 1000)) * 1000;

return timerfd_settime(fd, 0, &itimer, NULL);
}

-int mainloop_add_timeout(unsigned int seconds, mainloop_timeout_func callback,
+int mainloop_add_timeout(unsigned int msec, mainloop_timeout_func callback,
void *user_data, mainloop_destroy_func destroy)
{
struct timeout_data *data;
@@ -323,8 +324,8 @@ int mainloop_add_timeout(unsigned int seconds, mainloop_timeout_func callback,
return -EIO;
}

- if (seconds > 0) {
- if (timeout_set(data->fd, seconds) < 0) {
+ if (msec > 0) {
+ if (timeout_set(data->fd, msec) < 0) {
close(data->fd);
free(data);
return -EIO;
@@ -341,10 +342,10 @@ int mainloop_add_timeout(unsigned int seconds, mainloop_timeout_func callback,
return data->fd;
}

-int mainloop_modify_timeout(int id, unsigned int seconds)
+int mainloop_modify_timeout(int id, unsigned int msec)
{
- if (seconds > 0) {
- if (timeout_set(id, seconds) < 0)
+ if (msec > 0) {
+ if (timeout_set(id, msec) < 0)
return -EIO;
}

diff --git a/monitor/mainloop.h b/monitor/mainloop.h
index d36f5aa..dafec8b 100644
--- a/monitor/mainloop.h
+++ b/monitor/mainloop.h
@@ -40,9 +40,9 @@ int mainloop_add_fd(int fd, uint32_t events, mainloop_event_func callback,
int mainloop_modify_fd(int fd, uint32_t events);
int mainloop_remove_fd(int fd);

-int mainloop_add_timeout(unsigned int seconds, mainloop_timeout_func callback,
+int mainloop_add_timeout(unsigned int msec, mainloop_timeout_func callback,
void *user_data, mainloop_destroy_func destroy);
-int mainloop_modify_timeout(int fd, unsigned int seconds);
+int mainloop_modify_timeout(int fd, unsigned int msec);
int mainloop_remove_timeout(int id);

int mainloop_set_signal(sigset_t *mask, mainloop_signal_func callback,
diff --git a/tools/3dsp.c b/tools/3dsp.c
index d1b17f2..fc5c8e6 100644
--- a/tools/3dsp.c
+++ b/tools/3dsp.c
@@ -66,7 +66,7 @@ static void shutdown_device(void)
bt_hci_flush(hci_dev);

if (reset_on_shutdown) {
- id = mainloop_add_timeout(5, shutdown_timeout, NULL, NULL);
+ id = mainloop_add_timeout(5000, shutdown_timeout, NULL, NULL);

bt_hci_send(hci_dev, BT_HCI_CMD_RESET, NULL, 0,
shutdown_complete, UINT_TO_PTR(id), NULL);
diff --git a/tools/btinfo.c b/tools/btinfo.c
index 7951808..6693132 100644
--- a/tools/btinfo.c
+++ b/tools/btinfo.c
@@ -109,7 +109,7 @@ static void shutdown_device(void)
bt_hci_flush(hci_dev);

if (reset_on_shutdown) {
- id = mainloop_add_timeout(5, shutdown_timeout, NULL, NULL);
+ id = mainloop_add_timeout(5000, shutdown_timeout, NULL, NULL);

bt_hci_send(hci_dev, BT_HCI_CMD_RESET, NULL, 0,
shutdown_complete, UINT_TO_PTR(id), NULL);
diff --git a/tools/ibeacon.c b/tools/ibeacon.c
index 2071c6b..093fba1 100644
--- a/tools/ibeacon.c
+++ b/tools/ibeacon.c
@@ -65,7 +65,7 @@ static void shutdown_device(void)

bt_hci_flush(hci_dev);

- id = mainloop_add_timeout(5, shutdown_timeout, NULL, NULL);
+ id = mainloop_add_timeout(5000, shutdown_timeout, NULL, NULL);

bt_hci_send(hci_dev, BT_HCI_CMD_LE_SET_ADV_ENABLE,
&enable, 1, NULL, NULL, NULL);
--
1.8.4


2014-03-05 09:22:39

by Lukasz Rymanowski

[permalink] [raw]
Subject: [PATCH v2 3/5] shared: Add header for timeout abstraction

---
src/shared/timeout.h | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
create mode 100644 src/shared/timeout.h

diff --git a/src/shared/timeout.h b/src/shared/timeout.h
new file mode 100644
index 0000000..c13616f
--- /dev/null
+++ b/src/shared/timeout.h
@@ -0,0 +1,27 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2014 Intel Corporation. All rights reserved.
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ */
+
+#include <stdbool.h>
+
+typedef bool (*timeout_func_t)(void *user_data);
+typedef void (*timeout_destroy_func_t)(void *user_data);
+
+int timeout_add(unsigned int msec, timeout_func_t func, void *user_data,
+ timeout_destroy_func_t destroy);
+void timeout_remove(unsigned int id);
--
1.8.4