From a744fa640c5bfc9eda37d1dcf9d18763076190a8 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 9 Aug 2015 09:50:12 -0500 Subject: [PATCH] Implement patty_ax25_next_event() like I'm not a dumbass or some shit --- include/patty/ax25.h | 28 ++++++++++++++++++------- src/ax25.c | 49 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/include/patty/ax25.h b/include/patty/ax25.h index 251c43c..4b58224 100644 --- a/include/patty/ax25.h +++ b/include/patty/ax25.h @@ -15,12 +15,15 @@ #include #include -enum patty_ax25_event { - PATTY_AX25_IO_UNKNOWN, - PATTY_AX25_IO_RECV, - PATTY_AX25_IO_SEND, - PATTY_AX25_IO_READ, - PATTY_AX25_IO_WRITE +#define PATTY_AX25_EVENT_QUEUE_SIZE 32 + +enum patty_ax25_event_type { + PATTY_AX25_EVENT_UNKNOWN, + PATTY_AX25_EVENT_RECV, + PATTY_AX25_EVENT_SEND, + PATTY_AX25_EVENT_READ, + PATTY_AX25_EVENT_WRITE, + PATTY_AX25_EVENT_ERROR }; enum patty_ax25_sock_status { @@ -55,6 +58,12 @@ typedef struct _patty_ax25_sock { patty_ax25_address * remote; } patty_ax25_sock; +typedef struct _patty_ax25_event { + enum patty_ax25_event_type type; + + int fd; +} patty_ax25_event; + struct _patty_ax25 { patty_list * ifaces; patty_list * socks; @@ -62,8 +71,13 @@ struct _patty_ax25 { int unix_fdmax; fd_set unix_fds; + fd_set unix_fds_pending_read; + fd_set unix_fds_pending_write; + fd_set unix_fds_pending_error; + patty_dict * fd_lookup; int fd; + int current; }; int patty_ax25_init(patty_ax25 *ax25); @@ -89,7 +103,7 @@ int patty_ax25_accept(patty_ax25 *ax25, int patty_ax25_close(patty_ax25 *ax25, int fd); int patty_ax25_next_event(patty_ax25 *ax25, - enum patty_ax25_event *ev); + patty_ax25_event *ev); int patty_ax25_recv(patty_ax25 *ax25, int fd, patty_ax25_frame *frame); diff --git a/src/ax25.c b/src/ax25.c index 664c4f0..7082d86 100644 --- a/src/ax25.c +++ b/src/ax25.c @@ -1,6 +1,7 @@ #include #include #include +#include #include @@ -19,8 +20,6 @@ int patty_ax25_init(patty_ax25 *ax25) { goto error_dict_new_fd_lookup; } - ax25->fd = 0; - return 0; error_dict_new_fd_lookup: @@ -188,6 +187,50 @@ error_fd_lookup: return -1; } -int patty_ax25_next_event(patty_ax25 *ax25, enum patty_ax25_event *ev) { +int patty_ax25_next_event(patty_ax25 *ax25, patty_ax25_event *ev) { + int fd; + + if (ax25->current == 0) { + memcpy(&ax25->unix_fds_pending_read, &ax25->unix_fds, sizeof(ax25->unix_fds)); + memcpy(&ax25->unix_fds_pending_write, &ax25->unix_fds, sizeof(ax25->unix_fds)); + memcpy(&ax25->unix_fds_pending_error, &ax25->unix_fds, sizeof(ax25->unix_fds)); + + if (select(ax25->unix_fdmax, + &ax25->unix_fds_pending_read, + &ax25->unix_fds_pending_write, + &ax25->unix_fds_pending_error, + NULL) < 0) { + goto error_select; + } + } + + for (fd=ax25->current; fdunix_fdmax; fd++) { + if (FD_ISSET(fd, &ax25->unix_fds_pending_read)) { + ev->type = PATTY_AX25_EVENT_RECV; + ev->fd = fd; + + return 0; + } + + if (FD_ISSET(fd, &ax25->unix_fds_pending_write)) { + ev->type = PATTY_AX25_EVENT_SEND; + ev->fd = fd; + + return 0; + } + + if (FD_ISSET(fd, &ax25->unix_fds_pending_error)) { + ev->type = PATTY_AX25_EVENT_ERROR; + ev->fd = fd; + + return 0; + } + } + + ax25->current = 0; + return 0; + +error_select: + return -1; }