Skip to content

Commit 15c8b35

Browse files
committed
Merge branch 'main' into create-templates
2 parents f44b04e + f26b551 commit 15c8b35

File tree

14 files changed

+468
-85
lines changed

14 files changed

+468
-85
lines changed

.github/workflows/coverage.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: coverage
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
workflow_dispatch:
11+
12+
jobs:
13+
coverage:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
20+
- name: Install system dependencies
21+
run: |
22+
sudo apt-get update
23+
sudo apt-get install -y \
24+
build-essential \
25+
cmake \
26+
git \
27+
lcov \
28+
python3 \
29+
python3-pip
30+
31+
- name: Install Google Test
32+
run: |
33+
git clone https://github.com/google/googletest.git -b v1.14.0
34+
cd googletest
35+
cmake .
36+
make -j$(nproc)
37+
sudo make install
38+
39+
- name: Configure CMake with coverage
40+
run: |
41+
mkdir -p build
42+
cd build
43+
cmake .. \
44+
-DCMAKE_BUILD_TYPE=Debug \
45+
-DBUILD_TESTING=ON \
46+
-DBUILD_COVERAGE=ON
47+
48+
- name: Build
49+
run: |
50+
cd build
51+
cmake --build . -- -j$(nproc)
52+
53+
- name: Run tests with coverage
54+
run: |
55+
cd build
56+
make coverage
57+
58+
- name: Generate coverage summary
59+
run: |
60+
cd build
61+
make coverage-summary
62+
63+
- name: Upload coverage HTML report
64+
uses: actions/upload-artifact@v4
65+
with:
66+
name: coverage-report
67+
path: build/coverage_html/
68+
retention-days: 30
69+
70+
- name: Upload coverage to Codecov (optional)
71+
if: github.event_name == 'push' || github.event_name == 'pull_request'
72+
uses: codecov/codecov-action@v4
73+
with:
74+
file: build/coverage.info.cleaned
75+
flags: unittests
76+
name: codecov-umbrella
77+
fail_ci_if_error: false
78+
token: ${{ secrets.CODECOV_TOKEN }}
79+
80+
- name: Display coverage summary in logs
81+
if: always()
82+
run: |
83+
cd build
84+
if [ -f coverage.info.cleaned ]; then
85+
echo "=== Coverage Summary ==="
86+
lcov --summary coverage.info.cleaned
87+
else
88+
echo "Coverage file not found"
89+
fi

.github/workflows/lint.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
name: Lint
22

33
on:
4+
push:
5+
branches:
6+
- main
47
pull_request:
5-
branches: [main]
8+
branches:
9+
- main
10+
workflow_dispatch:
11+
612
jobs:
713
lint:
814
runs-on: ubuntu-latest

.github/workflows/unit-tests.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
name: unit-tests
22

33
on:
4+
push:
5+
branches:
6+
- main
47
pull_request:
5-
branches: [main]
8+
branches:
9+
- main
10+
workflow_dispatch:
611

712
jobs:
813
build-and-test:

CPPLINT.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
filter=-build/include_order,-whitespace/line_length,-whitespace/access-members,-whitespace/indent,-whitespace/newline,-readability/braces
1+
filter=-build/include_order,-whitespace/line_length,-whitespace/access-members,-whitespace/indent,-whitespace/newline,-readability/braces, -runtime/string
22
linelength=120

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CppSerial
22

