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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_enumerator.h" | |
10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
11 #include "base/location.h" | 12 #include "base/location.h" |
12 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
18 #include "base/task_runner_util.h" | 19 #include "base/task_runner_util.h" |
19 #include "base/thread_task_runner_handle.h" | 20 #include "base/thread_task_runner_handle.h" |
20 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
21 #include "base/threading/sequenced_worker_pool.h" | 22 #include "base/threading/sequenced_worker_pool.h" |
22 | 23 |
23 namespace chromeos { | 24 namespace chromeos { |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 // Paths to access necessary data from the accelerometer device. | 28 // Paths to access necessary data from the accelerometer device. |
28 const base::FilePath::CharType kAccelerometerTriggerPath[] = | 29 const base::FilePath::CharType kAccelerometerTriggerPath[] = |
29 FILE_PATH_LITERAL("/sys/bus/iio/devices/trigger0/trigger_now"); | 30 FILE_PATH_LITERAL("/sys/bus/iio/devices/trigger0/trigger_now"); |
30 const base::FilePath::CharType kAccelerometerDevicePath[] = | 31 const base::FilePath::CharType kAccelerometerDevicePath[] = |
31 FILE_PATH_LITERAL("/dev/cros-ec-accel"); | 32 FILE_PATH_LITERAL("/dev/cros-ec-accel"); |
32 const base::FilePath::CharType kAccelerometerIioBasePath[] = | 33 const base::FilePath::CharType kAccelerometerIioBasePath[] = |
33 FILE_PATH_LITERAL("/sys/bus/iio/devices/"); | 34 FILE_PATH_LITERAL("/sys/bus/iio/devices/"); |
34 | 35 |
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 | 36 // 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 | 37 // 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 | 38 // or later or have been patched to use new format: http://crbug.com/510831 |
42 const base::FilePath::CharType kSourceScaleNameFormatString[] = | 39 const base::FilePath::CharType kSourceScaleNameFormatString[] = |
43 "in_accel_%s_scale"; | 40 "in_accel_%s_scale"; |
44 | 41 |
42 // File within kAccelerometerDevicePath/device* which denotes a single scale to | |
43 // be used across all axes. | |
44 const base::FilePath::CharType kAccelerometerScaleFileName[] = "scale"; | |
45 | |
46 // File within kAccelerometerDevicePath/device* which denotes the | |
47 // AccelerometerSource for the accelerometer. | |
48 const base::FilePath::CharType kAccelerometerLocationFileName[] = "location"; | |
49 | |
45 // The filename giving the path to read the scan index of each accelerometer | 50 // The filename giving the path to read the scan index of each accelerometer |
46 // axis. | 51 // axis. |
47 const char kAccelerometerScanIndexPath[] = | 52 const char kAccelerometerScanIndexPath[] = |
48 "scan_elements/in_accel_%s_%s_index"; | 53 "scan_elements/in_accel_%s_%s_index"; |
49 | 54 |
55 // The filename giving the path to read the scan index of each accelerometer | |
56 // when they are separate device paths. | |
57 const char kSeparateAccelerometerScanIndexPath[] = | |
58 "scan_elements/in_accel_%s_index"; | |
59 | |
50 // The names of the accelerometers. Matches up with the enum AccelerometerSource | 60 // The names of the accelerometers. Matches up with the enum AccelerometerSource |
51 // in chromeos/accelerometer/accelerometer_types.h. | 61 // in chromeos/accelerometer/accelerometer_types.h. |
52 const char kAccelerometerNames[ACCELEROMETER_SOURCE_COUNT][5] = {"lid", "base"}; | 62 const char kAccelerometerNames[ACCELEROMETER_SOURCE_COUNT][5] = {"lid", "base"}; |
53 | 63 |
54 // The axes on each accelerometer. | 64 // The axes on each accelerometer. |
55 const char kAccelerometerAxes[][2] = {"y", "x", "z"}; | 65 const char kSeparateAccelerometerAxes[][2] = {"x", "y", "z"}; |
66 const char kUnifiedAccelerometerAxes[][2] = {"y", "x", "z"}; | |
56 | 67 |
57 // The length required to read uint values from configuration files. | 68 // The length required to read uint values from configuration files. |
58 const size_t kMaxAsciiUintLength = 21; | 69 const size_t kMaxAsciiUintLength = 21; |
59 | 70 |
60 // The size of individual values. | 71 // The size of individual values. |
61 const size_t kDataSize = 2; | 72 const size_t kDataSize = 2; |
62 | 73 |
74 // The size of reading values for an entire accelerometer, when they are | |
75 // separate device paths. | |
76 const size_t kDataSizeForSeparateDevices = 6; | |
77 | |
63 // The mean acceleration due to gravity on Earth in m/s^2. | 78 // The mean acceleration due to gravity on Earth in m/s^2. |
64 const float kMeanGravity = 9.80665f; | 79 const float kMeanGravity = 9.80665f; |
65 | 80 |
66 // The number of accelerometers. | 81 // The number of accelerometers. |
67 const int kNumberOfAccelerometers = 2; | 82 const int kNumberOfAccelerometers = 2; |
68 | 83 |
69 // The number of axes for which there are acceleration readings. | 84 // The number of axes for which there are acceleration readings. |
70 const int kNumberOfAxes = 3; | 85 const int kNumberOfAxes = 3; |
71 | 86 |
72 // The size of data in one reading of the accelerometers. | 87 // The size of data in one reading of the accelerometers. |
73 const int kSizeOfReading = kDataSize * kNumberOfAccelerometers * kNumberOfAxes; | 88 const int kSizeOfReading = kDataSize * kNumberOfAccelerometers * kNumberOfAxes; |
74 | 89 |
75 // Reads |path| to the unsigned int pointed to by |value|. Returns true on | 90 // Reads |path| to the unsigned int pointed to by |value|. Returns true on |
76 // success or false on failure. | 91 // success or false on failure. |
77 bool ReadFileToInt(const base::FilePath& path, int* value) { | 92 bool ReadFileToInt(const base::FilePath& path, int* value) { |
78 std::string s; | 93 std::string s; |
79 DCHECK(value); | 94 DCHECK(value); |
80 if (!base::ReadFileToString(path, &s, kMaxAsciiUintLength)) { | 95 if (!base::ReadFileToString(path, &s, kMaxAsciiUintLength)) { |
81 return false; | 96 return false; |
82 } | 97 } |
83 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s); | 98 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s); |
84 if (!base::StringToInt(s, value)) { | 99 if (!base::StringToInt(s, value)) { |
85 LOG(ERROR) << "Failed to parse \"" << s << "\" from " << path.value(); | 100 LOG(ERROR) << "Failed to parse int \"" << s << "\" from " << path.value(); |
86 return false; | 101 return false; |
87 } | 102 } |
88 return true; | 103 return true; |
104 } | |
105 | |
106 // Reads |path| to the double pointed to by |value|. Returns true on success or | |
107 // false on failure. | |
108 bool ReadFileToDouble(const base::FilePath& path, double* value) { | |
109 std::string s; | |
110 DCHECK(value); | |
111 if (!base::ReadFileToString(path, &s)) { | |
112 return false; | |
113 } | |
114 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s); | |
115 if (!base::StringToDouble(s, value)) { | |
116 LOG(ERROR) << "Failed to parse double \"" << s << "\" from " | |
117 << path.value(); | |
118 return false; | |
119 } | |
120 return true; | |
89 } | 121 } |
90 | 122 |
91 } // namespace | 123 } // namespace |
92 | 124 |
93 const int AccelerometerReader::kDelayBetweenReadsMs = 100; | 125 const int AccelerometerReader::kDelayBetweenReadsMs = 100; |
94 | 126 |
95 // Work that runs on a base::TaskRunner. It determines the accelerometer | 127 // Work that runs on a base::TaskRunner. It determines the accelerometer |
96 // configuartion, and reads the data. Upon a successful read it will notify | 128 // configuartion, and reads the data. Upon a successful read it will notify |
97 // all observers. | 129 // all observers. |
98 class AccelerometerFileReader | 130 class AccelerometerFileReader |
(...skipping 30 matching lines...) Expand all Loading... | |
129 size_t length; | 161 size_t length; |
130 | 162 |
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 // If true the accelerometer devices must be read from distinct files. | |
173 bool read_device_separately; | |
174 | |
175 // Length of reading per accelerometer | |
176 size_t device_length[ACCELEROMETER_SOURCE_COUNT]; | |
177 | |
178 // The path to append to |kAccelerometerDevicePath| where the accelerometer | |
179 // device resides. | |
180 base::FilePath device_path[ACCELEROMETER_SOURCE_COUNT]; | |
oshima
2015/08/21 06:44:46
don't have to be now, but it looks to me that the
jonross
2015/08/21 14:01:05
When all devices are updated to the new kernel, we
| |
139 }; | 181 }; |
140 | 182 |
141 ~AccelerometerFileReader() {} | 183 ~AccelerometerFileReader() {} |
142 | 184 |
185 // When accelerometers are presented as separate iio_devices this will perform | |
186 // the initialize for one of the devices, at the given |iio_path| and the | |
187 // symbolic link |name|. |location| is defined by AccelerometerSoure. | |
188 bool InitializeDevice(base::FilePath& iio_path, | |
oshima
2015/08/21 06:44:46
passing non const reference is uncommon, and out p
jonross
2015/08/21 14:01:05
I forgot to add const.... :(
| |
189 base::FilePath& name, | |
190 std::string& location); | |
191 | |
192 // When accelerometers are presented as a single iio_device this will perform | |
193 // the initialization for both of them. | |
194 void InitializeBothAccelerometers(base::FilePath& iio_path); | |
oshima
2015/08/21 06:44:46
ditto
| |
195 | |
143 // Attempts to read the accelerometer data. Upon a success, converts the raw | 196 // Attempts to read the accelerometer data. Upon a success, converts the raw |
144 // reading to an AccelerometerUpdate and notifies observers. | 197 // reading to an AccelerometerUpdate and notifies observers. |
145 void ReadFileAndNotify(); | 198 void ReadFileAndNotify(); |
146 | 199 |
200 // When accelerometers are presented as separate iio_devices this will perform | |
201 // the read of both devices, from their separate locations. | |
202 bool ReadSeparateDevices(); | |
203 | |
204 // When accelerometers are presented as a single iio_device this will perform | |
205 // the read of the device. | |
206 bool ReadUnifiedDevice(); | |
207 | |
147 // True if Initialize completed successfully, and there is an accelerometer | 208 // True if Initialize completed successfully, and there is an accelerometer |
148 // file to read. | 209 // file to read. |
149 bool initialization_successful_; | 210 bool initialization_successful_; |
150 | 211 |
151 // The accelerometer configuration. | 212 // The accelerometer configuration. |
152 ConfigurationData configuration_; | 213 ConfigurationData configuration_; |
153 | 214 |
154 // The observers to notify of accelerometer updates. | 215 // The observers to notify of accelerometer updates. |
155 scoped_refptr<base::ObserverListThreadSafe<AccelerometerReader::Observer>> | 216 scoped_refptr<base::ObserverListThreadSafe<AccelerometerReader::Observer>> |
156 observers_; | 217 observers_; |
(...skipping 15 matching lines...) Expand all Loading... | |
172 | 233 |
173 void AccelerometerFileReader::Initialize( | 234 void AccelerometerFileReader::Initialize( |
174 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner) { | 235 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner) { |
175 DCHECK( | 236 DCHECK( |
176 base::SequencedWorkerPool::GetSequenceTokenForCurrentThread().IsValid()); | 237 base::SequencedWorkerPool::GetSequenceTokenForCurrentThread().IsValid()); |
177 task_runner_ = sequenced_task_runner; | 238 task_runner_ = sequenced_task_runner; |
178 | 239 |
179 // Check for accelerometer symlink which will be created by the udev rules | 240 // Check for accelerometer symlink which will be created by the udev rules |
180 // file on detecting the device. | 241 // file on detecting the device. |
181 base::FilePath device; | 242 base::FilePath device; |
182 if (!base::ReadSymbolicLink(base::FilePath(kAccelerometerDevicePath), | 243 |
183 &device)) { | 244 if (base::IsDirectoryEmpty(base::FilePath(kAccelerometerDevicePath))) { |
245 LOG(ERROR) << "Accelerometer device directory is empty at " | |
246 << kAccelerometerDevicePath; | |
184 return; | 247 return; |
185 } | 248 } |
186 | 249 |
187 if (!base::PathExists(base::FilePath(kAccelerometerTriggerPath))) { | 250 if (!base::PathExists(base::FilePath(kAccelerometerTriggerPath))) { |
188 LOG(ERROR) << "Accelerometer trigger does not exist at" | 251 LOG(ERROR) << "Accelerometer trigger does not exist at" |
189 << kAccelerometerTriggerPath; | 252 << kAccelerometerTriggerPath; |
190 return; | 253 return; |
191 } | 254 } |
192 | 255 |
193 base::FilePath iio_path(base::FilePath(kAccelerometerIioBasePath).Append( | 256 base::FileEnumerator symlink_dir(base::FilePath(kAccelerometerDevicePath), |
194 device)); | 257 false, base::FileEnumerator::FILES); |
195 | 258 for (base::FilePath name = symlink_dir.Next(); !name.empty(); |
196 // Read the scale for all axes. | 259 name = symlink_dir.Next()) { |
197 int scale_divisor = 0; | 260 base::FilePath iio_device; |
198 bool per_source_scale = | 261 if (!base::ReadSymbolicLink(name, &iio_device)) { |
199 !ReadFileToInt(iio_path.Append(kScaleFileName), &scale_divisor); | 262 LOG(ERROR) << "Failed to read symbolic link " << kAccelerometerDevicePath |
200 if (!per_source_scale && scale_divisor == 0) { | 263 << "/" << name.MaybeAsASCII() << "\n"; |
201 LOG(ERROR) << "Accelerometer " << kScaleFileName | 264 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 } | 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 if (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 (!InitializeDevice(iio_path, name, location)) |
232 &(configuration_.index[i][j]))) { | 274 return; |
233 configuration_.has[i] = false; | 275 } else { |
234 break; | 276 InitializeBothAccelerometers(iio_path); |
235 } | 277 configuration_.device_path[ACCELEROMETER_SOURCE_SCREEN] = name.BaseName(); |
236 } | 278 } |
237 if (configuration_.has[i]) | |
238 configuration_.count++; | |
239 } | 279 } |
240 | 280 |
241 // Adjust the directions of accelerometers to match the AccelerometerUpdate | 281 // Adjust the directions of accelerometers to match the AccelerometerUpdate |
242 // type specified in chromeos/accelerometer/accelerometer_types.h. | 282 // type specified in chromeos/accelerometer/accelerometer_types.h. |
243 configuration_.scale[ACCELEROMETER_SOURCE_SCREEN][0] *= -1.0f; | 283 configuration_.scale[ACCELEROMETER_SOURCE_SCREEN][0] *= -1.0f; |
244 for (int i = 0; i < 3; ++i) { | 284 for (int i = 0; i < 3; ++i) { |
245 configuration_.scale[ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD][i] *= -1.0f; | 285 configuration_.scale[ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD][i] *= -1.0f; |
246 } | 286 } |
247 | 287 |
248 // Verify indices are within bounds. | 288 // Verify indices are within bounds. |
249 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { | 289 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { |
250 if (!configuration_.has[i]) | 290 if (!configuration_.has[i]) |
251 continue; | 291 continue; |
252 for (int j = 0; j < 3; ++j) { | 292 for (int j = 0; j < 3; ++j) { |
253 if (configuration_.index[i][j] < 0 || | 293 if (configuration_.index[i][j] < 0 || |
254 configuration_.index[i][j] >= | 294 configuration_.index[i][j] >= |
255 3 * static_cast<int>(configuration_.count)) { | 295 3 * static_cast<int>(configuration_.count)) { |
296 const char* axis = configuration_.read_device_separately | |
297 ? kSeparateAccelerometerAxes[j] | |
298 : kUnifiedAccelerometerAxes[j]; | |
256 LOG(ERROR) << "Field index for " << kAccelerometerNames[i] << " " | 299 LOG(ERROR) << "Field index for " << kAccelerometerNames[i] << " " |
257 << kAccelerometerAxes[j] << " axis out of bounds."; | 300 << axis << " axis out of bounds."; |
258 return; | 301 return; |
259 } | 302 } |
260 } | 303 } |
261 } | 304 } |
262 configuration_.length = kDataSize * 3 * configuration_.count; | 305 |
263 initialization_successful_ = true; | 306 initialization_successful_ = true; |
264 Read(); | 307 Read(); |
265 } | 308 } |
266 | 309 |
267 void AccelerometerFileReader::Read() { | 310 void AccelerometerFileReader::Read() { |
268 DCHECK( | 311 DCHECK( |
269 base::SequencedWorkerPool::GetSequenceTokenForCurrentThread().IsValid()); | 312 base::SequencedWorkerPool::GetSequenceTokenForCurrentThread().IsValid()); |
270 ReadFileAndNotify(); | 313 ReadFileAndNotify(); |
271 task_runner_->PostNonNestableDelayedTask( | 314 task_runner_->PostNonNestableDelayedTask( |
272 FROM_HERE, base::Bind(&AccelerometerFileReader::Read, this), | 315 FROM_HERE, base::Bind(&AccelerometerFileReader::Read, this), |
273 base::TimeDelta::FromMilliseconds( | 316 base::TimeDelta::FromMilliseconds( |
274 AccelerometerReader::kDelayBetweenReadsMs)); | 317 AccelerometerReader::kDelayBetweenReadsMs)); |
275 } | 318 } |
276 | 319 |
277 void AccelerometerFileReader::AddObserver( | 320 void AccelerometerFileReader::AddObserver( |
278 AccelerometerReader::Observer* observer) { | 321 AccelerometerReader::Observer* observer) { |
279 observers_->AddObserver(observer); | 322 observers_->AddObserver(observer); |
280 if (initialization_successful_) { | 323 if (initialization_successful_) { |
281 task_runner_->PostNonNestableTask( | 324 task_runner_->PostNonNestableTask( |
282 FROM_HERE, | 325 FROM_HERE, |
283 base::Bind(&AccelerometerFileReader::ReadFileAndNotify, this)); | 326 base::Bind(&AccelerometerFileReader::ReadFileAndNotify, this)); |
284 } | 327 } |
285 } | 328 } |
286 | 329 |
287 void AccelerometerFileReader::RemoveObserver( | 330 void AccelerometerFileReader::RemoveObserver( |
288 AccelerometerReader::Observer* observer) { | 331 AccelerometerReader::Observer* observer) { |
289 observers_->RemoveObserver(observer); | 332 observers_->RemoveObserver(observer); |
290 } | 333 } |
291 | 334 |
335 bool AccelerometerFileReader::InitializeDevice(base::FilePath& iio_path, | |
336 base::FilePath& name, | |
337 std::string& location) { | |
338 configuration_.read_device_separately = true; | |
339 base::TrimWhitespaceASCII(location, base::TRIM_ALL, &location); | |
340 int config_index = -1; | |
341 for (size_t j = 0; j < arraysize(kAccelerometerNames); ++j) { | |
342 if (location == kAccelerometerNames[j]) { | |
343 config_index = j; | |
344 break; | |
345 } | |
346 } | |
347 | |
348 if (config_index == -1) { | |
349 LOG(ERROR) << "Invalid location: " << location << "\n"; | |
350 return false; | |
351 } | |
352 configuration_.device_path[config_index] = name.BaseName(); | |
353 configuration_.has[config_index] = true; | |
354 | |
355 double scale; | |
356 if (!ReadFileToDouble(iio_path.Append(kAccelerometerScaleFileName), &scale)) { | |
357 return false; | |
358 } | |
359 | |
360 for (size_t j = 0; j < arraysize(kSeparateAccelerometerAxes); ++j) { | |
361 std::string accelerometer_index_path = base::StringPrintf( | |
362 kSeparateAccelerometerScanIndexPath, kSeparateAccelerometerAxes[j]); | |
363 if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()), | |
364 &(configuration_.index[config_index][j]))) { | |
365 configuration_.has[config_index] = false; | |
366 break; | |
367 } | |
368 configuration_.scale[config_index][j] = scale; | |
369 } | |
370 configuration_.device_length[config_index] = kDataSizeForSeparateDevices; | |
371 if (configuration_.has[config_index]) | |
372 configuration_.count++; | |
373 return true; | |
374 } | |
375 | |
376 void AccelerometerFileReader::InitializeBothAccelerometers( | |
377 base::FilePath& iio_path) { | |
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 break; | |
407 } | |
408 } | |
409 if (configuration_.has[i]) | |
410 configuration_.count++; | |
411 } | |
412 | |
413 configuration_.length = kDataSize * 3 * configuration_.count; | |
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 LOG(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) << "Acceleromter Read " << bytes_read << " byte(s), expected " | |
454 << configuration_.device_length[i] | |
455 << " bytes from accelerometer"; | |
456 return false; | |
457 } | |
318 | 458 |
319 int16* values = reinterpret_cast<int16*>(reading); | 459 int16* values = reinterpret_cast<int16*>(reading); |
320 update_->Set( | 460 update_->Set( |
321 static_cast<AccelerometerSource>(i), | 461 static_cast<AccelerometerSource>(i), |
322 values[configuration_.index[i][0]] * configuration_.scale[i][0], | 462 values[configuration_.index[i][0]] * configuration_.scale[i][0], |
323 values[configuration_.index[i][1]] * configuration_.scale[i][1], | 463 values[configuration_.index[i][1]] * configuration_.scale[i][1], |
324 values[configuration_.index[i][2]] * configuration_.scale[i][2]); | 464 values[configuration_.index[i][2]] * configuration_.scale[i][2]); |
325 } | 465 } |
326 | 466 return true; |
327 observers_->Notify(FROM_HERE, | |
328 &AccelerometerReader::Observer::OnAccelerometerUpdated, | |
329 update_); | |
330 } | 467 } |
331 | 468 |
332 AccelerometerFileReader::ConfigurationData::ConfigurationData() : count(0) { | 469 bool AccelerometerFileReader::ReadUnifiedDevice() { |
470 char reading[kSizeOfReading]; | |
471 int bytes_read = base::ReadFile( | |
472 base::FilePath(kAccelerometerDevicePath) | |
473 .Append(configuration_.device_path[ACCELEROMETER_SOURCE_SCREEN]), | |
474 reading, configuration_.length); | |
475 if (bytes_read < static_cast<int>(configuration_.length)) { | |
476 LOG(ERROR) << "Accelerometer Read " << bytes_read << " byte(s), expected " | |
477 << configuration_.length << " bytes from accelerometer"; | |
478 return false; | |
479 } | |
480 | |
481 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { | |
482 if (!configuration_.has[i]) | |
483 continue; | |
484 int16* values = reinterpret_cast<int16*>(reading); | |
485 update_->Set( | |
486 static_cast<AccelerometerSource>(i), | |
487 values[configuration_.index[i][0]] * configuration_.scale[i][0], | |
488 values[configuration_.index[i][1]] * configuration_.scale[i][1], | |
489 values[configuration_.index[i][2]] * configuration_.scale[i][2]); | |
490 } | |
491 return true; | |
492 } | |
493 | |
494 AccelerometerFileReader::ConfigurationData::ConfigurationData() | |
495 : count(0), read_device_separately(false) { | |
333 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { | 496 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { |
334 has[i] = false; | 497 has[i] = false; |
335 for (int j = 0; j < 3; ++j) { | 498 for (int j = 0; j < 3; ++j) { |
336 scale[i][j] = 0; | 499 scale[i][j] = 0; |
337 index[i][j] = -1; | 500 index[i][j] = -1; |
338 } | 501 } |
339 } | 502 } |
340 } | 503 } |
341 | 504 |
342 AccelerometerFileReader::ConfigurationData::~ConfigurationData() { | 505 AccelerometerFileReader::ConfigurationData::~ConfigurationData() { |
(...skipping 24 matching lines...) Expand all Loading... | |
367 } | 530 } |
368 | 531 |
369 AccelerometerReader::AccelerometerReader() | 532 AccelerometerReader::AccelerometerReader() |
370 : accelerometer_file_reader_(new AccelerometerFileReader()) { | 533 : accelerometer_file_reader_(new AccelerometerFileReader()) { |
371 } | 534 } |
372 | 535 |
373 AccelerometerReader::~AccelerometerReader() { | 536 AccelerometerReader::~AccelerometerReader() { |
374 } | 537 } |
375 | 538 |
376 } // namespace chromeos | 539 } // namespace chromeos |
OLD | NEW |