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

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

Powered by Google App Engine
This is Rietveld 408576698