Chromium Code Reviews| Index: chromeos/accelerometer/accelerometer_reader.cc |
| diff --git a/chromeos/accelerometer/accelerometer_reader.cc b/chromeos/accelerometer/accelerometer_reader.cc |
| index 04122ca09b90f18974d914382fba2a87a5721ce9..1d6d732c8c151000544c471a56f228a9bf2bfa3f 100644 |
| --- a/chromeos/accelerometer/accelerometer_reader.cc |
| +++ b/chromeos/accelerometer/accelerometer_reader.cc |
| @@ -7,6 +7,7 @@ |
| #include <string> |
| #include "base/bind.h" |
| +#include "base/files/file_enumerator.h" |
| #include "base/files/file_util.h" |
| #include "base/location.h" |
| #include "base/memory/singleton.h" |
| @@ -32,27 +33,37 @@ const base::FilePath::CharType kAccelerometerDevicePath[] = |
| const base::FilePath::CharType kAccelerometerIioBasePath[] = |
| FILE_PATH_LITERAL("/sys/bus/iio/devices/"); |
| -// File within the device in kAccelerometerIioBasePath containing the scale of |
| -// the accelerometers. |
| -const base::FilePath::CharType kScaleFileName[] = "in_accel_scale"; |
| - |
| // This is the per source scale file in use on kernels older than 3.18. We |
| // should remove this when all devices having accelerometers are on kernel 3.18 |
| // or later or have been patched to use new format: http://crbug.com/510831 |
| const base::FilePath::CharType kSourceScaleNameFormatString[] = |
| "in_accel_%s_scale"; |
| +// File within kAccelerometerDevicePath/device* which denotes a single scale to |
| +// be used across all axes. |
| +const base::FilePath::CharType kAccelerometerScaleFileName[] = "scale"; |
| + |
| +// File within kAccelerometerDevicePath/device* which denotes the |
| +// AccelerometerSource for the accelerometer. |
| +const base::FilePath::CharType kAccelerometerLocationFileName[] = "location"; |
| + |
| // The filename giving the path to read the scan index of each accelerometer |
| // axis. |
| const char kAccelerometerScanIndexPath[] = |
| "scan_elements/in_accel_%s_%s_index"; |
| +// The filename giving the path to read the scan index of each accelerometer |
| +// when they are separate device paths. |
| +const char kSeparateAccelerometerScanIndexPath[] = |
| + "scan_elements/in_accel_%s_index"; |
| + |
| // The names of the accelerometers. Matches up with the enum AccelerometerSource |
| // in chromeos/accelerometer/accelerometer_types.h. |
| const char kAccelerometerNames[ACCELEROMETER_SOURCE_COUNT][5] = {"lid", "base"}; |
| // The axes on each accelerometer. |
| -const char kAccelerometerAxes[][2] = {"y", "x", "z"}; |
| +const char kSeparateAccelerometerAxes[][2] = {"x", "y", "z"}; |
|
flackr
2015/08/21 19:12:03
It seems suspicious that the axes have changed ord
jonross
2015/08/24 18:30:34
kernel 3.18 is not available for glimmer/clapper.
|
| +const char kUnifiedAccelerometerAxes[][2] = {"y", "x", "z"}; |
| // The length required to read uint values from configuration files. |
| const size_t kMaxAsciiUintLength = 21; |
| @@ -60,6 +71,10 @@ const size_t kMaxAsciiUintLength = 21; |
| // The size of individual values. |
| const size_t kDataSize = 2; |
| +// The size of reading values for an entire accelerometer, when they are |
| +// separate device paths. |
| +const size_t kDataSizeForSeparateDevices = 6; |
| + |
| // The mean acceleration due to gravity on Earth in m/s^2. |
| const float kMeanGravity = 9.80665f; |
| @@ -82,7 +97,24 @@ bool ReadFileToInt(const base::FilePath& path, int* value) { |
| } |
| base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s); |
| if (!base::StringToInt(s, value)) { |
| - LOG(ERROR) << "Failed to parse \"" << s << "\" from " << path.value(); |
| + LOG(ERROR) << "Failed to parse int \"" << s << "\" from " << path.value(); |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +// Reads |path| to the double pointed to by |value|. Returns true on success or |
| +// false on failure. |
| +bool ReadFileToDouble(const base::FilePath& path, double* value) { |
| + std::string s; |
| + DCHECK(value); |
| + if (!base::ReadFileToString(path, &s)) { |
| + return false; |
| + } |
| + base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s); |
| + if (!base::StringToDouble(s, value)) { |
| + LOG(ERROR) << "Failed to parse double \"" << s << "\" from " |
| + << path.value(); |
| return false; |
| } |
| return true; |
| @@ -136,14 +168,43 @@ class AccelerometerFileReader |
| // Index of each accelerometer axis in data stream. |
| int index[ACCELEROMETER_SOURCE_COUNT][3]; |
| + |
| + // If true the accelerometer devices must be read from distinct files. |
| + bool read_device_separately; |
| + |
| + // Length of reading per accelerometer |
| + size_t device_length[ACCELEROMETER_SOURCE_COUNT]; |
| + |
| + // The path to append to |kAccelerometerDevicePath| where the accelerometer |
| + // device resides. |
| + base::FilePath device_path[ACCELEROMETER_SOURCE_COUNT]; |
| }; |
| ~AccelerometerFileReader() {} |
| + // When accelerometers are presented as separate iio_devices this will perform |
| + // the initialize for one of the devices, at the given |iio_path| and the |
| + // symbolic link |name|. |location| is defined by AccelerometerSoure. |
| + bool InitializeDevice(const base::FilePath& iio_path, |
| + const base::FilePath& name, |
| + std::string* location); |
| + |
| + // When accelerometers are presented as a single iio_device this will perform |
| + // the initialization for both of them. |
| + void InitializeBothAccelerometers(const base::FilePath& iio_path); |
|
flackr
2015/08/21 19:12:03
To emphasize that these two methods are for doing
jonross
2015/08/24 18:30:34
I was tempted to do this when I thought we would b
flackr
2015/08/25 21:27:55
I think it's still appropriate. Include in the com
jonross
2015/08/25 22:26:07
We might want to switch to the gyp flag style. Fro
|
| + |
| // Attempts to read the accelerometer data. Upon a success, converts the raw |
| // reading to an AccelerometerUpdate and notifies observers. |
| void ReadFileAndNotify(); |
| + // When accelerometers are presented as separate iio_devices this will perform |
| + // the read of both devices, from their separate locations. |
| + bool ReadSeparateDevices(); |
| + |
| + // When accelerometers are presented as a single iio_device this will perform |
| + // the read of the device. |
| + bool ReadUnifiedDevice(); |
| + |
| // True if Initialize completed successfully, and there is an accelerometer |
| // file to read. |
| bool initialization_successful_; |
| @@ -179,8 +240,10 @@ void AccelerometerFileReader::Initialize( |
| // Check for accelerometer symlink which will be created by the udev rules |
| // file on detecting the device. |
| base::FilePath device; |
| - if (!base::ReadSymbolicLink(base::FilePath(kAccelerometerDevicePath), |
| - &device)) { |
| + |
| + if (base::IsDirectoryEmpty(base::FilePath(kAccelerometerDevicePath))) { |
| + LOG(ERROR) << "Accelerometer device directory is empty at " |
| + << kAccelerometerDevicePath; |
| return; |
| } |
| @@ -190,52 +253,29 @@ void AccelerometerFileReader::Initialize( |
| return; |
| } |
| - base::FilePath iio_path(base::FilePath(kAccelerometerIioBasePath).Append( |
| - device)); |
| - |
| - // Read the scale for all axes. |
| - int scale_divisor = 0; |
| - bool per_source_scale = |
| - !ReadFileToInt(iio_path.Append(kScaleFileName), &scale_divisor); |
| - if (!per_source_scale && scale_divisor == 0) { |
| - LOG(ERROR) << "Accelerometer " << kScaleFileName |
| - << "has scale of 0 and will not be used."; |
| - return; |
| - } |
| - |
| - // Read configuration of each accelerometer axis from each accelerometer from |
| - // /sys/bus/iio/devices/iio:deviceX/. |
| - for (size_t i = 0; i < arraysize(kAccelerometerNames); ++i) { |
| - if (per_source_scale) { |
| - configuration_.has[i] = false; |
| - // Read scale of accelerometer. |
| - std::string accelerometer_scale_path = base::StringPrintf( |
| - kSourceScaleNameFormatString, kAccelerometerNames[i]); |
| - if (!ReadFileToInt(iio_path.Append(accelerometer_scale_path.c_str()), |
| - &scale_divisor)) { |
| - continue; |
| - } |
| - if (scale_divisor == 0) { |
| - LOG(ERROR) << "Accelerometer " << accelerometer_scale_path |
| - << "has scale of 0 and will not be used."; |
| - continue; |
| - } |
| + base::FileEnumerator symlink_dir(base::FilePath(kAccelerometerDevicePath), |
| + false, base::FileEnumerator::FILES); |
| + for (base::FilePath name = symlink_dir.Next(); !name.empty(); |
| + name = symlink_dir.Next()) { |
| + base::FilePath iio_device; |
| + if (!base::ReadSymbolicLink(name, &iio_device)) { |
| + LOG(ERROR) << "Failed to read symbolic link " << kAccelerometerDevicePath |
| + << "/" << name.MaybeAsASCII() << "\n"; |
| + return; |
| } |
| - configuration_.has[i] = true; |
| - for (size_t j = 0; j < arraysize(kAccelerometerAxes); ++j) { |
| - configuration_.scale[i][j] = kMeanGravity / scale_divisor; |
| - std::string accelerometer_index_path = base::StringPrintf( |
| - kAccelerometerScanIndexPath, kAccelerometerAxes[j], |
| - kAccelerometerNames[i]); |
| - if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()), |
| - &(configuration_.index[i][j]))) { |
| - configuration_.has[i] = false; |
| - break; |
| - } |
| + base::FilePath iio_path(base::FilePath(kAccelerometerIioBasePath) |
| + .Append(iio_device.BaseName())); |
| + std::string location; |
| + if (base::ReadFileToString( |
| + base::FilePath(iio_path).Append(kAccelerometerLocationFileName), |
| + &location)) { |
| + if (!InitializeDevice(iio_path, name, &location)) |
| + return; |
| + } else { |
| + InitializeBothAccelerometers(iio_path); |
| + configuration_.device_path[ACCELEROMETER_SOURCE_SCREEN] = name.BaseName(); |
| } |
| - if (configuration_.has[i]) |
| - configuration_.count++; |
| } |
| // Adjust the directions of accelerometers to match the AccelerometerUpdate |
| @@ -253,13 +293,16 @@ void AccelerometerFileReader::Initialize( |
| if (configuration_.index[i][j] < 0 || |
| configuration_.index[i][j] >= |
| 3 * static_cast<int>(configuration_.count)) { |
| + const char* axis = configuration_.read_device_separately |
| + ? kSeparateAccelerometerAxes[j] |
| + : kUnifiedAccelerometerAxes[j]; |
| LOG(ERROR) << "Field index for " << kAccelerometerNames[i] << " " |
| - << kAccelerometerAxes[j] << " axis out of bounds."; |
| + << axis << " axis out of bounds."; |
| return; |
| } |
| } |
| } |
| - configuration_.length = kDataSize * 3 * configuration_.count; |
| + |
| initialization_successful_ = true; |
| Read(); |
| } |
| @@ -289,32 +332,129 @@ void AccelerometerFileReader::RemoveObserver( |
| observers_->RemoveObserver(observer); |
| } |
| +bool AccelerometerFileReader::InitializeDevice(const base::FilePath& iio_path, |
| + const base::FilePath& name, |
| + std::string* location) { |
|
flackr
2015/08/21 19:12:02
Should be able to share most of this code with the
jonross
2015/08/24 18:30:35
Will look at merging as much as possible.
|
| + configuration_.read_device_separately = true; |
| + base::TrimWhitespaceASCII(*location, base::TRIM_ALL, location); |
|
flackr
2015/08/21 19:12:03
You should do this before passing the string in an
jonross
2015/08/24 18:30:34
Done.
|
| + int config_index = -1; |
| + for (size_t j = 0; j < arraysize(kAccelerometerNames); ++j) { |
|
flackr
2015/08/21 19:12:02
Use std::find
jonross
2015/08/24 18:30:34
Done.
|
| + if (*location == kAccelerometerNames[j]) { |
| + config_index = j; |
| + break; |
| + } |
| + } |
| + |
| + if (config_index == -1) { |
| + LOG(ERROR) << "Invalid location: " << location << "\n"; |
|
flackr
2015/08/21 19:12:03
Identify which device this came from. Also let's c
jonross
2015/08/24 18:30:34
Done.
|
| + return false; |
| + } |
| + configuration_.device_path[config_index] = name.BaseName(); |
| + configuration_.has[config_index] = true; |
| + |
| + double scale; |
| + if (!ReadFileToDouble(iio_path.Append(kAccelerometerScaleFileName), &scale)) { |
|
flackr
2015/08/21 19:12:03
nit: no curlies { }
jonross
2015/08/24 18:30:34
Done.
|
| + return false; |
| + } |
| + |
| + for (size_t j = 0; j < arraysize(kSeparateAccelerometerAxes); ++j) { |
| + std::string accelerometer_index_path = base::StringPrintf( |
| + kSeparateAccelerometerScanIndexPath, kSeparateAccelerometerAxes[j]); |
| + if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()), |
| + &(configuration_.index[config_index][j]))) { |
| + configuration_.has[config_index] = false; |
|
flackr
2015/08/21 19:12:03
Since the format has changed again, we should know
jonross
2015/08/24 18:30:35
Done.
|
| + break; |
| + } |
| + configuration_.scale[config_index][j] = scale; |
| + } |
| + configuration_.device_length[config_index] = kDataSizeForSeparateDevices; |
| + if (configuration_.has[config_index]) |
| + configuration_.count++; |
| + return true; |
| +} |
| + |
| +void AccelerometerFileReader::InitializeBothAccelerometers( |
| + const base::FilePath& iio_path) { |
| + // Read the scale for all axes. |
| + int scale_divisor = 0; |
| + // Read configuration of each accelerometer axis from each accelerometer from |
| + // /sys/bus/iio/devices/iio:deviceX/. |
| + for (size_t i = 0; i < arraysize(kAccelerometerNames); ++i) { |
| + configuration_.has[i] = false; |
| + // Read scale of accelerometer. |
| + std::string accelerometer_scale_path = base::StringPrintf( |
| + kSourceScaleNameFormatString, kAccelerometerNames[i]); |
| + if (!ReadFileToInt(iio_path.Append(accelerometer_scale_path.c_str()), |
| + &scale_divisor)) { |
| + continue; |
| + } |
| + if (scale_divisor == 0) { |
| + LOG(ERROR) << "Accelerometer " << accelerometer_scale_path |
| + << "has scale of 0 and will not be used."; |
| + continue; |
| + } |
| + |
| + configuration_.has[i] = true; |
| + for (size_t j = 0; j < arraysize(kUnifiedAccelerometerAxes); ++j) { |
| + configuration_.scale[i][j] = kMeanGravity / scale_divisor; |
| + std::string accelerometer_index_path = base::StringPrintf( |
| + kAccelerometerScanIndexPath, kUnifiedAccelerometerAxes[j], |
| + kAccelerometerNames[i]); |
| + if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()), |
| + &(configuration_.index[i][j]))) { |
| + configuration_.has[i] = false; |
|
flackr
2015/08/21 19:12:03
Same rationale as 365, make this fatal again.
jonross
2015/08/24 18:30:34
Done.
|
| + break; |
| + } |
| + } |
| + if (configuration_.has[i]) |
| + configuration_.count++; |
| + } |
| + |
| + configuration_.length = kDataSize * 3 * configuration_.count; |
| +} |
| + |
| void AccelerometerFileReader::ReadFileAndNotify() { |
| DCHECK(initialization_successful_); |
| - char reading[kSizeOfReading]; |
| // Initiate the trigger to read accelerometers simultaneously |
| int bytes_written = base::WriteFile( |
| base::FilePath(kAccelerometerTriggerPath), "1\n", 2); |
| if (bytes_written < 2) { |
| - PLOG(ERROR) << "Accelerometer trigger failure: " << bytes_written; |
| + LOG(ERROR) << "Accelerometer trigger failure: " << bytes_written; |
|
flackr
2015/08/21 19:12:03
Keep the PLOG here. The purpose is to display the
jonross
2015/08/24 18:30:34
Done.
|
| return; |
| } |
| // Read resulting sample from /dev/cros-ec-accel. |
| - int bytes_read = base::ReadFile(base::FilePath(kAccelerometerDevicePath), |
| - reading, configuration_.length); |
| - if (bytes_read < static_cast<int>(configuration_.length)) { |
| - LOG(ERROR) << "Read " << bytes_read << " byte(s), expected " |
| - << configuration_.length << " bytes from accelerometer"; |
| - return; |
| + bool result; |
| + update_ = new AccelerometerUpdate(); |
| + if (configuration_.read_device_separately) { |
|
flackr
2015/08/21 19:12:03
Thinking this over, it's not clear to me that the
jonross
2015/08/24 18:30:34
The updated init gives us:
old config = {"/dev/cro
flackr
2015/08/25 21:27:55
I think my suggestion was misunderstood, I was sug
jonross
2015/08/25 22:26:07
Ah, I did misinterpret your previous comment there
jonross
2015/08/26 17:40:59
Done.
|
| + result = ReadSeparateDevices(); |
| + } else { |
| + result = ReadUnifiedDevice(); |
| } |
| + if (!result) |
| + return; |
| - update_ = new AccelerometerUpdate(); |
| + observers_->Notify(FROM_HERE, |
| + &AccelerometerReader::Observer::OnAccelerometerUpdated, |
| + update_); |
| +} |
| +bool AccelerometerFileReader::ReadSeparateDevices() { |
| + char reading[kSizeOfReading]; |
| + int bytes_read; |
| for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { |
| if (!configuration_.has[i]) |
| continue; |
| + bytes_read = base::ReadFile(base::FilePath(kAccelerometerDevicePath) |
| + .Append(configuration_.device_path[i]), |
| + reading, configuration_.device_length[i]); |
| + if (bytes_read < static_cast<int>(configuration_.device_length[i])) { |
| + LOG(ERROR) << "Acceleromter Read " << bytes_read << " byte(s), expected " |
|
flackr
2015/08/21 19:12:02
nit: s/Accelerometer/Accelerometer
Since this is
jonross
2015/08/24 18:30:34
Done.
|
| + << configuration_.device_length[i] |
| + << " bytes from accelerometer"; |
| + return false; |
| + } |
| int16* values = reinterpret_cast<int16*>(reading); |
| update_->Set( |
| @@ -323,13 +463,36 @@ void AccelerometerFileReader::ReadFileAndNotify() { |
| values[configuration_.index[i][1]] * configuration_.scale[i][1], |
| values[configuration_.index[i][2]] * configuration_.scale[i][2]); |
| } |
| + return true; |
| +} |
| - observers_->Notify(FROM_HERE, |
| - &AccelerometerReader::Observer::OnAccelerometerUpdated, |
| - update_); |
| +bool AccelerometerFileReader::ReadUnifiedDevice() { |
| + char reading[kSizeOfReading]; |
| + int bytes_read = base::ReadFile( |
| + base::FilePath(kAccelerometerDevicePath) |
| + .Append(configuration_.device_path[ACCELEROMETER_SOURCE_SCREEN]), |
| + reading, configuration_.length); |
| + if (bytes_read < static_cast<int>(configuration_.length)) { |
| + LOG(ERROR) << "Accelerometer Read " << bytes_read << " byte(s), expected " |
| + << configuration_.length << " bytes from accelerometer"; |
| + return false; |
| + } |
| + |
| + for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { |
| + if (!configuration_.has[i]) |
| + continue; |
| + int16* values = reinterpret_cast<int16*>(reading); |
| + update_->Set( |
| + static_cast<AccelerometerSource>(i), |
| + values[configuration_.index[i][0]] * configuration_.scale[i][0], |
| + values[configuration_.index[i][1]] * configuration_.scale[i][1], |
| + values[configuration_.index[i][2]] * configuration_.scale[i][2]); |
| + } |
| + return true; |
| } |
| -AccelerometerFileReader::ConfigurationData::ConfigurationData() : count(0) { |
| +AccelerometerFileReader::ConfigurationData::ConfigurationData() |
| + : count(0), read_device_separately(false) { |
| for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { |
| has[i] = false; |
| for (int j = 0; j < 3; ++j) { |