summaryrefslogtreecommitdiff
path: root/src/usb/mod.rs
blob: 38d9c7d9ae41a2b15982cc4d6b4c39b406dc229e (plain)
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
//! USB communication module for the Geek szitman supercamera

mod device;
mod transfer;

pub use device::UsbSupercamera;
pub use transfer::{UsbTransferConfig, UsbTransferError, UsbTransferResult, UsbTransferStats};

use rusb::{Direction, TransferType};
use std::time::Duration;

/// USB device constants
pub const USB_VENDOR_ID: u16 = 0x2ce3;
pub const USB_PRODUCT_ID: u16 = 0x3828;
pub const INTERFACE_A_NUMBER: u8 = 0;
pub const INTERFACE_B_NUMBER: u8 = 1;
pub const INTERFACE_B_ALTERNATE_SETTING: u8 = 1;
pub const ENDPOINT_1: u8 = 1;
pub const ENDPOINT_2: u8 = 2;
pub const USB_TIMEOUT: Duration = Duration::from_millis(1000);

/// USB device information
#[derive(Debug, Clone)]
pub struct UsbDeviceInfo {
    pub vendor_id: u16,
    pub product_id: u16,
    pub manufacturer: Option<String>,
    pub product: Option<String>,
    pub serial_number: Option<String>,
}

impl Default for UsbDeviceInfo {
    fn default() -> Self {
        Self {
            vendor_id: USB_VENDOR_ID,
            product_id: USB_PRODUCT_ID,
            manufacturer: None,
            product: None,
            serial_number: None,
        }
    }
}

/// USB endpoint configuration
#[derive(Debug, Clone)]
pub struct UsbEndpoint {
    pub address: u8,
    pub direction: Direction,
    pub transfer_type: TransferType,
    pub max_packet_size: u16,
}

impl UsbEndpoint {
    /// Create a new USB endpoint
    pub fn new(
        address: u8,
        direction: Direction,
        transfer_type: TransferType,
        max_packet_size: u16,
    ) -> Self {
        Self {
            address,
            direction,
            transfer_type,
            max_packet_size,
        }
    }
}

/// USB interface configuration
#[derive(Debug, Clone)]
pub struct UsbInterface {
    pub number: u8,
    pub alternate_setting: u8,
    pub endpoints: Vec<UsbEndpoint>,
}

impl UsbInterface {
    /// Create a new USB interface
    pub fn new(number: u8, alternate_setting: u8) -> Self {
        Self {
            number,
            alternate_setting,
            endpoints: Vec::new(),
        }
    }

    /// Add an endpoint to this interface
    pub fn add_endpoint(&mut self, endpoint: UsbEndpoint) {
        self.endpoints.push(endpoint);
    }
}

/// USB device configuration
#[derive(Debug, Clone)]
pub struct UsbConfig {
    pub interfaces: Vec<UsbInterface>,
    pub max_packet_size: u16,
}

impl Default for UsbConfig {
    fn default() -> Self {
        let mut interface_a = UsbInterface::new(INTERFACE_A_NUMBER, 0);
        interface_a.add_endpoint(UsbEndpoint::new(
            ENDPOINT_1,
            Direction::In,
            TransferType::Bulk,
            0x1000,
        ));

        let mut interface_b = UsbInterface::new(INTERFACE_B_NUMBER, INTERFACE_B_ALTERNATE_SETTING);
        interface_b.add_endpoint(UsbEndpoint::new(
            ENDPOINT_2,
            Direction::Out,
            TransferType::Bulk,
            64,
        ));

        Self {
            interfaces: vec![interface_a, interface_b],
            max_packet_size: 0x1000,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_usb_device_info_default() {
        let info = UsbDeviceInfo::default();
        assert_eq!(info.vendor_id, USB_VENDOR_ID);
        assert_eq!(info.product_id, USB_PRODUCT_ID);
    }

    #[test]
    fn test_usb_endpoint_creation() {
        let endpoint = UsbEndpoint::new(ENDPOINT_1, Direction::In, TransferType::Bulk, 0x1000);
        assert_eq!(endpoint.address, ENDPOINT_1);
        assert_eq!(endpoint.direction, Direction::In);
        assert_eq!(endpoint.transfer_type, TransferType::Bulk);
        assert_eq!(endpoint.max_packet_size, 0x1000);
    }

    #[test]
    fn test_usb_interface_management() {
        let mut interface = UsbInterface::new(0, 0);
        let endpoint = UsbEndpoint::new(1, Direction::In, TransferType::Bulk, 64);

        interface.add_endpoint(endpoint);
        assert_eq!(interface.endpoints.len(), 1);
        assert_eq!(interface.endpoints[0].address, 1);
    }

    #[test]
    fn test_usb_config_default() {
        let config = UsbConfig::default();
        assert_eq!(config.interfaces.len(), 2);
        assert_eq!(config.interfaces[0].number, INTERFACE_A_NUMBER);
        assert_eq!(config.interfaces[1].number, INTERFACE_B_NUMBER);
        assert_eq!(config.max_packet_size, 0x1000);
    }
}