CHG: the thread comms refactoring from offical pm3 repo

chg: FPC com speed limited to 115200 when compiled with FPC
chg: USART remake (@drandreas)
This commit is contained in:
Chris 2018-09-06 21:43:20 +02:00
commit 24eaac8681
45 changed files with 915 additions and 949 deletions

View file

@ -41,6 +41,16 @@
#include "common.h"
#include "util_posix.h" // msclock
#if defined (_WIN32)
#define SERIAL_PORT_H "com3"
#elif defined(__APPLE__)
#define SERIAL_PORT_H "/dev/cu.usbmodem"
#else
#define SERIAL_PORT_H "/dev/ttyACM0"
#endif
/* serial_port is declared as a void*, which you should cast to whatever type
* makes sense to your connection method. Both the posix and win32
* implementations define their own structs in place.
@ -78,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, byte_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 byte_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

@ -117,10 +117,19 @@ serial_port uart_open(const char* pcPortName)
// Flush all lingering data that may exist
tcflush(sp->fd, TCIOFLUSH);
#ifdef WITH_FPC
uart_set_speed(sp, 115200);
printf("[=] UART Setting serial baudrate 115200 [FPC enabled]\n");
#else
// set speed, works for UBUNTU 14.04
bool err = uart_set_speed(sp, 460800);
if (!err)
uart_set_speed(sp, 115200);
bool success = uart_set_speed(sp, 460800);
if (success) {
printf("[=] UART Setting serial baudrate 460800\n");
} else {
uart_set_speed(sp, 115200);
printf("[=] UART Setting serial baudrate 115200\n");
}
#endif
return sp;
}
@ -144,7 +153,7 @@ void uart_close(const serial_port sp) {
free(sp);
}
bool uart_receive(const serial_port sp, byte_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;
@ -156,9 +165,9 @@ bool uart_receive(const serial_port sp, byte_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) {
@ -186,7 +195,7 @@ bool uart_receive(const serial_port sp, byte_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;
@ -203,7 +212,7 @@ bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t pszMaxRxLen, size_
return true;
}
bool uart_send(const serial_port sp, const byte_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;
@ -229,13 +238,10 @@ bool uart_send(const serial_port sp, const byte_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) {
printf("UART:: os troubles (%d)\n", res);
return false;
}
if (res <= 0) return false;
pos += res;
}

View file

@ -47,16 +47,6 @@ typedef struct {
DCB dcb; // Device control settings
COMMTIMEOUTS ct; // Serial port time-out configuration
} serial_port_windows;
/*
void upcase(char *p) {
while(*p != '\0') {
if(*p >= 97 && *p <= 122) {
*p -= 32;
}
++p;
}
}
*/
serial_port uart_open(const char* pcPortName) {
char acPortName[255];
@ -110,10 +100,17 @@ serial_port uart_open(const char* pcPortName) {
PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
#ifdef WITH_FPC
uart_set_speed(sp, 115200);
#else
bool success = uart_set_speed(sp, 460800);
if (!success)
if (success) {
printf("[=] UART Setting serial baudrate 460800\n");
} else {
uart_set_speed(sp, 115200);
printf("[=] UART Setting serial baudrate 115200\n");
}
#endif
return sp;
}
@ -155,32 +152,13 @@ uint32_t uart_get_speed(const serial_port sp) {
return 0;
}
bool uart_receive(const serial_port sp, byte_t* p_rx, size_t pszMaxRxLen, size_t* p_rxlen) {
int res = ReadFile(((serial_port_windows*)sp)->hPort, p_rx, pszMaxRxLen, (LPDWORD)p_rxlen, NULL);
if ( res == 0 ) {
//printf("[!] UART error reading from port\n");
return false;
}
bool read_test = ( pszMaxRxLen == *p_rxlen );
if ( !read_test && *p_rxlen > 0 ) {
printf("[!] UART error, not all data read from port len %u | read %u\n", pszMaxRxLen, *p_rxlen);
}
return read_test;
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 byte_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;
int res = WriteFile(((serial_port_windows*)sp)->hPort, p_tx, len, &txlen, NULL);
if ( res == 0) {
printf("[!] UART error writing to port\n");
return false;
}
bool write_test = ( len == txlen );
if ( !write_test ) {
printf("[!] UART error, not all data written to port len %u | sent %lu\n", len, txlen);
}
return write_test;
return WriteFile(((serial_port_windows*)sp)->hPort, p_tx, len, &txlen, NULL);
}
#endif