3+
[![unit-tests](https://github.com/NestorDP/cppserial/actions/workflows/unit-tests.yml/badge.svg)](https://github.com/NestorDP/cppserial/actions/workflows/unit-tests.yml)
4+
[![coverage](https://github.com/NestorDP/cppserial/actions/workflows/coverage.yml/badge.svg)](https://github.com/NestorDP/cppserial/actions/workflows/coverage.yml)
5+
[![codecov](https://codecov.io/gh/NestorDP/cppserial/branch/main/graph/badge.svg)](https://codecov.io/gh/NestorDP/cppserial)
6+
37
**C++ Application Programming Interface for the `asm/termbits.h` C library**
48

59
---

examples/list_ports.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
// Example: list serial ports using Ports class
44

55
#include <iostream>
6+
#include <vector>
7+
68
#include "libserial/ports.hpp"
9+
#include "libserial/device.hpp"
710

811
int main() {
912
libserial::Ports ports;
@@ -18,6 +21,16 @@ int main() {
1821
auto bus_path = ports.findBusPath(i);
1922
std::cout << " [" << i << "] " << name.value_or("unknown") << " -> " << port_path.value_or("unknown") << " (bus: " << bus_path.value_or("unknown") << ")\n";
2023
}
24+
std::vector<libserial::Device> devices;
25+
ports.getDevices(devices);
26+
27+
std::cout << "\nRetrieving device list via getDevices() method:\n";
28+
for (const auto& device : devices) {
29+
std::cout << "Device Name: " << device.getName() << "\n";
30+
std::cout << "Port Path: " << device.getPortPath() << "\n";
31+
std::cout << "Bus Path: " << device.getBusPath() << "\n";
32+
std::cout << "Device ID: " << device.getId() << "\n";
33+
}
2134

2235
return 0;
2336
}

include/libserial/device.hpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// @ Copyright 2022-2025 Nestor Neto
2+
3+
#ifndef INCLUDE_LIBSERIAL_DEVICE_HPP_
4+
#define INCLUDE_LIBSERIAL_DEVICE_HPP_
5+
6+
#include <string>
7+
#include <cstdint>
8+
9+
namespace libserial {
10+
11+
/**
12+
* @brief A class representing a serial device
13+
*
14+
* The Device class encapsulates the properties of a serial device,
15+
* including its name, port path, bus path, and unique identifier.
16+
*
17+
* @author Nestor Pereira Neto
18+
*/
19+
class Device {
20+
public:
21+
/**
22+
* @brief Default constructor of the Device class
23+
*
24+
*/
25+
Device() = default;
26+
27+
/**
28+
* @brief Default destructor of the Device class
29+
*
30+
*/
31+
~Device() = default;
32+
33+
/**
34+
* @brief Parameterized constructor of the Device class
35+
*
36+
* @param name The name of the device
37+
* @param port_path The port path of the device
38+
* @param bus_path The bus path of the device
39+
* @param id The unique identifier of the device
40+
*/
41+
Device(const std::string& name,
42+
const std::string& port_path,
43+
const std::string& bus_path,
44+
uint16_t id);
45+
46+
/**
47+
* @brief Retrieves the name of the device
48+
*
49+
* @return std::string The name of the device
50+
*/
51+
std::string getName() const;
52+
53+
/**
54+
* @brief Retrieves the port path of the device
55+
*
56+
* @return std::string The port path of the device
57+
*/
58+
std::string getPortPath() const;
59+
60+
/**
61+
* @brief Retrieves the bus path of the device
62+
*
63+
* @return std::string The bus path of the device
64+
*/
65+
std::string getBusPath() const;
66+
67+
/**
68+
* @brief Retrieves the unique identifier of the device
69+
*
70+
* @return uint16_t The unique identifier of the device
71+
*/
72+
uint16_t getId() const;
73+
74+
/**
75+
* @brief Sets the name of the device
76+
*
77+
* @param name The name to set
78+
*/
79+
void setName(const std::string& name);
80+
81+
/**
82+
* @brief Sets the port path of the device
83+
*
84+
* @param port_path The port path to set
85+
*/
86+
void setPortPath(const std::string& port_path);
87+
88+
/**
89+
* @brief Sets the bus path of the device
90+
*
91+
* @param bus_path The bus path to set
92+
*/
93+
void setBusPath(const std::string& bus_path);
94+
95+
/**
96+
* @brief Sets the unique identifier of the device
97+
*
98+
* @param id The unique identifier to set
99+
*/
100+
void setId(uint16_t id);
101+
102+
private:
103+
/**
104+
* @brief The name of the device
105+
*/
106+
std::string name_{"unknown"};
107+
108+
/**
109+
* @brief The port path of the device
110+
*/
111+
std::string port_path_{"unknown"};
112+
113+
/**
114+
* @brief The bus path of the device
115+
*/
116+
std::string bus_path_{"unknown"};
117+
118+
/**
119+
* @brief The unique identifier of the device
120+
*/
121+
uint16_t id_{0};
122+
};
123+
} // namespace libserial
124+
125+
#endif // INCLUDE_LIBSERIAL_DEVICE_HPP_

include/libserial/ports.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <algorithm>
1818
#include <optional>
1919

20+
#include "libserial/device.hpp"
2021
#include "libserial/serial_exception.hpp"
2122
#include "libserial/serial_types.hpp"
2223

@@ -49,22 +50,27 @@ Ports() = default;
4950
* @brief Scans the system for available serial ports
5051
*
5152
* @return uint16_t The number of serial ports found
53+
* @throws SerialException if scanning fails
54+
* @throws SerialException if no ports are found
55+
* @throws PermissionDeniedException if insufficient permissions
5256
*/
5357
uint16_t scanPorts();
5458

5559
/**
5660
* @brief Retrieves the list of detected serial devices
5761
*
58-
* @param list A reference to a vector that will be populated with
59-
* DeviceStruct entries for each detected device
62+
* @param devices A reference to a vector that will be populated with
63+
* Device entries for each detected device
64+
* @throws SerialException if device list cannot be retrieved
6065
*/
61-
void getDeviceList(std::vector<DeviceStruct> & list) const;
66+
void getDevices(std::vector<Device> & devices) const;
6267

6368
/**
6469
* @brief Finds the port path for a device with the specified ID
6570
*
6671
* @param id The unique identifier of the device to search for
6772
* @return std::optional<std::string> The port path if found, or std::nullopt if not found
73+
* @throws SerialException if search operation fails
6874
*/
6975
std::optional<std::string> findPortPath(uint16_t id) const;
7076

@@ -73,6 +79,7 @@ std::optional<std::string> findPortPath(uint16_t id) const;
7379
*
7480
* @param id The unique identifier of the device to search for
7581
* @return std::optional<std::string> The bus path if found, or std::nullopt if not found
82+
* @throws SerialException if search operation fails
7683
*/
7784
std::optional<std::string> findBusPath(uint16_t id) const;
7885

@@ -81,6 +88,7 @@ std::optional<std::string> findBusPath(uint16_t id) const;
8188
*
8289
* @param id The unique identifier of the device to search for
8390
* @return std::optional<std::string> The name if found, or std::nullopt if not found
91+
* @throws SerialException if search operation fails
8492
*/
8593
std::optional<std::string> findName(uint16_t id) const;
8694

@@ -93,7 +101,7 @@ static constexpr const char* kSysSerialByIdPath = "/dev/serial/by-id/";
93101
/**
94102
* @brief Internal list of detected serial devices
95103
*/
96-
std::vector<DeviceStruct> device_list_;
104+
std::vector<Device> devices_;
97105
};
98106
} // namespace libserial
99107

0 commit comments

Comments
 (0)