OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ | 5 #ifndef DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ |
6 #define DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ | 6 #define DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ |
7 | 7 |
8 #include "device/generic_sensor/platform_sensor_provider.h" | 8 #include "device/generic_sensor/platform_sensor_provider.h" |
9 | 9 |
| 10 #if defined(USE_UDEV) |
| 11 #include "device/generic_sensor/platform_sensor_manager_linux.h" |
| 12 #endif |
| 13 |
10 namespace base { | 14 namespace base { |
| 15 template <typename T> |
| 16 struct DefaultSingletonTraits; |
11 class Thread; | 17 class Thread; |
12 } | 18 } |
13 | 19 |
14 namespace device { | 20 namespace device { |
15 | 21 |
16 struct SensorDataLinux; | 22 struct SensorInfoLinux; |
17 class SensorReader; | |
18 | 23 |
19 class PlatformSensorProviderLinux : public PlatformSensorProvider { | 24 class DEVICE_GENERIC_SENSOR_EXPORT PlatformSensorProviderLinux |
| 25 : public PlatformSensorProvider, |
| 26 public SensorDeviceManager::Delegate { |
20 public: | 27 public: |
21 PlatformSensorProviderLinux(); | 28 static PlatformSensorProviderLinux* GetInstance(); |
| 29 |
| 30 // Sets another service provided by tests. |
| 31 void SetSensorDeviceManagerForTesting( |
| 32 std::unique_ptr<SensorDeviceManager> sensor_device_manager); |
| 33 |
| 34 // Sets task runner for tests. |
| 35 void SetFileTaskRunnerForTesting( |
| 36 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 37 |
| 38 protected: |
22 ~PlatformSensorProviderLinux() override; | 39 ~PlatformSensorProviderLinux() override; |
23 | 40 |
24 static PlatformSensorProviderLinux* GetInstance(); | |
25 | |
26 protected: | |
27 void CreateSensorInternal(mojom::SensorType type, | 41 void CreateSensorInternal(mojom::SensorType type, |
28 mojo::ScopedSharedBufferMapping mapping, | 42 mojo::ScopedSharedBufferMapping mapping, |
29 const CreateSensorCallback& callback) override; | 43 const CreateSensorCallback& callback) override; |
30 | 44 |
31 void AllSensorsRemoved() override; | 45 void AllSensorsRemoved() override; |
32 | 46 |
33 void SetFileTaskRunner( | 47 void SetFileTaskRunner( |
34 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) override; | 48 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) override; |
35 | 49 |
36 private: | 50 private: |
37 void SensorReaderFound( | 51 friend struct base::DefaultSingletonTraits<PlatformSensorProviderLinux>; |
| 52 |
| 53 using SensorDeviceMap = |
| 54 std::unordered_map<mojom::SensorType, std::unique_ptr<SensorInfoLinux>>; |
| 55 |
| 56 PlatformSensorProviderLinux(); |
| 57 |
| 58 void SensorDeviceFound( |
38 mojom::SensorType type, | 59 mojom::SensorType type, |
39 mojo::ScopedSharedBufferMapping mapping, | 60 mojo::ScopedSharedBufferMapping mapping, |
40 const PlatformSensorProviderBase::CreateSensorCallback& callback, | 61 const PlatformSensorProviderBase::CreateSensorCallback& callback, |
41 const SensorDataLinux& data, | 62 SensorInfoLinux* sensor_device); |
42 std::unique_ptr<SensorReader> sensor_reader); | 63 |
| 64 bool StartPollingThread(); |
43 | 65 |
44 // Stops a polling thread if there are no sensors left. Must be called on | 66 // Stops a polling thread if there are no sensors left. Must be called on |
45 // a different that polling thread that allows io. | 67 // a different than the polling thread which allows I/O. |
46 void StopPollingThread(); | 68 void StopPollingThread(); |
47 | 69 |
48 // TODO(maksims): make a separate class Manager that will | 70 // Initializes a sensor device manager. |
49 // create provide sensors with a polling task runner, check sensors existence | 71 bool InitializeSenorDeviceManager(); |
50 // and notify provider if a new sensor has appeared and it can be created if a | 72 |
51 // request comes again for the same sensor. | 73 // Shuts down a service that tracks events from iio subsystem. |
52 // A use case example: a request for a sensor X comes, manager checks if the | 74 void Shutdown(); |
53 // sensor exists on a platform and notifies a provider it is not found. | 75 |
54 // The provider stores this information into its cache and doesn't try to | 76 // Returns SensorInfoLinux structure of a requested type. |
55 // create this specific sensor if a request comes. But when, for example, | 77 // If a request cannot be processed immediately, returns nullptr and |
56 // the sensor X is plugged into a usb port, the manager notices that and | 78 // all the requests stored in |requests_map_| are processed after |
57 // notifies the provider, which updates its cache and starts handling requests | 79 // enumeration is ready. |
58 // for the sensor X. | 80 SensorInfoLinux* GetSensorDevice(mojom::SensorType type); |
59 // | 81 |
60 // Right now, this thread is used to find sensors files and poll data. | 82 // Returns all found iio devices. Currently not implemented. |
| 83 void GetAllSensorDevices(); |
| 84 |
| 85 // Processed stored requests in |request_map_|. |
| 86 void ProcessStoredRequests(); |
| 87 |
| 88 // Called when sensors are created asynchronously after enumeration is done. |
| 89 void CreateSensorAndNotify(mojom::SensorType type, |
| 90 SensorInfoLinux* sensor_device); |
| 91 |
| 92 bool enumeration_ready() { return enumeration_ready_; } |
| 93 |
| 94 bool sensor_device_manager_started() { |
| 95 return sensor_device_manager_started_; |
| 96 } |
| 97 |
| 98 // SensorDeviceManager::Delegate implements: |
| 99 void OnEnumerationReady() override; |
| 100 void OnDeviceAdded(mojom::SensorType type, |
| 101 std::unique_ptr<SensorInfoLinux> sensor_device) override; |
| 102 void OnDeviceRemoved(mojom::SensorType type, |
| 103 const std::string& device_node) override; |
| 104 |
| 105 // Set to true when enumeration is ready. |
| 106 bool enumeration_ready_; |
| 107 |
| 108 // Set to true when |sensor_device_manager_| has been started. |
| 109 bool sensor_device_manager_started_; |
| 110 |
| 111 // Stores all available sensor devices by type. |
| 112 SensorDeviceMap sensor_devices_by_type_; |
| 113 |
| 114 // A thread that is used by sensor readers in case of polling strategy. |
61 std::unique_ptr<base::Thread> polling_thread_; | 115 std::unique_ptr<base::Thread> polling_thread_; |
62 | 116 |
63 // A task runner that is passed to polling sensors to poll data. | 117 // This manager is being used to get |SensorInfoLinux|, which represents |
64 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner_; | 118 // all the information of a concrete sensor provided by OS. |
| 119 std::unique_ptr<SensorDeviceManager> sensor_device_manager_; |
65 | 120 |
66 // Browser's file thread task runner passed from renderer. Used to | 121 // Browser's file thread task runner passed from renderer. Used by this |
67 // stop a polling thread. | 122 // provider to stop a polling thread and passed to a manager that |
| 123 // runs a linux device monitor service on this task runner. |
68 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 124 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
69 | 125 |
70 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderLinux); | 126 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderLinux); |
71 }; | 127 }; |
72 | 128 |
73 } // namespace device | 129 } // namespace device |
74 | 130 |
75 #endif // DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ | 131 #endif // DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ |
OLD | NEW |