2021-09-22 05:29:46 -04:00
|
|
|
/*
|
2022-09-12 12:18:05 -04:00
|
|
|
* Copyright (C) 2021-2022 The LineageOS Project
|
2021-09-22 05:29:46 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Lights.h"
|
|
|
|
|
2021-09-26 15:08:29 -04:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include "LED.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
|
2021-09-22 05:29:46 -04:00
|
|
|
namespace aidl {
|
|
|
|
namespace android {
|
|
|
|
namespace hardware {
|
|
|
|
namespace light {
|
|
|
|
|
2021-09-26 15:08:29 -04:00
|
|
|
static const std::string kAllButtonsPaths[] = {
|
|
|
|
"/sys/class/leds/button-backlight/brightness",
|
|
|
|
"/sys/class/leds/button-backlight1/brightness",
|
|
|
|
};
|
|
|
|
|
|
|
|
enum led_type {
|
|
|
|
RED,
|
|
|
|
GREEN,
|
|
|
|
BLUE,
|
|
|
|
WHITE,
|
|
|
|
MAX_LEDS,
|
|
|
|
};
|
|
|
|
|
|
|
|
static LED kLEDs[MAX_LEDS] = {
|
|
|
|
[RED] = LED("red"),
|
|
|
|
[GREEN] = LED("green"),
|
|
|
|
[BLUE] = LED("blue"),
|
|
|
|
[WHITE] = LED("white"),
|
|
|
|
};
|
|
|
|
|
|
|
|
#define AutoHwLight(light) {.id = (int32_t)light, .type = light, .ordinal = 0}
|
|
|
|
|
|
|
|
static const HwLight kBacklightHwLight = AutoHwLight(LightType::BACKLIGHT);
|
|
|
|
static const HwLight kBatteryHwLight = AutoHwLight(LightType::BATTERY);
|
|
|
|
static const HwLight kButtonsHwLight = AutoHwLight(LightType::BUTTONS);
|
|
|
|
static const HwLight kNotificationHwLight = AutoHwLight(LightType::NOTIFICATIONS);
|
|
|
|
|
|
|
|
Lights::Lights() {
|
2022-09-12 14:58:21 -04:00
|
|
|
mBacklightDevice = getBacklightDevice();
|
|
|
|
if (mBacklightDevice) {
|
2021-09-26 15:08:29 -04:00
|
|
|
mLights.push_back(kBacklightHwLight);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& buttons : kAllButtonsPaths) {
|
|
|
|
if (!fileWriteable(buttons))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
mButtonsPaths.push_back(buttons);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mButtonsPaths.empty())
|
|
|
|
mLights.push_back(kButtonsHwLight);
|
|
|
|
|
|
|
|
mWhiteLED = kLEDs[WHITE].exists();
|
|
|
|
|
|
|
|
mLights.push_back(kBatteryHwLight);
|
|
|
|
mLights.push_back(kNotificationHwLight);
|
|
|
|
}
|
|
|
|
|
|
|
|
ndk::ScopedAStatus Lights::setLightState(int32_t id, const HwLightState& state) {
|
2022-09-12 15:00:23 -04:00
|
|
|
rgb_t color(state.color);
|
|
|
|
rgb_t batteryStateColor;
|
|
|
|
|
2021-09-26 15:08:29 -04:00
|
|
|
LightType type = static_cast<LightType>(id);
|
|
|
|
switch (type) {
|
|
|
|
case LightType::BACKLIGHT:
|
2022-09-12 14:58:21 -04:00
|
|
|
if (mBacklightDevice)
|
|
|
|
mBacklightDevice->setBacklight(color.toBrightness());
|
2021-09-26 15:08:29 -04:00
|
|
|
break;
|
|
|
|
case LightType::BUTTONS:
|
|
|
|
for (auto& buttons : mButtonsPaths)
|
2022-09-12 15:00:23 -04:00
|
|
|
writeToFile(buttons, color.isLit());
|
2021-09-26 15:08:29 -04:00
|
|
|
break;
|
|
|
|
case LightType::BATTERY:
|
|
|
|
case LightType::NOTIFICATIONS:
|
|
|
|
mLEDMutex.lock();
|
|
|
|
if (type == LightType::BATTERY)
|
|
|
|
mLastBatteryState = state;
|
|
|
|
else
|
|
|
|
mLastNotificationState = state;
|
2022-09-12 15:00:23 -04:00
|
|
|
batteryStateColor = rgb_t(mLastBatteryState.color);
|
|
|
|
setLED(batteryStateColor.isLit() ? mLastBatteryState : mLastNotificationState);
|
2021-09-26 15:08:29 -04:00
|
|
|
mLEDMutex.unlock();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ndk::ScopedAStatus::ok();
|
2021-09-22 05:29:46 -04:00
|
|
|
}
|
|
|
|
|
2021-09-26 15:08:29 -04:00
|
|
|
ndk::ScopedAStatus Lights::getLights(std::vector<HwLight> *_aidl_return) {
|
|
|
|
for (auto& light : mLights)
|
|
|
|
_aidl_return->push_back(light);
|
|
|
|
|
|
|
|
return ndk::ScopedAStatus::ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Lights::setLED(const HwLightState& state) {
|
|
|
|
bool rc = true;
|
2022-09-12 15:00:23 -04:00
|
|
|
rgb_t color(state.color);
|
2022-09-12 12:18:05 -04:00
|
|
|
uint8_t blink = (state.flashOnMs != 0 && state.flashOffMs != 0);
|
2021-09-26 15:08:29 -04:00
|
|
|
|
|
|
|
switch (state.flashMode) {
|
|
|
|
case FlashMode::HARDWARE:
|
|
|
|
case FlashMode::TIMED:
|
|
|
|
if (mWhiteLED) {
|
|
|
|
rc = kLEDs[WHITE].setBreath(blink);
|
|
|
|
} else {
|
|
|
|
if (!!color.red)
|
|
|
|
rc &= kLEDs[RED].setBreath(blink);
|
|
|
|
if (!!color.green)
|
|
|
|
rc &= kLEDs[GREEN].setBreath(blink);
|
|
|
|
if (!!color.blue)
|
|
|
|
rc &= kLEDs[BLUE].setBreath(blink);
|
|
|
|
}
|
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
FALLTHROUGH_INTENDED;
|
|
|
|
default:
|
|
|
|
if (mWhiteLED) {
|
2022-09-12 15:00:23 -04:00
|
|
|
rc = kLEDs[WHITE].setBrightness(color.toBrightness());
|
2021-09-26 15:08:29 -04:00
|
|
|
} else {
|
|
|
|
rc = kLEDs[RED].setBrightness(color.red);
|
|
|
|
rc &= kLEDs[GREEN].setBrightness(color.green);
|
|
|
|
rc &= kLEDs[BLUE].setBrightness(color.blue);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2021-09-22 05:29:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace light
|
|
|
|
} // namespace hardware
|
|
|
|
} // namespace android
|
|
|
|
} // namespace aidl
|