From a8484f951f46213274f41e1ce8523c3cecc2bc6f Mon Sep 17 00:00:00 2001 From: SamarV-121 Date: Wed, 14 Sep 2022 18:25:32 +0100 Subject: [PATCH] rosemary: Build OSS Lights AIDL from RM6785 Co-authored-by: Demon000 Co-authored-by: vasishath Co-authored-by: daniml3 Change-Id: Ibf28d4b5e47c715f72da0eeee200f24aa7c99b3f --- device.mk | 4 + lights/Android.bp | 32 ++++ lights/Light.cpp | 141 ++++++++++++++++++ lights/Light.h | 43 ++++++ ...android.hardware.light-service.rosemary.rc | 6 + ...ndroid.hardware.light-service.rosemary.xml | 6 + lights/main.cpp | 34 +++++ 7 files changed, 266 insertions(+) create mode 100644 lights/Android.bp create mode 100644 lights/Light.cpp create mode 100644 lights/Light.h create mode 100644 lights/android.hardware.light-service.rosemary.rc create mode 100644 lights/android.hardware.light-service.rosemary.xml create mode 100644 lights/main.cpp diff --git a/device.mk b/device.mk index 5768cc5..335e304 100644 --- a/device.mk +++ b/device.mk @@ -129,6 +129,10 @@ PRODUCT_PACKAGES += \ PRODUCT_PACKAGES += \ libshim_beanpod +# Lights +PRODUCT_PACKAGES += \ + android.hardware.light-service.rosemary + # Overlays PRODUCT_ENFORCE_RRO_TARGETS := * diff --git a/lights/Android.bp b/lights/Android.bp new file mode 100644 index 0000000..0a032f7 --- /dev/null +++ b/lights/Android.bp @@ -0,0 +1,32 @@ +// +// Copyright (C) 2021 The LineageOS Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +cc_binary { + name: "android.hardware.light-service.rosemary", + init_rc: ["android.hardware.light-service.rosemary.rc"], + vintf_fragments: ["android.hardware.light-service.rosemary.xml"], + relative_install_path: "hw", + srcs: [ + "main.cpp", + "Light.cpp", + ], + shared_libs: [ + "libbase", + "libhardware", + "libbinder_ndk", + "android.hardware.light-V1-ndk_platform", + ], + vendor: true, +} diff --git a/lights/Light.cpp b/lights/Light.cpp new file mode 100644 index 0000000..86c0c14 --- /dev/null +++ b/lights/Light.cpp @@ -0,0 +1,141 @@ +/* + * Copyright (C) 2018-2019 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Light.h" + +#include + +#include + +#define LCD_LED "/sys/class/leds/lcd-backlight/" + +#define BRIGHTNESS "brightness" +#define MAX_BRIGHTNESS "max_brightness" + +namespace { +/* + * Write value to path and close file. + */ +static void set(std::string path, std::string value) { + std::ofstream file(path); + + if (!file.is_open()) { + LOG(WARNING) << "failed to write " << value.c_str() << " to " << path.c_str(); + return; + } + + file << value; +} + +static void set(std::string path, int value) { + set(path, std::to_string(value)); +} + +/* + * Read max brightness from path and close file. + */ +static int getMaxBrightness(std::string path) { + std::ifstream file(path); + int value; + + if (!file.is_open()) { + LOG(WARNING) << "failed to read from " << path.c_str(); + return 0; + } + + file >> value; + return value; +} + +static uint32_t getBrightness(const HwLightState& state) { + uint32_t alpha, red, green, blue; + + /* + * Extract brightness from AARRGGBB. + */ + alpha = (state.color >> 24) & 0xFF; + red = (state.color >> 16) & 0xFF; + green = (state.color >> 8) & 0xFF; + blue = state.color & 0xFF; + + /* + * Scale RGB brightness using Alpha brightness. + */ + red = red * alpha / 0xFF; + green = green * alpha / 0xFF; + blue = blue * alpha / 0xFF; + + return (77 * red + 150 * green + 29 * blue) >> 8; +} + +static inline uint32_t scaleBrightness(uint32_t brightness, uint32_t maxBrightness) { + if (brightness == 0) { + return 0; + } + + return (brightness - 1) * (maxBrightness - 1) / (0xFF - 1) + 1; +} + +static inline uint32_t getScaledBrightness(const HwLightState& state, uint32_t maxBrightness) { + return scaleBrightness(getBrightness(state), maxBrightness); +} + +static void handleBacklight(const HwLightState& state) { + uint32_t brightness = getScaledBrightness(state, getMaxBrightness(LCD_LED MAX_BRIGHTNESS)); + set(LCD_LED BRIGHTNESS, brightness); +} + +/* Keep sorted in the order of importance. */ +static std::vector backends = { + LightType::BACKLIGHT, +}; + +} // anonymous namespace + +namespace aidl { +namespace android{ +namespace hardware { +namespace light { + +ndk::ScopedAStatus Lights::setLightState(int id, const HwLightState& state) { + switch(id) { + case (int) LightType::BACKLIGHT: + handleBacklight(state); + return ndk::ScopedAStatus::ok(); + default: + return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); + } +} + +ndk::ScopedAStatus Lights::getLights(std::vector* lights) { + int i = 0; + + for (const LightType& backend : backends) { + HwLight hwLight; + hwLight.id = (int) backend; + hwLight.type = backend; + hwLight.ordinal = i; + lights->push_back(hwLight); + i++; + } + + return ndk::ScopedAStatus::ok(); +} + +} // namespace light +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/lights/Light.h b/lights/Light.h new file mode 100644 index 0000000..502e1c6 --- /dev/null +++ b/lights/Light.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2018 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include +#include +#include +#include + +using ::aidl::android::hardware::light::HwLightState; +using ::aidl::android::hardware::light::HwLight; +using ::aidl::android::hardware::light::LightType; +using ::aidl::android::hardware::light::BnLights; + +namespace aidl { +namespace android { +namespace hardware { +namespace light { + +class Lights : public BnLights { + ndk::ScopedAStatus setLightState(int id, const HwLightState& state) override; + ndk::ScopedAStatus getLights(std::vector* types) override; +}; + +} // namespace light +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/lights/android.hardware.light-service.rosemary.rc b/lights/android.hardware.light-service.rosemary.rc new file mode 100644 index 0000000..301a976 --- /dev/null +++ b/lights/android.hardware.light-service.rosemary.rc @@ -0,0 +1,6 @@ +service vendor.light-default /vendor/bin/hw/android.hardware.light-service.rosemary + class hal + user system + group system + # shutting off lights while powering-off + shutdown critical diff --git a/lights/android.hardware.light-service.rosemary.xml b/lights/android.hardware.light-service.rosemary.xml new file mode 100644 index 0000000..db604d6 --- /dev/null +++ b/lights/android.hardware.light-service.rosemary.xml @@ -0,0 +1,6 @@ + + + android.hardware.light + ILights/default + + diff --git a/lights/main.cpp b/lights/main.cpp new file mode 100644 index 0000000..3c87ef0 --- /dev/null +++ b/lights/main.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "Light.h" + +using ::aidl::android::hardware::light::Lights; + +int main() { + ABinderProcess_setThreadPoolMaxThreadCount(0); + std::shared_ptr lights = ndk::SharedRefBase::make(); + + 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 +}