MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MayaFlux::Core::SerialDevice Class Reference

A serial port with a protocol on it: framing, parsing, commands and replies. More...

#include <SerialDevice.hpp>

Collaboration diagram for MayaFlux::Core::SerialDevice:

Classes

struct  Pending

Public Types

using ReaderToken = uint64_t
using ReplyCallback = std::function<void(SerialReply how, Transitive::Protocol::Message reply)>
using ReplyMatcher = std::function<bool(Transitive::Protocol::Message message)>
using StateCallback = ISerialLink::StateCallback
using ValueCallback = std::function<void(const InputValue&)>

Public Member Functions

ReaderToken add_reader (SerialSpec spec, uint32_t id, ValueCallback callback)
 Read the same bytes a second way.
void close ()
 Close the port.
uint32_t device_id () const
bool open (const std::string &path)
 Open the port at path with the spec's line setup.
SerialDevice & operator= (const SerialDevice &)=delete
SerialDevice & operator= (SerialDevice &&)=delete
void remove_reader (ReaderToken token)
 Stop a reader.
bool request (std::span< const uint8_t > command, ReplyMatcher matcher, std::chrono::milliseconds timeout, ReplyCallback done)
 Send a command and wait for its reply.
bool send (std::span< const uint8_t > bytes)
 Send bytes as they are.
bool send_break ()
bool send_framed (std::span< const uint8_t > message)
 Send one message framed the way the spec reads them.
 SerialDevice (const SerialDevice &)=delete
 SerialDevice (SerialDevice &&)=delete
 SerialDevice (uint32_t device_id, SerialSpec spec, std::unique_ptr< ISerialLink > link)
bool set_control_lines (std::optional< bool > dtr, std::optional< bool > rts)
void set_state_callback (StateCallback callback)
void set_value_callback (ValueCallback callback)
const SerialSpec & spec () const
EndpointState state () const
SerialDeviceStats stats () const
size_t write_backlog () const
 Bytes accepted for sending and not yet sent.
 ~SerialDevice ()

Private Types

using ReaderList = std::vector<std::pair<ReaderToken, std::shared_ptr<SerialReader>>>

Private Member Functions

bool complete_request (Transitive::Protocol::Message frame)
void end_request (const std::shared_ptr< Pending > &request, SerialReply how, Transitive::Protocol::Message reply)
void handle_chunk (std::span< const uint8_t > chunk)
void handle_state (EndpointState previous, EndpointState current)
bool is_pending (const std::shared_ptr< Pending > &request) const
std::shared_ptr< Pending > take_pending (const std::shared_ptr< Pending > &only={})

Private Attributes

uint32_t m_device_id
std::unique_ptr< ISerialLink > m_link
std::atomic< ReaderToken > m_next_token { 1 }
StateCallback m_on_state
Memory::SnapshotCell< std::shared_ptr< Pending > > m_pending
std::unique_ptr< SerialReader > m_reader
Memory::SnapshotCell< ReaderList > m_readers
SerialSpec m_spec

Detailed Description

A serial port with a protocol on it: framing, parsing, commands and replies.

The device knows only an ISerialLink and a SerialSpec. Chunks from the link are cut into messages by the spec's framing and each message becomes an InputValue through its parser, delivered to the value callback with the device id and InputType::SERIAL. Any number of further readers can be added, each with a spec of its own, and every reader sees the same bytes and reads them independently. A message over a spec's limit is dropped and counted, never queued.

A request sends a command and waits for a message its matcher accepts. One request is outstanding at a time, a matched message goes to the request and not to the value callback, and every other message flows on as usual. When the port leaves OPEN the partial message and the running MIDI status are dropped and a pending request ends with LINK_LOST, so the stream resynchronises when the port returns. Destroying the device drops a pending request without calling it.

The device's own callbacks run on the link's thread and must be set before open(). A reader's callback runs there too. send(), request(), add_reader() and remove_reader() may be called from any thread.

SerialDevice device(1, SerialSpec::lines().baud(115200).settle_ms(1500),
std::make_unique<NetworkSerialLink>(service));
device.set_value_callback([](const InputValue& value) { });
device.open("/dev/ttyACM0");
device.request(bytes_of("?\n"),
[](Transitive::Protocol::Message m) { return Transitive::Protocol::as_text(m).starts_with("ok"); },
std::chrono::milliseconds(200),
SerialDevice(uint32_t device_id, SerialSpec spec, std::unique_ptr< ISerialLink > link)
SerialReply
How a request ended.
std::span< const uint8_t > Message
One complete message.
Definition Framing.hpp:8
std::string_view as_text(Message message) noexcept
View a message as text without copying.
Definition Framing.hpp:56
Generic input value container.
static SerialSpec lines()

Definition at line 57 of file SerialDevice.hpp.


The documentation for this class was generated from the following files: