1use super::constants::*;
2use super::helpers::{descriptor_bytes, read_u16_le, read_u32_le, require_len};
3use super::parser_trait::DescriptorParser;
4use super::types::*;
5use super::validation::{
6 validate_as_interface_general, validate_format_type_i, validate_format_type_ii,
7 validate_format_type_iii,
8};
9use crate::descriptors::error::DescriptorError;
10
11pub struct AudioStreamingParser;
12
13impl AudioStreamingParser {
14 pub fn parse_as_general(
15 &self,
16 data: &[u8],
17 ) -> Result<AudioStreamingDescriptor, DescriptorError> {
18 if data.len() < 7 {
19 return Err(DescriptorError::Invalid("AS general too short".to_string()));
20 }
21 let data = descriptor_bytes(data, 7)?;
22 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC2_AS_GENERAL {
23 return Err(DescriptorError::Invalid("not AS general".to_string()));
24 }
25 let len = data[0] as usize;
30 if len >= 9 {
31 let g = AsInterfaceGeneralV2 {
33 b_terminal_link: data[3],
34 b_delay: data[4],
35 bm_controls: read_u16_le(data, 5)?,
36 w_format_tag: read_u16_le(data, 7)?,
37 };
38 validate_as_interface_general(&AsInterfaceGeneral {
39 b_terminal_link: g.b_terminal_link,
40 b_delay: g.b_delay,
41 w_format_tag: g.w_format_tag,
42 bm_controls: g.bm_controls,
43 })?;
44 Ok(AudioStreamingDescriptor::GeneralV2(g))
45 } else {
46 let g = AsInterfaceGeneralV1 {
48 b_terminal_link: data[3],
49 b_delay: data[4],
50 w_format_tag: read_u16_le(data, 5)?,
51 };
52 Ok(AudioStreamingDescriptor::GeneralV1(g))
53 }
54 }
55
56 pub fn parse_as_general_v2(&self, data: &[u8]) -> Result<AsInterfaceGeneral, DescriptorError> {
58 const LEN: usize = 7;
59 let data = descriptor_bytes(data, LEN)?;
60 if data[1] != USB_DT_CS_INTERFACE || data[2] != UAC2_AS_GENERAL {
61 return Err(DescriptorError::Invalid("not AS general".to_string()));
62 }
63 let len = data[0] as usize;
64 let (b_terminal_link, b_delay, w_format_tag, bm_controls) = if len >= 9 {
65 (
66 data[3],
67 data[4],
68 read_u16_le(data, 7)?,
69 read_u16_le(data, 5)?,
70 )
71 } else {
72 (data[3], data[4], read_u16_le(data, 5)?, 0u16)
73 };
74 let g = AsInterfaceGeneral {
75 b_terminal_link,
76 b_delay,
77 w_format_tag,
78 bm_controls,
79 };
80 validate_as_interface_general(&g)?;
81 Ok(g)
82 }
83
84 pub fn parse_format_type_i(&self, data: &[u8]) -> Result<FormatTypeI, DescriptorError> {
85 const MIN_LEN: usize = 8;
86 let data = descriptor_bytes(data, MIN_LEN)?;
87 if data[1] != USB_DT_CS_INTERFACE
88 || data[2] != UAC2_FORMAT_TYPE
89 || data[3] != UAC2_FORMAT_TYPE_I
90 {
91 return Err(DescriptorError::Invalid("not format type I".to_string()));
92 }
93 let b_sam_freq_type = data[6];
94 let mut sample_rates: Vec<u32> = Vec::new();
95 if b_sam_freq_type == 0 {
96 if data.len() >= 10 {
97 sample_rates.push(read_u32_le(data, 7)?);
98 }
99 } else {
100 let num_rates = b_sam_freq_type as usize;
101 require_len(data, 7 + num_rates * 6)?;
102 for i in 0..num_rates {
103 sample_rates.push(read_u32_le(data, 7 + i * 6)?);
104 }
105 }
106 let f = FormatTypeI {
107 b_subslot_size: data[4],
108 b_bit_resolution: data[5],
109 b_sam_freq_type,
110 sample_rates,
111 };
112 validate_format_type_i(&f)?;
113 Ok(f)
114 }
115
116 pub fn parse_format_type_ii(&self, data: &[u8]) -> Result<FormatTypeII, DescriptorError> {
117 const MIN_LEN: usize = 10;
118 let data = descriptor_bytes(data, MIN_LEN)?;
119 if data[1] != USB_DT_CS_INTERFACE
120 || data[2] != UAC2_FORMAT_TYPE
121 || data[3] != UAC2_FORMAT_TYPE_II
122 {
123 return Err(DescriptorError::Invalid("not format type II".to_string()));
124 }
125 let b_sam_freq_type = data[9];
126 let mut sample_rates: Vec<u32> = Vec::new();
127 if b_sam_freq_type == 0 {
128 if data.len() >= 13 {
129 sample_rates.push(read_u32_le(data, 10)?);
130 }
131 } else {
132 let num_rates = b_sam_freq_type as usize;
133 require_len(data, 10 + num_rates * 6)?;
134 for i in 0..num_rates {
135 sample_rates.push(read_u32_le(data, 10 + i * 6)?);
136 }
137 }
138 let f = FormatTypeII {
139 w_max_bit_rate: read_u16_le(data, 4)?,
140 w_samples_per_frame: read_u16_le(data, 6)?,
141 b_sam_freq_type,
142 sample_rates,
143 };
144 validate_format_type_ii(&f)?;
145 Ok(f)
146 }
147
148 pub fn parse_format_type_iii(&self, data: &[u8]) -> Result<FormatTypeIII, DescriptorError> {
149 const LEN: usize = 6;
150 let data = descriptor_bytes(data, LEN)?;
151 if data[1] != USB_DT_CS_INTERFACE
152 || data[2] != UAC2_FORMAT_TYPE
153 || data[3] != UAC2_FORMAT_TYPE_III
154 {
155 return Err(DescriptorError::Invalid("not format type III".to_string()));
156 }
157 let f = FormatTypeIII {
158 b_subslot_size: data[4],
159 b_bit_resolution: data[5],
160 };
161 validate_format_type_iii(&f)?;
162 Ok(f)
163 }
164
165 pub fn parse_endpoint_specific(
166 &self,
167 data: &[u8],
168 ) -> Result<EndpointSpecific, DescriptorError> {
169 const LEN: usize = 5;
170 let data = descriptor_bytes(data, LEN)?;
171 if data[1] != USB_DT_CS_ENDPOINT {
172 return Err(DescriptorError::Invalid("not CS endpoint".to_string()));
173 }
174 Ok(EndpointSpecific {
175 b_endpoint_address: data[2],
176 bm_attributes: data.get(3).copied().unwrap_or(0),
177 b_lock_delay_units: data.get(4).copied().unwrap_or(0),
178 w_lock_delay: if data.len() >= 7 {
179 read_u16_le(data, 5)?
180 } else {
181 0
182 },
183 })
184 }
185}
186
187impl DescriptorParser for AudioStreamingParser {
188 type Output = AudioStreamingDescriptor;
189
190 fn parse(&self, data: &[u8]) -> Result<Self::Output, DescriptorError> {
191 if data.len() < 4 {
192 return Err(DescriptorError::Invalid("descriptor too short".to_string()));
193 }
194 let data = descriptor_bytes(data, 4)?;
195 if data[1] == USB_DT_CS_ENDPOINT {
197 return self
198 .parse_endpoint_specific(data)
199 .map(AudioStreamingDescriptor::EndpointSpecific);
200 }
201 if data[1] != USB_DT_CS_INTERFACE {
202 return Err(DescriptorError::Invalid("not CS interface".to_string()));
203 }
204 match (data[2], data.get(3).copied().unwrap_or(0)) {
205 (UAC2_AS_GENERAL, _) => self.parse_as_general(data),
206 (UAC2_FORMAT_TYPE, UAC2_FORMAT_TYPE_I) => self
207 .parse_format_type_i(data)
208 .map(AudioStreamingDescriptor::FormatTypeI),
209 (UAC2_FORMAT_TYPE, UAC2_FORMAT_TYPE_II) => self
210 .parse_format_type_ii(data)
211 .map(AudioStreamingDescriptor::FormatTypeII),
212 (UAC2_FORMAT_TYPE, UAC2_FORMAT_TYPE_III) => self
213 .parse_format_type_iii(data)
214 .map(AudioStreamingDescriptor::FormatTypeIII),
215 _ => Err(DescriptorError::Invalid(format!(
216 "unknown AS descriptor subtype {} {}",
217 data[2],
218 data.get(3).unwrap_or(&0)
219 ))),
220 }
221 }
222}