usbip_device/
response.rs

1use crate::{cmd::UsbIpHeader, debug::DbgBuf};
2use std::fmt::{Debug, Formatter, Result as FmtResult};
3
4#[derive(Clone)]
5pub struct UsbIpResponse {
6    pub header: UsbIpHeader,
7    pub cmd: UsbIpResponseCmd,
8    pub data: Vec<u8>,
9}
10
11impl Debug for UsbIpResponse {
12    fn fmt(&self, f: &mut Formatter) -> FmtResult {
13        f.debug_struct("UsbIpResponse")
14            .field("header", &self.header)
15            .field("cmd", &self.cmd)
16            .field("data", &DbgBuf(&self.data))
17            .finish()
18    }
19}
20
21#[derive(Debug, Clone)]
22pub enum UsbIpResponseCmd {
23    Cmd(UsbIpRetSubmit),
24    Unlink(UsbIpRetUnlink),
25}
26
27impl UsbIpResponse {
28    pub fn to_vec(&self) -> Option<Vec<u8>> {
29        let mut result = vec![];
30
31        // Parse the header
32        result.extend_from_slice(&self.header.to_array());
33
34        // parse the command
35        match self.cmd {
36            UsbIpResponseCmd::Cmd(ref cmd) => {
37                result.extend_from_slice(&cmd.to_array());
38            }
39            UsbIpResponseCmd::Unlink(ref unlink) => {
40                result.extend_from_slice(&unlink.to_array());
41            }
42        }
43
44        // parse the data
45        result.extend_from_slice(&self.data[..]);
46
47        Some(result)
48    }
49}
50
51#[derive(Clone)]
52pub struct UsbIpRetSubmit {
53    pub status: i32,
54    pub actual_length: i32,
55    pub start_frame: i32,
56    pub number_of_packets: i32,
57    pub error_count: i32,
58}
59
60impl Debug for UsbIpRetSubmit {
61    /// As `start_frame`, `number_of_packets` and `error_count` are unused as of now,
62    /// they are not being printed
63    fn fmt(&self, f: &mut Formatter) -> FmtResult {
64        f.debug_struct("UsbIpRetSubmit")
65            .field("status", &self.status)
66            .field("actual_length", &self.actual_length)
67            .finish()
68    }
69}
70
71impl UsbIpRetSubmit {
72    fn to_array(&self) -> [u8; 28] {
73        let mut result = [0; 28];
74
75        result[0..4].copy_from_slice(&self.status.to_be_bytes());
76        result[4..8].copy_from_slice(&self.actual_length.to_be_bytes());
77        result[8..12].copy_from_slice(&self.start_frame.to_be_bytes());
78        result[12..16].copy_from_slice(&self.number_of_packets.to_be_bytes());
79        result[16..20].copy_from_slice(&self.error_count.to_be_bytes());
80
81        result
82    }
83}
84
85#[derive(Debug, Clone)]
86pub struct UsbIpRetUnlink {
87    pub status: u32,
88}
89
90impl UsbIpRetUnlink {
91    fn to_array(&self) -> [u8; 28] {
92        let mut result = [0; 28];
93        result[0..4].copy_from_slice(&self.status.to_be_bytes());
94        result
95    }
96}