From 6f96104a09a09b59d40fe12caa44e87e6827695d Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Mon, 10 Jul 2023 20:53:57 +0100 Subject: [PATCH] - replaced select by poll in linux hal (LIB61850-416)(#463) --- hal/serial/linux/serial_port_linux.c | 18 +++++++--------- hal/socket/linux/socket_linux.c | 32 +++++++++++----------------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/hal/serial/linux/serial_port_linux.c b/hal/serial/linux/serial_port_linux.c index f976e724..3bf4c974 100644 --- a/hal/serial/linux/serial_port_linux.c +++ b/hal/serial/linux/serial_port_linux.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "hal_serial.h" #include "hal_time.h" @@ -29,11 +30,10 @@ struct sSerialPort { char parity; uint8_t stopBits; uint64_t lastSentTime; - struct timeval timeout; + int timeout; SerialPortError lastError; }; - SerialPort SerialPort_create(const char* interfaceName, int baudRate, uint8_t dataBits, char parity, uint8_t stopBits) { @@ -46,8 +46,7 @@ SerialPort_create(const char* interfaceName, int baudRate, uint8_t dataBits, cha self->stopBits = stopBits; self->parity = parity; self->lastSentTime = 0; - self->timeout.tv_sec = 0; - self->timeout.tv_usec = 100000; /* 100 ms */ + self->timeout = 100; /* 100 ms */ strncpy(self->interfaceName, interfaceName, 99); self->lastError = SERIAL_PORT_ERROR_NONE; } @@ -212,8 +211,7 @@ SerialPort_discardInBuffer(SerialPort self) void SerialPort_setTimeout(SerialPort self, int timeout) { - self->timeout.tv_sec = timeout / 1000; - self->timeout.tv_usec = (timeout % 1000) * 1000; + self->timeout = timeout; } SerialPortError @@ -226,14 +224,14 @@ int SerialPort_readByte(SerialPort self) { uint8_t buf[1]; - fd_set set; + struct pollfd fds[1]; self->lastError = SERIAL_PORT_ERROR_NONE; - FD_ZERO(&set); - FD_SET(self->fd, &set); + fds[0].fd = self->fd; + fds[0].events = POLLIN; - int ret = select(self->fd + 1, &set, NULL, NULL, &(self->timeout)); + int ret = poll(fds, 1, self->timeout); if (ret == -1) { self->lastError = SERIAL_PORT_ERROR_UNKNOWN; diff --git a/hal/socket/linux/socket_linux.c b/hal/socket/linux/socket_linux.c index c188c24f..c939e2cb 100644 --- a/hal/socket/linux/socket_linux.c +++ b/hal/socket/linux/socket_linux.c @@ -478,10 +478,6 @@ Socket_connectAsync(Socket self, const char* address, int port) if (!prepareAddress(address, port, &serverAddress)) return false; - fd_set fdSet; - FD_ZERO(&fdSet); - FD_SET(self->fd, &fdSet); - activateTcpNoDelay(self); fcntl(self->fd, F_SETFL, O_NONBLOCK); @@ -505,17 +501,14 @@ Socket_connectAsync(Socket self, const char* address, int port) SocketState Socket_checkAsyncConnectState(Socket self) { - struct timeval timeout; - timeout.tv_sec = 0; - timeout.tv_usec = 0; + struct pollfd fds[1]; - fd_set fdSet; - FD_ZERO(&fdSet); - FD_SET(self->fd, &fdSet); + fds[0].fd = self->fd; + fds[0].events = POLLOUT; - int selectVal = select(self->fd + 1, NULL, &fdSet , NULL, &timeout); + int result = poll(fds, 1, 0); - if (selectVal == 1) { + if (result == 1) { /* Check if connection is established */ @@ -530,7 +523,7 @@ Socket_checkAsyncConnectState(Socket self) return SOCKET_STATE_FAILED; } - else if (selectVal == 0) { + else if (result == 0) { return SOCKET_STATE_CONNECTING; } else { @@ -544,15 +537,14 @@ Socket_connect(Socket self, const char* address, int port) if (Socket_connectAsync(self, address, port) == false) return false; - struct timeval timeout; - timeout.tv_sec = self->connectTimeout / 1000; - timeout.tv_usec = (self->connectTimeout % 1000) * 1000; + struct pollfd fds[1]; + + fds[0].fd = self->fd; + fds[0].events = POLLOUT; - fd_set fdSet; - FD_ZERO(&fdSet); - FD_SET(self->fd, &fdSet); + int result = poll(fds, 1, self->connectTimeout); - if (select(self->fd + 1, NULL, &fdSet , NULL, &timeout) == 1) { + if (result == 1) { /* Check if connection is established */