MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SerialDevice.cpp
Go to the documentation of this file.
1#include "SerialDevice.hpp"
2
3namespace MayaFlux::Core {
4
5namespace P = Transitive::Protocol;
6
7SerialDevice::SerialDevice(uint32_t device_id, SerialSpec spec, std::unique_ptr<ISerialLink> link)
9 , m_spec(std::move(spec))
10 , m_link(std::move(link))
11 , m_reader(std::make_unique<SerialReader>(device_id, m_spec, *m_link))
12{
13 m_reader->set_frame_filter([this](P::Message frame) { return complete_request(frame); });
14 m_link->set_chunk_callback([this](std::span<const uint8_t> chunk) { handle_chunk(chunk); });
15 m_link->set_state_callback([this](EndpointState previous, EndpointState current) {
16 handle_state(previous, current);
17 });
18}
19
21{
22 m_link->close();
23 if (const auto dropped = take_pending()) {
24 if (const auto token = dropped->timer.exchange(0); token != 0) {
25 m_link->cancel_post(token);
26 }
27 }
28}
29
31{
32 m_reader->set_value_callback(std::move(callback));
33}
34
36{
37 auto reader = std::make_shared<SerialReader>(id, std::move(spec), *m_link);
38 reader->set_value_callback(std::move(callback));
39 const ReaderToken token = m_next_token.fetch_add(1);
40 m_readers.update([&](ReaderList& list) {
41 list.emplace_back(token, std::move(reader));
42 return true;
43 });
44 return token;
45}
46
48{
49 std::shared_ptr<SerialReader> removed;
50 m_readers.update([&](ReaderList& list) {
51 removed.reset();
52 const auto it = std::ranges::find(list, token, &ReaderList::value_type::first);
53 if (it == list.end()) {
54 return false;
55 }
56 removed = std::move(it->second);
57 list.erase(it);
58 return true;
59 });
60}
61
63{
64 m_on_state = std::move(callback);
65}
66
67bool SerialDevice::open(const std::string& path)
68{
69 m_reader->reset();
70 return m_link->open(path, m_spec.line);
71}
72
78
80{
81 return m_link->state();
82}
83
84bool SerialDevice::send(std::span<const uint8_t> bytes)
85{
86 return m_link->write(bytes);
87}
88
89bool SerialDevice::send_framed(std::span<const uint8_t> message)
90{
91 std::vector<uint8_t> out;
92 out.reserve(message.size() + 4);
93
94 switch (m_spec.framing) {
96 if (!P::encode_line(message, out)) {
97 return false;
98 }
99 break;
101 if (!P::encode_delimited(message, m_spec.delimiter(), out)) {
102 return false;
103 }
104 break;
106 P::encode_cobs(message, out);
107 break;
109 P::encode_slip(message, out);
110 break;
113 out.assign(message.begin(), message.end());
114 break;
115 }
116
117 return m_link->write(out);
118}
119
120bool SerialDevice::request(std::span<const uint8_t> command, ReplyMatcher matcher,
121 std::chrono::milliseconds timeout, ReplyCallback done)
122{
123 if (!matcher || !done || m_link->state() != EndpointState::OPEN) {
124 return false;
125 }
126
127 auto request = std::make_shared<Pending>();
128 request->matcher = std::move(matcher);
129 request->done = std::move(done);
130
131 const bool installed = m_pending.update([&](std::shared_ptr<Pending>& slot) {
132 if (slot) {
133 return false;
134 }
135 slot = request;
136 return true;
137 });
138 if (!installed) {
139 return false;
140 }
141
142 request->timer.store(m_link->post_after(timeout, [this, request] {
143 if (const auto expired = take_pending(request)) {
144 end_request(expired, SerialReply::TIMED_OUT, {});
145 }
146 }));
147
148 if (!is_pending(request)) {
149 if (const auto token = request->timer.exchange(0); token != 0) {
150 m_link->cancel_post(token);
151 }
152 }
153
154 if (!m_link->write(command)) {
155 if (const auto failed = take_pending(request)) {
156 if (const auto token = failed->timer.exchange(0); token != 0) {
157 m_link->cancel_post(token);
158 }
159 return false;
160 }
161 return true;
162 }
163
164 return true;
165}
166
167bool SerialDevice::set_control_lines(std::optional<bool> dtr, std::optional<bool> rts)
168{
169 return m_link->set_control_lines(dtr, rts);
170}
171
173{
174 return m_link->send_break();
175}
176
178{
179 return m_link->backlog();
180}
181
183{
184 return m_reader->stats();
185}
186
187void SerialDevice::handle_chunk(std::span<const uint8_t> chunk)
188{
189 m_reader->push(chunk);
190 if (const auto readers = m_readers.read()) {
191 for (const auto& entry : *readers) {
192 entry.second->push(chunk);
193 }
194 }
195}
196
198{
199 m_reader->reset();
200 if (const auto readers = m_readers.read()) {
201 for (const auto& entry : *readers) {
202 entry.second->reset();
203 }
204 }
205
206 if (current != EndpointState::OPEN) {
208 }
209
210 if (m_on_state) {
211 m_on_state(previous, current);
212 }
213}
214
215bool SerialDevice::complete_request(P::Message frame)
216{
217 std::shared_ptr<Pending> request;
218 if (const auto held = m_pending.read()) {
219 request = *held;
220 }
221 if (!request || !request->matcher(frame)) {
222 return false;
223 }
224
225 if (!take_pending(request)) {
226 return false;
227 }
229 return true;
230}
231
232bool SerialDevice::is_pending(const std::shared_ptr<Pending>& request) const
233{
234 const auto held = m_pending.read();
235 return held && *held == request;
236}
237
238std::shared_ptr<SerialDevice::Pending> SerialDevice::take_pending(const std::shared_ptr<Pending>& only)
239{
240 std::shared_ptr<Pending> taken;
241 m_pending.update([&](std::shared_ptr<Pending>& slot) {
242 taken.reset();
243 if (!slot || (only && slot != only)) {
244 return false;
245 }
246 taken = std::move(slot);
247 slot.reset();
248 return true;
249 });
250 return taken;
251}
252
253void SerialDevice::end_request(const std::shared_ptr<Pending>& request, SerialReply how, P::Message reply)
254{
255 if (!request) {
256 return;
257 }
258 if (const auto token = request->timer.exchange(0); token != 0) {
259 m_link->cancel_post(token);
260 }
261 request->done(how, reply);
262}
263
264} // namespace MayaFlux::Core
Memory::SnapshotCell< ReaderList > m_readers
std::vector< std::pair< ReaderToken, std::shared_ptr< SerialReader > > > ReaderList
Memory::SnapshotCell< std::shared_ptr< Pending > > m_pending
std::unique_ptr< SerialReader > m_reader
bool send_framed(std::span< const uint8_t > message)
Send one message framed the way the spec reads them.
std::function< bool(Transitive::Protocol::Message message)> ReplyMatcher
ISerialLink::StateCallback StateCallback
SerialDeviceStats stats() const
std::function< void(SerialReply how, Transitive::Protocol::Message reply)> ReplyCallback
size_t write_backlog() const
Bytes accepted for sending and not yet sent.
std::shared_ptr< Pending > take_pending(const std::shared_ptr< Pending > &only={})
std::unique_ptr< ISerialLink > m_link
std::atomic< ReaderToken > m_next_token
void handle_chunk(std::span< const uint8_t > chunk)
SerialDevice(uint32_t device_id, SerialSpec spec, std::unique_ptr< ISerialLink > link)
void handle_state(EndpointState previous, EndpointState current)
bool set_control_lines(std::optional< bool > dtr, std::optional< bool > rts)
void set_value_callback(ValueCallback callback)
void end_request(const std::shared_ptr< Pending > &request, SerialReply how, Transitive::Protocol::Message reply)
bool open(const std::string &path)
Open the port at path with the spec's line setup.
std::function< void(const InputValue &)> ValueCallback
bool complete_request(Transitive::Protocol::Message frame)
const SerialSpec & spec() const
bool send(std::span< const uint8_t > bytes)
Send bytes as they are.
void set_state_callback(StateCallback callback)
bool is_pending(const std::shared_ptr< Pending > &request) const
EndpointState state() const
ReaderToken add_reader(SerialSpec spec, uint32_t id, ValueCallback callback)
Read the same bytes a second way.
bool request(std::span< const uint8_t > command, ReplyMatcher matcher, std::chrono::milliseconds timeout, ReplyCallback done)
Send a command and wait for its reply.
void close()
Close the port.
void remove_reader(ReaderToken token)
Stop a reader.
One reading of a byte stream: the spec's framing and parsing, to values.
EndpointState
Observable connection state for an endpoint.
SerialReaderStats SerialDeviceStats
SerialReply
How a request ended.
@ REPLIED
A message the matcher accepted arrived.
@ LINK_LOST
The port left OPEN, or the device was closed, before a reply.
What a serial device is: its line setup, framing and parsing, as a plain value.