From 3b738dc51c4c13ddfde03b1b47665bbdd49e4239 Mon Sep 17 00:00:00 2001 From: Tim Zimmermann Date: Wed, 24 Nov 2021 17:27:06 +0100 Subject: [PATCH] samsung: light: convert to AIDL implementation Change-Id: Id3860ca6e84ea77766322cbd43239e9cebecbc73 --- aidl/light/Android.bp | 23 ++++ hidl/light/Light.cpp => aidl/light/Lights.cpp | 100 ++++++++---------- aidl/light/Lights.h | 54 ++++++++++ .../android.hardware.light-service.samsung.rc | 5 + ...android.hardware.light-service.samsung.xml | 6 ++ {hidl => aidl}/light/include/samsung_lights.h | 0 aidl/light/service.cpp | 27 +++++ hidl/light/Android.mk | 41 ------- hidl/light/Light.h | 76 ------------- ...roid.hardware.light@2.0-service.samsung.rc | 7 -- hidl/light/service.cpp | 54 ---------- 11 files changed, 157 insertions(+), 236 deletions(-) create mode 100644 aidl/light/Android.bp rename hidl/light/Light.cpp => aidl/light/Lights.cpp (56%) create mode 100644 aidl/light/Lights.h create mode 100644 aidl/light/android.hardware.light-service.samsung.rc create mode 100644 aidl/light/android.hardware.light-service.samsung.xml rename {hidl => aidl}/light/include/samsung_lights.h (100%) create mode 100644 aidl/light/service.cpp delete mode 100644 hidl/light/Android.mk delete mode 100644 hidl/light/Light.h delete mode 100644 hidl/light/android.hardware.light@2.0-service.samsung.rc delete mode 100644 hidl/light/service.cpp diff --git a/aidl/light/Android.bp b/aidl/light/Android.bp new file mode 100644 index 0000000..1b2322c --- /dev/null +++ b/aidl/light/Android.bp @@ -0,0 +1,23 @@ +// +// Copyright (C) 2021 The LineageOS Project +// +// SPDX-License-Identifier: Apache-2.0 +// + +cc_binary { + name: "android.hardware.light-service.samsung", + relative_install_path: "hw", + init_rc: ["android.hardware.light-service.samsung.rc"], + vintf_fragments: ["android.hardware.light-service.samsung.xml"], + local_include_dirs: ["include"], + srcs: [ + "Lights.cpp", + "service.cpp", + ], + shared_libs: [ + "libbase", + "libbinder_ndk", + "android.hardware.light-V1-ndk_platform", + ], + vendor: true, +} diff --git a/hidl/light/Light.cpp b/aidl/light/Lights.cpp similarity index 56% rename from hidl/light/Light.cpp rename to aidl/light/Lights.cpp index 6a105da..749030e 100644 --- a/hidl/light/Light.cpp +++ b/aidl/light/Lights.cpp @@ -1,37 +1,23 @@ /* - * Copyright (C) 2019 The LineageOS Project + * Copyright (C) 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. + * SPDX-License-Identifier: Apache-2.0 */ -#define LOG_TAG "android.hardware.light@2.0-service.samsung" + +#define LOG_TAG "android.hardware.lights-service.samsung" #include -#include +#include -#include "Light.h" +#include "Lights.h" #define COLOR_MASK 0x00ffffff #define MAX_INPUT_BRIGHTNESS 255 -using android::hardware::light::V2_0::LightState; -using android::hardware::light::V2_0::Status; -using android::hardware::light::V2_0::Type; - +namespace aidl { namespace android { namespace hardware { namespace light { -namespace V2_0 { -namespace implementation { /* * Write value to path and close file. @@ -51,26 +37,27 @@ static T get(const std::string& path, const T& def) { return file.fail() ? def : result; } -Light::Light() { - mLights.emplace(Type::BACKLIGHT, - std::bind(&Light::handleBacklight, this, std::placeholders::_1)); +Lights::Lights() { + mLights.emplace(LightType::BACKLIGHT, + std::bind(&Lights::handleBacklight, this, std::placeholders::_1)); #ifdef BUTTON_BRIGHTNESS_NODE - mLights.emplace(Type::BUTTONS, std::bind(&Light::handleButtons, this, std::placeholders::_1)); + mLights.emplace(LightType::BUTTONS, std::bind(&Lights::handleButtons, this, std::placeholders::_1)); #endif /* BUTTON_BRIGHTNESS_NODE */ #ifdef LED_BLINK_NODE - mLights.emplace(Type::BATTERY, std::bind(&Light::handleBattery, this, std::placeholders::_1)); - mLights.emplace(Type::NOTIFICATIONS, - std::bind(&Light::handleNotifications, this, std::placeholders::_1)); - mLights.emplace(Type::ATTENTION, - std::bind(&Light::handleAttention, this, std::placeholders::_1)); + mLights.emplace(LightType::BATTERY, std::bind(&Lights::handleBattery, this, std::placeholders::_1)); + mLights.emplace(LightType::NOTIFICATIONS, + std::bind(&Lights::handleNotifications, this, std::placeholders::_1)); + mLights.emplace(LightType::ATTENTION, + std::bind(&Lights::handleAttention, this, std::placeholders::_1)); #endif /* LED_BLINK_NODE */ } -Return Light::setLight(Type type, const LightState& state) { +ndk::ScopedAStatus Lights::setLightState(int32_t id, const HwLightState& state) { + LightType type = static_cast(id); auto it = mLights.find(type); if (it == mLights.end()) { - return Status::LIGHT_NOT_SUPPORTED; + return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } /* @@ -80,10 +67,10 @@ Return Light::setLight(Type type, const LightState& state) { it->second(state); - return Status::SUCCESS; + return ndk::ScopedAStatus::ok(); } -void Light::handleBacklight(const LightState& state) { +void Lights::handleBacklight(const HwLightState& state) { uint32_t max_brightness = get(PANEL_MAX_BRIGHTNESS_NODE, MAX_INPUT_BRIGHTNESS); uint32_t brightness = rgbToBrightness(state); @@ -95,7 +82,7 @@ void Light::handleBacklight(const LightState& state) { } #ifdef BUTTON_BRIGHTNESS_NODE -void Light::handleButtons(const LightState& state) { +void Lights::handleButtons(const HwLightState& state) { #ifdef VAR_BUTTON_BRIGHTNESS uint32_t brightness = rgbToBrightness(state); #else @@ -107,24 +94,24 @@ void Light::handleButtons(const LightState& state) { #endif #ifdef LED_BLINK_NODE -void Light::handleBattery(const LightState& state) { +void Lights::handleBattery(const HwLightState& state) { mBatteryState = state; setNotificationLED(); } -void Light::handleNotifications(const LightState& state) { +void Lights::handleNotifications(const HwLightState& state) { mNotificationState = state; setNotificationLED(); } -void Light::handleAttention(const LightState& state) { +void Lights::handleAttention(const HwLightState& state) { mAttentionState = state; setNotificationLED(); } -void Light::setNotificationLED() { +void Lights::setNotificationLED() { int32_t adjusted_brightness = MAX_INPUT_BRIGHTNESS; - LightState state; + HwLightState state; #ifdef LED_BLN_NODE bool bln = false; #endif /* LED_BLN_NODE */ @@ -138,11 +125,11 @@ void Light::setNotificationLED() { } else if (mAttentionState.color & COLOR_MASK) { adjusted_brightness = LED_BRIGHTNESS_ATTENTION; state = mAttentionState; - if (state.flashMode == Flash::HARDWARE) { - if (state.flashOnMs > 0 && state.flashOffMs == 0) state.flashMode = Flash::NONE; + if (state.flashMode == FlashMode::HARDWARE) { + if (state.flashOnMs > 0 && state.flashOffMs == 0) state.flashMode = FlashMode::NONE; state.color = 0x000000ff; } - if (state.flashMode == Flash::NONE) { + if (state.flashMode == FlashMode::NONE) { state.color = 0; } } else if (mBatteryState.color & COLOR_MASK) { @@ -153,13 +140,13 @@ void Light::setNotificationLED() { return; } - if (state.flashMode == Flash::NONE) { + if (state.flashMode == FlashMode::NONE) { state.flashOnMs = 0; state.flashOffMs = 0; } state.color = calibrateColor(state.color & COLOR_MASK, adjusted_brightness); - set(LED_BLINK_NODE, android::base::StringPrintf("0x%08x %d %d", state.color, state.flashOnMs, + set(LED_BLINK_NODE, ::android::base::StringPrintf("0x%08x %d %d", state.color, state.flashOnMs, state.flashOffMs)); #ifdef LED_BLN_NODE @@ -169,7 +156,7 @@ void Light::setNotificationLED() { #endif /* LED_BLN_NODE */ } -uint32_t Light::calibrateColor(uint32_t color, int32_t brightness) { +uint32_t Lights::calibrateColor(uint32_t color, int32_t brightness) { uint32_t red = ((color >> 16) & 0xFF) * LED_ADJUSTMENT_R; uint32_t green = ((color >> 8) & 0xFF) * LED_ADJUSTMENT_G; uint32_t blue = (color & 0xFF) * LED_ADJUSTMENT_B; @@ -179,27 +166,24 @@ uint32_t Light::calibrateColor(uint32_t color, int32_t brightness) { } #endif /* LED_BLINK_NODE */ -Return Light::getSupportedTypes(getSupportedTypes_cb _hidl_cb) { - std::vector types; +#define AutoHwLight(light) {.id = (int32_t)light, .type = light, .ordinal = 0} +ndk::ScopedAStatus Lights::getLights(std::vector *_aidl_return) { for (auto const& light : mLights) { - types.push_back(light.first); + _aidl_return->push_back(AutoHwLight(light.first)); } - _hidl_cb(types); - - return Void(); + return ndk::ScopedAStatus::ok(); } -uint32_t Light::rgbToBrightness(const LightState& state) { +uint32_t Lights::rgbToBrightness(const HwLightState& state) { uint32_t color = state.color & COLOR_MASK; return ((77 * ((color >> 16) & 0xff)) + (150 * ((color >> 8) & 0xff)) + (29 * (color & 0xff))) >> 8; } -} // namespace implementation -} // namespace V2_0 -} // namespace light -} // namespace hardware -} // namespace android +} // namespace light +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/aidl/light/Lights.h b/aidl/light/Lights.h new file mode 100644 index 0000000..253167b --- /dev/null +++ b/aidl/light/Lights.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2021 The LineageOS Project + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include "samsung_lights.h" + +using ::aidl::android::hardware::light::HwLightState; +using ::aidl::android::hardware::light::HwLight; + +namespace aidl { +namespace android { +namespace hardware { +namespace light { + +class Lights : public BnLights { +public: + Lights(); + + ndk::ScopedAStatus setLightState(int32_t id, const HwLightState& state) override; + ndk::ScopedAStatus getLights(std::vector *_aidl_return) override; + +private: + void handleBacklight(const HwLightState& state); +#ifdef BUTTON_BRIGHTNESS_NODE + void handleButtons(const HwLightState& state); +#endif /* BUTTON_BRIGHTNESS_NODE */ +#ifdef LED_BLINK_NODE + void handleBattery(const HwLightState& state); + void handleNotifications(const HwLightState& state); + void handleAttention(const HwLightState& state); + void setNotificationLED(); + uint32_t calibrateColor(uint32_t color, int32_t brightness); + + HwLightState mAttentionState; + HwLightState mBatteryState; + HwLightState mNotificationState; +#endif /* LED_BLINK_NODE */ + + uint32_t rgbToBrightness(const HwLightState& state); + + std::mutex mLock; + std::unordered_map> mLights; +}; + +} // namespace light +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/aidl/light/android.hardware.light-service.samsung.rc b/aidl/light/android.hardware.light-service.samsung.rc new file mode 100644 index 0000000..249e036 --- /dev/null +++ b/aidl/light/android.hardware.light-service.samsung.rc @@ -0,0 +1,5 @@ +service vendor.light-default /vendor/bin/hw/android.hardware.light-service.samsung + class hal + user system + group system + shutdown critical diff --git a/aidl/light/android.hardware.light-service.samsung.xml b/aidl/light/android.hardware.light-service.samsung.xml new file mode 100644 index 0000000..db604d6 --- /dev/null +++ b/aidl/light/android.hardware.light-service.samsung.xml @@ -0,0 +1,6 @@ + + + android.hardware.light + ILights/default + + diff --git a/hidl/light/include/samsung_lights.h b/aidl/light/include/samsung_lights.h similarity index 100% rename from hidl/light/include/samsung_lights.h rename to aidl/light/include/samsung_lights.h diff --git a/aidl/light/service.cpp b/aidl/light/service.cpp new file mode 100644 index 0000000..bd3b7a7 --- /dev/null +++ b/aidl/light/service.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2021 The LineageOS Project + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define LOG_TAG "android.hardware.light-service.samsung" + +#include "Lights.h" + +#include +#include +#include + +using ::aidl::android::hardware::light::Lights; + +int main() { + ABinderProcess_setThreadPoolMaxThreadCount(0); + std::shared_ptr lights = ndk::SharedRefBase::make(); + + 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 reach +} diff --git a/hidl/light/Android.mk b/hidl/light/Android.mk deleted file mode 100644 index b3be1d9..0000000 --- a/hidl/light/Android.mk +++ /dev/null @@ -1,41 +0,0 @@ -# -# Copyright (C) 2019 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. -# - -LOCAL_PATH := $(call my-dir) - -include $(CLEAR_VARS) - -LOCAL_SRC_FILES := \ - Light.cpp \ - service.cpp - -LOCAL_C_INCLUDES := $(LOCAL_PATH)/include - -LOCAL_SHARED_LIBRARIES := \ - libbase \ - libbinder \ - libhidlbase \ - libutils \ - android.hardware.light@2.0 - -LOCAL_MODULE := android.hardware.light@2.0-service.samsung -LOCAL_INIT_RC := android.hardware.light@2.0-service.samsung.rc -LOCAL_MODULE_RELATIVE_PATH := hw -LOCAL_MODULE_TAGS := optional -LOCAL_MODULE_OWNER := samsung -LOCAL_VENDOR_MODULE := true - -include $(BUILD_EXECUTABLE) diff --git a/hidl/light/Light.h b/hidl/light/Light.h deleted file mode 100644 index a318e15..0000000 --- a/hidl/light/Light.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (C) 2019 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. - */ - -#ifndef ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H -#define ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H - -#include -#include -#include -#include -#include -#include "samsung_lights.h" - -namespace android { -namespace hardware { -namespace light { -namespace V2_0 { -namespace implementation { - -using ::android::sp; -using ::android::hardware::hidl_array; -using ::android::hardware::hidl_memory; -using ::android::hardware::hidl_string; -using ::android::hardware::hidl_vec; -using ::android::hardware::Return; -using ::android::hardware::Void; - -struct Light : public ILight { - Light(); - - Return setLight(Type type, const LightState& state) override; - Return getSupportedTypes(getSupportedTypes_cb _hidl_cb) override; - - private: - void handleBacklight(const LightState& state); -#ifdef BUTTON_BRIGHTNESS_NODE - void handleButtons(const LightState& state); -#endif /* BUTTON_BRIGHTNESS_NODE */ -#ifdef LED_BLINK_NODE - void handleBattery(const LightState& state); - void handleNotifications(const LightState& state); - void handleAttention(const LightState& state); - void setNotificationLED(); - uint32_t calibrateColor(uint32_t color, int32_t brightness); - - LightState mAttentionState; - LightState mBatteryState; - LightState mNotificationState; -#endif /* LED_BLINK_NODE */ - - uint32_t rgbToBrightness(const LightState& state); - - std::mutex mLock; - std::unordered_map> mLights; -}; - -} // namespace implementation -} // namespace V2_0 -} // namespace light -} // namespace hardware -} // namespace android - -#endif // ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H diff --git a/hidl/light/android.hardware.light@2.0-service.samsung.rc b/hidl/light/android.hardware.light@2.0-service.samsung.rc deleted file mode 100644 index ad796e4..0000000 --- a/hidl/light/android.hardware.light@2.0-service.samsung.rc +++ /dev/null @@ -1,7 +0,0 @@ -service vendor.light-hal-2-0 /vendor/bin/hw/android.hardware.light@2.0-service.samsung - interface android.hardware.light@2.0::ILight default - class hal - user system - group system - # shutting off lights while powering-off - shutdown critical diff --git a/hidl/light/service.cpp b/hidl/light/service.cpp deleted file mode 100644 index 5cc0855..0000000 --- a/hidl/light/service.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2019 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. - */ - -#define LOG_TAG "android.hardware.light@2.0-service.samsung" - -#include -#include -#include - -#include "Light.h" - -using android::hardware::configureRpcThreadpool; -using android::hardware::joinRpcThreadpool; - -using android::hardware::light::V2_0::ILight; -using android::hardware::light::V2_0::implementation::Light; - -using android::OK; -using android::sp; -using android::status_t; - -int main() { - sp light = new Light(); - - configureRpcThreadpool(1, true); - - status_t status = light->registerAsService(); - - if (status != OK) { - LOG(ERROR) << "Could not register service for Light HAL"; - goto shutdown; - } - - LOG(INFO) << "Light HAL service is Ready."; - joinRpcThreadpool(); - -shutdown: - // In normal operation, we don't expect the thread pool to shutdown - LOG(ERROR) << "Light HAL failed to join thread pool."; - return 1; -} -- 2.20.1