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

Unified 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, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: device/generic_sensor/platform_sensor_manager_linux.h
diff --git a/device/generic_sensor/platform_sensor_manager_linux.h b/device/generic_sensor/platform_sensor_manager_linux.h
new file mode 100644
index 0000000000000000000000000000000000000000..80111220bb069f08867232c04634e44679ad825f
--- /dev/null
+++ b/device/generic_sensor/platform_sensor_manager_linux.h
@@ -0,0 +1,179 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_MANAGER_LINUX_H_
+#define DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_MANAGER_LINUX_H_
+
+#include "base/scoped_observer.h"
+
+#include "device/base/device_monitor_linux.h"
+#include "device/generic_sensor/linux/sensor_data_linux.h"
+#include "device/generic_sensor/public/interfaces/sensor.mojom.h"
+
+namespace device {
+
+class SensorDeviceService;
+
+// This structure represents an iio device, which info is taken
+// from udev service. If a client requests a sensor from a provider,
+// the last one takes this structure and uses it to create a requested platform
+// sensor of a certain type.
+struct SensorDeviceLinux {
+ // Represents current sensor device node.
+ const std::string device_node;
+ // Represents frequency of a sensor.
+ const double device_frequency;
+ // Represents scaling value to be applied on raw data.
+ const double device_scaling_value;
+ // Represents offset value that must be appled on raw data.
+ const double device_offset_value;
+ // Reporting mode of a sensor taken from SensorDataLinux.
+ const mojom::ReportingMode reporting_mode;
+ // Functor that is used to convert raw data.
+ const SensorDataLinux::ReaderFunctor apply_scaling_func;
+ // Sensor files in sysfs. Used to poll data.
+ const std::vector<base::FilePath> device_reading_files;
+
+ SensorDeviceLinux(const std::string& sensor_device_node,
+ double sensor_device_frequency,
+ double sensor_device_scaling_value,
+ double sensor_device_offset_value,
+ mojom::ReportingMode mode,
+ SensorDataLinux::ReaderFunctor scaling_func,
+ std::vector<base::FilePath> iio_device_reading_files);
+ ~SensorDeviceLinux();
+};
+
+class DEVICE_GENERIC_SENSOR_EXPORT SensorDeviceManager {
+ public:
+ // Callback that is used to get a sensor.
+ using GetSensorDeviceCallback = base::Callback<void(SensorDeviceLinux*)>;
+ using OnDeviceAddedCallback =
+ base::Callback<void(mojom::SensorType,
+ std::unique_ptr<SensorDeviceLinux>)>;
+ using OnDeviceRemovedCallback =
+ base::Callback<void(mojom::SensorType, const std::string&)>;
+
+ SensorDeviceManager();
+ ~SensorDeviceManager();
+
+ // Shutdowns a service that tracks events from iio subsystem.
+ void Shutdown();
+
+ // Returns SensorDeviceLinux structure of a requested type.
+ // If a request cannot be processed immediately, stores the |callback|
+ // and processes it once manager is ready to provide such an info.
+ void GetSensorDevice(mojom::SensorType type,
+ const GetSensorDeviceCallback& callback);
+
+ // Returns found iio devices. Currently not implemented.
+ void GetAllSensorDevices(const GetSensorDeviceCallback& callback);
+
+ // A blocking task runner that is used by a sensor device service.
+ void SetBlockingTaskRunner(
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner);
+
+ // Sets another service provided by tests.
+ void SetSensorDeviceServiceForTesting(
+ std::unique_ptr<SensorDeviceService> service);
+
+ // Called by SensorDeviceService when enumeration is ready.
+ void SetEnumerationReady();
+
+ private:
+ using SensorDeviceMap =
+ std::unordered_map<mojom::SensorType, std::unique_ptr<SensorDeviceLinux>>;
+
+ using CallbackToTypeMap =
+ std::unordered_map<mojom::SensorType, GetSensorDeviceCallback>;
+
+ // Initializes a sensor device service.
+ bool InitializeService();
+
+ // Sets service has been started.
+ void SetServiceStarted();
+
+ // Called by SensorDeviceService when a sensor device is found/attached.
+ void OnDeviceAdded(mojom::SensorType type,
+ std::unique_ptr<SensorDeviceLinux> sensor_device);
+
+ // Called by SensorDeviceService when a sensor device is removed/detached.
+ void OnDeviceRemoved(mojom::SensorType type, const std::string& device_node);
+
+ // Triggers all stored callbacks when enumeration is ready.
+ void TriggerCallbacks();
+
+ bool enumeration_ready() { return enumeration_ready_; }
+
+ // Stores all available sensor devices by type.
+ SensorDeviceMap sensor_device_map_;
+
+ // Stores request callbacks from clients by type.
+ CallbackToTypeMap get_device_callbacks_;
+
+ // Works as a service, which enumerates all available sensor devices on
+ // start and sends attach/detach events to this SensorDeviceManager.
+ std::unique_ptr<SensorDeviceService> sensor_device_service_;
+
+ // Used by a sensor device service for blocking udev tasks.
+ scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
+
+ // Current task runner, which this manager belongs to.
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
+
+ // Tells if enumeration is ready.
+ bool enumeration_ready_;
+
+ // Tells if service has been started.
+ bool sensor_device_service_started_;
+
+ base::WeakPtrFactory<SensorDeviceManager> weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(SensorDeviceManager);
+};
+
+// A sensor device service that uses linux device monitor to enumerate devices
+// and listens to events sent from linux device monitor, which receives them
+// from udev, and notifies |manager_| about those sensors devices.
+class DEVICE_GENERIC_SENSOR_EXPORT SensorDeviceService
+ : public DeviceMonitorLinux::Observer {
+ public:
+ SensorDeviceService(scoped_refptr<base::SingleThreadTaskRunner> task_runner);
+ ~SensorDeviceService() override;
+
+ // Starts this service.
+ virtual void Start(
+ base::WeakPtr<SensorDeviceManager> manager,
+ SensorDeviceManager::OnDeviceAddedCallback device_added_callback,
+ SensorDeviceManager::OnDeviceRemovedCallback device_removed_callback);
+
+ protected:
+ // Wrappers around udev system methods that can be implemented differently
+ // by tests.
+ virtual std::string UdevDeviceGetSubsystem(udev_device* dev);
+ virtual std::string UdevDeviceGetSyspath(udev_device* dev);
+ virtual std::string UdevDeviceGetSysattrValue(udev_device* dev,
+ const std::string& attribute);
+ virtual std::string UdevDeviceGetDevnode(udev_device* dev);
+
+ // DeviceMonitorLinux::Observer:
+ void OnDeviceAdded(udev_device* udev_device) override;
+ void OnDeviceRemoved(udev_device* device) override;
+
+ base::ThreadChecker thread_checker_;
+ ScopedObserver<DeviceMonitorLinux, DeviceMonitorLinux::Observer> observer_;
+
+ SensorDeviceManager::OnDeviceAddedCallback device_added_callback_;
+ SensorDeviceManager::OnDeviceRemovedCallback device_removed_callback_;
+
+ base::WeakPtr<SensorDeviceManager> manager_;
+
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
+
+ DISALLOW_COPY_AND_ASSIGN(SensorDeviceService);
+};
+
+} // namespace device
+
+#endif // DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_MANAGER_LINUX_H_

Powered by Google App Engine
This is Rietveld 408576698