- replaced select by poll in linux hal (LIB61850-416)(#463)

v1.5
Michael Zillgith 2 years ago
parent d0e0dc22e9
commit 6f96104a09

@ -17,6 +17,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/select.h> #include <sys/select.h>
#include <poll.h>
#include "hal_serial.h" #include "hal_serial.h"
#include "hal_time.h" #include "hal_time.h"
@ -29,11 +30,10 @@ struct sSerialPort {
char parity; char parity;
uint8_t stopBits; uint8_t stopBits;
uint64_t lastSentTime; uint64_t lastSentTime;
struct timeval timeout; int timeout;
SerialPortError lastError; SerialPortError lastError;
}; };
SerialPort SerialPort
SerialPort_create(const char* interfaceName, int baudRate, uint8_t dataBits, char parity, uint8_t stopBits) 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->stopBits = stopBits;
self->parity = parity; self->parity = parity;
self->lastSentTime = 0; self->lastSentTime = 0;
self->timeout.tv_sec = 0; self->timeout = 100; /* 100 ms */
self->timeout.tv_usec = 100000; /* 100 ms */
strncpy(self->interfaceName, interfaceName, 99); strncpy(self->interfaceName, interfaceName, 99);
self->lastError = SERIAL_PORT_ERROR_NONE; self->lastError = SERIAL_PORT_ERROR_NONE;
} }
@ -212,8 +211,7 @@ SerialPort_discardInBuffer(SerialPort self)
void void
SerialPort_setTimeout(SerialPort self, int timeout) SerialPort_setTimeout(SerialPort self, int timeout)
{ {
self->timeout.tv_sec = timeout / 1000; self->timeout = timeout;
self->timeout.tv_usec = (timeout % 1000) * 1000;
} }
SerialPortError SerialPortError
@ -226,14 +224,14 @@ int
SerialPort_readByte(SerialPort self) SerialPort_readByte(SerialPort self)
{ {
uint8_t buf[1]; uint8_t buf[1];
fd_set set; struct pollfd fds[1];
self->lastError = SERIAL_PORT_ERROR_NONE; self->lastError = SERIAL_PORT_ERROR_NONE;
FD_ZERO(&set); fds[0].fd = self->fd;
FD_SET(self->fd, &set); 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) { if (ret == -1) {
self->lastError = SERIAL_PORT_ERROR_UNKNOWN; self->lastError = SERIAL_PORT_ERROR_UNKNOWN;

@ -478,10 +478,6 @@ Socket_connectAsync(Socket self, const char* address, int port)
if (!prepareAddress(address, port, &serverAddress)) if (!prepareAddress(address, port, &serverAddress))
return false; return false;
fd_set fdSet;
FD_ZERO(&fdSet);
FD_SET(self->fd, &fdSet);
activateTcpNoDelay(self); activateTcpNoDelay(self);
fcntl(self->fd, F_SETFL, O_NONBLOCK); fcntl(self->fd, F_SETFL, O_NONBLOCK);
@ -505,17 +501,14 @@ Socket_connectAsync(Socket self, const char* address, int port)
SocketState SocketState
Socket_checkAsyncConnectState(Socket self) Socket_checkAsyncConnectState(Socket self)
{ {
struct timeval timeout; struct pollfd fds[1];
timeout.tv_sec = 0;
timeout.tv_usec = 0;
fd_set fdSet; fds[0].fd = self->fd;
FD_ZERO(&fdSet); fds[0].events = POLLOUT;
FD_SET(self->fd, &fdSet);
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 */ /* Check if connection is established */
@ -530,7 +523,7 @@ Socket_checkAsyncConnectState(Socket self)
return SOCKET_STATE_FAILED; return SOCKET_STATE_FAILED;
} }
else if (selectVal == 0) { else if (result == 0) {
return SOCKET_STATE_CONNECTING; return SOCKET_STATE_CONNECTING;
} }
else { else {
@ -544,15 +537,14 @@ Socket_connect(Socket self, const char* address, int port)
if (Socket_connectAsync(self, address, port) == false) if (Socket_connectAsync(self, address, port) == false)
return false; return false;
struct timeval timeout; struct pollfd fds[1];
timeout.tv_sec = self->connectTimeout / 1000;
timeout.tv_usec = (self->connectTimeout % 1000) * 1000; fds[0].fd = self->fd;
fds[0].events = POLLOUT;
fd_set fdSet; int result = poll(fds, 1, self->connectTimeout);
FD_ZERO(&fdSet);
FD_SET(self->fd, &fdSet);
if (select(self->fd + 1, NULL, &fdSet , NULL, &timeout) == 1) { if (result == 1) {
/* Check if connection is established */ /* Check if connection is established */

Loading…
Cancel
Save