[winpr,comm] cleanup comm_serial_sys

* WINPR_ASSERT arguments
* remove use of restricted keywords (variables/functions starting with _)
This commit is contained in:
akallabeth 2024-08-22 11:18:29 +02:00
parent 652c5310f2
commit 2a7572e6b3
No known key found for this signature in database
GPG Key ID: A49454A3FC909FD5

View File

@ -156,8 +156,9 @@ static const speed_t _BAUD_TABLE[][3] = {
{ BAUD_TABLE_END, 0, 0 } { BAUD_TABLE_END, 0, 0 }
}; };
static BOOL _get_properties(WINPR_COMM* pComm, COMMPROP* pProperties) static BOOL get_properties(WINPR_COMM* pComm, COMMPROP* pProperties)
{ {
WINPR_ASSERT(pComm);
/* http://msdn.microsoft.com/en-us/library/windows/hardware/jj680684%28v=vs.85%29.aspx /* http://msdn.microsoft.com/en-us/library/windows/hardware/jj680684%28v=vs.85%29.aspx
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa363189%28v=vs.85%29.aspx * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363189%28v=vs.85%29.aspx
*/ */
@ -220,12 +221,14 @@ static BOOL _get_properties(WINPR_COMM* pComm, COMMPROP* pProperties)
return TRUE; return TRUE;
} }
static BOOL _set_baud_rate(WINPR_COMM* pComm, const SERIAL_BAUD_RATE* pBaudRate) static BOOL set_baud_rate(WINPR_COMM* pComm, const SERIAL_BAUD_RATE* pBaudRate)
{ {
speed_t newSpeed = 0; speed_t newSpeed = 0;
struct termios futureState; struct termios futureState = { 0 };
WINPR_ASSERT(pComm);
WINPR_ASSERT(pBaudRate);
ZeroMemory(&futureState, sizeof(struct termios));
if (tcgetattr(pComm->fd, &futureState) < if (tcgetattr(pComm->fd, &futureState) <
0) /* NB: preserves current settings not directly handled by the Communication Functions */ 0) /* NB: preserves current settings not directly handled by the Communication Functions */
{ {
@ -264,12 +267,14 @@ static BOOL _set_baud_rate(WINPR_COMM* pComm, const SERIAL_BAUD_RATE* pBaudRate)
return FALSE; return FALSE;
} }
static BOOL _get_baud_rate(WINPR_COMM* pComm, SERIAL_BAUD_RATE* pBaudRate) static BOOL get_baud_rate(WINPR_COMM* pComm, SERIAL_BAUD_RATE* pBaudRate)
{ {
speed_t currentSpeed = 0; speed_t currentSpeed = 0;
struct termios currentState; struct termios currentState = { 0 };
WINPR_ASSERT(pComm);
WINPR_ASSERT(pBaudRate);
ZeroMemory(&currentState, sizeof(struct termios));
if (tcgetattr(pComm->fd, &currentState) < 0) if (tcgetattr(pComm->fd, &currentState) < 0)
{ {
SetLastError(ERROR_IO_DEVICE); SetLastError(ERROR_IO_DEVICE);
@ -302,25 +307,20 @@ static BOOL _get_baud_rate(WINPR_COMM* pComm, SERIAL_BAUD_RATE* pBaudRate)
* ERROR_INVALID_PARAMETER when Xon and Xoff chars are the same; * ERROR_INVALID_PARAMETER when Xon and Xoff chars are the same;
* ERROR_NOT_SUPPORTED * ERROR_NOT_SUPPORTED
*/ */
static BOOL _set_serial_chars(WINPR_COMM* pComm, const SERIAL_CHARS* pSerialChars) static BOOL set_serial_chars(WINPR_COMM* pComm, const SERIAL_CHARS* pSerialChars)
{ {
BOOL result = TRUE; BOOL result = TRUE;
struct termios upcomingTermios; struct termios upcomingTermios = { 0 };
WINPR_ASSERT(pComm);
WINPR_ASSERT(pSerialChars);
ZeroMemory(&upcomingTermios, sizeof(struct termios));
if (tcgetattr(pComm->fd, &upcomingTermios) < 0) if (tcgetattr(pComm->fd, &upcomingTermios) < 0)
{ {
SetLastError(ERROR_IO_DEVICE); SetLastError(ERROR_IO_DEVICE);
return FALSE; return FALSE;
} }
if (pSerialChars->XonChar == pSerialChars->XoffChar)
{
/* https://msdn.microsoft.com/en-us/library/windows/hardware/ff546688?v=vs.85.aspx */
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
/* termios(3): (..) above symbolic subscript values are all /* termios(3): (..) above symbolic subscript values are all
* different, except that VTIME, VMIN may have the same value * different, except that VTIME, VMIN may have the same value
* as VEOL, VEOF, respectively. In noncanonical mode the * as VEOL, VEOF, respectively. In noncanonical mode the
@ -344,7 +344,7 @@ static BOOL _set_serial_chars(WINPR_COMM* pComm, const SERIAL_CHARS* pSerialChar
* get the prefix the prefix \377 \0 * get the prefix the prefix \377 \0
*/ */
/* FIXME: see also: _set_handflow() */ /* FIXME: see also: set_handflow() */
if (pSerialChars->ErrorChar != '\0') if (pSerialChars->ErrorChar != '\0')
{ {
CommLog_Print(WLOG_WARN, "ErrorChar 0x%02" PRIX8 " ('%c') cannot be set (unsupported).\n", CommLog_Print(WLOG_WARN, "ErrorChar 0x%02" PRIX8 " ('%c') cannot be set (unsupported).\n",
@ -353,7 +353,7 @@ static BOOL _set_serial_chars(WINPR_COMM* pComm, const SERIAL_CHARS* pSerialChar
result = FALSE; /* but keep on */ result = FALSE; /* but keep on */
} }
/* FIXME: see also: _set_handflow() */ /* FIXME: see also: set_handflow() */
if (pSerialChars->BreakChar != '\0') if (pSerialChars->BreakChar != '\0')
{ {
CommLog_Print(WLOG_WARN, "BreakChar 0x%02" PRIX8 " ('%c') cannot be set (unsupported).\n", CommLog_Print(WLOG_WARN, "BreakChar 0x%02" PRIX8 " ('%c') cannot be set (unsupported).\n",
@ -381,11 +381,13 @@ static BOOL _set_serial_chars(WINPR_COMM* pComm, const SERIAL_CHARS* pSerialChar
return result; return result;
} }
static BOOL _get_serial_chars(WINPR_COMM* pComm, SERIAL_CHARS* pSerialChars) static BOOL get_serial_chars(WINPR_COMM* pComm, SERIAL_CHARS* pSerialChars)
{ {
struct termios currentTermios; struct termios currentTermios = { 0 };
WINPR_ASSERT(pComm);
WINPR_ASSERT(pSerialChars);
ZeroMemory(&currentTermios, sizeof(struct termios));
if (tcgetattr(pComm->fd, &currentTermios) < 0) if (tcgetattr(pComm->fd, &currentTermios) < 0)
{ {
SetLastError(ERROR_IO_DEVICE); SetLastError(ERROR_IO_DEVICE);
@ -400,7 +402,7 @@ static BOOL _get_serial_chars(WINPR_COMM* pComm, SERIAL_CHARS* pSerialChars)
/* BreakChar unsupported */ /* BreakChar unsupported */
/* FIXME: see also: _set_serial_chars() */ /* FIXME: see also: set_serial_chars() */
/* EventChar */ /* EventChar */
pSerialChars->XonChar = currentTermios.c_cc[VSTART]; pSerialChars->XonChar = currentTermios.c_cc[VSTART];
@ -410,10 +412,13 @@ static BOOL _get_serial_chars(WINPR_COMM* pComm, SERIAL_CHARS* pSerialChars)
return TRUE; return TRUE;
} }
static BOOL _set_line_control(WINPR_COMM* pComm, const SERIAL_LINE_CONTROL* pLineControl) static BOOL set_line_control(WINPR_COMM* pComm, const SERIAL_LINE_CONTROL* pLineControl)
{ {
BOOL result = TRUE; BOOL result = TRUE;
struct termios upcomingTermios; struct termios upcomingTermios = { 0 };
WINPR_ASSERT(pComm);
WINPR_ASSERT(pLineControl);
/* http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214%28v=vs.85%29.aspx /* http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214%28v=vs.85%29.aspx
* *
@ -424,7 +429,6 @@ static BOOL _set_line_control(WINPR_COMM* pComm, const SERIAL_LINE_CONTROL* pLin
* this issue. At least produce a warning message? * this issue. At least produce a warning message?
*/ */
ZeroMemory(&upcomingTermios, sizeof(struct termios));
if (tcgetattr(pComm->fd, &upcomingTermios) < 0) if (tcgetattr(pComm->fd, &upcomingTermios) < 0)
{ {
SetLastError(ERROR_IO_DEVICE); SetLastError(ERROR_IO_DEVICE);
@ -525,11 +529,13 @@ static BOOL _set_line_control(WINPR_COMM* pComm, const SERIAL_LINE_CONTROL* pLin
return result; return result;
} }
static BOOL _get_line_control(WINPR_COMM* pComm, SERIAL_LINE_CONTROL* pLineControl) static BOOL get_line_control(WINPR_COMM* pComm, SERIAL_LINE_CONTROL* pLineControl)
{ {
struct termios currentTermios; struct termios currentTermios = { 0 };
WINPR_ASSERT(pComm);
WINPR_ASSERT(pLineControl);
ZeroMemory(&currentTermios, sizeof(struct termios));
if (tcgetattr(pComm->fd, &currentTermios) < 0) if (tcgetattr(pComm->fd, &currentTermios) < 0)
{ {
SetLastError(ERROR_IO_DEVICE); SetLastError(ERROR_IO_DEVICE);
@ -571,12 +577,14 @@ static BOOL _get_line_control(WINPR_COMM* pComm, SERIAL_LINE_CONTROL* pLineContr
return TRUE; return TRUE;
} }
static BOOL _set_handflow(WINPR_COMM* pComm, const SERIAL_HANDFLOW* pHandflow) static BOOL set_handflow(WINPR_COMM* pComm, const SERIAL_HANDFLOW* pHandflow)
{ {
BOOL result = TRUE; BOOL result = TRUE;
struct termios upcomingTermios; struct termios upcomingTermios = { 0 };
WINPR_ASSERT(pComm);
WINPR_ASSERT(pHandflow);
ZeroMemory(&upcomingTermios, sizeof(struct termios));
if (tcgetattr(pComm->fd, &upcomingTermios) < 0) if (tcgetattr(pComm->fd, &upcomingTermios) < 0)
{ {
SetLastError(ERROR_IO_DEVICE); SetLastError(ERROR_IO_DEVICE);
@ -771,11 +779,13 @@ static BOOL _set_handflow(WINPR_COMM* pComm, const SERIAL_HANDFLOW* pHandflow)
return result; return result;
} }
static BOOL _get_handflow(WINPR_COMM* pComm, SERIAL_HANDFLOW* pHandflow) static BOOL get_handflow(WINPR_COMM* pComm, SERIAL_HANDFLOW* pHandflow)
{ {
struct termios currentTermios; struct termios currentTermios = { 0 };
WINPR_ASSERT(pComm);
WINPR_ASSERT(pHandflow);
ZeroMemory(&currentTermios, sizeof(struct termios));
if (tcgetattr(pComm->fd, &currentTermios) < 0) if (tcgetattr(pComm->fd, &currentTermios) < 0)
{ {
SetLastError(ERROR_IO_DEVICE); SetLastError(ERROR_IO_DEVICE);
@ -839,8 +849,11 @@ static BOOL _get_handflow(WINPR_COMM* pComm, SERIAL_HANDFLOW* pHandflow)
return TRUE; return TRUE;
} }
static BOOL _set_timeouts(WINPR_COMM* pComm, const SERIAL_TIMEOUTS* pTimeouts) static BOOL set_timeouts(WINPR_COMM* pComm, const SERIAL_TIMEOUTS* pTimeouts)
{ {
WINPR_ASSERT(pComm);
WINPR_ASSERT(pTimeouts);
/* NB: timeouts are applied on system during read/write I/O */ /* NB: timeouts are applied on system during read/write I/O */
/* http://msdn.microsoft.com/en-us/library/windows/hardware/hh439614%28v=vs.85%29.aspx */ /* http://msdn.microsoft.com/en-us/library/windows/hardware/hh439614%28v=vs.85%29.aspx */
@ -874,8 +887,11 @@ static BOOL _set_timeouts(WINPR_COMM* pComm, const SERIAL_TIMEOUTS* pTimeouts)
return TRUE; return TRUE;
} }
static BOOL _get_timeouts(WINPR_COMM* pComm, SERIAL_TIMEOUTS* pTimeouts) static BOOL get_timeouts(WINPR_COMM* pComm, SERIAL_TIMEOUTS* pTimeouts)
{ {
WINPR_ASSERT(pComm);
WINPR_ASSERT(pTimeouts);
pTimeouts->ReadIntervalTimeout = pComm->timeouts.ReadIntervalTimeout; pTimeouts->ReadIntervalTimeout = pComm->timeouts.ReadIntervalTimeout;
pTimeouts->ReadTotalTimeoutMultiplier = pComm->timeouts.ReadTotalTimeoutMultiplier; pTimeouts->ReadTotalTimeoutMultiplier = pComm->timeouts.ReadTotalTimeoutMultiplier;
pTimeouts->ReadTotalTimeoutConstant = pComm->timeouts.ReadTotalTimeoutConstant; pTimeouts->ReadTotalTimeoutConstant = pComm->timeouts.ReadTotalTimeoutConstant;
@ -885,8 +901,10 @@ static BOOL _get_timeouts(WINPR_COMM* pComm, SERIAL_TIMEOUTS* pTimeouts)
return TRUE; return TRUE;
} }
static BOOL _set_lines(WINPR_COMM* pComm, UINT32 lines) static BOOL set_lines(WINPR_COMM* pComm, UINT32 lines)
{ {
WINPR_ASSERT(pComm);
if (ioctl(pComm->fd, TIOCMBIS, &lines) < 0) if (ioctl(pComm->fd, TIOCMBIS, &lines) < 0)
{ {
char ebuffer[256] = { 0 }; char ebuffer[256] = { 0 };
@ -899,8 +917,10 @@ static BOOL _set_lines(WINPR_COMM* pComm, UINT32 lines)
return TRUE; return TRUE;
} }
static BOOL _clear_lines(WINPR_COMM* pComm, UINT32 lines) static BOOL clear_lines(WINPR_COMM* pComm, UINT32 lines)
{ {
WINPR_ASSERT(pComm);
if (ioctl(pComm->fd, TIOCMBIC, &lines) < 0) if (ioctl(pComm->fd, TIOCMBIC, &lines) < 0)
{ {
char ebuffer[256] = { 0 }; char ebuffer[256] = { 0 };
@ -913,10 +933,12 @@ static BOOL _clear_lines(WINPR_COMM* pComm, UINT32 lines)
return TRUE; return TRUE;
} }
static BOOL _set_dtr(WINPR_COMM* pComm) static BOOL set_dtr(WINPR_COMM* pComm)
{ {
SERIAL_HANDFLOW handflow; SERIAL_HANDFLOW handflow = { 0 };
if (!_get_handflow(pComm, &handflow)) WINPR_ASSERT(pComm);
if (!get_handflow(pComm, &handflow))
return FALSE; return FALSE;
/* SERIAL_DTR_HANDSHAKE not supported as of today */ /* SERIAL_DTR_HANDSHAKE not supported as of today */
@ -928,13 +950,15 @@ static BOOL _set_dtr(WINPR_COMM* pComm)
return FALSE; return FALSE;
} }
return _set_lines(pComm, TIOCM_DTR); return set_lines(pComm, TIOCM_DTR);
} }
static BOOL _clear_dtr(WINPR_COMM* pComm) static BOOL clear_dtr(WINPR_COMM* pComm)
{ {
SERIAL_HANDFLOW handflow; SERIAL_HANDFLOW handflow = { 0 };
if (!_get_handflow(pComm, &handflow)) WINPR_ASSERT(pComm);
if (!get_handflow(pComm, &handflow))
return FALSE; return FALSE;
/* SERIAL_DTR_HANDSHAKE not supported as of today */ /* SERIAL_DTR_HANDSHAKE not supported as of today */
@ -946,13 +970,15 @@ static BOOL _clear_dtr(WINPR_COMM* pComm)
return FALSE; return FALSE;
} }
return _clear_lines(pComm, TIOCM_DTR); return clear_lines(pComm, TIOCM_DTR);
} }
static BOOL _set_rts(WINPR_COMM* pComm) static BOOL set_rts(WINPR_COMM* pComm)
{ {
SERIAL_HANDFLOW handflow; SERIAL_HANDFLOW handflow = { 0 };
if (!_get_handflow(pComm, &handflow)) WINPR_ASSERT(pComm);
if (!get_handflow(pComm, &handflow))
return FALSE; return FALSE;
if (handflow.FlowReplace & SERIAL_RTS_HANDSHAKE) if (handflow.FlowReplace & SERIAL_RTS_HANDSHAKE)
@ -961,13 +987,14 @@ static BOOL _set_rts(WINPR_COMM* pComm)
return FALSE; return FALSE;
} }
return _set_lines(pComm, TIOCM_RTS); return set_lines(pComm, TIOCM_RTS);
} }
static BOOL _clear_rts(WINPR_COMM* pComm) static BOOL clear_rts(WINPR_COMM* pComm)
{ {
SERIAL_HANDFLOW handflow; SERIAL_HANDFLOW handflow = { 0 };
if (!_get_handflow(pComm, &handflow)) WINPR_ASSERT(pComm);
if (!get_handflow(pComm, &handflow))
return FALSE; return FALSE;
if (handflow.FlowReplace & SERIAL_RTS_HANDSHAKE) if (handflow.FlowReplace & SERIAL_RTS_HANDSHAKE)
@ -976,12 +1003,16 @@ static BOOL _clear_rts(WINPR_COMM* pComm)
return FALSE; return FALSE;
} }
return _clear_lines(pComm, TIOCM_RTS); return clear_lines(pComm, TIOCM_RTS);
} }
static BOOL _get_modemstatus(WINPR_COMM* pComm, ULONG* pRegister) static BOOL get_modemstatus(WINPR_COMM* pComm, ULONG* pRegister)
{ {
UINT32 lines = 0; UINT32 lines = 0;
WINPR_ASSERT(pComm);
WINPR_ASSERT(pRegister);
if (ioctl(pComm->fd, TIOCMGET, &lines) < 0) if (ioctl(pComm->fd, TIOCMGET, &lines) < 0)
{ {
char ebuffer[256] = { 0 }; char ebuffer[256] = { 0 };
@ -1018,7 +1049,7 @@ static BOOL _get_modemstatus(WINPR_COMM* pComm, ULONG* pRegister)
} }
/* http://msdn.microsoft.com/en-us/library/windows/hardware/hh439605%28v=vs.85%29.aspx */ /* http://msdn.microsoft.com/en-us/library/windows/hardware/hh439605%28v=vs.85%29.aspx */
static const ULONG _SERIAL_SYS_SUPPORTED_EV_MASK = static const ULONG SERIAL_SYS_SUPPORTED_EV_MASK =
SERIAL_EV_RXCHAR | SERIAL_EV_RXFLAG | SERIAL_EV_TXEMPTY | SERIAL_EV_CTS | SERIAL_EV_DSR | SERIAL_EV_RXCHAR | SERIAL_EV_RXFLAG | SERIAL_EV_TXEMPTY | SERIAL_EV_CTS | SERIAL_EV_DSR |
SERIAL_EV_RLSD | SERIAL_EV_BREAK | SERIAL_EV_ERR | SERIAL_EV_RING | SERIAL_EV_RLSD | SERIAL_EV_BREAK | SERIAL_EV_ERR | SERIAL_EV_RING |
/* SERIAL_EV_PERR | */ /* SERIAL_EV_PERR | */
@ -1037,7 +1068,7 @@ static BOOL is_wait_set(WINPR_COMM* pComm)
return isWaiting; return isWaiting;
} }
static BOOL _set_wait_mask(WINPR_COMM* pComm, const ULONG* pWaitMask) static BOOL set_wait_mask(WINPR_COMM* pComm, const ULONG* pWaitMask)
{ {
ULONG possibleMask = 0; ULONG possibleMask = 0;
@ -1055,7 +1086,7 @@ static BOOL _set_wait_mask(WINPR_COMM* pComm, const ULONG* pWaitMask)
pComm->PendingEvents |= SERIAL_EV_WINPR_STOP; pComm->PendingEvents |= SERIAL_EV_WINPR_STOP;
LeaveCriticalSection(&pComm->EventsLock); LeaveCriticalSection(&pComm->EventsLock);
/* waiting the end of the pending _wait_on_mask() */ /* waiting the end of the pending wait_on_mask() */
while (is_wait_set(pComm)) while (is_wait_set(pComm))
Sleep(10); /* 10ms */ Sleep(10); /* 10ms */
} }
@ -1089,7 +1120,7 @@ static BOOL _set_wait_mask(WINPR_COMM* pComm, const ULONG* pWaitMask)
pComm->PendingEvents = 0; pComm->PendingEvents = 0;
} }
possibleMask = *pWaitMask & _SERIAL_SYS_SUPPORTED_EV_MASK; possibleMask = *pWaitMask & SERIAL_SYS_SUPPORTED_EV_MASK;
if (possibleMask != *pWaitMask) if (possibleMask != *pWaitMask)
{ {
@ -1111,14 +1142,20 @@ static BOOL _set_wait_mask(WINPR_COMM* pComm, const ULONG* pWaitMask)
return TRUE; return TRUE;
} }
static BOOL _get_wait_mask(WINPR_COMM* pComm, ULONG* pWaitMask) static BOOL get_wait_mask(WINPR_COMM* pComm, ULONG* pWaitMask)
{ {
WINPR_ASSERT(pComm);
WINPR_ASSERT(pWaitMask);
*pWaitMask = pComm->WaitEventMask; *pWaitMask = pComm->WaitEventMask;
return TRUE; return TRUE;
} }
static BOOL _set_queue_size(WINPR_COMM* pComm, const SERIAL_QUEUE_SIZE* pQueueSize) static BOOL set_queue_size(WINPR_COMM* pComm, const SERIAL_QUEUE_SIZE* pQueueSize)
{ {
WINPR_ASSERT(pComm);
WINPR_ASSERT(pQueueSize);
if ((pQueueSize->InSize <= N_TTY_BUF_SIZE) && (pQueueSize->OutSize <= N_TTY_BUF_SIZE)) if ((pQueueSize->InSize <= N_TTY_BUF_SIZE) && (pQueueSize->OutSize <= N_TTY_BUF_SIZE))
return TRUE; /* nothing to do */ return TRUE; /* nothing to do */
@ -1140,8 +1177,11 @@ static BOOL _set_queue_size(WINPR_COMM* pComm, const SERIAL_QUEUE_SIZE* pQueueSi
return FALSE; return FALSE;
} }
static BOOL _purge(WINPR_COMM* pComm, const ULONG* pPurgeMask) static BOOL purge(WINPR_COMM* pComm, const ULONG* pPurgeMask)
{ {
WINPR_ASSERT(pComm);
WINPR_ASSERT(pPurgeMask);
if ((*pPurgeMask & ~(SERIAL_PURGE_TXABORT | SERIAL_PURGE_RXABORT | SERIAL_PURGE_TXCLEAR | if ((*pPurgeMask & ~(SERIAL_PURGE_TXABORT | SERIAL_PURGE_RXABORT | SERIAL_PURGE_TXCLEAR |
SERIAL_PURGE_RXCLEAR)) > 0) SERIAL_PURGE_RXCLEAR)) > 0)
{ {
@ -1220,22 +1260,24 @@ static BOOL _purge(WINPR_COMM* pComm, const ULONG* pPurgeMask)
return TRUE; return TRUE;
} }
/* NB: _get_commstatus also produces most of the events consumed by _wait_on_mask(). Exceptions: /* NB: get_commstatus also produces most of the events consumed by wait_on_mask(). Exceptions:
* - SERIAL_EV_RXFLAG: FIXME: once EventChar supported * - SERIAL_EV_RXFLAG: FIXME: once EventChar supported
* *
*/ */
static BOOL _get_commstatus(WINPR_COMM* pComm, SERIAL_STATUS* pCommstatus) static BOOL get_commstatus(WINPR_COMM* pComm, SERIAL_STATUS* pCommstatus)
{ {
/* http://msdn.microsoft.com/en-us/library/jj673022%28v=vs.85%29.aspx */ /* http://msdn.microsoft.com/en-us/library/jj673022%28v=vs.85%29.aspx */
struct serial_icounter_struct currentCounters; struct serial_icounter_struct currentCounters = { 0 };
WINPR_ASSERT(pComm);
WINPR_ASSERT(pCommstatus);
/* NB: ensure to leave the critical section before to return */ /* NB: ensure to leave the critical section before to return */
EnterCriticalSection(&pComm->EventsLock); EnterCriticalSection(&pComm->EventsLock);
ZeroMemory(pCommstatus, sizeof(SERIAL_STATUS)); ZeroMemory(pCommstatus, sizeof(SERIAL_STATUS));
ZeroMemory(&currentCounters, sizeof(struct serial_icounter_struct));
if (ioctl(pComm->fd, TIOCGICOUNT, &currentCounters) < 0) if (ioctl(pComm->fd, TIOCGICOUNT, &currentCounters) < 0)
{ {
char ebuffer[256] = { 0 }; char ebuffer[256] = { 0 };
@ -1392,13 +1434,14 @@ static BOOL _get_commstatus(WINPR_COMM* pComm, SERIAL_STATUS* pCommstatus)
return TRUE; return TRUE;
} }
static BOOL _refresh_PendingEvents(WINPR_COMM* pComm) static BOOL refresh_PendingEvents(WINPR_COMM* pComm)
{ {
SERIAL_STATUS serialStatus; SERIAL_STATUS serialStatus = { 0 };
WINPR_ASSERT(pComm);
/* NB: also ensures PendingEvents to be up to date */ /* NB: also ensures PendingEvents to be up to date */
ZeroMemory(&serialStatus, sizeof(SERIAL_STATUS)); if (!get_commstatus(pComm, &serialStatus))
if (!_get_commstatus(pComm, &serialStatus))
{ {
return FALSE; return FALSE;
} }
@ -1406,8 +1449,11 @@ static BOOL _refresh_PendingEvents(WINPR_COMM* pComm)
return TRUE; return TRUE;
} }
static void _consume_event(WINPR_COMM* pComm, ULONG* pOutputMask, ULONG event) static void consume_event(WINPR_COMM* pComm, ULONG* pOutputMask, ULONG event)
{ {
WINPR_ASSERT(pComm);
WINPR_ASSERT(pOutputMask);
if ((pComm->WaitEventMask & event) && (pComm->PendingEvents & event)) if ((pComm->WaitEventMask & event) && (pComm->PendingEvents & event))
{ {
pComm->PendingEvents &= ~event; /* consumed */ pComm->PendingEvents &= ~event; /* consumed */
@ -1416,10 +1462,11 @@ static void _consume_event(WINPR_COMM* pComm, ULONG* pOutputMask, ULONG event)
} }
/* /*
* NB: see also: _set_wait_mask() * NB: see also: set_wait_mask()
*/ */
static BOOL _wait_on_mask(WINPR_COMM* pComm, ULONG* pOutputMask) static BOOL wait_on_mask(WINPR_COMM* pComm, ULONG* pOutputMask)
{ {
WINPR_ASSERT(pComm);
WINPR_ASSERT(*pOutputMask == 0); WINPR_ASSERT(*pOutputMask == 0);
EnterCriticalSection(&pComm->EventsLock); EnterCriticalSection(&pComm->EventsLock);
@ -1428,8 +1475,8 @@ static BOOL _wait_on_mask(WINPR_COMM* pComm, ULONG* pOutputMask)
while (TRUE) while (TRUE)
{ {
/* NB: EventsLock also used by _refresh_PendingEvents() */ /* NB: EventsLock also used by refresh_PendingEvents() */
if (!_refresh_PendingEvents(pComm)) if (!refresh_PendingEvents(pComm))
{ {
EnterCriticalSection(&pComm->EventsLock); EnterCriticalSection(&pComm->EventsLock);
pComm->PendingEvents &= ~SERIAL_EV_WINPR_WAITING; pComm->PendingEvents &= ~SERIAL_EV_WINPR_WAITING;
@ -1456,16 +1503,16 @@ static BOOL _wait_on_mask(WINPR_COMM* pComm, ULONG* pOutputMask)
return TRUE; return TRUE;
} }
_consume_event(pComm, pOutputMask, SERIAL_EV_RXCHAR); consume_event(pComm, pOutputMask, SERIAL_EV_RXCHAR);
_consume_event(pComm, pOutputMask, SERIAL_EV_RXFLAG); consume_event(pComm, pOutputMask, SERIAL_EV_RXFLAG);
_consume_event(pComm, pOutputMask, SERIAL_EV_TXEMPTY); consume_event(pComm, pOutputMask, SERIAL_EV_TXEMPTY);
_consume_event(pComm, pOutputMask, SERIAL_EV_CTS); consume_event(pComm, pOutputMask, SERIAL_EV_CTS);
_consume_event(pComm, pOutputMask, SERIAL_EV_DSR); consume_event(pComm, pOutputMask, SERIAL_EV_DSR);
_consume_event(pComm, pOutputMask, SERIAL_EV_RLSD); consume_event(pComm, pOutputMask, SERIAL_EV_RLSD);
_consume_event(pComm, pOutputMask, SERIAL_EV_BREAK); consume_event(pComm, pOutputMask, SERIAL_EV_BREAK);
_consume_event(pComm, pOutputMask, SERIAL_EV_ERR); consume_event(pComm, pOutputMask, SERIAL_EV_ERR);
_consume_event(pComm, pOutputMask, SERIAL_EV_RING); consume_event(pComm, pOutputMask, SERIAL_EV_RING);
_consume_event(pComm, pOutputMask, SERIAL_EV_RX80FULL); consume_event(pComm, pOutputMask, SERIAL_EV_RX80FULL);
LeaveCriticalSection(&pComm->EventsLock); LeaveCriticalSection(&pComm->EventsLock);
@ -1494,8 +1541,9 @@ static BOOL _wait_on_mask(WINPR_COMM* pComm, ULONG* pOutputMask)
} }
} }
static BOOL _set_break_on(WINPR_COMM* pComm) static BOOL set_break_on(WINPR_COMM* pComm)
{ {
WINPR_ASSERT(pComm);
if (ioctl(pComm->fd, TIOCSBRK, NULL) < 0) if (ioctl(pComm->fd, TIOCSBRK, NULL) < 0)
{ {
char ebuffer[256] = { 0 }; char ebuffer[256] = { 0 };
@ -1508,8 +1556,9 @@ static BOOL _set_break_on(WINPR_COMM* pComm)
return TRUE; return TRUE;
} }
static BOOL _set_break_off(WINPR_COMM* pComm) static BOOL set_break_off(WINPR_COMM* pComm)
{ {
WINPR_ASSERT(pComm);
if (ioctl(pComm->fd, TIOCCBRK, NULL) < 0) if (ioctl(pComm->fd, TIOCCBRK, NULL) < 0)
{ {
char ebuffer[256] = { 0 }; char ebuffer[256] = { 0 };
@ -1522,8 +1571,9 @@ static BOOL _set_break_off(WINPR_COMM* pComm)
return TRUE; return TRUE;
} }
static BOOL _set_xoff(WINPR_COMM* pComm) static BOOL set_xoff(WINPR_COMM* pComm)
{ {
WINPR_ASSERT(pComm);
if (tcflow(pComm->fd, TCIOFF) < 0) if (tcflow(pComm->fd, TCIOFF) < 0)
{ {
char ebuffer[256] = { 0 }; char ebuffer[256] = { 0 };
@ -1536,8 +1586,9 @@ static BOOL _set_xoff(WINPR_COMM* pComm)
return TRUE; return TRUE;
} }
static BOOL _set_xon(WINPR_COMM* pComm) static BOOL set_xon(WINPR_COMM* pComm)
{ {
WINPR_ASSERT(pComm);
if (tcflow(pComm->fd, TCION) < 0) if (tcflow(pComm->fd, TCION) < 0)
{ {
char ebuffer[256] = { 0 }; char ebuffer[256] = { 0 };
@ -1550,9 +1601,13 @@ static BOOL _set_xon(WINPR_COMM* pComm)
return TRUE; return TRUE;
} }
static BOOL _get_dtrrts(WINPR_COMM* pComm, ULONG* pMask) static BOOL get_dtrrts(WINPR_COMM* pComm, ULONG* pMask)
{ {
UINT32 lines = 0; UINT32 lines = 0;
WINPR_ASSERT(pComm);
WINPR_ASSERT(pMask);
if (ioctl(pComm->fd, TIOCMGET, &lines) < 0) if (ioctl(pComm->fd, TIOCMGET, &lines) < 0)
{ {
char ebuffer[256] = { 0 }; char ebuffer[256] = { 0 };
@ -1572,8 +1627,11 @@ static BOOL _get_dtrrts(WINPR_COMM* pComm, ULONG* pMask)
return TRUE; return TRUE;
} }
static BOOL _config_size(WINPR_COMM* pComm, ULONG* pSize) static BOOL config_size(WINPR_COMM* pComm, ULONG* pSize)
{ {
WINPR_ASSERT(pComm);
WINPR_ASSERT(pSize);
/* http://msdn.microsoft.com/en-us/library/ff546548%28v=vs.85%29.aspx */ /* http://msdn.microsoft.com/en-us/library/ff546548%28v=vs.85%29.aspx */
if (!pSize) if (!pSize)
return FALSE; return FALSE;
@ -1582,15 +1640,18 @@ static BOOL _config_size(WINPR_COMM* pComm, ULONG* pSize)
return TRUE; return TRUE;
} }
static BOOL _immediate_char(WINPR_COMM* pComm, const UCHAR* pChar) static BOOL immediate_char(WINPR_COMM* pComm, const UCHAR* pChar)
{ {
BOOL result = 0; BOOL result = 0;
DWORD nbBytesWritten = -1; DWORD nbBytesWritten = -1;
WINPR_ASSERT(pComm);
WINPR_ASSERT(pChar);
/* FIXME: CommWriteFile uses a critical section, shall it be /* FIXME: CommWriteFile uses a critical section, shall it be
* interrupted? * interrupted?
* *
* FIXME: see also _get_commstatus()'s WaitForImmediate boolean * FIXME: see also get_commstatus()'s WaitForImmediate boolean
*/ */
result = CommWriteFile(pComm, pChar, 1, &nbBytesWritten, NULL); result = CommWriteFile(pComm, pChar, 1, &nbBytesWritten, NULL);
@ -1600,50 +1661,50 @@ static BOOL _immediate_char(WINPR_COMM* pComm, const UCHAR* pChar)
return result; return result;
} }
static BOOL _reset_device(WINPR_COMM* pComm) static BOOL reset_device(WINPR_COMM* pComm)
{ {
/* http://msdn.microsoft.com/en-us/library/dn265347%28v=vs.85%29.aspx */ /* http://msdn.microsoft.com/en-us/library/dn265347%28v=vs.85%29.aspx */
return TRUE; return TRUE;
} }
static SERIAL_DRIVER _SerialSys = { static SERIAL_DRIVER SerialSys = {
.id = SerialDriverSerialSys, .id = SerialDriverSerialSys,
.name = _T("Serial.sys"), .name = _T("Serial.sys"),
.set_baud_rate = _set_baud_rate, .set_baud_rate = set_baud_rate,
.get_baud_rate = _get_baud_rate, .get_baud_rate = get_baud_rate,
.get_properties = _get_properties, .get_properties = get_properties,
.set_serial_chars = _set_serial_chars, .set_serial_chars = set_serial_chars,
.get_serial_chars = _get_serial_chars, .get_serial_chars = get_serial_chars,
.set_line_control = _set_line_control, .set_line_control = set_line_control,
.get_line_control = _get_line_control, .get_line_control = get_line_control,
.set_handflow = _set_handflow, .set_handflow = set_handflow,
.get_handflow = _get_handflow, .get_handflow = get_handflow,
.set_timeouts = _set_timeouts, .set_timeouts = set_timeouts,
.get_timeouts = _get_timeouts, .get_timeouts = get_timeouts,
.set_dtr = _set_dtr, .set_dtr = set_dtr,
.clear_dtr = _clear_dtr, .clear_dtr = clear_dtr,
.set_rts = _set_rts, .set_rts = set_rts,
.clear_rts = _clear_rts, .clear_rts = clear_rts,
.get_modemstatus = _get_modemstatus, .get_modemstatus = get_modemstatus,
.set_wait_mask = _set_wait_mask, .set_wait_mask = set_wait_mask,
.get_wait_mask = _get_wait_mask, .get_wait_mask = get_wait_mask,
.wait_on_mask = _wait_on_mask, .wait_on_mask = wait_on_mask,
.set_queue_size = _set_queue_size, .set_queue_size = set_queue_size,
.purge = _purge, .purge = purge,
.get_commstatus = _get_commstatus, .get_commstatus = get_commstatus,
.set_break_on = _set_break_on, .set_break_on = set_break_on,
.set_break_off = _set_break_off, .set_break_off = set_break_off,
.set_xoff = _set_xoff, .set_xoff = set_xoff,
.set_xon = _set_xon, .set_xon = set_xon,
.get_dtrrts = _get_dtrrts, .get_dtrrts = get_dtrrts,
.config_size = _config_size, .config_size = config_size,
.immediate_char = _immediate_char, .immediate_char = immediate_char,
.reset_device = _reset_device, .reset_device = reset_device,
}; };
SERIAL_DRIVER* SerialSys_s(void) SERIAL_DRIVER* SerialSys_s(void)
{ {
return &_SerialSys; return &SerialSys;
} }
#endif /* __linux__ */ #endif /* __linux__ */