Karl Petersson | 006c524 | 2017-05-30 17:24:10 +0200 | [diff] [blame] | 1 | // Copyright 2017 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 UDEV_DEVICE_MANAGER_H_ |
| 6 | #define UDEV_DEVICE_MANAGER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
Qijiang Fan | 253d949 | 2020-05-15 16:59:06 +0900 | [diff] [blame] | 10 | #include <base/files/file_descriptor_watcher_posix.h> |
Simon Que | 879e60a | 2017-06-22 20:33:00 -0400 | [diff] [blame] | 11 | #include <base/macros.h> |
Karl Petersson | 006c524 | 2017-05-30 17:24:10 +0200 | [diff] [blame] | 12 | #include <base/observer_list.h> |
| 13 | |
| 14 | #include "scoped_udev_handle.h" |
| 15 | #include "udev_subsystem_observer.h" |
| 16 | |
| 17 | namespace atrusctl { |
| 18 | |
Qijiang Fan | 253d949 | 2020-05-15 16:59:06 +0900 | [diff] [blame] | 19 | class UdevDeviceManager { |
Karl Petersson | 006c524 | 2017-05-30 17:24:10 +0200 | [diff] [blame] | 20 | public: |
| 21 | UdevDeviceManager(); |
| 22 | |
| 23 | // Initialize udev monitoring and start listening on udev socket |
| 24 | bool Initialize(); |
| 25 | |
| 26 | // Add listener for hidraw events that matches rule in "udev-atrus.rules" |
| 27 | void AddObserver(UdevSubsystemObserver* observer); |
| 28 | |
| 29 | // Remove listener |
| 30 | void RemoveObserver(UdevSubsystemObserver* observer); |
| 31 | |
| 32 | // Enumerate hidraw devices that matches |kUsbVid| and |kUSbPid| declared in |
| 33 | // atrus_device.h, call each observer's callback if a device was found |
| 34 | bool Enumerate(); |
| 35 | |
| 36 | private: |
Qijiang Fan | 253d949 | 2020-05-15 16:59:06 +0900 | [diff] [blame] | 37 | void OnFdReadable(); |
Karl Petersson | 006c524 | 2017-05-30 17:24:10 +0200 | [diff] [blame] | 38 | |
| 39 | void HandleEvent(const std::string& action_str, |
| 40 | const std::string& device_path); |
| 41 | |
| 42 | ScopedUdev udev_; |
| 43 | ScopedUdevMonitor monitor_; |
Qijiang Fan | 253d949 | 2020-05-15 16:59:06 +0900 | [diff] [blame] | 44 | std::unique_ptr<base::FileDescriptorWatcher::Controller> watcher_; |
Karl Petersson | 006c524 | 2017-05-30 17:24:10 +0200 | [diff] [blame] | 45 | base::ObserverList<UdevSubsystemObserver> observers_; |
Simon Que | 879e60a | 2017-06-22 20:33:00 -0400 | [diff] [blame] | 46 | |
| 47 | DISALLOW_COPY_AND_ASSIGN(UdevDeviceManager); |
Karl Petersson | 006c524 | 2017-05-30 17:24:10 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | } // namespace atrusctl |
| 51 | |
| 52 | #endif // UDEV_DEVICE_MANAGER_H_ |