12 bool same_port(
const std::string& first,
const std::string& second)
14 if (first == second) {
18 return std::filesystem::equivalent(first, second, ec) && !ec;
37 const auto [it, inserted] = roster.
ids.try_emplace(key, roster.
next_id);
55 .baud_rate = line.baud_rate,
70 "SerialInputBackend needs a NetworkService");
76 for (
const auto& port :
m_config.ports) {
77 const uint32_t
id =
id_for(roster,
"c:" + port.port_name);
78 roster.
entries.insert_or_assign(
id,
79 make_entry(
id, port.port_name, port.port_name, port.line, port.protocol,
true));
91 "SerialInputBackend initialized with {} port(s)",
get_devices().size());
114 std::vector<std::shared_ptr<SerialDevice>> open;
115 if (
const auto roster =
m_roster.read()) {
116 for (
const auto& [
id, entry] : roster->entries) {
118 open.push_back(entry.device);
128 for (
const auto& device : open) {
137 std::vector<InputDeviceInfo> devices;
138 if (
const auto roster =
m_roster.read()) {
139 devices.reserve(roster->entries.size());
140 for (
const auto& [
id, entry] : roster->entries) {
141 devices.push_back(entry.info);
154 std::vector<DeviceEvent> events;
159 bool changed =
false;
160 std::set<uint32_t> seen;
162 for (
const auto& port : found) {
163 const std::string path = port.path.string();
164 const bool configured_port = std::ranges::any_of(roster.
entries, [&](
const auto& item) {
165 return item.second.configured && item.second.parent == 0 && same_port(item.second.info.port_name, path);
167 if (configured_port) {
171 const uint32_t
id =
id_for(roster,
"e:" + path);
176 events.emplace_back(entry.
info,
true);
177 roster.
entries.insert_or_assign(
id, std::move(entry));
182 std::vector<uint32_t> gone;
183 for (
const auto& [
id, entry] : roster.
entries) {
184 if (!entry.configured && entry.parent == 0 && !entry.device && !seen.contains(
id)) {
188 for (
const uint32_t
id : gone) {
189 events.emplace_back(roster.
entries.find(
id)->second.info,
false);
194 std::vector<uint32_t> orphans;
195 for (
const auto& [
id, entry] : roster.
entries) {
196 if (entry.parent != 0 && roster.
entries.find(entry.parent) == roster.
entries.end()) {
197 orphans.push_back(
id);
200 for (
const uint32_t
id : orphans) {
201 events.emplace_back(roster.
entries.find(
id)->second.info,
false);
210 for (
const auto& [info, connected] : events) {
220 if (
const auto roster =
m_roster.read()) {
221 const auto it = roster->entries.find(device_id);
222 if (it == roster->entries.end()) {
224 "Serial device {} not found", device_id);
241 if (m_running.load()) {
246 const auto it = roster.
entries.find(device_id);
247 if (it == roster.
entries.end()) {
250 Entry changed = it->second;
252 roster.
entries.insert_or_assign(device_id, std::move(changed));
263 auto device = std::make_shared<SerialDevice>(device_id, entry.spec,
264 std::make_unique<NetworkSerialLink>(m_config.network));
265 device->set_value_callback([
this](
const InputValue& value) {
266 if (m_running.load()) {
271 on_link_state(device_id, previous, current);
274 if (!device->open(entry.info.port_name)) {
276 "Failed to open serial device {}: {}", device_id, entry.info.port_name);
280 enum class Outcome : uint8_t { Registered, AlreadyOpen, Gone };
281 Outcome outcome = Outcome::Gone;
282 m_roster.update([&](Roster& roster) {
283 const auto it = roster.entries.find(device_id);
284 if (it == roster.entries.end()) {
285 outcome = Outcome::Gone;
288 if (it->second.device) {
289 outcome = Outcome::AlreadyOpen;
292 Entry changed = it->second;
293 changed.device = device;
294 roster.entries.insert_or_assign(device_id, std::move(changed));
295 outcome = Outcome::Registered;
299 if (outcome != Outcome::Registered) {
303 if (outcome == Outcome::Gone) {
305 "Serial device {} left the list while it was opening", device_id);
309 if (outcome == Outcome::Registered) {
310 attach_readers(device_id, device);
314 "Opened serial device {}: {}", device_id, entry.info.port_name);
324 const auto it = roster.
entries.find(view_id);
325 if (it == roster.
entries.end() || it->second.view_reader == 0) {
328 Entry changed = it->second;
330 roster.
entries.insert_or_assign(view_id, std::move(changed));
340 bool is_view =
false;
341 if (
const auto roster =
m_roster.read()) {
342 const auto it = roster->entries.find(device_id);
343 is_view = it != roster->entries.end() && it->second.parent != 0;
350 std::shared_ptr<SerialDevice> device;
353 const auto it = roster.
entries.find(device_id);
354 if (it == roster.
entries.end() || !it->second.device) {
357 Entry changed = it->second;
358 device = std::move(changed.
device);
360 roster.
entries.insert_or_assign(device_id, std::move(changed));
371 if (
const auto roster =
m_roster.read()) {
372 const auto it = roster->entries.find(device_id);
373 if (it != roster->entries.end() && it->second.parent != 0) {
374 return it->second.view_reader != 0 &&
get_device(it->second.parent) !=
nullptr;
382 std::vector<uint32_t> open;
383 if (
const auto roster =
m_roster.read()) {
384 for (
const auto& [
id, entry] : roster->entries) {
396 if (!device || !device->send(data)) {
399 return static_cast<int>(data.size());
421 const auto it = roster.
entries.find(
id);
422 if (it == roster.
entries.end()) {
423 roster.
entries.insert_or_assign(
id,
427 if (it->second.device) {
432 roster.
entries.insert_or_assign(
id, std::move(changed));
441 const auto it = roster.
entries.find(device_id);
442 if (it == roster.
entries.end() || it->second.device) {
445 Entry changed = it->second;
447 roster.
entries.insert_or_assign(device_id, std::move(changed));
454 if (
const auto roster =
m_roster.read()) {
455 const auto it = roster->entries.find(device_id);
456 if (it != roster->entries.end()) {
457 if (it->second.parent != 0) {
458 const auto parent = roster->entries.find(it->second.parent);
459 return parent != roster->entries.end() ? parent->second.device :
nullptr;
461 return it->second.device;
477 const auto parent = roster.
entries.find(device_id);
478 if (parent == roster.
entries.end() || parent->second.parent != 0) {
482 Entry entry = parent->second;
489 entry.
info.
name = parent->second.info.name +
" (reading " + std::to_string(
id) +
")";
491 roster.
entries.insert_or_assign(
id, std::move(entry));
505 m_records.push_back({ .token = token, .device_id = device_id, .stamp = stamp,
506 .spec = std::move(spec), .callback = std::move(callback), .attached_to = {}, .device_token = 0 });
517 if (
record.device_id != device_id ||
record.attached_to.lock() == device) {
521 record.attached_to = device;
532 if (
const auto roster =
m_roster.read()) {
533 const auto it = roster->entries.find(device_id);
534 known = it != roster->entries.end() && it->second.parent == 0;
540 const uint64_t token =
add_record(device_id, device_id, std::move(spec), std::move(callback));
552 if (
const auto device = it->attached_to.lock()) {
553 device->remove_reader(it->device_token);
562 if (was_open == is_open) {
570 const auto it = roster.
entries.find(device_id);
571 if (it == roster.
entries.end()) {
574 Entry changed = it->second;
578 roster.
entries.insert_or_assign(device_id, std::move(changed));
597 (*callback)(info, connected);
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
@ SERIAL
Serial port communication (Arduino, etc.).
EndpointState
Observable connection state for an endpoint.
std::function< void(const InputDeviceInfo &, bool connected)> DeviceCallback
Callback signature for device connection/disconnection events.
std::function< void(const InputValue &)> InputCallback
Callback signature for input events.
@ InputBackend
Input device backend (HID, MIDI, OSC).
@ Core
Core engine, backend, subsystems.
std::pair< std::shared_ptr< Kriya::BufferPipeline >, std::shared_ptr< Buffers::AudioBuffer > > record(const std::shared_ptr< Kakshya::DynamicSoundStream > &stream, uint32_t input_channel, double seconds, std::optional< uint32_t > monitor, uint32_t channel)
Record a live input channel into a stream for a number of seconds.
std::shared_ptr< SerialDevice > device
Line setup applied when a serial endpoint opens.
SerialLineSettings line
Baud, framing bits, flow control, control lines at open.
SerialProtocolSettings protocol
Framing and parsing of what the port sends.
std::string port_name
e.g., "/dev/ttyUSB0" or "COM3"
One serial port, the line setup it opens with and how it is read.
How a port is read, as plain data a config can hold.
static SerialSpec from(const SerialLineSettings &line, const SerialProtocolSettings &protocol)
The spec a configured port describes.
What a serial device is: its line setup, framing and parsing, as a plain value.