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

Side by Side Diff: chromeos/accelerometer/accelerometer_reader.cc

Issue 1306453003: Update AccelerometerReader to support separate iio devices (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/accelerometer/accelerometer_reader.h" 5 #include "chromeos/accelerometer/accelerometer_reader.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/files/file_enumerator.h"
10 #include "base/files/file_util.h" 12 #include "base/files/file_util.h"
11 #include "base/location.h" 13 #include "base/location.h"
12 #include "base/memory/singleton.h" 14 #include "base/memory/singleton.h"
13 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
14 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
17 #include "base/task_runner.h" 19 #include "base/task_runner.h"
18 #include "base/task_runner_util.h" 20 #include "base/task_runner_util.h"
19 #include "base/thread_task_runner_handle.h" 21 #include "base/thread_task_runner_handle.h"
20 #include "base/threading/platform_thread.h" 22 #include "base/threading/platform_thread.h"
21 #include "base/threading/sequenced_worker_pool.h" 23 #include "base/threading/sequenced_worker_pool.h"
22 24
23 namespace chromeos { 25 namespace chromeos {
24 26
25 namespace { 27 namespace {
26 28
27 // Paths to access necessary data from the accelerometer device. 29 // Paths to access necessary data from the accelerometer device.
28 const base::FilePath::CharType kAccelerometerTriggerPath[] = 30 const base::FilePath::CharType kAccelerometerTriggerPath[] =
29 FILE_PATH_LITERAL("/sys/bus/iio/devices/trigger0/trigger_now"); 31 FILE_PATH_LITERAL("/sys/bus/iio/devices/trigger0/trigger_now");
30 const base::FilePath::CharType kAccelerometerDevicePath[] = 32 const base::FilePath::CharType kAccelerometerDevicePath[] =
31 FILE_PATH_LITERAL("/dev/cros-ec-accel"); 33 FILE_PATH_LITERAL("/dev/cros-ec-accel");
32 const base::FilePath::CharType kAccelerometerIioBasePath[] = 34 const base::FilePath::CharType kAccelerometerIioBasePath[] =
33 FILE_PATH_LITERAL("/sys/bus/iio/devices/"); 35 FILE_PATH_LITERAL("/sys/bus/iio/devices/");
34 36
35 // File within the device in kAccelerometerIioBasePath containing the scale of
36 // the accelerometers.
37 const base::FilePath::CharType kScaleFileName[] = "in_accel_scale";
38
39 // This is the per source scale file in use on kernels older than 3.18. We 37 // This is the per source scale file in use on kernels older than 3.18. We
40 // should remove this when all devices having accelerometers are on kernel 3.18 38 // should remove this when all devices having accelerometers are on kernel 3.18
41 // or later or have been patched to use new format: http://crbug.com/510831 39 // or later or have been patched to use new format: http://crbug.com/510831
42 const base::FilePath::CharType kSourceScaleNameFormatString[] = 40 const base::FilePath::CharType kLegacyScaleNameFormatString[] =
43 "in_accel_%s_scale"; 41 "in_accel_%s_scale";
44 42
43 // File within kAccelerometerDevicePath/device* which denotes a single scale to
44 // be used across all axes.
45 const base::FilePath::CharType kAccelerometerScaleFileName[] = "scale";
46
47 // File within kAccelerometerDevicePath/device* which denotes the
48 // AccelerometerSource for the accelerometer.
49 const base::FilePath::CharType kAccelerometerLocationFileName[] = "location";
50
45 // The filename giving the path to read the scan index of each accelerometer 51 // The filename giving the path to read the scan index of each accelerometer
46 // axis. 52 // axis.
47 const char kAccelerometerScanIndexPath[] = 53 const char kLegacyAccelerometerScanIndexPathFormatString[] =
48 "scan_elements/in_accel_%s_%s_index"; 54 "scan_elements/in_accel_%s_%s_index";
49 55
56 // The filename giving the path to read the scan index of each accelerometer
57 // when they are separate device paths.
58 const char kAccelerometerScanIndexPathFormatString[] =
59 "scan_elements/in_accel_%s_index";
60
50 // The names of the accelerometers. Matches up with the enum AccelerometerSource 61 // The names of the accelerometers. Matches up with the enum AccelerometerSource
51 // in chromeos/accelerometer/accelerometer_types.h. 62 // in chromeos/accelerometer/accelerometer_types.h.
52 const char kAccelerometerNames[ACCELEROMETER_SOURCE_COUNT][5] = {"lid", "base"}; 63 const char kAccelerometerNames[ACCELEROMETER_SOURCE_COUNT][5] = {"lid", "base"};
53 64
54 // The axes on each accelerometer. 65 // The axes on each accelerometer.
flackr 2015/08/27 22:32:11 nit: Add to comment that the order of axes was fli
jonross 2015/08/27 22:59:06 Done.
55 const char kAccelerometerAxes[][2] = {"y", "x", "z"}; 66 const char kAccelerometerAxes[][2] = {"x", "y", "z"};
67 const char kLegacyAccelerometerAxes[][2] = {"y", "x", "z"};
56 68
57 // The length required to read uint values from configuration files. 69 // The length required to read uint values from configuration files.
58 const size_t kMaxAsciiUintLength = 21; 70 const size_t kMaxAsciiUintLength = 21;
59 71
60 // The size of individual values. 72 // The size of individual values.
61 const size_t kDataSize = 2; 73 const size_t kDataSize = 2;
62 74
63 // The mean acceleration due to gravity on Earth in m/s^2. 75 // The mean acceleration due to gravity on Earth in m/s^2.
64 const float kMeanGravity = 9.80665f; 76 const float kMeanGravity = 9.80665f;
65 77
66 // The number of accelerometers.
67 const int kNumberOfAccelerometers = 2;
68
69 // The number of axes for which there are acceleration readings. 78 // The number of axes for which there are acceleration readings.
70 const int kNumberOfAxes = 3; 79 const int kNumberOfAxes = 3;
71 80
72 // The size of data in one reading of the accelerometers. 81 // The size of data in one reading of the accelerometers.
73 const int kSizeOfReading = kDataSize * kNumberOfAccelerometers * kNumberOfAxes; 82 const int kSizeOfReading = kDataSize * kNumberOfAxes;
74 83
75 // Reads |path| to the unsigned int pointed to by |value|. Returns true on 84 // Reads |path| to the unsigned int pointed to by |value|. Returns true on
76 // success or false on failure. 85 // success or false on failure.
77 bool ReadFileToInt(const base::FilePath& path, int* value) { 86 bool ReadFileToInt(const base::FilePath& path, int* value) {
78 std::string s; 87 std::string s;
79 DCHECK(value); 88 DCHECK(value);
80 if (!base::ReadFileToString(path, &s, kMaxAsciiUintLength)) { 89 if (!base::ReadFileToString(path, &s, kMaxAsciiUintLength)) {
81 return false; 90 return false;
82 } 91 }
83 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s); 92 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s);
84 if (!base::StringToInt(s, value)) { 93 if (!base::StringToInt(s, value)) {
85 LOG(ERROR) << "Failed to parse \"" << s << "\" from " << path.value(); 94 LOG(ERROR) << "Failed to parse int \"" << s << "\" from " << path.value();
86 return false; 95 return false;
87 } 96 }
88 return true; 97 return true;
98 }
99
100 // Reads |path| to the double pointed to by |value|. Returns true on success or
101 // false on failure.
102 bool ReadFileToDouble(const base::FilePath& path, double* value) {
103 std::string s;
104 DCHECK(value);
105 if (!base::ReadFileToString(path, &s)) {
106 return false;
107 }
108 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s);
109 if (!base::StringToDouble(s, value)) {
110 LOG(ERROR) << "Failed to parse double \"" << s << "\" from "
111 << path.value();
112 return false;
113 }
114 return true;
89 } 115 }
90 116
91 } // namespace 117 } // namespace
92 118
93 const int AccelerometerReader::kDelayBetweenReadsMs = 100; 119 const int AccelerometerReader::kDelayBetweenReadsMs = 100;
94 120
95 // Work that runs on a base::TaskRunner. It determines the accelerometer 121 // Work that runs on a base::TaskRunner. It determines the accelerometer
96 // configuartion, and reads the data. Upon a successful read it will notify 122 // configuartion, and reads the data. Upon a successful read it will notify
97 // all observers. 123 // all observers.
98 class AccelerometerFileReader 124 class AccelerometerFileReader
(...skipping 11 matching lines...) Expand all
110 // read at the current sampling rate. 136 // read at the current sampling rate.
111 void Read(); 137 void Read();
112 138
113 // Add/Remove observers. 139 // Add/Remove observers.
114 void AddObserver(AccelerometerReader::Observer* observer); 140 void AddObserver(AccelerometerReader::Observer* observer);
115 void RemoveObserver(AccelerometerReader::Observer* observer); 141 void RemoveObserver(AccelerometerReader::Observer* observer);
116 142
117 private: 143 private:
118 friend class base::RefCountedThreadSafe<AccelerometerFileReader>; 144 friend class base::RefCountedThreadSafe<AccelerometerFileReader>;
119 145
146 // Represents necessary information in order to read an accelerometer device.
147 struct ReadingData {
148 // The full path to the accelerometer device to read.
149 base::FilePath path;
150
151 // The accelerometer sources which can be read from |path|.
152 std::vector<AccelerometerSource> sources;
153 };
154
120 // Configuration structure for accelerometer device. 155 // Configuration structure for accelerometer device.
121 struct ConfigurationData { 156 struct ConfigurationData {
122 ConfigurationData(); 157 ConfigurationData();
123 ~ConfigurationData(); 158 ~ConfigurationData();
124 159
125 // Number of accelerometers on device. 160 // Number of accelerometers on device.
126 size_t count; 161 size_t count;
127 162
128 // Length of accelerometer updates.
129 size_t length;
130
131 // Which accelerometers are present on device. 163 // Which accelerometers are present on device.
132 bool has[ACCELEROMETER_SOURCE_COUNT]; 164 bool has[ACCELEROMETER_SOURCE_COUNT];
133 165
134 // Scale of accelerometers (i.e. raw value * scale = m/s^2). 166 // Scale of accelerometers (i.e. raw value * scale = m/s^2).
135 float scale[ACCELEROMETER_SOURCE_COUNT][3]; 167 float scale[ACCELEROMETER_SOURCE_COUNT][3];
136 168
137 // Index of each accelerometer axis in data stream. 169 // Index of each accelerometer axis in data stream.
138 int index[ACCELEROMETER_SOURCE_COUNT][3]; 170 int index[ACCELEROMETER_SOURCE_COUNT][3];
171
172 // The information for each accelerometer device to be read. In kernel 3.18
173 // there is one per ACCELEROMETER_SOURCE_COUNT, on 3.14 there is only one.
174 std::vector<ReadingData> reading_data;
139 }; 175 };
140 176
141 ~AccelerometerFileReader() {} 177 ~AccelerometerFileReader() {}
142 178
179 // When accelerometers are presented as separate iio_devices this will perform
180 // the initialize for one of the devices, at the given |iio_path| and the
181 // symbolic link |name|. |location| is defined by AccelerometerSoure.
182 bool InitializeAccelerometer(const base::FilePath& iio_path,
183 const base::FilePath& name,
184 const std::string& location);
185
186 // TODO(jonross): Separate the initialization into separate files. Add a gyp
187 // rule to have them built for the appropriate kernels. (crbug.com/525658)
188 // When accelerometers are presented as a single iio_device this will perform
189 // the initialization for both of them.
190 bool InitializeLegacyAccelerometers(const base::FilePath& iio_path,
191 const base::FilePath& name);
192
143 // Attempts to read the accelerometer data. Upon a success, converts the raw 193 // Attempts to read the accelerometer data. Upon a success, converts the raw
144 // reading to an AccelerometerUpdate and notifies observers. 194 // reading to an AccelerometerUpdate and notifies observers.
145 void ReadFileAndNotify(); 195 void ReadFileAndNotify();
146 196
147 // True if Initialize completed successfully, and there is an accelerometer 197 // True if Initialize completed successfully, and there is an accelerometer
148 // file to read. 198 // file to read.
149 bool initialization_successful_; 199 bool initialization_successful_;
150 200
151 // The accelerometer configuration. 201 // The accelerometer configuration.
152 ConfigurationData configuration_; 202 ConfigurationData configuration_;
(...skipping 19 matching lines...) Expand all
172 222
173 void AccelerometerFileReader::Initialize( 223 void AccelerometerFileReader::Initialize(
174 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner) { 224 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner) {
175 DCHECK( 225 DCHECK(
176 base::SequencedWorkerPool::GetSequenceTokenForCurrentThread().IsValid()); 226 base::SequencedWorkerPool::GetSequenceTokenForCurrentThread().IsValid());
177 task_runner_ = sequenced_task_runner; 227 task_runner_ = sequenced_task_runner;
178 228
179 // Check for accelerometer symlink which will be created by the udev rules 229 // Check for accelerometer symlink which will be created by the udev rules
180 // file on detecting the device. 230 // file on detecting the device.
181 base::FilePath device; 231 base::FilePath device;
182 if (!base::ReadSymbolicLink(base::FilePath(kAccelerometerDevicePath), 232
183 &device)) { 233 if (base::IsDirectoryEmpty(base::FilePath(kAccelerometerDevicePath))) {
234 LOG(ERROR) << "Accelerometer device directory is empty at "
235 << kAccelerometerDevicePath;
184 return; 236 return;
185 } 237 }
186 238
187 if (!base::PathExists(base::FilePath(kAccelerometerTriggerPath))) { 239 if (!base::PathExists(base::FilePath(kAccelerometerTriggerPath))) {
188 LOG(ERROR) << "Accelerometer trigger does not exist at" 240 LOG(ERROR) << "Accelerometer trigger does not exist at"
189 << kAccelerometerTriggerPath; 241 << kAccelerometerTriggerPath;
190 return; 242 return;
191 } 243 }
192 244
193 base::FilePath iio_path(base::FilePath(kAccelerometerIioBasePath).Append( 245 base::FileEnumerator symlink_dir(base::FilePath(kAccelerometerDevicePath),
194 device)); 246 false, base::FileEnumerator::FILES);
195 247 bool separate_devices = false;
flackr 2015/08/27 22:32:11 nit: s/separate_devices/legacy_cros_accel
jonross 2015/08/27 22:59:06 Done.
196 // Read the scale for all axes. 248 for (base::FilePath name = symlink_dir.Next(); !name.empty();
197 int scale_divisor = 0; 249 name = symlink_dir.Next()) {
198 bool per_source_scale = 250 base::FilePath iio_device;
199 !ReadFileToInt(iio_path.Append(kScaleFileName), &scale_divisor); 251 if (!base::ReadSymbolicLink(name, &iio_device)) {
200 if (!per_source_scale && scale_divisor == 0) { 252 LOG(ERROR) << "Failed to read symbolic link " << kAccelerometerDevicePath
201 LOG(ERROR) << "Accelerometer " << kScaleFileName 253 << "/" << name.MaybeAsASCII() << "\n";
202 << "has scale of 0 and will not be used."; 254 return;
203 return;
204 }
205
206 // Read configuration of each accelerometer axis from each accelerometer from
207 // /sys/bus/iio/devices/iio:deviceX/.
208 for (size_t i = 0; i < arraysize(kAccelerometerNames); ++i) {
209 if (per_source_scale) {
210 configuration_.has[i] = false;
211 // Read scale of accelerometer.
212 std::string accelerometer_scale_path = base::StringPrintf(
213 kSourceScaleNameFormatString, kAccelerometerNames[i]);
214 if (!ReadFileToInt(iio_path.Append(accelerometer_scale_path.c_str()),
215 &scale_divisor)) {
216 continue;
217 }
218 if (scale_divisor == 0) {
219 LOG(ERROR) << "Accelerometer " << accelerometer_scale_path
220 << "has scale of 0 and will not be used.";
221 continue;
222 }
223 } 255 }
224 256
225 configuration_.has[i] = true; 257 base::FilePath iio_path(base::FilePath(kAccelerometerIioBasePath)
226 for (size_t j = 0; j < arraysize(kAccelerometerAxes); ++j) { 258 .Append(iio_device.BaseName()));
227 configuration_.scale[i][j] = kMeanGravity / scale_divisor; 259 std::string location;
228 std::string accelerometer_index_path = base::StringPrintf( 260 separate_devices = base::ReadFileToString(
229 kAccelerometerScanIndexPath, kAccelerometerAxes[j], 261 base::FilePath(iio_path).Append(kAccelerometerLocationFileName),
230 kAccelerometerNames[i]); 262 &location);
231 if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()), 263 if (separate_devices) {
232 &(configuration_.index[i][j]))) { 264 base::TrimWhitespaceASCII(location, base::TRIM_ALL, &location);
233 configuration_.has[i] = false; 265 if (!InitializeAccelerometer(iio_path, name, location))
234 break; 266 return;
235 } 267 } else {
268 if (!InitializeLegacyAccelerometers(iio_path, name))
269 return;
236 } 270 }
237 if (configuration_.has[i])
238 configuration_.count++;
239 }
240
241 // Adjust the directions of accelerometers to match the AccelerometerUpdate
242 // type specified in chromeos/accelerometer/accelerometer_types.h.
243 configuration_.scale[ACCELEROMETER_SOURCE_SCREEN][0] *= -1.0f;
244 for (int i = 0; i < 3; ++i) {
245 configuration_.scale[ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD][i] *= -1.0f;
246 } 271 }
247 272
248 // Verify indices are within bounds. 273 // Verify indices are within bounds.
249 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { 274 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) {
250 if (!configuration_.has[i]) 275 if (!configuration_.has[i])
251 continue; 276 continue;
252 for (int j = 0; j < 3; ++j) { 277 for (int j = 0; j < 3; ++j) {
253 if (configuration_.index[i][j] < 0 || 278 if (configuration_.index[i][j] < 0 ||
254 configuration_.index[i][j] >= 279 configuration_.index[i][j] >=
255 3 * static_cast<int>(configuration_.count)) { 280 3 * static_cast<int>(configuration_.count)) {
281 const char* axis = separate_devices ? kAccelerometerAxes[j]
282 : kLegacyAccelerometerAxes[j];
256 LOG(ERROR) << "Field index for " << kAccelerometerNames[i] << " " 283 LOG(ERROR) << "Field index for " << kAccelerometerNames[i] << " "
257 << kAccelerometerAxes[j] << " axis out of bounds."; 284 << axis << " axis out of bounds.";
258 return; 285 return;
259 } 286 }
260 } 287 }
261 } 288 }
262 configuration_.length = kDataSize * 3 * configuration_.count; 289
263 initialization_successful_ = true; 290 initialization_successful_ = true;
264 Read(); 291 Read();
265 } 292 }
266 293
267 void AccelerometerFileReader::Read() { 294 void AccelerometerFileReader::Read() {
268 DCHECK( 295 DCHECK(
269 base::SequencedWorkerPool::GetSequenceTokenForCurrentThread().IsValid()); 296 base::SequencedWorkerPool::GetSequenceTokenForCurrentThread().IsValid());
270 ReadFileAndNotify(); 297 ReadFileAndNotify();
271 task_runner_->PostNonNestableDelayedTask( 298 task_runner_->PostNonNestableDelayedTask(
272 FROM_HERE, base::Bind(&AccelerometerFileReader::Read, this), 299 FROM_HERE, base::Bind(&AccelerometerFileReader::Read, this),
273 base::TimeDelta::FromMilliseconds( 300 base::TimeDelta::FromMilliseconds(
274 AccelerometerReader::kDelayBetweenReadsMs)); 301 AccelerometerReader::kDelayBetweenReadsMs));
275 } 302 }
276 303
277 void AccelerometerFileReader::AddObserver( 304 void AccelerometerFileReader::AddObserver(
278 AccelerometerReader::Observer* observer) { 305 AccelerometerReader::Observer* observer) {
279 observers_->AddObserver(observer); 306 observers_->AddObserver(observer);
280 if (initialization_successful_) { 307 if (initialization_successful_) {
281 task_runner_->PostNonNestableTask( 308 task_runner_->PostNonNestableTask(
282 FROM_HERE, 309 FROM_HERE,
283 base::Bind(&AccelerometerFileReader::ReadFileAndNotify, this)); 310 base::Bind(&AccelerometerFileReader::ReadFileAndNotify, this));
284 } 311 }
285 } 312 }
286 313
287 void AccelerometerFileReader::RemoveObserver( 314 void AccelerometerFileReader::RemoveObserver(
288 AccelerometerReader::Observer* observer) { 315 AccelerometerReader::Observer* observer) {
289 observers_->RemoveObserver(observer); 316 observers_->RemoveObserver(observer);
290 } 317 }
291 318
319 bool AccelerometerFileReader::InitializeAccelerometer(
320 const base::FilePath& iio_path,
321 const base::FilePath& name,
322 const std::string& location) {
323 size_t config_index = 0;
324 for (; config_index < arraysize(kAccelerometerNames); ++config_index) {
325 if (location == kAccelerometerNames[config_index])
326 break;
327 }
328
329 if (config_index >= arraysize(kAccelerometerNames)) {
330 LOG(ERROR) << "Unrecognized location: " << location << " for device "
331 << name.MaybeAsASCII() << "\n";
332 return false;
333 }
334
335 double scale;
336 if (!ReadFileToDouble(iio_path.Append(kAccelerometerScaleFileName), &scale))
337 return false;
338
339 const int kNumberAxes = arraysize(kAccelerometerAxes);
340 for (size_t i = 0; i < kNumberAxes; ++i) {
341 std::string accelerometer_index_path = base::StringPrintf(
342 kAccelerometerScanIndexPathFormatString, kAccelerometerAxes[i]);
343 if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()),
344 &(configuration_.index[config_index][i]))) {
345 LOG(ERROR) << "Index file " << accelerometer_index_path
346 << " could not be parsed\n";
347 return false;
348 }
349 configuration_.scale[config_index][i] = scale;
350 }
351 configuration_.has[config_index] = true;
352 configuration_.count++;
353
354 ReadingData reading_data;
355 reading_data.path =
356 base::FilePath(kAccelerometerDevicePath).Append(name.BaseName());
357 reading_data.sources.push_back(
358 static_cast<AccelerometerSource>(config_index));
359
360 configuration_.reading_data.push_back(reading_data);
361
362 return true;
363 }
364
365 bool AccelerometerFileReader::InitializeLegacyAccelerometers(
366 const base::FilePath& iio_path,
367 const base::FilePath& name) {
368 ReadingData reading_data;
369 reading_data.path =
370 base::FilePath(kAccelerometerDevicePath).Append(name.BaseName());
371 // Read the scale for all axes.
372 int scale_divisor = 0;
flackr 2015/08/27 22:32:11 nit: Move declaration to for loop scope right befo
jonross 2015/08/27 22:59:06 Done.
373 // Read configuration of each accelerometer axis from each accelerometer from
374 // /sys/bus/iio/devices/iio:deviceX/.
375 for (size_t i = 0; i < arraysize(kAccelerometerNames); ++i) {
376 configuration_.has[i] = false;
377 // Read scale of accelerometer.
378 std::string accelerometer_scale_path = base::StringPrintf(
379 kLegacyScaleNameFormatString, kAccelerometerNames[i]);
380 if (!ReadFileToInt(iio_path.Append(accelerometer_scale_path.c_str()),
381 &scale_divisor)) {
382 continue;
383 }
384 if (scale_divisor == 0) {
385 LOG(ERROR) << "Accelerometer " << accelerometer_scale_path
386 << "has scale of 0 and will not be used.";
387 continue;
388 }
389
390 configuration_.has[i] = true;
391 for (size_t j = 0; j < arraysize(kLegacyAccelerometerAxes); ++j) {
392 configuration_.scale[i][j] = kMeanGravity / scale_divisor;
393 std::string accelerometer_index_path = base::StringPrintf(
394 kLegacyAccelerometerScanIndexPathFormatString,
395 kLegacyAccelerometerAxes[j], kAccelerometerNames[i]);
396 if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()),
397 &(configuration_.index[i][j]))) {
398 configuration_.has[i] = false;
399 LOG(ERROR) << "Index file " << accelerometer_index_path
400 << " could not be parsed\n";
401 return false;
402 }
403 }
404 if (configuration_.has[i]) {
405 configuration_.count++;
406 reading_data.sources.push_back(static_cast<AccelerometerSource>(i));
407 }
408 }
409
410 // Adjust the directions of accelerometers to match the AccelerometerUpdate
411 // type specified in chromeos/accelerometer/accelerometer_types.h.
412 configuration_.scale[ACCELEROMETER_SOURCE_SCREEN][1] *= -1.0f;
413 configuration_.scale[ACCELEROMETER_SOURCE_SCREEN][2] *= -1.0f;
414
415 configuration_.reading_data.push_back(reading_data);
416 return true;
417 }
418
292 void AccelerometerFileReader::ReadFileAndNotify() { 419 void AccelerometerFileReader::ReadFileAndNotify() {
293 DCHECK(initialization_successful_); 420 DCHECK(initialization_successful_);
294 char reading[kSizeOfReading];
295 421
296 // Initiate the trigger to read accelerometers simultaneously 422 // Initiate the trigger to read accelerometers simultaneously
297 int bytes_written = base::WriteFile( 423 int bytes_written = base::WriteFile(
298 base::FilePath(kAccelerometerTriggerPath), "1\n", 2); 424 base::FilePath(kAccelerometerTriggerPath), "1\n", 2);
299 if (bytes_written < 2) { 425 if (bytes_written < 2) {
300 PLOG(ERROR) << "Accelerometer trigger failure: " << bytes_written; 426 PLOG(ERROR) << "Accelerometer trigger failure: " << bytes_written;
301 return; 427 return;
302 } 428 }
303 429
304 // Read resulting sample from /dev/cros-ec-accel. 430 // Read resulting sample from /dev/cros-ec-accel.
305 int bytes_read = base::ReadFile(base::FilePath(kAccelerometerDevicePath),
306 reading, configuration_.length);
307 if (bytes_read < static_cast<int>(configuration_.length)) {
308 LOG(ERROR) << "Read " << bytes_read << " byte(s), expected "
309 << configuration_.length << " bytes from accelerometer";
310 return;
311 }
312
313 update_ = new AccelerometerUpdate(); 431 update_ = new AccelerometerUpdate();
314 432 for (auto reading_data : configuration_.reading_data) {
315 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { 433 int reading_size = reading_data.sources.size() * kSizeOfReading;
316 if (!configuration_.has[i]) 434 DCHECK_GT(reading_size, 0);
317 continue; 435 char reading[reading_size];
318 436 int bytes_read = base::ReadFile(reading_data.path, reading, reading_size);
319 int16* values = reinterpret_cast<int16*>(reading); 437 if (bytes_read < reading_size) {
320 update_->Set( 438 LOG(ERROR) << "Accelerometer Read " << bytes_read << " byte(s), expected "
321 static_cast<AccelerometerSource>(i), 439 << reading_size << " bytes from accelerometer "
322 values[configuration_.index[i][0]] * configuration_.scale[i][0], 440 << reading_data.path.MaybeAsASCII();
323 values[configuration_.index[i][1]] * configuration_.scale[i][1], 441 return;
324 values[configuration_.index[i][2]] * configuration_.scale[i][2]); 442 }
443 for (AccelerometerSource source : reading_data.sources) {
444 if (!configuration_.has[source])
445 continue;
446 int16* values = reinterpret_cast<int16*>(reading);
447 update_->Set(source, values[configuration_.index[source][0]] *
448 configuration_.scale[source][0],
449 values[configuration_.index[source][1]] *
450 configuration_.scale[source][1],
451 values[configuration_.index[source][2]] *
452 configuration_.scale[source][2]);
453 }
325 } 454 }
326 455
327 observers_->Notify(FROM_HERE, 456 observers_->Notify(FROM_HERE,
328 &AccelerometerReader::Observer::OnAccelerometerUpdated, 457 &AccelerometerReader::Observer::OnAccelerometerUpdated,
329 update_); 458 update_);
330 } 459 }
331 460
332 AccelerometerFileReader::ConfigurationData::ConfigurationData() : count(0) { 461 AccelerometerFileReader::ConfigurationData::ConfigurationData() : count(0) {
333 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { 462 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) {
334 has[i] = false; 463 has[i] = false;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 } 496 }
368 497
369 AccelerometerReader::AccelerometerReader() 498 AccelerometerReader::AccelerometerReader()
370 : accelerometer_file_reader_(new AccelerometerFileReader()) { 499 : accelerometer_file_reader_(new AccelerometerFileReader()) {
371 } 500 }
372 501
373 AccelerometerReader::~AccelerometerReader() { 502 AccelerometerReader::~AccelerometerReader() {
374 } 503 }
375 504
376 } // namespace chromeos 505 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698