accelerometer_file_reader.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 ASH_ACCELEROMETER_ACCELEROMETER_FILE_READER_H_
  5. #define ASH_ACCELEROMETER_ACCELEROMETER_FILE_READER_H_
  6. #include <string>
  7. #include <vector>
  8. #include "ash/accelerometer/accelerometer_reader.h"
  9. #include "base/files/file_util.h"
  10. #include "base/sequence_checker.h"
  11. #include "base/time/time.h"
  12. #include "base/timer/timer.h"
  13. namespace ash {
  14. enum class State { INITIALIZING, SUCCESS, FAILED };
  15. // Work that runs on a base::TaskRunner. It determines the accelerometer
  16. // configuration, and reads the data. Upon a successful read it will notify
  17. // all observers.
  18. class AccelerometerFileReader : public AccelerometerProviderInterface {
  19. public:
  20. AccelerometerFileReader();
  21. AccelerometerFileReader(const AccelerometerFileReader&) = delete;
  22. AccelerometerFileReader& operator=(const AccelerometerFileReader&) = delete;
  23. // AccelerometerProviderInterface:
  24. void PrepareAndInitialize() override;
  25. void TriggerRead() override;
  26. void CancelRead() override;
  27. private:
  28. struct InitializationResult {
  29. InitializationResult();
  30. ~InitializationResult();
  31. State initialization_state;
  32. ECLidAngleDriverStatus ec_lid_angle_driver_status;
  33. };
  34. // Represents necessary information in order to read an accelerometer device.
  35. struct ReadingData {
  36. ReadingData();
  37. ReadingData(const ReadingData&);
  38. ~ReadingData();
  39. // The full path to the accelerometer device to read.
  40. base::FilePath path;
  41. // The accelerometer sources which can be read from |path|.
  42. std::vector<AccelerometerSource> sources;
  43. };
  44. // Configuration structure for accelerometer device.
  45. struct ConfigurationData {
  46. ConfigurationData();
  47. ~ConfigurationData();
  48. // Number of accelerometers on device.
  49. size_t count;
  50. // sysfs entry to trigger readings.
  51. base::FilePath trigger_now;
  52. // Which accelerometers are present on device.
  53. bool has[ACCELEROMETER_SOURCE_COUNT];
  54. // Scale of accelerometers (i.e. raw value * scale = m/s^2).
  55. float scale[ACCELEROMETER_SOURCE_COUNT][3];
  56. // Index of each accelerometer axis in data stream.
  57. int index[ACCELEROMETER_SOURCE_COUNT][3];
  58. // The information for each accelerometer device to be read. In kernel 3.18
  59. // there is one per ACCELEROMETER_SOURCE_COUNT. On 3.14 there is only one.
  60. std::vector<ReadingData> reading_data;
  61. };
  62. ~AccelerometerFileReader() override;
  63. // Post a task to initialize on |blocking_task_runner_| and process the result
  64. // on the UI thread. May be called multiple times in the retries.
  65. void TryScheduleInitialize();
  66. // Detects the accelerometer configuration in |blocking_task_runner_|.
  67. // If an accelerometer is available, it triggers reads.
  68. // This function MAY be called more than once.
  69. // This function contains the actual initialization code to be run by the
  70. // Initialize function. It is needed because on some devices the sensor hub
  71. // isn't available at the time the call to Initialize is made.
  72. // If the sensor is found to be missing we'll request a re-run of this
  73. // function by returning State::INITIALIZING. TryScheduleInitializeInternal.
  74. InitializationResult InitializeInternal();
  75. // Attempt to finish the initialization with the result state. If it's
  76. // State::INITIALIZING, it means something is missing and need to re-run
  77. // |TryScheduleInitialize|, if not timed out yet.
  78. void SetStatesWithInitializationResult(InitializationResult result);
  79. // When accelerometers are presented as separate iio_devices this will perform
  80. // the initialize for one of the devices, at the given |iio_path| and the
  81. // symbolic link |name|. |location| is defined by AccelerometerSource.
  82. bool InitializeAccelerometer(const base::FilePath& iio_path,
  83. const base::FilePath& name,
  84. const std::string& location);
  85. // TODO(jonross): Separate the initialization into separate files. Add a gyp
  86. // rule to have them built for the appropriate kernels. (crbug.com/525658)
  87. // When accelerometers are presented as a single iio_device this will perform
  88. // the initialization for both of them.
  89. bool InitializeLegacyAccelerometers(const base::FilePath& iio_path,
  90. const base::FilePath& name);
  91. // Controls accelerometer reading.
  92. void EnableAccelerometerReading();
  93. void DisableAccelerometerReading();
  94. // The current initialization state of reader.
  95. State initialization_state_ = State::INITIALIZING;
  96. // Attempts to read the accelerometer data in |blocking_task_runner_|. Upon a
  97. // success, converts the raw reading to an AccelerometerUpdate and notifies
  98. // observers.
  99. void ReadSample();
  100. // The time at which initialization re-tries should stop.
  101. base::TimeTicks initialization_timeout_;
  102. // The accelerometer configuration.
  103. // Only used in |blocking_task_runner_|.
  104. ConfigurationData configuration_ GUARDED_BY_CONTEXT(sequence_checker_);
  105. // The timer to repeatedly read samples.
  106. // Only used in |blocking_task_runner_|.
  107. base::RepeatingTimer read_refresh_timer_
  108. GUARDED_BY_CONTEXT(sequence_checker_);
  109. // The task runner to use for blocking tasks.
  110. scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
  111. SEQUENCE_CHECKER(sequence_checker_);
  112. };
  113. } // namespace ash
  114. #endif // ASH_ACCELEROMETER_ACCELEROMETER_FILE_READER_H_