berlna: Switch to common lights HAL

Change-Id: If515e7c3f6e5b6895076f1d8381fb2c22bd1b3de
This commit is contained in:
LuK1337
2025-02-19 21:08:26 +01:00
committed by Nolen Johnson
parent c6179b412c
commit 80fd8cbaa2
7 changed files with 1 additions and 288 deletions

View File

@@ -67,7 +67,7 @@ PRODUCT_PACKAGES += \
# Lights
PRODUCT_PACKAGES += \
android.hardware.lights-service.berlna
android.hardware.lights-service.lineage
# NFC
PRODUCT_PACKAGES += \

View File

@@ -1,23 +0,0 @@
//
// SPDX-FileCopyrightText: The LineageOS Project
// SPDX-License-Identifier: Apache-2.0
//
cc_binary {
name: "android.hardware.lights-service.berlna",
relative_install_path: "hw",
init_rc: ["android.hardware.lights.berlna.rc"],
vintf_fragments: ["android.hardware.lights.berlna.xml"],
vendor: true,
shared_libs: [
"libbase",
"liblog",
"libhardware",
"libbinder_ndk",
"android.hardware.light-V2-ndk",
],
srcs: [
"Lights.cpp",
"main.cpp",
],
}

View File

@@ -1,181 +0,0 @@
/*
* SPDX-FileCopyrightText: 2019 The Android Open Source Project
* SPDX-FileCopyrightText: The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
#define LOG_TAG "android.hardware.lights-service.berlna"
#include "Lights.h"
#include <android-base/file.h>
#include <android-base/logging.h>
#include <thread>
/* clang-format off */
#define PPCAT_NX(A, B) A/B
#define PPCAT(A, B) PPCAT_NX(A, B)
#define STRINGIFY_INNER(x) #x
#define STRINGIFY(x) STRINGIFY_INNER(x)
#define CHARGING_ATTR(x) STRINGIFY(PPCAT(/sys/class/leds/charging, x))
/* clang-format on */
namespace aidl {
namespace android {
namespace hardware {
namespace light {
namespace {
using ::android::base::WriteStringToFile;
// Write value to path and close file.
template <typename T>
inline bool WriteToFile(const std::string& path, T content) {
return WriteStringToFile(std::to_string(content), path);
}
uint32_t RgbaToBrightness(uint32_t color) {
// Extract brightness from AARRGGBB.
uint32_t alpha = (color >> 24) & 0xFF;
// Retrieve each of the RGB colors
uint32_t red = (color >> 16) & 0xFF;
uint32_t green = (color >> 8) & 0xFF;
uint32_t blue = color & 0xFF;
// Scale RGB colors if a brightness has been applied by the user
if (alpha != 0xFF && alpha != 0) {
red = red * alpha / 0xFF;
green = green * alpha / 0xFF;
blue = blue * alpha / 0xFF;
}
return (77 * red + 150 * green + 29 * blue) >> 8;
}
inline bool IsLit(uint32_t color) {
return color & 0x00ffffff;
}
bool SetChargingLedTimerTrigger(int on_ms, int off_ms) {
using namespace std::chrono_literals;
bool rc;
rc = WriteStringToFile("timer", CHARGING_ATTR(trigger));
if (!rc) {
LOG(DEBUG) << "charging doesn't support timer trigger";
return rc;
}
auto retries = 20;
while (retries--) {
LOG(DEBUG) << "retry " << retries << " set delay_off and delay_on";
std::this_thread::sleep_for(2ms);
rc = WriteToFile(CHARGING_ATTR(delay_off), off_ms);
if (!rc) continue;
rc = WriteToFile(CHARGING_ATTR(delay_on), on_ms);
if (rc) break;
}
if (!rc) {
LOG(ERROR) << "Error in writing to delay_on/off for charging";
}
return rc;
}
bool SetChargingLedBrightness(uint32_t brightness) {
bool rc;
rc = WriteStringToFile("none", CHARGING_ATTR(trigger));
if (!rc) {
LOG(DEBUG) << "charging failed to set trigger to none";
return rc;
}
rc = WriteToFile(CHARGING_ATTR(brightness), brightness);
return rc;
}
void ApplyNotificationState(const HwLightState& state) {
bool blink = state.flashOnMs > 0 && state.flashOffMs > 0;
bool ok = false;
switch (state.flashMode) {
case FlashMode::HARDWARE:
ok = WriteToFile(CHARGING_ATTR(breath), blink);
// fallback to timed blinking if breath is not supported
if (ok) break;
FALLTHROUGH_INTENDED;
case FlashMode::TIMED:
ok = SetChargingLedTimerTrigger(state.flashOnMs, state.flashOffMs);
// fallback to constant on if timed blinking is not supported
if (ok) break;
FALLTHROUGH_INTENDED;
case FlashMode::NONE:
default:
ok = SetChargingLedBrightness(RgbaToBrightness(state.color));
break;
}
LOG(DEBUG) << __func__ << ": mode=" << toString(state.flashMode) << ", colorRGB=" << std::hex
<< state.color << std::dec << ", onMS=" << state.flashOnMs
<< ", offMS=" << state.flashOffMs << ", ok=" << ok;
}
} // anonymous namespace
ndk::ScopedAStatus Lights::setLightState(int id, const HwLightState& state) {
static_assert(kAvailableLights.size() == std::tuple_size_v<decltype(notif_states_)>);
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";
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}
// 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();
}
ndk::ScopedAStatus Lights::getLights(std::vector<HwLight>* lights) {
lights->insert(lights->end(), kAvailableLights.begin(), kAvailableLights.end());
// 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();
}
} // namespace light
} // namespace hardware
} // namespace android
} // namespace aidl

