This commit is contained in:
Michael Farrell 2018-05-29 21:38:06 +00:00 committed by GitHub
commit 0f7e784ae7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 31 deletions

View file

@ -20,7 +20,7 @@
// Declare globals.
// Serial port that we are communicating with the PM3 on.
static serial_port sp;
static serial_port* sp = NULL;
// If TRUE, then there is no active connection to the PM3, and we will drop commands sent.
static bool offline;

View file

@ -69,11 +69,11 @@ 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.
*/
void uart_close(const serial_port sp);
void uart_close(serial_port* sp);
/* Reads from the given serial port for up to 30ms.
* pbtRx: A pointer to a buffer for the returned data to be written to.
@ -86,21 +86,21 @@ 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(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.
* szTxLen: The amount of data to be sent.
*/
bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen);
bool uart_send(serial_port* sp, const uint8_t* pbtTx, const size_t szTxLen);
/* Sets the current speed of the serial port, in baud.
*/
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed);
bool uart_set_speed(serial_port* sp, const uint32_t uiPortSpeed);
/* Gets the current speed of the serial port, in baud.
*/
uint32_t uart_get_speed(const serial_port sp);
uint32_t uart_get_speed(serial_port* sp);
#endif // _PM3_UART_H_

View file

@ -59,14 +59,14 @@ const struct timeval timeout = {
.tv_usec = 30000 // 30000 micro seconds
};
serial_port uart_open(const char* pcPortName)
serial_port* uart_open(const char* pcPortName)
{
serial_port_unix* sp = malloc(sizeof(serial_port_unix));
if (sp == 0) return INVALID_SERIAL_PORT;
sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if(sp->fd == -1) {
uart_close(sp);
uart_close((serial_port*)sp);
return INVALID_SERIAL_PORT;
}
@ -88,7 +88,7 @@ serial_port uart_open(const char* pcPortName)
// Try to retrieve the old (current) terminal info struct
if(tcgetattr(sp->fd,&sp->tiOld) == -1) {
uart_close(sp);
uart_close((serial_port*)sp);
return INVALID_SERIAL_PORT;
}
@ -108,17 +108,17 @@ serial_port uart_open(const char* pcPortName)
// Try to set the new terminal info struct
if(tcsetattr(sp->fd,TCSANOW,&sp->tiNew) == -1) {
uart_close(sp);
uart_close((serial_port*)sp);
return INVALID_SERIAL_PORT;
}
// Flush all lingering data that may exist
tcflush(sp->fd, TCIOFLUSH);
return sp;
return (serial_port*)sp;
}
void uart_close(const serial_port sp) {
void uart_close(serial_port* sp) {
serial_port_unix* spu = (serial_port_unix*)sp;
tcflush(spu->fd,TCIOFLUSH);
tcsetattr(spu->fd,TCSANOW,&(spu->tiOld));
@ -133,7 +133,7 @@ void uart_close(const serial_port sp) {
free(sp);
}
bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
bool uart_receive(serial_port* sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
int res;
int byteCount;
fd_set rfds;
@ -192,7 +192,7 @@ 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) {
bool uart_send(serial_port* sp, const uint8_t* pbtTx, const size_t szTxLen) {
int32_t res;
size_t szPos = 0;
fd_set rfds;
@ -226,8 +226,8 @@ 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;
bool uart_set_speed(serial_port* sp, const uint32_t uiPortSpeed) {
serial_port_unix* spu = (serial_port_unix*)sp;
speed_t stPortSpeed;
switch (uiPortSpeed) {
case 0: stPortSpeed = B0; break;
@ -270,10 +270,10 @@ 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(serial_port* sp) {
struct termios ti;
uint32_t uiPortSpeed;
const serial_port_unix* spu = (serial_port_unix*)sp;
serial_port_unix* spu = (serial_port_unix*)sp;
if (tcgetattr(spu->fd,&ti) == -1) return 0;
// Set port speed (Input)
speed_t stPortSpeed = cfgetispeed(&ti);

View file

@ -57,7 +57,7 @@ void upcase(char *p) {
}
}
serial_port uart_open(const char* pcPortName) {
serial_port* uart_open(const char* pcPortName) {
char acPortName[255];
serial_port_windows* sp = malloc(sizeof(serial_port_windows));
@ -68,7 +68,7 @@ serial_port uart_open(const char* pcPortName) {
// Try to open the serial port
sp->hPort = CreateFileA(acPortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if (sp->hPort == INVALID_HANDLE_VALUE) {
uart_close(sp);
uart_close((serial_port*)sp);
return INVALID_SERIAL_PORT;
}
@ -76,13 +76,13 @@ serial_port uart_open(const char* pcPortName) {
memset(&sp->dcb, 0, sizeof(DCB));
sp->dcb.DCBlength = sizeof(DCB);
if(!BuildCommDCBA("baud=9600 data=8 parity=N stop=1",&sp->dcb)) {
uart_close(sp);
uart_close((serial_port*)sp);
return INVALID_SERIAL_PORT;
}
// Update the active serial port
if(!SetCommState(sp->hPort,&sp->dcb)) {
uart_close(sp);
uart_close((serial_port*)sp);
return INVALID_SERIAL_PORT;
}
@ -93,38 +93,38 @@ serial_port uart_open(const char* pcPortName) {
sp->ct.WriteTotalTimeoutConstant = 30;
if(!SetCommTimeouts(sp->hPort,&sp->ct)) {
uart_close(sp);
uart_close((serial_port*)sp);
return INVALID_SERIAL_PORT;
}
PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
return sp;
return (serial_port*)sp;
}
void uart_close(const serial_port sp) {
void uart_close(serial_port* sp) {
CloseHandle(((serial_port_windows*)sp)->hPort);
free(sp);
}
bool uart_receive(const serial_port sp, uint8_t *pbtRx, size_t pszMaxRxLen, size_t *pszRxLen) {
bool uart_receive(serial_port* sp, uint8_t *pbtRx, size_t pszMaxRxLen, size_t *pszRxLen) {
return ReadFile(((serial_port_windows*)sp)->hPort, pbtRx, pszMaxRxLen, (LPDWORD)pszRxLen, NULL);
}
bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen) {
bool uart_send(serial_port* sp, const uint8_t* pbtTx, const size_t szTxLen) {
DWORD dwTxLen = 0;
return WriteFile(((serial_port_windows*)sp)->hPort, pbtTx, szTxLen, &dwTxLen, NULL);
}
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
bool uart_set_speed(serial_port* sp, const uint32_t uiPortSpeed) {
serial_port_windows* spw;
spw = (serial_port_windows*)sp;
spw->dcb.BaudRate = uiPortSpeed;
return SetCommState(spw->hPort, &spw->dcb);
}
uint32_t uart_get_speed(const serial_port sp) {
const serial_port_windows* spw = (serial_port_windows*)sp;
uint32_t uart_get_speed(serial_port* sp) {
serial_port_windows* spw = (serial_port_windows*)sp;
if (!GetCommState(spw->hPort, (serial_port)&spw->dcb)) {
return spw->dcb.BaudRate;
}