platform_sensor_linux.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2016 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. #include "services/device/generic_sensor/platform_sensor_linux.h"
  5. #include "base/bind.h"
  6. #include "base/ranges/algorithm.h"
  7. #include "base/time/time.h"
  8. #include "services/device/generic_sensor/linux/sensor_data_linux.h"
  9. #include "services/device/generic_sensor/platform_sensor_reader_linux.h"
  10. namespace device {
  11. PlatformSensorLinux::PlatformSensorLinux(
  12. mojom::SensorType type,
  13. SensorReadingSharedBuffer* reading_buffer,
  14. PlatformSensorProvider* provider,
  15. const SensorInfoLinux* sensor_device)
  16. : PlatformSensor(type, reading_buffer, provider),
  17. default_configuration_(
  18. PlatformSensorConfiguration(sensor_device->device_frequency)),
  19. reporting_mode_(sensor_device->reporting_mode) {
  20. sensor_reader_ =
  21. SensorReader::Create(*sensor_device, weak_factory_.GetWeakPtr());
  22. }
  23. PlatformSensorLinux::~PlatformSensorLinux() {
  24. DCHECK(main_task_runner()->RunsTasksInCurrentSequence());
  25. }
  26. mojom::ReportingMode PlatformSensorLinux::GetReportingMode() {
  27. DCHECK(main_task_runner()->RunsTasksInCurrentSequence());
  28. return reporting_mode_;
  29. }
  30. void PlatformSensorLinux::UpdatePlatformSensorReading(SensorReading reading) {
  31. DCHECK(main_task_runner()->RunsTasksInCurrentSequence());
  32. reading.raw.timestamp =
  33. (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
  34. UpdateSharedBufferAndNotifyClients(reading);
  35. }
  36. void PlatformSensorLinux::NotifyPlatformSensorError() {
  37. DCHECK(main_task_runner()->RunsTasksInCurrentSequence());
  38. NotifySensorError();
  39. }
  40. bool PlatformSensorLinux::StartSensor(
  41. const PlatformSensorConfiguration& configuration) {
  42. DCHECK(main_task_runner()->RunsTasksInCurrentSequence());
  43. sensor_reader_->StartFetchingData(configuration);
  44. return true;
  45. }
  46. void PlatformSensorLinux::StopSensor() {
  47. DCHECK(main_task_runner()->RunsTasksInCurrentSequence());
  48. sensor_reader_->StopFetchingData();
  49. }
  50. bool PlatformSensorLinux::CheckSensorConfiguration(
  51. const PlatformSensorConfiguration& configuration) {
  52. DCHECK(main_task_runner()->RunsTasksInCurrentSequence());
  53. return configuration.frequency() > 0 &&
  54. configuration.frequency() <= default_configuration_.frequency();
  55. }
  56. PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() {
  57. DCHECK(main_task_runner()->RunsTasksInCurrentSequence());
  58. return default_configuration_;
  59. }
  60. } // namespace device