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 "uart.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <limits.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
|
||||||
// Fix missing definition on OS X.
|
// Fix missing definition on OS X.
|
||||||
|
@ -69,13 +67,30 @@ typedef struct {
|
||||||
} serial_port_unix;
|
} serial_port_unix;
|
||||||
|
|
||||||
// Set time-out on 30 miliseconds
|
// Set time-out on 30 miliseconds
|
||||||
struct timeval timeout = {
|
static struct timeval timeout = {
|
||||||
.tv_sec = 0, // 0 second
|
.tv_sec = 0, // 0 second
|
||||||
.tv_usec = 30000 // 30000 micro seconds
|
.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));
|
serial_port_unix* sp = malloc(sizeof(serial_port_unix));
|
||||||
if (sp == 0) return INVALID_SERIAL_PORT;
|
if (sp == 0) return INVALID_SERIAL_PORT;
|
||||||
|
|
||||||
|
@ -89,14 +104,15 @@ serial_port uart_open(const char* pcPortName)
|
||||||
char *colon = strrchr(addrstr, ':');
|
char *colon = strrchr(addrstr, ':');
|
||||||
char *portstr;
|
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;
|
timeout.tv_usec = 300000;
|
||||||
|
|
||||||
if (colon) {
|
if (colon) {
|
||||||
portstr = colon + 1;
|
portstr = colon + 1;
|
||||||
*colon = '\0';
|
*colon = '\0';
|
||||||
} else
|
} else {
|
||||||
portstr = "7901";
|
portstr = "7901";
|
||||||
|
}
|
||||||
|
|
||||||
struct addrinfo info;
|
struct addrinfo info;
|
||||||
|
|
||||||
|
@ -112,14 +128,11 @@ serial_port uart_open(const char* pcPortName)
|
||||||
|
|
||||||
int sfd;
|
int sfd;
|
||||||
for (rp = addr; rp != NULL; rp = rp->ai_next) {
|
for (rp = addr; rp != NULL; rp = rp->ai_next) {
|
||||||
sfd = socket(rp->ai_family, rp->ai_socktype,
|
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||||
rp->ai_protocol);
|
|
||||||
if (sfd == -1)
|
if (sfd == -1)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
|
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
close(sfd);
|
close(sfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,23 +205,8 @@ serial_port uart_open(const char* pcPortName)
|
||||||
return sp;
|
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) {
|
bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
|
||||||
int res;
|
|
||||||
int byteCount;
|
int byteCount;
|
||||||
fd_set rfds;
|
fd_set rfds;
|
||||||
struct timeval tv;
|
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_ZERO(&rfds);
|
||||||
FD_SET(((serial_port_unix*)sp)->fd,&rfds);
|
FD_SET(((serial_port_unix*)sp)->fd,&rfds);
|
||||||
tv = timeout;
|
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
|
// Read error
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
|
@ -266,8 +264,8 @@ bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen) {
|
bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen) {
|
||||||
int32_t res;
|
|
||||||
size_t szPos = 0;
|
size_t szPos = 0;
|
||||||
fd_set rfds;
|
fd_set rfds;
|
||||||
struct timeval tv;
|
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_ZERO(&rfds);
|
||||||
FD_SET(((serial_port_unix*)sp)->fd,&rfds);
|
FD_SET(((serial_port_unix*)sp)->fd,&rfds);
|
||||||
tv = timeout;
|
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
|
// Write error
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
|
@ -300,6 +298,7 @@ bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
|
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
|
||||||
const serial_port_unix* spu = (serial_port_unix*)sp;
|
const serial_port_unix* spu = (serial_port_unix*)sp;
|
||||||
speed_t stPortSpeed;
|
speed_t stPortSpeed;
|
||||||
|
@ -388,4 +387,3 @@ uint32_t uart_get_speed(const serial_port sp) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue