From cbbc8322a79f42646865120ce49a709212e983b4 Mon Sep 17 00:00:00 2001 From: Jan Altensen Date: Mon, 18 May 2020 21:04:47 +0200 Subject: [PATCH] exynos9610: add custom lights hidl hal Change-Id: I9f6b94c6d1dee2ec8b09762a8bf94a1d6f3363fb --- common.mk | 4 + hidl/light/Android.bp | 35 ++++++ hidl/light/Android.mk | 42 +++++++ hidl/light/Light.cpp | 112 ++++++++++++++++++ hidl/light/Light.h | 60 ++++++++++ ...d.hardware.light@2.0-service.exynos9610.rc | 7 ++ hidl/light/service.cpp | 54 +++++++++ proprietary-files-vendor.txt | 4 - 8 files changed, 314 insertions(+), 4 deletions(-) create mode 100644 hidl/light/Android.bp create mode 100644 hidl/light/Android.mk create mode 100644 hidl/light/Light.cpp create mode 100644 hidl/light/Light.h create mode 100644 hidl/light/android.hardware.light@2.0-service.exynos9610.rc create mode 100644 hidl/light/service.cpp diff --git a/common.mk b/common.mk index 24364b7..684d29d 100644 --- a/common.mk +++ b/common.mk @@ -121,6 +121,10 @@ PRODUCT_COPY_FILES += \ $(COMMON_PATH)/configs/init/vendor.mmi.carrier.rc:$(TARGET_COPY_OUT_VENDOR)/etc/init/vendor.mmi.carrier.rc \ $(COMMON_PATH)/configs/fstab.exynos9610:$(TARGET_COPY_OUT_VENDOR)/etc/fstab.exynos9610 +# Lights +PRODUCT_PACKAGES += \ + android.hardware.light@2.0-service.exynos9610 + # Media PRODUCT_COPY_FILES += \ $(COMMON_PATH)/configs/media/media_codecs.xml:$(TARGET_COPY_OUT_VENDOR)/etc/media_codecs.xml \ diff --git a/hidl/light/Android.bp b/hidl/light/Android.bp new file mode 100644 index 0000000..57e6370 --- /dev/null +++ b/hidl/light/Android.bp @@ -0,0 +1,35 @@ +// Copyright (C) 2020 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@2.0-service.exynos9610", + defaults: ["hidl_defaults"], + vendor: true, + relative_install_path: "hw", + init_rc: ["android.hardware.light@2.0-service.exynos9610.rc"], + + srcs: [ + "Light.cpp", + "service.cpp", + ], + + shared_libs: [ + "libbase", + "libhardware", + "libhidlbase", + "liblog", + "libutils", + "android.hardware.light@2.0", + ], +} diff --git a/hidl/light/Android.mk b/hidl/light/Android.mk new file mode 100644 index 0000000..9a6bdd8 --- /dev/null +++ b/hidl/light/Android.mk @@ -0,0 +1,42 @@ +# +# Copyright (C) 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. +# + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + Light.cpp \ + service.cpp + +LOCAL_C_INCLUDES := $(LOCAL_PATH)/include + +LOCAL_SHARED_LIBRARIES := \ + libbase \ + libbinder \ + libhidlbase \ + libhidltransport \ + libutils \ + android.hardware.light@2.0 + +LOCAL_MODULE := android.hardware.light@2.0-service.samsung +LOCAL_INIT_RC := android.hardware.light@2.0-service.samsung.rc +LOCAL_MODULE_RELATIVE_PATH := hw +LOCAL_MODULE_TAGS := optional +LOCAL_MODULE_OWNER := samsung +LOCAL_VENDOR_MODULE := true + +include $(BUILD_EXECUTABLE) diff --git a/hidl/light/Light.cpp b/hidl/light/Light.cpp new file mode 100644 index 0000000..6517d14 --- /dev/null +++ b/hidl/light/Light.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2020 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. + */ +#define LOG_TAG "android.hardware.light@2.0-service.exynos9610" + +#include +#include + +#include "Light.h" + +#define COLOR_MASK 0x00ffffff +#define MAX_INPUT_BRIGHTNESS 255 +#define PANEL_BRIGHTNESS_NODE "/sys/class/backlight/backlight_0/brightness" +#define PANEL_MAX_BRIGHTNESS_NODE "/sys/class/backlight/backlight_0/max_brightness" + +using android::hardware::light::V2_0::LightState; +using android::hardware::light::V2_0::Status; +using android::hardware::light::V2_0::Type; + +namespace android { +namespace hardware { +namespace light { +namespace V2_0 { +namespace implementation { + +/* + * Write value to path and close file. + */ +template +static void set(const std::string& path, const T& value) { + std::ofstream file(path); + file << value << std::endl; +} + +template +static T get(const std::string& path, const T& def) { + std::ifstream file(path); + T result; + + file >> result; + return file.fail() ? def : result; +} + +Light::Light() { + mLights.emplace(Type::BACKLIGHT, + std::bind(&Light::handleBacklight, this, std::placeholders::_1)); +} + +Return 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 lock(mLock); + + it->second(state); + + return Status::SUCCESS; +} + +void Light::handleBacklight(const LightState& state) { + uint32_t max_brightness = get(PANEL_MAX_BRIGHTNESS_NODE, MAX_INPUT_BRIGHTNESS); + uint32_t brightness = rgbToBrightness(state); + + if (max_brightness != MAX_INPUT_BRIGHTNESS) { + brightness = brightness * max_brightness / MAX_INPUT_BRIGHTNESS; + } + + set(PANEL_BRIGHTNESS_NODE, brightness); +} + +Return Light::getSupportedTypes(getSupportedTypes_cb _hidl_cb) { + std::vector types; + + for (auto const& light : mLights) { + types.push_back(light.first); + } + + _hidl_cb(types); + + return Void(); +} + +uint32_t Light::rgbToBrightness(const LightState& state) { + uint32_t color = state.color & COLOR_MASK; + + return ((77 * ((color >> 16) & 0xff)) + (150 * ((color >> 8) & 0xff)) + (29 * (color & 0xff))) >> + 8; +} + +} // namespace implementation +} // namespace V2_0 +} // namespace light +} // namespace hardware +} // namespace android diff --git a/hidl/light/Light.h b/hidl/light/Light.h new file mode 100644 index 0000000..0117abd --- /dev/null +++ b/hidl/light/Light.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2020 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. + */ + +#ifndef ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H +#define ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H + +#include +#include +#include +#include +#include + +namespace android { +namespace hardware { +namespace light { +namespace V2_0 { +namespace implementation { + +using ::android::sp; +using ::android::hardware::hidl_array; +using ::android::hardware::hidl_memory; +using ::android::hardware::hidl_string; +using ::android::hardware::hidl_vec; +using ::android::hardware::Return; +using ::android::hardware::Void; + +struct Light : public ILight { + Light(); + + Return setLight(Type type, const LightState& state) override; + Return getSupportedTypes(getSupportedTypes_cb _hidl_cb) override; + + private: + void handleBacklight(const LightState& state); + uint32_t rgbToBrightness(const LightState& state); + + std::mutex mLock; + std::unordered_map> mLights; +}; + +} // namespace implementation +} // namespace V2_0 +} // namespace light +} // namespace hardware +} // namespace android + +#endif // ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H diff --git a/hidl/light/android.hardware.light@2.0-service.exynos9610.rc b/hidl/light/android.hardware.light@2.0-service.exynos9610.rc new file mode 100644 index 0000000..ac0db8e --- /dev/null +++ b/hidl/light/android.hardware.light@2.0-service.exynos9610.rc @@ -0,0 +1,7 @@ +service vendor.light-hal-2-0 /vendor/bin/hw/android.hardware.light@2.0-service.exynos9610 + interface android.hardware.light@2.0::ILight default + class hal + user system + group system + # shutting off lights while powering-off + shutdown critical diff --git a/hidl/light/service.cpp b/hidl/light/service.cpp new file mode 100644 index 0000000..02d2044 --- /dev/null +++ b/hidl/light/service.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2020 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. + */ + +#define LOG_TAG "android.hardware.light@2.0-service.exynos9610" + +#include +#include +#include + +#include "Light.h" + +using android::hardware::configureRpcThreadpool; +using android::hardware::joinRpcThreadpool; + +using android::hardware::light::V2_0::ILight; +using android::hardware::light::V2_0::implementation::Light; + +using android::OK; +using android::sp; +using android::status_t; + +int main() { + sp light = new Light(); + + configureRpcThreadpool(1, true); + + status_t status = light->registerAsService(); + + if (status != OK) { + LOG(ERROR) << "Could not register service for Light HAL"; + goto shutdown; + } + + LOG(INFO) << "Light HAL service is Ready."; + joinRpcThreadpool(); + +shutdown: + // In normal operation, we don't expect the thread pool to shutdown + LOG(ERROR) << "Light HAL failed to join thread pool."; + return 1; +} diff --git a/proprietary-files-vendor.txt b/proprietary-files-vendor.txt index 91aca3e..eb10d00 100644 --- a/proprietary-files-vendor.txt +++ b/proprietary-files-vendor.txt @@ -237,10 +237,6 @@ vendor/lib64/libmpp.so vendor/lib/hw/keystore.exynos9610.so vendor/lib64/hw/keystore.exynos9610.so -# lights -vendor/lib/hw/lights.exynos9610.so -vendor/lib64/hw/lights.exynos9610.so - # mobicore vendor/app/mcRegistry/00060308060501020000000000000000.tlbin vendor/app/mcRegistry/07010000000000000000000000000000.tlbin -- 2.20.1