2015-04-12 10:19:02 +02:00
|
|
|
/*
|
2015-04-14 07:42:23 +02:00
|
|
|
Tis file is part of libcapwap.
|
2015-04-12 10:19:02 +02:00
|
|
|
|
|
|
|
libcapwap is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
libcapwap 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2015-10-18 09:03:17 +02:00
|
|
|
along with libcapwap. If not, see <http://www.gnu.org/licenses/>.
|
2015-04-12 10:19:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-04-11 19:02:45 +02:00
|
|
|
#ifndef __CW_TIMER_H
|
|
|
|
#define __CW_TIMER_H
|
|
|
|
|
|
|
|
#include <time.h>
|
2015-04-12 11:03:43 +02:00
|
|
|
#include <sys/time.h>
|
2015-04-11 19:02:45 +02:00
|
|
|
|
|
|
|
/**
|
2015-10-17 19:28:07 +02:00
|
|
|
* @defgroup TimerFunctions Timer & Clock Functions
|
2015-04-11 19:02:45 +02:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2015-04-19 16:44:20 +02:00
|
|
|
/**
|
2015-05-01 20:44:18 +02:00
|
|
|
* CAPWAP timer type, used in conjunction with #cw_timer_start and #cw_timer_timeout.
|
2015-04-19 16:44:20 +02:00
|
|
|
*/
|
|
|
|
typedef time_t cw_timer_t;
|
|
|
|
|
|
|
|
|
2015-04-11 19:02:45 +02:00
|
|
|
/**
|
|
|
|
* Start a timer.
|
|
|
|
* @param t number of seconds until the timer expires
|
2015-10-18 08:59:15 +02:00
|
|
|
* @return timer value to initialize a variable of #cw_timer_t
|
2015-04-11 19:02:45 +02:00
|
|
|
*
|
2015-10-18 08:54:38 +02:00
|
|
|
* Example:
|
|
|
|
* @code
|
2015-10-18 08:59:15 +02:00
|
|
|
* cw_timer_t timer = cw_timer_start(60);
|
2015-10-18 08:54:38 +02:00
|
|
|
* while (!cw_timer_timeout(timer)){
|
2018-03-19 17:26:01 +01:00
|
|
|
* @startcomment Do something for max. 60 seconds ... @endcomment
|
2015-10-18 08:54:38 +02:00
|
|
|
* }
|
|
|
|
* @endcode
|
2015-04-11 19:02:45 +02:00
|
|
|
*/
|
2016-03-27 04:23:06 +02:00
|
|
|
#define cw_timer_start(t) (time(NULL)+(t))
|
2015-04-11 19:02:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a timer is expired.
|
2015-10-18 08:59:15 +02:00
|
|
|
* @param t a variable of type #cw_timer_t intialized by #cw_timer_start
|
2015-04-11 19:02:45 +02:00
|
|
|
* @return 0=timer is not expired\n 1=timer is expired.
|
|
|
|
*/
|
2016-03-27 04:23:06 +02:00
|
|
|
#define cw_timer_timeout(t) (time(NULL)>(t) ? 1 : 0)
|
2015-04-11 19:02:45 +02:00
|
|
|
|
2016-03-27 04:37:57 +02:00
|
|
|
/**
|
|
|
|
* Get the number of seconds left in a timer
|
|
|
|
* @param t timer
|
|
|
|
* @return number of seconds
|
|
|
|
*/
|
|
|
|
#define cw_timer_timeleft(t) ((t)-time(NULL))
|
|
|
|
|
2015-05-01 20:44:18 +02:00
|
|
|
/**
|
|
|
|
* Get the number of nano seconds from a timeval.
|
|
|
|
*/
|
2015-04-12 11:03:43 +02:00
|
|
|
#define cw_timevaltodouble(tv) \
|
|
|
|
(( ((1000000.0)*(double)((tv)->tv_sec) + (double)(tv)->tv_usec)) )
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define a clock variable to measure runtime (not CPU runtime, but
|
2015-10-17 19:28:07 +02:00
|
|
|
* real time). The created variable can be used with #cw_clock_start
|
2015-04-12 11:03:43 +02:00
|
|
|
* and #cw_clock_lap.
|
|
|
|
* @param c name of the variable
|
2015-10-17 19:28:07 +02:00
|
|
|
*
|
2018-03-30 19:45:27 +02:00
|
|
|
* Example
|
|
|
|
* @code{.c}
|
|
|
|
* #include "cw/timer.h"
|
|
|
|
*
|
2015-10-17 19:28:07 +02:00
|
|
|
* CW_CLOCK_DEFINE(clk);
|
2018-03-30 19:45:27 +02:00
|
|
|
* int t;
|
|
|
|
*
|
2015-10-17 19:28:07 +02:00
|
|
|
* cw_clock_start(clk);
|
2018-03-30 19:45:27 +02:00
|
|
|
*
|
2018-03-19 17:26:01 +01:00
|
|
|
* @startcomment do something ... @endcomment
|
2018-03-30 19:45:27 +02:00
|
|
|
*
|
|
|
|
* t = cw_clock_lap(clk);
|
2015-10-17 19:28:07 +02:00
|
|
|
* printf("Caclulation took %d seconds\n",t);
|
|
|
|
* @endcode
|
|
|
|
*
|
2015-05-01 20:44:18 +02:00
|
|
|
*/
|
2015-10-17 19:28:07 +02:00
|
|
|
#define CW_CLOCK_DEFINE(c)\
|
2015-05-01 20:44:18 +02:00
|
|
|
struct timeval c;
|
2015-04-12 11:03:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the clock (stop watch), start measuring time
|
2015-10-18 08:54:38 +02:00
|
|
|
* @param c a pointer to a variable defined with #CW_CLOCK_DEFINE
|
2015-05-01 20:44:18 +02:00
|
|
|
*/
|
2015-04-12 11:03:43 +02:00
|
|
|
#define cw_clock_start(c)\
|
|
|
|
gettimeofday(c,NULL);
|
|
|
|
|
2018-03-02 13:36:03 +01:00
|
|
|
|
|
|
|
double cw_clock_lap(struct timeval *tv);
|
|
|
|
|
2015-04-12 11:03:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An alias for #cw_clock_lap
|
2015-05-01 20:44:18 +02:00
|
|
|
*/
|
2015-04-12 11:03:43 +02:00
|
|
|
#define cw_clock_stop cw_clock_lap
|
2015-04-11 19:02:45 +02:00
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|