make style

This commit is contained in:
Philippe Teuwen 2019-03-10 00:00:59 +01:00
commit 0373696662
483 changed files with 56514 additions and 52451 deletions

View file

@ -55,7 +55,7 @@
* makes sense to your connection method. Both the posix and win32
* implementations define their own structs in place.
*/
typedef void* serial_port;
typedef void *serial_port;
/* Returned by uart_open if the serial port specified was invalid.
*/
@ -71,7 +71,7 @@ typedef void* serial_port;
*
* On errors, this method returns INVALID_SERIAL_PORT or CLAIMED_SERIAL_PORT.
*/
serial_port uart_open(const char* pcPortName);
serial_port uart_open(const char *pcPortName);
/* Closes the given port.
*/
@ -88,13 +88,13 @@ void uart_close(const serial_port sp);
* partial read may have completed into the buffer by the corresponding
* implementation, so pszRxLen should be checked to see if any data was written.
*/
bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen);
bool uart_receive(const serial_port sp, uint8_t *pbtRx, size_t pszMaxRxLen, size_t *pszRxLen);
/* Sends a buffer to a given serial port.
* pbtTx: A pointer to a buffer containing the data to send.
* len: The amount of data to be sent.
*/
bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t len);
bool uart_send(const serial_port sp, const uint8_t *pbtTx, const size_t len);
/* Sets the current speed of the serial port, in baud.
*/

View file

