• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::ops::Deref;
6 
7 use crate::bindings::libusb_endpoint_descriptor;
8 use crate::types::{EndpointDirection, EndpointType};
9 
10 /// EndpointDescriptor wraps libusb_endpoint_descriptor.
11 pub struct EndpointDescriptor<'a>(&'a libusb_endpoint_descriptor);
12 
13 const ENDPOINT_DESCRIPTOR_DIRECTION_MASK: u8 = 1 << 7;
14 const ENDPOINT_DESCRIPTOR_NUMBER_MASK: u8 = 0xf;
15 const ENDPOINT_DESCRIPTOR_ATTRIBUTES_TYPE_MASK: u8 = 0x3;
16 
17 impl<'a> EndpointDescriptor<'a> {
18     // Create new endpoint descriptor.
new(descriptor: &libusb_endpoint_descriptor) -> EndpointDescriptor19     pub fn new(descriptor: &libusb_endpoint_descriptor) -> EndpointDescriptor {
20         EndpointDescriptor(descriptor)
21     }
22 
23     // Get direction of this endpoint.
get_direction(&self) -> EndpointDirection24     pub fn get_direction(&self) -> EndpointDirection {
25         let direction = self.0.bEndpointAddress & ENDPOINT_DESCRIPTOR_DIRECTION_MASK;
26         if direction > 0 {
27             EndpointDirection::DeviceToHost
28         } else {
29             EndpointDirection::HostToDevice
30         }
31     }
32 
33     // Get endpoint number.
get_endpoint_number(&self) -> u834     pub fn get_endpoint_number(&self) -> u8 {
35         self.0.bEndpointAddress & ENDPOINT_DESCRIPTOR_NUMBER_MASK
36     }
37 
38     // Get endpoint type.
get_endpoint_type(&self) -> Option<EndpointType>39     pub fn get_endpoint_type(&self) -> Option<EndpointType> {
40         let ep_type = self.0.bmAttributes & ENDPOINT_DESCRIPTOR_ATTRIBUTES_TYPE_MASK;
41         match ep_type {
42             0 => Some(EndpointType::Control),
43             1 => Some(EndpointType::Isochronous),
44             2 => Some(EndpointType::Bulk),
45             3 => Some(EndpointType::Interrupt),
46             _ => None,
47         }
48     }
49 }
50 
51 impl<'a> Deref for EndpointDescriptor<'a> {
52     type Target = libusb_endpoint_descriptor;
53 
deref(&self) -> &libusb_endpoint_descriptor54     fn deref(&self) -> &libusb_endpoint_descriptor {
55         self.0
56     }
57 }
58