viva_u3v/lib.rs
1//! USB3 Vision transport layer.
2//!
3//! Provides device discovery, GenCP-over-USB control, bootstrap register
4//! parsing, and bulk-endpoint streaming for USB3 Vision cameras.
5//!
6//! The `usb` feature flag enables actual USB hardware access via `nusb`, a
7//! pure-Rust USB library (ADR-0032). Without it, only protocol types,
8//! encoding, and mock-based testing compile.
9//!
10//! The modules are private; what is public is re-exported here (ADR-0030).
11//! [`registers`] is the one public namespace: the bootstrap register maps,
12//! the vocabulary the fake camera shares.
13
14mod bootstrap;
15mod control;
16mod descriptor;
17mod device;
18mod discovery;
19mod event;
20#[cfg(feature = "usb")]
21mod native;
22mod status;
23mod stream;
24mod usb;
25
26/// The bootstrap register maps, by offset: the ABRM, the SBRM and the SIRM,
27/// with their capability bits (USB3 Vision 1.0; GenCP 1.2 for the ABRM).
28pub mod registers {
29 pub use crate::bootstrap::{abrm, capability, eirm, sbrm, sirm, u3vcp_capability};
30}
31
32pub use bootstrap::{Abrm, Eirm, FileFormat, ManifestEntry, Sbrm, Sirm, UsbSpeed};
33pub use control::ControlChannel;
34pub use descriptor::U3vInterfaceInfo;
35pub use device::{U3vDevice, U3vStreamOptions};
36pub use discovery::U3vDeviceInfo;
37#[cfg(feature = "usb")]
38#[cfg_attr(docsrs, doc(cfg(feature = "usb")))]
39pub use discovery::discover;
40pub use event::{U3vEvent, U3vEventStream, parse_event_command};
41#[cfg(feature = "usb")]
42#[cfg_attr(docsrs, doc(cfg(feature = "usb")))]
43pub use native::NusbTransfer;
44pub use status::U3vStatus;
45pub use stream::{
46 DEFAULT_MAX_TRANSFER_SIZE, DEFAULT_TRANSFERS_IN_FLIGHT, Leader, PAYLOAD_TYPE_CHUNK,
47 PAYLOAD_TYPE_IMAGE, PAYLOAD_TYPE_IMAGE_EXTENDED_CHUNK, RawFrame, StreamLayout, Trailer,
48 U3vStream, U3vStreamCounters, parse_chunks, parse_leader, parse_trailer,
49};
50pub use usb::{BulkIn, Completion, MockUsbTransfer, UsbTransfer};
51
52/// The error a source is carried as when its type is not part of this crate's
53/// API: the USB library's, the ZIP reader's.
54type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
55
56/// Errors produced by the USB3 Vision transport layer.
57///
58/// A USB library's failure is the [`source`](std::error::Error::source) of
59/// [`U3vError::Usb`], never flattened into text (ADR-0030).
60#[derive(Debug, thiserror::Error)]
61#[non_exhaustive]
62pub enum U3vError {
63 /// A USB operation failed. The source is the USB library's error.
64 #[error("usb: {0}")]
65 Usb(#[source] BoxError),
66
67 /// No device is at the bus and address enumeration reported.
68 #[error("no USB device at bus {bus}, address {address}")]
69 NotFound {
70 /// The USB bus, as the operating system names it.
71 bus: String,
72 /// The device's address on it.
73 address: u8,
74 },
75
76 /// A bulk transfer or control transaction timed out.
77 #[error("timeout")]
78 Timeout,
79
80 /// The device is gone: unplugged, or reset by the host. Every later
81 /// transfer fails the same way; the camera must be opened again once it
82 /// is back (backlog UV-03).
83 #[error("the device is disconnected")]
84 Disconnected,
85
86 /// An endpoint stalled: the device refused a transfer and halted it. A
87 /// stream recovers by resetting the endpoint (backlog UV-02).
88 #[error("endpoint {endpoint:#04x} stalled")]
89 Stalled {
90 /// The endpoint's address.
91 endpoint: u8,
92 },
93
94 /// The transfer was cancelled before it completed, by the host.
95 #[error("the transfer was cancelled")]
96 Cancelled,
97
98 /// An interface could not be claimed. The source is the USB library's
99 /// error: another application, or a kernel driver, may hold it, or — on
100 /// Windows — the interface has no WinUSB driver, which the hint says.
101 #[error(
102 "claim the {interface} interface ({number}): {source}{}",
103 .hint.map_or(String::new(), |hint| format!("; {hint}"))
104 )]
105 Claim {
106 /// Which USB3 Vision interface: `"control"`, `"stream"` or `"event"`.
107 interface: &'static str,
108 /// Its interface number.
109 number: u8,
110 /// The USB library's error.
111 #[source]
112 source: BoxError,
113 /// What the user can do about it, when the cause is known.
114 hint: Option<&'static str>,
115 },
116
117 /// GenCP-level decode error.
118 #[error("gencp: {0}")]
119 GenCp(#[from] viva_gencp::GenCpError),
120
121 /// The device answered with an error status, decoded with USB3 Vision's
122 /// table (ADR-0020).
123 #[error("device status: {0}")]
124 Status(U3vStatus),
125
126 /// An acknowledge, leader or trailer does not have the shape GenCP and
127 /// USB3 Vision give it.
128 #[error("malformed {packet}: {problem}")]
129 Malformed {
130 /// What arrived, such as `"acknowledge"` or `"trailer"`.
131 packet: &'static str,
132 /// What is wrong, such as `"lacks the U3VC prefix"`.
133 problem: &'static str,
134 },
135
136 /// A transfer carried another number of bytes than asked for.
137 #[error("{what} is {got} bytes; expected {expected}")]
138 Length {
139 /// What was counted, such as `"READMEM_ACK payload"`.
140 what: &'static str,
141 /// The bytes asked for.
142 expected: usize,
143 /// The bytes that arrived.
144 got: usize,
145 },
146
147 /// The device's bootstrap registers describe something this host cannot
148 /// use.
149 #[error("the device {0}")]
150 Device(&'static str),
151
152 /// The device's XML could not be unpacked. The source is the ZIP reader's
153 /// or the UTF-8 decoder's error.
154 #[error("device XML: {0}")]
155 Xml(#[source] BoxError),
156}
157
158impl U3vError {
159 /// A USB library's failure; `err` becomes the source.
160 pub fn usb(err: impl Into<BoxError>) -> Self {
161 U3vError::Usb(err.into())
162 }
163
164 /// Whether a device that is gone can fail a transfer this way without
165 /// saying so: a USB library's unnamed failure, or a transfer the host
166 /// ended. On macOS an unplug is `kIOReturnNotResponding`,
167 /// `kIOReturnNotReady` or `kIOReturnNotAttached`, which `nusb` passes on
168 /// as unknown (backlog UV-05). A timeout, a stall and anything the
169 /// device answered say it is there.
170 pub(crate) fn may_mean_gone(&self) -> bool {
171 matches!(self, U3vError::Usb(_) | U3vError::Cancelled)
172 }
173}