1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/power/auto_screen_brightness/light_provider_mojo.h"
#include <iterator>
#include <utility>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "chromeos/components/sensors/ash/sensor_hal_dispatcher.h"
namespace {
// Delay of the reconnection to Sensor Hal Dispatcher.
constexpr base::TimeDelta kDelayReconnect = base::Milliseconds(1000);
constexpr base::TimeDelta kNewDevicesTimeout = base::Milliseconds(10000);
constexpr char kCrosECLightName[] = "cros-ec-light";
constexpr char kAcpiAlsName[] = "acpi-als";
} // namespace
namespace ash {
namespace power {
namespace auto_screen_brightness {
LightProviderMojo::LightProviderMojo(AlsReader* als_reader)
: LightProviderInterface(als_reader) {
RegisterSensorClient();
}
LightProviderMojo::~LightProviderMojo() = default;
void LightProviderMojo::SetUpChannel(
mojo::PendingRemote<chromeos::sensors::mojom::SensorService>
pending_remote) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (sensor_service_remote_.is_bound()) {
LOG(ERROR) << "Ignoring the second Remote<SensorService>";
return;
}
DCHECK(!new_devices_observer_.is_bound());
sensor_service_remote_.Bind(std::move(pending_remote));
sensor_service_remote_.set_disconnect_handler(
base::BindOnce(&LightProviderMojo::OnSensorServiceDisconnect,
weak_ptr_factory_.GetWeakPtr()));
if (light_device_id_.has_value()) {
SetupLightSamplesObserver();
auto& light = lights_[light_device_id_.value()];
DCHECK(!light.ignored);
DCHECK(light.name.has_value());
if (light.name.value().compare(kCrosECLightName) == 0 &&
light.on_lid.value_or(false)) {
return;
}
}
sensor_service_remote_->RegisterNewDevicesObserver(
new_devices_observer_.BindNewPipeAndPassRemote());
new_devices_observer_.set_disconnect_handler(
base::BindOnce(&LightProviderMojo::OnNewDevicesObserverDisconnect,
weak_ptr_factory_.GetWeakPtr()));
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&LightProviderMojo::OnNewDevicesTimeout,
weak_ptr_factory_.GetWeakPtr()),
kNewDevicesTimeout);
QueryDevices();
}
void LightProviderMojo::OnNewDeviceAdded(
int32_t iio_device_id,
const std::vector<chromeos::sensors::mojom::DeviceType>& types) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!base::Contains(types, chromeos::sensors::mojom::DeviceType::LIGHT)) {
// Not a light sensor. Ignoring this device.
return;
}
RegisterLightWithId(iio_device_id);
}
LightProviderMojo::LightData::LightData() = default;
LightProviderMojo::LightData::~LightData() = default;
void LightProviderMojo::OnNewDevicesObserverDisconnect() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
LOG(ERROR)
<< "OnNewDevicesObserverDisconnect. Mojo connection to IIO "
"Service is lost. Resetting the SensorService Mojo channel as well";
// Assumes IIO Service has crashed and waits for its relaunch.
ResetSensorService();
}
void LightProviderMojo::OnNewDevicesTimeout() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
new_devices_observer_.reset();
if (als_init_status_set_)
return;
als_init_status_set_ = true;
LOG(ERROR) << "Target light sensor isn't available after timeout. "
"Initialization failed.";
lights_.clear();
ResetSensorService();
sensor_hal_client_.reset();
als_reader_->SetAlsInitStatus(AlsReader::AlsInitStatus::kIncorrectConfig);
}
void LightProviderMojo::RegisterSensorClient() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
chromeos::sensors::SensorHalDispatcher::GetInstance()->RegisterClient(
sensor_hal_client_.BindNewPipeAndPassRemote());
sensor_hal_client_.set_disconnect_handler(
base::BindOnce(&LightProviderMojo::OnSensorHalClientFailure,
weak_ptr_factory_.GetWeakPtr()));
}
void LightProviderMojo::OnSensorHalClientFailure() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
LOG(ERROR) << "OnSensorHalClientFailure. Mojo connection to "
"SensorHalDispatcher is lost.";
ResetSensorService();
sensor_hal_client_.reset();
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&LightProviderMojo::RegisterSensorClient,
weak_ptr_factory_.GetWeakPtr()),
kDelayReconnect);
}
void LightProviderMojo::OnSensorServiceDisconnect() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
LOG(ERROR)
<< "OnSensorServiceDisconnect. Mojo connection to IIO Service is lost";
ResetSensorService();
}
void LightProviderMojo::ResetSensorService() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observer_.reset();
for (auto& light : lights_)
light.second.remote.reset();
new_devices_observer_.reset();
sensor_service_remote_.reset();
}
void LightProviderMojo::ResetStates() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observer_.reset();
light_device_id_.reset();
lights_.clear();
if (sensor_service_remote_.is_bound())
QueryDevices();
}
void LightProviderMojo::QueryDevices() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(sensor_service_remote_.is_bound());
sensor_service_remote_->GetDeviceIds(
chromeos::sensors::mojom::DeviceType::LIGHT,
base::BindOnce(&LightProviderMojo::GetLightIdsCallback,
weak_ptr_factory_.GetWeakPtr()));
}
void LightProviderMojo::GetLightIdsCallback(
const std::vector<int32_t>& light_ids) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (int32_t id : light_ids)
RegisterLightWithId(id);
}
void LightProviderMojo::RegisterLightWithId(int32_t id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(sensor_service_remote_.is_bound());
auto& light = lights_[id];
if (light.ignored || light.name.has_value() || light.on_lid.has_value()) {
// The attributes have already been retrieved. Would've been used if chosen
// in |SetUpChannel|.
return;
}
DCHECK(!light.remote.is_bound());
light.remote = GetSensorDeviceRemote(id);
light.remote->GetAttributes(
std::vector<std::string>{chromeos::sensors::mojom::kDeviceName,
chromeos::sensors::mojom::kLocation},
base::BindOnce(&LightProviderMojo::GetNameLocationCallback,
weak_ptr_factory_.GetWeakPtr(), id));
}
void LightProviderMojo::GetNameLocationCallback(
int32_t id,
const std::vector<std::optional<std::string>>& values) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_NE(light_device_id_.value_or(-1), id);
if (light_device_id_.has_value()) {
auto& orig_light = lights_[light_device_id_.value()];
if (orig_light.name.has_value() &&
orig_light.name.value().compare(kCrosECLightName) == 0 &&
orig_light.on_lid.value_or(false)) {
// Already has the cros-ec-light on the lid. Ignoring other light sensors.
IgnoreLight(id);
return;
}
}
if (values.size() < 2) {
LOG(ERROR) << "Sensor values don't contain name and location attribute";
IgnoreLight(id);
return;
}
if (values.size() != 2) {
LOG(WARNING)
<< "Sensor values contain more than name and location attribute. Size: "
<< values.size();
}
auto& light = lights_[id];
DCHECK(light.remote.is_bound());
light.name = values[0];
light.on_lid =
values[1].has_value() &&
(values[1].value().compare(chromeos::sensors::mojom::kLocationLid) == 0);
if (light.name.has_value() &&
light.name.value().compare(kCrosECLightName) == 0) {
if (light.on_lid.value_or(false)) {
// If a light sensor was chosen, migrate to this cros-ec-light light
// sensor.
DetermineLightSensor(id);
new_devices_observer_.reset(); // Don't need new light sensors anymore.
return;
}
if (light_device_id_.has_value()) {
auto& orig_light = lights_[light_device_id_.value()];
if (orig_light.name.has_value() &&
orig_light.name.value().compare(kCrosECLightName) == 0) {
// Already has the cros-ec-light. Ignoring other non-lid cros-ec-light.
IgnoreLight(id);
return;
}
}
// Choose the current non-lid cros-ec-light.
DetermineLightSensor(id);
}
if (light_device_id_.has_value()) {
// Use the current light sensor over the current non cros-ec-light.
IgnoreLight(id);
return;
}
if (!light.name.has_value() ||
light.name.value().compare(kAcpiAlsName) != 0) {
LOG(WARNING) << "Unexpected light name: " << light.name.value_or("");
}
// Choose the current acpi-als.
DetermineLightSensor(id);
}
void LightProviderMojo::IgnoreLight(int32_t id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto& light = lights_[id];
light.ignored = true;
light.remote.reset();
}
mojo::Remote<chromeos::sensors::mojom::SensorDevice>
LightProviderMojo::GetSensorDeviceRemote(int32_t id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(sensor_service_remote_.is_bound());
auto& light = lights_[id];
if (light.remote.is_bound()) {
// Reuse the previous remote.
return std::move(light.remote);
}
mojo::Remote<chromeos::sensors::mojom::SensorDevice> sensor_device_remote;
sensor_service_remote_->GetDevice(
id, sensor_device_remote.BindNewPipeAndPassReceiver());
sensor_device_remote.set_disconnect_with_reason_handler(
base::BindOnce(&LightProviderMojo::OnLightRemoteDisconnect,
weak_ptr_factory_.GetWeakPtr(), id));
return sensor_device_remote;
}
void LightProviderMojo::OnLightRemoteDisconnect(
int32_t id,
uint32_t custom_reason_code,
const std::string& description) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const auto reason =
static_cast<chromeos::sensors::mojom::SensorDeviceDisconnectReason>(
custom_reason_code);
LOG(WARNING) << "OnLightRemoteDisconnect: " << id << ", reason: " << reason
<< ", description: " << description;
// Assumes IIO Service has crashed and waits for its relaunch.
switch (reason) {
case chromeos::sensors::mojom::SensorDeviceDisconnectReason::
IIOSERVICE_CRASHED:
ResetSensorService();
break;
case chromeos::sensors::mojom::SensorDeviceDisconnectReason::DEVICE_REMOVED:
if (light_device_id_ != id) {
// This light sensor is not in use.
lights_.erase(id);
} else {
// Reset usages & states, and restart the mojo devices initialization.
ResetStates();
}
break;
}
}
void LightProviderMojo::DetermineLightSensor(int32_t id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!als_init_status_set_) {
als_init_status_set_ = true;
als_reader_->SetAlsInitStatus(AlsReader::AlsInitStatus::kSuccess);
}
light_device_id_ = id;
SetupLightSamplesObserver();
}
void LightProviderMojo::SetupLightSamplesObserver() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(light_device_id_.has_value());
observer_ = std::make_unique<LightSamplesObserver>(
als_reader_, GetSensorDeviceRemote(light_device_id_.value()));
}
} // namespace auto_screen_brightness
} // namespace power
} // namespace ash
|