From 4672106699dfa94cd4733a55f947b3525f6647e0 Mon Sep 17 00:00:00 2001 From: Eric Katzfey Date: Thu, 20 Jul 2023 13:11:35 -0700 Subject: [PATCH] Added baudrate validation functions to the Serial implementations --- platforms/nuttx/src/px4/common/SerialImpl.cpp | 26 ++++++++++++++++ .../src/px4/common/include/SerialImpl.hpp | 1 + platforms/posix/include/SerialImpl.hpp | 1 + platforms/posix/src/px4/common/SerialImpl.cpp | 26 ++++++++++++++++ platforms/qurt/include/SerialImpl.hpp | 2 ++ platforms/qurt/src/px4/SerialImpl.cpp | 30 +++++++++++++++++++ 6 files changed, 86 insertions(+) diff --git a/platforms/nuttx/src/px4/common/SerialImpl.cpp b/platforms/nuttx/src/px4/common/SerialImpl.cpp index 4794e66602..4fcf2e499c 100644 --- a/platforms/nuttx/src/px4/common/SerialImpl.cpp +++ b/platforms/nuttx/src/px4/common/SerialImpl.cpp @@ -70,11 +70,32 @@ SerialImpl::~SerialImpl() } } +bool SerialImpl::validateBaudrate(uint32_t baudrate) +{ + if ((baudrate == 9600) || + (baudrate == 19200) || + (baudrate == 38400) || + (baudrate == 57600) || + (baudrate == 115200) || + (baudrate == 230400) || + (baudrate == 460800) || + (baudrate == 921600)) { + return true; + } + + return false; +} + bool SerialImpl::configure() { /* process baud rate */ int speed; + if (! validateBaudrate(_baudrate)) { + PX4_ERR("ERR: unknown baudrate: %lu", _baudrate); + return false; + } + switch (_baudrate) { case 9600: speed = B9600; break; @@ -329,6 +350,11 @@ uint32_t SerialImpl::getBaudrate() const bool SerialImpl::setBaudrate(uint32_t baudrate) { + if (! validateBaudrate(baudrate)) { + PX4_ERR("ERR: invalid baudrate: %lu", baudrate); + return false; + } + // check if already configured if ((baudrate == _baudrate) && _open) { return true; diff --git a/platforms/nuttx/src/px4/common/include/SerialImpl.hpp b/platforms/nuttx/src/px4/common/include/SerialImpl.hpp index 1cf9fd3bcd..4476cb20a9 100644 --- a/platforms/nuttx/src/px4/common/include/SerialImpl.hpp +++ b/platforms/nuttx/src/px4/common/include/SerialImpl.hpp @@ -100,6 +100,7 @@ private: StopBits _stopbits{StopBits::One}; FlowControl _flowcontrol{FlowControl::Disabled}; + bool validateBaudrate(uint32_t baudrate); bool configure(); }; diff --git a/platforms/posix/include/SerialImpl.hpp b/platforms/posix/include/SerialImpl.hpp index ba085f2ef8..93765efac7 100644 --- a/platforms/posix/include/SerialImpl.hpp +++ b/platforms/posix/include/SerialImpl.hpp @@ -69,6 +69,7 @@ private: StopBits _stopbits{StopBits::One}; FlowControl _flowcontrol{FlowControl::Disabled}; + bool validateBaudrate(uint32_t baudrate); bool configure(); // Mutex used to lock the read functions diff --git a/platforms/posix/src/px4/common/SerialImpl.cpp b/platforms/posix/src/px4/common/SerialImpl.cpp index 7e181de881..ab48a1a58a 100644 --- a/platforms/posix/src/px4/common/SerialImpl.cpp +++ b/platforms/posix/src/px4/common/SerialImpl.cpp @@ -36,11 +36,32 @@ SerialImpl::~SerialImpl() } } +bool SerialImpl::validateBaudrate(uint32_t baudrate) +{ + if ((baudrate == 9600) || + (baudrate == 19200) || + (baudrate == 38400) || + (baudrate == 57600) || + (baudrate == 115200) || + (baudrate == 230400) || + (baudrate == 460800) || + (baudrate == 921600)) { + return true; + } + + return false; +} + bool SerialImpl::configure() { /* process baud rate */ int speed; + if (! validateBaudrate(_baudrate)) { + PX4_ERR("ERR: unknown baudrate: %u", _baudrate); + return false; + } + switch (_baudrate) { case 9600: speed = B9600; break; @@ -295,6 +316,11 @@ uint32_t SerialImpl::getBaudrate() const bool SerialImpl::setBaudrate(uint32_t baudrate) { + if (! validateBaudrate(baudrate)) { + PX4_ERR("ERR: invalid baudrate: %u", baudrate); + return false; + } + // check if already configured if ((baudrate == _baudrate) && _open) { return true; diff --git a/platforms/qurt/include/SerialImpl.hpp b/platforms/qurt/include/SerialImpl.hpp index ebede1f5ed..dadda42e77 100644 --- a/platforms/qurt/include/SerialImpl.hpp +++ b/platforms/qurt/include/SerialImpl.hpp @@ -68,6 +68,8 @@ private: StopBits _stopbits{StopBits::One}; FlowControl _flowcontrol{FlowControl::Disabled}; + bool validateBaudrate(uint32_t baudrate); + // Mutex used to lock the read functions //pthread_mutex_t read_mutex; diff --git a/platforms/qurt/src/px4/SerialImpl.cpp b/platforms/qurt/src/px4/SerialImpl.cpp index 24eb5f4e8b..9ce43e94f2 100644 --- a/platforms/qurt/src/px4/SerialImpl.cpp +++ b/platforms/qurt/src/px4/SerialImpl.cpp @@ -32,6 +32,26 @@ SerialImpl::~SerialImpl() } } +bool SerialImpl::validateBaudrate(uint32_t baudrate) +{ + if ((baudrate != 9600) && + (baudrate != 38400) && + (baudrate != 57600) && + (baudrate != 115200) && + (baudrate != 230400) && + (baudrate != 250000) && + (baudrate != 420000) && + (baudrate != 460800) && + (baudrate != 921600) && + (baudrate != 1000000) && + (baudrate != 1843200) && + (baudrate != 2000000)) { + return false; + } + + return true; +} + bool SerialImpl::open() { // There's no harm in calling open multiple times on the same port. @@ -40,6 +60,11 @@ bool SerialImpl::open() _open = false; _serial_fd = -1; + if (! validateBaudrate(_baudrate)) { + PX4_ERR("Invalid baudrate: %u", _baudrate); + return false; + } + if (_bytesize != ByteSize::EightBits) { PX4_ERR("Qurt platform only supports ByteSize::EightBits"); return false; @@ -219,6 +244,11 @@ uint32_t SerialImpl::getBaudrate() const bool SerialImpl::setBaudrate(uint32_t baudrate) { + if (! validateBaudrate(baudrate)) { + PX4_ERR("Invalid baudrate: %u", baudrate); + return false; + } + // check if already configured if (baudrate == _baudrate) { return true;