msm8998-common: Switch to common lights HAL

Change-Id: If515e7c3f6e5b6895076f1d8381fb2c22bd1b3de
This commit is contained in:
LuK1337
2025-01-17 17:06:51 +01:00
committed by Georg Veichtlbauer
parent 92cd65cfa0
commit 224e21b47a
9 changed files with 1 additions and 394 deletions

View File

@@ -255,7 +255,7 @@ PRODUCT_PACKAGES += \
# Lights
PRODUCT_PACKAGES += \
android.hardware.light@2.0-service.oneplus_msm8998
android.hardware.light-service.lineage
# Lineage Health
PRODUCT_PACKAGES += \

View File

@@ -1,13 +1,4 @@
<compatibility-matrix version="2.0" type="framework">
<hal format="hidl">
<name>android.hardware.light</name>
<transport>hwbinder</transport>
<version>2.0</version>
<interface>
<name>ILight</name>
<instance>default</instance>
</interface>
</hal>
<hal format="hidl">
<name>vendor.oneplus.camera.CameraHIDL</name>
<transport>hwbinder</transport>

View File

@@ -1,24 +0,0 @@
//
// Copyright (C) 2018 The LineageOS Project
// SPDX-License-Identifier: Apache-2.0
//
cc_binary {
name: "android.hardware.light@2.0-service.oneplus_msm8998",
relative_install_path: "hw",
init_rc: ["android.hardware.light@2.0-service.oneplus_msm8998.rc"],
vintf_fragments: ["android.hardware.light@2.0-service.oneplus_msm8998.xml"],
srcs: [
"service.cpp",
"Light.cpp",
],
shared_libs: [
"libbase",
"libcutils",
"libhardware",
"libhidlbase",
"libutils",
"android.hardware.light@2.0",
],
proprietary: true,
}

View File

