Chromium Code Reviews| Index: device/generic_sensor/platform_sensor_ambient_light_iio.cc |
| diff --git a/device/generic_sensor/platform_sensor_ambient_light_iio.cc b/device/generic_sensor/platform_sensor_ambient_light_iio.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..27b0191f54d52ece57f68b49a61359859f70e07d |
| --- /dev/null |
| +++ b/device/generic_sensor/platform_sensor_ambient_light_iio.cc |
| @@ -0,0 +1,110 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "device/generic_sensor/platform_sensor_ambient_light_iio.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/synchronization/shared_memory_seqlock_buffer.h" |
| + |
| +namespace device { |
| + |
| +namespace { |
| + |
| +using DeviceLightHardwareBuffer = base::SharedMemorySeqLockBuffer<double>; |
| + |
| +void DestroySensorOnMainThread(scoped_refptr<PlatformSensor> sensor) { |
| + sensor = nullptr; |
| +} |
| + |
| +} // namespace |
| + |
| +// static |
| +scoped_refptr<PlatformSensor> PlatformSensorAmbientLightIio::CreateSensor( |
| + mojom::SensorType type, |
| + mojo::ScopedSharedBufferMapping mapping, |
| + uint64_t buffer_size, |
| + PlatformSensorProvider* provider, |
| + scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) { |
| + scoped_refptr<PlatformSensorAmbientLightIio> sensor = |
| + new PlatformSensorAmbientLightIio(type, std::move(mapping), buffer_size, |
| + provider, ipc_task_runner); |
| + if (!sensor->sensor_exists()) { |
| + ipc_task_runner->PostTask( |
| + FROM_HERE, base::Bind(&DestroySensorOnMainThread, std::move(sensor))); |
| + return nullptr; |
| + } |
| + return std::move(sensor); |
| +} |
| + |
| +PlatformSensorAmbientLightIio::PlatformSensorAmbientLightIio( |
| + mojom::SensorType type, |
| + mojo::ScopedSharedBufferMapping mapping, |
| + uint64_t buffer_size, |
| + PlatformSensorProvider* provider, |
| + scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) |
| + : PollingPlatformSensor(type, |
| + std::move(mapping), |
| + provider, |
| + std::move(ipc_task_runner)), |
| + lux_value_(-1), |
| + sensor_exists_(false), |
| + ambient_light_reader_(new AmbientLightSensorReaderIio()), |
| + weak_factory_(this) { |
| + DCHECK(polling_thread_task_runner_->BelongsToCurrentThread()); |
| + if (ambient_light_reader_->DetectLightSensor()) { |
| + sensor_exists_ = true; |
| + } |
| +} |
| + |
| +PlatformSensorAmbientLightIio::~PlatformSensorAmbientLightIio() = default; |
| + |
| +mojom::ReportingMode PlatformSensorAmbientLightIio::GetReportingMode() { |
| + DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| + return mojom::ReportingMode::ON_CHANGE; |
| +} |
| + |
| +PlatformSensorConfiguration |
| +PlatformSensorAmbientLightIio::GetDefaultConfiguration() { |
| + DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| + return PlatformSensorConfiguration(5); |
| +} |
| + |
| +bool PlatformSensorAmbientLightIio::CheckSensorConfiguration( |
| + const PlatformSensorConfiguration& configuration) { |
| + DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| + return configuration.frequency() > 0 && |
| + configuration.frequency() <= |
| + PlatformSensorConfiguration::kMaxAllowedFrequency; |
| +} |
| + |
| +void PlatformSensorAmbientLightIio::UpdateReading() { |
| + DCHECK(polling_thread_task_runner_->BelongsToCurrentThread()); |
| + |
| + double update = -1; |
| + if (!ambient_light_reader_->ReadSensorLuxValue(&update)) { |
| + StopSensor(); |
|
darktears
2016/09/30 21:35:32
Is this the behavior that we should follow?
|
| + ipc_task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&PlatformSensorAmbientLightIio::NotifySensorError, |
| + weak_factory_.GetWeakPtr())); |
| + return; |
| + } |
| + |
| + if (lux_value_ == update) |
| + return; |
| + |
| + lux_value_ = update; |
| + |
| + DeviceLightHardwareBuffer* light_buffer = |
| + static_cast<DeviceLightHardwareBuffer*>(shared_buffer_mapping_.get()); |
| + light_buffer->seqlock.WriteBegin(); |
| + light_buffer->data = lux_value_; |
| + light_buffer->seqlock.WriteEnd(); |
| + |
| + ipc_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&PlatformSensorAmbientLightIio::NotifySensorReadingChanged, |
| + weak_factory_.GetWeakPtr())); |
| +} |
| + |
| +} // namespace device |