From f3aad90446e981fa75b484f369ca81b13383e2b3 Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Sun, 26 Oct 2025 08:30:57 -0300 Subject: [PATCH 01/12] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(serial):=20?= =?UTF-8?q?Create=20pointer=20to=20::ioctl=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/libserial/serial.hpp | 10 ++++++++++ src/serial.cpp | 8 ++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/libserial/serial.hpp b/include/libserial/serial.hpp index 6b615c4..fad068f 100644 --- a/include/libserial/serial.hpp +++ b/include/libserial/serial.hpp @@ -354,6 +354,16 @@ void setSystemCallFunctions( #endif private: +/** + * @brief Ioctl system call function wrapper + * + * Allows injection of custom ioctl function for testing. + */ +std::function ioctl_ = + [](int fd, unsigned long request, void* arg) { + return ::ioctl(fd, request, arg); + }; + /** * @brief Poll system call function wrapper * diff --git a/src/serial.cpp b/src/serial.cpp index 7fc2600..52e520a 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -189,7 +189,7 @@ size_t Serial::readUntil(std::shared_ptr buffer, char terminator) { } void Serial::flushInputBuffer() { - if (ioctl(fd_serial_port_, TCFLSH, TCIFLUSH) != 0) { + if (ioctl_(fd_serial_port_, TCFLSH, TCIFLUSH) != 0) { throw SerialException("Error flushing input buffer: " + std::string(strerror(errno))); } } @@ -208,7 +208,7 @@ void Serial::setBaudRate(BaudRate baud_rate) { } void Serial::setTermios2() { - ssize_t error = ioctl(fd_serial_port_, TCSETS2, &options_); + ssize_t error = ioctl_(fd_serial_port_, TCSETS2, &options_); if (error < 0) { throw SerialException("Error set Termios2: " + std::string(strerror(errno))); } @@ -355,7 +355,7 @@ size_t Serial::getMaxSafeReadSize() const { int Serial::getAvailableData() const { int bytes_available; - if (ioctl(fd_serial_port_, FIONREAD, &bytes_available) < 0) { + if (ioctl_(fd_serial_port_, FIONREAD, &bytes_available) < 0) { throw SerialException("Error getting available data: " + std::string(strerror(errno))); } return bytes_available; @@ -367,7 +367,7 @@ int Serial::getBaudRate() const { } void Serial::getTermios2() const { - ssize_t error = ioctl(fd_serial_port_, TCGETS2, &options_); + ssize_t error = ioctl_(fd_serial_port_, TCGETS2, &options_); if (error < 0) { throw SerialException("Error get Termios2: " + std::string(strerror(errno))); } From 1b6d35287f06105e73a1a614f5f91f1dc5f63536 Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Sun, 26 Oct 2025 09:13:05 -0300 Subject: [PATCH 02/12] =?UTF-8?q?=E2=9C=85=20test(serial):=20Update=20test?= =?UTF-8?q?s=20with=20new=20pointers=20for=20system=20call=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/libserial/serial.hpp | 18 ++++++++++++++---- test/test_serial_pty.cpp | 25 +++++++++---------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/include/libserial/serial.hpp b/include/libserial/serial.hpp index fad068f..83fcd50 100644 --- a/include/libserial/serial.hpp +++ b/include/libserial/serial.hpp @@ -341,16 +341,26 @@ void setFdForTest(int fd) { // WARNING: Test helper only! This function allows injection of custom // system call functions for testing error handling. It should NEVER be // used in production code. -void setSystemCallFunctions( - std::function poll_func, - std::function read_func) { +void setPollSystemFunction( + std::function poll_func) { poll_ = [poll_func](struct pollfd* f, nfds_t n, int t) { return poll_func(f, n, t); }; + } + +void setReadSystemFunction( + std::function read_func) { read_ = [read_func](int fd, void* buf, size_t sz) { return read_func(fd, buf, sz); }; -} + } + +void setIoctlSystemFunction( + std::function ioctl_func) { + ioctl_ = [ioctl_func](int fd, unsigned long request, void* arg) { + return ioctl_func(fd, request, arg); + }; + } #endif private: diff --git a/test/test_serial_pty.cpp b/test/test_serial_pty.cpp index 3cb2f73..30445c2 100644 --- a/test/test_serial_pty.cpp +++ b/test/test_serial_pty.cpp @@ -309,10 +309,11 @@ TEST_F(PseudoTerminalTest, ReadWithReadFail) { auto read_buffer = std::make_shared(); for (const auto& [error_num, error_msg] : errors_read_) { - serial_port.setSystemCallFunctions( + serial_port.setPollSystemFunction( [](struct pollfd*, nfds_t, int) -> int { return 1; - }, + }); + serial_port.setReadSystemFunction( [error_num](int, void*, size_t) -> ssize_t { errno = error_num; return -1; @@ -337,13 +338,10 @@ TEST_F(PseudoTerminalTest, ReadWithPollFail) { auto read_buffer = std::make_shared(); for (const auto& [error_num, error_msg] : errors_poll_) { - serial_port.setSystemCallFunctions( + serial_port.setPollSystemFunction( [error_num](struct pollfd*, nfds_t, int) -> int { errno = error_num; return -1; - }, - [](int, void*, size_t) -> ssize_t { - return 1; }); auto expected_what = "Error in poll(): " + error_msg; @@ -436,10 +434,7 @@ TEST_F(PseudoTerminalTest, ReadBytesWithReadFail) { auto read_buffer = std::make_shared(); for (const auto& [error_num, error_msg] : errors_read_) { - serial_port.setSystemCallFunctions( - [](struct pollfd*, nfds_t, int) -> int { - return 1; - }, + serial_port.setReadSystemFunction( [error_num](int, void*, size_t) -> ssize_t { errno = error_num; return -1; @@ -554,10 +549,11 @@ TEST_F(PseudoTerminalTest, ReadUntilWithReadFail) { // Skip these as they are handled differently in readUntil continue; } - serial_port.setSystemCallFunctions( + serial_port.setPollSystemFunction( [](struct pollfd*, nfds_t, int) -> int { return 1; - }, + }); + serial_port.setReadSystemFunction( [error_num](int, void*, size_t) -> ssize_t { errno = error_num; return -1; @@ -582,13 +578,10 @@ TEST_F(PseudoTerminalTest, ReadUntilWithPollFail) { auto read_buffer = std::make_shared(); for (const auto& [error_num, error_msg] : errors_poll_) { - serial_port.setSystemCallFunctions( + serial_port.setPollSystemFunction( [error_num](struct pollfd*, nfds_t, int) -> int { errno = error_num; return -1; - }, - [](int, void*, size_t) -> ssize_t { - return 1; }); auto expected_what = "Error in poll(): " + error_msg; From b519e873bff300e69d7d2bfabd42755c5b1b720c Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Sun, 26 Oct 2025 09:16:20 -0300 Subject: [PATCH 03/12] =?UTF-8?q?=E2=9C=85=20test(serial):=20Implemente=20?= =?UTF-8?q?throwing=20exception=20setTermios2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_serial_pty.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/test_serial_pty.cpp b/test/test_serial_pty.cpp index 30445c2..3d3046f 100644 --- a/test/test_serial_pty.cpp +++ b/test/test_serial_pty.cpp @@ -102,6 +102,31 @@ TEST_F(PseudoTerminalTest, ParameterizedConstructor) { libserial::Serial serial_port(slave_port_); } +TEST_F(PseudoTerminalTest, SetTermios2WithFail) { + libserial::Serial serial_port; + + serial_port.open(slave_port_); + + // Inject failure into ioctl for setTermios2 + serial_port.setIoctlSystemFunction( + [](int, unsigned long, void*) -> int { + errno = EIO; + return -1; + }); + + EXPECT_THROW({ + serial_port.setBaudRate(9600); + }, libserial::SerialException); + + // Restore ioctl function for cleanup + serial_port.setIoctlSystemFunction( + [](int fd, unsigned long request, void* arg) -> int { + return ::ioctl(fd, request, arg); + }); + + serial_port.close(); +} + TEST_F(PseudoTerminalTest, SetAndGetBaudRate) { libserial::Serial serial_port; From a07aafef41dbe90dfb57ad1669b7b75eac8941dc Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Tue, 28 Oct 2025 12:20:08 -0300 Subject: [PATCH 04/12] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(serial):=20?= =?UTF-8?q?Delet=20data=5Flenght=5F=20atribut,=20and=20guet=20the=20values?= =?UTF-8?q?=20from=20the=20termios2=20structure=20directly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/libserial/serial.hpp | 10 +++------ src/serial.cpp | 23 ++++++++++++++++----- test/test_serial_pty.cpp | 39 ++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/include/libserial/serial.hpp b/include/libserial/serial.hpp index 83fcd50..d269032 100644 --- a/include/libserial/serial.hpp +++ b/include/libserial/serial.hpp @@ -330,6 +330,9 @@ size_t getMaxSafeReadSize() const; */ int getBaudRate() const; + +DataLength getDataLength() const; + #ifdef BUILD_TESTING_ON // WARNING: Test helper only! This function bypasses normal initialization // and may leave the Serial object in an inconsistent state. It is intended @@ -476,13 +479,6 @@ uint16_t min_number_char_read_{0}; */ CanonicalMode canonical_mode_{CanonicalMode::ENABLE}; -/** - * @brief Data length setting - * - * Specifies the number of data bits per character (default EIGHT). - */ -DataLength data_length_{DataLength::EIGHT}; - /** * @brief Line terminator character * diff --git a/src/serial.cpp b/src/serial.cpp index 52e520a..2f0efdd 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -208,8 +208,8 @@ void Serial::setBaudRate(BaudRate baud_rate) { } void Serial::setTermios2() { - ssize_t error = ioctl_(fd_serial_port_, TCSETS2, &options_); - if (error < 0) { + int ret = ioctl_(fd_serial_port_, TCSETS2, &options_); + if (ret < 0) { throw SerialException("Error set Termios2: " + std::string(strerror(errno))); } } @@ -223,22 +223,23 @@ void Serial::setWriteTimeout(std::chrono::milliseconds timeout) { } void Serial::setDataLength(DataLength nbits) { - data_length_ = nbits; - this->getTermios2(); - // Clear bits options_.c_cflag &= ~CSIZE; switch (nbits) { case DataLength::FIVE: + std::cout << "FIVE" << std::endl; options_.c_cflag |= CS5; break; case DataLength::SIX: + std::cout << "SIX" << std::endl; options_.c_cflag |= CS6; break; case DataLength::SEVEN: + std::cout << "SEVEN" << std::endl; options_.c_cflag |= CS7; break; case DataLength::EIGHT: + std::cout << "EIGHT" << std::endl; options_.c_cflag |= CS8; break; } @@ -366,6 +367,18 @@ int Serial::getBaudRate() const { return (static_cast(options_.c_ispeed)); } +DataLength Serial::getDataLength() const { + this->getTermios2(); + std::cout << "Getting data length: " << static_cast(options_.c_cflag & CSIZE) << std::endl; + switch (options_.c_cflag & CSIZE) { + case CS5: return DataLength::FIVE; + case CS6: return DataLength::SIX; + case CS7: return DataLength::SEVEN; + case CS8: return DataLength::EIGHT; + default: return DataLength::EIGHT; + } +} + void Serial::getTermios2() const { ssize_t error = ioctl_(fd_serial_port_, TCGETS2, &options_); if (error < 0) { diff --git a/test/test_serial_pty.cpp b/test/test_serial_pty.cpp index 3d3046f..cfae0ca 100644 --- a/test/test_serial_pty.cpp +++ b/test/test_serial_pty.cpp @@ -150,6 +150,45 @@ TEST_F(PseudoTerminalTest, SetAndGetBaudRate) { serial_port.close(); } +// TEST_F(PseudoTerminalTest, SetAndGetDataLength) { +// libserial::Serial serial_port; + +// serial_port.open(slave_port_); + +// // Test multiple data lengths to be more thorough +// std::vector test_lengths = { +// libserial::DataLength::FIVE, +// libserial::DataLength::SIX, +// libserial::DataLength::SEVEN, +// libserial::DataLength::EIGHT +// }; + +// for (const auto& expected_length : test_lengths) { +// // Set data length +// EXPECT_NO_THROW({ +// serial_port.setDataLength(expected_length); +// }); + +// // Add a small delay and flush +// std::this_thread::sleep_for(std::chrono::milliseconds(50)); + +// // Force a re-read of the current settings +// serial_port.close(); +// serial_port.open(slave_port_); + +// // Get data length and verify +// libserial::DataLength actual_length; +// EXPECT_NO_THROW({ +// actual_length = serial_port.getDataLength(); +// }); + +// EXPECT_EQ(actual_length, expected_length) +// << "Failed for data length: " << static_cast(expected_length); +// } + +// serial_port.close(); +// } + TEST_F(PseudoTerminalTest, SetParity) { libserial::Serial serial_port; From c2e50019340369788228a82f44c5237d9b9405e7 Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Tue, 28 Oct 2025 12:22:52 -0300 Subject: [PATCH 05/12] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(serial):=20?= =?UTF-8?q?Rename=20erros=20variable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/serial.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/serial.cpp b/src/serial.cpp index 2f0efdd..8cebae6 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -208,8 +208,8 @@ void Serial::setBaudRate(BaudRate baud_rate) { } void Serial::setTermios2() { - int ret = ioctl_(fd_serial_port_, TCSETS2, &options_); - if (ret < 0) { + ssize_t error = ioctl_(fd_serial_port_, TCSETS2, &options_); + if (error < 0) { throw SerialException("Error set Termios2: " + std::string(strerror(errno))); } } @@ -227,19 +227,15 @@ void Serial::setDataLength(DataLength nbits) { options_.c_cflag &= ~CSIZE; switch (nbits) { case DataLength::FIVE: - std::cout << "FIVE" << std::endl; options_.c_cflag |= CS5; break; case DataLength::SIX: - std::cout << "SIX" << std::endl; options_.c_cflag |= CS6; break; case DataLength::SEVEN: - std::cout << "SEVEN" << std::endl; options_.c_cflag |= CS7; break; case DataLength::EIGHT: - std::cout << "EIGHT" << std::endl; options_.c_cflag |= CS8; break; } From cca3cdd0fc666ec68f8336f7bc2687e9098f355f Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Tue, 28 Oct 2025 12:28:14 -0300 Subject: [PATCH 06/12] =?UTF-8?q?=F0=9F=8E=A8=20style(lint):=20Fix=20style?= =?UTF-8?q?=20erros?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/libserial/serial.hpp | 23 ++++++++++++----------- src/serial.cpp | 10 +++++----- test/test_serial_pty.cpp | 30 +++++++++++++++--------------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/include/libserial/serial.hpp b/include/libserial/serial.hpp index d269032..4d49e55 100644 --- a/include/libserial/serial.hpp +++ b/include/libserial/serial.hpp @@ -349,21 +349,21 @@ void setPollSystemFunction( poll_ = [poll_func](struct pollfd* f, nfds_t n, int t) { return poll_func(f, n, t); }; - } +} void setReadSystemFunction( std::function read_func) { read_ = [read_func](int fd, void* buf, size_t sz) { return read_func(fd, buf, sz); }; - } +} void setIoctlSystemFunction( - std::function ioctl_func) { - ioctl_ = [ioctl_func](int fd, unsigned long request, void* arg) { - return ioctl_func(fd, request, arg); - }; - } + std::function ioctl_func) { // NOLINT + ioctl_ = [ioctl_func](int fd, unsigned long request, void* arg) { // NOLINT + return ioctl_func(fd, request, arg); + }; +} #endif private: @@ -372,10 +372,11 @@ void setIoctlSystemFunction( * * Allows injection of custom ioctl function for testing. */ -std::function ioctl_ = - [](int fd, unsigned long request, void* arg) { - return ::ioctl(fd, request, arg); - }; +std::function ioctl_ = // NOLINT + [](int fd, unsigned long request, + void* arg) { // NOLINT + return ::ioctl(fd, request, arg); + }; /** * @brief Poll system call function wrapper diff --git a/src/serial.cpp b/src/serial.cpp index 8cebae6..cef2109 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -367,11 +367,11 @@ DataLength Serial::getDataLength() const { this->getTermios2(); std::cout << "Getting data length: " << static_cast(options_.c_cflag & CSIZE) << std::endl; switch (options_.c_cflag & CSIZE) { - case CS5: return DataLength::FIVE; - case CS6: return DataLength::SIX; - case CS7: return DataLength::SEVEN; - case CS8: return DataLength::EIGHT; - default: return DataLength::EIGHT; + case CS5: return DataLength::FIVE; + case CS6: return DataLength::SIX; + case CS7: return DataLength::SEVEN; + case CS8: return DataLength::EIGHT; + default: return DataLength::EIGHT; } } diff --git a/test/test_serial_pty.cpp b/test/test_serial_pty.cpp index cfae0ca..f340e93 100644 --- a/test/test_serial_pty.cpp +++ b/test/test_serial_pty.cpp @@ -109,10 +109,10 @@ TEST_F(PseudoTerminalTest, SetTermios2WithFail) { // Inject failure into ioctl for setTermios2 serial_port.setIoctlSystemFunction( - [](int, unsigned long, void*) -> int { - errno = EIO; - return -1; - }); + [](int, unsigned long, void*) -> int { // NOLINT + errno = EIO; + return -1; + }); EXPECT_THROW({ serial_port.setBaudRate(9600); @@ -120,9 +120,9 @@ TEST_F(PseudoTerminalTest, SetTermios2WithFail) { // Restore ioctl function for cleanup serial_port.setIoctlSystemFunction( - [](int fd, unsigned long request, void* arg) -> int { - return ::ioctl(fd, request, arg); - }); + [](int fd, unsigned long request, void* arg) -> int { // NOLINT + return ::ioctl(fd, request, arg); + }); serial_port.close(); } @@ -158,31 +158,31 @@ TEST_F(PseudoTerminalTest, SetAndGetBaudRate) { // // Test multiple data lengths to be more thorough // std::vector test_lengths = { // libserial::DataLength::FIVE, -// libserial::DataLength::SIX, +// libserial::DataLength::SIX, // libserial::DataLength::SEVEN, // libserial::DataLength::EIGHT // }; // for (const auto& expected_length : test_lengths) { // // Set data length -// EXPECT_NO_THROW({ -// serial_port.setDataLength(expected_length); +// EXPECT_NO_THROW({ +// serial_port.setDataLength(expected_length); // }); // // Add a small delay and flush // std::this_thread::sleep_for(std::chrono::milliseconds(50)); - + // // Force a re-read of the current settings // serial_port.close(); // serial_port.open(slave_port_); // // Get data length and verify // libserial::DataLength actual_length; -// EXPECT_NO_THROW({ -// actual_length = serial_port.getDataLength(); +// EXPECT_NO_THROW({ +// actual_length = serial_port.getDataLength(); // }); - -// EXPECT_EQ(actual_length, expected_length) + +// EXPECT_EQ(actual_length, expected_length) // << "Failed for data length: " << static_cast(expected_length); // } From 506be61e14b7edbca2742c676c892b95c571b03f Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Tue, 28 Oct 2025 13:31:04 -0300 Subject: [PATCH 07/12] =?UTF-8?q?=F0=9F=A7=B9=20cleanup(serial):=20Remove?= =?UTF-8?q?=20unecessary=20debug=20message=20output=20and=20blank=20spaces?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/libserial/serial.hpp | 7 +++---- src/serial.cpp | 1 - 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/include/libserial/serial.hpp b/include/libserial/serial.hpp index 4d49e55..65831cd 100644 --- a/include/libserial/serial.hpp +++ b/include/libserial/serial.hpp @@ -373,10 +373,9 @@ void setIoctlSystemFunction( * Allows injection of custom ioctl function for testing. */ std::function ioctl_ = // NOLINT - [](int fd, unsigned long request, - void* arg) { // NOLINT - return ::ioctl(fd, request, arg); - }; + [](int fd, unsigned long request, void* arg) { // NOLINT + return ::ioctl(fd, request, arg); + }; /** * @brief Poll system call function wrapper diff --git a/src/serial.cpp b/src/serial.cpp index cef2109..f596dee 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -365,7 +365,6 @@ int Serial::getBaudRate() const { DataLength Serial::getDataLength() const { this->getTermios2(); - std::cout << "Getting data length: " << static_cast(options_.c_cflag & CSIZE) << std::endl; switch (options_.c_cflag & CSIZE) { case CS5: return DataLength::FIVE; case CS6: return DataLength::SIX; From 5a003daddeed361759a726ca1c17a3e6ec10f909 Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Tue, 28 Oct 2025 13:32:49 -0300 Subject: [PATCH 08/12] =?UTF-8?q?=F0=9F=93=9D=20docs(serial):=20Add=20func?= =?UTF-8?q?tion=20description?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/libserial/serial.hpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/libserial/serial.hpp b/include/libserial/serial.hpp index 65831cd..6597b38 100644 --- a/include/libserial/serial.hpp +++ b/include/libserial/serial.hpp @@ -330,7 +330,13 @@ size_t getMaxSafeReadSize() const; */ int getBaudRate() const; - +/** + * @brief Gets the current data length setting + * + * Retrieves the number of data bits configured for serial communication. + * + * @return The current data length (number of data bits) + */ DataLength getDataLength() const; #ifdef BUILD_TESTING_ON From e63d31dfcc66fda2b179314c6dd3561c5365f919 Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Tue, 28 Oct 2025 13:36:41 -0300 Subject: [PATCH 09/12] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(serial):=20?= =?UTF-8?q?Change=20usingned=20long=20for=20uint=5F64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/libserial/serial.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/libserial/serial.hpp b/include/libserial/serial.hpp index 6597b38..0496164 100644 --- a/include/libserial/serial.hpp +++ b/include/libserial/serial.hpp @@ -365,8 +365,8 @@ void setReadSystemFunction( } void setIoctlSystemFunction( - std::function ioctl_func) { // NOLINT - ioctl_ = [ioctl_func](int fd, unsigned long request, void* arg) { // NOLINT + std::function ioctl_func) { + ioctl_ = [ioctl_func](int fd, uint64_t request, void* arg) { return ioctl_func(fd, request, arg); }; } @@ -378,8 +378,8 @@ void setIoctlSystemFunction( * * Allows injection of custom ioctl function for testing. */ -std::function ioctl_ = // NOLINT - [](int fd, unsigned long request, void* arg) { // NOLINT +std::function ioctl_ = + [](int fd, uint64_t request, void* arg) { return ::ioctl(fd, request, arg); }; From 6d1f02899a80215c5b04787fe39ae6cbaa75008b Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Tue, 28 Oct 2025 18:51:25 -0300 Subject: [PATCH 10/12] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(serial):=20?= =?UTF-8?q?Change=20methods=20ordem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/serial.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/serial.cpp b/src/serial.cpp index f596dee..4c18667 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -194,6 +194,13 @@ void Serial::flushInputBuffer() { } } +void Serial::setTermios2() { + ssize_t error = ioctl_(fd_serial_port_, TCSETS2, &options_); + if (error != 0) { + throw SerialException("Error set Termios2: " + std::string(strerror(errno))); + } +} + void Serial::setBaudRate(unsigned int baud_rate) { this->getTermios2(); options_.c_cflag &= ~CBAUD; @@ -207,13 +214,6 @@ void Serial::setBaudRate(BaudRate baud_rate) { this->setBaudRate(static_cast(baud_rate)); } -void Serial::setTermios2() { - ssize_t error = ioctl_(fd_serial_port_, TCSETS2, &options_); - if (error < 0) { - throw SerialException("Error set Termios2: " + std::string(strerror(errno))); - } -} - void Serial::setReadTimeout(std::chrono::milliseconds timeout) { read_timeout_ms_ = timeout; } From 3d02d6365f56f17785fb6b8c22297e8b46cd98c7 Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Tue, 28 Oct 2025 18:55:58 -0300 Subject: [PATCH 11/12] =?UTF-8?q?=E2=9C=85=20test(serial):=20Fix=20missmat?= =?UTF-8?q?ch=20in=20variables=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_serial_pty.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_serial_pty.cpp b/test/test_serial_pty.cpp index f340e93..2bde929 100644 --- a/test/test_serial_pty.cpp +++ b/test/test_serial_pty.cpp @@ -109,7 +109,7 @@ TEST_F(PseudoTerminalTest, SetTermios2WithFail) { // Inject failure into ioctl for setTermios2 serial_port.setIoctlSystemFunction( - [](int, unsigned long, void*) -> int { // NOLINT + [](int, uint64_t, void*) -> int { // NOLINT errno = EIO; return -1; }); @@ -120,7 +120,7 @@ TEST_F(PseudoTerminalTest, SetTermios2WithFail) { // Restore ioctl function for cleanup serial_port.setIoctlSystemFunction( - [](int fd, unsigned long request, void* arg) -> int { // NOLINT + [](int fd, uint64_t request, void* arg) -> int { // NOLINT return ::ioctl(fd, request, arg); }); From 89d894a892a92dc6d2d8c6b444b7a3c31015949b Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Tue, 28 Oct 2025 19:01:56 -0300 Subject: [PATCH 12/12] =?UTF-8?q?=E2=9C=85=20test(serial):=20Fix=20missmat?= =?UTF-8?q?ch=20in=20parameter=20of=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/libserial/serial.hpp | 12 ++++++++---- src/serial.cpp | 2 +- test/test_serial_pty.cpp | 4 ++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/libserial/serial.hpp b/include/libserial/serial.hpp index 0496164..37dcd95 100644 --- a/include/libserial/serial.hpp +++ b/include/libserial/serial.hpp @@ -364,12 +364,14 @@ void setReadSystemFunction( }; } +/* *INDENT-OFF* */ void setIoctlSystemFunction( - std::function ioctl_func) { - ioctl_ = [ioctl_func](int fd, uint64_t request, void* arg) { + std::function ioctl_func) { // NOLINT + ioctl_ = [ioctl_func](int fd, unsigned long request, void* arg) { // NOLINT return ioctl_func(fd, request, arg); }; } +/* *INDENT-ON* */ #endif private: @@ -378,10 +380,12 @@ void setIoctlSystemFunction( * * Allows injection of custom ioctl function for testing. */ -std::function ioctl_ = - [](int fd, uint64_t request, void* arg) { +/* *INDENT-OFF* */ +std::function ioctl_ = // NOLINT + [](int fd, unsigned long request, void* arg) { // NOLINT return ::ioctl(fd, request, arg); }; +/* *INDENT-ON* */ /** * @brief Poll system call function wrapper diff --git a/src/serial.cpp b/src/serial.cpp index 4c18667..9cedc4f 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -196,7 +196,7 @@ void Serial::flushInputBuffer() { void Serial::setTermios2() { ssize_t error = ioctl_(fd_serial_port_, TCSETS2, &options_); - if (error != 0) { + if (error < 0) { throw SerialException("Error set Termios2: " + std::string(strerror(errno))); } } diff --git a/test/test_serial_pty.cpp b/test/test_serial_pty.cpp index 2bde929..f340e93 100644 --- a/test/test_serial_pty.cpp +++ b/test/test_serial_pty.cpp @@ -109,7 +109,7 @@ TEST_F(PseudoTerminalTest, SetTermios2WithFail) { // Inject failure into ioctl for setTermios2 serial_port.setIoctlSystemFunction( - [](int, uint64_t, void*) -> int { // NOLINT + [](int, unsigned long, void*) -> int { // NOLINT errno = EIO; return -1; }); @@ -120,7 +120,7 @@ TEST_F(PseudoTerminalTest, SetTermios2WithFail) { // Restore ioctl function for cleanup serial_port.setIoctlSystemFunction( - [](int fd, uint64_t request, void* arg) -> int { // NOLINT + [](int fd, unsigned long request, void* arg) -> int { // NOLINT return ::ioctl(fd, request, arg); });