samsung: light: convert to AIDL implementation

Change-Id: Id3860ca6e84ea77766322cbd43239e9cebecbc73
tirimbino
Tim Zimmermann 3 years ago
parent 99af8deb5f
commit 3b738dc51c
No known key found for this signature in database
GPG Key ID: 6DC21A63F819C5EF
  1. 23
      aidl/light/Android.bp
  2. 100
      aidl/light/Lights.cpp
  3. 54
      aidl/light/Lights.h
  4. 5
      aidl/light/android.hardware.light-service.samsung.rc
  5. 6
      aidl/light/android.hardware.light-service.samsung.xml
  6. 0
      aidl/light/include/samsung_lights.h
  7. 27
      aidl/light/service.cpp
  8. 41
      hidl/light/Android.mk
  9. 76
      hidl/light/Light.h
  10. 7
      hidl/light/android.hardware.light@2.0-service.samsung.rc
  11. 54
      hidl/light/service.cpp

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

@ -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 <android-base/stringprintf.h>
#include <iomanip>
#include <fstream>
#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<Status> Light::setLight(Type type, const LightState& state) {
ndk::ScopedAStatus Lights::setLightState(int32_t id, const HwLightState& state) {
LightType type = static_cast<LightType>(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<Status> 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<void> Light::getSupportedTypes(getSupportedTypes_cb _hidl_cb) {
std::vector<Type> types;
#define AutoHwLight(light) {.id = (int32_t)light, .type = light, .ordinal = 0}
ndk::ScopedAStatus Lights::getLights(std::vector<HwLight> *_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

@ -0,0 +1,54 @@
/*
* Copyright (C) 2021 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <aidl/android/hardware/light/BnLights.h>
#include <unordered_map>
#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<HwLight> *_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<LightType, std::function<void(const HwLightState&)>> mLights;
};
} // namespace light
} // namespace hardware
} // namespace android
} // namespace aidl

@ -0,0 +1,5 @@
service vendor.light-default /vendor/bin/hw/android.hardware.light-service.samsung
class hal
user system
group system
shutdown critical

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

@ -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 <android/binder_manager.h>
#include <android/binder_process.h>
#include <android-base/logging.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 reach
}

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

@ -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 <android/hardware/light/2.0/ILight.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>
#include <fstream>
#include <unordered_map>
#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<Status> setLight(Type type, const LightState& state) override;
Return<void> 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<Type, std::function<void(const LightState&)>> mLights;
};
} // namespace implementation
} // namespace V2_0
} // namespace light
} // namespace hardware
} // namespace android
#endif // ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H

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

@ -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 <android-base/logging.h>
#include <hidl/HidlTransportSupport.h>
#include <utils/Errors.h>
#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<ILight> 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;
}
Loading…
Cancel
Save