[hal] Check error codes during serial port initialization (#3712)

Throw if serial port init fails.

Fixes #2036.
This commit is contained in:
Tyler Veness
2021-11-09 20:10:48 -08:00
committed by GitHub
parent 52f1464029
commit 42d3a50aa2

View File

@@ -83,7 +83,7 @@ HAL_SerialPortHandle HAL_InitializeSerialPortDirect(HAL_SerialPort port,
serialPort->portId = open(portName, O_RDWR | O_NOCTTY);
if (serialPort->portId < 0) {
*status = errno;
*status = -errno;
if (*status == EACCES) {
*status = HAL_CONSOLE_OUT_ENABLED_ERROR;
}
@@ -94,8 +94,20 @@ HAL_SerialPortHandle HAL_InitializeSerialPortDirect(HAL_SerialPort port,
std::memset(&serialPort->tty, 0, sizeof(serialPort->tty));
serialPort->baudRate = B9600;
cfsetospeed(&serialPort->tty, static_cast<speed_t>(serialPort->baudRate));
cfsetispeed(&serialPort->tty, static_cast<speed_t>(serialPort->baudRate));
if (cfsetospeed(&serialPort->tty,
static_cast<speed_t>(serialPort->baudRate)) != 0) {
*status = -errno;
close(serialPort->portId);
serialPortHandles->Free(handle);
return HAL_kInvalidHandle;
}
if (cfsetispeed(&serialPort->tty,
static_cast<speed_t>(serialPort->baudRate)) != 0) {
*status = -errno;
close(serialPort->portId);
serialPortHandles->Free(handle);
return HAL_kInvalidHandle;
}
serialPort->tty.c_cflag &= ~PARENB;
serialPort->tty.c_cflag &= ~CSTOPB;
@@ -115,9 +127,14 @@ HAL_SerialPortHandle HAL_InitializeSerialPortDirect(HAL_SerialPort port,
*/
serialPort->tty.c_oflag = ~OPOST;
tcflush(serialPort->portId, TCIOFLUSH);
if (tcflush(serialPort->portId, TCIOFLUSH) != 0) {
*status = -errno;
close(serialPort->portId);
serialPortHandles->Free(handle);
return HAL_kInvalidHandle;
}
if (tcsetattr(serialPort->portId, TCSANOW, &serialPort->tty) != 0) {
*status = errno;
*status = -errno;
close(serialPort->portId);
serialPortHandles->Free(handle);
return HAL_kInvalidHandle;