Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 1 | // Copyright 2018 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 PERMISSION_BROKER_USB_CONTROL_H_ |
| 6 | #define PERMISSION_BROKER_USB_CONTROL_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <memory> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <libusb-1.0/libusb.h> |
| 14 | |
| 15 | #include <base/macros.h> |
| 16 | #include <base/time/time.h> |
| 17 | #include <brillo/dbus/dbus_object.h> |
| 18 | |
| 19 | #include "permission_broker/libusb_wrapper.h" |
| 20 | |
| 21 | namespace permission_broker { |
| 22 | |
| 23 | // This class encapsulates the logic used to interact with the VBUS subsystem to |
| 24 | // control the power state of USB devices. |
| 25 | class UsbControl { |
| 26 | public: |
| 27 | explicit UsbControl(std::unique_ptr<UsbDeviceManagerInterface> manager); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 28 | UsbControl(const UsbControl&) = delete; |
| 29 | UsbControl& operator=(const UsbControl&) = delete; |
| 30 | |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 31 | ~UsbControl(); |
| 32 | |
| 33 | // Based on |vid| and |pid| of a USB device, this function determines if the |
Garrick Evans | d48463e | 2020-06-22 14:09:10 +0900 | [diff] [blame] | 34 | // device type can be controlled by the API. The UsbControl implements an |
| 35 | // allow/deny mechanism, meaning that if a device is *not* allowed, it |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 36 | // cannot be controlled. |
Garrick Evans | d48463e | 2020-06-22 14:09:10 +0900 | [diff] [blame] | 37 | bool IsDeviceAllowed(uint16_t vid, uint16_t pid) const; |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 38 | // When called, this function will find all the USB devices identified by |
| 39 | // |vid| and |pid| and will try to power-cycle them using the VBUS subsystem. |
| 40 | // The |delay| determines the delay between powering all the devices found on |
| 41 | // and powering them all off. |
Tom Hughes | 4f300e5 | 2020-08-24 18:08:59 -0700 | [diff] [blame] | 42 | void PowerCycleUsbPorts(base::Callback<void(bool)> callback, |
| 43 | uint16_t vid, |
| 44 | uint16_t pid, |
| 45 | base::TimeDelta delay); |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | std::unique_ptr<UsbDeviceManagerInterface> manager_ = nullptr; |
Armando Miraglia | cd70cfa | 2018-06-04 15:24:09 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | } // namespace permission_broker |
| 52 | |
| 53 | #endif // PERMISSION_BROKER_USB_CONTROL_H_ |