MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ open_device()

bool MayaFlux::Core::SerialInputBackend::open_device ( uint32_t device_id)
overridevirtual

Open a device for input.

Parameters
device_idDevice identifier from get_devices()
Returns
true if device was opened successfully

Implements MayaFlux::Core::IInputBackend.

Definition at line 217 of file SerialInputBackend.cpp.

218{
219 Entry entry;
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);
225 return false;
226 }
227 entry = it->second;
228 } else {
229 return false;
230 }
231
232 if (entry.parent != 0) {
233 if (!open_device(entry.parent)) {
234 return false;
235 }
236 if (entry.view_reader != 0) {
237 return true;
238 }
239 const uint64_t token = add_record(entry.parent, device_id, entry.spec,
240 [this](const InputValue& value) {
241 if (m_running.load()) {
242 notify_input(value);
243 }
244 });
245 m_roster.update([&](Roster& roster) {
246 const auto it = roster.entries.find(device_id);
247 if (it == roster.entries.end()) {
248 return false;
249 }
250 Entry changed = it->second;
251 changed.view_reader = token;
252 roster.entries.insert_or_assign(device_id, std::move(changed));
253 return true;
254 });
255 attach_readers(entry.parent, get_device(entry.parent));
256 return true;
257 }
258
259 if (entry.device) {
260 return true;
261 }
262
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()) {
267 notify_input(value);
268 }
269 });
270 device->set_state_callback([this, device_id](EndpointState previous, EndpointState current) {
271 on_link_state(device_id, previous, current);
272 });
273
274 if (!device->open(entry.info.port_name)) {
276 "Failed to open serial device {}: {}", device_id, entry.info.port_name);
277 return false;
278 }
279
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;
286 return false;
287 }
288 if (it->second.device) {
289 outcome = Outcome::AlreadyOpen;
290 return false;
291 }
292 Entry changed = it->second;
293 changed.device = device;
294 roster.entries.insert_or_assign(device_id, std::move(changed));
295 outcome = Outcome::Registered;
296 return true;
297 });
298
299 if (outcome != Outcome::Registered) {
300 device->close();
301 }
302
303 if (outcome == Outcome::Gone) {
305 "Serial device {} left the list while it was opening", device_id);
306 return false;
307 }
308
309 if (outcome == Outcome::Registered) {
310 attach_readers(device_id, device);
311 }
312
314 "Opened serial device {}: {}", device_id, entry.info.port_name);
315
316 return true;
317}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
uint64_t add_record(uint32_t device_id, uint32_t stamp, SerialSpec spec, ReaderCallback callback)
std::shared_ptr< SerialDevice > get_device(uint32_t device_id) const
The open device, or null.
void on_link_state(uint32_t device_id, EndpointState previous, EndpointState current)
bool open_device(uint32_t device_id) override
Open a device for input.
void attach_readers(uint32_t device_id, const std::shared_ptr< SerialDevice > &device)
Memory::SnapshotCell< Roster > m_roster
void notify_input(const InputValue &value) const
EndpointState
Observable connection state for an endpoint.
@ InputBackend
Input device backend (HID, MIDI, OSC).
@ Core
Core engine, backend, subsystems.

References add_record(), attach_readers(), MayaFlux::Journal::Core, MayaFlux::Core::SerialInputBackend::Roster::entries, get_device(), MayaFlux::Journal::InputBackend, m_roster, MF_ERROR, open_device(), MayaFlux::Core::SerialInputBackend::Entry::parent, MayaFlux::Core::SerialInputBackend::Entry::spec, and MayaFlux::Core::SerialInputBackend::Entry::view_reader.

Referenced by open_device().

Here is the call graph for this function:
Here is the caller graph for this function: