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
+14
View File
@@ -36,6 +36,10 @@
namespace device
{
Serial::Serial() :
_impl(nullptr, 57600, ByteSize::EightBits, Parity::None, StopBits::One, FlowControl::Disabled) {}
Serial::Serial(const char *port, uint32_t baudrate, ByteSize bytesize, Parity parity, StopBits stopbits,
FlowControl flowcontrol) :
_impl(port, baudrate, bytesize, parity, stopbits, flowcontrol)
@@ -135,4 +139,14 @@ const char *Serial::getPort() const
return _impl.getPort();
}
bool Serial::validatePort(const char *port)
{
return SerialImpl::validatePort(port);
}
bool Serial::setPort(const char *port)
{
return _impl.setPort(port);
}
} // namespace device
@@ -48,6 +48,7 @@ namespace device __EXPORT
class Serial
{
public:
Serial();
Serial(const char *port, uint32_t baudrate = 57600,
ByteSize bytesize = ByteSize::EightBits, Parity parity = Parity::None,
StopBits stopbits = StopBits::One, FlowControl flowcontrol = FlowControl::Disabled);
@@ -83,6 +84,8 @@ public:
FlowControl getFlowcontrol() const;
bool setFlowcontrol(FlowControl flowcontrol);
static bool validatePort(const char *port);
bool setPort(const char *port);
const char *getPort() const;
private:
+28 -3
View File
@@ -53,9 +53,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;
@@ -192,6 +191,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);
@@ -324,6 +328,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;
@@ -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);
+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;
+1
View File
@@ -65,6 +65,7 @@ public:
const char *getPort() const;
bool setPort(const char *port);
static bool validatePort(const char *port);
uint32_t getBaudrate() const;
bool setBaudrate(uint32_t baudrate);
+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;