blob: 5beb1a53cc3ffcf14c1aaf3f4e103af727b3cb52 [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);
39
40 ~UsbInterfaceDescriptor();
41
42 // Getters for retrieving fields of the libusb_interface_descriptor struct.
Ben Chan956f79b2014-08-06 17:11:01 -070043 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 Chan53551302013-05-28 13:23:02 -070051 std::string GetInterfaceDescription() const;
52
Ben Chan64b60f62019-06-06 16:57:30 -070053 // 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 Chan53551302013-05-28 13:23:02 -070058
Ben Chan64b60f62019-06-06 16:57:30 -070059 // 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 Chan53551302013-05-28 13:23:02 -070066
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.
81std::ostream& operator<<(
82 std::ostream& stream,
83 const mist::UsbInterfaceDescriptor& interface_descriptor);
84
85#endif // MIST_USB_INTERFACE_DESCRIPTOR_H_