3#include "StreamBackend.inl"
5#if !defined(MAYAFLUX_PLATFORM_WINDOWS)
18 asio::error_code apply_lines(Port& port, std::optional<bool> dtr, std::optional<bool> rts)
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() };
25 if (rts && !EscapeCommFunction(handle, *rts ? SETRTS : CLRRTS)) {
26 return {
static_cast<int>(GetLastError()), asio::system_category() };
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() };
36 if (
const auto ec = drive(dtr, TIOCM_DTR)) {
39 if (
const auto ec = drive(rts, TIOCM_RTS)) {
46 asio::serial_port_base::parity::type parity_of(
SerialParity parity)
50 return asio::serial_port_base::parity::odd;
52 return asio::serial_port_base::parity::even;
56 return asio::serial_port_base::parity::none;
59 asio::serial_port_base::stop_bits::type stop_bits_of(
SerialStopBits stop_bits)
63 return asio::serial_port_base::stop_bits::onepointfive;
65 return asio::serial_port_base::stop_bits::two;
69 return asio::serial_port_base::stop_bits::one;
72 asio::serial_port_base::flow_control::type flow_control_of(
SerialFlowControl flow)
76 return asio::serial_port_base::flow_control::software;
78 return asio::serial_port_base::flow_control::hardware;
82 return asio::serial_port_base::flow_control::none;
90 if (info.
remote_path.empty() || line.data_bits < 5 || line.data_bits > 8) {
91 return asio::error::invalid_argument;
100 const auto fail = [&port](
const asio::error_code& cause) {
101 asio::error_code ignored;
106 port.set_option(asio::serial_port_base::character_size(line.data_bits), ec);
110 port.set_option(asio::serial_port_base::parity(parity_of(line.parity)), ec);
114 port.set_option(asio::serial_port_base::stop_bits(stop_bits_of(line.stop_bits)), ec);
118 port.set_option(asio::serial_port_base::flow_control(flow_control_of(line.flow_control)), ec);
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) {
127 ec = error == 0 ? asio::error_code {} : asio::error_code(error, asio::system_category());
134#if defined(MAYAFLUX_PLATFORM_WINDOWS)
135 ec = apply_lines(port, line.dtr_on_open, line.rts_on_open);
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 });
155#if defined(MAYAFLUX_PLATFORM_WINDOWS)
156 PurgeComm(port.native_handle(), PURGE_RXCLEAR);
158 ::tcflush(port.native_handle(), TCIFLUSH);
164 return !apply_lines(port, dtr, rts);
170 static_cast<void>(port.send_break(ec));
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.
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)