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

Side by Side Diff: device/generic_sensor/linux/sensor_data_linux.cc

Issue 2865263002: Move //device/generic_sensor to be part of the internal implementation of the Device Service. (Closed)
Patch Set: code rebase Created 3 years, 7 months 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 "base/sys_info.h"
6 #include "base/version.h"
7 #include "device/generic_sensor/generic_sensor_consts.h"
8 #include "device/generic_sensor/linux/sensor_data_linux.h"
9 #include "device/generic_sensor/public/cpp/sensor_reading.h"
10
11 namespace device {
12
13 namespace {
14
15 using mojom::SensorType;
16
17 #if defined(OS_CHROMEOS)
18 // ChromeOS kernel version, when axes were changed to XYZ. Before 3.18,
19 // they were YXZ.
20 const char kChangedAxisKernelVersion[] = "3.18.0";
21 #endif
22
23 void InitAmbientLightSensorData(SensorPathsLinux* data) {
24 std::vector<std::string> file_names{
25 "in_illuminance0_input", "in_illuminance_input", "in_illuminance0_raw",
26 "in_illuminance_raw", "in_intensity_both_raw"};
27 data->sensor_file_names.push_back(std::move(file_names));
28 data->sensor_frequency_file_name = "in_intensity_sampling_frequency";
29 data->sensor_scale_name = "in_intensity_scale";
30 data->apply_scaling_func = base::Bind(
31 [](double scaling_value, double offset, SensorReading& reading) {
32 reading.values[0] = scaling_value * (reading.values[0] + offset);
33 });
34 data->default_configuration =
35 PlatformSensorConfiguration(kDefaultAmbientLightFrequencyHz);
36 }
37
38 // Depending on a kernel version, CrOS has a different axes plane.
39 // Before 3.18 it was YXZ and after XYZ.
40 // TODO(maksims): Track crbug.com/501184. 3.14 will have the same sensor stack
41 // as 3.18 has, which will probably change the order of axes.
42 void MaybeCheckKernelVersionAndAssignFileNames(
43 const std::vector<std::string>& file_names_x,
44 const std::vector<std::string>& file_names_y,
45 const std::vector<std::string>& file_names_z,
46 SensorPathsLinux* data) {
47 #if defined(OS_CHROMEOS)
48 const base::Version checked_kernel_version(kChangedAxisKernelVersion);
49 DCHECK(checked_kernel_version.IsValid());
50 const base::Version current_version(base::SysInfo::OperatingSystemVersion());
51 if (current_version.IsValid() && current_version < checked_kernel_version) {
52 data->sensor_file_names.push_back(file_names_y);
53 data->sensor_file_names.push_back(file_names_x);
54 } else {
55 data->sensor_file_names.push_back(file_names_x);
56 data->sensor_file_names.push_back(file_names_y);
57 }
58 data->sensor_file_names.push_back(file_names_z);
59 #else
60 data->sensor_file_names.push_back(file_names_x);
61 data->sensor_file_names.push_back(file_names_y);
62 data->sensor_file_names.push_back(file_names_z);
63 #endif
64 }
65
66 // TODO(maksims): add support for lid accelerometer on chromeos.
67 void InitAccelerometerSensorData(SensorPathsLinux* data) {
68 std::vector<std::string> file_names_x{"in_accel_x_base_raw",
69 "in_accel_x_raw"};
70 std::vector<std::string> file_names_y{"in_accel_y_base_raw",
71 "in_accel_y_raw"};
72 std::vector<std::string> file_names_z{"in_accel_z_base_raw",
73 "in_accel_z_raw"};
74
75 #if defined(OS_CHROMEOS)
76 data->sensor_scale_name = "in_accel_base_scale";
77 data->sensor_frequency_file_name = "in_accel_base_sampling_frequency";
78 data->apply_scaling_func = base::Bind(
79 [](double scaling_value, double offset, SensorReading& reading) {
80 double scaling = kMeanGravity / scaling_value;
81 reading.values[0] = scaling * (reading.values[0] + offset);
82 reading.values[1] = scaling * (reading.values[1] + offset);
83 reading.values[2] = scaling * (reading.values[2] + offset);
84 });
85 #else
86 data->sensor_scale_name = "in_accel_scale";
87 data->sensor_offset_file_name = "in_accel_offset";
88 data->sensor_frequency_file_name = "in_accel_sampling_frequency";
89 data->apply_scaling_func = base::Bind(
90 [](double scaling_value, double offset, SensorReading& reading) {
91 // Adapt Linux reading values to generic sensor api specs.
92 reading.values[0] = -scaling_value * (reading.values[0] + offset);
93 reading.values[1] = -scaling_value * (reading.values[1] + offset);
94 reading.values[2] = -scaling_value * (reading.values[2] + offset);
95 });
96 #endif
97
98 MaybeCheckKernelVersionAndAssignFileNames(file_names_x, file_names_y,
99 file_names_z, data);
100 data->default_configuration =
101 PlatformSensorConfiguration(kDefaultAccelerometerFrequencyHz);
102 }
103
104 void InitGyroscopeSensorData(SensorPathsLinux* data) {
105 std::vector<std::string> file_names_x{"in_anglvel_x_base_raw",
106 "in_anglvel_x_raw"};
107 std::vector<std::string> file_names_y{"in_anglvel_y_base_raw",
108 "in_anglvel_y_raw"};
109 std::vector<std::string> file_names_z{"in_anglvel_z_base_raw",
110 "in_anglvel_z_raw"};
111 #if defined(OS_CHROMEOS)
112 data->sensor_scale_name = "in_anglvel_base_scale";
113 data->sensor_frequency_file_name = "in_anglvel_base_frequency";
114 data->apply_scaling_func = base::Bind(
115 [](double scaling_value, double offset, SensorReading& reading) {
116 double scaling = kMeanGravity * kRadiansInDegrees / scaling_value;
117 // Adapt CrOS reading values to generic sensor api specs.
118 reading.values[0] = -scaling * (reading.values[0] + offset);
119 reading.values[1] = -scaling * (reading.values[1] + offset);
120 reading.values[2] = -scaling * (reading.values[2] + offset);
121 });
122 #else
123 data->sensor_scale_name = "in_anglvel_scale";
124 data->sensor_offset_file_name = "in_anglvel_offset";
125 data->sensor_frequency_file_name = "in_anglvel_sampling_frequency";
126 data->apply_scaling_func = base::Bind(
127 [](double scaling_value, double offset, SensorReading& reading) {
128 reading.values[0] = scaling_value * (reading.values[0] + offset);
129 reading.values[1] = scaling_value * (reading.values[1] + offset);
130 reading.values[2] = scaling_value * (reading.values[2] + offset);
131 });
132 #endif
133
134 MaybeCheckKernelVersionAndAssignFileNames(file_names_x, file_names_y,
135 file_names_z, data);
136 data->default_configuration =
137 PlatformSensorConfiguration(kDefaultGyroscopeFrequencyHz);
138 }
139
140 // TODO(maksims): Verify magnitometer works correctly on a chromebook when
141 // I get one with that sensor onboard.
142 void InitMagnitometerSensorData(SensorPathsLinux* data) {
143 std::vector<std::string> file_names_x{"in_magn_x_raw"};
144 std::vector<std::string> file_names_y{"in_magn_y_raw"};
145 std::vector<std::string> file_names_z{"in_magn_z_raw"};
146
147 data->sensor_scale_name = "in_magn_scale";
148 data->sensor_offset_file_name = "in_magn_offset";
149 data->sensor_frequency_file_name = "in_magn_sampling_frequency";
150 data->apply_scaling_func = base::Bind(
151 [](double scaling_value, double offset, SensorReading& reading) {
152 double scaling = scaling_value * kMicroteslaInGauss;
153 reading.values[0] = scaling * (reading.values[0] + offset);
154 reading.values[1] = scaling * (reading.values[1] + offset);
155 reading.values[2] = scaling * (reading.values[2] + offset);
156 });
157
158 MaybeCheckKernelVersionAndAssignFileNames(file_names_x, file_names_y,
159 file_names_z, data);
160 data->default_configuration =
161 PlatformSensorConfiguration(kDefaultMagnetometerFrequencyHz);
162 }
163
164 } // namespace
165
166 SensorPathsLinux::SensorPathsLinux() = default;
167 SensorPathsLinux::~SensorPathsLinux() = default;
168 SensorPathsLinux::SensorPathsLinux(const SensorPathsLinux& other) = default;
169
170 bool InitSensorData(SensorType type, SensorPathsLinux* data) {
171 DCHECK(data);
172
173 data->type = type;
174 switch (type) {
175 case SensorType::AMBIENT_LIGHT:
176 InitAmbientLightSensorData(data);
177 break;
178 case SensorType::ACCELEROMETER:
179 InitAccelerometerSensorData(data);
180 break;
181 case SensorType::GYROSCOPE:
182 InitGyroscopeSensorData(data);
183 break;
184 case SensorType::MAGNETOMETER:
185 InitMagnitometerSensorData(data);
186 break;
187 default:
188 return false;
189 }
190
191 return true;
192 }
193
194 SensorInfoLinux::SensorInfoLinux(
195 const std::string& sensor_device_node,
196 double sensor_device_frequency,
197 double sensor_device_scaling_value,
198 double sensor_device_offset_value,
199 mojom::ReportingMode mode,
200 SensorPathsLinux::ReaderFunctor scaling_func,
201 std::vector<base::FilePath> device_reading_files)
202 : device_node(sensor_device_node),
203 device_frequency(sensor_device_frequency),
204 device_scaling_value(sensor_device_scaling_value),
205 device_offset_value(sensor_device_offset_value),
206 reporting_mode(mode),
207 apply_scaling_func(scaling_func),
208 device_reading_files(std::move(device_reading_files)) {}
209
210 SensorInfoLinux::~SensorInfoLinux() = default;
211
212 } // namespace device
OLDNEW
« no previous file with comments | « device/generic_sensor/linux/sensor_data_linux.h ('k') | device/generic_sensor/linux/sensor_device_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698