@@ -1,191 +0,0 @@
/*
* Copyright (C) 2018-2021 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#define LOG_TAG "LightsService"
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <fstream>
#include "Light.h"
namespace android {
namespace hardware {
namespace light {
namespace V2_0 {
namespace implementation {
/*
* Write value to path and close file.
*/
template <typename T>
static void set(const std::string& path, const T& value) {
std::ofstream file(path);
file << value;
}
template <typename T>
static T get(const std::string& path, const T& def) {
std::ifstream file(path);
T result;
file >> result;
return file.fail() ? def : result;
}
static int rgbToBrightness(const LightState& state) {
int color = state.color & 0x00ffffff;
return ((77 * ((color >> 16) & 0x00ff))
+ (150 * ((color >> 8) & 0x00ff))
+ (29 * (color & 0x00ff))) >> 8;
}
Light::Light() {
mLights.emplace(Type::ATTENTION, std::bind(&Light::setRgbLight, this, std::placeholders::_1, 0));
mLights.emplace(Type::BACKLIGHT, std::bind(&Light::setBacklight, this, std::placeholders::_1));
mLights.emplace(Type::BATTERY, std::bind(&Light::setRgbLight, this, std::placeholders::_1, 2));
mLights.emplace(Type::BUTTONS, std::bind(&Light::setButtonBacklight, this, std::placeholders::_1));
mLights.emplace(Type::NOTIFICATIONS, std::bind(&Light::setRgbLight, this, std::placeholders::_1, 1));
}
void Light::setBacklight(const LightState& state) {
int maxBrightness = get("/sys/class/leds/lcd-backlight/max_brightness", -1);
if (maxBrightness < 0) {
maxBrightness = 255;
}
int newBrightness = rgbToBrightness(state);
// If max panel brightness exceeds 255, apply proper linear scaling
if (newBrightness > 0 && maxBrightness != 255) {
int originalBrightness = newBrightness;
newBrightness = (((maxBrightness - 1) * (newBrightness - 1)) / (254)) + 1;
LOG(DEBUG) << "Scaling backlight brightness from " << originalBrightness << " => " << newBrightness;
}
set("/sys/class/leds/lcd-backlight/brightness", newBrightness);
}
void Light::setButtonBacklight(const LightState& state) {
set("/sys/class/leds/button-backlight/brightness", rgbToBrightness(state));
}
void Light::setRgbLight(const LightState& state, size_t index) {
mLightStates.at(index) = state;
LightState stateToUse = mLightStates.front();
for (const auto& lightState : mLightStates) {
if (lightState.color & 0xffffff) {
stateToUse = lightState;
break;
}
}
std::map<std::string, int> colorValues;
int brightness = (stateToUse.color >> 24) & 0xff;
colorValues["red"] = ((stateToUse.color >> 16) & 0xff) * brightness / 0xff;
colorValues["green"] = ((stateToUse.color >> 8) & 0xff) * brightness / 0xff;
colorValues["blue"] = (stateToUse.color & 0xff) * brightness / 0xff;
int onMs = stateToUse.flashMode == Flash::TIMED ? stateToUse.flashOnMs : 0;
int offMs = stateToUse.flashMode == Flash::TIMED ? stateToUse.flashOffMs : 0;
// LUT has 63 entries, we could theoretically use them as 3 (colors) * 21 (steps).
// However, the last LUT entries don't seem to behave correctly for unknown
// reasons, so we use 17 steps for a total of 51 LUT entries only.
static constexpr int kRampSteps = 16;
static constexpr int kRampMaxStepDurationMs = 15;
auto makeLedPath = [](const std::string& led, const std::string& op) -> std::string {
return "/sys/class/leds/" + led + "/" + op;
};
auto getScaledDutyPercent = [](int brightness) -> std::string {
std::string output;
for (int i = 0; i <= kRampSteps; i++) {
if (i != 0) {
output += ",";
}
output += std::to_string(i * 512 * brightness / (255 * kRampSteps));
}
return output;
};
// Disable all blinking before starting
for (const auto& entry : colorValues) {
set(makeLedPath(entry.first, "blink"), 0);
}
if (onMs > 0 && offMs > 0) {
int pauseLo, pauseHi, stepDuration, index = 0;
if (kRampMaxStepDurationMs * kRampSteps > onMs) {
stepDuration = onMs / kRampSteps;
pauseHi = 0;
pauseLo = offMs;
} else {
stepDuration = kRampMaxStepDurationMs;
pauseHi = onMs - kRampSteps * stepDuration;
pauseLo = offMs - kRampSteps * stepDuration;
}
for (const auto& entry : colorValues) {
set(makeLedPath(entry.first, "lut_flags"), 95);
set(makeLedPath(entry.first, "start_idx"), index);
set(makeLedPath(entry.first, "duty_pcts"), getScaledDutyPercent(entry.second));
set(makeLedPath(entry.first, "pause_lo"), pauseLo);
set(makeLedPath(entry.first, "pause_hi"), pauseHi);
set(makeLedPath(entry.first, "ramp_step_ms"), stepDuration);
index += kRampSteps + 1;
}
// Start blinking
for (const auto& entry : colorValues) {
set(makeLedPath(entry.first, "blink"), entry.second);
}
} else {
for (const auto& entry : colorValues) {
set(makeLedPath(entry.first, "brightness"), entry.second);
}
}
LOG(DEBUG) << base::StringPrintf(
"setRgbLight: mode=%d, color=%08X, onMs=%d, offMs=%d",
static_cast<std::underlying_type<Flash>::type>(stateToUse.flashMode), stateToUse.color,
onMs, offMs);
}
// Methods from ::android::hardware::light::V2_0::ILight follow.
Return<Status> Light::setLight(Type type, const LightState& state) {
auto it = mLights.find(type);
if (it == mLights.end()) {
return Status::LIGHT_NOT_SUPPORTED;
}
// Lock global mutex until light state is updated.
std::lock_guard<std::mutex> lock(mLock);
it->second(state);
return Status::SUCCESS;
}
Return<void> Light::getSupportedTypes(getSupportedTypes_cb _hidl_cb) {
std::vector<Type> types;
for (auto const& light : mLights) {
types.push_back(light.first);
}
_hidl_cb(types);
return Void();
}
} // namespace implementation
} // namespace V2_0
} // namespace light
} // namespace hardware
} // namespace android

View File

