From 89661b471f6b0f15f5c67ac41587107cc3fd6283 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sat, 18 Nov 2017 15:01:16 +0100 Subject: [PATCH] add EthernetHandleSet implementation for BSD --- src/hal/ethernet/bsd/ethernet_bsd.c | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/hal/ethernet/bsd/ethernet_bsd.c b/src/hal/ethernet/bsd/ethernet_bsd.c index fe601996..4020d9d8 100644 --- a/src/hal/ethernet/bsd/ethernet_bsd.c +++ b/src/hal/ethernet/bsd/ethernet_bsd.c @@ -22,6 +22,7 @@ */ #include #include +#include #include #include #include @@ -47,6 +48,58 @@ struct sEthernetSocket { }; int _Ethernet_activateBpdFilter(EthernetSocket self) +struct sEthernetHandleSet { + fd_set handles; + int maxHandle; +}; + +EthernetHandleSet +EthernetHandleSet_new(void) +{ + EthernetHandleSet result = (EthernetHandleSet) GLOBAL_MALLOC(sizeof(struct sEthernetHandleSet)); + + if (result != NULL) { + FD_ZERO(&result->handles); + result->maxHandle = -1; + } + return result; +} + +void +EthernetHandleSet_addSocket(EthernetHandleSet self, const EthernetSocket sock) +{ + if (self != NULL && sock != NULL && sock->bpf != -1) { + FD_SET(sock->bpf, &self->handles); + if (sock->bpf > self->maxHandle) { + self->maxHandle = sock->bpf; + } + } +} + +int +EthernetHandleSet_waitReady(EthernetHandleSet self, unsigned int timeoutMs) +{ + int result; + + if ((self != NULL) && (self->maxHandle >= 0)) { + struct timeval timeout; + + timeout.tv_sec = timeoutMs / 1000; + timeout.tv_usec = (timeoutMs % 1000) * 1000; + result = select(self->maxHandle + 1, &self->handles, NULL, NULL, &timeout); + } else { + result = -1; + } + + return result; +} + +void +EthernetHandleSet_destroy(EthernetHandleSet self) +{ + GLOBAL_FREEMEM(self); +} + { return ioctl(self->bpf, BIOCSETF, &self->bpfProgram); }