OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium 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 SERVICES_DEVICE_GENERIC_SENSOR_LINUX_SENSOR_DEVICE_MANAGER_H_ | |
6 #define SERVICES_DEVICE_GENERIC_SENSOR_LINUX_SENSOR_DEVICE_MANAGER_H_ | |
7 | |
8 #include "base/scoped_observer.h" | |
9 | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "device/base/device_monitor_linux.h" | |
12 #include "services/device/public/interfaces/sensor.mojom.h" | |
13 | |
14 namespace device { | |
15 | |
16 struct SensorInfoLinux; | |
17 | |
18 // This SensorDeviceManager uses LinuxDeviceMonitor to enumerate devices | |
19 // and listen to "add/removed" events to notify |provider_| about | |
20 // added or removed iio devices. It has own cache to speed up an identification | |
21 // process of removed devices. | |
22 class SensorDeviceManager : public DeviceMonitorLinux::Observer { | |
23 public: | |
24 class Delegate { | |
25 public: | |
26 // Called when SensorDeviceManager has enumerated through all possible | |
27 // iio udev devices. | |
28 virtual void OnSensorNodesEnumerated() = 0; | |
29 | |
30 // Called after SensorDeviceManager has identified a udev device, which | |
31 // belongs to "iio" subsystem. | |
32 virtual void OnDeviceAdded(mojom::SensorType type, | |
33 std::unique_ptr<SensorInfoLinux> sensor) = 0; | |
34 | |
35 // Called after "removed" event is received from LinuxDeviceMonitor and | |
36 // sensor is identified as known. | |
37 virtual void OnDeviceRemoved(mojom::SensorType type, | |
38 const std::string& device_node) = 0; | |
39 | |
40 protected: | |
41 virtual ~Delegate() {} | |
42 }; | |
43 | |
44 SensorDeviceManager(); | |
45 ~SensorDeviceManager() override; | |
46 | |
47 // Starts this service. | |
48 virtual void Start(Delegate* delegate); | |
49 | |
50 protected: | |
51 using SensorDeviceMap = std::unordered_map<std::string, mojom::SensorType>; | |
52 | |
53 // Wrappers around udev system methods that can be implemented differently | |
54 // by tests. | |
55 virtual std::string GetUdevDeviceGetSubsystem(udev_device* dev); | |
56 virtual std::string GetUdevDeviceGetSyspath(udev_device* dev); | |
57 virtual std::string GetUdevDeviceGetSysattrValue( | |
58 udev_device* dev, | |
59 const std::string& attribute); | |
60 virtual std::string GetUdevDeviceGetDevnode(udev_device* dev); | |
61 | |
62 // DeviceMonitorLinux::Observer: | |
63 void OnDeviceAdded(udev_device* udev_device) override; | |
64 void OnDeviceRemoved(udev_device* device) override; | |
65 | |
66 // Represents a map of sensors that are already known to this manager after | |
67 // initial enumeration. | |
68 SensorDeviceMap sensors_by_node_; | |
69 | |
70 THREAD_CHECKER(thread_checker_); | |
71 | |
72 ScopedObserver<DeviceMonitorLinux, DeviceMonitorLinux::Observer> observer_; | |
73 | |
74 Delegate* delegate_; | |
75 | |
76 // A task runner, which |delegate_| lives on. | |
77 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(SensorDeviceManager); | |
80 }; | |
81 | |
82 } // namespace device | |
83 | |
84 #endif // SERVICES_DEVICE_GENERIC_SENSOR_LINUX_SENSOR_DEVICE_MANAGER_H_ | |
OLD | NEW |