From efee7262b60a7caa640519cfeb5047c12608c192 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Wed, 5 Sep 2018 17:15:20 +0200 Subject: [PATCH 1/3] lib: Split off timeout helpers And rename poll.c to fpi-poll.c --- libfprint/drivers_api.h | 10 +--------- libfprint/fp_internal.h | 4 +--- libfprint/{poll.c => fpi-poll.c} | 1 + libfprint/fpi-poll.h | 26 ++++++++++++++++++++++++++ libfprint/meson.build | 3 ++- 5 files changed, 31 insertions(+), 13 deletions(-) rename libfprint/{poll.c => fpi-poll.c} (99%) create mode 100644 libfprint/fpi-poll.h diff --git a/libfprint/drivers_api.h b/libfprint/drivers_api.h index 02fe43c..cba678a 100644 --- a/libfprint/drivers_api.h +++ b/libfprint/drivers_api.h @@ -37,6 +37,7 @@ #include "fprint.h" #include "fpi-ssm.h" +#include "fpi-poll.h" #include "assembling.h" #include "drivers/driver_ids.h" @@ -203,15 +204,6 @@ struct fp_img *fpi_img_new_for_imgdev(struct fp_img_dev *dev); struct fp_img *fpi_img_resize(struct fp_img *img, size_t newsize); struct fp_img *fpi_im_resize(struct fp_img *img, unsigned int w_factor, unsigned int h_factor); -/* polling and timeouts */ - -typedef void (*fpi_timeout_fn)(void *data); - -struct fpi_timeout; -struct fpi_timeout *fpi_timeout_add(unsigned int msec, fpi_timeout_fn callback, - void *data); -void fpi_timeout_cancel(struct fpi_timeout *timeout); - void fpi_drvcb_open_complete(struct fp_dev *dev, int status); void fpi_drvcb_close_complete(struct fp_dev *dev); diff --git a/libfprint/fp_internal.h b/libfprint/fp_internal.h index 48a2518..9aa03f2 100644 --- a/libfprint/fp_internal.h +++ b/libfprint/fp_internal.h @@ -341,13 +341,11 @@ int fpi_img_compare_print_data_to_gallery(struct fp_print_data *print, struct fp_print_data **gallery, int match_threshold, size_t *match_offset); struct fp_img *fpi_im_resize(struct fp_img *img, unsigned int w_factor, unsigned int h_factor); -/* polling and timeouts */ +/* polling */ void fpi_poll_init(void); void fpi_poll_exit(void); -typedef void (*fpi_timeout_fn)(void *data); - void fpi_drvcb_open_complete(struct fp_dev *dev, int status); void fpi_drvcb_close_complete(struct fp_dev *dev); diff --git a/libfprint/poll.c b/libfprint/fpi-poll.c similarity index 99% rename from libfprint/poll.c rename to libfprint/fpi-poll.c index 615ba40..b572e56 100644 --- a/libfprint/poll.c +++ b/libfprint/fpi-poll.c @@ -20,6 +20,7 @@ #define FP_COMPONENT "poll" #include "fp_internal.h" +#include "fpi-poll.h" #include #include diff --git a/libfprint/fpi-poll.h b/libfprint/fpi-poll.h new file mode 100644 index 0000000..be2de9c --- /dev/null +++ b/libfprint/fpi-poll.h @@ -0,0 +1,26 @@ +/* + * Polling/timing management + * Copyright (C) 2008 Daniel Drake + * Copyright (C) 2018 Bastien Nocera + * + * 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. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +typedef void (*fpi_timeout_fn)(void *data); + +struct fpi_timeout; +struct fpi_timeout *fpi_timeout_add(unsigned int msec, fpi_timeout_fn callback, + void *data); +void fpi_timeout_cancel(struct fpi_timeout *timeout); diff --git a/libfprint/meson.build b/libfprint/meson.build index 0a9ed02..c6d1ac9 100644 --- a/libfprint/meson.build +++ b/libfprint/meson.build @@ -6,9 +6,10 @@ libfprint_sources = [ 'data.c', 'fpi-ssm.c', 'fpi-ssm.h', + 'fpi-poll.h', + 'fpi-poll.c', 'img.c', 'imgdev.c', - 'poll.c', 'sync.c', 'assembling.c', 'assembling.h', From 969eefc81f29bbf6c7993b8fb0f7ca179f69e001 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Wed, 5 Sep 2018 17:33:25 +0200 Subject: [PATCH 2/3] lib: Add fpi_timeout API documentation --- doc/libfprint-docs.xml | 1 + doc/libfprint-sections.txt | 9 +++++++++ libfprint/fpi-poll.c | 33 +++++++++++++++++++++++++++++++-- libfprint/fpi-poll.h | 12 ++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/doc/libfprint-docs.xml b/doc/libfprint-docs.xml index 8f8622d..c4e3445 100644 --- a/doc/libfprint-docs.xml +++ b/doc/libfprint-docs.xml @@ -39,6 +39,7 @@ Writing Drivers + diff --git a/doc/libfprint-sections.txt b/doc/libfprint-sections.txt index 1f99e5a..91310b9 100644 --- a/doc/libfprint-sections.txt +++ b/doc/libfprint-sections.txt @@ -162,3 +162,12 @@ fpi_ssm_get_user_data fpi_ssm_get_error fpi_ssm_get_cur_state + +
+fpi-poll.h +fpi-poll +fpi_timeout +fpi_timeout_fn +fpi_timeout_add +fpi_timeout_cancel +
diff --git a/libfprint/fpi-poll.c b/libfprint/fpi-poll.c index b572e56..cd6f9ba 100644 --- a/libfprint/fpi-poll.c +++ b/libfprint/fpi-poll.c @@ -64,6 +64,15 @@ * your main loop. */ +/** + * SECTION:fpi-poll + * @title: Timeouts + * + * Helper functions to schedule a function call to be made after a timeout. This + * is useful to avoid making blocking calls while waiting for hardware to answer + * for example. + */ + /* this is a singly-linked list of pending timers, sorted with the timer that * is expiring soonest at the head. */ static GSList *active_timers = NULL; @@ -93,9 +102,22 @@ static int timeout_sort_fn(gconstpointer _a, gconstpointer _b) return 0; } -/* A timeout is the asynchronous equivalent of sleeping. You create a timeout +/** + * fpi_timeout_add: + * @msec: the time before calling the function, in milliseconds (1/1000ths of a second) + * @callback: function to callback + * @data: data to pass to @callback + * + * A timeout is the asynchronous equivalent of sleeping. You create a timeout * saying that you'd like to have a function invoked at a certain time in - * the future. */ + * the future. + * + * Note that you should hold onto the return value of this function to cancel it + * use fpi_timeout_cancel(), otherwise the callback could be called while the driver + * is being torn down. %NULL is returned on failure. + * + * Returns: an #fpi_timeout structure + */ struct fpi_timeout *fpi_timeout_add(unsigned int msec, fpi_timeout_fn callback, void *data) { @@ -129,6 +151,13 @@ struct fpi_timeout *fpi_timeout_add(unsigned int msec, fpi_timeout_fn callback, return timeout; } +/** + * fpi_timeout_cancel: + * @timeout: an #fpi_timeout structure + * + * Cancels a timeout scheduled with fpi_timeout_add(), and frees the + * @timeout structure. + */ void fpi_timeout_cancel(struct fpi_timeout *timeout) { G_DEBUG_HERE(); diff --git a/libfprint/fpi-poll.h b/libfprint/fpi-poll.h index be2de9c..3f85704 100644 --- a/libfprint/fpi-poll.h +++ b/libfprint/fpi-poll.h @@ -18,8 +18,20 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +/** + * fpi_timeout_fn: + * @data: the data passed to fpi_timeout_add() + * + * The prototype of the callback function for fpi_timeout_add(). + */ typedef void (*fpi_timeout_fn)(void *data); +/** + * fpi_timeout: + * + * An opaque structure representing a scheduled function call, created with + * fpi_timeout_add(). + */ struct fpi_timeout; struct fpi_timeout *fpi_timeout_add(unsigned int msec, fpi_timeout_fn callback, void *data); From 2946dabd244bfc4d06ff6691d9cc236cbf72f3cb Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Wed, 5 Sep 2018 17:34:59 +0200 Subject: [PATCH 3/3] lib: Typedef fpi_timeout --- libfprint/drivers/uru4000.c | 2 +- libfprint/drivers/vfs101.c | 2 +- libfprint/drivers/vfs301.c | 2 +- libfprint/drivers/vfs5011.c | 2 +- libfprint/fpi-poll.c | 10 +++++----- libfprint/fpi-poll.h | 6 +++--- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/libfprint/drivers/uru4000.c b/libfprint/drivers/uru4000.c index fddd411..cad94fa 100644 --- a/libfprint/drivers/uru4000.c +++ b/libfprint/drivers/uru4000.c @@ -135,7 +135,7 @@ struct uru4k_dev { unsigned char powerup_hwstat; int scanpwr_irq_timeouts; - struct fpi_timeout *scanpwr_irq_timeout; + fpi_timeout *scanpwr_irq_timeout; int fwfixer_offset; unsigned char fwfixer_value; diff --git a/libfprint/drivers/vfs101.c b/libfprint/drivers/vfs101.c index d57b4fc..a2ac6b9 100644 --- a/libfprint/drivers/vfs101.c +++ b/libfprint/drivers/vfs101.c @@ -106,7 +106,7 @@ struct vfs101_dev int ignore_error; /* Timeout */ - struct fpi_timeout *timeout; + fpi_timeout *timeout; /* Loop counter */ int counter; diff --git a/libfprint/drivers/vfs301.c b/libfprint/drivers/vfs301.c index 403bd36..66806ae 100644 --- a/libfprint/drivers/vfs301.c +++ b/libfprint/drivers/vfs301.c @@ -38,7 +38,7 @@ static void async_sleep_cb(void *data) static void async_sleep(unsigned int msec, fpi_ssm *ssm) { struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm); - struct fpi_timeout *timeout; + fpi_timeout *timeout; /* Add timeout */ timeout = fpi_timeout_add(msec, async_sleep_cb, ssm); diff --git a/libfprint/drivers/vfs5011.c b/libfprint/drivers/vfs5011.c index f1e3d99..93deb05 100644 --- a/libfprint/drivers/vfs5011.c +++ b/libfprint/drivers/vfs5011.c @@ -672,7 +672,7 @@ static void activate_loop(fpi_ssm *ssm) struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm); struct vfs5011_data *data; int r; - struct fpi_timeout *timeout; + fpi_timeout *timeout; data = fpi_imgdev_get_user_data(dev); diff --git a/libfprint/fpi-poll.c b/libfprint/fpi-poll.c index cd6f9ba..5c6fc96 100644 --- a/libfprint/fpi-poll.c +++ b/libfprint/fpi-poll.c @@ -89,8 +89,8 @@ struct fpi_timeout { static int timeout_sort_fn(gconstpointer _a, gconstpointer _b) { - struct fpi_timeout *a = (struct fpi_timeout *) _a; - struct fpi_timeout *b = (struct fpi_timeout *) _b; + fpi_timeout *a = (fpi_timeout *) _a; + fpi_timeout *b = (fpi_timeout *) _b; struct timeval *tv_a = &a->expiry; struct timeval *tv_b = &b->expiry; @@ -118,12 +118,12 @@ static int timeout_sort_fn(gconstpointer _a, gconstpointer _b) * * Returns: an #fpi_timeout structure */ -struct fpi_timeout *fpi_timeout_add(unsigned int msec, fpi_timeout_fn callback, +fpi_timeout *fpi_timeout_add(unsigned int msec, fpi_timeout_fn callback, void *data) { struct timespec ts; struct timeval add_msec; - struct fpi_timeout *timeout; + fpi_timeout *timeout; int r; fp_dbg("in %dms", msec); @@ -158,7 +158,7 @@ struct fpi_timeout *fpi_timeout_add(unsigned int msec, fpi_timeout_fn callback, * Cancels a timeout scheduled with fpi_timeout_add(), and frees the * @timeout structure. */ -void fpi_timeout_cancel(struct fpi_timeout *timeout) +void fpi_timeout_cancel(fpi_timeout *timeout) { G_DEBUG_HERE(); active_timers = g_slist_remove(active_timers, timeout); diff --git a/libfprint/fpi-poll.h b/libfprint/fpi-poll.h index 3f85704..34c2520 100644 --- a/libfprint/fpi-poll.h +++ b/libfprint/fpi-poll.h @@ -32,7 +32,7 @@ typedef void (*fpi_timeout_fn)(void *data); * An opaque structure representing a scheduled function call, created with * fpi_timeout_add(). */ -struct fpi_timeout; -struct fpi_timeout *fpi_timeout_add(unsigned int msec, fpi_timeout_fn callback, +typedef struct fpi_timeout fpi_timeout; +fpi_timeout *fpi_timeout_add(unsigned int msec, fpi_timeout_fn callback, void *data); -void fpi_timeout_cancel(struct fpi_timeout *timeout); +void fpi_timeout_cancel(fpi_timeout *timeout);