Ben Chan | 4f6444a | 2013-05-29 13:21:37 -0700 | [diff] [blame] | 1 | // 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 Chan | 956f79b | 2014-08-06 17:11:01 -0700 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Ben Chan | d88dc42 | 2016-08-27 20:56:18 -0700 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <vector> |
| 12 | |
Ben Chan | 4746c8a | 2014-09-02 20:34:58 -0700 | [diff] [blame] | 13 | #include <base/macros.h> |
Hidehiko Abe | be95e98 | 2019-09-19 02:17:03 +0900 | [diff] [blame] | 14 | #include <base/memory/weak_ptr.h> |
Ben Chan | 4f6444a | 2013-05-29 13:21:37 -0700 | [diff] [blame] | 15 | |
| 16 | #include "mist/usb_error.h" |
| 17 | |
| 18 | struct libusb_context; |
| 19 | |
| 20 | namespace mist { |
| 21 | |
Ben Chan | 3cb21b8 | 2013-05-29 17:01:16 -0700 | [diff] [blame] | 22 | class EventDispatcher; |
Ben Chan | 4f6444a | 2013-05-29 13:21:37 -0700 | [diff] [blame] | 23 | class UsbDevice; |
| 24 | |
| 25 | // A USB manager for managing a USB session created by libusb 1.0. |
Hidehiko Abe | be95e98 | 2019-09-19 02:17:03 +0900 | [diff] [blame] | 26 | class UsbManager { |
Ben Chan | 4f6444a | 2013-05-29 13:21:37 -0700 | [diff] [blame] | 27 | public: |
Ben Chan | 3cb21b8 | 2013-05-29 17:01:16 -0700 | [diff] [blame] | 28 | // 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 Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 32 | UsbManager(const UsbManager&) = delete; |
| 33 | UsbManager& operator=(const UsbManager&) = delete; |
Ben Chan | 3cb21b8 | 2013-05-29 17:01:16 -0700 | [diff] [blame] | 34 | |
Hidehiko Abe | be95e98 | 2019-09-19 02:17:03 +0900 | [diff] [blame] | 35 | ~UsbManager(); |
Ben Chan | 4f6444a | 2013-05-29 13:21:37 -0700 | [diff] [blame] | 36 | |
| 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 Chan | 875e01a | 2013-06-13 14:16:05 -0700 | [diff] [blame] | 43 | // 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 Chan | 64b60f6 | 2019-06-06 16:57:30 -0700 | [diff] [blame] | 48 | 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 Chan | 875e01a | 2013-06-13 14:16:05 -0700 | [diff] [blame] | 52 | |
Ben Chan | 4f6444a | 2013-05-29 13:21:37 -0700 | [diff] [blame] | 53 | // 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 Chan | d88dc42 | 2016-08-27 20:56:18 -0700 | [diff] [blame] | 57 | bool GetDevices(std::vector<std::unique_ptr<UsbDevice>>* devices); |
Ben Chan | 4f6444a | 2013-05-29 13:21:37 -0700 | [diff] [blame] | 58 | |
| 59 | const UsbError& error() const { return error_; } |
| 60 | |
| 61 | private: |
Ben Chan | 3cb21b8 | 2013-05-29 17:01:16 -0700 | [diff] [blame] | 62 | 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 Chan | 3cb21b8 | 2013-05-29 17:01:16 -0700 | [diff] [blame] | 74 | EventDispatcher* const dispatcher_; |
Ben Chan | 4f6444a | 2013-05-29 13:21:37 -0700 | [diff] [blame] | 75 | libusb_context* context_; |
| 76 | UsbError error_; |
| 77 | |
Hidehiko Abe | be95e98 | 2019-09-19 02:17:03 +0900 | [diff] [blame] | 78 | base::WeakPtrFactory<UsbManager> weak_factory_{this}; |
Ben Chan | 4f6444a | 2013-05-29 13:21:37 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | } // namespace mist |
| 82 | |
| 83 | #endif // MIST_USB_MANAGER_H_ |