Skip to content

Commit

Permalink
accelerometer: Update Ash Adding AccelerometerProviderMojo
Browse files Browse the repository at this point in the history
This commit refactors ash/accelerometer that if group iioservice exists,
registering to Sensor Hal Dispatcher and waiting for samples updated
from iioservice instead of reading on sysfs files directly for
accelerometers' data.

BUG=b:172208566, b:172414227, b:168434557
TEST=builds, unit tests, and run on octopus & jacuzzi. Accels' samples
are updated successfully, and the auto-rotate screen is working.

Cq-Depend: chromium:2470222
Change-Id: Icf65705b2b8facb7fe79423edba163639e2ae45f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2345942
Reviewed-by: Xiaoqian Dai <xdai@chromium.org>
Reviewed-by: Mitsuru Oshima <oshima@chromium.org>
Commit-Queue: Cheng-Hao Yang <chenghaoyang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827626}
  • Loading branch information
HarveyCHYang authored and Commit Bot committed Nov 15, 2020
1 parent 706cc01 commit 0196b31
Show file tree
Hide file tree
Showing 14 changed files with 1,616 additions and 54 deletions.
12 changes: 12 additions & 0 deletions ash/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,15 @@ component("ash") {
"accelerators/pre_target_accelerator_handler.h",
"accelerators/spoken_feedback_toggler.cc",
"accelerators/spoken_feedback_toggler.h",
"accelerometer/accelerometer_constants.h",
"accelerometer/accelerometer_file_reader.cc",
"accelerometer/accelerometer_file_reader.h",
"accelerometer/accelerometer_provider_mojo.cc",
"accelerometer/accelerometer_provider_mojo.h",
"accelerometer/accelerometer_reader.cc",
"accelerometer/accelerometer_reader.h",
"accelerometer/accelerometer_samples_observer.cc",
"accelerometer/accelerometer_samples_observer.h",
"accelerometer/accelerometer_types.cc",
"accelerometer/accelerometer_types.h",
"accessibility/accessibility_controller_impl.cc",
Expand Down Expand Up @@ -1763,6 +1768,8 @@ component("ash") {
# TODO(https://crbug.com/644336): Make CrasAudioHandler Chrome or Ash only.
"//chromeos/audio",
"//chromeos/components/multidevice/logging",
"//chromeos/components/sensors:sensors",
"//chromeos/components/sensors/mojom",
"//chromeos/constants",
"//chromeos/dbus",

Expand Down Expand Up @@ -1920,6 +1927,8 @@ test("ash_unittests") {
"accelerators/accelerator_unittest.cc",
"accelerators/magnifier_key_scroller_unittest.cc",
"accelerators/spoken_feedback_toggler_unittest.cc",
"accelerometer/accelerometer_provider_mojo_unittest.cc",
"accelerometer/accelerometer_samples_observer_unittest.cc",
"accessibility/accessibility_controller_unittest.cc",
"accessibility/accessibility_focus_ring_controller_unittest.cc",
"accessibility/accessibility_focus_ring_group_unittest.cc",
Expand Down Expand Up @@ -2366,6 +2375,9 @@ test("ash_unittests") {
"//chromeos/components/bloom/public/cpp",
"//chromeos/components/phonehub:test_support",
"//chromeos/components/quick_answers:quick_answers",
"//chromeos/components/sensors:sensors",
"//chromeos/components/sensors:test_support",
"//chromeos/components/sensors/mojom",
"//chromeos/constants",
"//chromeos/dbus:test_support",
"//chromeos/dbus/audio",
Expand Down
1 change: 1 addition & 0 deletions ash/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ include_rules = [
"+chromeos/components/proximity_auth/public/mojom",
"+chromeos/components/quick_answers",
"+chromeos/components/security_token_pin",
"+chromeos/components/sensors",
"+chromeos/constants",
# TODO(https://crbug.com/940810): Eliminate this.
"+chromeos/dbus/initialize_dbus_client.h",
Expand Down
23 changes: 23 additions & 0 deletions ash/accelerometer/accelerometer_constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_ACCELEROMETER_ACCELEROMETER_CONSTANTS_H_
#define ASH_ACCELEROMETER_ACCELEROMETER_CONSTANTS_H_

#include "ash/accelerometer/accelerometer_types.h"

namespace ash {

const char kAccelerometerChannels[][8] = {"accel_x", "accel_y", "accel_z"};

// The number of axes for which there are accelerometer readings.
constexpr uint32_t kNumberOfAxes = 3u;

// The names of the accelerometers. Matches up with the enum AccelerometerSource
// in ash/accelerometer/accelerometer_types.h.
const char kLocationStrings[ACCELEROMETER_SOURCE_COUNT][5] = {"lid", "base"};

} // namespace ash

#endif // ASH_ACCELEROMETER_ACCELEROMETER_CONSTANTS_H_
44 changes: 22 additions & 22 deletions ash/accelerometer/accelerometer_file_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string>
#include <vector>

#include "ash/accelerometer/accelerometer_constants.h"
#include "ash/public/cpp/tablet_mode_observer.h"
#include "ash/shell.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
Expand All @@ -26,6 +27,8 @@
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/system/sys_info.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/task_runner.h"
#include "base/task_runner_util.h"
#include "base/threading/platform_thread.h"
Expand Down Expand Up @@ -78,11 +81,6 @@ constexpr char kLegacyAccelerometerScanIndexPathFormatString[] =
constexpr char kAccelerometerScanIndexPathFormatString[] =
"scan_elements/in_accel_%s_index";

// The names of the accelerometers. Matches up with the enum AccelerometerSource
// in ash/accelerometer/accelerometer_types.h.
constexpr char kAccelerometerNames[ACCELEROMETER_SOURCE_COUNT][5] = {"lid",
"base"};

// The axes on each accelerometer. The order was changed on kernel 3.18+.
constexpr char kAccelerometerAxes[][2] = {"x", "y", "z"};
constexpr char kLegacyAccelerometerAxes[][2] = {"y", "x", "z"};
Expand All @@ -93,9 +91,6 @@ constexpr size_t kMaxAsciiUintLength = 21;
// The size of individual values.
constexpr size_t kDataSize = 2;

// The number of axes for which there are acceleration readings.
constexpr int kNumberOfAxes = 3;

// The size of data in one reading of the accelerometers.
constexpr int kSizeOfReading = kDataSize * kNumberOfAxes;

Expand Down Expand Up @@ -150,9 +145,14 @@ AccelerometerFileReader::AccelerometerFileReader()
: observers_(
new base::ObserverListThreadSafe<AccelerometerReader::Observer>()) {}

void AccelerometerFileReader::PrepareAndInitialize(
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner) {
task_runner_ = sequenced_task_runner;
void AccelerometerFileReader::PrepareAndInitialize() {
// AccelerometerReader is important for screen orientation so we need
// USER_VISIBLE priority.
// Use CONTINUE_ON_SHUTDOWN to avoid blocking shutdown since the datareading
// could get blocked on certain devices. See https://crbug.com/1023989.
task_runner_ = base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN});

initialization_state_ = State::INITIALIZING;

Expand Down Expand Up @@ -287,8 +287,8 @@ void AccelerometerFileReader::InitializeInternal() {
3 * static_cast<int>(configuration_.count)) {
const char* axis = legacy_cross_accel ? kLegacyAccelerometerAxes[j]
: kAccelerometerAxes[j];
LOG(ERROR) << "Field index for " << kAccelerometerNames[i] << " "
<< axis << " axis out of bounds.";
LOG(ERROR) << "Field index for " << kLocationStrings[i] << " " << axis
<< " axis out of bounds.";
initialization_state_ = State::FAILED;
return;
}
Expand Down Expand Up @@ -438,12 +438,12 @@ bool AccelerometerFileReader::InitializeAccelerometer(
const base::FilePath& name,
const std::string& location) {
size_t config_index = 0;
for (; config_index < base::size(kAccelerometerNames); ++config_index) {
if (location == kAccelerometerNames[config_index])
for (; config_index < base::size(kLocationStrings); ++config_index) {
if (location == kLocationStrings[config_index])
break;
}

if (config_index >= base::size(kAccelerometerNames)) {
if (config_index >= base::size(kLocationStrings)) {
LOG(ERROR) << "Unrecognized location: " << location << " for device "
<< name.MaybeAsASCII() << "\n";
return false;
Expand Down Expand Up @@ -487,11 +487,11 @@ bool AccelerometerFileReader::InitializeLegacyAccelerometers(
base::FilePath(kAccelerometerDevicePath).Append(name.BaseName());
// Read configuration of each accelerometer axis from each accelerometer from
// /sys/bus/iio/devices/iio:deviceX/.
for (size_t i = 0; i < base::size(kAccelerometerNames); ++i) {
for (size_t i = 0; i < base::size(kLocationStrings); ++i) {
configuration_.has[i] = false;
// Read scale of accelerometer.
std::string accelerometer_scale_path = base::StringPrintf(
kLegacyScaleNameFormatString, kAccelerometerNames[i]);
std::string accelerometer_scale_path =
base::StringPrintf(kLegacyScaleNameFormatString, kLocationStrings[i]);
// Read the scale for all axes.
int scale_divisor = 0;
if (!ReadFileToInt(iio_path.Append(accelerometer_scale_path.c_str()),
Expand All @@ -507,9 +507,9 @@ bool AccelerometerFileReader::InitializeLegacyAccelerometers(
configuration_.has[i] = true;
for (size_t j = 0; j < base::size(kLegacyAccelerometerAxes); ++j) {
configuration_.scale[i][j] = base::kMeanGravityFloat / scale_divisor;
std::string accelerometer_index_path = base::StringPrintf(
kLegacyAccelerometerScanIndexPathFormatString,
kLegacyAccelerometerAxes[j], kAccelerometerNames[i]);
std::string accelerometer_index_path =
base::StringPrintf(kLegacyAccelerometerScanIndexPathFormatString,
kLegacyAccelerometerAxes[j], kLocationStrings[i]);
if (!ReadFileToInt(iio_path.Append(accelerometer_index_path.c_str()),
&(configuration_.index[i][j]))) {
configuration_.has[i] = false;
Expand Down
13 changes: 4 additions & 9 deletions ash/accelerometer/accelerometer_file_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

namespace ash {

enum class State { INITIALIZING, SUCCESS, FAILED };

// Work that runs on a base::TaskRunner. It determines the accelerometer
// configuration, and reads the data. Upon a successful read it will notify
// all observers.
Expand All @@ -28,8 +26,7 @@ class AccelerometerFileReader : public AccelerometerProviderInterface,
AccelerometerFileReader& operator=(const AccelerometerFileReader&) = delete;

// AccelerometerProviderInterface:
void PrepareAndInitialize(
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner) override;
void PrepareAndInitialize() override;
void AddObserver(AccelerometerReader::Observer* observer) override;
void RemoveObserver(AccelerometerReader::Observer* observer) override;
void StartListenToTabletModeController() override;
Expand All @@ -48,8 +45,9 @@ class AccelerometerFileReader : public AccelerometerProviderInterface,
// Tracks if accelerometer initialization is completed.
void CheckInitStatus();

// With ChromeOS EC lid angle driver present, accelerometer read is cancelled
// in clamshell mode, and triggered when entering tablet mode.
// With ChromeOS EC lid angle driver present, it's triggered when the device
// is physically used as a tablet (even thought its UI might be in clamshell
// mode), cancelled otherwise.
void TriggerRead();
void CancelRead();

Expand Down Expand Up @@ -130,9 +128,6 @@ class AccelerometerFileReader : public AccelerometerProviderInterface,

void SetEmitEventsInternal(bool emit_events);

// The current initialization state of reader.
State initialization_state_ = State::INITIALIZING;

// True if periodical accelerometer read is on.
bool accelerometer_read_on_ = false;

Expand Down
Loading

0 comments on commit 0196b31

Please sign in to comment.