@ -73,8 +73,9 @@ struct timeval timeout = {
.tv_usec = 30000 // 30 000 micro seconds
};
serial_port uart_open(const char* pcPortName) {
serial_port_unix* sp = calloc(sizeof(serial_port_unix), sizeof(uint8_t));
serial_port uart_open(const char *pcPortName)
{
serial_port_unix *sp = calloc(sizeof(serial_port_unix), sizeof(uint8_t));
if (sp == 0) return INVALID_SERIAL_PORT;
if (memcmp(pcPortName, "tcp:", 4) == 0) {
@ -135,7 +136,7 @@ serial_port uart_open(const char* pcPortName) {
int one = 1;
int res = setsockopt(sp->fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
if ( res != 0) {
if (res != 0) {
free(sp);
return INVALID_SERIAL_PORT;
}
@ -166,7 +167,7 @@ serial_port uart_open(const char* pcPortName) {
}
// Try to retrieve the old (current) terminal info struct
if (tcgetattr(sp->fd,&sp->tiOld) == -1) {
if (tcgetattr(sp->fd, &sp->tiOld) == -1) {
uart_close(sp);
return INVALID_SERIAL_PORT;
}
@ -191,11 +192,11 @@ serial_port uart_open(const char* pcPortName) {
return INVALID_SERIAL_PORT;
}
// Flush all lingering data that may exist
tcflush(sp->fd, TCIOFLUSH);
// Flush all lingering data that may exist
tcflush(sp->fd, TCIOFLUSH);
#ifdef WITH_FPC
if ( uart_set_speed(sp, 115200) ) {
if (uart_set_speed(sp, 115200)) {
printf("[=] UART Setting serial baudrate 115200 [FPC enabled]\n");
} else {
uart_set_speed(sp, 9600);
@ -214,8 +215,9 @@ serial_port uart_open(const char* pcPortName) {
return sp;
}
void uart_close(const serial_port sp) {
serial_port_unix* spu = (serial_port_unix*)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;
@ -227,14 +229,15 @@ void uart_close(const serial_port sp) {
// Does the system allows us to place a lock on this file descriptor
int err = fcntl(spu->fd, F_SETLK, &fl);
if ( err == -1) {
if (err == -1) {
//perror("fcntl");
}
close(spu->fd);
free(sp);
}
bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
bool uart_receive(const serial_port sp, uint8_t *pbtRx, size_t pszMaxRxLen, size_t *pszRxLen)
{
int res;
int byteCount;
fd_set rfds;
@ -246,9 +249,9 @@ bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size
do {
// Reset file descriptor
FD_ZERO(&rfds);
FD_SET(((serial_port_unix*)sp)->fd, &rfds);
FD_SET(((serial_port_unix *)sp)->fd, &rfds);
tv = timeout;
res = select(((serial_port_unix*)sp)->fd + 1, &rfds, NULL, NULL, &tv);
res = select(((serial_port_unix *)sp)->fd + 1, &rfds, NULL, NULL, &tv);
// Read error
if (res < 0) {
@ -267,7 +270,7 @@ bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size
}
// Retrieve the count of the incoming bytes
res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);
res = ioctl(((serial_port_unix *)sp)->fd, FIONREAD, &byteCount);
if (res < 0) return false;
// Cap the number of bytes, so we don't overrun the buffer
@ -276,7 +279,7 @@ bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size
}
// There is something available, read the data
res = read(((serial_port_unix*)sp)->fd, pbtRx + (*pszRxLen), byteCount);
res = read(((serial_port_unix *)sp)->fd, pbtRx + (*pszRxLen), byteCount);
// Stop if the OS has some troubles reading the data
if (res <= 0) return false;
@ -293,7 +296,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 len) {
bool uart_send(const serial_port sp, const uint8_t *pbtTx, const size_t len)
{
int32_t res;
size_t pos = 0;
fd_set rfds;
@ -302,9 +306,9 @@ bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t len) {
while (pos < len) {
// Reset file descriptor
FD_ZERO(&rfds);
FD_SET(((serial_port_unix*)sp)->fd, &rfds);
FD_SET(((serial_port_unix *)sp)->fd, &rfds);
tv = timeout;
res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv);
res = select(((serial_port_unix *)sp)->fd + 1, NULL, &rfds, NULL, &tv);
// Write error
if (res < 0) {
@ -319,7 +323,7 @@ bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t len) {
}
// Send away the bytes
res = write(((serial_port_unix*)sp)->fd, pbtTx + pos, len - pos);
res = write(((serial_port_unix *)sp)->fd, pbtTx + pos, len - pos);
// Stop if the OS has some troubles sending the data
if (res <= 0) return false;
@ -329,45 +333,87 @@ bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t len) {
return true;
}
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
const serial_port_unix* spu = (serial_port_unix*)sp;
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed)
{
const serial_port_unix *spu = (serial_port_unix *)sp;
speed_t stPortSpeed;
switch (uiPortSpeed) {
case 0: stPortSpeed = B0; break;
case 50: stPortSpeed = B50; break;
case 75: stPortSpeed = B75; break;
case 110: stPortSpeed = B110; break;
case 134: stPortSpeed = B134; break;
case 150: stPortSpeed = B150; break;
case 300: stPortSpeed = B300; break;
case 600: stPortSpeed = B600; break;
case 1200: stPortSpeed = B1200; break;
case 1800: stPortSpeed = B1800; break;
case 2400: stPortSpeed = B2400; break;
case 4800: stPortSpeed = B4800; break;
case 9600: stPortSpeed = B9600; break;
case 19200: stPortSpeed = B19200; break;
case 38400: stPortSpeed = B38400; break;
case 0:
stPortSpeed = B0;
break;
case 50:
stPortSpeed = B50;
break;
case 75:
stPortSpeed = B75;
break;
case 110:
stPortSpeed = B110;
break;
case 134:
stPortSpeed = B134;
break;
case 150:
stPortSpeed = B150;
break;
case 300:
stPortSpeed = B300;
break;
case 600:
stPortSpeed = B600;
break;
case 1200:
stPortSpeed = B1200;
break;
case 1800:
stPortSpeed = B1800;
break;
case 2400:
stPortSpeed = B2400;
break;
case 4800:
stPortSpeed = B4800;
break;
case 9600:
stPortSpeed = B9600;
break;
case 19200:
stPortSpeed = B19200;
break;
case 38400:
stPortSpeed = B38400;
break;
# ifdef B57600
case 57600: stPortSpeed = B57600; break;
case 57600:
stPortSpeed = B57600;
break;
# endif
# ifdef B115200
case 115200: stPortSpeed = B115200; break;
case 115200:
stPortSpeed = B115200;
break;
# endif
# ifdef B230400
case 230400: stPortSpeed = B230400; break;
case 230400:
stPortSpeed = B230400;
break;
# endif
# ifdef B460800
case 460800: stPortSpeed = B460800; break;
case 460800:
stPortSpeed = B460800;
break;
# endif
# ifdef B921600
case 921600: stPortSpeed = B921600; break;
case 921600:
stPortSpeed = B921600;
break;
# endif
default: return false;
default:
return false;
};
struct termios ti;
if (tcgetattr(spu->fd,&ti) == -1)
if (tcgetattr(spu->fd, &ti) == -1)
return false;
// Set port speed (Input and Output)
@ -376,10 +422,11 @@ bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
return (tcsetattr(spu->fd, TCSANOW, &ti) != -1);
}
uint32_t uart_get_speed(const serial_port sp) {
uint32_t uart_get_speed(const serial_port sp)
{
struct termios ti;
uint32_t uiPortSpeed;
const serial_port_unix* spu = (serial_port_unix*)sp;
const serial_port_unix *spu = (serial_port_unix *)sp;
if (tcgetattr(spu->fd, &ti) == -1)
return 0;
@ -387,37 +434,78 @@ uint32_t uart_get_speed(const serial_port sp) {
// Set port speed (Input)
speed_t stPortSpeed = cfgetispeed(&ti);
switch (stPortSpeed) {
case B0: uiPortSpeed = 0; break;
case B50: uiPortSpeed = 50; break;
case B75: uiPortSpeed = 75; break;
case B110: uiPortSpeed = 110; break;
case B134: uiPortSpeed = 134; break;
case B150: uiPortSpeed = 150; break;
case B300: uiPortSpeed = 300; break;
case B600: uiPortSpeed = 600; break;
case B1200: uiPortSpeed = 1200; break;
case B1800: uiPortSpeed = 1800; break;
case B2400: uiPortSpeed = 2400; break;
case B4800: uiPortSpeed = 4800; break;
case B9600: uiPortSpeed = 9600; break;
case B19200: uiPortSpeed = 19200; break;
case B38400: uiPortSpeed = 38400; break;
case B0:
uiPortSpeed = 0;
break;
case B50:
uiPortSpeed = 50;
break;
case B75:
uiPortSpeed = 75;
break;
case B110:
uiPortSpeed = 110;
break;
case B134:
uiPortSpeed = 134;
break;
case B150:
uiPortSpeed = 150;
break;
case B300:
uiPortSpeed = 300;
break;
case B600:
uiPortSpeed = 600;
break;
case B1200:
uiPortSpeed = 1200;
break;
case B1800:
uiPortSpeed = 1800;
break;
case B2400:
uiPortSpeed = 2400;
break;
case B4800:
uiPortSpeed = 4800;
break;
case B9600:
uiPortSpeed = 9600;
break;
case B19200:
uiPortSpeed = 19200;
break;
case B38400:
uiPortSpeed = 38400;
break;
# ifdef B57600
case B57600: uiPortSpeed = 57600; break;
case B57600:
uiPortSpeed = 57600;
break;
# endif
# ifdef B115200
case B115200: uiPortSpeed = 115200; break;
case B115200:
uiPortSpeed = 115200;
break;
# endif
# ifdef B230400
case B230400: uiPortSpeed = 230400; break;
case B230400:
uiPortSpeed = 230400;
break;
# endif
# ifdef B460800
case B460800: uiPortSpeed = 460800; break;
case B460800:
uiPortSpeed = 460800;
break;
# endif
# ifdef B921600
case B921600: uiPortSpeed = 921600; break;
case B921600:
uiPortSpeed = 921600;
break;
# endif
default: return 0;
default:
return 0;
};
return uiPortSpeed;
}

View file

@ -43,26 +43,27 @@
#include <windows.h>
typedef struct {
HANDLE hPort; // Serial port handle
DCB dcb; // Device control settings
COMMTIMEOUTS ct; // Serial port time-out configuration
HANDLE hPort; // Serial port handle
DCB dcb; // Device control settings
COMMTIMEOUTS ct; // Serial port time-out configuration
} serial_port_windows;
serial_port uart_open(const char* pcPortName) {
serial_port uart_open(const char *pcPortName)
{
char acPortName[255];
serial_port_windows* sp = calloc(sizeof(serial_port_windows), sizeof(uint8_t));
serial_port_windows *sp = calloc(sizeof(serial_port_windows), sizeof(uint8_t));
if (sp == 0) {
printf("[!] UART failed to allocate memory\n");
return INVALID_SERIAL_PORT;
}
// Copy the input "com?" to "\\.\COM?" format
sprintf(acPortName,"\\\\.\\%s", pcPortName);
sprintf(acPortName, "\\\\.\\%s", pcPortName);
_strupr(acPortName);
// Try to open the serial port
// r/w, none-share comport, no security, existing, no overlapping, no templates
sp->hPort = CreateFileA(acPortName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
sp->hPort = CreateFileA(acPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (sp->hPort == INVALID_HANDLE_VALUE) {
uart_close(sp);
return INVALID_SERIAL_PORT;
@ -109,7 +110,7 @@ serial_port uart_open(const char* pcPortName) {
PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
#ifdef WITH_FPC
if ( uart_set_speed(sp, 115200) ) {
if (uart_set_speed(sp, 115200)) {
printf("[=] UART Setting serial baudrate 115200 [FPC enabled]\n");
} else {
uart_set_speed(sp, 9600);
@ -127,14 +128,16 @@ serial_port uart_open(const char* pcPortName) {
return sp;
}
void uart_close(const serial_port sp) {
if (((serial_port_windows*)sp)->hPort != INVALID_HANDLE_VALUE )
CloseHandle(((serial_port_windows*)sp)->hPort);
void uart_close(const serial_port sp)
{
if (((serial_port_windows *)sp)->hPort != INVALID_HANDLE_VALUE)
CloseHandle(((serial_port_windows *)sp)->hPort);
free(sp);
}
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
serial_port_windows* spw;
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed)
{
serial_port_windows *spw;
// Set port speed (Input and Output)
switch (uiPortSpeed) {
@ -145,33 +148,36 @@ bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
case 115200:
case 230400:
case 460800:
break;
break;
default:
return false;
};
spw = (serial_port_windows*)sp;
spw = (serial_port_windows *)sp;
spw->dcb.BaudRate = uiPortSpeed;
bool result = SetCommState(spw->hPort, &spw->dcb);
PurgeComm(spw->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
return result;
}
uint32_t uart_get_speed(const serial_port sp) {
const serial_port_windows* spw = (serial_port_windows*)sp;
uint32_t uart_get_speed(const serial_port sp)
{
const serial_port_windows *spw = (serial_port_windows *)sp;
if (!GetCommState(spw->hPort, (serial_port) & spw->dcb))
return spw->dcb.BaudRate;
return 0;
}
bool uart_receive(const serial_port sp, uint8_t* p_rx, size_t pszMaxRxLen, size_t* len) {
return ReadFile(((serial_port_windows*)sp)->hPort, p_rx, pszMaxRxLen, (LPDWORD)len, NULL);
bool uart_receive(const serial_port sp, uint8_t *p_rx, size_t pszMaxRxLen, size_t *len)
{
return ReadFile(((serial_port_windows *)sp)->hPort, p_rx, pszMaxRxLen, (LPDWORD)len, NULL);
}
bool uart_send(const serial_port sp, const uint8_t* p_tx, const size_t len) {
bool uart_send(const serial_port sp, const uint8_t *p_tx, const size_t len)
{
DWORD txlen = 0;
return WriteFile(((serial_port_windows*)sp)->hPort, p_tx, len, &txlen, NULL);
return WriteFile(((serial_port_windows *)sp)->hPort, p_tx, len, &txlen, NULL);
}
#endif