blob: 156c83e8b2a7f4ab9480f42377d7fd19870371eb [file] [log] [blame]
Ben Chan53551302013-05-28 13:23:02 -07001// 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 Chan956f79b2014-08-06 17:11:01 -07008#include <stdint.h>
9
Ben Chan64b60f62019-06-06 16:57:30 -070010#include <memory>
Ben Chanabfa15a2014-06-16 20:07:35 -070011#include <ostream> // NOLINT(readability/streams)
Ben Chan53551302013-05-28 13:23:02 -070012#include <string>
13
Ben Chan4746c8a2014-09-02 20:34:58 -070014#include <base/macros.h>
Ben Chan53551302013-05-28 13:23:02 -070015#include <base/memory/weak_ptr.h>
16
17#include "mist/usb_constants.h"
18
19struct libusb_interface_descriptor;
20
21namespace mist {
22
23class UsbDevice;
24class UsbEndpointDescriptor;
25
26// A USB interface descriptor, which wraps a libusb_interface_descriptor C
27// struct from libusb 1.0 into a C++ object.
28class 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);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090039 UsbInterfaceDescriptor(const UsbInterfaceDescriptor&) = delete;
40 UsbInterfaceDescriptor& operator=(const UsbInterfaceDescriptor&) = delete;
Ben Chan53551302013-05-28 13:23:02 -070041
42 ~UsbInterfaceDescriptor();
43
44 // Getters for retrieving fields of the libusb_interface_descriptor struct.
Ben Chan956f79b2014-08-06 17:11:01 -070045 uint8_t GetLength() const;
46 uint8_t GetDescriptorType() const;
47 uint8_t GetInterfaceNumber() const;
48 uint8_t GetAlternateSetting() const;
49 uint8_t GetNumEndpoints() const;
50 uint8_t GetInterfaceClass() const;
51 uint8_t GetInterfaceSubclass() const;
52 uint8_t GetInterfaceProtocol() const;
Ben Chan53551302013-05-28 13:23:02 -070053 std::string GetInterfaceDescription() const;
54
Ben Chan64b60f62019-06-06 16:57:30 -070055 // Returns a UsbEndpointDescriptor object for the endpoint descriptor indexed
56 // at |index|, or a nullptr if |index| is invalid. The returned object should
57 // not be held beyond the lifetime of this object.
58 std::unique_ptr<UsbEndpointDescriptor> GetEndpointDescriptor(
59 uint8_t index) const;
Ben Chan53551302013-05-28 13:23:02 -070060
Ben Chan64b60f62019-06-06 16:57:30 -070061 // Returns a UsbEndpointDescriptor object for the first endpoint descriptor
62 // with its transfer type equal to |transfer_type| and its direction equal to
63 // |direction|, or a nullptr if not matching endpoint descriptor is found.
64 // The returned object should not be held beyond the lifetime of this object.
65 std::unique_ptr<UsbEndpointDescriptor>
66 GetEndpointDescriptorByTransferTypeAndDirection(UsbTransferType transfer_type,
67 UsbDirection direction) const;
Ben Chan53551302013-05-28 13:23:02 -070068
69 // Returns a string describing the properties of this object for logging
70 // purpose.
71 std::string ToString() const;
72
73 private:
74 base::WeakPtr<UsbDevice> device_;
75 const libusb_interface_descriptor* const interface_descriptor_;
Ben Chan53551302013-05-28 13:23:02 -070076};
77
78} // namespace mist
79
80// Output stream operator provided to facilitate logging.
81std::ostream& operator<<(
82 std::ostream& stream,
83 const mist::UsbInterfaceDescriptor& interface_descriptor);
84
85#endif // MIST_USB_INTERFACE_DESCRIPTOR_H_