Additions to the Serial UART API (#22953)

- Added an empty constructor, setPort, and validatePort functions for Serial API
- Changed GPS to not allocate Serial object dynamically
- Moved access check on serial port name into the Serial API
- Improved the Qurt platform validatePort Serial function to implement a more rigorous check. Added safety check
to the setPort Serial function to make sure it isn't called after the port has been already opened.
This commit is contained in:
Eric Katzfey
2024-04-01 12:27:59 -04:00
committed by GitHub
parent 416b6a35a4
commit ccdf060393
11 changed files with 143 additions and 38 deletions
+28 -3
View File
@@ -48,9 +48,8 @@ SerialImpl::SerialImpl(const char *port, uint32_t baudrate, ByteSize bytesize, P
_stopbits(stopbits),
_flowcontrol(flowcontrol)
{
if (port) {
strncpy(_port, port, sizeof(_port) - 1);
_port[sizeof(_port) - 1] = '\0';
if (validatePort(port)) {
setPort(port);
} else {
_port[0] = 0;
@@ -117,6 +116,11 @@ bool SerialImpl::open()
return false;
}
if (!validatePort(_port)) {
PX4_ERR("Invalid port %s", _port);
return false;
}
// qurt_uart_open will check validity of port and baudrate
int serial_fd = qurt_uart_open(_port, _baudrate);
@@ -256,6 +260,27 @@ const char *SerialImpl::getPort() const
return _port;
}
bool SerialImpl::validatePort(const char *port)
{
return (qurt_uart_get_port(port) >= 0);
}
bool SerialImpl::setPort(const char *port)
{
if (_open) {
PX4_ERR("Cannot set port after port has already been opened");
return false;
}
if (validatePort(port)) {
strncpy(_port, port, sizeof(_port) - 1);
_port[sizeof(_port) - 1] = '\0';
return true;
}
return false;
}
uint32_t SerialImpl::getBaudrate() const
{
return _baudrate;