blob: 92ca7f67c200f4df3b2e92048d8ee211b1619c7a [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
Qijiang Fan713061e2021-03-08 15:45:12 +09009#include <base/check.h>
Ben Chanf4930e52013-05-28 13:24:54 -070010#include <base/logging.h>
Ben Chandcc0c7a2014-02-05 18:03:39 -080011#include <base/strings/stringprintf.h>
Ben Chanf4930e52013-05-28 13:24:54 -070012
13#include "mist/usb_device.h"
14#include "mist/usb_interface_descriptor.h"
15
Ben Chanf4930e52013-05-28 13:24:54 -070016namespace mist {
17
18UsbInterface::UsbInterface(const base::WeakPtr<UsbDevice>& device,
19 const libusb_interface* interface)
Ben Chanc440db52017-09-26 06:30:47 -070020 : device_(device), interface_(interface) {
Ben Chanf4930e52013-05-28 13:24:54 -070021 CHECK(interface_);
22}
23
Ben Chanf4930e52013-05-28 13:24:54 -070024int UsbInterface::GetNumAlternateSettings() const {
25 return interface_->num_altsetting;
26}
27
Ben Chan64b60f62019-06-06 16:57:30 -070028std::unique_ptr<UsbInterfaceDescriptor> UsbInterface::GetAlternateSetting(
29 int index) const {
Ben Chanf4930e52013-05-28 13:24:54 -070030 if (index < 0 || index >= GetNumAlternateSettings()) {
Ben Chan9870f8b2019-06-06 17:31:35 -070031 LOG(ERROR) << base::StringPrintf(
Ben Chanc440db52017-09-26 06:30:47 -070032 "Invalid alternate setting index %d. "
33 "Must be non-negative and less than %d.",
34 index, GetNumAlternateSettings());
Ben Chan61ac5ba2014-08-29 18:06:43 -070035 return nullptr;
Ben Chanf4930e52013-05-28 13:24:54 -070036 }
37
Ben Chan64b60f62019-06-06 16:57:30 -070038 return std::make_unique<UsbInterfaceDescriptor>(
39 device_, &interface_->altsetting[index]);
Ben Chanf4930e52013-05-28 13:24:54 -070040}
41
42} // namespace mist