usb_descriptor_decoder/descriptors/
topological_desc.rs

1use alloc::vec;
2use alloc::vec::Vec;
3
4use super::{
5    desc_configuration::Configuration,
6    desc_device::Device,
7    desc_endpoint::Endpoint,
8    desc_interface::{Interface, InterfaceAssociation},
9    desc_uvc::uvc_endpoints::UVCVideoControlInterruptEndpoint,
10    parser::ParserMetaData,
11    USBDescriptor,
12};
13
14#[derive(Clone, Debug)]
15pub struct TopologicalUSBDescriptorDevice {
16    pub data: Device,
17    pub child: Vec<TopologicalUSBDescriptorConfiguration>,
18}
19
20#[derive(Clone, Debug)]
21pub struct TopologicalUSBDescriptorConfiguration {
22    pub data: Configuration,
23    pub child: Vec<TopologicalUSBDescriptorFunction>,
24}
25
26#[derive(Clone, Debug)]
27pub enum TopologicalUSBDescriptorFunction {
28    InterfaceAssociation((InterfaceAssociation, Vec<TopologicalUSBDescriptorFunction>)), //maybe we would have multi layer compose device in future? for now just treat it as a trick!
29    Interface(
30        Vec<(
31            Interface,
32            Vec<USBDescriptor>,
33            Vec<TopologicalUSBDescriptorEndpoint>,
34        )>,
35    ),
36}
37#[derive(Clone, Debug)]
38pub struct TopologicalUSBDescriptorRoot {
39    pub device: TopologicalUSBDescriptorDevice,
40    pub others: Vec<USBDescriptor>,
41    pub metadata: ParserMetaData,
42}
43
44#[derive(Clone, Debug)]
45pub enum TopologicalUSBDescriptorEndpoint {
46    Standard(Endpoint),
47    UNVVideoControlInterruptEndpoint(UVCVideoControlInterruptEndpoint),
48}
49
50pub enum USBFunctionExpressions<'a> {
51    Interface(&'a Interface),
52    InterfaceAssociation(&'a InterfaceAssociation),
53    Device(&'a Device),
54}
55
56impl TopologicalUSBDescriptorRoot {
57    pub fn interfaces<'a>(&'a self) -> Vec<USBFunctionExpressions<'a>> {
58        if self.device.data.is_refer_interface() {
59            self.device
60                .child
61                .first()
62                .expect("atleast 1 cfg, this device got some issue!")
63                .child
64                .iter()
65                .map(|int| match int {
66                    TopologicalUSBDescriptorFunction::InterfaceAssociation((ia, _)) => {
67                        USBFunctionExpressions::InterfaceAssociation(ia)
68                    }
69                    TopologicalUSBDescriptorFunction::Interface(vec) => {
70                        USBFunctionExpressions::Interface(
71                            &vec.first()
72                                .expect(
73                                    "atleast 1 interface exist, this device must had some issue!",
74                                )
75                                .0,
76                        )
77                    }
78                })
79                .collect()
80        } else {
81            vec![USBFunctionExpressions::Device(&self.device.data)]
82        }
83    }
84
85    pub fn configs(&self) -> Vec<&Configuration> {
86        self.device.child.iter().map(|cfg| &cfg.data).collect()
87    }
88}
89
90impl<'a> USBFunctionExpressions<'a> {
91    pub fn class_subclass_protocol(&self) -> (u8, u8, u8) {
92        match self {
93            USBFunctionExpressions::Interface(interface) => (
94                interface.interface_class,
95                interface.interface_subclass,
96                interface.interface_protocol,
97            ),
98            USBFunctionExpressions::InterfaceAssociation(interface_association) => (
99                interface_association.function_class,
100                interface_association.function_subclass,
101                interface_association.function_protocol,
102            ),
103            USBFunctionExpressions::Device(device) => {
104                (device.class, device.subclass, device.protocol)
105            }
106        }
107    }
108}