exynos9610: add custom lights hidl hal
authorJan Altensen <info@stricted.net>
Mon, 18 May 2020 19:04:47 +0000 (21:04 +0200)
committerJan Altensen <info@stricted.net>
Mon, 7 Dec 2020 07:23:03 +0000 (08:23 +0100)
Change-Id: I9f6b94c6d1dee2ec8b09762a8bf94a1d6f3363fb

common.mk
hidl/light/Android.bp [new file with mode: 0644]
hidl/light/Light.cpp [new file with mode: 0644]
hidl/light/Light.h [new file with mode: 0644]
hidl/light/android.hardware.light@2.0-service.exynos9610.rc [new file with mode: 0644]
hidl/light/service.cpp [new file with mode: 0644]
proprietary-files-vendor.txt

index 24364b7ad7a6c8818d860b3c9cfa68293ebbd33b..684d29dd93704f343f738d31e9562a72a3fdedd9 100644 (file)
--- 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 (file)
index 0000000..57e6370
--- /dev/null
@@ -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/Light.cpp b/hidl/light/Light.cpp
new file mode 100644 (file)
index 0000000..6517d14
--- /dev/null
@@ -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 <android-base/stringprintf.h>
+#include <iomanip>
+
+#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 <typename T>
+static void set(const std::string& path, const T& value) {
+    std::ofstream file(path);
+    file << value << std::endl;
+}
+
+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;
+}
+
+Light::Light() {
+    mLights.emplace(Type::BACKLIGHT,
+                    std::bind(&Light::handleBacklight, this, std::placeholders::_1));
+}
+
+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;
+}
+
+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<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();
+}
+
+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 (file)
index 0000000..0117abd
--- /dev/null
@@ -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 <android/hardware/light/2.0/ILight.h>
+#include <hidl/MQDescriptor.h>
+#include <hidl/Status.h>
+#include <fstream>
+#include <unordered_map>
+
+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<Status> setLight(Type type, const LightState& state) override;
+    Return<void> getSupportedTypes(getSupportedTypes_cb _hidl_cb) override;
+
+  private:
+    void handleBacklight(const LightState& state);
+    uint32_t rgbToBrightness(const LightState& state);
+
+    std::mutex mLock;
+    std::unordered_map<Type, std::function<void(const LightState&)>> 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 (file)
index 0000000..ac0db8e
--- /dev/null
@@ -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 (file)
index 0000000..02d2044
--- /dev/null
@@ -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 <android-base/logging.h>
+#include <hidl/HidlTransportSupport.h>
+#include <utils/Errors.h>
+
+#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<ILight> 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;
+}
index 91aca3e1bcf7cf2608e4999fdc13401064c1d0ad..eb10d0032ff98df54284a81dfbe89b5384640a40 100644 (file)
@@ -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