1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//! GET WINDOW, SET WINDOW and SCAN. Sections 2-9, 2-10 and 2-7
use super::{MOVE_TIMEOUT, PROBE_TIMEOUT, Session, malformed};
use crate::{
error::Error,
protocol::{
caps::set_window::ScanKind,
cdbs::{GetWindow, Scan, SetWindow},
data::{CooperativeAction, Rect},
image::Layout,
window::{self, GetWindowHeader, SetWindowHeader, Window},
},
transport::Data,
};
use tracing::*;
impl Session {
/// One GET WINDOW, exactly as asked: header plus descriptors, unparsed
fn get_window_raw(&mut self, cmd: GetWindow) -> Result<Vec<u8>, Error> {
let mut buf = vec![0u8; cmd.allocation_length()];
let completion = self.run(&cmd.cdb(), Data::In(&mut buf), PROBE_TIMEOUT)?;
buf.truncate(completion.transferred);
Ok(buf)
}
/// Read back every window descriptor the unit currently holds
///
/// Two passes: a transfer longer than what is there gets refused, so the
/// header has to say how much there is first
pub fn windows(&mut self) -> Result<Vec<Window>, Error> {
let probe = self.get_window_raw(GetWindow::all(window::HEADER as u32))?;
let (probe, _) =
GetWindowHeader::from_bytes(&probe).map_err(|e| malformed(e.to_string()))?;
let data = self.get_window_raw(GetWindow::all(2 + u32::from(probe.data_length)))?;
let (header, descriptors) =
GetWindowHeader::from_bytes(&data).map_err(|e| malformed(e.to_string()))?;
let stride = usize::from(header.descriptor_length);
debug!(stride, bytes = descriptors.len(), "window descriptors");
if stride < window::LENGTH {
return Err(malformed(format!(
"descriptor stride of {stride} is shorter than the {} bytes 2-10-3 defines",
window::LENGTH
)));
}
descriptors
.chunks_exact(stride)
.map(|d| Window::try_from(d).map_err(|e| malformed(e.to_string())))
.collect()
}
/// Whether the stage has to home before it can reach this window
///
/// Only an image window sits in a frame; a thumbnail spans everything. With
/// no table read or written there is nothing to judge against
fn homes_to_reach(&self, window: &Window) -> bool {
if !window.scanning_kind.contains(ScanKind::IMAGE) {
return false;
}
let Some(frames) = self.frames().filter(|f| !f.frames.is_empty()) else {
return false;
};
let (left, top) = window.origin;
frames
.holding(Rect {
top,
left,
bottom: top + window.size.1,
right: left + window.size.0,
})
.is_none()
}
/// Define one window, which is also what moves the stage
///
/// An image window inside a known frame steps straight to it; one no frame
/// holds sends the mechanism out to its home stop first. The unit takes it
/// either way and the image is the same, so this says so rather than refusing
pub fn set_window(&mut self, window: &Window) -> Result<(), Error> {
window.validate(&self.caps)?;
if self.homes_to_reach(window) {
warn!(
id = window.id,
?window.origin,
?window.size,
"no frame holds this window, so the stage will home to reach it"
);
}
let header = SetWindowHeader {
descriptor_length: window::LENGTH as u16,
};
let mut payload = Vec::with_capacity(window::HEADER + window::LENGTH);
payload.extend_from_slice(&header.to_bytes());
payload.extend_from_slice(&window.to_bytes());
let cmd = SetWindow::new(payload.len() as u32);
debug!(
id = window.id,
?window.origin,
?window.size,
"setting window"
);
// The descriptor is the one place a pass's geometry actually lives, so
// it is what a capture of another driver has to be compared against
if enabled!(Level::TRACE) {
let hex: Vec<String> = payload.iter().map(|b| format!("{b:02X}")).collect();
trace!(id = window.id, bytes = hex.join(" "), "set window payload");
}
// setting windows is not a cooperative action
self.transport
.execute(&cmd.cdb(), Data::Out(&payload), MOVE_TIMEOUT)?;
Ok(())
}
/// Scan the windows named, and hand back what it will produce
///
/// 2-7: the unit answers, then scans, so this returns once it has started
/// and [`test_unit_ready`](Session::test_unit_ready) says when the data is
/// there.
///
/// A cooperative request is not a blocker. The captures read the
/// `DataType::Cooperation` record and
/// send SCAN again with nothing in between: it says what the host will owe
/// the *data*, not what has to happen before the scan runs. Whatever it
/// asks for comes back on [`Started::cooperations`] for the caller to honor
/// once the image is read.
pub fn scan(&mut self, windows: &[Window]) -> Result<Started, Error> {
// Checks every rule spanning the set on the way
let ids: Vec<u8> = windows.iter().map(|w| w.id).collect();
let cmd = Scan::new(ids.len() as u8);
debug!("scan cmd {:02x?}", &cmd.cdb());
// 2-7: the unit answers, then asks what the host will owe the data.
// `run_handshake` reads the `DataType::Cooperation` record, sends SCAN
// again with nothing in between, and hands the record back so the
// caller can honor it once the image is read
let (_, cooperations) = self.run_handshake(&cmd.cdb(), Data::Out(&ids), MOVE_TIMEOUT)?;
debug!(?ids, ?cooperations, "scanning");
let truncation = cooperations.iter().find_map(|c| match c {
CooperativeAction::Truncate(t) => Some(t),
_ => None,
});
// The scan is valid from here, so a layout the unit's own numbers do
// not describe has to stop it rather than return through it
let mut layout = match Layout::new(&self.caps, windows, self.divisor, truncation) {
Ok(layout) => layout,
Err(e) => {
self.abandon_scan();
return Err(e);
}
};
layout.multiline_registered = CooperativeAction::any_multiline_registration(&cooperations);
Ok(Started {
layout,
cooperations,
})
}
}
/// A scan that has started
#[derive(Debug, Clone)]
pub struct Started {
/// What the stream will look like
pub layout: Layout,
/// What the unit asked the host to do with the data, if anything. Reading
/// the record is what lets the scan proceed; honoring it is the caller's
pub cooperations: Vec<CooperativeAction>,
}