platform_sensor_fusion.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2017 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. #ifndef SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_FUSION_H_
  5. #define SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_FUSION_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/containers/flat_map.h"
  9. #include "base/gtest_prod_util.h"
  10. #include "services/device/generic_sensor/platform_sensor.h"
  11. #include "services/device/generic_sensor/platform_sensor_provider_base.h"
  12. namespace device {
  13. class PlatformSensorFusionAlgorithm;
  14. // Implementation of a platform sensor using sensor fusion. There will be a
  15. // instance of this fusion sensor per browser process which is created by
  16. // the PlatformSensorProvider. If there are no clients, this instance is not
  17. // created.
  18. //
  19. // This class implements the generic concept of sensor fusion. It implements
  20. // a new sensor using data from one or more existing sensors. For example,
  21. // it can implement a *_EULER_ANGLES orientation sensor using a
  22. // *_QUATERNION orientation sensor, or vice-versa.
  23. //
  24. // It can also implement an orientation sensor using an ACCELEROMETER, etc.
  25. class PlatformSensorFusion : public PlatformSensor,
  26. public PlatformSensor::Client {
  27. public:
  28. // Construct a platform fusion sensor of type |fusion_sensor_type| using
  29. // one or more sensors whose sensor types are |source_sensor_types|, given
  30. // a buffer |mapping| where readings will be written.
  31. // The result of this method is passed asynchronously through the given
  32. // |callback| call: it can be either newly created object on success or
  33. // nullptr on failure.
  34. static void Create(
  35. SensorReadingSharedBuffer* reading_buffer,
  36. PlatformSensorProvider* provider,
  37. std::unique_ptr<PlatformSensorFusionAlgorithm> fusion_algorithm,
  38. PlatformSensorProviderBase::CreateSensorCallback callback);
  39. PlatformSensorFusion(const PlatformSensorFusion&) = delete;
  40. PlatformSensorFusion& operator=(const PlatformSensorFusion&) = delete;
  41. // PlatformSensor:
  42. mojom::ReportingMode GetReportingMode() override;
  43. PlatformSensorConfiguration GetDefaultConfiguration() override;
  44. bool CheckSensorConfiguration(
  45. const PlatformSensorConfiguration& configuration) override;
  46. double GetMaximumSupportedFrequency() override;
  47. // PlatformSensor::Client:
  48. void OnSensorReadingChanged(mojom::SensorType type) override;
  49. void OnSensorError() override;
  50. bool IsSuspended() override;
  51. virtual bool GetSourceReading(mojom::SensorType type, SensorReading* result);
  52. bool IsSignificantlyDifferent(const SensorReading& reading1,
  53. const SensorReading& reading2,
  54. mojom::SensorType sensor_type) override;
  55. protected:
  56. class Factory;
  57. using SourcesMap =
  58. base::flat_map<mojom::SensorType, scoped_refptr<PlatformSensor>>;
  59. PlatformSensorFusion(
  60. SensorReadingSharedBuffer* reading_buffer,
  61. PlatformSensorProvider* provider,
  62. std::unique_ptr<PlatformSensorFusionAlgorithm> fusion_algorithm,
  63. SourcesMap sources);
  64. ~PlatformSensorFusion() override;
  65. bool StartSensor(const PlatformSensorConfiguration& configuration) override;
  66. void StopSensor() override;
  67. PlatformSensorFusionAlgorithm* fusion_algorithm() const {
  68. return fusion_algorithm_.get();
  69. }
  70. FRIEND_TEST_ALL_PREFIXES(PlatformSensorFusionTest, OnSensorReadingChanged);
  71. FRIEND_TEST_ALL_PREFIXES(PlatformSensorFusionTest,
  72. FusionIsSignificantlyDifferent);
  73. private:
  74. std::unique_ptr<PlatformSensorFusionAlgorithm> fusion_algorithm_;
  75. SourcesMap source_sensors_;
  76. mojom::ReportingMode reporting_mode_;
  77. };
  78. } // namespace device
  79. #endif // SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_FUSION_H_