blob: a4962901ec0c7adfeb6a1e4925ab8fb97115724a [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);
32
Hidehiko Abebe95e982019-09-19 02:17:03 +090033 ~UsbManager();
Ben Chan4f6444a2013-05-29 13:21:37 -070034
35 // Initializes a USB session via libusb. Returns true on success.
36 bool Initialize();
37
38 // Sets the debug level of libusb to |level|.
39 void SetDebugLevel(int level);
40
Ben Chan875e01a2013-06-13 14:16:05 -070041 // Gets the USB device that is currently connected to the bus numbered
42 // |bus_number|, at the address |device_address| on the bus, with its vendor
43 // ID equal to |vendor_id|, and its product ID equal to |product_id|. Returns
44 // NULL if no such device is found. The returned UsbDevice object becomes
45 // invalid, and thus should not be held, beyond the lifetime of this object.
Ben Chan64b60f62019-06-06 16:57:30 -070046 std::unique_ptr<UsbDevice> GetDevice(uint8_t bus_number,
47 uint8_t device_address,
48 uint16_t vendor_id,
49 uint16_t product_id);
Ben Chan875e01a2013-06-13 14:16:05 -070050
Ben Chan4f6444a2013-05-29 13:21:37 -070051 // Gets the list of USB devices currently attached to the system. Returns true
52 // on success. |devices| is always cleared before being updated. The returned
53 // UsbDevice objects become invalid, and thus should not be held, beyond the
54 // lifetime of this object.
Ben Chand88dc422016-08-27 20:56:18 -070055 bool GetDevices(std::vector<std::unique_ptr<UsbDevice>>* devices);
Ben Chan4f6444a2013-05-29 13:21:37 -070056
57 const UsbError& error() const { return error_; }
58
59 private:
Ben Chan3cb21b82013-05-29 17:01:16 -070060 static void OnPollFileDescriptorAdded(int file_descriptor,
61 short events, // NOLINT
62 void* user_data);
63 static void OnPollFileDescriptorRemoved(int file_descriptor, void* user_data);
64
65 // Starts watching the file descriptors for libusb events. Returns true on
66 // success.
67 bool StartWatchingPollFileDescriptors();
68
69 // Handles libusb events in non-blocking mode.
70 void HandleEventsNonBlocking();
71
Ben Chan3cb21b82013-05-29 17:01:16 -070072 EventDispatcher* const dispatcher_;
Ben Chan4f6444a2013-05-29 13:21:37 -070073 libusb_context* context_;
74 UsbError error_;
75
Hidehiko Abebe95e982019-09-19 02:17:03 +090076 base::WeakPtrFactory<UsbManager> weak_factory_{this};
77
Ben Chan4f6444a2013-05-29 13:21:37 -070078 DISALLOW_COPY_AND_ASSIGN(UsbManager);
79};
80
81} // namespace mist
82
83#endif // MIST_USB_MANAGER_H_