Ben Chan | f4930e5 | 2013-05-28 13:24:54 -0700 | [diff] [blame] | 1 | // 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 Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 9 | #include <base/check.h> |
Ben Chan | f4930e5 | 2013-05-28 13:24:54 -0700 | [diff] [blame] | 10 | #include <base/logging.h> |
Ben Chan | dcc0c7a | 2014-02-05 18:03:39 -0800 | [diff] [blame] | 11 | #include <base/strings/stringprintf.h> |
Ben Chan | f4930e5 | 2013-05-28 13:24:54 -0700 | [diff] [blame] | 12 | |
| 13 | #include "mist/usb_device.h" |
| 14 | #include "mist/usb_interface_descriptor.h" |
| 15 | |
Ben Chan | f4930e5 | 2013-05-28 13:24:54 -0700 | [diff] [blame] | 16 | namespace mist { |
| 17 | |
| 18 | UsbInterface::UsbInterface(const base::WeakPtr<UsbDevice>& device, |
| 19 | const libusb_interface* interface) |
Ben Chan | c440db5 | 2017-09-26 06:30:47 -0700 | [diff] [blame] | 20 | : device_(device), interface_(interface) { |
Ben Chan | f4930e5 | 2013-05-28 13:24:54 -0700 | [diff] [blame] | 21 | CHECK(interface_); |
| 22 | } |
| 23 | |
Ben Chan | f4930e5 | 2013-05-28 13:24:54 -0700 | [diff] [blame] | 24 | int UsbInterface::GetNumAlternateSettings() const { |
| 25 | return interface_->num_altsetting; |
| 26 | } |
| 27 | |
Ben Chan | 64b60f6 | 2019-06-06 16:57:30 -0700 | [diff] [blame] | 28 | std::unique_ptr<UsbInterfaceDescriptor> UsbInterface::GetAlternateSetting( |
| 29 | int index) const { |
Ben Chan | f4930e5 | 2013-05-28 13:24:54 -0700 | [diff] [blame] | 30 | if (index < 0 || index >= GetNumAlternateSettings()) { |
Ben Chan | 9870f8b | 2019-06-06 17:31:35 -0700 | [diff] [blame] | 31 | LOG(ERROR) << base::StringPrintf( |
Ben Chan | c440db5 | 2017-09-26 06:30:47 -0700 | [diff] [blame] | 32 | "Invalid alternate setting index %d. " |
| 33 | "Must be non-negative and less than %d.", |
| 34 | index, GetNumAlternateSettings()); |
Ben Chan | 61ac5ba | 2014-08-29 18:06:43 -0700 | [diff] [blame] | 35 | return nullptr; |
Ben Chan | f4930e5 | 2013-05-28 13:24:54 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Ben Chan | 64b60f6 | 2019-06-06 16:57:30 -0700 | [diff] [blame] | 38 | return std::make_unique<UsbInterfaceDescriptor>( |
| 39 | device_, &interface_->altsetting[index]); |
Ben Chan | f4930e5 | 2013-05-28 13:24:54 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | } // namespace mist |