rosemary: Build OSS Lights AIDL from RM6785

Co-authored-by: Demon000 <demonsingur@gmail.com>
Co-authored-by: vasishath <vasishath@gmail.com>
Co-authored-by: daniml3 <daniel@danielml.dev>
Change-Id: Ibf28d4b5e47c715f72da0eeee200f24aa7c99b3f
This commit is contained in:
SamarV-121
2022-09-14 18:25:32 +01:00
committed by Matsvei Niaverau
parent 69cae96159
commit a8484f951f
7 changed files with 266 additions and 0 deletions

View File

@@ -129,6 +129,10 @@ PRODUCT_PACKAGES += \
PRODUCT_PACKAGES += \ PRODUCT_PACKAGES += \
libshim_beanpod libshim_beanpod
# Lights
PRODUCT_PACKAGES += \
android.hardware.light-service.rosemary
# Overlays # Overlays
PRODUCT_ENFORCE_RRO_TARGETS := * PRODUCT_ENFORCE_RRO_TARGETS := *

32
lights/Android.bp Normal file
View File

@@ -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,
}

141
lights/Light.cpp Normal file
View File

@@ -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 <android-base/properties.h>
#include <fstream>
#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<LightType> 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<HwLight>* 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

43
lights/Light.h Normal file
View File

@@ -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 <aidl/android/hardware/light/BnLights.h>
#include <android-base/logging.h>
#include <hardware/hardware.h>
#include <hardware/lights.h>
#include <vector>
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<HwLight>* types) override;
};
} // namespace light
} // namespace hardware
} // namespace android
} // namespace aidl

View File

@@ -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

View File

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

34
lights/main.cpp Normal file
View File

@@ -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 <android/binder_manager.h>
#include <android/binder_process.h>
#include "Light.h"
using ::aidl::android::hardware::light::Lights;
int main() {
ABinderProcess_setThreadPoolMaxThreadCount(0);
std::shared_ptr<Lights> lights = ndk::SharedRefBase::make<Lights>();
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
}