MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SerialReader.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "SerialLink.hpp"
4#include "SerialSpec.hpp"
5
6namespace MayaFlux::Core {
7
8/**
9 * @struct SerialReaderStats
10 * @brief What a reader has seen since it was made.
11 *
12 * dropped counts messages the framer refused for size or encoding, and unparsed counts
13 * messages the parser did not turn into a value.
14 */
16 uint64_t frames {};
17 uint64_t dropped {};
18 uint64_t unparsed {};
19};
20
21/**
22 * @class SerialReader
23 * @brief One reading of a byte stream: the spec's framing and parsing, to values.
24 *
25 * Bytes pushed in are cut into messages by the spec's framing and each message becomes an
26 * InputValue through its parser, stamped with the id and InputType::SERIAL and handed to the
27 * value callback. A frame filter sees each message first and a message it accepts is not
28 * parsed. Several readers over the same bytes read them independently.
29 *
30 * The link is used only for the timer of an idle gap framing and must outlive the reader.
31 * push() and reset() belong to one thread, the link's.
32 */
33class MAYAFLUX_API SerialReader {
34public:
35 using ValueCallback = std::function<void(const InputValue&)>;
36 using FrameFilter = std::function<bool(Transitive::Protocol::Message message)>;
37
38 /**
39 * @param id Id stamped on every value.
40 * @param spec The framing and parsing. Its line setup is not used.
41 * @param link Supplies the timer of an idle gap framing.
42 */
43 SerialReader(uint32_t id, SerialSpec spec, ISerialLink& link);
45
46 SerialReader(const SerialReader&) = delete;
50
51 void set_value_callback(ValueCallback callback);
52 void set_frame_filter(FrameFilter filter);
53
54 void push(std::span<const uint8_t> chunk);
55
56 /** @brief Drop the partial message, the running MIDI status and a pending gap. */
57 void reset();
58
59 [[nodiscard]] SerialReaderStats stats() const;
60 [[nodiscard]] uint32_t id() const { return m_id; }
61 [[nodiscard]] const SerialSpec& spec() const { return m_spec; }
62
63private:
64 using Framer = std::variant<
71
72 struct MidiStream {
73 uint8_t status {};
74 uint8_t data[2] {};
75 uint8_t need {};
76 uint8_t have {};
77 bool in_sysex {};
78 };
79
80 static Framer make_framer(const SerialSpec& spec);
81
82 void handle_frame(Transitive::Protocol::Message frame);
83 void handle_gap();
84 void parse_midi(std::span<const uint8_t> chunk);
85 void deliver(std::optional<InputValue> value);
86 void arm_gap();
87 void cancel_gap();
88
89 uint32_t m_id;
96 std::atomic<ISerialLink::TimerToken> m_gap_timer { 0 };
97
98 std::atomic<uint64_t> m_frames { 0 };
99 std::atomic<uint64_t> m_dropped { 0 };
100 std::atomic<uint64_t> m_unparsed { 0 };
101};
102
103} // namespace MayaFlux::Core
std::atomic< uint64_t > m_frames
SerialReader(const SerialReader &)=delete
SerialReader & operator=(SerialReader &&)=delete
SerialReaderStats stats() const
std::variant< Transitive::Protocol::RawChunkFramer, Transitive::Protocol::LineFramer, Transitive::Protocol::DelimiterFramer, Transitive::Protocol::CobsFramer, Transitive::Protocol::SlipFramer, Transitive::Protocol::IdleGapFramer > Framer
std::atomic< uint64_t > m_dropped
SerialReader & operator=(const SerialReader &)=delete
std::function< bool(Transitive::Protocol::Message message)> FrameFilter
std::atomic< ISerialLink::TimerToken > m_gap_timer
std::function< void(const InputValue &)> ValueCallback
void reset()
Drop the partial message, the running MIDI status and a pending gap.
std::atomic< uint64_t > m_unparsed
SerialReader(SerialReader &&)=delete
const SerialSpec & spec() const
SerialReader(uint32_t id, SerialSpec spec, ISerialLink &link)
void push(std::span< const uint8_t > chunk)
void set_frame_filter(FrameFilter filter)
void set_value_callback(ValueCallback callback)
Messages in Consistent Overhead Byte Stuffing, each ended by a zero byte.
Definition Framing.hpp:530
Splits a byte stream at a terminator of one or more bytes, whatever the chunking.
Definition Framing.hpp:381
Messages ended by a quiet period on the line, as in Modbus RTU.
Definition Framing.hpp:782
Splits a byte stream into lines, whatever the chunking.
Definition Framing.hpp:112
Messages in SLIP (RFC 1055), each ended by 0xC0.
Definition Framing.hpp:654
std::span< const uint8_t > Message
One complete message.
Definition Framing.hpp:8
Generic input value container.
What a reader has seen since it was made.
What a serial device is: its line setup, framing and parsing, as a plain value.
The framer for raw streams: every push is one message, with nothing to reassemble.
Definition Framing.hpp:355