mirror of
https://github.com/nfc-tools/libnfc.git
synced 2026-03-12 18:43:47 +00:00
astyle --formatted --mode=c --indent=spaces=2 --indent-switches --indent-preprocessor --keep-one-line-blocks --max-instatement-indent=60 --brackets=linux --pad-oper --unpad-paren --pad-header
This commit is contained in:
@@ -42,16 +42,16 @@ typedef void *serial_port;
|
||||
# define INVALID_SERIAL_PORT (void*)(~1)
|
||||
# define CLAIMED_SERIAL_PORT (void*)(~2)
|
||||
|
||||
serial_port uart_open (const char *pcPortName);
|
||||
void uart_close (const serial_port sp);
|
||||
void uart_flush_input (const serial_port sp);
|
||||
serial_port uart_open(const char *pcPortName);
|
||||
void uart_close(const serial_port sp);
|
||||
void uart_flush_input(const serial_port sp);
|
||||
|
||||
void uart_set_speed (serial_port sp, const uint32_t uiPortSpeed);
|
||||
uint32_t uart_get_speed (const serial_port sp);
|
||||
void uart_set_speed(serial_port sp, const uint32_t uiPortSpeed);
|
||||
uint32_t uart_get_speed(const serial_port sp);
|
||||
|
||||
int uart_receive (serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, int timeout);
|
||||
int uart_send (serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout);
|
||||
int uart_receive(serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, int timeout);
|
||||
int uart_send(serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout);
|
||||
|
||||
char **uart_list_ports (void);
|
||||
char **uart_list_ports(void);
|
||||
|
||||
#endif // __NFC_BUS_UART_H__
|
||||
|
||||
@@ -68,29 +68,29 @@ struct serial_port_unix {
|
||||
|
||||
#define UART_DATA( X ) ((struct serial_port_unix *) X)
|
||||
|
||||
void uart_close_ext (const serial_port sp, const bool restore_termios);
|
||||
void uart_close_ext(const serial_port sp, const bool restore_termios);
|
||||
|
||||
serial_port
|
||||
uart_open (const char *pcPortName)
|
||||
uart_open(const char *pcPortName)
|
||||
{
|
||||
struct serial_port_unix *sp = malloc (sizeof (struct serial_port_unix));
|
||||
struct serial_port_unix *sp = malloc(sizeof(struct serial_port_unix));
|
||||
|
||||
if (sp == 0)
|
||||
return INVALID_SERIAL_PORT;
|
||||
|
||||
sp->fd = open (pcPortName, O_RDWR | O_NOCTTY | O_NONBLOCK);
|
||||
sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NONBLOCK);
|
||||
if (sp->fd == -1) {
|
||||
uart_close_ext (sp, false);
|
||||
uart_close_ext(sp, false);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
if (tcgetattr (sp->fd, &sp->termios_backup) == -1) {
|
||||
uart_close_ext (sp, false);
|
||||
if (tcgetattr(sp->fd, &sp->termios_backup) == -1) {
|
||||
uart_close_ext(sp, false);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
// Make sure the port is not claimed already
|
||||
if (sp->termios_backup.c_iflag & CCLAIMED) {
|
||||
uart_close_ext (sp, false);
|
||||
uart_close_ext(sp, false);
|
||||
return CLAIMED_SERIAL_PORT;
|
||||
}
|
||||
// Copy the old terminal info struct
|
||||
@@ -104,40 +104,40 @@ uart_open (const char *pcPortName)
|
||||
sp->termios_new.c_cc[VMIN] = 0; // block until n bytes are received
|
||||
sp->termios_new.c_cc[VTIME] = 0; // block until a timer expires (n * 100 mSec.)
|
||||
|
||||
if (tcsetattr (sp->fd, TCSANOW, &sp->termios_new) == -1) {
|
||||
uart_close_ext (sp, true);
|
||||
if (tcsetattr(sp->fd, TCSANOW, &sp->termios_new) == -1) {
|
||||
uart_close_ext(sp, true);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
return sp;
|
||||
}
|
||||
|
||||
void
|
||||
uart_flush_input (serial_port sp)
|
||||
uart_flush_input(serial_port sp)
|
||||
{
|
||||
// This line seems to produce absolutely no effect on my system (GNU/Linux 2.6.35)
|
||||
tcflush (UART_DATA(sp)->fd, TCIFLUSH);
|
||||
tcflush(UART_DATA(sp)->fd, TCIFLUSH);
|
||||
// So, I wrote this byte-eater
|
||||
// Retrieve the count of the incoming bytes
|
||||
int available_bytes_count = 0;
|
||||
int res;
|
||||
res = ioctl (UART_DATA(sp)->fd, FIONREAD, &available_bytes_count);
|
||||
res = ioctl(UART_DATA(sp)->fd, FIONREAD, &available_bytes_count);
|
||||
if (res != 0) {
|
||||
return;
|
||||
}
|
||||
if (available_bytes_count == 0) {
|
||||
return;
|
||||
}
|
||||
char* rx = malloc (available_bytes_count);
|
||||
char* rx = malloc(available_bytes_count);
|
||||
// There is something available, read the data
|
||||
res = read (UART_DATA(sp)->fd, rx, available_bytes_count);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d bytes have eatten.", available_bytes_count);
|
||||
free (rx);
|
||||
res = read(UART_DATA(sp)->fd, rx, available_bytes_count);
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d bytes have eatten.", available_bytes_count);
|
||||
free(rx);
|
||||
}
|
||||
|
||||
void
|
||||
uart_set_speed (serial_port sp, const uint32_t uiPortSpeed)
|
||||
uart_set_speed(serial_port sp, const uint32_t uiPortSpeed)
|
||||
{
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Serial port speed requested to be set to %d bauds.", uiPortSpeed);
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Serial port speed requested to be set to %d bauds.", uiPortSpeed);
|
||||
|
||||
// Portability note: on some systems, B9600 != 9600 so we have to do
|
||||
// uint32_t <=> speed_t associations by hand.
|
||||
@@ -173,24 +173,24 @@ uart_set_speed (serial_port sp, const uint32_t uiPortSpeed)
|
||||
break;
|
||||
# endif
|
||||
default:
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set serial port speed to %d bauds. Speed value must be one of those defined in termios(3).",
|
||||
uiPortSpeed);
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set serial port speed to %d bauds. Speed value must be one of those defined in termios(3).",
|
||||
uiPortSpeed);
|
||||
return;
|
||||
};
|
||||
|
||||
// Set port speed (Input and Output)
|
||||
cfsetispeed (&(UART_DATA(sp)->termios_new), stPortSpeed);
|
||||
cfsetospeed (&(UART_DATA(sp)->termios_new), stPortSpeed);
|
||||
if (tcsetattr (UART_DATA(sp)->fd, TCSADRAIN, &(UART_DATA(sp)->termios_new)) == -1) {
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to apply new speed settings.");
|
||||
cfsetispeed(&(UART_DATA(sp)->termios_new), stPortSpeed);
|
||||
cfsetospeed(&(UART_DATA(sp)->termios_new), stPortSpeed);
|
||||
if (tcsetattr(UART_DATA(sp)->fd, TCSADRAIN, &(UART_DATA(sp)->termios_new)) == -1) {
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to apply new speed settings.");
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t
|
||||
uart_get_speed (serial_port sp)
|
||||
uart_get_speed(serial_port sp)
|
||||
{
|
||||
uint32_t uiPortSpeed = 0;
|
||||
switch (cfgetispeed (&UART_DATA(sp)->termios_new)) {
|
||||
switch (cfgetispeed(&UART_DATA(sp)->termios_new)) {
|
||||
case B9600:
|
||||
uiPortSpeed = 9600;
|
||||
break;
|
||||
@@ -226,20 +226,20 @@ uart_get_speed (serial_port sp)
|
||||
}
|
||||
|
||||
void
|
||||
uart_close_ext (const serial_port sp, const bool restore_termios)
|
||||
uart_close_ext(const serial_port sp, const bool restore_termios)
|
||||
{
|
||||
if (UART_DATA(sp)->fd >= 0) {
|
||||
if (restore_termios)
|
||||
tcsetattr (UART_DATA(sp)->fd, TCSANOW, &UART_DATA(sp)->termios_backup);
|
||||
close (UART_DATA(sp)->fd);
|
||||
tcsetattr(UART_DATA(sp)->fd, TCSANOW, &UART_DATA(sp)->termios_backup);
|
||||
close(UART_DATA(sp)->fd);
|
||||
}
|
||||
free (sp);
|
||||
free(sp);
|
||||
}
|
||||
|
||||
void
|
||||
uart_close (const serial_port sp)
|
||||
uart_close(const serial_port sp)
|
||||
{
|
||||
uart_close_ext (sp, true);
|
||||
uart_close_ext(sp, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -248,7 +248,7 @@ uart_close (const serial_port sp)
|
||||
* @return 0 on success, otherwise driver error code
|
||||
*/
|
||||
int
|
||||
uart_receive (serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, int timeout)
|
||||
uart_receive(serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, int timeout)
|
||||
{
|
||||
int iAbortFd = abort_p ? *((int*)abort_p) : 0;
|
||||
int received_bytes_count = 0;
|
||||
@@ -259,11 +259,11 @@ uart_receive (serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p,
|
||||
do {
|
||||
select:
|
||||
// Reset file descriptor
|
||||
FD_ZERO (&rfds);
|
||||
FD_SET (UART_DATA(sp)->fd, &rfds);
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(UART_DATA(sp)->fd, &rfds);
|
||||
|
||||
if (iAbortFd) {
|
||||
FD_SET (iAbortFd, &rfds);
|
||||
FD_SET(iAbortFd, &rfds);
|
||||
}
|
||||
|
||||
struct timeval timeout_tv;
|
||||
@@ -272,7 +272,7 @@ select:
|
||||
timeout_tv.tv_usec = ((timeout % 1000) * 1000);
|
||||
}
|
||||
|
||||
res = select (MAX(UART_DATA(sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, timeout ? &timeout_tv : NULL);
|
||||
res = select(MAX(UART_DATA(sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, timeout ? &timeout_tv : NULL);
|
||||
|
||||
if ((res < 0) && (EINTR == errno)) {
|
||||
// The system call was interupted by a signal and a signal handler was
|
||||
@@ -282,29 +282,29 @@ select:
|
||||
|
||||
// Read error
|
||||
if (res < 0) {
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Error: %s", strerror(errno));
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Error: %s", strerror(errno));
|
||||
return NFC_EIO;
|
||||
}
|
||||
// Read time-out
|
||||
if (res == 0) {
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "Timeout!");
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "Timeout!");
|
||||
return NFC_ETIMEOUT;
|
||||
}
|
||||
|
||||
if (FD_ISSET (iAbortFd, &rfds)) {
|
||||
if (FD_ISSET(iAbortFd, &rfds)) {
|
||||
// Abort requested
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "Abort!");
|
||||
close (iAbortFd);
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "Abort!");
|
||||
close(iAbortFd);
|
||||
return NFC_EOPABORTED;
|
||||
}
|
||||
|
||||
// Retrieve the count of the incoming bytes
|
||||
res = ioctl (UART_DATA(sp)->fd, FIONREAD, &available_bytes_count);
|
||||
res = ioctl(UART_DATA(sp)->fd, FIONREAD, &available_bytes_count);
|
||||
if (res != 0) {
|
||||
return NFC_EIO;
|
||||
}
|
||||
// There is something available, read the data
|
||||
res = read (UART_DATA(sp)->fd, pbtRx + received_bytes_count, MIN(available_bytes_count, (expected_bytes_count - received_bytes_count)));
|
||||
res = read(UART_DATA(sp)->fd, pbtRx + received_bytes_count, MIN(available_bytes_count, (expected_bytes_count - received_bytes_count)));
|
||||
// Stop if the OS has some troubles reading the data
|
||||
if (res <= 0) {
|
||||
return NFC_EIO;
|
||||
@@ -312,7 +312,7 @@ select:
|
||||
received_bytes_count += res;
|
||||
|
||||
} while (expected_bytes_count > received_bytes_count);
|
||||
LOG_HEX ("RX", pbtRx, szRx);
|
||||
LOG_HEX("RX", pbtRx, szRx);
|
||||
return NFC_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -322,20 +322,20 @@ select:
|
||||
* @return 0 on success, otherwise a driver error is returned
|
||||
*/
|
||||
int
|
||||
uart_send (serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout)
|
||||
uart_send(serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout)
|
||||
{
|
||||
(void) timeout;
|
||||
LOG_HEX ("TX", pbtTx, szTx);
|
||||
if ((int) szTx == write (UART_DATA(sp)->fd, pbtTx, szTx))
|
||||
LOG_HEX("TX", pbtTx, szTx);
|
||||
if ((int) szTx == write(UART_DATA(sp)->fd, pbtTx, szTx))
|
||||
return NFC_SUCCESS;
|
||||
else
|
||||
return NFC_EIO;
|
||||
}
|
||||
|
||||
char **
|
||||
uart_list_ports (void)
|
||||
uart_list_ports(void)
|
||||
{
|
||||
char **res = malloc (sizeof (char *));
|
||||
char **res = malloc(sizeof(char *));
|
||||
size_t szRes = 1;
|
||||
|
||||
res[0] = NULL;
|
||||
@@ -343,21 +343,21 @@ uart_list_ports (void)
|
||||
DIR *pdDir = opendir("/dev");
|
||||
struct dirent *pdDirEnt;
|
||||
while ((pdDirEnt = readdir(pdDir)) != NULL) {
|
||||
if (!isdigit (pdDirEnt->d_name[strlen (pdDirEnt->d_name) - 1]))
|
||||
if (!isdigit(pdDirEnt->d_name[strlen(pdDirEnt->d_name) - 1]))
|
||||
continue;
|
||||
|
||||
const char **p = serial_ports_device_radix;
|
||||
while (*p) {
|
||||
if (!strncmp(pdDirEnt->d_name, *p, strlen (*p))) {
|
||||
char **res2 = realloc (res, (szRes + 1) * sizeof (char *));
|
||||
if (!strncmp(pdDirEnt->d_name, *p, strlen(*p))) {
|
||||
char **res2 = realloc(res, (szRes + 1) * sizeof(char *));
|
||||
if (!res2)
|
||||
goto oom;
|
||||
|
||||
res = res2;
|
||||
if (!(res[szRes - 1] = malloc (6 + strlen (pdDirEnt->d_name))))
|
||||
if (!(res[szRes - 1] = malloc(6 + strlen(pdDirEnt->d_name))))
|
||||
goto oom;
|
||||
|
||||
sprintf (res[szRes - 1], "/dev/%s", pdDirEnt->d_name);
|
||||
sprintf(res[szRes - 1], "/dev/%s", pdDirEnt->d_name);
|
||||
|
||||
szRes++;
|
||||
res[szRes - 1] = NULL;
|
||||
@@ -366,7 +366,7 @@ uart_list_ports (void)
|
||||
}
|
||||
}
|
||||
oom:
|
||||
closedir (pdDir);
|
||||
closedir(pdDir);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -38,31 +38,31 @@ struct serial_port_windows {
|
||||
};
|
||||
|
||||
serial_port
|
||||
uart_open (const char *pcPortName)
|
||||
uart_open(const char *pcPortName)
|
||||
{
|
||||
char acPortName[255];
|
||||
struct serial_port_windows *sp = malloc (sizeof (struct serial_port_windows));
|
||||
struct serial_port_windows *sp = malloc(sizeof(struct serial_port_windows));
|
||||
|
||||
// Copy the input "com?" to "\\.\COM?" format
|
||||
sprintf (acPortName, "\\\\.\\%s", pcPortName);
|
||||
_strupr (acPortName);
|
||||
sprintf(acPortName, "\\\\.\\%s", pcPortName);
|
||||
_strupr(acPortName);
|
||||
|
||||
// Try to open the serial port
|
||||
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);
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
// Prepare the device control
|
||||
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);
|
||||
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);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
// Update the active serial port
|
||||
if (!SetCommState (sp->hPort, &sp->dcb)) {
|
||||
uart_close (sp);
|
||||
if (!SetCommState(sp->hPort, &sp->dcb)) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
@@ -72,37 +72,37 @@ uart_open (const char *pcPortName)
|
||||
sp->ct.WriteTotalTimeoutMultiplier = 30;
|
||||
sp->ct.WriteTotalTimeoutConstant = 0;
|
||||
|
||||
if (!SetCommTimeouts (sp->hPort, &sp->ct)) {
|
||||
uart_close (sp);
|
||||
if (!SetCommTimeouts(sp->hPort, &sp->ct)) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
PurgeComm (sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
||||
PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
||||
|
||||
return sp;
|
||||
}
|
||||
|
||||
void
|
||||
uart_close (const serial_port sp)
|
||||
uart_close(const serial_port sp)
|
||||
{
|
||||
if (((struct serial_port_windows *) sp)->hPort != INVALID_HANDLE_VALUE) {
|
||||
CloseHandle (((struct serial_port_windows *) sp)->hPort);
|
||||
CloseHandle(((struct serial_port_windows *) sp)->hPort);
|
||||
}
|
||||
free (sp);
|
||||
free(sp);
|
||||
}
|
||||
|
||||
void
|
||||
uart_flush_input (const serial_port sp)
|
||||
uart_flush_input(const serial_port sp)
|
||||
{
|
||||
PurgeComm(((struct serial_port_windows *) sp)->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
||||
}
|
||||
|
||||
void
|
||||
uart_set_speed (serial_port sp, const uint32_t uiPortSpeed)
|
||||
uart_set_speed(serial_port sp, const uint32_t uiPortSpeed)
|
||||
{
|
||||
struct serial_port_windows *spw;
|
||||
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Serial port speed requested to be set to %d bauds.", uiPortSpeed);
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Serial port speed requested to be set to %d bauds.", uiPortSpeed);
|
||||
// Set port speed (Input and Output)
|
||||
switch (uiPortSpeed) {
|
||||
case 9600:
|
||||
@@ -114,32 +114,32 @@ uart_set_speed (serial_port sp, const uint32_t uiPortSpeed)
|
||||
case 460800:
|
||||
break;
|
||||
default:
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set serial port speed to %d bauds. Speed value must be one of these constants: 9600 (default), 19200, 38400, 57600, 115200, 230400 or 460800.", uiPortSpeed);
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set serial port speed to %d bauds. Speed value must be one of these constants: 9600 (default), 19200, 38400, 57600, 115200, 230400 or 460800.", uiPortSpeed);
|
||||
return;
|
||||
};
|
||||
spw = (struct serial_port_windows *) sp;
|
||||
|
||||
// Set baud rate
|
||||
spw->dcb.BaudRate = uiPortSpeed;
|
||||
if (!SetCommState (spw->hPort, &spw->dcb)) {
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to apply new speed settings.");
|
||||
if (!SetCommState(spw->hPort, &spw->dcb)) {
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to apply new speed settings.");
|
||||
return;
|
||||
}
|
||||
PurgeComm (spw->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
||||
PurgeComm(spw->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
uart_get_speed (const serial_port sp)
|
||||
uart_get_speed(const serial_port sp)
|
||||
{
|
||||
const struct serial_port_windows *spw = (struct serial_port_windows *) sp;
|
||||
if (!GetCommState (spw->hPort, (serial_port) & spw->dcb))
|
||||
if (!GetCommState(spw->hPort, (serial_port) & spw->dcb))
|
||||
return spw->dcb.BaudRate;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, int timeout)
|
||||
uart_receive(serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, int timeout)
|
||||
{
|
||||
DWORD dwBytesToGet = (DWORD)szRx;
|
||||
DWORD dwBytesReceived = 0;
|
||||
@@ -155,26 +155,26 @@ uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p
|
||||
timeouts.WriteTotalTimeoutMultiplier = 0;
|
||||
timeouts.WriteTotalTimeoutConstant = timeout_ms;
|
||||
|
||||
if (!SetCommTimeouts (((struct serial_port_windows *) sp)->hPort, &timeouts)) {
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to apply new timeout settings.");
|
||||
if (!SetCommTimeouts(((struct serial_port_windows *) sp)->hPort, &timeouts)) {
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to apply new timeout settings.");
|
||||
return NFC_EIO;
|
||||
}
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Timeouts are set to %u ms", timeout_ms);
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Timeouts are set to %u ms", timeout_ms);
|
||||
|
||||
// TODO Enhance the reception method
|
||||
// - According to MSDN, it could be better to implement nfc_abort_command() mecanism using Cancello()
|
||||
volatile bool * abort_flag_p = (volatile bool *)abort_p;
|
||||
do {
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "ReadFile");
|
||||
res = ReadFile (((struct serial_port_windows *) sp)->hPort, pbtRx + dwTotalBytesReceived,
|
||||
dwBytesToGet,
|
||||
&dwBytesReceived, NULL);
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "ReadFile");
|
||||
res = ReadFile(((struct serial_port_windows *) sp)->hPort, pbtRx + dwTotalBytesReceived,
|
||||
dwBytesToGet,
|
||||
&dwBytesReceived, NULL);
|
||||
|
||||
dwTotalBytesReceived += dwBytesReceived;
|
||||
|
||||
if (!res) {
|
||||
DWORD err = GetLastError();
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "ReadFile error: %u", err);
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "ReadFile error: %u", err);
|
||||
return NFC_EIO;
|
||||
} else if (dwBytesReceived == 0) {
|
||||
return NFC_ETIMEOUT;
|
||||
@@ -188,13 +188,13 @@ uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p
|
||||
return NFC_EOPABORTED;
|
||||
}
|
||||
} while (((DWORD)szRx) > dwTotalBytesReceived);
|
||||
LOG_HEX ("RX", pbtRx, szRx);
|
||||
LOG_HEX("RX", pbtRx, szRx);
|
||||
|
||||
return (dwTotalBytesReceived == (DWORD) szRx) ? 0 : NFC_EIO;
|
||||
}
|
||||
|
||||
int
|
||||
uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, int timeout)
|
||||
uart_send(serial_port sp, const uint8_t * pbtTx, const size_t szTx, int timeout)
|
||||
{
|
||||
DWORD dwTxLen = 0;
|
||||
|
||||
@@ -205,13 +205,13 @@ uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, int timeout
|
||||
timeouts.WriteTotalTimeoutMultiplier = 0;
|
||||
timeouts.WriteTotalTimeoutConstant = timeout;
|
||||
|
||||
if (!SetCommTimeouts (((struct serial_port_windows *) sp)->hPort, &timeouts)) {
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to apply new timeout settings.");
|
||||
if (!SetCommTimeouts(((struct serial_port_windows *) sp)->hPort, &timeouts)) {
|
||||
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to apply new timeout settings.");
|
||||
return NFC_EIO;
|
||||
}
|
||||
|
||||
LOG_HEX ("TX", pbtTx, szTx);
|
||||
if (!WriteFile (((struct serial_port_windows *) sp)->hPort, pbtTx, szTx, &dwTxLen, NULL)) {
|
||||
LOG_HEX("TX", pbtTx, szTx);
|
||||
if (!WriteFile(((struct serial_port_windows *) sp)->hPort, pbtTx, szTx, &dwTxLen, NULL)) {
|
||||
return NFC_EIO;
|
||||
}
|
||||
if (!dwTxLen)
|
||||
@@ -236,7 +236,7 @@ BOOL is_port_available(int nPort)
|
||||
// Try to guess what we should use.
|
||||
#define MAX_SERIAL_PORT_WIN 255
|
||||
char **
|
||||
uart_list_ports (void)
|
||||
uart_list_ports(void)
|
||||
{
|
||||
char **availablePorts = malloc((1 + MAX_SERIAL_PORT_WIN) * sizeof(char*));
|
||||
int curIndex = 0;
|
||||
|
||||
Reference in New Issue
Block a user