Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: device/generic_sensor/platform_sensor_manager_linux.h

Issue 2533793002: [sensors](CrOS/Linux) Implement Sensor device manager for sensors (Closed)
Patch Set: fix build.gn Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_MANAGER_LINUX_H_
6 #define DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_MANAGER_LINUX_H_
7
8 #include "base/scoped_observer.h"
9
10 #include "device/base/device_monitor_linux.h"
11 #include "device/generic_sensor/linux/sensor_data_linux.h"
12 #include "device/generic_sensor/public/interfaces/sensor.mojom.h"
13
14 namespace device {
15
16 class SensorDeviceService;
17
18 // This structure represents an iio device, which info is taken
19 // from udev service. If a client requests a sensor from a provider,
20 // the last one takes this structure and uses it to create a requested platform
21 // sensor of a certain type.
22 struct SensorDeviceLinux {
23 // Represents current sensor device node.
24 const std::string device_node;
25 // Represents frequency of a sensor.
26 const double device_frequency;
27 // Represents scaling value to be applied on raw data.
28 const double device_scaling_value;
29 // Represents offset value that must be appled on raw data.
30 const double device_offset_value;
31 // Reporting mode of a sensor taken from SensorDataLinux.
32 const mojom::ReportingMode reporting_mode;
33 // Functor that is used to convert raw data.
34 const SensorDataLinux::ReaderFunctor apply_scaling_func;
35 // Sensor files in sysfs. Used to poll data.
36 const std::vector<base::FilePath> device_reading_files;
37
38 SensorDeviceLinux(const std::string& sensor_device_node,
39 double sensor_device_frequency,
40 double sensor_device_scaling_value,
41 double sensor_device_offset_value,
42 mojom::ReportingMode mode,
43 SensorDataLinux::ReaderFunctor scaling_func,
44 std::vector<base::FilePath> iio_device_reading_files);
45 ~SensorDeviceLinux();
46 };
47
48 class DEVICE_GENERIC_SENSOR_EXPORT SensorDeviceManager {
49 public:
50 // Callback that is used to get a sensor.
51 using GetSensorDeviceCallback = base::Callback<void(SensorDeviceLinux*)>;
52 using OnDeviceAddedCallback =
53 base::Callback<void(mojom::SensorType,
54 std::unique_ptr<SensorDeviceLinux>)>;
55 using OnDeviceRemovedCallback =
56 base::Callback<void(mojom::SensorType, const std::string&)>;
57
58 SensorDeviceManager();
59 ~SensorDeviceManager();
60
61 // Shutdowns a service that tracks events from iio subsystem.
62 void Shutdown();
63
64 // Returns SensorDeviceLinux structure of a requested type.
65 // If a request cannot be processed immediately, stores the |callback|
66 // and processes it once manager is ready to provide such an info.
67 void GetSensorDevice(mojom::SensorType type,
68 const GetSensorDeviceCallback& callback);
69
70 // Returns found iio devices. Currently not implemented.
71 void GetAllSensorDevices(const GetSensorDeviceCallback& callback);
72
73 // A blocking task runner that is used by a sensor device service.
74 void SetBlockingTaskRunner(
75 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
76
77 // Sets another service provided by tests.
78 void SetSensorDeviceServiceForTesting(
79 std::unique_ptr<SensorDeviceService> service);
80
81 // Called by SensorDeviceService when enumeration is ready.
82 void SetEnumerationReady();
83
84 private:
85 using SensorDeviceMap =
86 std::unordered_map<mojom::SensorType, std::unique_ptr<SensorDeviceLinux>>;
87
88 using CallbackToTypeMap =
89 std::unordered_map<mojom::SensorType, GetSensorDeviceCallback>;
90
91 // Initializes a sensor device service.
92 bool InitializeService();
93
94 // Sets service has been started.
95 void SetServiceStarted();
96
97 // Called by SensorDeviceService when a sensor device is found/attached.
98 void OnDeviceAdded(mojom::SensorType type,
99 std::unique_ptr<SensorDeviceLinux> sensor_device);
100
101 // Called by SensorDeviceService when a sensor device is removed/detached.
102 void OnDeviceRemoved(mojom::SensorType type, const std::string& device_node);
103
104 // Triggers all stored callbacks when enumeration is ready.
105 void TriggerCallbacks();
106
107 bool enumeration_ready() { return enumeration_ready_; }
108
109 // Stores all available sensor devices by type.
110 SensorDeviceMap sensor_device_map_;
111
112 // Stores request callbacks from clients by type.
113 CallbackToTypeMap get_device_callbacks_;
114
115 // Works as a service, which enumerates all available sensor devices on
116 // start and sends attach/detach events to this SensorDeviceManager.
117 std::unique_ptr<SensorDeviceService> sensor_device_service_;
118
119 // Used by a sensor device service for blocking udev tasks.
120 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
121
122 // Current task runner, which this manager belongs to.
123 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
124
125 // Tells if enumeration is ready.
126 bool enumeration_ready_;
127
128 // Tells if service has been started.
129 bool sensor_device_service_started_;
130
131 base::WeakPtrFactory<SensorDeviceManager> weak_factory_;
132
133 DISALLOW_COPY_AND_ASSIGN(SensorDeviceManager);
134 };
135
136 // A sensor device service that uses linux device monitor to enumerate devices
137 // and listens to events sent from linux device monitor, which receives them
138 // from udev, and notifies |manager_| about those sensors devices.
139 class DEVICE_GENERIC_SENSOR_EXPORT SensorDeviceService
140 : public DeviceMonitorLinux::Observer {
141 public:
142 SensorDeviceService(scoped_refptr<base::SingleThreadTaskRunner> task_runner);
143 ~SensorDeviceService() override;
144
145 // Starts this service.
146 virtual void Start(
147 base::WeakPtr<SensorDeviceManager> manager,
148 SensorDeviceManager::OnDeviceAddedCallback device_added_callback,
149 SensorDeviceManager::OnDeviceRemovedCallback device_removed_callback);
150
151 protected:
152 // Wrappers around udev system methods that can be implemented differently
153 // by tests.
154 virtual std::string UdevDeviceGetSubsystem(udev_device* dev);
155 virtual std::string UdevDeviceGetSyspath(udev_device* dev);
156 virtual std::string UdevDeviceGetSysattrValue(udev_device* dev,
157 const std::string& attribute);
158 virtual std::string UdevDeviceGetDevnode(udev_device* dev);
159
160 // DeviceMonitorLinux::Observer:
161 void OnDeviceAdded(udev_device* udev_device) override;
162 void OnDeviceRemoved(udev_device* device) override;
163
164 base::ThreadChecker thread_checker_;
165 ScopedObserver<DeviceMonitorLinux, DeviceMonitorLinux::Observer> observer_;
166
167 SensorDeviceManager::OnDeviceAddedCallback device_added_callback_;
168 SensorDeviceManager::OnDeviceRemovedCallback device_removed_callback_;
169
170 base::WeakPtr<SensorDeviceManager> manager_;
171
172 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
173
174 DISALLOW_COPY_AND_ASSIGN(SensorDeviceService);
175 };
176
177 } // namespace device
178
179 #endif // DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_MANAGER_LINUX_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698