Conversation
…nd readUntil functions and it's tests
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| Security | 3 critical |
🟢 Metrics 59 complexity · -7 duplication
Metric Results Complexity 59 Duplication -7
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
Pull request overview
Adds raw byte read/write support to libserial::Serial, updates the unit tests to use the new non-shared_ptr APIs, and introduces an optional serialctl CLI build target.
Changes:
- Replaced
shared_ptr<std::string>read/write APIs withstd::string&/std::string_viewvariants and addedwriteRaw()/readRaw(). - Expanded PTY-based tests to cover raw read/write behavior and error handling.
- Added
BUILD_APPCMake option to build/install aserialctlCLI.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/serialctl.cpp | New CLI for listing serial ports and printing basic info. |
| src/serial.cpp | Implements new raw read/write APIs and updates existing read/write signatures/behavior. |
| include/libserial/serial.hpp | Updates public API declarations and adds test hooks for injected write() syscall. |
| test/test_serial_simple.cpp | Updates simple unit tests for new string/string_view APIs; adds writeRaw validation tests. |
| test/test_serial_pty.cpp | Updates PTY integration tests; adds coverage for raw read/write scenarios. |
| test/test_ports.cpp | Adjusts device ordering assertions for scanned devices. |
| CMakeLists.txt | Adds BUILD_APP option to build/install the new serialctl tool. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
This PR introduces raw (byte-buffer) read/write support in libserial::Serial, modernizes the string-based read/write API away from std::shared_ptr<std::string>, and adds an optional CLI (serialctl) to list available serial ports.
Changes:
- Add
Serial::writeRaw(...)andSerial::readRaw(...)for raw byte I/O, including test hooks for injectingwrite()behavior. - Change string-based I/O APIs to use
std::string&(read) andstd::string_view(write), updating tests accordingly. - Add an optionally-built/installable
serialctlCLI viaBUILD_APP.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 14 comments.
Show a summary per file
| File | Description |
|---|---|
include/libserial/serial.hpp |
Updates public Serial API signatures, adds raw I/O declarations, and extends test injection hooks. |
src/serial.cpp |
Implements raw I/O, updates existing read/write implementations, and adjusts port open behavior. |
test/test_serial_simple.cpp |
Updates unit tests for the new string/reference APIs and adds basic writeRaw validation tests. |
test/test_serial_pty.cpp |
Refactors PTY tests to use the new APIs and adds comprehensive writeRaw/readRaw behavior tests. |
test/test_ports.cpp |
Makes port-name assertions order-independent. |
tools/serialctl.cpp |
Adds a small CLI to list detected serial ports. |
CMakeLists.txt |
Adds BUILD_APP option and installs serialctl when enabled. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -113,31 +135,30 @@ void write(std::shared_ptr<std::string> data); | |||
| * memory management and avoids unnecessary string copies. Just works | |||
| * in canonical mode. | |||
| * | |||
| * @param buffer Shared pointer to string where data will be stored | |||
| * @param buffer Pointer to string where data will be stored | |||
| * @return Number of bytes actually read | |||
| * @throws SerialException if read operation fails | |||
| * @throws SerialException if buffer is null | |||
| * @throws libserial::IOException if the read operation fails | |||
| * | |||
| * @note The buffer will be resized to contain exactly the read data | |||
| */ | |||
| size_t read(std::shared_ptr<std::string> buffer); | |||
| size_t read(std::string & buffer); | |||
|
|
|||
| /** | |||
| * @brief Reads a specific number of bytes from the serial port | |||
| * | |||
| * Reads exactly num_bytes from the serial port and stores them | |||
| * in the provided shared string buffer. Just works in non-canonical mode. | |||
| * | |||
| * @param buffer Shared pointer to string where data will be stored | |||
| * @param buffer Pointer to string where data will be stored | |||
| * @param num_bytes Number of bytes to read | |||
| * @return Number of bytes actually read | |||
| * @throws SerialException if read operation fails | |||
| * @throws SerialException if buffer is null | |||
| * @throws SerialException if num_bytes is zero | |||
| * @throws libserial::IOException if the read operation fails | |||
| * @throws std::invalid_argument if buffer is null | |||
| * @throws std::invalid_argument if num_bytes is zero | |||
| * | |||
| * @note The buffer will be resized to contain exactly the read data | |||
| */ | |||
| size_t readBytes(std::shared_ptr<std::string> buffer, size_t num_bytes); | |||
| size_t readBytes(std::string & buffer, size_t num_bytes); | |||
| ssize_t bytes_written = write_(fd_serial_port_, data.data(), data.size()); | ||
|
|
||
| if (bytes_written < 0) { | ||
| throw IOException("Error writing to serial port: " + std::string(strerror(errno))); |
| * @throws SerialException if read operation fails | ||
| * @throws std::invalid_argument if buffer pointer is null |
| if (flags == -1) { | ||
| int saved_errno = errno; | ||
| this->close(); | ||
| throw SerialException("Error configuring port " + port + ": " + strerror(saved_errno)); | ||
| } | ||
| if (::fcntl(fd_serial_port_, F_SETFL, flags & ~O_NONBLOCK) == -1) { | ||
| int saved_errno = errno; | ||
| this->close(); | ||
| throw SerialException("Error configuring port " + port + ": " + strerror(saved_errno)); |
| if (ret < 0) { | ||
| if (errno == EINTR) continue; | ||
| throw IOException("Error writing raw data: " + std::string(strerror(errno))); | ||
| } |
| int timeout_ms = static_cast<int>(read_timeout_ms_.count()); | ||
| int pr = poll_(&fd_poll, 1, timeout_ms); | ||
| if (pr < 0) { | ||
| int poll_result = poll_(&fd_poll, 1, timeout_ms); | ||
| if (poll_result < 0) { | ||
| throw IOException(std::string("Error in poll(): ") + strerror(errno)); | ||
| } | ||
| if (pr == 0) { | ||
| if (poll_result == 0) { | ||
| throw IOException("Read operation timed out after " + std::to_string(timeout_ms) + | ||
| " milliseconds"); | ||
| } |
Summary
Changes
Details
How to test
Screenshots/Logs (optional)
Checklist
make cppcheck/make cpplint/make uncrustify[x] Example(s) tested (if touched)Risks and rollbacks