capsule_library/import/camera/
ptp_device.rs1use std::io::{Read, Write};
4
5use ptpip::session::Session;
6use ptpip::{DeviceInfo, ObjectHandle, OperationCode, StorageId};
7
8use super::{CameraDevice, CameraError, CameraObject};
9
10const PARTIAL_CHUNK: u32 = 8 * 1024 * 1024;
12
13#[derive(Debug)]
15pub struct PtpCamera<C, E> {
16 session: Session<C, E>,
17 device: DeviceInfo,
18}
19
20impl<C: Read + Write, E: Read + Write> PtpCamera<C, E> {
21 pub fn new(session: Session<C, E>, device: DeviceInfo) -> Self {
23 Self { session, device }
24 }
25
26 pub fn device(&self) -> &DeviceInfo {
28 &self.device
29 }
30
31 pub fn into_session(self) -> Session<C, E> {
33 self.session
34 }
35}
36
37impl From<ptpip::tcp::Camera> for PtpCamera<std::net::TcpStream, std::net::TcpStream> {
38 fn from(camera: ptpip::tcp::Camera) -> Self {
39 Self::new(camera.session, camera.device)
40 }
41}
42
43impl From<ptpip::Error> for CameraError {
44 fn from(error: ptpip::Error) -> Self {
45 match error {
46 ptpip::Error::Response { operation, code } => CameraError::Refused {
47 operation: operation.to_string(),
48 code: code.to_string(),
49 },
50 ptpip::Error::Sink(source) => CameraError::Sink(source),
51 other => CameraError::Disconnected(other.to_string()),
52 }
53 }
54}
55
56impl<C: Read + Write, E: Read + Write> CameraDevice for PtpCamera<C, E> {
57 fn supports_partial_reads(&self) -> bool {
58 self.device.supports(OperationCode::GET_PARTIAL_OBJECT)
59 }
60
61 fn storage_ids(&mut self) -> Result<Vec<u32>, CameraError> {
62 Ok(self
63 .session
64 .storage_ids()?
65 .into_iter()
66 .map(|id| id.0)
67 .collect())
68 }
69
70 fn object_handles(&mut self, storage: u32) -> Result<Vec<u32>, CameraError> {
71 Ok(self
72 .session
73 .object_handles(StorageId(storage))?
74 .into_iter()
75 .map(|handle| handle.0)
76 .collect())
77 }
78
79 fn object(&mut self, handle: u32) -> Result<CameraObject, CameraError> {
80 let info = self.session.object_info(ObjectHandle(handle))?;
81 Ok(CameraObject {
82 storage: info.storage_id.0,
83 handle,
84 parent: info.parent.0,
85 size: info.known_size(),
86 captured_at: info.captured_at().map(|at| at.civil),
87 is_folder: info.format.is_association(),
88 filename: info.filename,
89 })
90 }
91
92 fn read_object(
93 &mut self,
94 object: &CameraObject,
95 offset: u64,
96 sink: &mut dyn Write,
97 ) -> Result<u64, CameraError> {
98 let handle = ObjectHandle(object.handle);
99 if offset == 0 {
100 return Ok(self.session.get_object(handle, sink)?);
101 }
102 let unsupported = CameraError::ResumeUnsupported {
103 handle: object.handle,
104 offset,
105 };
106 let Some(size) = object.size else {
107 return Err(unsupported);
108 };
109 if !self.supports_partial_reads() || u32::try_from(offset).is_err() {
110 return Err(unsupported);
111 }
112 let mut position = offset;
113 while position < size {
114 let Ok(at) = u32::try_from(position) else {
116 return Err(CameraError::ResumeUnsupported {
117 handle: object.handle,
118 offset: position,
119 });
120 };
121 let want = u32::try_from(size - position)
122 .map_or(PARTIAL_CHUNK, |left| left.min(PARTIAL_CHUNK));
123 let got = self.session.get_partial_object(handle, at, want, sink)?;
124 tracing::trace!(handle = %handle, at, got, "camera import: partial chunk");
125 if got == 0 {
126 break;
128 }
129 position += got;
130 }
131 Ok(position - offset)
132 }
133}
134
135#[cfg(test)]
136mod tests;