platform_sensor_chromeos.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2020 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_CHROMEOS_H_
  5. #define SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_CHROMEOS_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/gtest_prod_util.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/sequence_checker.h"
  11. #include "chromeos/components/sensors/mojom/sensor.mojom.h"
  12. #include "mojo/public/cpp/bindings/remote.h"
  13. #include "services/device/generic_sensor/platform_sensor.h"
  14. namespace device {
  15. class PlatformSensorChromeOS
  16. : public PlatformSensor,
  17. public chromeos::sensors::mojom::SensorDeviceSamplesObserver {
  18. public:
  19. PlatformSensorChromeOS(
  20. int32_t iio_device_id,
  21. mojom::SensorType type,
  22. SensorReadingSharedBuffer* reading_buffer,
  23. PlatformSensorProvider* provider,
  24. mojo::ConnectionErrorWithReasonCallback sensor_device_disconnect_callback,
  25. double scale,
  26. mojo::Remote<chromeos::sensors::mojom::SensorDevice>
  27. sensor_device_remote);
  28. PlatformSensorChromeOS(const PlatformSensorChromeOS&) = delete;
  29. PlatformSensorChromeOS& operator=(const PlatformSensorChromeOS&) = delete;
  30. // PlatformSensor overrides:
  31. // Only ambient light sensors' ReportingMode is ON_CHANGE.
  32. mojom::ReportingMode GetReportingMode() override;
  33. bool CheckSensorConfiguration(
  34. const PlatformSensorConfiguration& configuration) override;
  35. PlatformSensorConfiguration GetDefaultConfiguration() override;
  36. void SensorReplaced() override;
  37. // chromeos::sensors::mojom::SensorDeviceSamplesObserver overrides:
  38. void OnSampleUpdated(const base::flat_map<int32_t, int64_t>& sample) override;
  39. void OnErrorOccurred(
  40. chromeos::sensors::mojom::ObserverErrorType type) override;
  41. protected:
  42. ~PlatformSensorChromeOS() override;
  43. // PlatformSensor overrides:
  44. bool StartSensor(const PlatformSensorConfiguration& configuration) override;
  45. void StopSensor() override;
  46. private:
  47. // Those numbers were based on the values used in CrOS Power Manager:
  48. // src/platform2/power_manager/powerd/system/ambient_light_sensor_delegate_mojo.h
  49. static constexpr uint32_t kNumFailedReadsBeforeGivingUp = 20;
  50. // Number of successful reads to recover |num_failed_reads_| by one.
  51. static constexpr uint32_t kNumRecoveryReads = 2;
  52. void ResetOnError();
  53. void OnSensorDeviceDisconnect(uint32_t custom_reason_code,
  54. const std::string& description);
  55. void StartReadingIfReady();
  56. mojo::PendingRemote<chromeos::sensors::mojom::SensorDeviceSamplesObserver>
  57. BindNewPipeAndPassRemote();
  58. void OnObserverDisconnect(uint32_t custom_reason_code,
  59. const std::string& description);
  60. void SetRequiredChannels();
  61. void GetAllChannelIdsCallback(
  62. const std::vector<std::string>& iio_channel_ids);
  63. void UpdateSensorDeviceFrequency();
  64. void SetFrequencyCallback(double target_frequency, double result_frequency);
  65. void SetChannelsEnabled();
  66. void SetChannelsEnabledCallback(const std::vector<int32_t>& failed_indices);
  67. double GetScaledValue(int64_t value) const;
  68. void OnReadFailure();
  69. int32_t iio_device_id_;
  70. mojo::ConnectionErrorWithReasonCallback sensor_device_disconnect_callback_;
  71. const PlatformSensorConfiguration default_configuration_;
  72. PlatformSensorConfiguration current_configuration_;
  73. double scale_;
  74. mojo::Remote<chromeos::sensors::mojom::SensorDevice> sensor_device_remote_;
  75. // The required channel ids for the sensor.
  76. std::vector<std::string> required_channel_ids_;
  77. // The list of channel ids retrieved from iioservice. Use channels' indices
  78. // in this list to identify them.
  79. std::vector<std::string> iio_channel_ids_;
  80. // Channel indices of |required_channel_ids_| to enable.
  81. std::vector<int32_t> channel_indices_;
  82. // Number of failed reads. Triggers an error if it reaches
  83. // kNumFailedReadsBeforeGivingUp.
  84. uint32_t num_failed_reads_ = 0;
  85. // Every time this reaches kNumRecoveryReads |num_failed_reads_| is
  86. // decremented by 1.
  87. uint32_t num_recovery_reads_ = 0;
  88. mojo::Receiver<chromeos::sensors::mojom::SensorDeviceSamplesObserver>
  89. receiver_{this};
  90. SEQUENCE_CHECKER(sequence_checker_);
  91. base::WeakPtrFactory<PlatformSensorChromeOS> weak_factory_{this};
  92. FRIEND_TEST_ALL_PREFIXES(PlatformSensorChromeOSOneChannelTest,
  93. ResetOnTooManyFailures);
  94. };
  95. } // namespace device
  96. #endif // SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_CHROMEOS_H_