diff --git a/light/Android.bp b/light/Android.bp deleted file mode 100644 index ce96b78e..00000000 --- a/light/Android.bp +++ /dev/null @@ -1,38 +0,0 @@ -// -// SPDX-License-Identifier: Apache-2.0 -// - -cc_binary { - defaults: ["hidl_defaults"], - name: "android.hardware.light@2.0-service.xiaomi_sdm660", - init_rc: ["hidl/android.hardware.light@2.0-service.xiaomi_sdm660.rc"], - vintf_fragments: ["hidl/android.hardware.light@2.0-service.xiaomi_sdm660.xml"], - srcs: ["hidl/service.cpp", "hidl/Light.cpp"], - shared_libs: [ - "libbase", - "libhidlbase", - "libutils", - "android.hardware.light@2.0", - ], - relative_install_path : "hw", - vendor: true, -} - -cc_binary { - name: "android.hardware.lights-service.xiaomi_sdm660", - relative_install_path: "hw", - init_rc: ["aidl/android.hardware.lights.xiaomi_sdm660.rc"], - vintf_fragments: ["aidl/android.hardware.lights.xiaomi_sdm660.xml"], - vendor: true, - shared_libs: [ - "libbase", - "liblog", - "libhardware", - "libbinder_ndk", - "android.hardware.light-V2-ndk", - ], - srcs: [ - "aidl/Lights.cpp", - "aidl/main.cpp", - ], -} diff --git a/light/aidl/Lights.cpp b/light/aidl/Lights.cpp deleted file mode 100644 index d2fefc34..00000000 --- a/light/aidl/Lights.cpp +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * Copyright (C) 2020 The LineageOS Project - * - * SPDX-License-Identifier: Apache-2.0 - */ - -// Author := dev_harsh1998, Isaac Chen - -#define LOG_TAG "android.hardware.lights-service.xiaomi_sdm660" -/* #define LOG_NDEBUG 0 */ - -#include "Lights.h" - -#include -#include -#include - -namespace { - -#define PPCAT_NX(A, B) A/B -#define PPCAT(A, B) PPCAT_NX(A, B) -#define STRINGIFY_INNER(x) #x -#define STRINGIFY(x) STRINGIFY_INNER(x) - -#define LEDS(x) PPCAT(/sys/class/leds, x) -#define LCD_ATTR(x) STRINGIFY(PPCAT(LEDS(lcd-backlight), x)) -#define WHITE_ATTR(x) STRINGIFY(PPCAT(LEDS(white), x)) -#define BUTTON_ATTR(x) STRINGIFY(PPCAT(LEDS(button-backlight), x)) -#define BUTTON1_ATTR(x) STRINGIFY(PPCAT(LEDS(button-backlight1), x)) - -using ::android::base::ReadFileToString; -using ::android::base::WriteStringToFile; - -// Default max brightness -constexpr auto kDefaultMaxLedBrightness = 255; -constexpr auto kDefaultMaxScreenBrightness = 4095; - -// Write value to path and close file. -bool WriteToFile(const std::string& path, uint32_t content) { - return WriteStringToFile(std::to_string(content), path); -} - -bool WriteToFile(const std::string& path, const std::string& content) { - return WriteStringToFile(content, path); -} - -uint32_t RgbaToBrightness(uint32_t color) { - // Extract brightness from AARRGGBB. - uint32_t alpha = (color >> 24) & 0xFF; - - // Retrieve each of the RGB colors - uint32_t red = (color >> 16) & 0xFF; - uint32_t green = (color >> 8) & 0xFF; - uint32_t blue = color & 0xFF; - - // Scale RGB colors if a brightness has been applied by the user - if (alpha != 0xFF) { - red = red * alpha / 0xFF; - green = green * alpha / 0xFF; - blue = blue * alpha / 0xFF; - } - - return (77 * red + 150 * green + 29 * blue) >> 8; -} - -inline uint32_t RgbaToBrightness(uint32_t color, uint32_t max_brightness) { - return RgbaToBrightness(color) * max_brightness / 0xFF; -} - -inline bool IsLit(uint32_t color) { - return color & 0x00ffffff; -} - -} // anonymous namespace - -namespace aidl { -namespace android { -namespace hardware { -namespace light { - -Lights::Lights() { -std::map> lights_{ - {(int)LightType::NOTIFICATIONS, - [this](auto&&... args) { setLightNotification(args...); }}, - {(int)LightType::BATTERY, [this](auto&&... args) { setLightNotification(args...); }}, - {(int)LightType::BACKLIGHT, [this](auto&&... args) { setLightBacklight(args...); }}}; - - std::vector availableLights; - for (auto const& pair : lights_) { - int id = pair.first; - HwLight hwLight{}; - hwLight.id = id; - availableLights.emplace_back(hwLight); - } - mAvailableLights = availableLights; - mLights = lights_; - - std::string buf; - - if (ReadFileToString(LCD_ATTR(max_brightness), &buf)) { - max_screen_brightness_ = std::stoi(buf); - } else { - max_screen_brightness_ = kDefaultMaxScreenBrightness; - LOG(ERROR) << "Failed to read max screen brightness, fallback to " - << kDefaultMaxScreenBrightness; - } - - if (ReadFileToString(WHITE_ATTR(max_brightness), &buf)) { - max_led_brightness_ = std::stoi(buf); - } else { - max_led_brightness_ = kDefaultMaxLedBrightness; - LOG(ERROR) << "Failed to read max LED brightness, fallback to " << kDefaultMaxLedBrightness; - } - - if (!access(BUTTON_ATTR(brightness), W_OK)) { - lights_.emplace(std::make_pair((int)LightType::BUTTONS, - [this](auto&&... args) { setLightButtons(args...); })); - buttons_.emplace_back(BUTTON_ATTR(brightness)); - - if (!access(BUTTON1_ATTR(brightness), W_OK)) { - buttons_.emplace_back(BUTTON1_ATTR(brightness)); - } - - if (ReadFileToString(BUTTON_ATTR(max_brightness), &buf)) { - max_button_brightness_ = std::stoi(buf); - } else { - max_button_brightness_ = kDefaultMaxLedBrightness; - LOG(ERROR) << "Failed to read max button brightness, fallback to " - << kDefaultMaxLedBrightness; - } - } -} - -ndk::ScopedAStatus Lights::setLightState(int id, const HwLightState& state) { - auto it = mLights.find(id); - if (it == mLights.end()) { - LOG(ERROR) << "Light not supported"; - return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); - } - - it->second(id, state); - - return ndk::ScopedAStatus::ok(); -} - -ndk::ScopedAStatus Lights::getLights(std::vector* lights) { - for (auto i = mAvailableLights.begin(); i != mAvailableLights.end(); i++) { - lights->push_back(*i); - } - return ndk::ScopedAStatus::ok(); -} - -void Lights::setLightBacklight(int /*id*/, const HwLightState& state) { - uint32_t brightness = RgbaToBrightness(state.color, max_screen_brightness_); - WriteToFile(LCD_ATTR(brightness), brightness); -} - -void Lights::setLightButtons(int /*id*/, const HwLightState& state) { - uint32_t brightness = RgbaToBrightness(state.color, max_button_brightness_); - for (auto&& button : buttons_) { - WriteToFile(button, brightness); - } -} - -void Lights::setLightNotification(int id, const HwLightState& state) { - bool found = false; - for (auto&& [cur_id, cur_state] : notif_states_) { - if (cur_id == id) { - cur_state = state; - } - - // Fallback to battery light - if (!found && (cur_id == (int)LightType::BATTERY || IsLit(cur_state.color))) { - found = true; - LOG(DEBUG) << __func__ << ": id=" << id; - applyNotificationState(cur_state); - } - } -} - -void Lights::applyNotificationState(const HwLightState& state) { - uint32_t white_brightness = RgbaToBrightness(state.color, max_led_brightness_); - - // Turn off the leds (initially) - WriteToFile(WHITE_ATTR(breath), 0); - - if (state.flashMode == FlashMode::TIMED && state.flashOnMs > 0 && state.flashOffMs > 0) { - - // White - WriteToFile(WHITE_ATTR(delay_off), static_cast(state.flashOffMs)); - WriteToFile(WHITE_ATTR(delay_on), static_cast(state.flashOnMs)); - WriteToFile(WHITE_ATTR(breath), 1); - } else { - WriteToFile(WHITE_ATTR(brightness), white_brightness); - } -} - -} // namespace light -} // namespace hardware -} // namespace android -} // namespace aidl diff --git a/light/aidl/Lights.h b/light/aidl/Lights.h deleted file mode 100644 index f6346a18..00000000 --- a/light/aidl/Lights.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * Copyright (C) 2020-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. - */ - -#pragma once - -#include -#include -#include -#include -#include - -namespace aidl { -namespace android { -namespace hardware { -namespace light { - -class Lights : public BnLights { - public: - Lights(); - ndk::ScopedAStatus setLightState(int id, const HwLightState& state) override; - ndk::ScopedAStatus getLights(std::vector* lights) override; - - private: - void setLightBacklight(int id, const HwLightState& state); - void setLightButtons(int id, const HwLightState& state); - void setLightNotification(int id, const HwLightState& state); - void applyNotificationState(const HwLightState& state); - - uint32_t max_button_brightness_; - uint32_t max_led_brightness_; - uint32_t max_screen_brightness_; - - std::map> mLights; - std::vector mAvailableLights; - - // Keep sorted in the order of importance. - std::array, 2> notif_states_ = {{ - {(int)LightType::NOTIFICATIONS, {}}, - {(int)LightType::BATTERY, {}}, - }}; - - std::vector buttons_; -}; - -} // namespace light -} // namespace hardware -} // namespace android -} // namespace aidl diff --git a/light/aidl/android.hardware.lights.xiaomi_sdm660.rc b/light/aidl/android.hardware.lights.xiaomi_sdm660.rc deleted file mode 100644 index febf338a..00000000 --- a/light/aidl/android.hardware.lights.xiaomi_sdm660.rc +++ /dev/null @@ -1,23 +0,0 @@ -on boot - # Notification LED - chown system system /sys/class/leds/white/blink - chown system system /sys/class/leds/white/brightness - chown system system /sys/class/leds/white/duty_pcts - chown system system /sys/class/leds/white/max_brightness - chown system system /sys/class/leds/white/pause_hi - chown system system /sys/class/leds/white/pause_lo - chown system system /sys/class/leds/white/ramp_step_ms - chown system system /sys/class/leds/white/start_idx - - chown system system /sys/class/leds/button-backlight/max_brightness - chown system system /sys/class/leds/button-backlight1/brightness - chown system system /sys/class/leds/button-backlight1/max_brightness - - chown system system /sys/class/leds/lcd-backlight/max_brightness - -service vendor.light /vendor/bin/hw/android.hardware.lights-service.xiaomi_sdm660 - class hal - user system - group system - shutdown critical - writepid /dev/cpuset/system-background/tasks diff --git a/light/aidl/android.hardware.lights.xiaomi_sdm660.xml b/light/aidl/android.hardware.lights.xiaomi_sdm660.xml deleted file mode 100644 index abdd74e4..00000000 --- a/light/aidl/android.hardware.lights.xiaomi_sdm660.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - android.hardware.light - 2 - ILights/default - - diff --git a/light/aidl/main.cpp b/light/aidl/main.cpp deleted file mode 100644 index 552c48ee..00000000 --- a/light/aidl/main.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2020 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 -#include -#include -#include "Lights.h" - -using ::aidl::android::hardware::light::Lights; - -int main() { - ABinderProcess_setThreadPoolMaxThreadCount(0); - std::shared_ptr lights = ndk::SharedRefBase::make(); - if (!lights) { - return EXIT_FAILURE; - } - - const std::string instance = std::string() + Lights::descriptor + "/default"; - binder_status_t status = AServiceManager_addService(lights->asBinder().get(), instance.c_str()); - CHECK_EQ(status, STATUS_OK); - - ABinderProcess_joinThreadPool(); - return EXIT_FAILURE; // should not reached -} diff --git a/light/hidl/Light.cpp b/light/hidl/Light.cpp deleted file mode 100644 index 3c5347cf..00000000 --- a/light/hidl/Light.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * Copyright (C) 2020 The LineageOS Project - * - * SPDX-License-Identifier: Apache-2.0 - */ - -// Author := dev_harsh1998, Isaac Chen - -#define LOG_TAG "android.hardware.light@2.0-impl.xiaomi_sdm660" -/* #define LOG_NDEBUG 0 */ - -#include "Light.h" - -#include -#include -#include - -namespace { - -#define PPCAT_NX(A, B) A/B -#define PPCAT(A, B) PPCAT_NX(A, B) -#define STRINGIFY_INNER(x) #x -#define STRINGIFY(x) STRINGIFY_INNER(x) - -#define LEDS(x) PPCAT(/sys/class/leds, x) -#define LCD_ATTR(x) STRINGIFY(PPCAT(LEDS(lcd-backlight), x)) -#define WHITE_ATTR(x) STRINGIFY(PPCAT(LEDS(red), x)) - -using ::android::base::ReadFileToString; -using ::android::base::WriteStringToFile; - -// Default max brightness -constexpr auto kDefaultMaxLedBrightness = 255; -constexpr auto kDefaultMaxScreenBrightness = 4095; - -// Write value to path and close file. -bool WriteToFile(const std::string& path, uint32_t content) { - return WriteStringToFile(std::to_string(content), path); -} - -uint32_t RgbaToBrightness(uint32_t color) { - // Extract brightness from AARRGGBB. - uint32_t alpha = (color >> 24) & 0xFF; - - // Retrieve each of the RGB colors - uint32_t red = (color >> 16) & 0xFF; - uint32_t green = (color >> 8) & 0xFF; - uint32_t blue = color & 0xFF; - - // Scale RGB colors if a brightness has been applied by the user - if (alpha != 0xFF) { - red = red * alpha / 0xFF; - green = green * alpha / 0xFF; - blue = blue * alpha / 0xFF; - } - - return (77 * red + 150 * green + 29 * blue) >> 8; -} - -inline uint32_t RgbaToBrightness(uint32_t color, uint32_t max_brightness) { - return RgbaToBrightness(color) * max_brightness / 0xFF; -} - -inline bool IsLit(uint32_t color) { - return color & 0x00ffffff; -} - -} // anonymous namespace - -namespace android { -namespace hardware { -namespace light { -namespace V2_0 { -namespace implementation { - -Light::Light() { - std::string buf; - - if (ReadFileToString(LCD_ATTR(max_brightness), &buf)) { - max_screen_brightness_ = std::stoi(buf); - } else { - max_screen_brightness_ = kDefaultMaxScreenBrightness; - LOG(ERROR) << "Failed to read max screen brightness, fallback to " - << kDefaultMaxScreenBrightness; - } - - if (ReadFileToString(WHITE_ATTR(max_brightness), &buf)) { - max_led_brightness_ = std::stoi(buf); - } else { - max_led_brightness_ = kDefaultMaxLedBrightness; - LOG(ERROR) << "Failed to read max LED brightness, fallback to " << kDefaultMaxLedBrightness; - } -} - -Return Light::setLight(Type type, const LightState& state) { - auto it = lights_.find(type); - - if (it == lights_.end()) { - return Status::LIGHT_NOT_SUPPORTED; - } - - it->second(type, state); - - return Status::SUCCESS; -} - -Return Light::getSupportedTypes(getSupportedTypes_cb _hidl_cb) { - std::vector types; - - for (auto&& light : lights_) types.emplace_back(light.first); - - _hidl_cb(types); - - return Void(); -} - -void Light::setLightBacklight(Type /*type*/, const LightState& state) { - uint32_t brightness = RgbaToBrightness(state.color, max_screen_brightness_); - WriteToFile(LCD_ATTR(brightness), brightness); -} - -void Light::setLightNotification(Type type, const LightState& state) { - bool found = false; - for (auto&& [cur_type, cur_state] : notif_states_) { - if (cur_type == type) { - cur_state = state; - } - - // Fallback to battery light - if (!found && (cur_type == Type::BATTERY || IsLit(cur_state.color))) { - found = true; - LOG(DEBUG) << __func__ << ": type=" << toString(cur_type); - applyNotificationState(cur_state); - } - } -} - -void Light::applyNotificationState(const LightState& state) { - uint32_t white_brightness = RgbaToBrightness(state.color, max_led_brightness_); - - // Turn off the leds (initially) - WriteToFile(WHITE_ATTR(breath), 0); - - if (state.flashMode == Flash::TIMED && state.flashOnMs > 0 && state.flashOffMs > 0) { - LOG(DEBUG) << __func__ << ": color=" << std::hex << state.color << std::dec - << " onMs=" << state.flashOnMs << " offMs=" << state.flashOffMs; - - // White - WriteToFile(WHITE_ATTR(delay_off), static_cast(state.flashOffMs)); - WriteToFile(WHITE_ATTR(delay_on), static_cast(state.flashOnMs)); - WriteToFile(WHITE_ATTR(breath), 1); - } else { - WriteToFile(WHITE_ATTR(brightness), white_brightness); - } -} - -} // namespace implementation -} // namespace V2_0 -} // namespace light -} // namespace hardware -} // namespace android diff --git a/light/hidl/Light.h b/light/hidl/Light.h deleted file mode 100644 index 18cbb084..00000000 --- a/light/hidl/Light.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2017-2020 The LineageOS Project - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include - -#include - -namespace android { -namespace hardware { -namespace light { -namespace V2_0 { -namespace implementation { - -using ::android::hardware::Return; -using ::android::hardware::light::V2_0::ILight; -using ::android::hardware::light::V2_0::LightState; -using ::android::hardware::light::V2_0::Status; -using ::android::hardware::light::V2_0::Type; - -class Light : public ILight { - public: - Light(); - - Return setLight(Type type, const LightState& state) override; - Return getSupportedTypes(getSupportedTypes_cb _hidl_cb) override; - - private: - void setLightBacklight(Type type, const LightState& state); - void setLightButtons(Type type, const LightState& state); - void setLightNotification(Type type, const LightState& state); - void applyNotificationState(const LightState& state); - - uint32_t max_button_brightness_; - uint32_t max_led_brightness_; - uint32_t max_screen_brightness_; - - std::unordered_map> lights_{ - {Type::BACKLIGHT, [this](auto&&... args) { setLightBacklight(args...); }}, - {Type::BATTERY, [this](auto&&... args) { setLightNotification(args...); }}, - {Type::NOTIFICATIONS, [this](auto&&... args) { setLightNotification(args...); }}}; - - // Keep sorted in the order of importance. - std::array, 2> notif_states_ = {{ - {Type::NOTIFICATIONS, {}}, - {Type::BATTERY, {}}, - }}; - - std::vector buttons_; -}; - -} // namespace implementation -} // namespace V2_0 -} // namespace light -} // namespace hardware -} // namespace android diff --git a/light/hidl/android.hardware.light@2.0-service.xiaomi_sdm660.rc b/light/hidl/android.hardware.light@2.0-service.xiaomi_sdm660.rc deleted file mode 100644 index 313ee8ff..00000000 --- a/light/hidl/android.hardware.light@2.0-service.xiaomi_sdm660.rc +++ /dev/null @@ -1,19 +0,0 @@ -on early-boot - # Notification LED - chown system system /sys/class/leds/red/blink - chown system system /sys/class/leds/red/brightness - chown system system /sys/class/leds/red/duty_pcts - chown system system /sys/class/leds/red/max_brightness - chown system system /sys/class/leds/red/pause_hi - chown system system /sys/class/leds/red/pause_lo - chown system system /sys/class/leds/red/ramp_step_ms - chown system system /sys/class/leds/red/start_idx - - chown system system /sys/class/leds/lcd-backlight/max_brightness - -service vendor.light-hal-2-0 /vendor/bin/hw/android.hardware.light@2.0-service.xiaomi_sdm660 - interface android.hardware.light@2.0::ILight default - class hal - user system - group system - shutdown critical diff --git a/light/hidl/android.hardware.light@2.0-service.xiaomi_sdm660.xml b/light/hidl/android.hardware.light@2.0-service.xiaomi_sdm660.xml deleted file mode 100644 index a8bc15e2..00000000 --- a/light/hidl/android.hardware.light@2.0-service.xiaomi_sdm660.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - android.hardware.light - hwbinder - @2.0::ILight/default - - diff --git a/light/hidl/service.cpp b/light/hidl/service.cpp deleted file mode 100644 index 5b223a11..00000000 --- a/light/hidl/service.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * Copyright (C) 2020 The LineageOS Project - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#define LOG_TAG "android.hardware.light@2.0-service.xiaomi_sdm660" - -#include -#include - -#include "Light.h" - -using android::hardware::configureRpcThreadpool; -using android::hardware::joinRpcThreadpool; - -using android::hardware::light::V2_0::implementation::Light; - -using android::OK; -using android::status_t; - -int main() { - android::sp service = new Light(); - - configureRpcThreadpool(1, true); - - status_t status = service->registerAsService(); - if (status != OK) { - LOG(ERROR) << "Cannot register Light HAL service."; - return 1; - } - - LOG(DEBUG) << "Light HAL service ready."; - - joinRpcThreadpool(); - - LOG(ERROR) << "Light HAL service failed to join thread pool."; - return 1; -} diff --git a/sdm660.mk b/sdm660.mk index 75c82edb..02193dc0 100644 --- a/sdm660.mk +++ b/sdm660.mk @@ -329,7 +329,7 @@ PRODUCT_PACKAGES += \ # Lights PRODUCT_PACKAGES += \ - android.hardware.lights-service.xiaomi_sdm660 + android.hardware.light-service.xiaomi # Media PRODUCT_PACKAGES += \ diff --git a/sepolicy/vendor/file_contexts b/sepolicy/vendor/file_contexts index 4328f77b..997629f4 100644 --- a/sepolicy/vendor/file_contexts +++ b/sepolicy/vendor/file_contexts @@ -57,8 +57,8 @@ # IR HAL /(vendor|system/vendor)/bin/hw/android\.hardware\.ir@1\.0-service\.xiaomi_sdm660 u:object_r:hal_ir_default_exec:s0 -# Light HAL -/(vendor|system/vendor)/bin/hw/android\.hardware\.lights-service\.xiaomi_sdm660 u:object_r:hal_light_default_exec:s0 +# Lights +/vendor/bin/hw/android\.hardware\.light-service\.xiaomi u:object_r:hal_light_default_exec:s0 # Notification LED /sys/devices/platform/soc/800f000.qcom,spmi/spmi-0/spmi0-03/800f000.qcom,spmi:qcom,pm660l@3:qcom,leds@d000/leds/blue(/.*)? u:object_r:sysfs_graphics:s0