1use std::net::{Ipv4Addr, SocketAddrV4};
4use std::time::Duration;
5
6use thiserror::Error;
7
8use crate::ControlState;
9use crate::genapi::GenApiError;
10use crate::genapi::persistence::ParseConfigurationError;
11use crate::gige::GigeError;
12use viva_genapi_xml::XmlError;
13use viva_pfnc::convert::ConvertError;
14
15#[derive(Debug, Error)]
23#[non_exhaustive]
24pub enum GenicamError {
25 #[error(transparent)]
27 GenApi(#[from] GenApiError),
28 #[error("GenApi XML: {0}")]
30 Xml(#[from] XmlError),
31 #[error("{during}: {source}")]
33 Gige {
34 during: &'static str,
36 #[source]
38 source: GigeError,
39 },
40 #[cfg(feature = "u3v")]
42 #[cfg_attr(docsrs, doc(cfg(feature = "u3v")))]
43 #[error("{during}: {source}")]
44 U3v {
45 during: &'static str,
47 #[source]
49 source: crate::u3v::U3vError,
50 },
51 #[error("{during}: {source}")]
54 Network {
55 during: &'static str,
57 #[source]
59 source: std::io::Error,
60 },
61 #[error("i/o: {0}")]
63 Io(#[from] std::io::Error),
64 #[error("configuration file: {0}")]
67 Configuration(#[from] ParseConfigurationError),
68 #[error("cannot convert the frame: {0}")]
70 Convert(#[source] ConvertError),
71 #[error("chunk feature '{0}' not found; verify camera supports chunk data")]
73 MissingChunkFeature(String),
74 #[error("unsupported pixel format: {0}")]
76 UnsupportedPixelFormat(viva_pfnc::PixelFormat),
77 #[error("command {name} was executed but did not report completion within {waited:?}")]
83 CommandNotDone {
84 name: String,
86 waited: Duration,
89 },
90 #[error("timed out after {0:?}")]
92 Timeout(Duration),
93 #[error("the camera has no {what}: it declares none of {}", tried.join(", "))]
95 MissingFeature {
96 what: &'static str,
98 tried: &'static [&'static str],
100 },
101 #[error(
105 "cannot enable events {events:?}: the camera declares neither EventSelector nor \
106 EventNotification, and event selection has no bootstrap register"
107 )]
108 NoEventSelection {
109 events: Vec<String>,
111 },
112 #[error("{}", not_in_control(*.0))]
116 NotInControl(ControlState),
117 #[error(
119 "GVSP packet size {size} is below the {min}-byte minimum; raise the camera's \
120 GevSCPSPacketSize, or pass auto_packet_size() or packet_size(n)"
121 )]
122 PacketTooSmall {
123 size: u32,
125 min: u32,
127 },
128 #[error("GVSP packet size {size} exceeds the {max}-byte maximum an IPv4 datagram carries")]
130 PacketTooLarge {
131 size: u32,
133 max: u32,
135 },
136 #[error("{}", stream_channel(*channel, *count))]
139 StreamChannel {
140 channel: u32,
142 count: Option<u32>,
144 },
145 #[error("cannot set a packet delay of {delay:?}: {problem}")]
147 PacketDelay {
148 delay: Duration,
150 problem: &'static str,
152 },
153 #[error("a stream needs a network interface to receive on; none was given")]
155 NoInterface,
156 #[error("interface {0} has no IPv4 address")]
158 InterfaceWithoutIpv4(String),
159 #[error("no local interface routes to {camera}: {source}")]
161 NoRoute {
162 camera: Ipv4Addr,
164 #[source]
166 source: std::io::Error,
167 },
168 #[error("{0} is not a multicast group (224.0.0.0/4)")]
171 NotMulticast(SocketAddrV4),
172 #[error("time calibration: {0}")]
174 TimeSync(&'static str),
175 #[error("{feature}: entry {entry} names no binning factor")]
177 NoBinningFactor {
178 feature: &'static str,
180 entry: String,
182 },
183 #[error("{feature}: no entry names a binning factor of {factor}; its entries are {}", entries.join(", "))]
185 NoBinningEntry {
186 feature: &'static str,
188 factor: i64,
190 entries: Vec<String>,
192 },
193 #[error(
196 "cannot size a {pixel_format} frame: the camera has no PayloadSize and the format \
197 declares no size; give the payload size explicitly"
198 )]
199 UnsizedFrame {
200 pixel_format: String,
202 },
203 #[error("{0} panicked")]
206 Panicked(&'static str),
207 #[error("{during}: {source}")]
209 Task {
210 during: &'static str,
212 #[source]
214 source: tokio::task::JoinError,
215 },
216 #[error(
219 "blocking::{0} was called inside an async runtime, which it would stall; use the async API there"
220 )]
221 InsideRuntime(&'static str),
222}
223
224fn not_in_control(state: ControlState) -> &'static str {
226 match state {
227 ControlState::Monitor => {
228 "the camera was opened as a read-only monitor, so nothing is written; open it \
229 with Access::Control, or reclaim_control, to write"
230 }
231 ControlState::Released => {
232 "control of the camera was released, so nothing is written; reclaim_control \
233 takes it again"
234 }
235 _ => {
236 "control of the camera was lost, so nothing is written; reclaim_control takes it again"
237 }
238 }
239}
240
241fn stream_channel(channel: u32, count: Option<u32>) -> String {
242 match count {
243 Some(count) => format!(
244 "the camera has {count} stream channel(s) (GevNumberOfStreamChannels); stream \
245 channel {channel} does not exist"
246 ),
247 None => format!(
248 "the camera does not say how many stream channels it has; only stream channel 0 \
249 can be configured, not {channel}"
250 ),
251 }
252}
253
254impl From<GigeError> for GenicamError {
255 fn from(source: GigeError) -> Self {
256 GenicamError::Gige {
257 during: "GigE Vision control",
258 source,
259 }
260 }
261}
262
263#[cfg(feature = "u3v")]
264impl From<crate::u3v::U3vError> for GenicamError {
265 fn from(source: crate::u3v::U3vError) -> Self {
266 GenicamError::U3v {
267 during: "USB3 Vision control",
268 source,
269 }
270 }
271}
272
273impl From<ConvertError> for GenicamError {
274 fn from(err: ConvertError) -> Self {
275 match err {
276 ConvertError::Unsupported(format) => GenicamError::UnsupportedPixelFormat(format),
277 other => GenicamError::Convert(other),
278 }
279 }
280}
281
282pub(crate) fn gige(during: &'static str) -> impl FnOnce(GigeError) -> GenicamError {
284 move |source| GenicamError::Gige { during, source }
285}
286
287#[cfg(feature = "u3v")]
289pub(crate) fn u3v(during: &'static str) -> impl FnOnce(crate::u3v::U3vError) -> GenicamError {
290 move |source| GenicamError::U3v { during, source }
291}
292
293pub(crate) fn network(during: &'static str) -> impl FnOnce(std::io::Error) -> GenicamError {
295 move |source| GenicamError::Network { during, source }
296}
297
298pub(crate) fn task(during: &'static str) -> impl FnOnce(tokio::task::JoinError) -> GenicamError {
300 move |source| GenicamError::Task { during, source }
301}
302
303#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
309#[error("{} (register {address:#010x})", not_in_control(*state))]
310#[non_exhaustive]
311pub struct WriteRefused {
312 pub state: ControlState,
314 pub address: u64,
316}
317
318impl WriteRefused {
319 pub(crate) fn new(state: ControlState, address: u64) -> Self {
320 Self { state, address }
321 }
322}