From 0e1f83c4de432d8cb3ecdf43872c71fd556a23a8 Mon Sep 17 00:00:00 2001 From: LandryNorris Date: Thu, 4 Apr 2024 16:19:19 -0500 Subject: [PATCH] Port last error code and device path fixes from librhsp --- packages/rhsplib/librhsp/include/rhsp/serial.h | 6 ++++++ packages/rhsplib/librhsp/src/arch/linux/serial.c | 5 ++++- packages/rhsplib/librhsp/src/arch/mac/serial.c | 5 +++++ packages/rhsplib/librhsp/src/arch/win/serial.c | 15 ++++++++++++++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/rhsplib/librhsp/include/rhsp/serial.h b/packages/rhsplib/librhsp/include/rhsp/serial.h index a59c66d8..c574491e 100644 --- a/packages/rhsplib/librhsp/include/rhsp/serial.h +++ b/packages/rhsplib/librhsp/include/rhsp/serial.h @@ -113,6 +113,12 @@ int rhsp_serialWrite(RhspSerial* serial, const uint8_t* buffer, size_t bytesToWr * */ void rhsp_serialClose(RhspSerial* serial); +/** + * @brief Get the last error code from the OS. + * This is errno on unix, and GetLastError on windows. + * */ +int rhsp_getLastOsError(); + #ifdef __cplusplus } #endif diff --git a/packages/rhsplib/librhsp/src/arch/linux/serial.c b/packages/rhsplib/librhsp/src/arch/linux/serial.c index 1d775ffb..1c2a5a84 100644 --- a/packages/rhsplib/librhsp/src/arch/linux/serial.c +++ b/packages/rhsplib/librhsp/src/arch/linux/serial.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "rhsp/serial.h" @@ -359,4 +360,6 @@ static int baudrateToBits(uint32_t baudrate) } } - +int rhsp_getLastOsError() { + return errno; +} diff --git a/packages/rhsplib/librhsp/src/arch/mac/serial.c b/packages/rhsplib/librhsp/src/arch/mac/serial.c index 4c049771..288c625d 100644 --- a/packages/rhsplib/librhsp/src/arch/mac/serial.c +++ b/packages/rhsplib/librhsp/src/arch/mac/serial.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "rhsp/serial.h" @@ -228,3 +229,7 @@ void rhsp_serialClose(RhspSerial* serial) serial->fd = -1; } +int rhsp_getLastOsError() { + return errno; +} + diff --git a/packages/rhsplib/librhsp/src/arch/win/serial.c b/packages/rhsplib/librhsp/src/arch/win/serial.c index 0265a07a..2d42d9ab 100644 --- a/packages/rhsplib/librhsp/src/arch/win/serial.c +++ b/packages/rhsplib/librhsp/src/arch/win/serial.c @@ -109,9 +109,18 @@ int rhsp_serialOpen(RhspSerial* serial, { return RHSP_SERIAL_ERROR; } + + char modifiedSerialPortName[20]; + + // if the user has already prefixed the workaround, don't add it again + if(serialPort[0] == '\\') { + strncpy(modifiedSerialPortName, serialPort, 20); + } else { + snprintf(modifiedSerialPortName, 20, "\\\\.\\%s", serialPort); + } /* Try to open specified COM-port */ - serial->handle = CreateFile(serialPort, + serial->handle = CreateFile(modifiedSerialPortName, GENERIC_READ | GENERIC_WRITE, 0, // must be opened with exclusive-access NULL, // default security attributes @@ -242,3 +251,7 @@ void rhsp_serialClose(RhspSerial* serial) } } } + +int rhsp_getLastOsError(void) { + return (int) GetLastError(); +}