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

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: TODO gyp separation 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[] =
Daniel Erat 2015/08/27 15:36:19 marginal nit: i think i'd prefer if this constant
jonross 2015/08/27 19:20:57 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;
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.
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;
flackr 2015/08/27 16:24:22 Instead of length and offset, if we have std::vect
jonross 2015/08/27 19:20:57 Done.
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 // TODO(jonross): Separate the initialization into separate files. Add a gyp
199 // rule to have them built for the appropriate kernels. (crbug.com/525658)
200 // When accelerometers are presented as a single iio_device this will perform
201 // the initialization for both of them.
202 bool InitializeLegacyAccelerometers(const base::FilePath& iio_path,
203 const base::FilePath& name);
204
143 // Attempts to read the accelerometer data. Upon a success, converts the raw 205 // Attempts to read the accelerometer data. Upon a success, converts the raw
144 // reading to an AccelerometerUpdate and notifies observers. 206 // reading to an AccelerometerUpdate and notifies observers.
145 void ReadFileAndNotify(); 207 void ReadFileAndNotify();
146 208
147 // True if Initialize completed successfully, and there is an accelerometer 209 // True if Initialize completed successfully, and there is an accelerometer
148 // file to read. 210 // file to read.
149 bool initialization_successful_; 211 bool initialization_successful_;
150 212
151 // The accelerometer configuration. 213 // The accelerometer configuration.
152 ConfigurationData configuration_; 214 ConfigurationData configuration_;
(...skipping 19 matching lines...) Expand all
172 234
173 void AccelerometerFileReader::Initialize( 235 void AccelerometerFileReader::Initialize(
174 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner) { 236 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner) {
175 DCHECK( 237 DCHECK(
176 base::SequencedWorkerPool::GetSequenceTokenForCurrentThread().IsValid()); 238 base::SequencedWorkerPool::GetSequenceTokenForCurrentThread().IsValid());
177 task_runner_ = sequenced_task_runner; 239 task_runner_ = sequenced_task_runner;
178 240
179 // Check for accelerometer symlink which will be created by the udev rules 241 // Check for accelerometer symlink which will be created by the udev rules
180 // file on detecting the device. 242 // file on detecting the device.
181 base::FilePath device; 243 base::FilePath device;
182 if (!base::ReadSymbolicLink(base::FilePath(kAccelerometerDevicePath), 244
183 &device)) { 245 if (base::IsDirectoryEmpty(base::FilePath(kAccelerometerDevicePath))) {
246 LOG(ERROR) << "Accelerometer device directory is empty at "
247 << kAccelerometerDevicePath;
184 return; 248 return;
185 } 249 }
186 250
187 if (!base::PathExists(base::FilePath(kAccelerometerTriggerPath))) { 251 if (!base::PathExists(base::FilePath(kAccelerometerTriggerPath))) {
188 LOG(ERROR) << "Accelerometer trigger does not exist at" 252 LOG(ERROR) << "Accelerometer trigger does not exist at"
189 << kAccelerometerTriggerPath; 253 << kAccelerometerTriggerPath;
190 return; 254 return;
191 } 255 }
192 256
193 base::FilePath iio_path(base::FilePath(kAccelerometerIioBasePath).Append( 257 base::FileEnumerator symlink_dir(base::FilePath(kAccelerometerDevicePath),
194 device)); 258 false, base::FileEnumerator::FILES);
195 259 bool separate_devices = false;
196 // Read the scale for all axes. 260 for (base::FilePath name = symlink_dir.Next(); !name.empty();
197 int scale_divisor = 0; 261 name = symlink_dir.Next()) {
198 bool per_source_scale = 262 base::FilePath iio_device;
199 !ReadFileToInt(iio_path.Append(kScaleFileName), &scale_divisor); 263 if (!base::ReadSymbolicLink(name, &iio_device)) {
200 if (!per_source_scale && scale_divisor == 0) { 264 LOG(ERROR) << "Failed to read symbolic link " << kAccelerometerDevicePath
201 LOG(ERROR) << "Accelerometer " << kScaleFileName 265 << "/" << name.MaybeAsASCII() << "\n";
202 << "has scale of 0 and will not be used."; 266 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 } 267 }
224 268
225 configuration_.has[i] = true; 269 base::FilePath iio_path(base::FilePath(kAccelerometerIioBasePath)
226 for (size_t j = 0; j < arraysize(kAccelerometerAxes); ++j) { 270 .Append(iio_device.BaseName()));
227 configuration_.scale[i][j] = kMeanGravity / scale_divisor; 271 std::string location;
228 std::string accelerometer_index_path = base::StringPrintf( 272 separate_devices = base::ReadFileToString(
229 kAccelerometerScanIndexPath, kAccelerometerAxes[j], 273 base::FilePath(iio_path).Append(kAccelerometerLocationFileName),
230 kAccelerometerNames[i]); 274 &location);
231 if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()), 275 if (separate_devices) {
232 &(configuration_.index[i][j]))) { 276 base::TrimWhitespaceASCII(location, base::TRIM_ALL, &location);
233 configuration_.has[i] = false; 277 if (!InitializeAccelerometer(iio_path, name, location))
234 break; 278 return;
235 } 279 } else {
280 if (!InitializeLegacyAccelerometers(iio_path, name))
281 return;
236 } 282 }
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 } 283 }
247 284
248 // Verify indices are within bounds. 285 // Verify indices are within bounds.
249 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { 286 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) {
250 if (!configuration_.has[i]) 287 if (!configuration_.has[i])
251 continue; 288 continue;
252 for (int j = 0; j < 3; ++j) { 289 for (int j = 0; j < 3; ++j) {
253 if (configuration_.index[i][j] < 0 || 290 if (configuration_.index[i][j] < 0 ||
254 configuration_.index[i][j] >= 291 configuration_.index[i][j] >=
255 3 * static_cast<int>(configuration_.count)) { 292 3 * static_cast<int>(configuration_.count)) {
293 const char* axis = separate_devices ? kSeparateAccelerometerAxes[j]
294 : kUnifiedAccelerometerAxes[j];
256 LOG(ERROR) << "Field index for " << kAccelerometerNames[i] << " " 295 LOG(ERROR) << "Field index for " << kAccelerometerNames[i] << " "
257 << kAccelerometerAxes[j] << " axis out of bounds."; 296 << axis << " axis out of bounds.";
258 return; 297 return;
259 } 298 }
260 } 299 }
261 } 300 }
262 configuration_.length = kDataSize * 3 * configuration_.count; 301
263 initialization_successful_ = true; 302 initialization_successful_ = true;
264 Read(); 303 Read();
265 } 304 }
266 305
267 void AccelerometerFileReader::Read() { 306 void AccelerometerFileReader::Read() {
268 DCHECK( 307 DCHECK(
269 base::SequencedWorkerPool::GetSequenceTokenForCurrentThread().IsValid()); 308 base::SequencedWorkerPool::GetSequenceTokenForCurrentThread().IsValid());
270 ReadFileAndNotify(); 309 ReadFileAndNotify();
271 task_runner_->PostNonNestableDelayedTask( 310 task_runner_->PostNonNestableDelayedTask(
272 FROM_HERE, base::Bind(&AccelerometerFileReader::Read, this), 311 FROM_HERE, base::Bind(&AccelerometerFileReader::Read, this),
273 base::TimeDelta::FromMilliseconds( 312 base::TimeDelta::FromMilliseconds(
274 AccelerometerReader::kDelayBetweenReadsMs)); 313 AccelerometerReader::kDelayBetweenReadsMs));
275 } 314 }
276 315
277 void AccelerometerFileReader::AddObserver( 316 void AccelerometerFileReader::AddObserver(
278 AccelerometerReader::Observer* observer) { 317 AccelerometerReader::Observer* observer) {
279 observers_->AddObserver(observer); 318 observers_->AddObserver(observer);
280 if (initialization_successful_) { 319 if (initialization_successful_) {
281 task_runner_->PostNonNestableTask( 320 task_runner_->PostNonNestableTask(
282 FROM_HERE, 321 FROM_HERE,
283 base::Bind(&AccelerometerFileReader::ReadFileAndNotify, this)); 322 base::Bind(&AccelerometerFileReader::ReadFileAndNotify, this));
284 } 323 }
285 } 324 }
286 325
287 void AccelerometerFileReader::RemoveObserver( 326 void AccelerometerFileReader::RemoveObserver(
288 AccelerometerReader::Observer* observer) { 327 AccelerometerReader::Observer* observer) {
289 observers_->RemoveObserver(observer); 328 observers_->RemoveObserver(observer);
290 } 329 }
291 330
331 bool AccelerometerFileReader::InitializeAccelerometer(
332 const base::FilePath& iio_path,
333 const base::FilePath& name,
334 const std::string& location) {
335 int config_index = std::find(std::begin(kAccelerometerNames),
Daniel Erat 2015/08/27 15:36:19 huh, i hadn't seen this before. just to double-che
336 std::end(kAccelerometerNames), location) -
337 std::begin(kAccelerometerNames);
Daniel Erat 2015/08/27 15:36:19 is it safe to subtract std::begin from std::last (
flackr 2015/08/27 16:24:22 std::find returns the second argument (the end pos
jonross 2015/08/27 19:20:57 Yeah I thought std:find would have been cleaner th
338 if (config_index == -1) {
339 LOG(ERROR) << "Unrecognized location: " << location << " for device "
340 << name.MaybeAsASCII() << "\n";
341 return false;
342 }
343
344 double scale;
345 if (!ReadFileToDouble(iio_path.Append(kAccelerometerScaleFileName), &scale))
346 return false;
347
348 const int number_axes = arraysize(kSeparateAccelerometerAxes);
Daniel Erat 2015/08/27 15:36:19 nit: kNumberAxes
jonross 2015/08/27 19:20:57 Done.
349 for (size_t j = 0; j < number_axes; ++j) {
Daniel Erat 2015/08/27 15:36:19 nit: s/j/i/
jonross 2015/08/27 19:20:57 Done.
350 std::string accelerometer_index_path = base::StringPrintf(
351 kSeparateAccelerometerScanIndexPath, kSeparateAccelerometerAxes[j]);
352 if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()),
353 &(configuration_.index[config_index][j]))) {
354 LOG(ERROR) << "Index file " << accelerometer_index_path
355 << " could not be parsed\n";
356 return false;
357 }
358 configuration_.index[config_index][j] += config_index * number_axes;
359 configuration_.scale[config_index][j] = scale;
360 }
361 configuration_.has[config_index] = true;
362 configuration_.count++;
363
364 ReadingData reading_data;
365 reading_data.path =
366 base::FilePath(kAccelerometerDevicePath).Append(name.BaseName());
367 reading_data.offset = config_index * kDataSizeForSeparateDevices;
368 reading_data.length = kDataSizeForSeparateDevices;
369
370 configuration_.reading_data.push_back(reading_data);
371
372 return true;
373 }
374
375 bool AccelerometerFileReader::InitializeLegacyAccelerometers(
376 const base::FilePath& iio_path,
377 const base::FilePath& name) {
378 // Read the scale for all axes.
379 int scale_divisor = 0;
380 // Read configuration of each accelerometer axis from each accelerometer from
381 // /sys/bus/iio/devices/iio:deviceX/.
382 for (size_t i = 0; i < arraysize(kAccelerometerNames); ++i) {
383 configuration_.has[i] = false;
384 // Read scale of accelerometer.
385 std::string accelerometer_scale_path = base::StringPrintf(
386 kSourceScaleNameFormatString, kAccelerometerNames[i]);
387 if (!ReadFileToInt(iio_path.Append(accelerometer_scale_path.c_str()),
388 &scale_divisor)) {
389 continue;
390 }
391 if (scale_divisor == 0) {
392 LOG(ERROR) << "Accelerometer " << accelerometer_scale_path
393 << "has scale of 0 and will not be used.";
394 continue;
395 }
396
397 configuration_.has[i] = true;
398 for (size_t j = 0; j < arraysize(kUnifiedAccelerometerAxes); ++j) {
399 configuration_.scale[i][j] = kMeanGravity / scale_divisor;
400 std::string accelerometer_index_path = base::StringPrintf(
401 kAccelerometerScanIndexPath, kUnifiedAccelerometerAxes[j],
402 kAccelerometerNames[i]);
403 if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()),
404 &(configuration_.index[i][j]))) {
405 configuration_.has[i] = false;
406 LOG(ERROR) << "Index file " << accelerometer_index_path
407 << " could not be parsed\n";
408 return false;
409 }
410 }
411 if (configuration_.has[i])
412 configuration_.count++;
413 }
414
415 // Adjust the directions of accelerometers to match the AccelerometerUpdate
416 // type specified in chromeos/accelerometer/accelerometer_types.h.
417 configuration_.scale[ACCELEROMETER_SOURCE_SCREEN][1] *= -1.0f;
418 configuration_.scale[ACCELEROMETER_SOURCE_SCREEN][2] *= -1.0f;
419
420 ReadingData reading_data;
421 reading_data.path =
422 base::FilePath(kAccelerometerDevicePath).Append(name.BaseName());
423 reading_data.offset = 0;
424 reading_data.length = kDataSize * 3 * configuration_.count;
425
426 configuration_.reading_data.push_back(reading_data);
427 return true;
428 }
429
292 void AccelerometerFileReader::ReadFileAndNotify() { 430 void AccelerometerFileReader::ReadFileAndNotify() {
293 DCHECK(initialization_successful_); 431 DCHECK(initialization_successful_);
294 char reading[kSizeOfReading];
295 432
296 // Initiate the trigger to read accelerometers simultaneously 433 // Initiate the trigger to read accelerometers simultaneously
297 int bytes_written = base::WriteFile( 434 int bytes_written = base::WriteFile(
298 base::FilePath(kAccelerometerTriggerPath), "1\n", 2); 435 base::FilePath(kAccelerometerTriggerPath), "1\n", 2);
299 if (bytes_written < 2) { 436 if (bytes_written < 2) {
300 PLOG(ERROR) << "Accelerometer trigger failure: " << bytes_written; 437 PLOG(ERROR) << "Accelerometer trigger failure: " << bytes_written;
301 return; 438 return;
302 } 439 }
303 440
304 // Read resulting sample from /dev/cros-ec-accel. 441 // Read resulting sample from /dev/cros-ec-accel.
305 int bytes_read = base::ReadFile(base::FilePath(kAccelerometerDevicePath), 442 char reading[kSizeOfReading];
306 reading, configuration_.length); 443 int bytes_read;
Daniel Erat 2015/08/27 15:36:19 nit: scope this within the for loop
jonross 2015/08/27 19:20:57 Done.
307 if (bytes_read < static_cast<int>(configuration_.length)) { 444 for (auto reading_data : configuration_.reading_data) {
308 LOG(ERROR) << "Read " << bytes_read << " byte(s), expected " 445 bytes_read = base::ReadFile(
309 << configuration_.length << " bytes from accelerometer"; 446 reading_data.path, reading + reading_data.offset, reading_data.length);
310 return; 447 if (bytes_read < static_cast<int>(reading_data.length)) {
448 LOG(ERROR) << "Accelerometer Read " << bytes_read << " byte(s), expected "
449 << reading_data.length << " bytes from accelerometer "
450 << reading_data.path.MaybeAsASCII();
451 return;
452 }
311 } 453 }
312 454
313 update_ = new AccelerometerUpdate(); 455 update_ = new AccelerometerUpdate();
314 456
315 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { 457 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) {
316 if (!configuration_.has[i]) 458 if (!configuration_.has[i])
317 continue; 459 continue;
318 460
319 int16* values = reinterpret_cast<int16*>(reading); 461 int16* values = reinterpret_cast<int16*>(reading);
320 update_->Set( 462 update_->Set(
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 } 509 }
368 510
369 AccelerometerReader::AccelerometerReader() 511 AccelerometerReader::AccelerometerReader()
370 : accelerometer_file_reader_(new AccelerometerFileReader()) { 512 : accelerometer_file_reader_(new AccelerometerFileReader()) {
371 } 513 }
372 514
373 AccelerometerReader::~AccelerometerReader() { 515 AccelerometerReader::~AccelerometerReader() {
374 } 516 }
375 517
376 } // namespace chromeos 518 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698