From 7c91105efd1ee8dc2df874465bf797ba60e6b536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Fillod=2C=20F8CFE?= Date: Sun, 28 Mar 2010 16:33:03 +0000 Subject: [PATCH] added helper functions for cache timeout management git-svn-id: https://hamlib.svn.sourceforge.net/svnroot/hamlib/trunk@2858 7ae35d74-ebe9-4afe-98af-79ac388436b8 --- src/misc.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++--- src/misc.h | 8 ++++++- tuner/v4l.c | 13 +++++----- 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/src/misc.c b/src/misc.c index f30bc0be2..6789ed853 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1,8 +1,6 @@ /* * Hamlib Interface - toolbox - * Copyright (c) 2000-2005 by Stephane Fillod - * - * $Id$ + * Copyright (c) 2000-2010 by Stephane Fillod * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as @@ -44,6 +42,9 @@ #ifdef HAVE_SYS_TYPES_H #include #endif +#ifdef HAVE_SYS_TIME_H +#include +#endif #include #include @@ -776,4 +777,66 @@ const char * HAMLIB_API rig_strmtype(chan_type_t mtype) } +static long timediff(const struct timeval *tv1, const struct timeval *tv2) +{ + struct timeval tv; + + tv.tv_usec = tv1->tv_usec - tv2->tv_usec; + tv.tv_sec = tv1->tv_sec - tv2->tv_sec; + + return ((tv.tv_sec * 1000L) + (tv.tv_usec / 1000L)); +} + +/** + * \brief Helper for checking cache timeout + * \param tv pointer to timeval, date of cache + * \param timeout duration of cache validity, in millisec + * \return 1 when timed out, 0 when cache shall be used + */ +int HAMLIB_API rig_check_cache_timeout(const struct timeval *tv, int timeout) +{ + struct timeval curr; + long t; + + if (tv->tv_sec == 0 && tv->tv_usec == 0) { + rig_debug(RIG_DEBUG_VERBOSE, "forced cache timeout\n"); + return 1; + } + + gettimeofday(&curr, NULL); + + t = timediff(&curr, tv); + if (t < timeout) { + rig_debug(RIG_DEBUG_VERBOSE, "%s: using cache (%ld ms)\n", + __func__, t); + return 0; + } else { + rig_debug(RIG_DEBUG_VERBOSE, "%s: cache timed out (%ld ms)\n", + __func__, t); + return 1; + } +} + +/** + * \brief Helper for forcing cache timeout next call + * + * This function is typically to be called in backend_set_* functions, + * so that a sequence: + * +\code + rig_get_freq(); + rig_set_freq(); + rig_get_freq(); +\endcode + * + * doesn't return a bogus (cached) value in the last rig_get_freq(). + * + * \param tv pointer to timeval to be reset + */ +void HAMLIB_API rig_force_cache_timeout(struct timeval *tv) +{ + tv->tv_sec = 0; + tv->tv_usec = 0; +} + /** @} */ diff --git a/src/misc.h b/src/misc.h index 0bc7ea413..b44227de8 100644 --- a/src/misc.h +++ b/src/misc.h @@ -62,7 +62,6 @@ extern HAMLIB_EXPORT(unsigned long long) from_bcd_be(const unsigned char bcd_dat extern HAMLIB_EXPORT(int) sprintf_freq(char *str, freq_t); - /* check if it's any of CR or LF */ #define isreturn(c) ((c) == 10 || (c) == 13) @@ -71,6 +70,13 @@ extern HAMLIB_EXPORT(int) sprintf_freq(char *str, freq_t); #ifdef HAVE_INTTYPES_H #include #endif +#ifdef HAVE_SYS_TIME_H +#include +#endif + +extern HAMLIB_EXPORT(int) rig_check_cache_timeout(const struct timeval *tv, int timeout); +extern HAMLIB_EXPORT(void) rig_force_cache_timeout(struct timeval *tv); + #ifdef PRId64 /** \brief printf(3) format to be used for long long (64bits) type */ diff --git a/tuner/v4l.c b/tuner/v4l.c index a6efb2440..836ea0449 100644 --- a/tuner/v4l.c +++ b/tuner/v4l.c @@ -1,8 +1,7 @@ /* * Hamlib Tuner backend - Video4Linux (v1) description - * Copyright (c) 2004-2009 by Stephane Fillod + * Copyright (c) 2004-2010 by Stephane Fillod * - * $Id: v4l.c,v 1.2 2004-09-25 14:33:52 fillods Exp $ * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as @@ -26,15 +25,15 @@ #include #include +#include "hamlib/rig.h" +#include "tuner.h" /* include config.h */ + +#include "misc.h" + #ifdef HAVE_SYS_IOCTL_H #include #endif -#include "hamlib/rig.h" -#include "misc.h" - -#include "tuner.h" /* include config.h */ - #ifdef V4L_IOCTL #include