@@ -1,53 +0,0 @@
/*
* Copyright (C) 2018-2021 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H
#define ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H
#include <android/hardware/light/2.0/ILight.h>
#include <hidl/Status.h>
#include <mutex>
#include <unordered_map>
namespace android {
namespace hardware {
namespace light {
namespace V2_0 {
namespace implementation {
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hardware::hidl_vec;
using ::android::hardware::light::V2_0::ILight;
using ::android::hardware::light::V2_0::LightState;
using ::android::hardware::light::V2_0::Status;
using ::android::hardware::light::V2_0::Type;
class Light : public ILight {
public:
Light();
// Methods from ::android::hardware::light::V2_0::ILight follow.
Return<Status> setLight(Type type, const LightState& state) override;
Return<void> getSupportedTypes(getSupportedTypes_cb _hidl_cb) override;
private:
void setBacklight(const LightState& state);
void setButtonBacklight(const LightState& state);
void setRgbLight(const LightState& state, size_t index);
std::mutex mLock;
std::unordered_map<Type, std::function<void(const LightState&)>> mLights;
std::array<LightState, 3> mLightStates;
};
} // namespace implementation
} // namespace V2_0
} // namespace light
} // namespace hardware
} // namespace android
#endif // ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H

View File

@@ -1,70 +0,0 @@
on init
# RGB lights
chown system system /sys/class/leds/red/pause_lo
chown system system /sys/class/leds/blue/pause_lo
chown system system /sys/class/leds/green/pause_lo
chmod 640 /sys/class/leds/red/pause_lo
chmod 640 /sys/class/leds/blue/pause_lo
chmod 640 /sys/class/leds/green/pause_lo
chown system system /sys/class/leds/red/pause_hi
chown system system /sys/class/leds/blue/pause_hi
chown system system /sys/class/leds/green/pause_hi
chmod 640 /sys/class/leds/red/pause_hi
chmod 640 /sys/class/leds/blue/pause_hi
chmod 640 /sys/class/leds/green/pause_hi
chown system system /sys/class/leds/red/blink
chown system system /sys/class/leds/blue/blink
chown system system /sys/class/leds/green/blink
chmod 640 /sys/class/leds/red/blink
chmod 640 /sys/class/leds/blue/blink
chmod 640 /sys/class/leds/green/blink
chown system system /sys/class/leds/rgb/rgb_blink
chmod 640 /sys/class/leds/rgb/rgb_blink
chown system system /sys/class/leds/red/ramp_step_ms
chown system system /sys/class/leds/blue/ramp_step_ms
chown system system /sys/class/leds/green/ramp_step_ms
chmod 640 /sys/class/leds/red/ramp_step_ms
chmod 640 /sys/class/leds/green/ramp_step_ms
chmod 640 /sys/class/leds/blue/ramp_step_ms
chown system system /sys/class/leds/red/duty_pcts
chown system system /sys/class/leds/blue/duty_pcts
chown system system /sys/class/leds/green/duty_pcts
chmod 640 /sys/class/leds/red/duty_pcts
chmod 640 /sys/class/leds/green/duty_pcts
chmod 640 /sys/class/leds/blue/duty_pcts
chown system system /sys/class/leds/red/start_idx
chown system system /sys/class/leds/blue/start_idx
chown system system /sys/class/leds/green/start_idx
chmod 640 /sys/class/leds/red/start_idx
chmod 640 /sys/class/leds/green/start_idx
chmod 640 /sys/class/leds/blue/start_idx
# RGB light support
chown system system /sys/class/leds/red/brightness
chown system system /sys/class/leds/green/brightness
chown system system /sys/class/leds/blue/brightness
chmod 640 /sys/class/leds/red/brightness
chmod 640 /sys/class/leds/green/brightness
chmod 640 /sys/class/leds/blue/brightness
# Button backlight permissions
chown system system /sys/class/leds/button-backlight/brightness
chmod 640 /sys/class/leds/button-backlight/brightness
# LCD backlight permissions
chown system system /sys/class/leds/lcd-backlight/brightness
chmod 640 /sys/class/leds/lcd-backlight/brightness
service vendor.light-hal-2-0 /vendor/bin/hw/android.hardware.light@2.0-service.oneplus_msm8998
interface android.hardware.light@2.0::ILight default
class hal
user system
group system
# shutting off lights while powering-off
shutdown critical

View File

@@ -1,11 +0,0 @@
<manifest version="1.0" type="device">
<hal format="hidl">
<name>android.hardware.light</name>
<transport>hwbinder</transport>
<version>2.0</version>
<interface>
<name>ILight</name>
<instance>default</instance>
</interface>
</hal>
</manifest>

View File

@@ -1,34 +0,0 @@
/*
* Copyright (C) 2018-2021 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#define LOG_TAG "android.hardware.light@2.0-service.oneplus_msm8998"
#include <android-base/logging.h>
#include <hidl/HidlTransportSupport.h>
#include "Light.h"
// Generated HIDL files
using android::hardware::light::V2_0::ILight;
using android::hardware::light::V2_0::implementation::Light;
int main() {
android::sp<ILight> service = new Light();
android::hardware::configureRpcThreadpool(1 /*threads*/, true /*callerWillJoin*/);
if (service->registerAsService() != android::OK) {
LOG(ERROR) << "Cannot register Light HAL service.";
return 1;
}
LOG(INFO) << "Light HAL ready.";
android::hardware::joinRpcThreadpool();
LOG(ERROR) << "Light HAL failed to join thread pool.";
return 1;
}

View File

@@ -14,7 +14,6 @@
# HALs
/(vendor|system/vendor)/bin/hw/android\.hardware\.biometrics\.fingerprint@2\.1-service-custom u:object_r:hal_fingerprint_default_exec:s0
/(vendor|system/vendor)/bin/hw/android\.hardware\.light@2\.0-service\.oneplus_msm8998 u:object_r:hal_light_default_exec:s0
/(vendor|system/vendor)/bin/hw/android\.hardware\.power-service\.oneplus-libperfmgr u:object_r:hal_power_default_exec:s0
/(vendor|system/vendor)/bin/hw/vendor\.lineage\.livedisplay@2\.0-service\.oneplus_msm8998 u:object_r:hal_livedisplay_oneplus_msm8998_exec:s0
/(vendor|system/vendor)/bin/hw/vendor\.oneplus\.camera\.CameraHIDL@1\.0-service u:object_r:hal_cameraHIDL_default_exec:s0