Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Unified Diff: device/generic_sensor/platform_sensor_manager_linux.cc

Issue 2533793002: [sensors](CrOS/Linux) Implement Sensor device manager for sensors (Closed)
Patch Set: fix build.gn Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: device/generic_sensor/platform_sensor_manager_linux.cc
diff --git a/device/generic_sensor/platform_sensor_manager_linux.cc b/device/generic_sensor/platform_sensor_manager_linux.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2170ea642768f3d8fb21c19b02cd45944e0ae81e
--- /dev/null
+++ b/device/generic_sensor/platform_sensor_manager_linux.cc
@@ -0,0 +1,341 @@
+// 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_manager_linux.h"
+
+#include "base/strings/string_number_conversions.h"
+#include "base/threading/thread_restrictions.h"
+#include "base/threading/thread_task_runner_handle.h"
+#include "device/generic_sensor/linux/platform_sensor_reader_linux.h"
+#include "device/generic_sensor/linux/sensor_data_linux.h"
+#include "device/generic_sensor/platform_sensor_provider_linux.h"
+
+namespace device {
+
+namespace {
+
+std::string StringOrEmptyIfNull(const char* value) {
+ return value ? value : std::string();
+}
+
+} // namespace
+
+SensorDeviceLinux::SensorDeviceLinux(
+ const std::string& sensor_device_node,
+ double sensor_device_frequency,
+ double sensor_device_scaling_value,
+ double sensor_device_offset_value,
+ mojom::ReportingMode mode,
+ SensorDataLinux::ReaderFunctor scaling_func,
+ std::vector<base::FilePath> device_reading_files)
+ : device_node(sensor_device_node),
+ device_frequency(sensor_device_frequency),
+ device_scaling_value(sensor_device_scaling_value),
+ device_offset_value(sensor_device_offset_value),
+ reporting_mode(mode),
+ apply_scaling_func(scaling_func),
+ device_reading_files(std::move(device_reading_files)) {}
+
+SensorDeviceLinux::~SensorDeviceLinux() = default;
+
+SensorDeviceService::SensorDeviceService(
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner)
+ : observer_(this), manager_(nullptr), task_runner_(task_runner) {
Mikhail 2016/11/30 12:29:36 nit: move(task_runner)
maksims (do not use this acc) 2016/12/05 13:06:59 not needed any more.
+ thread_checker_.DetachFromThread();
+}
+
+SensorDeviceService::~SensorDeviceService() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+}
+
+void SensorDeviceService::Start(
+ base::WeakPtr<SensorDeviceManager> manager,
+ SensorDeviceManager::OnDeviceAddedCallback device_added_callback,
+ SensorDeviceManager::OnDeviceRemovedCallback device_removed_callback) {
+ base::ThreadRestrictions::AssertIOAllowed();
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ manager_ = manager;
+ device_added_callback_ = device_added_callback;
+ device_removed_callback_ = device_removed_callback;
+
+ DeviceMonitorLinux* monitor = DeviceMonitorLinux::GetInstance();
+ observer_.Add(monitor);
+ monitor->Enumerate(
+ base::Bind(&SensorDeviceService::OnDeviceAdded, base::Unretained(this)));
+
+ task_runner_->PostTask(
Mikhail 2016/11/30 12:29:36 could be done on upper call frame
+ FROM_HERE,
+ base::Bind(&SensorDeviceManager::SetEnumerationReady, manager_));
+}
+
+std::string SensorDeviceService::UdevDeviceGetSubsystem(udev_device* dev) {
Mikhail 2016/11/30 12:29:36 nit: Get..
maksims (do not use this acc) 2016/12/05 13:06:59 Done.
+ return StringOrEmptyIfNull(udev_device_get_subsystem(dev));
+}
+
+std::string SensorDeviceService::UdevDeviceGetSyspath(udev_device* dev) {
+ return StringOrEmptyIfNull(udev_device_get_syspath(dev));
+}
+
+std::string SensorDeviceService::UdevDeviceGetSysattrValue(
+ udev_device* dev,
+ const std::string& attribute) {
+ return StringOrEmptyIfNull(
+ udev_device_get_sysattr_value(dev, attribute.c_str()));
+}
+
+std::string SensorDeviceService::UdevDeviceGetDevnode(udev_device* dev) {
+ return StringOrEmptyIfNull(udev_device_get_devnode(dev));
+}
+
+void SensorDeviceService::OnDeviceAdded(udev_device* dev) {
+ const std::string subsystem = UdevDeviceGetSubsystem(dev);
+ if (subsystem.empty() || strcmp(subsystem.c_str(), "iio") != 0)
Mikhail 2016/11/30 12:29:36 subsystem.compare()
maksims (do not use this acc) 2016/12/05 13:06:59 Done.
+ return;
+
+ const std::string sysfs_path = UdevDeviceGetSyspath(dev);
+ if (sysfs_path.empty())
+ return;
+
+ const std::string device_node = UdevDeviceGetDevnode(dev);
+ if (device_node.empty())
+ return;
+
+ const uint32_t first = static_cast<uint32_t>(mojom::SensorType::FIRST);
+ const uint32_t last = static_cast<uint32_t>(mojom::SensorType::LAST);
+ for (uint32_t i = first; i < last; ++i) {
+ SensorDataLinux data;
+ mojom::SensorType type = static_cast<mojom::SensorType>(i);
+ if (!InitSensorData(type, &data)) {
+ continue;
+ }
+
+ std::vector<base::FilePath> sensor_file_names;
+ for (auto const& names : data.sensor_file_names) {
+ for (auto const& name : names) {
+ const std::string value = UdevDeviceGetSysattrValue(dev, name.c_str());
+ if (value.empty())
+ continue;
+ base::FilePath full_path = base::FilePath(sysfs_path).Append(name);
+ sensor_file_names.push_back(full_path);
+ break;
+ }
+ }
+
+ if (sensor_file_names.empty())
+ continue;
+
+ const std::string scaling_value =
+ UdevDeviceGetSysattrValue(dev, data.sensor_scale_name.c_str());
+ // If scaling value is not found, treat it as 1.
+ double sensor_scaling_value = 1;
+ if (!scaling_value.empty())
+ base::StringToDouble(scaling_value, &sensor_scaling_value);
+
+ const std::string offset_vallue =
+ UdevDeviceGetSysattrValue(dev, data.sensor_offset_file_name.c_str());
+ // If offset value is not found, treat it as 0.
+ double sensor_offset_value = 0;
+ if (!offset_vallue.empty())
+ base::StringToDouble(offset_vallue, &sensor_offset_value);
+
+ const std::string frequency_value =
+ UdevDeviceGetSysattrValue(dev, data.sensor_frequency_file_name.c_str());
+ // If frequency is not found, use default one from SensorDataLinux struct.
+ double sensor_frequency_value = data.default_configuration.frequency();
+ // By default, |reporting_mode| is ON_CHANGE. But if platform provides
+ // sampling frequency, the reporting mode is CONTINUOUS.
+ mojom::ReportingMode reporting_mode = mojom::ReportingMode::ON_CHANGE;
+ if (!frequency_value.empty()) {
+ base::StringToDouble(frequency_value, &sensor_frequency_value);
+ reporting_mode = mojom::ReportingMode::CONTINUOUS;
+ }
+
+ // One |dev| can represent more than one sensor.
+ // For example, there is an accelerometer and gyroscope represented by one
+ // |dev| in Chrome OS, kernel < 3.18. Thus, iterate through all possible
+ // types of sensors.
+ std::unique_ptr<SensorDeviceLinux> device(new SensorDeviceLinux(
+ device_node, sensor_frequency_value, sensor_scaling_value,
+ sensor_offset_value, reporting_mode, data.apply_scaling_func,
+ std::move(sensor_file_names)));
+ task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(device_added_callback_, data.type, base::Passed(&device)));
+ }
+}
+
+void SensorDeviceService::OnDeviceRemoved(udev_device* dev) {
+ const char* subsystem = udev_device_get_subsystem(dev);
+ if (!subsystem || strcmp(subsystem, "iio") != 0)
+ return;
+
+ const char* value = udev_device_get_devnode(dev);
+ if (!value)
+ return;
+ const std::string device_node = value;
+
+ const uint32_t first = static_cast<uint32_t>(mojom::SensorType::FIRST);
+ const uint32_t last = static_cast<uint32_t>(mojom::SensorType::LAST);
+ for (uint32_t i = first; i < last; ++i) {
Mikhail 2016/11/30 12:29:36 better to cache sensor nodes instead of running th
maksims (do not use this acc) 2016/12/05 13:06:59 Done.
+ SensorDataLinux data;
+ mojom::SensorType type = static_cast<mojom::SensorType>(i);
+ if (!InitSensorData(type, &data))
+ continue;
+
+ std::vector<base::FilePath> sensor_file_names;
+ for (auto const& names : data.sensor_file_names) {
+ for (auto const& name : names) {
+ const char* value = udev_device_get_sysattr_value(dev, name.c_str());
+ if (value)
+ break;
+ }
+ break;
+ }
+
+ // One |dev| can represent more than one sensor.
+ // For example, there is an accelerometer and gyroscope represented by one
+ // |dev| in Chrome OS, kernel < 3.18. Thus, iterate through all possible
+ // type of sensors.
+ task_runner_->PostTask(FROM_HERE, base::Bind(device_removed_callback_,
+ data.type, device_node));
+ }
+}
+
+SensorDeviceManager::SensorDeviceManager()
+ : task_runner_(base::ThreadTaskRunnerHandle::Get()),
+ enumeration_ready_(false),
+ sensor_device_service_started_(false),
+ weak_factory_(this) {}
+
+SensorDeviceManager::~SensorDeviceManager() {
+ DCHECK(!sensor_device_service_);
+}
+
+void SensorDeviceManager::Shutdown() {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+ const bool did_post_task = file_task_runner_->DeleteSoon(
+ FROM_HERE, sensor_device_service_.release());
+ DCHECK(did_post_task);
+ enumeration_ready_ = false;
+ sensor_device_service_started_ = false;
+ sensor_device_map_.clear();
+}
+
+void SensorDeviceManager::GetSensorDevice(
+ mojom::SensorType type,
+ const GetSensorDeviceCallback& callback) {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+ if (!InitializeService()) {
+ callback.Run(nullptr);
+ return;
+ }
+ // Enumeration is not ready yet.
+ // Store callbacks and run them when ready.
+ if (!enumeration_ready()) {
+ DCHECK(!base::ContainsKey(get_device_callbacks_, type));
+ get_device_callbacks_[type] = callback;
+ return;
+ }
+ SensorDeviceLinux* device = nullptr;
+ if (base::ContainsKey(sensor_device_map_, type))
+ device = sensor_device_map_[type].get();
+ callback.Run(device);
+}
+
+void SensorDeviceManager::GetAllSensorDevices(
+ const GetSensorDeviceCallback& callback) {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+ // TODO(maksims): implement this method once we have discovery API.
+ NOTIMPLEMENTED();
+}
+
+void SensorDeviceManager::SetSensorDeviceServiceForTesting(
+ std::unique_ptr<SensorDeviceService> service) {
+ Shutdown();
+ sensor_device_service_ = std::move(service);
+}
+
+void SensorDeviceManager::SetBlockingTaskRunner(
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
+ file_task_runner_ = task_runner;
+}
+
+bool SensorDeviceManager::InitializeService() {
+ if (!sensor_device_service_) {
+ sensor_device_service_ =
+ base::MakeUnique<SensorDeviceService>(task_runner_);
+ }
+ if (!sensor_device_service_started_) {
+ OnDeviceAddedCallback device_added_callback = base::Bind(
+ &SensorDeviceManager::OnDeviceAdded, weak_factory_.GetWeakPtr());
+ OnDeviceRemovedCallback device_removed_callback = base::Bind(
+ &SensorDeviceManager::OnDeviceRemoved, weak_factory_.GetWeakPtr());
+
+ SetServiceStarted();
+ return file_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&SensorDeviceService::Start,
+ base::Unretained(sensor_device_service_.get()),
+ weak_factory_.GetWeakPtr(), std::move(device_added_callback),
+ std::move(device_removed_callback)));
+ }
+
+ return sensor_device_service_started_;
+}
+
+void SensorDeviceManager::SetServiceStarted() {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+ DCHECK(!sensor_device_service_started_);
+ sensor_device_service_started_ = true;
+}
+
+void SensorDeviceManager::OnDeviceAdded(
+ mojom::SensorType type,
+ std::unique_ptr<SensorDeviceLinux> sensor_device) {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+ // At the moment, we support only one device per type.
+ if (base::ContainsKey(sensor_device_map_, type))
+ return;
+ sensor_device_map_[type] = std::move(sensor_device);
+}
+
+void SensorDeviceManager::OnDeviceRemoved(mojom::SensorType type,
+ const std::string& device_node) {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+ auto it = sensor_device_map_.find(type);
+ if (it == sensor_device_map_.end())
+ return;
+ if (it->second->device_node == device_node)
+ sensor_device_map_.erase(it);
+}
+
+void SensorDeviceManager::SetEnumerationReady() {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+ DCHECK(!enumeration_ready());
+ enumeration_ready_ = true;
+ TriggerCallbacks();
+}
+
+void SensorDeviceManager::TriggerCallbacks() {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+
+ CallbackToTypeMap::iterator cb_it = get_device_callbacks_.begin();
Mikhail 2016/11/30 12:29:36 I think we can avoid caching callbacks here, if in
maksims (do not use this acc) 2016/12/05 13:06:59 As long as I've merged this class with the provide
+ while (cb_it != get_device_callbacks_.end()) {
+ SensorDeviceLinux* device = nullptr;
+ mojom::SensorType type = cb_it->first;
+ auto dev_it = sensor_device_map_.find(type);
+ if (dev_it != sensor_device_map_.end())
+ device = dev_it->second.get();
+
+ GetSensorDeviceCallback cb = cb_it->second;
+ cb_it->second.Reset();
+ if (!cb.is_null())
+ cb.Run(device);
+ ++cb_it;
+ }
+ get_device_callbacks_.clear();
+}
+
+} // namespace device

Powered by Google App Engine
This is Rietveld 408576698