Skip to content

Commit 675dfe4

Browse files
committed
Minor formatting fixes
1 parent 3eb79b6 commit 675dfe4

File tree

1 file changed

+63
-65
lines changed

1 file changed

+63
-65
lines changed

src/descriptor.rs

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub mod descriptor_type {
1111
pub const STRING: u8 = 3;
1212
pub const INTERFACE: u8 = 4;
1313
pub const ENDPOINT: u8 = 5;
14-
pub const BOS: u8 = 0x0f;
15-
pub const CAPABILITY: u8 = 0x10;
14+
pub const BOS: u8 = 15;
15+
pub const CAPABILITY: u8 = 16;
1616
}
1717

1818
/// String descriptor language IDs.
@@ -41,68 +41,6 @@ pub struct DescriptorWriter<'a> {
4141
num_endpoints_mark: Option<usize>,
4242
}
4343

44-
/// A writer for Binary Object Store descriptor.
45-
pub struct BosWriter<'w, 'a: 'w> {
46-
writer: &'w mut DescriptorWriter<'a>,
47-
num_caps_mark: Option<usize>,
48-
}
49-
50-
impl<'w, 'a: 'w> BosWriter<'w, 'a> {
51-
pub(crate) fn new(writer: &'w mut DescriptorWriter<'a>) -> Self {
52-
Self {
53-
writer: writer,
54-
num_caps_mark: None,
55-
}
56-
}
57-
58-
pub(crate) fn bos(&mut self) -> Result<()> {
59-
self.num_caps_mark= Some(self.writer.position + 4);
60-
self.writer.write(
61-
descriptor_type::BOS,
62-
&[
63-
0x00, 0x00, // wTotalLength
64-
0x00, // bNumDeviceCaps
65-
])
66-
}
67-
68-
/// Writes capability descriptor to a BOS
69-
///
70-
/// # Arguments
71-
///
72-
/// * `capability_type` - Type of a capability
73-
/// * `data` - Binary data of the descriptor
74-
pub fn capability(&mut self, capability_type: u8, data: &[u8]) -> Result<()> {
75-
match self.num_caps_mark{
76-
Some(mark) => self.writer.buf[mark] += 1,
77-
None => return Err(UsbError::InvalidState),
78-
}
79-
80-
let mut start = self.writer.position;
81-
let blen = data.len();
82-
83-
if start + blen + 3 > self.writer.buf.len()
84-
|| (blen + 3) > 255 {
85-
return Err(UsbError::BufferOverflow);
86-
}
87-
88-
self.writer.buf[start] = (blen + 3) as u8;
89-
self.writer.buf[start+1] = descriptor_type::CAPABILITY;
90-
self.writer.buf[start+2] = capability_type;
91-
92-
start += 3;
93-
self.writer.buf[start..start+blen].copy_from_slice(data);
94-
self.writer.position = start + blen;
95-
96-
Ok(())
97-
}
98-
99-
pub(crate) fn end_bos(&mut self) {
100-
self.num_caps_mark= None;
101-
let position = self.writer.position as u16;
102-
self.writer.buf[2..4].copy_from_slice(&position.to_le_bytes());
103-
}
104-
}
105-
10644
impl DescriptorWriter<'_> {
10745
pub(crate) fn new(buf: &mut [u8]) -> DescriptorWriter<'_> {
10846
DescriptorWriter {
@@ -138,7 +76,6 @@ impl DescriptorWriter<'_> {
13876
Ok(())
13977
}
14078

141-
14279
pub(crate) fn device(&mut self, config: &device::Config) -> Result<()> {
14380
self.write(
14481
descriptor_type::DEVICE,
@@ -276,3 +213,64 @@ impl DescriptorWriter<'_> {
276213
Ok(())
277214
}
278215
}
216+
217+
/// A writer for Binary Object Store descriptor.
218+
pub struct BosWriter<'w, 'a: 'w> {
219+
writer: &'w mut DescriptorWriter<'a>,
220+
num_caps_mark: Option<usize>,
221+
}
222+
223+
impl<'w, 'a: 'w> BosWriter<'w, 'a> {
224+
pub(crate) fn new(writer: &'w mut DescriptorWriter<'a>) -> Self {
225+
Self {
226+
writer: writer,
227+
num_caps_mark: None,
228+
}
229+
}
230+
231+
pub(crate) fn bos(&mut self) -> Result<()> {
232+
self.num_caps_mark= Some(self.writer.position + 4);
233+
self.writer.write(
234+
descriptor_type::BOS,
235+
&[
236+
0x00, 0x00, // wTotalLength
237+
0x00, // bNumDeviceCaps
238+
])
239+
}
240+
241+
/// Writes capability descriptor to a BOS
242+
///
243+
/// # Arguments
244+
///
245+
/// * `capability_type` - Type of a capability
246+
/// * `data` - Binary data of the descriptor
247+
pub fn capability(&mut self, capability_type: u8, data: &[u8]) -> Result<()> {
248+
match self.num_caps_mark{
249+
Some(mark) => self.writer.buf[mark] += 1,
250+
None => return Err(UsbError::InvalidState),
251+
}
252+
253+
let mut start = self.writer.position;
254+
let blen = data.len();
255+
256+
if (start + blen + 3) > self.writer.buf.len() || (blen + 3) > 255 {
257+
return Err(UsbError::BufferOverflow);
258+
}
259+
260+
self.writer.buf[start] = (blen + 3) as u8;
261+
self.writer.buf[start+1] = descriptor_type::CAPABILITY;
262+
self.writer.buf[start+2] = capability_type;
263+
264+
start += 3;
265+
self.writer.buf[start..start+blen].copy_from_slice(data);
266+
self.writer.position = start + blen;
267+
268+
Ok(())
269+
}
270+
271+
pub(crate) fn end_bos(&mut self) {
272+
self.num_caps_mark= None;
273+
let position = self.writer.position as u16;
274+
self.writer.buf[2..4].copy_from_slice(&position.to_le_bytes());
275+
}
276+
}

0 commit comments

Comments
 (0)