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 09:27:59 -07:00
committed by GitHub
parent 416b6a35a4
commit ccdf060393
11 changed files with 143 additions and 38 deletions
+2
View File
@@ -65,6 +65,8 @@ public:
ssize_t write(const void *buffer, size_t buffer_size);
const char *getPort() const;
static bool validatePort(const char *port);
bool setPort(const char *port);
uint32_t getBaudrate() const;
bool setBaudrate(uint32_t baudrate);
+28 -3
View File
@@ -51,9 +51,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;
@@ -190,6 +189,11 @@ bool SerialImpl::open()
return true;
}
if (!validatePort(_port)) {
PX4_ERR("Invalid port %s", _port);
return false;
}
// Open the serial port
int serial_fd = ::open(_port, O_RDWR | O_NOCTTY);
@@ -317,6 +321,27 @@ const char *SerialImpl::getPort() const
return _port;
}
bool SerialImpl::validatePort(const char *port)
{
return (port && (access(port, R_OK | W_OK) == 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;