blob: 8a8fc6a4e0fe13b19a78c1e575db420f6f8f00c1 [file] [log] [blame]
Ben Chanf4930e52013-05-28 13:24:54 -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#include "mist/usb_interface.h"
6
7#include <libusb.h>
8
9#include <base/logging.h>
Ben Chandcc0c7a2014-02-05 18:03:39 -080010#include <base/strings/stringprintf.h>
Ben Chanf4930e52013-05-28 13:24:54 -070011
12#include "mist/usb_device.h"
13#include "mist/usb_interface_descriptor.h"
14
Ben Chanf4930e52013-05-28 13:24:54 -070015namespace mist {
16
17UsbInterface::UsbInterface(const base::WeakPtr<UsbDevice>& device,
18 const libusb_interface* interface)
Ben Chanc440db52017-09-26 06:30:47 -070019 : device_(device), interface_(interface) {
Ben Chanf4930e52013-05-28 13:24:54 -070020 CHECK(interface_);
21}
22
Ben Chanf4930e52013-05-28 13:24:54 -070023int UsbInterface::GetNumAlternateSettings() const {
24 return interface_->num_altsetting;
25}
26
Ben Chan64b60f62019-06-06 16:57:30 -070027std::unique_ptr<UsbInterfaceDescriptor> UsbInterface::GetAlternateSetting(
28 int index) const {
Ben Chanf4930e52013-05-28 13:24:54 -070029 if (index < 0 || index >= GetNumAlternateSettings()) {
Ben Chan9870f8b2019-06-06 17:31:35 -070030 LOG(ERROR) << base::StringPrintf(
Ben Chanc440db52017-09-26 06:30:47 -070031 "Invalid alternate setting index %d. "
32 "Must be non-negative and less than %d.",
33 index, GetNumAlternateSettings());
Ben Chan61ac5ba2014-08-29 18:06:43 -070034 return nullptr;
Ben Chanf4930e52013-05-28 13:24:54 -070035 }
36
Ben Chan64b60f62019-06-06 16:57:30 -070037 return std::make_unique<UsbInterfaceDescriptor>(
38 device_, &interface_->altsetting[index]);
Ben Chanf4930e52013-05-28 13:24:54 -070039}
40
41} // namespace mist