View File

@@ -1,36 +0,0 @@
/*
* SPDX-FileCopyrightText: 2020 The Android Open Source Project
* SPDX-FileCopyrightText: The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <aidl/android/hardware/light/BnLights.h>
#include <array>
namespace aidl {
namespace android {
namespace hardware {
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 {
public:
ndk::ScopedAStatus setLightState(int id, const HwLightState& state) override;
ndk::ScopedAStatus getLights(std::vector<HwLight>* types) override;
private:
std::array<HwLightState, kAvailableLights.size()> notif_states_;
};
} // namespace light
} // namespace hardware
} // namespace android
} // namespace aidl

View File

@@ -1,14 +0,0 @@
service vendor.light /vendor/bin/hw/android.hardware.lights-service.berlna
class hal
user system
group system
shutdown critical
on boot
# Change ownership and permission for leds backlight
chown system system /sys/class/leds/charging/brightness
chmod 0664 /sys/class/leds/charging/brightness
# change permission of red leds
chown system system /sys/class/leds/red/brightness
chmod 0664 /sys/class/leds/red/brightness

View File

@@ -1,7 +0,0 @@
<manifest version="1.0" type="device">
<hal format="aidl">
<name>android.hardware.light</name>
<version>2</version>
<fqname>ILights/default</fqname>
</hal>
</manifest>

View File

@@ -1,26 +0,0 @@
/*
* SPDX-FileCopyrightText: 2020 The Android Open Source Project
* SPDX-License-Identifier: Apache-2.0
*/
#include <android-base/logging.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>
#include "Lights.h"
using ::aidl::android::hardware::light::Lights;
int main() {
ABinderProcess_setThreadPoolMaxThreadCount(0);
std::shared_ptr<Lights> lights = ndk::SharedRefBase::make<Lights>();
if (!lights) {
return EXIT_FAILURE;
}
const std::string instance = std::string() + Lights::descriptor + "/default";
binder_status_t status = AServiceManager_addService(lights->asBinder().get(), instance.c_str());
CHECK(status == STATUS_OK);
ABinderProcess_joinThreadPool();
return EXIT_FAILURE; // should not reached
}