mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-28 10:46:33 +08:00
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:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user