MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SerialBackend.cpp
Go to the documentation of this file.
1#include "SerialBackend.hpp"
2
3#include "StreamBackend.inl"
4
5#if !defined(MAYAFLUX_PLATFORM_WINDOWS)
7
8#include <sys/ioctl.h>
9#include <termios.h>
10#endif
11
12namespace MayaFlux::Core {
13
14namespace {
15
16 using Port = SerialTransport::Stream;
17
18 asio::error_code apply_lines(Port& port, std::optional<bool> dtr, std::optional<bool> rts)
19 {
20#if defined(MAYAFLUX_PLATFORM_WINDOWS)
21 const HANDLE handle = port.native_handle();
22 if (dtr && !EscapeCommFunction(handle, *dtr ? SETDTR : CLRDTR)) {
23 return { static_cast<int>(GetLastError()), asio::system_category() };
24 }
25 if (rts && !EscapeCommFunction(handle, *rts ? SETRTS : CLRRTS)) {
26 return { static_cast<int>(GetLastError()), asio::system_category() };
27 }
28#else
29 const int fd = port.native_handle();
30 const auto drive = [fd](std::optional<bool> level, int line) -> asio::error_code {
31 if (level && ::ioctl(fd, *level ? TIOCMBIS : TIOCMBIC, &line) != 0) {
32 return { errno, asio::system_category() };
33 }
34 return {};
35 };
36 if (const auto ec = drive(dtr, TIOCM_DTR)) {
37 return ec;
38 }
39 if (const auto ec = drive(rts, TIOCM_RTS)) {
40 return ec;
41 }
42#endif
43 return {};
44 }
45
46 asio::serial_port_base::parity::type parity_of(SerialParity parity)
47 {
48 switch (parity) {
50 return asio::serial_port_base::parity::odd;
52 return asio::serial_port_base::parity::even;
54 break;
55 }
56 return asio::serial_port_base::parity::none;
57 }
58
59 asio::serial_port_base::stop_bits::type stop_bits_of(SerialStopBits stop_bits)
60 {
61 switch (stop_bits) {
63 return asio::serial_port_base::stop_bits::onepointfive;
65 return asio::serial_port_base::stop_bits::two;
67 break;
68 }
69 return asio::serial_port_base::stop_bits::one;
70 }
71
72 asio::serial_port_base::flow_control::type flow_control_of(SerialFlowControl flow)
73 {
74 switch (flow) {
76 return asio::serial_port_base::flow_control::software;
78 return asio::serial_port_base::flow_control::hardware;
80 break;
81 }
82 return asio::serial_port_base::flow_control::none;
83 }
84
85} // namespace
86
87asio::error_code SerialTransport::open(Stream& port, const EndpointInfo& info)
88{
89 const SerialLineSettings& line = info.serial;
90 if (info.remote_path.empty() || line.data_bits < 5 || line.data_bits > 8) {
91 return asio::error::invalid_argument;
92 }
93
94 asio::error_code ec;
95 port.open(info.remote_path, ec);
96 if (ec) {
97 return ec;
98 }
99
100 const auto fail = [&port](const asio::error_code& cause) {
101 asio::error_code ignored;
102 port.close(ignored);
103 return cause;
104 };
105
106 port.set_option(asio::serial_port_base::character_size(line.data_bits), ec);
107 if (ec) {
108 return fail(ec);
109 }
110 port.set_option(asio::serial_port_base::parity(parity_of(line.parity)), ec);
111 if (ec) {
112 return fail(ec);
113 }
114 port.set_option(asio::serial_port_base::stop_bits(stop_bits_of(line.stop_bits)), ec);
115 if (ec) {
116 return fail(ec);
117 }
118 port.set_option(asio::serial_port_base::flow_control(flow_control_of(line.flow_control)), ec);
119 if (ec) {
120 return fail(ec);
121 }
122
123 port.set_option(asio::serial_port_base::baud_rate(line.baud_rate), ec);
124#if !defined(MAYAFLUX_PLATFORM_WINDOWS)
125 if (ec == asio::error::invalid_argument) {
126 const int error = detail::set_custom_baud(port.native_handle(), line.baud_rate);
127 ec = error == 0 ? asio::error_code {} : asio::error_code(error, asio::system_category());
128 }
129#endif
130 if (ec) {
131 return fail(ec);
132 }
133
134#if defined(MAYAFLUX_PLATFORM_WINDOWS)
135 ec = apply_lines(port, line.dtr_on_open, line.rts_on_open);
136#else
137 ec = apply_lines(port,
138 line.dtr_on_open ? std::nullopt : std::optional<bool> { false },
139 line.rts_on_open ? std::nullopt : std::optional<bool> { false });
140#endif
141 if (ec) {
142 return fail(ec);
143 }
144
145 return {};
146}
147
148std::chrono::milliseconds SerialTransport::settle_time(const EndpointInfo& info)
149{
150 return std::chrono::milliseconds(info.serial.settle_ms);
151}
152
154{
155#if defined(MAYAFLUX_PLATFORM_WINDOWS)
156 PurgeComm(port.native_handle(), PURGE_RXCLEAR);
157#else
158 ::tcflush(port.native_handle(), TCIFLUSH);
159#endif
160}
161
162bool SerialTransport::set_control_lines(Stream& port, std::optional<bool> dtr, std::optional<bool> rts)
163{
164 return !apply_lines(port, dtr, rts);
165}
166
168{
169 asio::error_code ec;
170 static_cast<void>(port.send_break(ec));
171 return !ec;
172}
173
175{
176 return info.remote_path;
177}
178
180{
181 return info.remote_path;
182}
183
184template class MAYAFLUX_API StreamBackend<SerialTransport>;
185
186} // namespace MayaFlux::Core
Connection-oriented reliable message transport over an Asio stream protocol.
int set_custom_baud(int, uint32_t)
Set a baud rate the portable serial option table does not accept.
SerialStopBits
Stop bits of a serial line.
SerialParity
Parity bit of a serial line.
SerialFlowControl
Flow control of a serial line: XON/XOFF in software, or RTS/CTS in hardware.
Vruta::SoundRoutine line(float start_value, float end_value, float duration_seconds, uint32_t step_duration, bool restartable)
Creates a continuous interpolation generator between two values over time.
Definition Tasks.cpp:60
SerialLineSettings serial
Serial: the line setup applied at open. The device path is remote_path.
std::string remote_path
UDS: socket to connect to, given the same way as local_path.
Describes one logical send/receive endpoint managed by a backend.
Line setup applied when a serial endpoint opens.
static std::string peer_label(const EndpointInfo &info)
static std::chrono::milliseconds settle_time(const EndpointInfo &info)
static asio::error_code open(Stream &port, const EndpointInfo &info)
static void ready(Stream &port, const EndpointInfo &info)
static bool set_control_lines(Stream &port, std::optional< bool > dtr, std::optional< bool > rts)
static bool send_break(Stream &port)
static std::string describe(const EndpointInfo &info)