blob: ab63939905ae041badf735c9eddefe6e3d3323db [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);
91 ~UsbDevice() override;
92
93 UsbDeviceInfo GetInfo() const override;
94 std::unique_ptr<UsbDeviceInterface> GetParent() const override;
95 uint8_t GetPort() const override;
96
97 bool SetPowerState(bool enabled, uint16_t port) const override;
98
99 private:
100 std::unique_ptr<libusb_device, void (*)(libusb_device*)> device_;
101 UsbDeviceInfo info_;
102
103 DISALLOW_COPY_AND_ASSIGN(UsbDevice);
104};
105
106// Manager intended to provide an API to interact with multiple USB devices. The
107// main purpose of the class is to provide search mechanisms over the list of
108// connected devices.
109// This interface is intended as a generalization of that for testing purposes.
110class UsbDeviceManagerInterface {
111 public:
112 virtual ~UsbDeviceManagerInterface() = default;
113
114 virtual std::vector<std::unique_ptr<UsbDeviceInterface>> GetDevicesByVidPid(
Tom Hughes4f300e52020-08-24 18:08:59 -0700115 uint16_t vid, uint16_t pid) = 0;
Armando Miragliacd70cfa2018-06-04 15:24:09 +0200116};
117
118// Specialized implementation of UsbDeviceManagerInterface that uses libusb
119// to retrieve and filter USB devices connected to CrOS.
120class UsbDeviceManager : public UsbDeviceManagerInterface {
121 public:
122 UsbDeviceManager();
123 ~UsbDeviceManager() override;
124
125 std::vector<std::unique_ptr<UsbDeviceInterface>> GetDevicesByVidPid(
Tom Hughes4f300e52020-08-24 18:08:59 -0700126 uint16_t vid, uint16_t pid) override;
Armando Miragliacd70cfa2018-06-04 15:24:09 +0200127
128 private:
129 std::unique_ptr<libusb_context, LibusbContextDeleter> context_;
130
131 DISALLOW_COPY_AND_ASSIGN(UsbDeviceManager);
132};
133
134} // namespace permission_broker
135
136#endif // PERMISSION_BROKER_LIBUSB_WRAPPER_H_