blob: ae87c3516474be45481dd2d25adc47d997653aef [file] [log] [blame]
Ben Chan4f6444a2013-05-29 13:21:37 -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#ifndef MIST_USB_MANAGER_H_
6#define MIST_USB_MANAGER_H_
7
Ben Chan956f79b2014-08-06 17:11:01 -07008#include <stdint.h>
9
Ben Chand88dc422016-08-27 20:56:18 -070010#include <memory>
11#include <vector>
12
Ben Chan4746c8a2014-09-02 20:34:58 -070013#include <base/macros.h>
Hidehiko Abebe95e982019-09-19 02:17:03 +090014#include <base/memory/weak_ptr.h>
Ben Chan4f6444a2013-05-29 13:21:37 -070015
16#include "mist/usb_error.h"
17
18struct libusb_context;
19
20namespace mist {
21
Ben Chan3cb21b82013-05-29 17:01:16 -070022class EventDispatcher;
Ben Chan4f6444a2013-05-29 13:21:37 -070023class UsbDevice;
24
25// A USB manager for managing a USB session created by libusb 1.0.
Hidehiko Abebe95e982019-09-19 02:17:03 +090026class UsbManager {
Ben Chan4f6444a2013-05-29 13:21:37 -070027 public:
Ben Chan3cb21b82013-05-29 17:01:16 -070028 // Constructs a UsbManager object by taking a raw pointer to an
29 // EventDispatcher as |dispatcher|. The ownership of |dispatcher| is not
30 // transferred, and thus it should outlive this object.
31 explicit UsbManager(EventDispatcher* dispatcher);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090032 UsbManager(const UsbManager&) = delete;
33 UsbManager& operator=(const UsbManager&) = delete;
Ben Chan3cb21b82013-05-29 17:01:16 -070034
Hidehiko Abebe95e982019-09-19 02:17:03 +090035 ~UsbManager();
Ben Chan4f6444a2013-05-29 13:21:37 -070036
37 // Initializes a USB session via libusb. Returns true on success.
38 bool Initialize();
39
40 // Sets the debug level of libusb to |level|.
41 void SetDebugLevel(int level);
42
Ben Chan875e01a2013-06-13 14:16:05 -070043 // Gets the USB device that is currently connected to the bus numbered
44 // |bus_number|, at the address |device_address| on the bus, with its vendor
45 // ID equal to |vendor_id|, and its product ID equal to |product_id|. Returns
46 // NULL if no such device is found. The returned UsbDevice object becomes
47 // invalid, and thus should not be held, beyond the lifetime of this object.
Ben Chan64b60f62019-06-06 16:57:30 -070048 std::unique_ptr<UsbDevice> GetDevice(uint8_t bus_number,
49 uint8_t device_address,
50 uint16_t vendor_id,
51 uint16_t product_id);
Ben Chan875e01a2013-06-13 14:16:05 -070052
Ben Chan4f6444a2013-05-29 13:21:37 -070053 // Gets the list of USB devices currently attached to the system. Returns true
54 // on success. |devices| is always cleared before being updated. The returned
55 // UsbDevice objects become invalid, and thus should not be held, beyond the
56 // lifetime of this object.
Ben Chand88dc422016-08-27 20:56:18 -070057 bool GetDevices(std::vector<std::unique_ptr<UsbDevice>>* devices);
Ben Chan4f6444a2013-05-29 13:21:37 -070058
59 const UsbError& error() const { return error_; }
60
61 private:
Ben Chan3cb21b82013-05-29 17:01:16 -070062 static void OnPollFileDescriptorAdded(int file_descriptor,
63 short events, // NOLINT
64 void* user_data);
65 static void OnPollFileDescriptorRemoved(int file_descriptor, void* user_data);
66
67 // Starts watching the file descriptors for libusb events. Returns true on
68 // success.
69 bool StartWatchingPollFileDescriptors();
70
71 // Handles libusb events in non-blocking mode.
72 void HandleEventsNonBlocking();
73
Ben Chan3cb21b82013-05-29 17:01:16 -070074 EventDispatcher* const dispatcher_;
Ben Chan4f6444a2013-05-29 13:21:37 -070075 libusb_context* context_;
76 UsbError error_;
77
Hidehiko Abebe95e982019-09-19 02:17:03 +090078 base::WeakPtrFactory<UsbManager> weak_factory_{this};
Ben Chan4f6444a2013-05-29 13:21:37 -070079};
80
81} // namespace mist
82
83#endif // MIST_USB_MANAGER_H_