5namespace P = Transitive::Protocol;
14 m_link->set_chunk_callback([
this](std::span<const uint8_t> chunk) {
handle_chunk(chunk); });
24 if (
const auto token = dropped->timer.exchange(0); token != 0) {
25 m_link->cancel_post(token);
32 m_reader->set_value_callback(std::move(callback));
37 auto reader = std::make_shared<SerialReader>(
id, std::move(
spec), *
m_link);
38 reader->set_value_callback(std::move(callback));
41 list.emplace_back(token, std::move(reader));
49 std::shared_ptr<SerialReader> removed;
52 const auto it = std::ranges::find(list, token, &ReaderList::value_type::first);
53 if (it == list.end()) {
56 removed = std::move(it->second);
86 return m_link->write(bytes);
91 std::vector<uint8_t> out;
92 out.reserve(message.size() + 4);
96 if (!P::encode_line(message, out)) {
101 if (!P::encode_delimited(message,
m_spec.delimiter(), out)) {
106 P::encode_cobs(message, out);
109 P::encode_slip(message, out);
113 out.assign(message.begin(), message.end());
117 return m_link->write(out);
127 auto request = std::make_shared<Pending>();
128 request->matcher = std::move(matcher);
129 request->done = std::move(done);
131 const bool installed =
m_pending.update([&](std::shared_ptr<Pending>& slot) {
143 if (const auto expired = take_pending(request)) {
144 end_request(expired, SerialReply::TIMED_OUT, {});
148 if (!is_pending(request)) {
149 if (
const auto token = request->timer.exchange(0); token != 0) {
150 m_link->cancel_post(token);
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);
169 return m_link->set_control_lines(dtr, rts);
174 return m_link->send_break();
190 if (
const auto readers =
m_readers.read()) {
191 for (
const auto& entry : *readers) {
192 entry.second->push(chunk);
200 if (
const auto readers =
m_readers.read()) {
201 for (
const auto& entry : *readers) {
202 entry.second->reset();
217 std::shared_ptr<Pending>
request;
218 if (
const auto held =
m_pending.read()) {
235 return held && *held ==
request;
240 std::shared_ptr<Pending> taken;
241 m_pending.update([&](std::shared_ptr<Pending>& slot) {
243 if (!slot || (only && slot != only)) {
246 taken = std::move(slot);
258 if (
const auto token =
request->timer.exchange(0); token != 0) {
259 m_link->cancel_post(token);
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
uint32_t device_id() const
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.