Skip to content

Feat raw data#22

Open
NestorDP wants to merge 34 commits intomainfrom
feat-raw-data
Open

Feat raw data#22
NestorDP wants to merge 34 commits intomainfrom
feat-raw-data

Conversation

@NestorDP
Copy link
Copy Markdown
Owner

Summary

  • Inclide method to hadle read and write raw data

Changes

  • API changes
  • Behavior changes
  • Docs updated
  • Tests added/updated

Details

How to test

Screenshots/Logs (optional)

Checklist

  • Builds locally (Release + Debug)
  • All tests pass (if applicable)
  • Lint/format pass: make cppcheck / make cpplint / make uncrustify
  • [x] Example(s) tested (if touched)
  • Documentation updated (README/docs)

Risks and rollbacks

@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented Apr 21, 2026

Not up to standards ⛔

🔴 Issues 3 critical

Alerts:
⚠ 3 issues (≤ 0 issues of at least minor severity)

Results:
3 new issues

Category Results
Security 3 critical

View in Codacy

🟢 Metrics 59 complexity · -7 duplication

Metric Results
Complexity 59
Duplication -7

View in Codacy

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.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with std::string& / std::string_view variants and added writeRaw() / readRaw().
  • Expanded PTY-based tests to cover raw read/write behavior and error handling.
  • Added BUILD_APP CMake option to build/install a serialctl CLI.

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.

Comment thread test/test_serial_simple.cpp Outdated
Comment thread src/serial.cpp
Comment thread include/libserial/serial.hpp
Comment thread include/libserial/serial.hpp Outdated
Comment thread test/test_ports.cpp Outdated
Comment thread test/test_serial_simple.cpp Outdated
Comment thread tools/serialctl.cpp Outdated
Comment thread src/serial.cpp
Comment thread include/libserial/serial.hpp
Comment thread test/test_serial_simple.cpp Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

❌ Patch coverage is 93.58974% with 20 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/serial.cpp 81.30% 20 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(...) and Serial::readRaw(...) for raw byte I/O, including test hooks for injecting write() behavior.
  • Change string-based I/O APIs to use std::string& (read) and std::string_view (write), updating tests accordingly.
  • Add an optionally-built/installable serialctl CLI via BUILD_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.

Comment thread src/serial.cpp
Comment thread include/libserial/serial.hpp
Comment on lines 93 to +161
@@ -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);
Comment thread src/serial.cpp
Comment on lines +66 to 69
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)));
Comment thread test/test_serial_simple.cpp
Comment on lines +190 to +191
* @throws SerialException if read operation fails
* @throws std::invalid_argument if buffer pointer is null
Comment thread src/serial.cpp
Comment on lines +39 to +47
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));
Comment thread src/serial.cpp
if (ret < 0) {
if (errno == EINTR) continue;
throw IOException("Error writing raw data: " + std::string(strerror(errno)));
}
Comment thread src/serial.cpp
Comment on lines 113 to 121
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");
}
Comment thread tools/serialctl.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants