Skip to content

Commit

Permalink
Try and mitigate some win32 warnings (#36)
Browse files Browse the repository at this point in the history
Remove some win32 unsigned -> signed conversion warnings
  • Loading branch information
AndreRenaud authored Sep 3, 2024
1 parent c2875ce commit e7cc896
Showing 1 changed file with 25 additions and 39 deletions.
64 changes: 25 additions & 39 deletions simple_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@
#define mseconds() (uint64_t)(GetTickCount64())
#endif

#ifdef _WIN32
int win32_errno(void)
{
DWORD e = GetLastError();
if (e > INT_MAX)
return -1;
return -(int)e;
}
#endif

#ifndef TIOCSRS485
#define TIOCSRS485 0x542F
#endif
Expand Down Expand Up @@ -86,9 +96,8 @@ ssize_t simple_uart_read(struct simple_uart *sc, void *buffer, size_t max_len)
SetCommTimeouts(sc->port, &commTimeout);
}
if (max_len > ULONG_MAX) max_len = ULONG_MAX; // avoid overflow at typecast
if (!ReadFile (sc->port, buffer, (DWORD)max_len, (LPDWORD)&r, NULL)) {
return -GetLastError();
}
if (!ReadFile (sc->port, buffer, (DWORD)max_len, (LPDWORD)&r, NULL))
return win32_errno();
#else
fd_set readfds, exceptfds;
struct timeval t;
Expand Down Expand Up @@ -161,17 +170,12 @@ static int simple_uart_set_config(struct simple_uart *sc, int speed, const char
{
/* Variables */
DCB options; // config handle for windows, @see https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-dcb
DWORD ero; // error return code

/* only non negativ values */
if (0 > speed) speed = CBR_38400; // default speed
/* Accquire current port config */
if (!GetCommState (sc->port, &options)) {
ero = -GetLastError();
if (ero > INT_MAX)
return -1;
return (int) ero;
}
/* Acquire current port config */
if (!GetCommState (sc->port, &options))
return win32_errno();
options.BaudRate = (DWORD) speed;
/* parse mode string */
// parity
Expand Down Expand Up @@ -214,12 +218,8 @@ static int simple_uart_set_config(struct simple_uart *sc, int speed, const char
/* mandatory options */
options.fBinary = TRUE;
/* assign to port */
if (!SetCommState (sc->port, &options)) {
ero = -GetLastError();
if (ero > INT_MAX)
return -1;
return (int) ero;
}
if (!SetCommState (sc->port, &options))
return win32_errno();
/* graceful end */
return 0;
}
Expand Down Expand Up @@ -551,7 +551,7 @@ int simple_uart_describe(const char *uart, char *description, size_t max_desc_le
#if defined(_WIN32)
int intMatchUart = 0; // device on machine matched
HDEVINFO deviceInfoSet;
DWORD i, r;
DWORD i;

/* Get handle of device information
* @see https://learn.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdigetclassdevsa
Expand All @@ -577,9 +577,7 @@ int simple_uart_describe(const char *uart, char *description, size_t max_desc_le
break;
}
} else {
r = -GetLastError();
if (r > INT_MAX) r = INT_MAX;
return (int)r;
return win32_errno();
}
++i;
}
Expand Down Expand Up @@ -785,12 +783,8 @@ int simple_uart_get_pin(struct simple_uart *uart, int pin)
}
#else
DWORD status;
DWORD r;
if (!GetCommModemStatus(uart->port, &status)) {
r = -GetLastError();
if (r > INT_MAX) r = INT_MAX;
return (int)r;
}
if (!GetCommModemStatus(uart->port, &status))
return win32_errno();
switch (pin) {
case SIMPLE_UART_CTS:
return (status & MS_CTS_ON) ? 1 : 0;
Expand Down Expand Up @@ -830,7 +824,6 @@ int simple_uart_set_pin(struct simple_uart *uart, int pin, bool high)
return 0;
#else
bool res;
DWORD r;
switch(pin) {
case SIMPLE_UART_RTS:
res = EscapeCommFunction(uart->port, high ? SETRTS : CLRRTS);
Expand All @@ -841,11 +834,8 @@ int simple_uart_set_pin(struct simple_uart *uart, int pin, bool high)
default:
return -EINVAL;
}
if (!res) {
r = -GetLastError();
if (r > INT_MAX) r = INT_MAX;
return (int)r;
}
if (!res)
return win32_errno();
return 0;
#endif
}
Expand All @@ -856,12 +846,8 @@ int simple_uart_flush(struct simple_uart *uart)
if (tcdrain(uart->fd) < 0)
return -errno;
#else
DWORD r;
if (!FlushFileBuffers(uart->port)) {
r = -GetLastError();
if (r > INT_MAX) r = INT_MAX;
return (int)r;
}
if (!FlushFileBuffers(uart->port))
return win32_errno();
#endif
return 0;
}

0 comments on commit e7cc896

Please sign in to comment.