blob: 9d0b8c764d5ad79932d8ba03df62b426a2dcf686 [file] [log] [blame]
// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include <memory>
#include <ostream> // NOLINT(readability/streams)
#include <string>
#include <base/macros.h>
#include <base/memory/weak_ptr.h>
#include <gtest/gtest_prod.h>
struct libusb_config_descriptor;
namespace mist {
class UsbDevice;
class UsbInterface;
// A USB configuration descriptor, which wraps a libusb_config_descriptor C
// struct from libusb 1.0 into a C++ object.
class UsbConfigDescriptor {
// Constructs a UsbConfigDescriptor object by taking a weak pointer to a
// UsbDevice object as |device| and a raw pointer to a
// libusb_config_descriptor struct as |config_descriptor|. |device| is
// used for getting USB string descriptors related to this object. The
// ownership of |config_descriptor| is transferred to this object if
// |own_config_descriptor| is true. Otherwise, |config_descriptor| should
// outlive this object.
UsbConfigDescriptor(const base::WeakPtr<UsbDevice>& device,
libusb_config_descriptor* config_descriptor,
bool own_config_descriptor);
UsbConfigDescriptor(const UsbConfigDescriptor&) = delete;
UsbConfigDescriptor& operator=(const UsbConfigDescriptor&) = delete;
// Destructs this UsbConfigDescriptor object and frees the underlying
// libusb_config_descriptor struct if that is owned by this object.
// Getters for retrieving fields of the libusb_config_descriptor struct.
uint8_t GetLength() const;
uint8_t GetDescriptorType() const;
uint16_t GetTotalLength() const;
uint8_t GetNumInterfaces() const;
uint8_t GetConfigurationValue() const;
std::string GetConfigurationDescription() const;
uint8_t GetAttributes() const;
uint8_t GetMaxPower() const;
// Returns an UsbInterface object for the USB interface indexed at |index|,
// or a nullptr if the index is invalid. The returned object should not be
// held beyond the lifetime of this object.
std::unique_ptr<UsbInterface> GetInterface(uint8_t index) const;
// Returns a string describing the properties of this object for logging
// purpose.
std::string ToString() const;
FRIEND_TEST(UsbConfigDescriptorTest, TrivialGetters);
base::WeakPtr<UsbDevice> device_;
libusb_config_descriptor* config_descriptor_;
bool own_config_descriptor_;
} // namespace mist
// Output stream operator provided to facilitate logging.
std::ostream& operator<<(std::ostream& stream,
const mist::UsbConfigDescriptor& config_descriptor);