mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-07-06 21:21:17 -07:00
cleaning up uart_posix.c
* whitespace fixes * sorting out #includes
This commit is contained in:
parent
ac37ee816b
commit
fd66752193
1 changed files with 389 additions and 391 deletions
66
uart/uart_posix.c
Normal file → Executable file
66
uart/uart_posix.c
Normal file → Executable 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,14 +104,15 @@ 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;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -388,4 +387,3 @@ uint32_t uart_get_speed(const serial_port sp) {
|
|||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue