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
use crate::uevent::UEvent;
use serde::Serialize;
use std::collections::HashMap;
use std::str::FromStr;

/// De-serialized /sys/class/bus/usb to a Rust structure.
/// Note unknown fields are stored in raw format in the attributes: HashMap<String, String>
#[derive(Serialize, Default)]
pub struct UsbAttributes {
    pub num_endpoints: u8,
    pub num_configurations: u8,
    pub num_interfaces: u8,
    pub interface_class: u8,
    pub interface_number: u8,
    pub interface_protocol: u8,
    pub alternate_setting: u8,
    pub interface_subclass: u8,
    pub max_packet_size0: u16,
    pub supports_autosuspend: u8,
    pub authorized: u8,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub modalias: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id_product: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id_vendor: Option<String>,
    //#[serde(skip_serializing_if = "Option::is_none")]
    pub bus_num: u8,
    pub dev_num: u8,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub product: String,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub manufacturer: String,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub serial: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uevent: Option<UEvent>,
    #[serde(skip)]
    pub descriptors: Vec<u8>,
    #[serde(flatten, skip_serializing_if = "HashMap::is_empty")]
    pub attributes: HashMap<String, String>,
}

impl UsbAttributes {
    pub(crate) fn add(&mut self, key: &str, value: Vec<u8>) -> Result<(), std::num::ParseIntError> {
        if key == "descriptors" {
            // Raw descriptors from the device is stored in a Vec.
            self.descriptors = value;
            return Ok(());
        }

        let value = if let Ok(value) = String::from_utf8(value) {
            value.trim().to_string()
        } else {
            log::warn!("Could not decode {} as UTF8 string.", key);
            return Ok(());
        };
        match &key[0..] {
            "busnum" => self.bus_num = value.parse()?,
            "devnum" => self.dev_num = value.parse()?,
            "supports_autosuspend" => self.supports_autosuspend = value.parse()?,
            "authorized" => self.authorized = value.parse()?,
            "uevent" => self.uevent = UEvent::from_str(&value).ok(),
            "bNumEndpoints" => self.num_endpoints = value.parse()?,
            "bNumInterfaces" => self.num_interfaces = value.parse()?,
            "bNumConfigurations" => self.num_configurations = value.parse()?,
            "bInterfaceNumber" => self.interface_number = u8::from_str_radix(&value, 16)?,
            "bInterfaceProtocol" => self.interface_protocol = u8::from_str_radix(&value, 16)?,
            "bInterfaceClass" => self.interface_class = u8::from_str_radix(&value, 16)?,
            "bInterfaceSubClass" => self.interface_subclass = u8::from_str_radix(&value, 16)?,
            "bAlternateSetting" => self.alternate_setting = u8::from_str_radix(&value, 16)?,
            "bMaxPacketSize0" => self.max_packet_size0 = value.parse()?,
            "manufacturer" => self.manufacturer = value,
            "product" => self.product = value,
            "serial" => self.serial = value,
            _ => {
                self.attributes.insert(key.into(), value);
            }
        }

        Ok(())
    }
}