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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
//! USB transfer handling for the Geek szitman supercamera
use crate::error::Result;
use rusb::{Direction, TransferType};
use std::time::Duration;
use tracing::{error, trace};
/// USB transfer error types
#[derive(Debug, thiserror::Error)]
pub enum UsbTransferError {
/// Transfer timeout
#[error("Transfer timeout after {:?}", duration)]
Timeout { duration: Duration },
/// Transfer failed
#[error("Transfer failed: {}", reason)]
Failed { reason: String },
/// Invalid endpoint
#[error("Invalid endpoint: {}", endpoint)]
InvalidEndpoint { endpoint: u8 },
/// Buffer too small
#[error("Buffer too small: required {}, provided {}", required, provided)]
BufferTooSmall { required: usize, provided: usize },
}
/// USB transfer configuration
#[derive(Debug, Clone)]
pub struct UsbTransferConfig {
pub timeout: Duration,
pub retry_count: u32,
pub buffer_size: usize,
}
impl Default for UsbTransferConfig {
fn default() -> Self {
Self {
timeout: Duration::from_millis(1000),
retry_count: 3,
buffer_size: 0x1000,
}
}
}
/// USB transfer result
#[derive(Debug)]
pub struct UsbTransferResult {
pub bytes_transferred: usize,
pub endpoint: u8,
pub direction: Direction,
pub transfer_type: TransferType,
}
impl UsbTransferResult {
/// Create a new transfer result
pub fn new(
bytes_transferred: usize,
endpoint: u8,
direction: Direction,
transfer_type: TransferType,
) -> Self {
Self {
bytes_transferred,
endpoint,
direction,
transfer_type,
}
}
/// Check if the transfer was successful
pub fn is_successful(&self) -> bool {
self.bytes_transferred > 0
}
/// Get the effective endpoint address
pub fn endpoint_address(&self) -> u8 {
match self.direction {
Direction::In => self.endpoint | 0x80,
Direction::Out => self.endpoint,
}
}
}
/// USB transfer statistics
#[derive(Debug, Default)]
pub struct UsbTransferStats {
pub total_transfers: u64,
pub successful_transfers: u64,
pub failed_transfers: u64,
pub total_bytes_transferred: u64,
pub average_transfer_size: f64,
}
impl UsbTransferStats {
/// Update statistics with a transfer result
pub fn update(&mut self, result: &UsbTransferResult) {
self.total_transfers += 1;
if result.is_successful() {
self.successful_transfers += 1;
self.total_bytes_transferred += result.bytes_transferred as u64;
} else {
self.failed_transfers += 1;
}
// Update average transfer size
if self.successful_transfers > 0 {
self.average_transfer_size =
self.total_bytes_transferred as f64 / self.successful_transfers as f64;
}
}
/// Get success rate as a percentage
pub fn success_rate(&self) -> f64 {
if self.total_transfers == 0 {
0.0
} else {
(self.successful_transfers as f64 / self.total_transfers as f64) * 100.0
}
}
/// Reset statistics
pub fn reset(&mut self) {
*self = Self::default();
}
}
/// USB transfer manager
pub struct UsbTransferManager {
config: UsbTransferConfig,
stats: UsbTransferStats,
}
impl UsbTransferManager {
/// Create a new transfer manager
pub fn new(config: UsbTransferConfig) -> Self {
Self {
config,
stats: UsbTransferStats::default(),
}
}
/// Create a transfer manager with default configuration
pub fn new_default() -> Self {
Self::new(UsbTransferConfig::default())
}
/// Get current statistics
pub fn stats(&self) -> &UsbTransferStats {
&self.stats
}
/// Get mutable reference to statistics
pub fn stats_mut(&mut self) -> &mut UsbTransferStats {
&mut self.stats
}
/// Reset statistics
pub fn reset_stats(&mut self) {
self.stats.reset();
}
/// Validate endpoint configuration
pub fn validate_endpoint(
&self,
endpoint: u8,
direction: Direction,
transfer_type: TransferType,
) -> Result<()> {
if endpoint > 15 {
return Err(UsbTransferError::InvalidEndpoint { endpoint }.into());
}
// Validate endpoint address based on direction
let endpoint_address = match direction {
Direction::In => endpoint | 0x80,
Direction::Out => endpoint,
};
trace!(
"Validated endpoint: 0x{:02X} ({:?}, {:?})",
endpoint_address,
direction,
transfer_type
);
Ok(())
}
/// Validate buffer size
pub fn validate_buffer(&self, buffer_size: usize) -> Result<()> {
if buffer_size < self.config.buffer_size {
return Err(UsbTransferError::BufferTooSmall {
required: self.config.buffer_size,
provided: buffer_size,
}
.into());
}
Ok(())
}
/// Get transfer configuration
pub fn config(&self) -> &UsbTransferConfig {
&self.config
}
/// Update transfer configuration
pub fn update_config(&mut self, config: UsbTransferConfig) {
self.config = config;
}
}
impl Default for UsbTransferManager {
fn default() -> Self {
Self::new_default()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_usb_transfer_config_default() {
let config = UsbTransferConfig::default();
assert_eq!(config.timeout, Duration::from_millis(1000));
assert_eq!(config.retry_count, 3);
assert_eq!(config.buffer_size, 0x1000);
}
#[test]
fn test_usb_transfer_result() {
let result = UsbTransferResult::new(1024, 1, Direction::In, TransferType::Bulk);
assert_eq!(result.bytes_transferred, 1024);
assert_eq!(result.endpoint, 1);
assert!(result.is_successful());
assert_eq!(result.endpoint_address(), 0x81);
}
#[test]
fn test_usb_transfer_stats() {
let mut stats = UsbTransferStats::default();
let result = UsbTransferResult::new(1024, 1, Direction::In, TransferType::Bulk);
stats.update(&result);
assert_eq!(stats.total_transfers, 1);
assert_eq!(stats.successful_transfers, 1);
assert_eq!(stats.failed_transfers, 0);
assert_eq!(stats.total_bytes_transferred, 1024);
assert_eq!(stats.success_rate(), 100.0);
}
#[test]
fn test_usb_transfer_manager() {
let manager = UsbTransferManager::new_default();
assert_eq!(manager.config().timeout, Duration::from_millis(1000));
assert_eq!(manager.stats().total_transfers, 0);
}
#[test]
fn test_endpoint_validation() {
let manager = UsbTransferManager::new_default();
// Valid endpoint
assert!(manager
.validate_endpoint(1, Direction::In, TransferType::Bulk)
.is_ok());
// Invalid endpoint
assert!(manager
.validate_endpoint(16, Direction::In, TransferType::Bulk)
.is_err());
}
#[test]
fn test_buffer_validation() {
let manager = UsbTransferManager::new_default();
// Valid buffer size
assert!(manager.validate_buffer(0x1000).is_ok());
// Invalid buffer size
assert!(manager.validate_buffer(0x800).is_err());
}
}
|