| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "base/task_runner_util.h" | 25 #include "base/task_runner_util.h" |
| 26 #include "base/threading/platform_thread.h" | 26 #include "base/threading/platform_thread.h" |
| 27 #include "base/threading/sequenced_worker_pool.h" | 27 #include "base/threading/sequenced_worker_pool.h" |
| 28 #include "base/threading/thread_task_runner_handle.h" | 28 #include "base/threading/thread_task_runner_handle.h" |
| 29 | 29 |
| 30 namespace chromeos { | 30 namespace chromeos { |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // Paths to access necessary data from the accelerometer device. | 34 // Paths to access necessary data from the accelerometer device. |
| 35 const base::FilePath::CharType kAccelerometerTriggerPath[] = | |
| 36 FILE_PATH_LITERAL("/sys/bus/iio/devices/trigger0/trigger_now"); | |
| 37 const base::FilePath::CharType kAccelerometerDevicePath[] = | 35 const base::FilePath::CharType kAccelerometerDevicePath[] = |
| 38 FILE_PATH_LITERAL("/dev/cros-ec-accel"); | 36 FILE_PATH_LITERAL("/dev/cros-ec-accel"); |
| 39 const base::FilePath::CharType kAccelerometerIioBasePath[] = | 37 const base::FilePath::CharType kAccelerometerIioBasePath[] = |
| 40 FILE_PATH_LITERAL("/sys/bus/iio/devices/"); | 38 FILE_PATH_LITERAL("/sys/bus/iio/devices/"); |
| 41 | 39 |
| 40 // Trigger created by accelerometer-init.sh to query the sensors. |
| 41 const char kTriggerPrefix[] = "trigger"; |
| 42 const char kTriggerName[] = "sysfstrig0\n"; |
| 43 |
| 44 // Sysfs entry to trigger readings. |
| 45 const base::FilePath::CharType kTriggerNow[] = "trigger_now"; |
| 46 |
| 42 // This is the per source scale file in use on kernels older than 3.18. We | 47 // This is the per source scale file in use on kernels older than 3.18. We |
| 43 // should remove this when all devices having accelerometers are on kernel 3.18 | 48 // should remove this when all devices having accelerometers are on kernel 3.18 |
| 44 // or later or have been patched to use new format: http://crbug.com/510831 | 49 // or later or have been patched to use new format: http://crbug.com/510831 |
| 45 const base::FilePath::CharType kLegacyScaleNameFormatString[] = | 50 const base::FilePath::CharType kLegacyScaleNameFormatString[] = |
| 46 "in_accel_%s_scale"; | 51 "in_accel_%s_scale"; |
| 47 | 52 |
| 48 // File within kAccelerometerDevicePath/device* which denotes a single scale to | 53 // File within kAccelerometerDevicePath/device* which denotes a single scale to |
| 49 // be used across all axes. | 54 // be used across all axes. |
| 50 const base::FilePath::CharType kAccelerometerScaleFileName[] = "scale"; | 55 const base::FilePath::CharType kAccelerometerScaleFileName[] = "scale"; |
| 51 | 56 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 }; | 163 }; |
| 159 | 164 |
| 160 // Configuration structure for accelerometer device. | 165 // Configuration structure for accelerometer device. |
| 161 struct ConfigurationData { | 166 struct ConfigurationData { |
| 162 ConfigurationData(); | 167 ConfigurationData(); |
| 163 ~ConfigurationData(); | 168 ~ConfigurationData(); |
| 164 | 169 |
| 165 // Number of accelerometers on device. | 170 // Number of accelerometers on device. |
| 166 size_t count; | 171 size_t count; |
| 167 | 172 |
| 173 // sysfs entry to trigger readings. |
| 174 base::FilePath trigger_now; |
| 175 |
| 168 // Which accelerometers are present on device. | 176 // Which accelerometers are present on device. |
| 169 bool has[ACCELEROMETER_SOURCE_COUNT]; | 177 bool has[ACCELEROMETER_SOURCE_COUNT]; |
| 170 | 178 |
| 171 // Scale of accelerometers (i.e. raw value * scale = m/s^2). | 179 // Scale of accelerometers (i.e. raw value * scale = m/s^2). |
| 172 float scale[ACCELEROMETER_SOURCE_COUNT][3]; | 180 float scale[ACCELEROMETER_SOURCE_COUNT][3]; |
| 173 | 181 |
| 174 // Index of each accelerometer axis in data stream. | 182 // Index of each accelerometer axis in data stream. |
| 175 int index[ACCELEROMETER_SOURCE_COUNT][3]; | 183 int index[ACCELEROMETER_SOURCE_COUNT][3]; |
| 176 | 184 |
| 177 // The information for each accelerometer device to be read. In kernel 3.18 | 185 // The information for each accelerometer device to be read. In kernel 3.18 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 241 |
| 234 // Check for accelerometer symlink which will be created by the udev rules | 242 // Check for accelerometer symlink which will be created by the udev rules |
| 235 // file on detecting the device. | 243 // file on detecting the device. |
| 236 if (base::IsDirectoryEmpty(base::FilePath(kAccelerometerDevicePath))) { | 244 if (base::IsDirectoryEmpty(base::FilePath(kAccelerometerDevicePath))) { |
| 237 if (base::SysInfo::IsRunningOnChromeOS()) { | 245 if (base::SysInfo::IsRunningOnChromeOS()) { |
| 238 LOG(WARNING) << "Accelerometer device directory is empty at " | 246 LOG(WARNING) << "Accelerometer device directory is empty at " |
| 239 << kAccelerometerDevicePath; | 247 << kAccelerometerDevicePath; |
| 240 } | 248 } |
| 241 return; | 249 return; |
| 242 } | 250 } |
| 243 if (!base::PathExists(base::FilePath(kAccelerometerTriggerPath))) { | 251 |
| 252 // Find trigger to use: |
| 253 base::FileEnumerator trigger_dir(base::FilePath(kAccelerometerIioBasePath), |
| 254 false, base::FileEnumerator::DIRECTORIES); |
| 255 std::string prefix = kTriggerPrefix; |
| 256 for (base::FilePath name = trigger_dir.Next(); !name.empty(); |
| 257 name = trigger_dir.Next()) { |
| 258 if (name.BaseName().value().substr(0, prefix.size()) != prefix) |
| 259 continue; |
| 260 std::string trigger_name; |
| 261 if (!base::ReadFileToString(name.Append("name"), &trigger_name)) { |
| 262 if (base::SysInfo::IsRunningOnChromeOS()) { |
| 263 LOG(WARNING) << "Unable to read the trigger name at " << name.value(); |
| 264 } |
| 265 continue; |
| 266 } |
| 267 if (trigger_name == kTriggerName) { |
| 268 base::FilePath trigger_now = name.Append(kTriggerNow); |
| 269 if (!base::PathExists(trigger_now)) { |
| 270 if (base::SysInfo::IsRunningOnChromeOS()) { |
| 271 LOG(ERROR) << "Accelerometer trigger does not exist at " |
| 272 << trigger_now.value(); |
| 273 } |
| 274 return; |
| 275 } else { |
| 276 configuration_.trigger_now = trigger_now; |
| 277 break; |
| 278 } |
| 279 } |
| 280 } |
| 281 if (configuration_.trigger_now.empty()) { |
| 244 if (base::SysInfo::IsRunningOnChromeOS()) { | 282 if (base::SysInfo::IsRunningOnChromeOS()) { |
| 245 LOG(ERROR) << "Accelerometer trigger does not exist at" | 283 LOG(ERROR) << "Accelerometer trigger not found"; |
| 246 << kAccelerometerTriggerPath; | |
| 247 } | 284 } |
| 248 return; | 285 return; |
| 249 } | 286 } |
| 250 | 287 |
| 251 base::FileEnumerator symlink_dir(base::FilePath(kAccelerometerDevicePath), | 288 base::FileEnumerator symlink_dir(base::FilePath(kAccelerometerDevicePath), |
| 252 false, base::FileEnumerator::FILES); | 289 false, base::FileEnumerator::FILES); |
| 253 bool legacy_cross_accel = false; | 290 bool legacy_cross_accel = false; |
| 254 for (base::FilePath name = symlink_dir.Next(); !name.empty(); | 291 for (base::FilePath name = symlink_dir.Next(); !name.empty(); |
| 255 name = symlink_dir.Next()) { | 292 name = symlink_dir.Next()) { |
| 256 base::FilePath iio_device; | 293 base::FilePath iio_device; |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 configuration_.scale[ACCELEROMETER_SOURCE_SCREEN][2] *= -1.0f; | 456 configuration_.scale[ACCELEROMETER_SOURCE_SCREEN][2] *= -1.0f; |
| 420 | 457 |
| 421 configuration_.reading_data.push_back(reading_data); | 458 configuration_.reading_data.push_back(reading_data); |
| 422 return true; | 459 return true; |
| 423 } | 460 } |
| 424 | 461 |
| 425 void AccelerometerFileReader::ReadFileAndNotify() { | 462 void AccelerometerFileReader::ReadFileAndNotify() { |
| 426 DCHECK(initialization_successful_); | 463 DCHECK(initialization_successful_); |
| 427 | 464 |
| 428 // Initiate the trigger to read accelerometers simultaneously | 465 // Initiate the trigger to read accelerometers simultaneously |
| 429 int bytes_written = base::WriteFile( | 466 int bytes_written = base::WriteFile(configuration_.trigger_now, "1\n", 2); |
| 430 base::FilePath(kAccelerometerTriggerPath), "1\n", 2); | |
| 431 if (bytes_written < 2) { | 467 if (bytes_written < 2) { |
| 432 PLOG(ERROR) << "Accelerometer trigger failure: " << bytes_written; | 468 PLOG(ERROR) << "Accelerometer trigger failure: " << bytes_written; |
| 433 return; | 469 return; |
| 434 } | 470 } |
| 435 | 471 |
| 436 // Read resulting sample from /dev/cros-ec-accel. | 472 // Read resulting sample from /dev/cros-ec-accel. |
| 437 update_ = new AccelerometerUpdate(); | 473 update_ = new AccelerometerUpdate(); |
| 438 for (auto reading_data : configuration_.reading_data) { | 474 for (auto reading_data : configuration_.reading_data) { |
| 439 int reading_size = reading_data.sources.size() * kSizeOfReading; | 475 int reading_size = reading_data.sources.size() * kSizeOfReading; |
| 440 DCHECK_GT(reading_size, 0); | 476 DCHECK_GT(reading_size, 0); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 } | 537 } |
| 502 | 538 |
| 503 AccelerometerReader::AccelerometerReader() | 539 AccelerometerReader::AccelerometerReader() |
| 504 : accelerometer_file_reader_(new AccelerometerFileReader()) { | 540 : accelerometer_file_reader_(new AccelerometerFileReader()) { |
| 505 } | 541 } |
| 506 | 542 |
| 507 AccelerometerReader::~AccelerometerReader() { | 543 AccelerometerReader::~AccelerometerReader() { |
| 508 } | 544 } |
| 509 | 545 |
| 510 } // namespace chromeos | 546 } // namespace chromeos |
| OLD | NEW |