/* * Copyright (C) 2019 The Android Open Source Project * Copyright (C) 2023 KonstaKANG * * 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. */ #define LOG_TAG "android.hardware.light-service.rpi" #include "Lights.h" #include #include using ::android::base::ReadFileToString; using ::android::base::WriteStringToFile; namespace aidl::android::hardware::light { static const uint32_t defaultMaxBrightness = 255; static const std::string backlightBasePath = "/sys/class/backlight/"; static const std::string backlightPaths[] = { "10-0045", "11-0045", "rpi_backlight", }; static const std::vector availableLights = { {.id = (int)LightType::BACKLIGHT, .type = LightType::BACKLIGHT, .ordinal = 0} }; Lights::Lights() { maxBrightness = defaultMaxBrightness; for (auto &path : backlightPaths) { std::string backlightMaxBrightnessPath = backlightBasePath + path + "/max_brightness"; if (!access(backlightMaxBrightnessPath.c_str(), R_OK)) { std::string maxBrightnessValue; if (ReadFileToString(backlightMaxBrightnessPath, &maxBrightnessValue)) { maxBrightness = std::stoi(maxBrightnessValue); LOG(INFO) << backlightMaxBrightnessPath << " = " << maxBrightnessValue; break; } } } } ndk::ScopedAStatus Lights::setLightState(int id, const HwLightState& state) { HwLight const& light = availableLights[id]; std::string const brightness = std::to_string(rgbToScaledBrightness(state, maxBrightness)); switch (light.type) { case LightType::BACKLIGHT: for (auto &path : backlightPaths) { std::string backlightBrightnessPath = backlightBasePath + path + "/brightness"; if (!access(backlightBrightnessPath.c_str(), W_OK)) { WriteStringToFile(brightness, backlightBrightnessPath); break; } } break; default: return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Lights::getLights(std::vector* lights) { for (auto i = availableLights.begin(); i != availableLights.end(); i++) { lights->push_back(*i); } return ndk::ScopedAStatus::ok(); } uint32_t Lights::rgbToScaledBrightness(const HwLightState& state, uint32_t maxBrightness) { uint32_t color = state.color & 0x00ffffff; uint32_t brightness = ((77 * ((color >> 16) & 0xff)) + (150 * ((color >> 8) & 0xff)) + (29 * (color & 0xff))) >> 8; return brightness * maxBrightness / 0xff; } } // aidl::android::hardware::light