Ben Chan | 5355130 | 2013-05-28 13:23:02 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | #ifndef MIST_USB_INTERFACE_DESCRIPTOR_H_ |
| 6 | #define MIST_USB_INTERFACE_DESCRIPTOR_H_ |
| 7 | |
Ben Chan | 956f79b | 2014-08-06 17:11:01 -0700 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Ben Chan | 64b60f6 | 2019-06-06 16:57:30 -0700 | [diff] [blame] | 10 | #include <memory> |
Ben Chan | abfa15a | 2014-06-16 20:07:35 -0700 | [diff] [blame] | 11 | #include <ostream> // NOLINT(readability/streams) |
Ben Chan | 5355130 | 2013-05-28 13:23:02 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Ben Chan | 4746c8a | 2014-09-02 20:34:58 -0700 | [diff] [blame] | 14 | #include <base/macros.h> |
Ben Chan | 5355130 | 2013-05-28 13:23:02 -0700 | [diff] [blame] | 15 | #include <base/memory/weak_ptr.h> |
| 16 | |
| 17 | #include "mist/usb_constants.h" |
| 18 | |
| 19 | struct libusb_interface_descriptor; |
| 20 | |
| 21 | namespace mist { |
| 22 | |
| 23 | class UsbDevice; |
| 24 | class UsbEndpointDescriptor; |
| 25 | |
| 26 | // A USB interface descriptor, which wraps a libusb_interface_descriptor C |
| 27 | // struct from libusb 1.0 into a C++ object. |
| 28 | class UsbInterfaceDescriptor { |
| 29 | public: |
| 30 | // Constructs a UsbInterfaceDescriptor object by taking a weak pointer to a |
| 31 | // UsbDevice object as |device| and a raw pointer to a |
| 32 | // libusb_interface_descriptor struct as |interface_descriptor|. |device| is |
| 33 | // used for getting USB string descriptors related to this object. The |
| 34 | // ownership of |interface_descriptor| is not transferred, and thus it should |
| 35 | // outlive this object. |
| 36 | UsbInterfaceDescriptor( |
| 37 | const base::WeakPtr<UsbDevice>& device, |
| 38 | const libusb_interface_descriptor* interface_descriptor); |
| 39 | |
| 40 | ~UsbInterfaceDescriptor(); |
| 41 | |
| 42 | // Getters for retrieving fields of the libusb_interface_descriptor struct. |
Ben Chan | 956f79b | 2014-08-06 17:11:01 -0700 | [diff] [blame] | 43 | uint8_t GetLength() const; |
| 44 | uint8_t GetDescriptorType() const; |
| 45 | uint8_t GetInterfaceNumber() const; |
| 46 | uint8_t GetAlternateSetting() const; |
| 47 | uint8_t GetNumEndpoints() const; |
| 48 | uint8_t GetInterfaceClass() const; |
| 49 | uint8_t GetInterfaceSubclass() const; |
| 50 | uint8_t GetInterfaceProtocol() const; |
Ben Chan | 5355130 | 2013-05-28 13:23:02 -0700 | [diff] [blame] | 51 | std::string GetInterfaceDescription() const; |
| 52 | |
Ben Chan | 64b60f6 | 2019-06-06 16:57:30 -0700 | [diff] [blame] | 53 | // Returns a UsbEndpointDescriptor object for the endpoint descriptor indexed |
| 54 | // at |index|, or a nullptr if |index| is invalid. The returned object should |
| 55 | // not be held beyond the lifetime of this object. |
| 56 | std::unique_ptr<UsbEndpointDescriptor> GetEndpointDescriptor( |
| 57 | uint8_t index) const; |
Ben Chan | 5355130 | 2013-05-28 13:23:02 -0700 | [diff] [blame] | 58 | |
Ben Chan | 64b60f6 | 2019-06-06 16:57:30 -0700 | [diff] [blame] | 59 | // Returns a UsbEndpointDescriptor object for the first endpoint descriptor |
| 60 | // with its transfer type equal to |transfer_type| and its direction equal to |
| 61 | // |direction|, or a nullptr if not matching endpoint descriptor is found. |
| 62 | // The returned object should not be held beyond the lifetime of this object. |
| 63 | std::unique_ptr<UsbEndpointDescriptor> |
| 64 | GetEndpointDescriptorByTransferTypeAndDirection(UsbTransferType transfer_type, |
| 65 | UsbDirection direction) const; |
Ben Chan | 5355130 | 2013-05-28 13:23:02 -0700 | [diff] [blame] | 66 | |
| 67 | // Returns a string describing the properties of this object for logging |
| 68 | // purpose. |
| 69 | std::string ToString() const; |
| 70 | |
| 71 | private: |
| 72 | base::WeakPtr<UsbDevice> device_; |
| 73 | const libusb_interface_descriptor* const interface_descriptor_; |
| 74 | |
| 75 | DISALLOW_COPY_AND_ASSIGN(UsbInterfaceDescriptor); |
| 76 | }; |
| 77 | |
| 78 | } // namespace mist |
| 79 | |
| 80 | // Output stream operator provided to facilitate logging. |
| 81 | std::ostream& operator<<( |
| 82 | std::ostream& stream, |
| 83 | const mist::UsbInterfaceDescriptor& interface_descriptor); |
| 84 | |
| 85 | #endif // MIST_USB_INTERFACE_DESCRIPTOR_H_ |