MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NetworkSerialLink.cpp
Go to the documentation of this file.
2
5
6namespace MayaFlux::Core {
7
11 std::atomic<EndpointState> reported { EndpointState::CLOSED };
12
13 void report(EndpointState current)
14 {
15 const EndpointState previous = reported.exchange(current);
16 if (previous != current && state) {
17 state(previous, current);
18 }
19 }
20};
21
26
31
32bool NetworkSerialLink::open(const std::string& path, const SerialLineSettings& line)
33{
34 if (!m_service || !m_service->open_endpoint || !m_service->set_endpoint_receive_callback) {
36 "Serial link {} needs a NetworkService", path);
37 return false;
38 }
39
40 close();
41
42 auto shared = std::make_shared<Shared>();
43 shared->chunk = m_chunk;
44 shared->state = m_state;
45
46 EndpointInfo info;
50 info.remote_path = path;
51 info.label = path;
52 info.serial = line;
53
54 const uint64_t endpoint = m_service->open_endpoint(info);
55 if (endpoint == 0) {
57 "Serial link could not open {}: the port must exist, or the serial transport must be enabled in GlobalNetworkConfig and set to reconnect",
58 path);
59 return false;
60 }
61
62 m_endpoint = endpoint;
63 m_shared = shared;
64 const std::weak_ptr<Shared> weak = shared;
65
66 m_service->set_endpoint_receive_callback(endpoint,
67 [weak](uint64_t, const uint8_t* data, size_t size, std::string_view) {
68 if (const auto held = weak.lock(); held && held->chunk) {
69 held->chunk(std::span<const uint8_t> { data, size });
70 }
71 });
72
73 if (m_service->set_endpoint_state_callback) {
74 m_service->set_endpoint_state_callback(endpoint,
75 [weak](uint64_t, EndpointState, EndpointState current) {
76 if (const auto held = weak.lock()) {
77 held->report(current);
78 }
79 });
80 }
81
82 if (m_service->get_endpoint_state) {
83 shared->report(m_service->get_endpoint_state(endpoint));
84 }
85
86 return true;
87}
88
90{
91 if (m_endpoint != 0 && m_service && m_service->close_endpoint) {
92 m_service->close_endpoint(m_endpoint);
93 }
94 m_endpoint = 0;
95 m_shared.reset();
96}
97
99{
100 if (m_endpoint == 0 || !m_service || !m_service->get_endpoint_state) {
102 }
103 return m_service->get_endpoint_state(m_endpoint);
104}
105
106bool NetworkSerialLink::write(std::span<const uint8_t> bytes)
107{
108 if (m_endpoint == 0 || !m_service || !m_service->send) {
109 return false;
110 }
111 return m_service->send(m_endpoint, bytes.data(), bytes.size());
112}
113
115{
116 if (m_endpoint == 0 || !m_service || !m_service->get_write_backlog) {
117 return 0;
118 }
119 return m_service->get_write_backlog(m_endpoint);
120}
121
122bool NetworkSerialLink::set_control_lines(std::optional<bool> dtr, std::optional<bool> rts)
123{
124 if (m_endpoint == 0 || !m_service || !m_service->set_control_lines) {
125 return false;
126 }
127 return m_service->set_control_lines(m_endpoint, dtr, rts);
128}
129
131{
132 if (m_endpoint == 0 || !m_service || !m_service->send_break) {
133 return false;
134 }
135 return m_service->send_break(m_endpoint);
136}
137
138ISerialLink::TimerToken NetworkSerialLink::post_after(std::chrono::milliseconds delay, std::function<void()> callback)
139{
140 if (!m_service || !m_service->post_after) {
141 return 0;
142 }
143 return m_service->post_after(delay, std::move(callback));
144}
145
147{
148 if (token != 0 && m_service && m_service->cancel_post) {
149 m_service->cancel_post(token);
150 }
151}
152
154{
155 m_chunk = std::move(callback);
156}
157
159{
160 m_state = std::move(callback);
161}
162
163} // namespace MayaFlux::Core
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
EndpointState
Observable connection state for an endpoint.
@ InputBackend
Input device backend (HID, MIDI, OSC).
@ Core
Core engine, backend, subsystems.
SerialLineSettings serial
Serial: the line setup applied at open. The device path is remote_path.
FramingPolicy framing
TCP and UDS: how messages are delimited.
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.
Backend network transport service interface.