Skip to content

Commit

Permalink
pissarro: import lights from rubens
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik-91 committed Oct 9, 2022
1 parent f21b1c0 commit d8513fd
Show file tree
Hide file tree
Showing 9 changed files with 345 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ $(VENDOR_SYMLINKS): $(LOCAL_INSTALLED_MODULE)

ALL_DEFAULT_INSTALLED_MODULES += $(VENDOR_SYMLINKS)
endif

include $(CLEAR_VARS)

LIGHT_REPLACEMENT += $(TARGET_OUT_PRODUCT)/vendor_overlay/${PRODUCT_TARGET_VNDK_VERSION}/bin/hw/android.hardware.lights-service.mediatek
$(LIGHT_REPLACEMENT): $(LOCAL_INSTALLED_MODULE)
@mkdir -p $(dir $@)
$(hide) ln -s /system/bin/hw/$(notdir $@) $@

ALL_DEFAULT_INSTALLED_MODULES += $(LIGHT_REPLACEMENT)
4 changes: 4 additions & 0 deletions BoardConfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,7 @@ ENABLE_VENDOR_RIL_SERVICE := true

# Security patch level
VENDOR_SECURITY_PATCH := 2021-11-05

SYSTEM_EXT_PRIVATE_SEPOLICY_DIRS += $(DEVICE_PATH)/sepolicy/private
TARGET_USES_PREBUILT_VENDOR_SEPOLICY := true
TARGET_HAS_FUSEBLK_SEPOLICY_ON_VENDOR := true
4 changes: 4 additions & 0 deletions device.mk
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ DEVICE_PACKAGE_OVERLAYS += \
$(LOCAL_PATH)/overlay \
$(LOCAL_PATH)/overlay-lineage

# Lights
PRODUCT_PACKAGES += \
android.hardware.lights-service.xiaomi_mt6877

# Properties
PRODUCT_SYSTEM_DEFAULT_PROPERTIES += ro.adb.secure.recovery=0

Expand Down
14 changes: 14 additions & 0 deletions lights/Android.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cc_binary {
name: "android.hardware.lights-service.xiaomi_mt6877",
stem: "android.hardware.lights-service.mediatek",
relative_install_path: "hw",
shared_libs: [
"libbase",
"libbinder_ndk",
"android.hardware.light-V1-ndk_platform",
],
srcs: [
"Lights.cpp",
"main.cpp",
],
}
203 changes: 203 additions & 0 deletions lights/Lights.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
* 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 "Lights.h"

#include <android-base/file.h>
#include <android-base/logging.h>
#include <fcntl.h>

using ::android::base::ReadFileToString;
using ::android::base::WriteStringToFile;

namespace aidl {
namespace android {
namespace hardware {
namespace light {

#define LED_PATH(led) "/sys/class/leds/" led "/"

#define LED_BRIGHTNESS 255

static const std::string led_paths[] {
[BACKLIGHT] = LED_PATH("lcd-backlight"),
[RED] = LED_PATH("red"),
[GREEN] = LED_PATH("green"),
[BLUE] = LED_PATH("blue"),
[WHITE] = LED_PATH("white"),
};

static const std::string brightness_clone_path = "/sys/devices/virtual/mi_display/disp_feature/disp-DSI-0/brightness_clone";

#define AutoHwLight(light) {.id = (int)light, .type = light, .ordinal = 0}

// List of supported lights
const static std::vector<HwLight> kAvailableLights = {
AutoHwLight(LightType::BACKLIGHT),
AutoHwLight(LightType::BATTERY),
AutoHwLight(LightType::NOTIFICATIONS)
};

Lights::Lights() {
mWhiteLed = !access((led_paths[WHITE] + "brightness").c_str(), W_OK);
}

// AIDL methods
ndk::ScopedAStatus Lights::setLightState(int id, const HwLightState& state) {
switch (id) {
case (int)LightType::BACKLIGHT:
handleBacklight(state);
break;
case (int)LightType::BATTERY:
mBattery = state;
handleSpeakerBatteryLocked();
break;
case (int)LightType::NOTIFICATIONS:
mNotification = state;
handleSpeakerBatteryLocked();
break;
default:
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
break;
}

return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Lights::getLights(std::vector<HwLight>* lights) {
for (auto i = kAvailableLights.begin(); i != kAvailableLights.end(); i++) {
lights->push_back(*i);
}
return ndk::ScopedAStatus::ok();
}

void Lights::handleBacklight(const HwLightState& state) {
std::string value;
if (!ReadFileToString((led_paths[BACKLIGHT] + "max_brightness").c_str(), &value)) {
LOG(INFO) << "Failed reading file=" << (led_paths[BACKLIGHT] + "max_brightness").c_str();
}
int maxBrightness = stoi(value);
LOG(INFO) << "max_brightness" << maxBrightness;
if (maxBrightness < 0) {
maxBrightness = LED_BRIGHTNESS;
}
uint32_t sentBrightness = RgbaToBrightness(state.color);
uint32_t brightness = sentBrightness * maxBrightness / LED_BRIGHTNESS;
WriteToFile((led_paths[BACKLIGHT] + "brightness").c_str(), brightness);
WriteToFile((brightness_clone_path).c_str(), brightness);
}

// device methods
void Lights::setSpeakerLightLocked(const HwLightState& state) {
uint32_t alpha, red, green, blue;
uint32_t blink;
bool rc = true;

// Extract brightness from AARRGGBB
alpha = (state.color >> 24) & 0xFF;
red = (state.color >> 16) & 0xFF;
green = (state.color >> 8) & 0xFF;
blue = state.color & 0xFF;

// Scale RGB brightness if Alpha brightness is not 0xFF
if (alpha != 0xFF) {
red = (red * alpha) / 0xFF;
green = (green * alpha) / 0xFF;
blue = (blue * alpha) / 0xFF;
}

blink = (state.flashOnMs != 0 && state.flashOffMs != 0);

switch (state.flashMode) {
case FlashMode::HARDWARE:
case FlashMode::TIMED:
if (mWhiteLed) {
rc = setLedBreath(WHITE, blink);
} else {
if (!!red)
rc = setLedBreath(RED, blink);
if (!!green)
rc &= setLedBreath(GREEN, blink);
if (!!blue)
rc &= setLedBreath(BLUE, blink);
}
if (rc)
break;
FALLTHROUGH_INTENDED;
case FlashMode::NONE:
default:
if (mWhiteLed) {
rc = setLedBrightness(WHITE, RgbaToBrightness(state.color));
} else {
rc = setLedBrightness(RED, red);
rc &= setLedBrightness(GREEN, green);
rc &= setLedBrightness(BLUE, blue);
}
break;
}

return;
}

void Lights::handleSpeakerBatteryLocked() {
if (IsLit(mBattery.color))
return setSpeakerLightLocked(mBattery);
else
return setSpeakerLightLocked(mNotification);
}

bool Lights::setLedBreath(led_type led, uint32_t value) {
return WriteToFile(led_paths[led] + "blink", value);
}

bool Lights::setLedBrightness(led_type led, uint32_t value) {
return WriteToFile(led_paths[led] + "brightness", value);
}

// Utils
bool Lights::IsLit(uint32_t color) {
return color & 0x00ffffff;
}

uint32_t Lights::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;
}

// Write value to path and close file.
bool Lights::WriteToFile(const std::string& path, uint32_t content) {
LOG(INFO) << "Writing to file=" << path << ", " << content;
return WriteStringToFile(std::to_string(content), path);
}

} // namespace light
} // namespace hardware
} // namespace android
} // namespace aidl
61 changes: 61 additions & 0 deletions lights/Lights.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.
*/

#pragma once

#include <aidl/android/hardware/light/BnLights.h>

namespace aidl {
namespace android {
namespace hardware {
namespace light {

enum led_type {
BACKLIGHT,
RED,
GREEN,
BLUE,
WHITE,
};

class Lights : public BnLights {
public:
Lights();

ndk::ScopedAStatus setLightState(int id, const HwLightState& state) override;
ndk::ScopedAStatus getLights(std::vector<HwLight>* types) override;

private:
void setSpeakerLightLocked(const HwLightState& state);
void handleSpeakerBatteryLocked();
void handleBacklight(const HwLightState& state);

bool setLedBreath(led_type led, uint32_t value);
bool setLedBrightness(led_type led, uint32_t value);

bool IsLit(uint32_t color);
uint32_t RgbaToBrightness(uint32_t color);
bool WriteToFile(const std::string& path, uint32_t content);

bool mWhiteLed;
HwLightState mNotification;
HwLightState mBattery;
};

} // namespace light
} // namespace hardware
} // namespace android
} // namespace aidl
35 changes: 35 additions & 0 deletions lights/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 "Lights.h"

#include <android-base/logging.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>

using ::aidl::android::hardware::light::Lights;

int main() {
ABinderProcess_setThreadPoolMaxThreadCount(0);
std::shared_ptr<Lights> lights = ndk::SharedRefBase::make<Lights>();

const std::string instance = std::string() + Lights::descriptor + "/default";
binder_status_t status = AServiceManager_addService(lights->asBinder().get(), instance.c_str());
CHECK(status == STATUS_OK);

ABinderProcess_joinThreadPool();
return EXIT_FAILURE; // should not reached
}
7 changes: 7 additions & 0 deletions sepolicy/private/file_contexts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Lights
/system/bin/hw/android\.hardware\.lights-service\.mediatek u:object_r:hal_light_default_exec:s0
/(product|system/product)/vendor_overlay/[0-9]+/bin/hw/android\.hardware\.lights-service\.mediatek u:object_r:hal_light_default_exec:s0

# Vendor overlay
/(product|system/product)/vendor_overlay/[0-9]+/etc(/.*)? u:object_r:vendor_configs_file:s0
/(product|system/product)/vendor_overlay/[0-9]+/lib(64)?/hw u:object_r:vendor_hal_file:s0
8 changes: 8 additions & 0 deletions sepolicy/private/init.te
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
allow init vendor_file:file mounton;

# Allow init to mount vendor configs
allow init vendor_configs_file:dir mounton;
allow init vendor_configs_file:file mounton;

# Allow init to mount vendor overlay
allow init vendor_overlay_file:dir mounton;

0 comments on commit d8513fd

Please sign in to comment.