diff options
author | belgin <belginstirbu@hotmail.com> | 2021-06-29 11:26:13 +0300 |
---|---|---|
committer | Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org> | 2021-09-02 13:04:25 +0200 |
commit | 730a3558999f10d56dc157d94cc1a848138f34de (patch) | |
tree | c18908c2f8dd720fadc291485070a8e0c21b9bd7 | |
parent | dbb15a84516f17a25fe4e69358c9d5bfc35a10bb (diff) | |
download | device_samsung_midas_common-730a3558999f10d56dc157d94cc1a848138f34de.tar.gz device_samsung_midas_common-730a3558999f10d56dc157d94cc1a848138f34de.tar.bz2 device_samsung_midas_common-730a3558999f10d56dc157d94cc1a848138f34de.zip |
Enable backlight adjustment for replicant 11
Without that patch, with the GT-I9300 and the GT-I9305
users don't have the ability to adjust the backlight
levels.
The liblight HAL was taken from device/google/wahoo,
and renamed.
Signed-off-by: belgin <belginstirbu@hotmail.com>
GNUtoo: squashed commits, modified the commit message(s)
Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
-rw-r--r-- | BoardConfigCommon.mk | 1 | ||||
-rw-r--r-- | lights/Android.bp | 14 | ||||
-rw-r--r-- | lights/lights.cpp | 139 | ||||
-rw-r--r-- | lights/lights.rc | 5 | ||||
-rw-r--r-- | lights/lights.xml | 6 | ||||
-rw-r--r-- | midas.mk | 7 | ||||
-rw-r--r-- | sepolicy/file_contexts | 2 | ||||
-rw-r--r-- | sepolicy/hal_light_default.te | 2 |
8 files changed, 172 insertions, 4 deletions
diff --git a/BoardConfigCommon.mk b/BoardConfigCommon.mk index 0a5d355..ee8b462 100644 --- a/BoardConfigCommon.mk +++ b/BoardConfigCommon.mk @@ -62,5 +62,6 @@ DEVICE_MANIFEST_FILE := device/samsung/midas-common/manifest.xml PRODUCT_FULL_TREBLE_OVERRIDE := true BUILD_BROKEN_USES_BUILD_HOST_STATIC_LIBRARY := true BOARD_VENDOR_SEPOLICY_DIRS += device/samsung/midas-common/sepolicy +TARGET_KERNEL_CLANG_COMPILE := true -include vendor/replicant/config/BoardConfigReplicant.mk diff --git a/lights/Android.bp b/lights/Android.bp new file mode 100644 index 0000000..0e0bf18 --- /dev/null +++ b/lights/Android.bp @@ -0,0 +1,14 @@ +cc_binary { + name: "lights-midas", + relative_install_path: "hw", + init_rc: ["lights.rc"], + vintf_fragments: ["lights.xml"], + vendor: true, + shared_libs: [ + "libbase", + "libbinder_ndk", + "android.hardware.light-ndk_platform", + ], + + srcs: ["lights.cpp"], +} diff --git a/lights/lights.cpp b/lights/lights.cpp new file mode 100644 index 0000000..354fc57 --- /dev/null +++ b/lights/lights.cpp @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2019 The Android Open Source 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 <array> + +#include <errno.h> +#include <fcntl.h> +#include <pthread.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <sys/ioctl.h> +#include <sys/types.h> + +#include <aidl/android/hardware/light/BnLights.h> +#include <android-base/logging.h> +#include <android/binder_manager.h> +#include <android/binder_process.h> + +using ::aidl::android::hardware::light::BnLights; +using ::aidl::android::hardware::light::HwLight; +using ::aidl::android::hardware::light::HwLightState; +using ::aidl::android::hardware::light::ILights; +using ::aidl::android::hardware::light::LightType; +using ::ndk::ScopedAStatus; +using ::ndk::SharedRefBase; + +char const* const BACKLIGHT_FILE = "/sys/class/backlight/panel/brightness"; + +static int sys_write_int(int fd, int value) { + char buffer[16]; + size_t bytes; + ssize_t amount; + + bytes = snprintf(buffer, sizeof(buffer), "%d\n", value); + if (bytes >= sizeof(buffer)) return -EINVAL; + amount = write(fd, buffer, bytes); + return amount == -1 ? -errno : 0; +} + +class Lights : public BnLights { + private: + std::vector<HwLight> availableLights; + + void addLight(LightType const type, int const ordinal) { + HwLight light{}; + light.id = availableLights.size(); + light.type = type; + light.ordinal = ordinal; + availableLights.emplace_back(light); + } + + int rgbToBrightness(int color) + { + color = color & 0x00ffffff; + return ((77*((color>>16)&0x00ff)) + + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8; + } + + void writeLed(const char* path, int color) { + int fd = open(path, O_WRONLY); + if (fd < 0) { + LOG(ERROR) << "COULD NOT OPEN LED_DEVICE " << path; + return; + } + + sys_write_int(fd, color); + close(fd); + } + + public: + Lights() : BnLights() { + addLight(LightType::BACKLIGHT, 0); + } + + ScopedAStatus setLightState(int id, const HwLightState& state) override { + if (!(0 <= id && id < availableLights.size())) { + LOG(ERROR) << "Light id " << (int32_t)id << " does not exist."; + return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); + } + + int const color = rgbToBrightness(state.color); + HwLight const& light = availableLights[id]; + + int ret = 0; + + switch (light.type) { + case LightType::BACKLIGHT: + writeLed(BACKLIGHT_FILE, color/255.f * 24); + break; + } + + if (ret == 0) { + return ScopedAStatus::ok(); + } else { + return ScopedAStatus::fromServiceSpecificError(ret); + } + } + + ScopedAStatus getLights(std::vector<HwLight>* lights) override { + for (auto i = availableLights.begin(); i != availableLights.end(); i++) { + lights->push_back(*i); + } + return ScopedAStatus::ok(); + } +}; + +int main() { + ABinderProcess_setThreadPoolMaxThreadCount(0); + + std::shared_ptr<Lights> light = SharedRefBase::make<Lights>(); + + const std::string instance = std::string() + ILights::descriptor + "/default"; + binder_status_t status = AServiceManager_addService(light->asBinder().get(), instance.c_str()); + + if (status != STATUS_OK) { + LOG(ERROR) << "Could not register" << instance; + // should abort, but don't want crash loop for local testing + } + + ABinderProcess_joinThreadPool(); + + return 1; // should not reach +} diff --git a/lights/lights.rc b/lights/lights.rc new file mode 100644 index 0000000..7fdd4e3 --- /dev/null +++ b/lights/lights.rc @@ -0,0 +1,5 @@ +service vendor.lights-midas /vendor/bin/hw/lights-midas + class hal + user system + group system + shutdown critical diff --git a/lights/lights.xml b/lights/lights.xml new file mode 100644 index 0000000..db604d6 --- /dev/null +++ b/lights/lights.xml @@ -0,0 +1,6 @@ +<manifest version="1.0" type="device"> + <hal format="aidl"> + <name>android.hardware.light</name> + <fqname>ILights/default</fqname> + </hal> +</manifest> @@ -134,8 +134,7 @@ PRODUCT_COPY_FILES += $(LOCAL_PATH)/resize2fs_partitions.sh:system/bin/resize2fs # Lights # ########## -PRODUCT_PACKAGES += \ - android.hardware.light@2.0-service.samsung \ +PRODUCT_PACKAGES += lights-midas ########## # Memory # @@ -144,7 +143,7 @@ PRODUCT_PACKAGES += \ # HAL packages PRODUCT_PACKAGES += \ android.hidl.memory@1.0-impl \ - android.hidl.memory@1.0-service \ + android.hidl.memory@1.0-service ######### # Power # @@ -152,7 +151,7 @@ PRODUCT_PACKAGES += \ # HAL packages PRODUCT_PACKAGES += \ - android.hardware.power-service.example \ + android.hardware.power-service.example \ ############ # Security # diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts index 35d19da..c156bd4 100644 --- a/sepolicy/file_contexts +++ b/sepolicy/file_contexts @@ -1 +1,3 @@ /(vendor|system/vendor)/bin/hw/android\.hardware\.gatekeeper@1\.0-service\.software u:object_r:hal_gatekeeper_default_exec:s0 + +/(vendor|system/vendor)/bin/hw/lights-midas u:object_r:hal_light_default_exec:s0 diff --git a/sepolicy/hal_light_default.te b/sepolicy/hal_light_default.te new file mode 100644 index 0000000..20ee2e2 --- /dev/null +++ b/sepolicy/hal_light_default.te @@ -0,0 +1,2 @@ +#============= hal_light_default ============== +allow hal_light_default sysfs:file { open write }; |