Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "device/generic_sensor/platform_sensor_manager_linux.h" | |
| 6 | |
| 7 #include "base/strings/string_number_conversions.h" | |
| 8 #include "base/threading/thread_restrictions.h" | |
| 9 #include "base/threading/thread_task_runner_handle.h" | |
| 10 #include "device/generic_sensor/linux/sensor_data_linux.h" | |
| 11 | |
| 12 namespace device { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 std::string StringOrEmptyIfNull(const char* value) { | |
| 17 return value ? value : std::string(); | |
| 18 } | |
| 19 | |
| 20 } // namespace | |
| 21 | |
| 22 SensorDeviceManager::SensorDeviceManager() | |
| 23 : observer_(this), | |
| 24 delegate_(nullptr), | |
| 25 task_runner_(base::ThreadTaskRunnerHandle::Get()) { | |
| 26 thread_checker_.DetachFromThread(); | |
| 27 } | |
| 28 | |
| 29 SensorDeviceManager::~SensorDeviceManager() { | |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 31 } | |
| 32 | |
| 33 void SensorDeviceManager::Start(Delegate* delegate) { | |
| 34 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 35 base::ThreadRestrictions::AssertIOAllowed(); | |
| 36 | |
| 37 delegate_ = delegate; | |
|
Reilly Grant (use Gerrit)
2016/12/08 02:31:15
DCHECK(!delegate_)?
maksims (do not use this acc)
2016/12/08 18:39:17
Well, yeah. Good point.
| |
| 38 | |
| 39 DeviceMonitorLinux* monitor = DeviceMonitorLinux::GetInstance(); | |
| 40 observer_.Add(monitor); | |
| 41 monitor->Enumerate( | |
| 42 base::Bind(&SensorDeviceManager::OnDeviceAdded, base::Unretained(this))); | |
| 43 | |
| 44 task_runner_->PostTask( | |
| 45 FROM_HERE, base::Bind(&SensorDeviceManager::Delegate::OnEnumerationReady, | |
| 46 base::Unretained(delegate_))); | |
| 47 } | |
| 48 | |
| 49 std::string SensorDeviceManager::GetUdevDeviceGetSubsystem(udev_device* dev) { | |
| 50 return StringOrEmptyIfNull(udev_device_get_subsystem(dev)); | |
| 51 } | |
| 52 | |
| 53 std::string SensorDeviceManager::GetUdevDeviceGetSyspath(udev_device* dev) { | |
| 54 return StringOrEmptyIfNull(udev_device_get_syspath(dev)); | |
| 55 } | |
| 56 | |
| 57 std::string SensorDeviceManager::GetUdevDeviceGetSysattrValue( | |
| 58 udev_device* dev, | |
| 59 const std::string& attribute) { | |
| 60 return StringOrEmptyIfNull( | |
| 61 udev_device_get_sysattr_value(dev, attribute.c_str())); | |
| 62 } | |
| 63 | |
| 64 std::string SensorDeviceManager::GetUdevDeviceGetDevnode(udev_device* dev) { | |
| 65 return StringOrEmptyIfNull(udev_device_get_devnode(dev)); | |
| 66 } | |
| 67 | |
| 68 void SensorDeviceManager::OnDeviceAdded(udev_device* dev) { | |
| 69 const std::string subsystem = GetUdevDeviceGetSubsystem(dev); | |
| 70 if (subsystem.empty() || subsystem.compare("iio") != 0) | |
| 71 return; | |
| 72 | |
| 73 const std::string sysfs_path = GetUdevDeviceGetSyspath(dev); | |
| 74 if (sysfs_path.empty()) | |
| 75 return; | |
| 76 | |
| 77 const std::string device_node = GetUdevDeviceGetDevnode(dev); | |
| 78 if (device_node.empty()) | |
| 79 return; | |
| 80 | |
| 81 const uint32_t first = static_cast<uint32_t>(mojom::SensorType::FIRST); | |
| 82 const uint32_t last = static_cast<uint32_t>(mojom::SensorType::LAST); | |
| 83 for (uint32_t i = first; i < last; ++i) { | |
| 84 SensorPathsLinux data; | |
| 85 mojom::SensorType type = static_cast<mojom::SensorType>(i); | |
| 86 if (!InitSensorData(type, &data)) { | |
| 87 continue; | |
| 88 } | |
|
Reilly Grant (use Gerrit)
2016/12/08 02:31:14
nit: no braces around single-line if
maksims (do not use this acc)
2016/12/08 18:39:17
Done.
| |
| 89 | |
| 90 std::vector<base::FilePath> sensor_file_names; | |
| 91 for (auto const& names : data.sensor_file_names) { | |
|
Reilly Grant (use Gerrit)
2016/12/08 02:31:14
Spell out the type here so that it is clear what t
maksims (do not use this acc)
2016/12/08 18:39:17
Done.
| |
| 92 for (auto const& name : names) { | |
|
Reilly Grant (use Gerrit)
2016/12/08 02:31:14
const auto&
maksims (do not use this acc)
2016/12/08 18:39:17
Done.
| |
| 93 const std::string value = | |
| 94 GetUdevDeviceGetSysattrValue(dev, name.c_str()); | |
| 95 if (value.empty()) | |
| 96 continue; | |
| 97 base::FilePath full_path = base::FilePath(sysfs_path).Append(name); | |
| 98 sensor_file_names.push_back(full_path); | |
| 99 break; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 if (sensor_file_names.empty()) | |
| 104 continue; | |
| 105 | |
| 106 const std::string scaling_value = | |
| 107 GetUdevDeviceGetSysattrValue(dev, data.sensor_scale_name.c_str()); | |
| 108 // If scaling value is not found, treat it as 1. | |
| 109 double sensor_scaling_value = 1; | |
| 110 if (!scaling_value.empty()) | |
| 111 base::StringToDouble(scaling_value, &sensor_scaling_value); | |
| 112 | |
| 113 const std::string offset_vallue = | |
| 114 GetUdevDeviceGetSysattrValue(dev, data.sensor_offset_file_name.c_str()); | |
| 115 // If offset value is not found, treat it as 0. | |
| 116 double sensor_offset_value = 0; | |
| 117 if (!offset_vallue.empty()) | |
| 118 base::StringToDouble(offset_vallue, &sensor_offset_value); | |
| 119 | |
| 120 const std::string frequency_value = GetUdevDeviceGetSysattrValue( | |
| 121 dev, data.sensor_frequency_file_name.c_str()); | |
| 122 // If frequency is not found, use default one from SensorPathsLinux struct. | |
| 123 double sensor_frequency_value = data.default_configuration.frequency(); | |
| 124 // By default, |reporting_mode| is ON_CHANGE. But if platform provides | |
| 125 // sampling frequency, the reporting mode is CONTINUOUS. | |
| 126 mojom::ReportingMode reporting_mode = mojom::ReportingMode::ON_CHANGE; | |
| 127 if (!frequency_value.empty()) { | |
| 128 base::StringToDouble(frequency_value, &sensor_frequency_value); | |
| 129 reporting_mode = mojom::ReportingMode::CONTINUOUS; | |
| 130 } | |
| 131 | |
| 132 // Update own cache of known sensor devices. | |
| 133 if (!base::ContainsKey(sensors_by_node_, device_node)) | |
| 134 sensors_by_node_[device_node] = data.type; | |
| 135 | |
| 136 std::unique_ptr<SensorInfoLinux> device(new SensorInfoLinux( | |
| 137 device_node, sensor_frequency_value, sensor_scaling_value, | |
| 138 sensor_offset_value, reporting_mode, data.apply_scaling_func, | |
| 139 std::move(sensor_file_names))); | |
| 140 task_runner_->PostTask( | |
| 141 FROM_HERE, base::Bind(&SensorDeviceManager::Delegate::OnDeviceAdded, | |
| 142 base::Unretained(delegate_), data.type, | |
| 143 base::Passed(&device))); | |
| 144 | |
| 145 // One |dev| can represent more than one sensor. | |
| 146 // For example, there is an accelerometer and gyroscope represented by one | |
| 147 // |dev| in Chrome OS, kernel < 3.18. Thus, iterate through all possible | |
| 148 // types of sensors. | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 void SensorDeviceManager::OnDeviceRemoved(udev_device* dev) { | |
| 153 const std::string subsystem = GetUdevDeviceGetSubsystem(dev); | |
| 154 if (subsystem.empty() || subsystem.compare("iio") != 0) | |
| 155 return; | |
| 156 | |
| 157 const std::string device_node = GetUdevDeviceGetDevnode(dev); | |
| 158 if (device_node.empty()) | |
| 159 return; | |
| 160 | |
| 161 auto sensor = sensors_by_node_.find(device_node); | |
| 162 if (sensor == sensors_by_node_.end()) | |
| 163 return; | |
| 164 mojom::SensorType type = sensor->second; | |
| 165 sensors_by_node_.erase(sensor); | |
| 166 | |
| 167 task_runner_->PostTask( | |
| 168 FROM_HERE, base::Bind(&SensorDeviceManager::Delegate::OnDeviceRemoved, | |
| 169 base::Unretained(delegate_), type, device_node)); | |
| 170 } | |
| 171 | |
| 172 } // namespace device | |
| OLD | NEW |