blob: 79c7fa46565c5a1cc3cda3a845813f9af162d3c5 [file] [log] [blame]
Armando Miragliacd70cfa2018-06-04 15:24:09 +02001// 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_LIBUSB_WRAPPER_H_
6#define PERMISSION_BROKER_LIBUSB_WRAPPER_H_
7
8#include <iostream>
9#include <memory>
10#include <vector>
11
12#include <libusb-1.0/libusb.h>
13#include <linux/usb/ch11.h>
14
15#include <base/macros.h>
16#include <base/strings/stringprintf.h>
17
18namespace permission_broker {
19
20// Container of USB device information. The class encapsulates device
21// information like Vendor ID and Product ID and abstracts the way these
22// information are extracted from libusb.
23struct UsbDeviceInfo {
24 uint16_t vid;
25 uint16_t pid;
26 uint8_t device_class;
27
28 UsbDeviceInfo() : vid(0), pid(0), device_class(LIBUSB_CLASS_VENDOR_SPEC) {}
29 // Convenience overloaded constructor used when VID and PID are known at
30 // instantiation time.
31 UsbDeviceInfo(uint16_t vid, uint16_t pid)
32 : vid(vid), pid(pid), device_class(LIBUSB_CLASS_VENDOR_SPEC) {}
33 // Convenience overloaded constructor used when VID, PID, and device class are
34 // known at instantiation time.
35 UsbDeviceInfo(uint16_t vid, uint16_t pid, uint8_t device_class)
36 : vid(vid), pid(pid), device_class(device_class) {}
37
38 // We ignore the device class for comparison; the comparison is only based on
39 // VID and PID.
40 bool operator==(const UsbDeviceInfo& object) const {
41 return object.vid == vid && object.pid == pid;
42 }
43
44 friend std::ostream& operator<<(std::ostream& os,
45 const UsbDeviceInfo& object) {
46 auto description =
47 base::StringPrintf("vid: 0x%04x, pid: 0x%04x, class: 0x%02x",
48 object.vid, object.pid, object.device_class);
49 os << description;
50 return os;
51 }
52};
53
54// Convenience deleter object used to manage the libusb context object. Such
55// contexts require to be initialized via libusb_init() and destroyed via
56// libusb_exit(). To make the lifetime more apparent we use the deleter to
57// combine the raw pointer with smart pointers.
58struct LibusbContextDeleter {
59 void operator()(libusb_context* ctx) { libusb_exit(ctx); }
60};
61
62// Interface used to abstract the interaction with the libusb provided API. This
63// API defines the required functions needed by UsbControl to be able to
64// interact with the USB subsystem.
65// Such abstraction is also meant to simplify testing.
66class UsbDeviceInterface {
67 public:
68 virtual ~UsbDeviceInterface() = default;
69
70 virtual UsbDeviceInfo GetInfo() const = 0;
71 virtual uint8_t GetPort() const = 0;
72 // This function implementation can return nullptr to indicate that it was not
73 // possible to obtain a parent device for the represented USB device. This can
74 // also be the case when, for example, the parent device is **not** a hub.
75 virtual std::unique_ptr<UsbDeviceInterface> GetParent() const = 0;
76
77 // Sets the power state to on/off depending on |enabled| of a specified port.
78 // This API can only be used on HUB devices.
79 virtual bool SetPowerState(bool enabled, uint16_t port) const = 0;
80};
81
82// Specialization of the UsbDeviceInterface that uses the libusb API to
83// communicate with the USB peripherals.
84class UsbDevice : public UsbDeviceInterface {
85 public:
86 // When passing a libusb_device pointer to this constructor, the ownership of
87 // pointer is assigned to the new instance of UsbDevice.
88 // In other words, UsbDevice will take care of ref up and ref down the object
89 // respectively at construction and at destructiopn.
90 explicit UsbDevice(libusb_device* device);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090091 UsbDevice(const UsbDevice&) = delete;
92 UsbDevice& operator=(const UsbDevice&) = delete;
93
Armando Miragliacd70cfa2018-06-04 15:24:09 +020094 ~UsbDevice() override;
95
96 UsbDeviceInfo GetInfo() const override;
97 std::unique_ptr<UsbDeviceInterface> GetParent() const override;
98 uint8_t GetPort() const override;
99
100 bool SetPowerState(bool enabled, uint16_t port) const override;
101
102 private:
103 std::unique_ptr<libusb_device, void (*)(libusb_device*)> device_;
104 UsbDeviceInfo info_;
Armando Miragliacd70cfa2018-06-04 15:24:09 +0200105};
106
107// Manager intended to provide an API to interact with multiple USB devices. The
108// main purpose of the class is to provide search mechanisms over the list of
109// connected devices.
110// This interface is intended as a generalization of that for testing purposes.
111class UsbDeviceManagerInterface {
112 public:
113 virtual ~UsbDeviceManagerInterface() = default;
114
115 virtual std::vector<std::unique_ptr<UsbDeviceInterface>> GetDevicesByVidPid(
Tom Hughes4f300e52020-08-24 18:08:59 -0700116 uint16_t vid, uint16_t pid) = 0;
Armando Miragliacd70cfa2018-06-04 15:24:09 +0200117};
118
119// Specialized implementation of UsbDeviceManagerInterface that uses libusb
120// to retrieve and filter USB devices connected to CrOS.
121class UsbDeviceManager : public UsbDeviceManagerInterface {
122 public:
123 UsbDeviceManager();
Qijiang Fan6bc59e12020-11-11 02:51:06 +0900124 UsbDeviceManager(const UsbDeviceManager&) = delete;
125 UsbDeviceManager& operator=(const UsbDeviceManager&) = delete;
126
Armando Miragliacd70cfa2018-06-04 15:24:09 +0200127 ~UsbDeviceManager() override;
128
129 std::vector<std::unique_ptr<UsbDeviceInterface>> GetDevicesByVidPid(
Tom Hughes4f300e52020-08-24 18:08:59 -0700130 uint16_t vid, uint16_t pid) override;
Armando Miragliacd70cfa2018-06-04 15:24:09 +0200131
132 private:
133 std::unique_ptr<libusb_context, LibusbContextDeleter> context_;
Armando Miragliacd70cfa2018-06-04 15:24:09 +0200134};
135
136} // namespace permission_broker
137
138#endif // PERMISSION_BROKER_LIBUSB_WRAPPER_H_