OLD | NEW |
---|---|
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> |
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 kSourceScaleNameFormatString[] = |
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 kAccelerometerScanIndexPath[] = |
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 kSeparateAccelerometerScanIndexPath[] = | |
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. |
55 const char kAccelerometerAxes[][2] = {"y", "x", "z"}; | 66 const char kSeparateAccelerometerAxes[][2] = {"x", "y", "z"}; |
67 const char kUnifiedAccelerometerAxes[][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 |
75 // The size of reading values for an entire accelerometer, when they are | |
76 // separate device paths. | |
77 const size_t kDataSizeForSeparateDevices = 6; | |
78 | |
63 // The mean acceleration due to gravity on Earth in m/s^2. | 79 // The mean acceleration due to gravity on Earth in m/s^2. |
64 const float kMeanGravity = 9.80665f; | 80 const float kMeanGravity = 9.80665f; |
65 | 81 |
66 // The number of accelerometers. | 82 // The number of accelerometers. |
67 const int kNumberOfAccelerometers = 2; | 83 const int kNumberOfAccelerometers = 2; |
68 | 84 |
69 // The number of axes for which there are acceleration readings. | 85 // The number of axes for which there are acceleration readings. |
70 const int kNumberOfAxes = 3; | 86 const int kNumberOfAxes = 3; |
71 | 87 |
72 // The size of data in one reading of the accelerometers. | 88 // The size of data in one reading of the accelerometers. |
73 const int kSizeOfReading = kDataSize * kNumberOfAccelerometers * kNumberOfAxes; | 89 const int kSizeOfReading = kDataSize * kNumberOfAccelerometers * kNumberOfAxes; |
74 | 90 |
75 // Reads |path| to the unsigned int pointed to by |value|. Returns true on | 91 // Reads |path| to the unsigned int pointed to by |value|. Returns true on |
76 // success or false on failure. | 92 // success or false on failure. |
77 bool ReadFileToInt(const base::FilePath& path, int* value) { | 93 bool ReadFileToInt(const base::FilePath& path, int* value) { |
78 std::string s; | 94 std::string s; |
79 DCHECK(value); | 95 DCHECK(value); |
80 if (!base::ReadFileToString(path, &s, kMaxAsciiUintLength)) { | 96 if (!base::ReadFileToString(path, &s, kMaxAsciiUintLength)) { |
81 return false; | 97 return false; |
82 } | 98 } |
83 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s); | 99 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s); |
84 if (!base::StringToInt(s, value)) { | 100 if (!base::StringToInt(s, value)) { |
85 LOG(ERROR) << "Failed to parse \"" << s << "\" from " << path.value(); | 101 LOG(ERROR) << "Failed to parse int \"" << s << "\" from " << path.value(); |
86 return false; | 102 return false; |
87 } | 103 } |
88 return true; | 104 return true; |
105 } | |
106 | |
107 // Reads |path| to the double pointed to by |value|. Returns true on success or | |
108 // false on failure. | |
109 bool ReadFileToDouble(const base::FilePath& path, double* value) { | |
110 std::string s; | |
111 DCHECK(value); | |
112 if (!base::ReadFileToString(path, &s)) { | |
113 return false; | |
114 } | |
115 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s); | |
116 if (!base::StringToDouble(s, value)) { | |
117 LOG(ERROR) << "Failed to parse double \"" << s << "\" from " | |
118 << path.value(); | |
119 return false; | |
120 } | |
121 return true; | |
89 } | 122 } |
90 | 123 |
91 } // namespace | 124 } // namespace |
92 | 125 |
93 const int AccelerometerReader::kDelayBetweenReadsMs = 100; | 126 const int AccelerometerReader::kDelayBetweenReadsMs = 100; |
94 | 127 |
95 // Work that runs on a base::TaskRunner. It determines the accelerometer | 128 // Work that runs on a base::TaskRunner. It determines the accelerometer |
96 // configuartion, and reads the data. Upon a successful read it will notify | 129 // configuartion, and reads the data. Upon a successful read it will notify |
97 // all observers. | 130 // all observers. |
98 class AccelerometerFileReader | 131 class AccelerometerFileReader |
(...skipping 30 matching lines...) Expand all Loading... | |
129 size_t length; | 162 size_t length; |
130 | 163 |
131 // Which accelerometers are present on device. | 164 // Which accelerometers are present on device. |
132 bool has[ACCELEROMETER_SOURCE_COUNT]; | 165 bool has[ACCELEROMETER_SOURCE_COUNT]; |
133 | 166 |
134 // Scale of accelerometers (i.e. raw value * scale = m/s^2). | 167 // Scale of accelerometers (i.e. raw value * scale = m/s^2). |
135 float scale[ACCELEROMETER_SOURCE_COUNT][3]; | 168 float scale[ACCELEROMETER_SOURCE_COUNT][3]; |
136 | 169 |
137 // Index of each accelerometer axis in data stream. | 170 // Index of each accelerometer axis in data stream. |
138 int index[ACCELEROMETER_SOURCE_COUNT][3]; | 171 int index[ACCELEROMETER_SOURCE_COUNT][3]; |
172 | |
173 // If true the accelerometer devices must be read from distinct files. | |
174 bool read_device_separately; | |
175 | |
176 // Length of reading per accelerometer | |
177 size_t device_length[ACCELEROMETER_SOURCE_COUNT]; | |
178 | |
179 // The path to append to |kAccelerometerDevicePath| where the accelerometer | |
180 // device resides. | |
181 base::FilePath device_path[ACCELEROMETER_SOURCE_COUNT]; | |
139 }; | 182 }; |
140 | 183 |
141 ~AccelerometerFileReader() {} | 184 ~AccelerometerFileReader() {} |
142 | 185 |
186 // When accelerometers are presented as separate iio_devices this will perform | |
187 // the initialize for one of the devices, at the given |iio_path| and the | |
188 // symbolic link |name|. |location| is defined by AccelerometerSoure. | |
189 bool InitializeDevice(const base::FilePath& iio_path, | |
190 const base::FilePath& name, | |
191 const std::string& location); | |
192 | |
193 // When accelerometers are presented as a single iio_device this will perform | |
194 // the initialization for both of them. | |
195 bool InitializeBothAccelerometers(const base::FilePath& iio_path); | |
196 | |
143 // Attempts to read the accelerometer data. Upon a success, converts the raw | 197 // Attempts to read the accelerometer data. Upon a success, converts the raw |
144 // reading to an AccelerometerUpdate and notifies observers. | 198 // reading to an AccelerometerUpdate and notifies observers. |
145 void ReadFileAndNotify(); | 199 void ReadFileAndNotify(); |
146 | 200 |
201 // When accelerometers are presented as separate iio_devices this will perform | |
202 // the read of both devices, from their separate locations. | |
203 bool ReadSeparateDevices(); | |
204 | |
205 // When accelerometers are presented as a single iio_device this will perform | |
206 // the read of the device. | |
207 bool ReadUnifiedDevice(); | |
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_; |
153 | 215 |
154 // The observers to notify of accelerometer updates. | 216 // The observers to notify of accelerometer updates. |
155 scoped_refptr<base::ObserverListThreadSafe<AccelerometerReader::Observer>> | 217 scoped_refptr<base::ObserverListThreadSafe<AccelerometerReader::Observer>> |
156 observers_; | 218 observers_; |
(...skipping 15 matching lines...) Expand all Loading... | |
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 for (base::FilePath name = symlink_dir.Next(); !name.empty(); |
196 // Read the scale for all axes. | 260 name = symlink_dir.Next()) { |
197 int scale_divisor = 0; | 261 base::FilePath iio_device; |
198 bool per_source_scale = | 262 if (!base::ReadSymbolicLink(name, &iio_device)) { |
199 !ReadFileToInt(iio_path.Append(kScaleFileName), &scale_divisor); | 263 LOG(ERROR) << "Failed to read symbolic link " << kAccelerometerDevicePath |
200 if (!per_source_scale && scale_divisor == 0) { | 264 << "/" << name.MaybeAsASCII() << "\n"; |
201 LOG(ERROR) << "Accelerometer " << kScaleFileName | 265 return; |
202 << "has scale of 0 and will not be used."; | |
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 } | 266 } |
224 | 267 |
225 configuration_.has[i] = true; | 268 base::FilePath iio_path(base::FilePath(kAccelerometerIioBasePath) |
226 for (size_t j = 0; j < arraysize(kAccelerometerAxes); ++j) { | 269 .Append(iio_device.BaseName())); |
227 configuration_.scale[i][j] = kMeanGravity / scale_divisor; | 270 std::string location; |
228 std::string accelerometer_index_path = base::StringPrintf( | 271 if (base::ReadFileToString( |
229 kAccelerometerScanIndexPath, kAccelerometerAxes[j], | 272 base::FilePath(iio_path).Append(kAccelerometerLocationFileName), |
230 kAccelerometerNames[i]); | 273 &location)) { |
231 if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()), | 274 base::TrimWhitespaceASCII(location, base::TRIM_ALL, &location); |
232 &(configuration_.index[i][j]))) { | 275 if (!InitializeDevice(iio_path, name, location)) |
233 configuration_.has[i] = false; | 276 return; |
234 break; | 277 } else { |
235 } | 278 if (!InitializeBothAccelerometers(iio_path)) |
279 return; | |
280 configuration_.device_path[ACCELEROMETER_SOURCE_SCREEN] = name.BaseName(); | |
236 } | 281 } |
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 } | 282 } |
247 | 283 |
248 // Verify indices are within bounds. | 284 // Verify indices are within bounds. |
249 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { | 285 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { |
250 if (!configuration_.has[i]) | 286 if (!configuration_.has[i]) |
251 continue; | 287 continue; |
252 for (int j = 0; j < 3; ++j) { | 288 for (int j = 0; j < 3; ++j) { |
253 if (configuration_.index[i][j] < 0 || | 289 if (configuration_.index[i][j] < 0 || |
254 configuration_.index[i][j] >= | 290 configuration_.index[i][j] >= |
255 3 * static_cast<int>(configuration_.count)) { | 291 3 * static_cast<int>(configuration_.count)) { |
292 const char* axis = configuration_.read_device_separately | |
293 ? 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::InitializeDevice(const base::FilePath& iio_path, | |
332 const base::FilePath& name, | |
333 const std::string& location) { | |
334 configuration_.read_device_separately = true; | |
335 int config_index = std::find(std::begin(kAccelerometerNames), | |
336 std::end(kAccelerometerNames), location) - | |
337 std::begin(kAccelerometerNames); | |
338 if (config_index == -1) { | |
339 LOG(ERROR) << "Unrecognized location: " << location << " for device " | |
340 << name.MaybeAsASCII() << "\n"; | |
341 return false; | |
342 } | |
343 configuration_.device_path[config_index] = name.BaseName(); | |
344 configuration_.has[config_index] = true; | |
345 | |
346 double scale; | |
347 if (!ReadFileToDouble(iio_path.Append(kAccelerometerScaleFileName), &scale)) | |
348 return false; | |
349 | |
350 for (size_t j = 0; j < arraysize(kSeparateAccelerometerAxes); ++j) { | |
351 std::string accelerometer_index_path = base::StringPrintf( | |
352 kSeparateAccelerometerScanIndexPath, kSeparateAccelerometerAxes[j]); | |
353 if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()), | |
354 &(configuration_.index[config_index][j]))) { | |
355 configuration_.has[config_index] = false; | |
356 LOG(ERROR) << "Index file " << accelerometer_index_path | |
357 << " could not be parsed\n"; | |
358 return false; | |
359 } | |
360 configuration_.scale[config_index][j] = scale; | |
361 } | |
362 configuration_.device_length[config_index] = kDataSizeForSeparateDevices; | |
363 if (configuration_.has[config_index]) | |
364 configuration_.count++; | |
365 return true; | |
366 } | |
367 | |
368 bool AccelerometerFileReader::InitializeBothAccelerometers( | |
369 const base::FilePath& iio_path) { | |
370 // Read the scale for all axes. | |
371 int scale_divisor = 0; | |
372 // Read configuration of each accelerometer axis from each accelerometer from | |
373 // /sys/bus/iio/devices/iio:deviceX/. | |
374 for (size_t i = 0; i < arraysize(kAccelerometerNames); ++i) { | |
375 configuration_.has[i] = false; | |
376 // Read scale of accelerometer. | |
377 std::string accelerometer_scale_path = base::StringPrintf( | |
378 kSourceScaleNameFormatString, kAccelerometerNames[i]); | |
379 if (!ReadFileToInt(iio_path.Append(accelerometer_scale_path.c_str()), | |
380 &scale_divisor)) { | |
381 continue; | |
382 } | |
383 if (scale_divisor == 0) { | |
384 LOG(ERROR) << "Accelerometer " << accelerometer_scale_path | |
385 << "has scale of 0 and will not be used."; | |
386 continue; | |
387 } | |
388 | |
389 configuration_.has[i] = true; | |
390 for (size_t j = 0; j < arraysize(kUnifiedAccelerometerAxes); ++j) { | |
391 configuration_.scale[i][j] = kMeanGravity / scale_divisor; | |
392 std::string accelerometer_index_path = base::StringPrintf( | |
393 kAccelerometerScanIndexPath, kUnifiedAccelerometerAxes[j], | |
394 kAccelerometerNames[i]); | |
395 if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()), | |
396 &(configuration_.index[i][j]))) { | |
397 configuration_.has[i] = false; | |
398 LOG(ERROR) << "Index file " << accelerometer_index_path | |
399 << " could not be parsed\n"; | |
400 return false; | |
401 } | |
402 } | |
403 if (configuration_.has[i]) | |
404 configuration_.count++; | |
405 } | |
406 | |
407 // Adjust the directions of accelerometers to match the AccelerometerUpdate | |
408 // type specified in chromeos/accelerometer/accelerometer_types.h. | |
jonross
2015/08/24 18:30:35
With the latest change to the kernel, the axes and
| |
409 configuration_.scale[ACCELEROMETER_SOURCE_SCREEN][1] *= -1.0f; | |
410 configuration_.scale[ACCELEROMETER_SOURCE_SCREEN][2] *= -1.0f; | |
411 | |
412 configuration_.length = kDataSize * 3 * configuration_.count; | |
413 return true; | |
414 } | |
415 | |
292 void AccelerometerFileReader::ReadFileAndNotify() { | 416 void AccelerometerFileReader::ReadFileAndNotify() { |
293 DCHECK(initialization_successful_); | 417 DCHECK(initialization_successful_); |
294 char reading[kSizeOfReading]; | |
295 | 418 |
296 // Initiate the trigger to read accelerometers simultaneously | 419 // Initiate the trigger to read accelerometers simultaneously |
297 int bytes_written = base::WriteFile( | 420 int bytes_written = base::WriteFile( |
298 base::FilePath(kAccelerometerTriggerPath), "1\n", 2); | 421 base::FilePath(kAccelerometerTriggerPath), "1\n", 2); |
299 if (bytes_written < 2) { | 422 if (bytes_written < 2) { |
300 PLOG(ERROR) << "Accelerometer trigger failure: " << bytes_written; | 423 PLOG(ERROR) << "Accelerometer trigger failure: " << bytes_written; |
301 return; | 424 return; |
302 } | 425 } |
303 | 426 |
304 // Read resulting sample from /dev/cros-ec-accel. | 427 // Read resulting sample from /dev/cros-ec-accel. |
305 int bytes_read = base::ReadFile(base::FilePath(kAccelerometerDevicePath), | 428 bool result; |
306 reading, configuration_.length); | 429 update_ = new AccelerometerUpdate(); |
307 if (bytes_read < static_cast<int>(configuration_.length)) { | 430 if (configuration_.read_device_separately) { |
308 LOG(ERROR) << "Read " << bytes_read << " byte(s), expected " | 431 result = ReadSeparateDevices(); |
309 << configuration_.length << " bytes from accelerometer"; | 432 } else { |
433 result = ReadUnifiedDevice(); | |
434 } | |
435 if (!result) | |
310 return; | 436 return; |
311 } | |
312 | 437 |
313 update_ = new AccelerometerUpdate(); | 438 observers_->Notify(FROM_HERE, |
439 &AccelerometerReader::Observer::OnAccelerometerUpdated, | |
440 update_); | |
441 } | |
314 | 442 |
443 bool AccelerometerFileReader::ReadSeparateDevices() { | |
444 char reading[kSizeOfReading]; | |
445 int bytes_read; | |
315 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { | 446 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { |
316 if (!configuration_.has[i]) | 447 if (!configuration_.has[i]) |
317 continue; | 448 continue; |
449 bytes_read = base::ReadFile(base::FilePath(kAccelerometerDevicePath) | |
450 .Append(configuration_.device_path[i]), | |
451 reading, configuration_.device_length[i]); | |
452 if (bytes_read < static_cast<int>(configuration_.device_length[i])) { | |
453 LOG(ERROR) << "Accelerometer Read " << bytes_read << " byte(s), expected " | |
454 << configuration_.device_length[i] | |
455 << " bytes from accelerometer " | |
456 << configuration_.device_path[i].MaybeAsASCII(); | |
457 return false; | |
458 } | |
318 | 459 |
319 int16* values = reinterpret_cast<int16*>(reading); | 460 int16* values = reinterpret_cast<int16*>(reading); |
320 update_->Set( | 461 update_->Set( |
321 static_cast<AccelerometerSource>(i), | 462 static_cast<AccelerometerSource>(i), |
322 values[configuration_.index[i][0]] * configuration_.scale[i][0], | 463 values[configuration_.index[i][0]] * configuration_.scale[i][0], |
323 values[configuration_.index[i][1]] * configuration_.scale[i][1], | 464 values[configuration_.index[i][1]] * configuration_.scale[i][1], |
324 values[configuration_.index[i][2]] * configuration_.scale[i][2]); | 465 values[configuration_.index[i][2]] * configuration_.scale[i][2]); |
325 } | 466 } |
326 | 467 return true; |
327 observers_->Notify(FROM_HERE, | |
328 &AccelerometerReader::Observer::OnAccelerometerUpdated, | |
329 update_); | |
330 } | 468 } |
331 | 469 |
332 AccelerometerFileReader::ConfigurationData::ConfigurationData() : count(0) { | 470 bool AccelerometerFileReader::ReadUnifiedDevice() { |
471 char reading[kSizeOfReading]; | |
472 int bytes_read = base::ReadFile( | |
473 base::FilePath(kAccelerometerDevicePath) | |
474 .Append(configuration_.device_path[ACCELEROMETER_SOURCE_SCREEN]), | |
475 reading, configuration_.length); | |
476 if (bytes_read < static_cast<int>(configuration_.length)) { | |
477 LOG(ERROR) << "Accelerometer Read " << bytes_read << " byte(s), expected " | |
478 << configuration_.length << " bytes from accelerometer"; | |
479 return false; | |
480 } | |
481 | |
482 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { | |
483 if (!configuration_.has[i]) | |
484 continue; | |
485 int16* values = reinterpret_cast<int16*>(reading); | |
486 update_->Set( | |
487 static_cast<AccelerometerSource>(i), | |
488 values[configuration_.index[i][0]] * configuration_.scale[i][0], | |
489 values[configuration_.index[i][1]] * configuration_.scale[i][1], | |
490 values[configuration_.index[i][2]] * configuration_.scale[i][2]); | |
491 } | |
492 return true; | |
493 } | |
494 | |
495 AccelerometerFileReader::ConfigurationData::ConfigurationData() | |
496 : count(0), read_device_separately(false) { | |
333 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { | 497 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { |
334 has[i] = false; | 498 has[i] = false; |
335 for (int j = 0; j < 3; ++j) { | 499 for (int j = 0; j < 3; ++j) { |
336 scale[i][j] = 0; | 500 scale[i][j] = 0; |
337 index[i][j] = -1; | 501 index[i][j] = -1; |
338 } | 502 } |
339 } | 503 } |
340 } | 504 } |
341 | 505 |
342 AccelerometerFileReader::ConfigurationData::~ConfigurationData() { | 506 AccelerometerFileReader::ConfigurationData::~ConfigurationData() { |
(...skipping 24 matching lines...) Expand all Loading... | |
367 } | 531 } |
368 | 532 |
369 AccelerometerReader::AccelerometerReader() | 533 AccelerometerReader::AccelerometerReader() |
370 : accelerometer_file_reader_(new AccelerometerFileReader()) { | 534 : accelerometer_file_reader_(new AccelerometerFileReader()) { |
371 } | 535 } |
372 | 536 |
373 AccelerometerReader::~AccelerometerReader() { | 537 AccelerometerReader::~AccelerometerReader() { |
374 } | 538 } |
375 | 539 |
376 } // namespace chromeos | 540 } // namespace chromeos |
OLD | NEW |