MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ serial_source()

MAYAFLUX_API std::shared_ptr< Vruta::NetworkSource > MayaFlux::Portal::Network::serial_source ( uint32_t device_id,
const Core::SerialSpec & framing )

Read a serial device as network messages, framed the way framing says.

Parameters
device_idId of a serial device from open_serial_port().
framingOnly its framing, terminator, gap and message limit are used. Every message is delivered as bytes whatever its parser says.
Returns
A source fed with one NetworkMessage per framed message, or nothing when serial input is not enabled or the device is unknown.

The source reads the same bytes as the device and as every other reader, cut by its own framing, so the device's own reading and the nodes bound to it are unaffected. The message carries the device id as endpoint_id and no sender address. Hand it to on_message_received() or await it like any other NetworkSource. Dropping the last reference stops the reading.

auto board = open_serial_port(port);
on_message_received(frames, [](const Core::NetworkMessage& message) { });
std::shared_ptr< Vruta::NetworkSource > serial_source(uint32_t device_id, const Core::SerialSpec &framing)
Read a serial device as network messages, framed the way framing says.
std::optional< Core::InputDeviceInfo > open_serial_port(const Core::SerialPortConfig &port)
List a serial port, apply its line setup and protocol, and open it.
Definition Input.cpp:227
void on_message_received(std::shared_ptr< Vruta::NetworkSource > source, std::function< void(const Core::NetworkMessage &)> callback, std::string name)
Schedule a handler for every message received by an existing NetworkSource.
Definition Chronie.cpp:340
A received datagram or framed message with sender metadata.
static SerialSpec cobs()

Definition at line 10 of file SerialSource.cpp.

11{
13 if (!service || !service->add_serial_reader || !service->remove_serial_reader) {
15 "serial_source: InputService not available");
16 return nullptr;
17 }
18
19 Core::SerialSpec spec = framing;
21 spec.parser = nullptr;
22
23 auto source = std::make_shared<Vruta::NetworkSource>();
24 const std::weak_ptr<Vruta::NetworkSource> weak = source;
25
26 const uint64_t token = service->add_serial_reader(device_id, spec,
27 [weak, device_id](const Core::InputValue& value) {
28 const auto held = weak.lock();
29 const auto* bytes = value.try_bytes();
30 if (!held || !bytes) {
31 return;
32 }
34 message.endpoint_id = device_id;
35 message.data = *bytes;
36 held->signal(message);
37 });
38
39 if (token == 0) {
41 "serial_source: serial device {} is not available", device_id);
42 return nullptr;
43 }
44
45 return { source.get(), [source, token, remove = service->remove_serial_reader](Vruta::NetworkSource*) mutable {
46 remove(token);
47 source.reset();
48 } };
49}
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
@ Networking
Network operations (data transfer, protocol handling).
@ Portal
High-level user-facing API layer.
Source source()
Begin a Source chain.
Definition Plot.hpp:127
const std::vector< uint8_t > * try_bytes() const noexcept
Safe bytes extraction.
Generic input value container.
What a serial device is: its line setup, framing and parsing, as a plain value.
Backend input device service interface.

References MayaFlux::Core::BYTES, MayaFlux::Core::NetworkMessage::data, MayaFlux::Core::NetworkMessage::endpoint_id, MayaFlux::Registry::BackendRegistry::get_service(), MayaFlux::Registry::BackendRegistry::instance(), MF_ERROR, MF_WARN, MayaFlux::Journal::Networking, MayaFlux::Core::SerialSpec::parse, MayaFlux::Core::SerialSpec::parser, MayaFlux::Journal::Portal, and MayaFlux::Core::InputValue::try_bytes().

Here is the call graph for this function: