sdm660-common: Move to common Xiaomi lights AIDL

Change-Id: Icb3bb31ebad01519b91a6d41b47e1b57e1ac84e1
Signed-off-by: pix106 <sbordenave@gmail.com>
This commit is contained in:
Sebastiano Barezzi 2022-10-06 15:41:51 +05:30 committed by pix106
parent 4ecaa0a848
commit c8d924ba0a
13 changed files with 3 additions and 660 deletions

View file

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

View file

@ -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 <android-base/file.h>
#include <android-base/logging.h>
#include <unistd.h>
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<int, std::function<void(int id, const HwLightState&)>> 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<HwLight> 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<HwLight>* 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<uint32_t>(state.flashOffMs));
WriteToFile(WHITE_ATTR(delay_on), static_cast<uint32_t>(state.flashOnMs));
WriteToFile(WHITE_ATTR(breath), 1);
} else {
WriteToFile(WHITE_ATTR(brightness), white_brightness);
}
}
} // namespace light
} // namespace hardware
} // namespace android
} // namespace aidl

View file

@ -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 <aidl/android/hardware/light/BnLights.h>
#include <hardware/hardware.h>
#include <hardware/lights.h>
#include <map>
#include <sstream>
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<HwLight>* 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<int, std::function<void(int id, const HwLightState&)>> mLights;
std::vector<HwLight> mAvailableLights;
// Keep sorted in the order of importance.
std::array<std::pair<int, HwLightState>, 2> notif_states_ = {{
{(int)LightType::NOTIFICATIONS, {}},
{(int)LightType::BATTERY, {}},
}};
std::vector<std::string> buttons_;
};
} // namespace light
} // namespace hardware
} // namespace android
} // namespace aidl

View file

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

View file

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

View file

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

View file

@ -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 <android-base/file.h>
#include <android-base/logging.h>
#include <unistd.h>
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<Status> 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<void> Light::getSupportedTypes(getSupportedTypes_cb _hidl_cb) {
std::vector<Type> 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<uint32_t>(state.flashOffMs));
WriteToFile(WHITE_ATTR(delay_on), static_cast<uint32_t>(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

View file

@ -1,60 +0,0 @@
/*
* Copyright (C) 2017-2020 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <android/hardware/light/2.0/ILight.h>
#include <unordered_map>
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<Status> setLight(Type type, const LightState& state) override;
Return<void> 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<Type, std::function<void(Type type, const LightState&)>> 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<std::pair<Type, LightState>, 2> notif_states_ = {{
{Type::NOTIFICATIONS, {}},
{Type::BATTERY, {}},
}};
std::vector<std::string> buttons_;
};
} // namespace implementation
} // namespace V2_0
} // namespace light
} // namespace hardware
} // namespace android

View file

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

View file

@ -1,7 +0,0 @@
<manifest version="2.0" type="device">
<hal format="hidl">
<name>android.hardware.light</name>
<transport>hwbinder</transport>
<fqname>@2.0::ILight/default</fqname>
</hal>
</manifest>

View file

@ -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 <android-base/logging.h>
#include <hidl/HidlTransportSupport.h>
#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<Light> 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;
}

View file

@ -329,7 +329,7 @@ PRODUCT_PACKAGES += \
# Lights # Lights
PRODUCT_PACKAGES += \ PRODUCT_PACKAGES += \
android.hardware.lights-service.xiaomi_sdm660 android.hardware.light-service.xiaomi
# Media # Media
PRODUCT_PACKAGES += \ PRODUCT_PACKAGES += \

View file

@ -57,8 +57,8 @@
# IR HAL # IR HAL
/(vendor|system/vendor)/bin/hw/android\.hardware\.ir@1\.0-service\.xiaomi_sdm660 u:object_r:hal_ir_default_exec:s0 /(vendor|system/vendor)/bin/hw/android\.hardware\.ir@1\.0-service\.xiaomi_sdm660 u:object_r:hal_ir_default_exec:s0
# Light HAL # Lights
/(vendor|system/vendor)/bin/hw/android\.hardware\.lights-service\.xiaomi_sdm660 u:object_r:hal_light_default_exec:s0 /vendor/bin/hw/android\.hardware\.light-service\.xiaomi u:object_r:hal_light_default_exec:s0
# Notification LED # 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 /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