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

Side by Side Diff: device/generic_sensor/platform_sensor_manager_linux.cc

Issue 2533793002: [sensors](CrOS/Linux) Implement Sensor device manager for sensors (Closed)
Patch Set: comments Created 4 years 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 unified diff | Download patch
OLDNEW
(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 DCHECK(!delegate_);
37
38 delegate_ = delegate;
39
40 DeviceMonitorLinux* monitor = DeviceMonitorLinux::GetInstance();
41 observer_.Add(monitor);
42 monitor->Enumerate(
43 base::Bind(&SensorDeviceManager::OnDeviceAdded, base::Unretained(this)));
44
45 task_runner_->PostTask(
46 FROM_HERE, base::Bind(&SensorDeviceManager::Delegate::OnEnumerationReady,
47 base::Unretained(delegate_)));
48 }
49
50 std::string SensorDeviceManager::GetUdevDeviceGetSubsystem(udev_device* dev) {
51 return StringOrEmptyIfNull(udev_device_get_subsystem(dev));
52 }
53
54 std::string SensorDeviceManager::GetUdevDeviceGetSyspath(udev_device* dev) {
55 return StringOrEmptyIfNull(udev_device_get_syspath(dev));
56 }
57
58 std::string SensorDeviceManager::GetUdevDeviceGetSysattrValue(
59 udev_device* dev,
60 const std::string& attribute) {
61 return StringOrEmptyIfNull(
62 udev_device_get_sysattr_value(dev, attribute.c_str()));
63 }
64
65 std::string SensorDeviceManager::GetUdevDeviceGetDevnode(udev_device* dev) {
66 return StringOrEmptyIfNull(udev_device_get_devnode(dev));
67 }
68
69 void SensorDeviceManager::OnDeviceAdded(udev_device* dev) {
70 const std::string subsystem = GetUdevDeviceGetSubsystem(dev);
71 if (subsystem.empty() || subsystem.compare("iio") != 0)
72 return;
73
74 const std::string sysfs_path = GetUdevDeviceGetSyspath(dev);
75 if (sysfs_path.empty())
76 return;
77
78 const std::string device_node = GetUdevDeviceGetDevnode(dev);
79 if (device_node.empty())
80 return;
81
82 const uint32_t first = static_cast<uint32_t>(mojom::SensorType::FIRST);
83 const uint32_t last = static_cast<uint32_t>(mojom::SensorType::LAST);
84 for (uint32_t i = first; i < last; ++i) {
85 SensorPathsLinux data;
86 mojom::SensorType type = static_cast<mojom::SensorType>(i);
87 if (!InitSensorData(type, &data))
88 continue;
89
90 std::vector<base::FilePath> sensor_file_names;
91 for (const std::vector<std::string>& names : data.sensor_file_names) {
92 for (const auto& name : names) {
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698