Skip to content

Commit

Permalink
Merge pull request #132 from REVrobotics/chore/port-rhsp-changes
Browse files Browse the repository at this point in the history
Port last error code and device path fixes from librhsp
  • Loading branch information
LandryNorris authored Apr 5, 2024
2 parents 0f4e36d + 0e1f83c commit 8b4817f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/rhsplib/librhsp/include/rhsp/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion packages/rhsplib/librhsp/src/arch/linux/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>

#include "rhsp/serial.h"

Expand Down Expand Up @@ -359,4 +360,6 @@ static int baudrateToBits(uint32_t baudrate)
}
}


int rhsp_getLastOsError() {
return errno;
}
5 changes: 5 additions & 0 deletions packages/rhsplib/librhsp/src/arch/mac/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>

#include "rhsp/serial.h"

Expand Down Expand Up @@ -228,3 +229,7 @@ void rhsp_serialClose(RhspSerial* serial)
serial->fd = -1;
}

int rhsp_getLastOsError() {
return errno;
}

15 changes: 14 additions & 1 deletion packages/rhsplib/librhsp/src/arch/win/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -242,3 +251,7 @@ void rhsp_serialClose(RhspSerial* serial)
}
}
}

int rhsp_getLastOsError(void) {
return (int) GetLastError();
}

0 comments on commit 8b4817f

Please sign in to comment.