berlna: Clean up lights HAL
Change-Id: Iaa5f7b62261730d12ddbbdc0c87ef3c2fbf8b395
This commit is contained in:
parent
f9e7851d2b
commit
f364528ccf
3 changed files with 72 additions and 104 deletions
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2019 The Android Open Source Project
|
* Copyright (C) 2019 The Android Open Source Project
|
||||||
* Copyright (C) 2020-2021 The LineageOS Project
|
* Copyright (C) 2020-2022 The LineageOS Project
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -21,27 +21,26 @@
|
||||||
#include <android-base/file.h>
|
#include <android-base/file.h>
|
||||||
#include <android-base/logging.h>
|
#include <android-base/logging.h>
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
/* clang-format off */
|
/* clang-format off */
|
||||||
#define PPCAT_NX(A, B) A/B
|
#define PPCAT_NX(A, B) A/B
|
||||||
#define PPCAT(A, B) PPCAT_NX(A, B)
|
#define PPCAT(A, B) PPCAT_NX(A, B)
|
||||||
#define STRINGIFY_INNER(x) #x
|
#define STRINGIFY_INNER(x) #x
|
||||||
#define STRINGIFY(x) STRINGIFY_INNER(x)
|
#define STRINGIFY(x) STRINGIFY_INNER(x)
|
||||||
|
|
||||||
#define LEDS(x) PPCAT(/sys/class/leds, x)
|
#define CHARGING_ATTR(x) STRINGIFY(PPCAT(/sys/class/leds/charging, x))
|
||||||
#define WHITE_ATTR(x) STRINGIFY(PPCAT(LEDS(charging), x))
|
|
||||||
/* clang-format on */
|
/* clang-format on */
|
||||||
|
|
||||||
using ::android::base::ReadFileToString;
|
namespace aidl {
|
||||||
using ::android::base::WriteStringToFile;
|
namespace android {
|
||||||
|
namespace hardware {
|
||||||
|
namespace light {
|
||||||
|
|
||||||
// Default max brightness
|
namespace {
|
||||||
constexpr auto kDefaultMaxLedBrightness = 255;
|
|
||||||
|
|
||||||
// Write value to path and close file.
|
// Write value to path and close file.
|
||||||
bool WriteToFile(const std::string& path, uint32_t content) {
|
template <typename T>
|
||||||
return WriteStringToFile(std::to_string(content), path);
|
inline bool WriteToFile(const std::string& path, T content) {
|
||||||
|
return ::android::base::WriteStringToFile(std::to_string(content), path);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t RgbaToBrightness(uint32_t color) {
|
uint32_t RgbaToBrightness(uint32_t color) {
|
||||||
|
@ -63,97 +62,72 @@ uint32_t RgbaToBrightness(uint32_t color) {
|
||||||
return (77 * red + 150 * green + 29 * blue) >> 8;
|
return (77 * red + 150 * green + 29 * blue) >> 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline uint32_t RgbaToBrightness(uint32_t color, uint32_t max_brightness) {
|
|
||||||
return RgbaToBrightness(color) * max_brightness / 0xFF;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool IsLit(uint32_t color) {
|
inline bool IsLit(uint32_t color) {
|
||||||
return color & 0x00ffffff;
|
return color & 0x00ffffff;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // anonymous namespace
|
void ApplyNotificationState(const HwLightState& state) {
|
||||||
|
auto brightness = RgbaToBrightness(state.color);
|
||||||
|
|
||||||
namespace aidl {
|
// Turn off the leds (initially)
|
||||||
namespace android {
|
WriteToFile(CHARGING_ATTR(breath), 0);
|
||||||
namespace hardware {
|
if (state.flashMode == FlashMode::TIMED && state.flashOnMs > 0 && state.flashOffMs > 0) {
|
||||||
namespace light {
|
WriteToFile(CHARGING_ATTR(delay_on), state.flashOnMs);
|
||||||
|
WriteToFile(CHARGING_ATTR(delay_off), state.flashOffMs);
|
||||||
Lights::Lights() {
|
WriteToFile(CHARGING_ATTR(breath), 1);
|
||||||
std::map<int, std::function<void(int id, const HwLightState&)>> lights_{
|
|
||||||
{(int)LightType::NOTIFICATIONS,
|
|
||||||
[this](auto&&... args) { setLightNotification(args...); }},
|
|
||||||
{(int)LightType::BATTERY, [this](auto&&... args) { setLightNotification(args...); }},
|
|
||||||
{(int)LightType::BACKLIGHT, {}}};
|
|
||||||
|
|
||||||
std::vector<HwLight> availableLights;
|
|
||||||
for (auto const& pair : lights_) {
|
|
||||||
int id = pair.first;
|
|
||||||
HwLight hwLight{};
|
|
||||||
hwLight.id = id;
|
|
||||||
availableLights.emplace_back(hwLight);
|
|
||||||
}
|
|
||||||
mAvailableLights = availableLights;
|
|
||||||
mLights = lights_;
|
|
||||||
|
|
||||||
std::string buf;
|
|
||||||
|
|
||||||
if (ReadFileToString(WHITE_ATTR(max_brightness), &buf)) {
|
|
||||||
max_led_brightness_ = std::stoi(buf);
|
|
||||||
} else {
|
} else {
|
||||||
max_led_brightness_ = kDefaultMaxLedBrightness;
|
WriteToFile(CHARGING_ATTR(brightness), brightness);
|
||||||
LOG(ERROR) << "Failed to read max LED brightness, fallback to " << kDefaultMaxLedBrightness;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // anonymous namespace
|
||||||
|
|
||||||
ndk::ScopedAStatus Lights::setLightState(int id, const HwLightState& state) {
|
ndk::ScopedAStatus Lights::setLightState(int id, const HwLightState& state) {
|
||||||
auto it = mLights.find(id);
|
static_assert(kAvailableLights.size() == std::tuple_size_v<decltype(notif_states_)>);
|
||||||
if (it == mLights.end()) {
|
|
||||||
|
if (id == static_cast<int32_t>(LightType::BACKLIGHT)) {
|
||||||
|
// Stub backlight handling
|
||||||
|
return ndk::ScopedAStatus::ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update saved state first
|
||||||
|
bool found = false;
|
||||||
|
for (size_t i = 0; i < notif_states_.size(); ++i) {
|
||||||
|
if (kAvailableLights[i].id == id) {
|
||||||
|
notif_states_[i] = state;
|
||||||
|
LOG(DEBUG) << __func__ << ": updating id=" << id
|
||||||
|
<< ", type=" << toString(kAvailableLights[i].type);
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
LOG(ERROR) << "Light not supported";
|
LOG(ERROR) << "Light not supported";
|
||||||
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
|
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
|
||||||
}
|
}
|
||||||
|
|
||||||
it->second(id, state);
|
// Lit up in order or fallback to battery light if others are dim
|
||||||
|
for (size_t i = 0; i < notif_states_.size(); ++i) {
|
||||||
|
auto&& cur_state = notif_states_[i];
|
||||||
|
auto&& cur_light = kAvailableLights[i];
|
||||||
|
if (IsLit(cur_state.color) || cur_light.type == LightType::BATTERY) {
|
||||||
|
LOG(DEBUG) << __func__ << ": applying id=" << cur_light.id
|
||||||
|
<< ", type=" << toString(cur_light.type);
|
||||||
|
ApplyNotificationState(cur_state);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return ndk::ScopedAStatus::ok();
|
return ndk::ScopedAStatus::ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
ndk::ScopedAStatus Lights::getLights(std::vector<HwLight>* lights) {
|
ndk::ScopedAStatus Lights::getLights(std::vector<HwLight>* lights) {
|
||||||
for (auto i = mAvailableLights.begin(); i != mAvailableLights.end(); i++) {
|
lights->insert(lights->end(), kAvailableLights.begin(), kAvailableLights.end());
|
||||||
lights->push_back(*i);
|
// We don't handle backlight but still need to report as supported.
|
||||||
}
|
lights->push_back({static_cast<int32_t>(LightType::BACKLIGHT), 0, LightType::BACKLIGHT});
|
||||||
return ndk::ScopedAStatus::ok();
|
return ndk::ScopedAStatus::ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lights::setLightNotification(int id, const HwLightState& state) {
|
|
||||||
bool found = false;
|
|
||||||
for (auto&& [cur_id, cur_state] : notif_states_) {
|
|
||||||
if (cur_id == id) {
|
|
||||||
cur_state = state;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback to battery light
|
|
||||||
if (!found && (cur_id == (int)LightType::BATTERY || IsLit(cur_state.color))) {
|
|
||||||
found = true;
|
|
||||||
LOG(DEBUG) << __func__ << ": id=" << id;
|
|
||||||
applyNotificationState(cur_state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Lights::applyNotificationState(const HwLightState& state) {
|
|
||||||
int brightness = RgbaToBrightness(state.color, max_led_brightness_);
|
|
||||||
|
|
||||||
// Turn off the leds (initially)
|
|
||||||
WriteToFile(WHITE_ATTR(breath), 0);
|
|
||||||
if (state.flashMode == FlashMode::TIMED && state.flashOnMs > 0 && state.flashOffMs > 0) {
|
|
||||||
WriteToFile(WHITE_ATTR(delay_on), static_cast<uint32_t>(state.flashOnMs));
|
|
||||||
WriteToFile(WHITE_ATTR(delay_off), static_cast<uint32_t>(state.flashOffMs));
|
|
||||||
WriteToFile(WHITE_ATTR(breath), 1);
|
|
||||||
} else {
|
|
||||||
WriteToFile(WHITE_ATTR(brightness), brightness);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace light
|
} // namespace light
|
||||||
} // namespace hardware
|
} // namespace hardware
|
||||||
} // namespace android
|
} // namespace android
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2020 The Android Open Source Project
|
* Copyright (C) 2020 The Android Open Source Project
|
||||||
* Copyright (C) 2020-2021 The LineageOS Project
|
* Copyright (C) 2020-2022 The LineageOS Project
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -18,36 +18,27 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <aidl/android/hardware/light/BnLights.h>
|
#include <aidl/android/hardware/light/BnLights.h>
|
||||||
#include <hardware/hardware.h>
|
#include <array>
|
||||||
#include <hardware/lights.h>
|
|
||||||
#include <map>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
namespace aidl {
|
namespace aidl {
|
||||||
namespace android {
|
namespace android {
|
||||||
namespace hardware {
|
namespace hardware {
|
||||||
namespace light {
|
namespace light {
|
||||||
|
|
||||||
|
// Keep sorted in the order of priority.
|
||||||
|
constexpr std::array kAvailableLights = {
|
||||||
|
// id, ordinal, type
|
||||||
|
HwLight{static_cast<int32_t>(LightType::NOTIFICATIONS), 0, LightType::NOTIFICATIONS},
|
||||||
|
HwLight{static_cast<int32_t>(LightType::BATTERY), 0, LightType::BATTERY},
|
||||||
|
};
|
||||||
|
|
||||||
class Lights : public BnLights {
|
class Lights : public BnLights {
|
||||||
public:
|
public:
|
||||||
Lights();
|
|
||||||
ndk::ScopedAStatus setLightState(int id, const HwLightState& state) override;
|
ndk::ScopedAStatus setLightState(int id, const HwLightState& state) override;
|
||||||
ndk::ScopedAStatus getLights(std::vector<HwLight>* types) override;
|
ndk::ScopedAStatus getLights(std::vector<HwLight>* types) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setLightNotification(int id, const HwLightState& state);
|
std::array<HwLightState, kAvailableLights.size()> notif_states_;
|
||||||
void applyNotificationState(const HwLightState& state);
|
|
||||||
|
|
||||||
uint32_t max_led_brightness_;
|
|
||||||
|
|
||||||
std::map<int, std::function<void(int id, const HwLightState&)>> mLights;
|
|
||||||
std::vector<HwLight> mAvailableLights;
|
|
||||||
|
|
||||||
// Keep sorted in the order of importance.
|
|
||||||
std::array<std::pair<int, HwLightState>, 2> notif_states_ = {{
|
|
||||||
{(int)LightType::NOTIFICATIONS, {}},
|
|
||||||
{(int)LightType::BATTERY, {}},
|
|
||||||
}};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace light
|
} // namespace light
|
||||||
|
|
|
@ -6,12 +6,15 @@ service vendor.light /vendor/bin/hw/android.hardware.lights-service.berlna
|
||||||
|
|
||||||
on boot
|
on boot
|
||||||
# Change ownership and permission for leds backlight
|
# Change ownership and permission for leds backlight
|
||||||
chmod 0664 /sys/class/leds/lcd-backlight/brightness
|
chown system system /sys/class/leds/charging/breath
|
||||||
|
chmod 0664 /sys/class/leds/charging/breath
|
||||||
chown system system /sys/class/leds/charging/brightness
|
chown system system /sys/class/leds/charging/brightness
|
||||||
chmod 0664 /sys/class/leds/charging/brightness
|
chmod 0664 /sys/class/leds/charging/brightness
|
||||||
chown system system /sys/class/leds/charging/breath
|
|
||||||
chmod 660 /sys/class/leds/charging/breath
|
|
||||||
chown system system /sys/class/leds/charging/delay_off
|
chown system system /sys/class/leds/charging/delay_off
|
||||||
chmod 660 /sys/class/leds/charging/delay_off
|
chmod 0664 /sys/class/leds/charging/delay_off
|
||||||
chown system system /sys/class/leds/charging/delay_on
|
chown system system /sys/class/leds/charging/delay_on
|
||||||
chmod 660 /sys/class/leds/charging/delay_on
|
chmod 0664 /sys/class/leds/charging/delay_on
|
||||||
|
|
||||||
|
# change permission of red leds
|
||||||
|
chown system system /sys/class/leds/red/brightness
|
||||||
|
chmod 0664 /sys/class/leds/red/brightness
|
||||||
|
|
Loading…
Reference in a new issue