patty/src/timer.c
XANTRONIX Development e10ce6b8e1 Implement generic timer functions in src/timer.c
Changes:

    * Implement patty_timer_expired() to determine if a timer has
      expired or has gone negative

    * Implement patty_timer_cancel() to set a timer to zero

    * Implement patty_timer_start() to initialize a timer, add a
      specified number of milliseconds, and ensure a target timer (such
      as the server timer) is set for at least that value as well

    * Implement patty_timer_tick() to subtract an elapsed time value
      from a timer

    * Remove functions specific to each timer in src/sock.c, as those
      can now be handled generically by src/timer.c

    * Replace calls to patty_ax25_sock_timer_t1_*() with patty_timer_*()
      calls instead; reference the struct timeval objects directly
      within the sock (for the time being, pending opaque structures)
2024-03-01 00:20:46 -05:00

36 lines
860 B
C

#include <patty/timer.h>
int patty_timer_expired(struct timeval *timer) {
return (timer->tv_sec <= 0 && timer->tv_usec == 0)? 1: 0;
}
void patty_timer_cancel(struct timeval *timer) {
timer->tv_sec = 0;
timer->tv_usec = 0;
}
void patty_timer_start(struct timeval *timer,
struct timeval *target,
time_t ms) {
timer->tv_sec = ms / 1000;
timer->tv_usec = (ms % 1000) * 1000;
if (timercmp(timer, target, >)) {
struct timeval res;
timeradd(timer, target, &res);
timer->tv_sec = res.tv_sec;
timer->tv_usec = res.tv_usec;
}
}
void patty_timer_tick(struct timeval *timer,
struct timeval *elapsed) {
struct timeval res;
timersub(timer, elapsed, &res);
timer->tv_sec = res.tv_sec;
timer->tv_usec = res.tv_usec;
}