accelerometer_provider_mojo.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_PROVIDER_MOJO_H_
  5. #define ASH_ACCELEROMETER_ACCELEROMETER_PROVIDER_MOJO_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "ash/accelerometer/accelerometer_reader.h"
  12. #include "ash/accelerometer/accelerometer_samples_observer.h"
  13. #include "ash/ash_export.h"
  14. #include "base/sequence_checker.h"
  15. #include "chromeos/components/sensors/mojom/cros_sensor_service.mojom.h"
  16. #include "mojo/public/cpp/bindings/receiver.h"
  17. #include "mojo/public/cpp/bindings/remote.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. namespace ash {
  20. // As devices may be late-present, the state and available devices cannot be
  21. // determined within any given time or requests. This MojoState helps
  22. // AccelerometerProviderMojo determine the current available devices and
  23. // provides a clear finite state machine. States that should provide samples:
  24. // LID, LID_BASE, ANGL_LID.
  25. enum class MojoState {
  26. INITIALIZING, // No devices available yet.
  27. LID, // Only lid-accelerometer is available.
  28. BASE, // Only base-accelerometer is available.
  29. LID_BASE, // Both accelerometers are available.
  30. ANGL, // Only lid-angle driver is available.
  31. ANGL_LID, // Both lid-angle driver and lid-accelerometer are available.
  32. };
  33. class AccelerometerProviderMojoTest;
  34. // Work that runs on the UI thread. As a sensor client, it communicates with IIO
  35. // Service, determines the accelerometers' configuration, and waits for the
  36. // accelerometers' samples. Upon receiving a sample, it will notify all
  37. // observers.
  38. class ASH_EXPORT AccelerometerProviderMojo
  39. : public AccelerometerProviderInterface,
  40. public chromeos::sensors::mojom::SensorHalClient,
  41. public chromeos::sensors::mojom::SensorServiceNewDevicesObserver {
  42. public:
  43. AccelerometerProviderMojo();
  44. AccelerometerProviderMojo(const AccelerometerProviderMojo&) = delete;
  45. AccelerometerProviderMojo& operator=(const AccelerometerProviderMojo&) =
  46. delete;
  47. // AccelerometerProviderInterface:
  48. void PrepareAndInitialize() override;
  49. void TriggerRead() override;
  50. void CancelRead() override;
  51. // chromeos::sensors::mojom::SensorHalClient:
  52. void SetUpChannel(mojo::PendingRemote<chromeos::sensors::mojom::SensorService>
  53. pending_remote) override;
  54. // chromeos::sensors::mojom::SensorServiceNewDevicesObserver
  55. void OnNewDeviceAdded(
  56. int32_t iio_device_id,
  57. const std::vector<chromeos::sensors::mojom::DeviceType>& types) override;
  58. MojoState GetInitializationStateForTesting() const;
  59. protected:
  60. // AccelerometerProviderInterface:
  61. bool ShouldDelayOnTabletPhysicalStateChanged() override;
  62. private:
  63. friend AccelerometerProviderMojoTest;
  64. struct AccelerometerData {
  65. AccelerometerData();
  66. ~AccelerometerData();
  67. bool ignored = false;
  68. // Temporarily stores the accelerometer remote, waiting for it's scale and
  69. // location information. It'll be passed to |samples_observer| as an
  70. // argument after all information is collected.
  71. mojo::Remote<chromeos::sensors::mojom::SensorDevice> remote;
  72. absl::optional<AccelerometerSource> location;
  73. absl::optional<float> scale;
  74. std::unique_ptr<AccelerometerSamplesObserver> samples_observer;
  75. };
  76. ~AccelerometerProviderMojo() override;
  77. // Registers chromeos::sensors::mojom::SensorHalClient to Sensor Hal
  78. // Dispatcher, waiting for the Mojo connection to IIO Service.
  79. void RegisterSensorClient();
  80. void OnSensorHalClientFailure();
  81. void OnSensorServiceDisconnect();
  82. void ResetSensorService();
  83. // Called when an in-use device is unplugged, and we need to search for other
  84. // devices to use.
  85. // Assumes that the angle device won't be unplugged.
  86. void ResetStates();
  87. void QueryDevices();
  88. void SetECLidAngleDriverSupported();
  89. // Update |initialization_state_| upon new devices' arrival.
  90. // MojoState (|initialization_state_|) transition:
  91. // INITIALIZING -> ANGL
  92. // LID -> ANGL_LID
  93. // BASE -> ANGL
  94. // ANGL Shouldn't happen
  95. // ANGL_LID Shouldn't happen
  96. void UpdateStateWithECLidAngleDriverSupported();
  97. // MojoState (|initialization_state_|) transition:
  98. // INITIALIZING -> LID
  99. // LID Shouldn't happen
  100. // BASE -> LID_BASE
  101. // ANGL -> ANGL_LID
  102. // ANGL_LID Shouldn't happen
  103. void UpdateStateWithLidAccelerometer();
  104. // MojoState (|initialization_state_|) transition:
  105. // INITIALIZING -> BASE
  106. // LID -> LID_BASE
  107. // BASE Shouldn't happen
  108. // ANGL -> ANGL
  109. // ANGL_LID -> ANGL_LID
  110. void UpdateStateWithBaseAccelerometer();
  111. void SetNewDevicesObserver();
  112. void OnNewDevicesObserverDisconnect();
  113. // Timeout of new devices. If lid-angle driver is still not present, assumes
  114. // it not supported and notifies observers.
  115. // This class still listens to new devices after the timeout to catch the
  116. // really late-present devices and avoid those issues, as the current use
  117. // cases/observers allow that.
  118. void OnNewDevicesTimeout();
  119. // Callback of GetDeviceIds(ANGL), containing the lid-angle device's id if it
  120. // exists.
  121. void GetLidAngleIdsCallback(const std::vector<int32_t>& lid_angle_ids);
  122. // Callback of GetDeviceIds(ACCEL), containing all iio_device_ids of
  123. // accelerometers.
  124. void GetAccelerometerIdsCallback(
  125. const std::vector<int32_t>& accelerometer_ids);
  126. // Creates the Mojo channel for the accelerometer, and requests the
  127. // accelerometer's required attributes before creating the
  128. // AccelerometerSamplesObserver of it.
  129. void RegisterAccelerometerWithId(int32_t id);
  130. void OnAccelerometerRemoteDisconnect(int32_t id,
  131. uint32_t custom_reason_code,
  132. const std::string& description);
  133. void GetAttributesCallback(
  134. int32_t id,
  135. const std::vector<absl::optional<std::string>>& values);
  136. // Ignores the accelerometer as the attributes are not expected.
  137. void IgnoreAccelerometer(int32_t id);
  138. // Creates the AccelerometerSamplesObserver for the accelerometer with |id|.
  139. void CreateAccelerometerSamplesObserver(int32_t id);
  140. // Controls accelerometer reading.
  141. void EnableAccelerometerReading();
  142. void DisableAccelerometerReading();
  143. // Called by |AccelerometerData::samples_observer| stored in the
  144. // |accelerometers_| map, containing a sample of the accelerometer.
  145. void OnSampleUpdatedCallback(int iio_device_id, std::vector<float> sample);
  146. // The state that contains the information of devices we have now. Used for
  147. // late-present devices.
  148. MojoState initialization_state_ = MojoState::INITIALIZING;
  149. // The Mojo channel connecting to Sensor Hal Dispatcher.
  150. mojo::Receiver<chromeos::sensors::mojom::SensorHalClient> sensor_hal_client_{
  151. this};
  152. // The Mojo channel to query and request for devices.
  153. mojo::Remote<chromeos::sensors::mojom::SensorService> sensor_service_remote_;
  154. // The Mojo channel to get notified when new devices are added to IIO Service.
  155. mojo::Receiver<chromeos::sensors::mojom::SensorServiceNewDevicesObserver>
  156. new_devices_observer_{this};
  157. // First is the accelerometer's iio device id, second is it's data, mojo
  158. // remote and samples observer.
  159. std::map<int32_t, AccelerometerData> accelerometers_;
  160. // First is the location index, second is the id of the accelerometer being
  161. // used in this reader.
  162. std::map<AccelerometerSource, int32_t> location_to_accelerometer_id_;
  163. // The flag to delay |OnTabletPhysicalStateChanged| until
  164. // |ec_lid_angle_driver_status_| is set.
  165. bool pending_on_tablet_physical_state_changed_ = false;
  166. // True if periodical accelerometer read is on.
  167. bool accelerometer_read_on_ = false;
  168. // The last seen accelerometer data.
  169. AccelerometerUpdate update_;
  170. SEQUENCE_CHECKER(sequence_checker_);
  171. };
  172. } // namespace ash
  173. #endif // ASH_ACCELEROMETER_ACCELEROMETER_PROVIDER_MOJO_H_