cleaning up uart_posix.c

* whitespace fixes
* sorting out #includes
This commit is contained in:
pwpiwi 2020-01-24 03:24:39 -05:00
parent ac37ee816b
commit fd66752193

78
uart/uart_posix.c Normal file → Executable file
View file

@ -39,20 +39,18 @@
#include "uart.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <sys/time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
// Fix missing definition on OS X.
@ -69,13 +67,30 @@ typedef struct {
} serial_port_unix;
// Set time-out on 30 miliseconds
struct timeval timeout = {
static struct timeval timeout = {
.tv_sec = 0, // 0 second
.tv_usec = 30000 // 30000 micro seconds
};
serial_port uart_open(const char* pcPortName)
{
void uart_close(const serial_port sp) {
serial_port_unix* spu = (serial_port_unix*)sp;
tcflush(spu->fd,TCIOFLUSH);
tcsetattr(spu->fd,TCSANOW,&(spu->tiOld));
struct flock fl;
fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
fcntl(spu->fd, F_SETLK, &fl);
close(spu->fd);
free(sp);
}
serial_port uart_open(const char* pcPortName) {
serial_port_unix* sp = malloc(sizeof(serial_port_unix));
if (sp == 0) return INVALID_SERIAL_PORT;
@ -89,18 +104,19 @@ serial_port uart_open(const char* pcPortName)
char *colon = strrchr(addrstr, ':');
char *portstr;
// Set time-out to 300 miliseconds only for TCP port
// Set time-out to 300 milliseconds only for TCP port
timeout.tv_usec = 300000;
if (colon) {
portstr = colon + 1;
*colon = '\0';
} else
} else {
portstr = "7901";
}
struct addrinfo info;
memset (&info, 0, sizeof(info));
memset(&info, 0, sizeof(info));
info.ai_socktype = SOCK_STREAM;
@ -112,14 +128,11 @@ serial_port uart_open(const char* pcPortName)
int sfd;
for (rp = addr; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1)
continue;
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
break;
close(sfd);
}
@ -139,7 +152,7 @@ serial_port uart_open(const char* pcPortName)
}
sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if(sp->fd == -1) {
if (sp->fd == -1) {
uart_close(sp);
return INVALID_SERIAL_PORT;
}
@ -192,23 +205,8 @@ serial_port uart_open(const char* pcPortName)
return sp;
}
void uart_close(const serial_port sp) {
serial_port_unix* spu = (serial_port_unix*)sp;
tcflush(spu->fd,TCIOFLUSH);
tcsetattr(spu->fd,TCSANOW,&(spu->tiOld));
struct flock fl;
fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
fcntl(spu->fd, F_SETLK, &fl);
close(spu->fd);
free(sp);
}
bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
int res;
int byteCount;
fd_set rfds;
struct timeval tv;
@ -221,7 +219,7 @@ bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size
FD_ZERO(&rfds);
FD_SET(((serial_port_unix*)sp)->fd,&rfds);
tv = timeout;
res = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &tv);
int res = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &tv);
// Read error
if (res < 0) {
@ -266,8 +264,8 @@ bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size
return true;
}
bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen) {
int32_t res;
size_t szPos = 0;
fd_set rfds;
struct timeval tv;
@ -277,7 +275,7 @@ bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen)
FD_ZERO(&rfds);
FD_SET(((serial_port_unix*)sp)->fd,&rfds);
tv = timeout;
res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv);
int res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv);
// Write error
if (res < 0) {
@ -290,7 +288,7 @@ bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen)
}
// Send away the bytes
res = write(((serial_port_unix*)sp)->fd,pbtTx+szPos,szTxLen-szPos);
res = write(((serial_port_unix*)sp)->fd, pbtTx+szPos, szTxLen-szPos);
// Stop if the OS has some troubles sending the data
if (res <= 0) return false;
@ -300,6 +298,7 @@ bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen)
return true;
}
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
const serial_port_unix* spu = (serial_port_unix*)sp;
speed_t stPortSpeed;
@ -337,18 +336,18 @@ bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
default: return false;
};
struct termios ti;
if (tcgetattr(spu->fd,&ti) == -1) return false;
if (tcgetattr(spu->fd, &ti) == -1) return false;
// Set port speed (Input and Output)
cfsetispeed(&ti,stPortSpeed);
cfsetospeed(&ti,stPortSpeed);
return (tcsetattr(spu->fd,TCSANOW,&ti) != -1);
return (tcsetattr(spu->fd, TCSANOW, &ti) != -1);
}
uint32_t uart_get_speed(const serial_port sp) {
struct termios ti;
uint32_t uiPortSpeed;
const serial_port_unix* spu = (serial_port_unix*)sp;
if (tcgetattr(spu->fd,&ti) == -1) return 0;
if (tcgetattr(spu->fd, &ti) == -1) return 0;
// Set port speed (Input)
speed_t stPortSpeed = cfgetispeed(&ti);
switch (stPortSpeed) {
@ -388,4 +387,3 @@ uint32_t uart_get_speed(const serial_port sp) {
}
#endif