MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SerialLink.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Core {
6
7/**
8 * @class ISerialLink
9 * @brief What a serial device needs from the transport under it.
10 *
11 * One link is one port. Both callbacks run on the link's own thread and never
12 * concurrently with each other or with a timer, and both must be set before open().
13 * write() returns whether the bytes were accepted for sending, not delivered.
14 */
15class MAYAFLUX_API ISerialLink {
16public:
17 using ChunkCallback = std::function<void(std::span<const uint8_t> bytes)>;
18 using StateCallback = std::function<void(EndpointState previous, EndpointState current)>;
19 using TimerToken = uint64_t;
20
21 virtual ~ISerialLink() = default;
22
23 /**
24 * @brief Open the port at @p path with @p line.
25 * @return false when it cannot be opened. A link that reconnects returns true for a port
26 * that is not there yet and reports the wait as RECONNECTING.
27 */
28 virtual bool open(const std::string& path, const SerialLineSettings& line) = 0;
29
30 /** @brief Close the port. No-op when it is not open. */
31 virtual void close() = 0;
32
33 [[nodiscard]] virtual EndpointState state() const = 0;
34
35 virtual bool write(std::span<const uint8_t> bytes) = 0;
36
37 /** @brief Bytes accepted by write() and not yet sent. */
38 [[nodiscard]] virtual size_t backlog() const = 0;
39
40 /** @brief Set DTR and RTS, each optional so one can change alone. */
41 virtual bool set_control_lines(std::optional<bool> dtr, std::optional<bool> rts) = 0;
42
43 virtual bool send_break() = 0;
44
45 /** @brief Run @p callback once after @p delay on the link's thread. */
46 virtual TimerToken post_after(std::chrono::milliseconds delay, std::function<void()> callback) = 0;
47
48 /** @brief Cancel a timer. No-op when it already ran. */
49 virtual void cancel_post(TimerToken token) = 0;
50
51 virtual void set_chunk_callback(ChunkCallback callback) = 0;
52 virtual void set_state_callback(StateCallback callback) = 0;
53};
54
55} // namespace MayaFlux::Core
EndpointState
Observable connection state for an endpoint.
Line setup applied when a serial endpoint opens.