Skip to content

Commit 31eb26c

Browse files
authored
Merge pull request #5 from NestorDP/feat-control-data
Feat control data
2 parents 280b949 + 3df2327 commit 31eb26c

File tree

10 files changed

+362
-83
lines changed

10 files changed

+362
-83
lines changed

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

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

include/libserial/serial.hpp

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ int getAvailableData() const;
196196
* timing out. A value of 0 means no timeout (blocking).
197197
*
198198
* @param timeout Timeout in milliseconds
199+
* @throws SerialException if setting cannot be applied
199200
*/
200201
void setReadTimeout(unsigned int timeout);
201202

@@ -206,14 +207,87 @@ void setReadTimeout(unsigned int timeout);
206207
* timing out. A value of 0 means no timeout (blocking).
207208
*
208209
* @param timeout Timeout in milliseconds
210+
* @throws SerialException if setting cannot be applied
209211
*/
210212
void setWriteTimeout(unsigned int timeout);
211213

212-
// Future planned methods (not yet implemented):
213-
// void setNumberBits(NumBits num_bits);
214-
// void setParity(Parity parity);
215-
// void setStopBits(StopBits stop_bits);
214+
/**
215+
* @brief Sets the number of data bits per byte
216+
*
217+
* Configures the number of data bits used in each byte of serial
218+
* communication.
219+
*
220+
* @param nbits The desired number of data bits (5, 6, 7, or 8)
221+
* @throws SerialException if number of bits cannot be set
222+
*/
223+
void setDataLength(DataLength nbits);
224+
225+
/**
226+
* @brief Sets the parity configuration
227+
*
228+
* Configures the parity checking mechanism for serial communication.
229+
*
230+
* @param parity The desired parity setting (ENABLE or DISABLE)
231+
* @throws SerialException if parity cannot be set
232+
*/
233+
void setParity([[maybe_unused]] Parity parity);
234+
235+
/**
236+
* @brief Sets the stop bits configuration
237+
*
238+
* Configures the number of stop bits used in serial communication.
239+
*
240+
* @param stop_bits The desired stop bits setting (ONE, ONE_AND_HALF, or TWO)
241+
* @throws SerialException if stop bits cannot be set
242+
*/
243+
void setStopBits([[maybe_unused]] StopBits stop_bits);
244+
245+
/**
246+
* @brief Sets the flow control configuration
247+
*
248+
* Configures the flow control mechanism for serial communication.
249+
*
250+
* @param flow_control The desired flow control setting (HARDWARE or SOFTWARE)
251+
* @throws SerialException if flow control cannot be set
252+
*/
253+
void setFlowControl([[maybe_unused]] FlowControl flow_control);
254+
255+
/**
256+
* @brief Sets canonical mode for input processing
257+
*
258+
* Configures whether the input is processed in canonical (line-based)
259+
* mode or non-canonical (raw) mode.
260+
*
261+
* @param canonical_mode The desired canonical mode setting (ENABLE or DISABLE)
262+
* @throws SerialException if canonical mode cannot be set
263+
*/
264+
void setCanonicalMode([[maybe_unused]] CanonicalMode canonical_mode);
265+
266+
/**
267+
* @brief Sets the terminator character for readUntil operations
268+
*
269+
* Configures the character that signals the end of a readUntil operation.
270+
*
271+
* @param term The desired terminator character
272+
* @throws SerialException if terminator cannot be set
273+
*/
274+
void setTerminator([[maybe_unused]] Terminator term);
216275

276+
/**
277+
* @brief Sets the read timeout in deciseconds
278+
*
279+
* @param time Timeout in deciseconds
280+
* @throws SerialException if setting cannot be applied
281+
*/
282+
void setTimeOut([[maybe_unused]] int time);
283+
284+
/**
285+
* @brief Sets the minimum number of characters to read
286+
*
287+
* @param num Minimum number of characters to read
288+
* @throws SerialException if setting cannot be applied
289+
*/
290+
void setMinNumberCharRead([[maybe_unused]] int num);
217291

218292
private:
219293
/**

include/libserial/serial_types.hpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@
77

88
namespace libserial {
99

10-
struct DeviceStruct {
11-
std::string name;
12-
std::string port_path;
13-
std::string bus_path;
14-
uint16_t id;
15-
};
16-
1710
/**
1811
* @enum Parity
1912
* @brief Enumeration for serial port parity configuration
@@ -38,12 +31,10 @@ enum class Parity {
3831
* serial communication. Common configurations include 1 stop bit (standard)
3932
* or 2 stop bits (for slower or noisy communication).
4033
*
41-
* @note This enum currently provides basic enable/disable functionality.
42-
* Future versions may include specific stop bit counts (ONE, TWO).
4334
*/
4435
enum class StopBits {
45-
ENABLE, ///< Enable stop bits configuration
46-
DISABLE, ///< Disable stop bits configuration
36+
ONE, ///< One stop bit
37+
TWO, ///< Two stop bits
4738
};
4839

4940
/**
@@ -84,6 +75,7 @@ enum class CanonicalMode {
8475
enum class Terminator {
8576
EOT = 4, ///< End of Transmission (ASCII 4, Ctrl+D)
8677
CR = 13, ///< Carriage Return (ASCII 13, '\r')
78+
LF = 10 ///< Line Feed (ASCII 10, '\n')
8779
};
8880

8981
/**
@@ -130,6 +122,22 @@ enum class BaudRate {
130122
BAUD_RATE_230400 = 230400 ///< 230400 bps - Very high speed applications
131123
};
132124

125+
/**
126+
* @enum DataLength
127+
* @brief Enumeration for serial port data bits configuration
128+
*
129+
* Data bits define the number of bits used to represent each byte of data
130+
* in serial communication. Common configurations include 5, 6, 7, or 8 data
131+
* bits per byte. The choice of data bits affects the range of values that
132+
* can be transmitted and may need to match between communicating devices.
133+
*/
134+
enum class DataLength {
135+
FIVE = 5, ///< 5 data bits per byte
136+
SIX = 6, ///< 6 data bits per byte
137+
SEVEN = 7, ///< 7 data bits per byte
138+
EIGHT = 8 ///< 8 data bits per byte
139+
};
140+
133141
} // namespace libserial
134142

135143
#endif // INCLUDE_LIBSERIAL_SERIAL_TYPES_HPP_

0 commit comments

Comments
 (0)