From e10ce6b8e10636a6e7f4d17cd0fa8ad68635b09f Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Thu, 30 Jul 2020 01:26:55 -0400 Subject: [PATCH] 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) --- include/patty/ax25/sock.h | 10 ---------- include/patty/timer.h | 17 +++++++++++++++++ src/Makefile | 8 +++++--- src/server.c | 15 ++++++++------- src/sock.c | 35 ----------------------------------- src/timer.c | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 55 deletions(-) create mode 100644 include/patty/timer.h create mode 100644 src/timer.c diff --git a/include/patty/ax25/sock.h b/include/patty/ax25/sock.h index b602944..d22d37d 100644 --- a/include/patty/ax25/sock.h +++ b/include/patty/ax25/sock.h @@ -98,16 +98,6 @@ patty_ax25_sock *patty_ax25_sock_new(enum patty_ax25_proto proto, void patty_ax25_sock_destroy(patty_ax25_sock *sock); -int patty_ax25_sock_timer_t1_expired(patty_ax25_sock *sock); - -void patty_ax25_sock_timer_t1_cancel(patty_ax25_sock *sock); - -void patty_ax25_sock_timer_t1_start(patty_ax25_sock *sock, - struct timeval *timer); - -void patty_ax25_sock_timer_t1_sub(patty_ax25_sock *sock, - struct timeval *elapsed); - int patty_ax25_sock_reset(patty_ax25_sock *sock); int patty_ax25_sock_upgrade(patty_ax25_sock *sock, diff --git a/include/patty/timer.h b/include/patty/timer.h new file mode 100644 index 0000000..18259d6 --- /dev/null +++ b/include/patty/timer.h @@ -0,0 +1,17 @@ +#ifndef _PATTY_TIMER_H +#define _PATTY_TIMER_H + +#include + +int patty_timer_expired(struct timeval *timer); + +void patty_timer_cancel(struct timeval *timer); + +void patty_timer_start(struct timeval *timer, + struct timeval *target, + time_t ms); + +void patty_timer_tick(struct timeval *timer, + struct timeval *elapsed); + +#endif /* _PATTY_TIMER_H */ diff --git a/src/Makefile b/src/Makefile index f7c693a..7a02606 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,10 +8,12 @@ CFLAGS = $(CGFLAGS) -fPIC -Wall -O2 -I$(INCLUDE_PATH) LDFLAGS = HEADERS = kiss.h ax25.h ax25/if.h ax25/call.h ax25/frame.h ax25/sock.h \ - ax25/route.h ax25/server.h list.h hash.h dict.h print.h + ax25/route.h ax25/server.h list.h hash.h dict.h timer.h \ + print.h -OBJS = kiss.o ax25.o if.o call.o frame.o sock.o route.o server.o \ - list.o hash.o dict.o print.o +OBJS = kiss.o ax25.o if.o call.o frame.o sock.o \ + route.o server.o list.o hash.o dict.o timer.o \ + print.o VERSION_MAJOR = 0 VERSION_MINOR = 0.1 diff --git a/src/server.c b/src/server.c index 803bf0c..715f903 100644 --- a/src/server.c +++ b/src/server.c @@ -11,6 +11,7 @@ #include #include +#include typedef int (*patty_ax25_server_call)(patty_ax25_server *, int); @@ -829,7 +830,7 @@ static int server_connect(patty_ax25_server *server, return respond_connect(client, -1, errno); } - patty_ax25_sock_timer_t1_start(sock, &server->timer); + patty_timer_start(&sock->timer_t1, &server->timer, sock->n_ack); /* * At this point, we will wait for a DM, FRMR or XID response, which @@ -1152,7 +1153,7 @@ static int handle_frmr(patty_ax25_server *server, if (sock->status == PATTY_AX25_SOCK_PENDING_CONNECT) { int ret = patty_ax25_sock_send_sabm(sock, PATTY_AX25_FRAME_POLL); - patty_ax25_sock_timer_t1_start(sock, &server->timer); + patty_timer_start(&sock->timer_t1, &server->timer, sock->n_ack); return ret; } @@ -1250,7 +1251,7 @@ static int handle_ua(patty_ax25_server *server, return reply_dm(iface, frame, PATTY_AX25_FRAME_FINAL); } - patty_ax25_sock_timer_t1_cancel(sock); + patty_timer_cancel(&sock->timer_t1); sock->status = PATTY_AX25_SOCK_ESTABLISHED; @@ -1452,7 +1453,7 @@ static int handle_xid(patty_ax25_server *server, ret = patty_ax25_sock_send_sabm(remote, PATTY_AX25_FRAME_POLL); - patty_ax25_sock_timer_t1_start(remote, &server->timer); + patty_timer_start(&remote->timer_t1, &server->timer, remote->n_ack); return ret; } @@ -1629,11 +1630,11 @@ static int handle_sock(uint32_t key, ssize_t len; if (sock->status == PATTY_AX25_SOCK_PENDING_CONNECT) { - if (patty_ax25_sock_timer_t1_expired(sock)) { + if (patty_timer_expired(&sock->timer_t1)) { if (sock->retries) { int ret = patty_ax25_sock_send_sabm(sock, PATTY_AX25_FRAME_POLL); - patty_ax25_sock_timer_t1_start(sock, &server->timer); + patty_timer_start(&sock->timer_t1, &server->timer, sock->n_ack); return ret; } else { @@ -1642,7 +1643,7 @@ static int handle_sock(uint32_t key, return sock_close(server, sock); } } else { - patty_ax25_sock_timer_t1_sub(sock, &server->elapsed); + patty_timer_tick(&sock->timer_t1, &server->elapsed); } goto done; diff --git a/src/sock.c b/src/sock.c index a02e496..15aef13 100644 --- a/src/sock.c +++ b/src/sock.c @@ -173,41 +173,6 @@ void patty_ax25_sock_destroy(patty_ax25_sock *sock) { free(sock); } -int patty_ax25_sock_timer_t1_expired(patty_ax25_sock *sock) { - return (sock->timer_t1.tv_sec <= 0 && sock->timer_t1.tv_usec == 0)? 1: 0; -} - -void patty_ax25_sock_timer_t1_cancel(patty_ax25_sock *sock) { - sock->timer_t1.tv_sec = 0; - sock->timer_t1.tv_usec = 0; -} - -/* - * AX.25 v2.2 Specification, Section 6.3.1 "AX.25 Link Connection Establishment" - */ -void patty_ax25_sock_timer_t1_start(patty_ax25_sock *sock, - struct timeval *timer) { - sock->timer_t1.tv_sec = sock->n_ack / 1000; - sock->timer_t1.tv_usec = (sock->n_ack % 1000) * 1000; - - if (timercmp(&sock->timer_t1, timer, >)) { - struct timeval res; - - timeradd(timer, &sock->timer_t1, &res); - - memcpy(timer, &res, sizeof(*timer)); - } -} - -void patty_ax25_sock_timer_t1_sub(patty_ax25_sock *sock, - struct timeval *elapsed) { - struct timeval res; - - timersub(&sock->timer_t1, elapsed, &res); - - memcpy(&sock->timer_t1, &res, sizeof(sock->timer_t1)); -} - /* * AX.25 v2.2 Specification, Section 6.5 "Resetting Procedure" */ diff --git a/src/timer.c b/src/timer.c new file mode 100644 index 0000000..58d390f --- /dev/null +++ b/src/timer.c @@ -0,0 +1,36 @@ +#include + +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; +}