| Index: device/generic_sensor/platform_sensor_provider_iio.cc
|
| diff --git a/device/generic_sensor/platform_sensor_provider_iio.cc b/device/generic_sensor/platform_sensor_provider_iio.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..3640ffb1bbb4dd90d27f5ef581a401145f75a19c
|
| --- /dev/null
|
| +++ b/device/generic_sensor/platform_sensor_provider_iio.cc
|
| @@ -0,0 +1,139 @@
|
| +// 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_provider.h"
|
| +
|
| +#include "base/memory/singleton.h"
|
| +#include "base/task_runner_util.h"
|
| +#include "base/threading/thread.h"
|
| +#include "device/generic_sensor/iio/platform_sensor_utils_iio.h"
|
| +#include "device/generic_sensor/iio/sensor_data_iio.h"
|
| +#include "device/generic_sensor/platform_sensor_iio.h"
|
| +
|
| +namespace base {
|
| +class SingleThreadTaskRunner;
|
| +}
|
| +
|
| +namespace device {
|
| +
|
| +class PlatformSensorProviderIio : public PlatformSensorProvider {
|
| + public:
|
| + static PlatformSensorProviderIio* GetInstance();
|
| +
|
| + protected:
|
| + void CreateSensorInternal(mojom::SensorType type,
|
| + mojo::ScopedSharedBufferMapping mapping,
|
| + const CreateSensorCallback& callback) override;
|
| +
|
| + private:
|
| + friend struct base::DefaultSingletonTraits<PlatformSensorProviderIio>;
|
| + PlatformSensorProviderIio();
|
| + ~PlatformSensorProviderIio() override;
|
| +
|
| + void SensorReaderFound(
|
| + mojom::SensorType type,
|
| + mojo::ScopedSharedBufferMapping mapping,
|
| + const PlatformSensorProviderBase::CreateSensorCallback& callback,
|
| + const SensorDataIio& data,
|
| + std::unique_ptr<base::Thread> polling_thread,
|
| + std::unique_ptr<SensorReader> sensor_reader);
|
| +
|
| + // Helper function that gets a new sensor reader with a sensor type specific
|
| + // options taken from SensorDataIio.
|
| + std::unique_ptr<SensorReader> GetSensorReader(
|
| + const SensorDataIio& data,
|
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner);
|
| +
|
| + // TODO(maksims): make this as a separate class Manager that will
|
| + // create threads for new sensors, check sensors existence and notify provider
|
| + // if a new sensor has appeared and it can be created if a request comes
|
| + // again for the same sensor.
|
| + // A use case example: a request for a sensor X comes, manager checks if the
|
| + // sensor exists on a platform and notifies a provider it is not found.
|
| + // The provider stores this information into its cache and doesn't try to
|
| + // create this specific sensor if a request comes. But when, for example,
|
| + // the sensor X is plugged into a usb port, the manager notices that and
|
| + // notifies the provider, which updates its cache and starts handling requests
|
| + // for the sensor X.
|
| + //
|
| + // Right now, it is only used to delete polling threads if a new sensor
|
| + // is not created and the thread cannot be passed to it.
|
| + std::unique_ptr<base::Thread> manager_thread_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderIio);
|
| +};
|
| +
|
| +PlatformSensorProvider* PlatformSensorProvider::GetInstance() {
|
| + return PlatformSensorProviderIio::GetInstance();
|
| +}
|
| +
|
| +// static
|
| +PlatformSensorProviderIio* PlatformSensorProviderIio::GetInstance() {
|
| + return base::Singleton<
|
| + PlatformSensorProviderIio,
|
| + base::LeakySingletonTraits<PlatformSensorProviderIio>>::get();
|
| +}
|
| +
|
| +PlatformSensorProviderIio::PlatformSensorProviderIio() = default;
|
| +
|
| +PlatformSensorProviderIio::~PlatformSensorProviderIio() = default;
|
| +
|
| +void PlatformSensorProviderIio::CreateSensorInternal(
|
| + mojom::SensorType type,
|
| + mojo::ScopedSharedBufferMapping mapping,
|
| + const CreateSensorCallback& callback) {
|
| + SensorDataIio data;
|
| + if (!InitSensorData(type, &data)) {
|
| + callback.Run(nullptr);
|
| + return;
|
| + }
|
| +
|
| + if (!manager_thread_) {
|
| + manager_thread_.reset(new base::Thread("Linux/CrOS manager thread"));
|
| + if (!manager_thread_->StartWithOptions(
|
| + base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
|
| + callback.Run(nullptr);
|
| + return;
|
| + }
|
| + }
|
| +
|
| + std::unique_ptr<base::Thread> polling_thread(
|
| + new base::Thread("Linux/CrOS polling sensor thread."));
|
| +
|
| + if (!polling_thread->StartWithOptions(
|
| + base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
|
| + callback.Run(nullptr);
|
| + return;
|
| + }
|
| +
|
| + base::PostTaskAndReplyWithResult(
|
| + polling_thread->task_runner().get(), FROM_HERE,
|
| + base::Bind(SensorReader::Create, data),
|
| + base::Bind(&PlatformSensorProviderIio::SensorReaderFound,
|
| + base::Unretained(this), type, base::Passed(&mapping), callback,
|
| + data, base::Passed(&polling_thread)));
|
| +}
|
| +
|
| +void PlatformSensorProviderIio::SensorReaderFound(
|
| + mojom::SensorType type,
|
| + mojo::ScopedSharedBufferMapping mapping,
|
| + const PlatformSensorProviderBase::CreateSensorCallback& callback,
|
| + const SensorDataIio& data,
|
| + std::unique_ptr<base::Thread> polling_thread,
|
| + std::unique_ptr<SensorReader> sensor_reader) {
|
| + DCHECK(CalledOnValidThread());
|
| +
|
| + if (!sensor_reader) {
|
| + manager_thread_->task_runner()->DeleteSoon(FROM_HERE,
|
| + polling_thread.release());
|
| + callback.Run(nullptr);
|
| + return;
|
| + }
|
| +
|
| + callback.Run(new PlatformSensorIio(type, std::move(mapping), this, data,
|
| + std::move(polling_thread),
|
| + std::move(sensor_reader)));
|
| +}
|
| +
|
| +} // namespace device
|
|
|