From b3c318ead7d2deffdd569cbb69458b9bbdb7580c Mon Sep 17 00:00:00 2001 From: Tim Zimmermann Date: Sun, 3 Apr 2022 03:09:42 +0900 Subject: [PATCH] universal7885: aidl: vibrator: properly implement effects --- .../samsung/aidl/vibrator/Vibrator.cpp | 111 ++++++++++-------- .../hardware/samsung/aidl/vibrator/Vibrator.h | 39 +++--- ...droid.hardware.vibrator-service.samsung.rc | 5 + .../samsung/aidl/vibrator/service.cpp | 7 +- 4 files changed, 87 insertions(+), 75 deletions(-) diff --git a/universal7885-common/hardware/samsung/aidl/vibrator/Vibrator.cpp b/universal7885-common/hardware/samsung/aidl/vibrator/Vibrator.cpp index cf93d70..95d5aec 100644 --- a/universal7885-common/hardware/samsung/aidl/vibrator/Vibrator.cpp +++ b/universal7885-common/hardware/samsung/aidl/vibrator/Vibrator.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include namespace aidl { @@ -18,6 +19,14 @@ namespace android { namespace hardware { namespace vibrator { +static std::map CP_TRIGGER_EFFECTS { + { Effect::CLICK, 10 }, + { Effect::DOUBLE_CLICK, 14 }, + { Effect::HEAVY_CLICK, 23 }, + { Effect::TEXTURE_TICK, 50 }, + { Effect::TICK, 50 } +}; + /* * Write value to path and close file. */ @@ -48,13 +57,13 @@ static bool nodeExists(const std::string& path) { Vibrator::Vibrator() { mIsTimedOutVibrator = nodeExists(VIBRATOR_TIMEOUT_PATH); mHasTimedOutIntensity = nodeExists(VIBRATOR_INTENSITY_PATH); + mHasTimedOutEffect = nodeExists(VIBRATOR_CP_TRIGGER_PATH); } ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) { *_aidl_return = IVibrator::CAP_ON_CALLBACK | IVibrator::CAP_PERFORM_CALLBACK | IVibrator::CAP_EXTERNAL_CONTROL /*| IVibrator::CAP_COMPOSE_EFFECTS | - IVibrator::CAP_ALWAYS_ON_CONTROL*/ - ; + IVibrator::CAP_ALWAYS_ON_CONTROL*/; if (mHasTimedOutIntensity) { *_aidl_return = *_aidl_return | IVibrator::CAP_AMPLITUDE_CONTROL | @@ -68,15 +77,19 @@ ndk::ScopedAStatus Vibrator::off() { return activate(0); } -ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, - const std::shared_ptr& callback) { - ndk::ScopedAStatus status = activate(timeoutMs); +ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, const std::shared_ptr& callback) { + ndk::ScopedAStatus status; + + if (mHasTimedOutEffect) + writeNode(VIBRATOR_CP_TRIGGER_PATH, 0); // Clear all effects + + status = activate(timeoutMs); if (callback != nullptr) { std::thread([=] { - LOG(INFO) << "Starting on on another thread"; + LOG(DEBUG) << "Starting on on another thread"; usleep(timeoutMs * 1000); - LOG(INFO) << "Notifying on complete"; + LOG(DEBUG) << "Notifying on complete"; if (!callback->onComplete().isOk()) { LOG(ERROR) << "Failed to call onComplete"; } @@ -86,30 +99,36 @@ ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, return status; } -ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, - const std::shared_ptr& callback, - int32_t* _aidl_return) { +ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, const std::shared_ptr& callback, int32_t* _aidl_return) { ndk::ScopedAStatus status; - uint8_t amplitude; - uint32_t ms; + uint32_t amplitude = strengthToAmplitude(strength, &status); + uint32_t ms = 1000; - amplitude = strengthToAmplitude(strength, &status); - if (!status.isOk()) { + if (!status.isOk()) return status; - } + + activate(0); setAmplitude(amplitude); - ms = effectToMs(effect, &status); - if (!status.isOk()) { - return status; + if (mHasTimedOutEffect && CP_TRIGGER_EFFECTS.find(effect) != CP_TRIGGER_EFFECTS.end()) { + writeNode(VIBRATOR_CP_TRIGGER_PATH, CP_TRIGGER_EFFECTS[effect]); + } else { + if (mHasTimedOutEffect) + writeNode(VIBRATOR_CP_TRIGGER_PATH, 0); // Clear previous effect + + ms = effectToMs(effect, &status); + + if (!status.isOk()) + return status; } + status = activate(ms); if (callback != nullptr) { std::thread([=] { - LOG(INFO) << "Starting perform on another thread"; + LOG(DEBUG) << "Starting perform on another thread"; usleep(ms * 1000); - LOG(INFO) << "Notifying perform complete"; + LOG(DEBUG) << "Notifying perform complete"; callback->onComplete(); }).detach(); } @@ -119,13 +138,14 @@ ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, } ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector* _aidl_return) { - *_aidl_return = { - Effect::CLICK, Effect::DOUBLE_CLICK, Effect::HEAVY_CLICK, Effect::TICK, - Effect::TEXTURE_TICK, Effect::THUD, Effect::POP, Effect::RINGTONE_1, - Effect::RINGTONE_2, Effect::RINGTONE_3, Effect::RINGTONE_4, Effect::RINGTONE_5, - Effect::RINGTONE_6, Effect::RINGTONE_7, Effect::RINGTONE_7, Effect::RINGTONE_8, - Effect::RINGTONE_9, Effect::RINGTONE_10, Effect::RINGTONE_11, Effect::RINGTONE_12, - Effect::RINGTONE_13, Effect::RINGTONE_14, Effect::RINGTONE_15}; + *_aidl_return = {Effect::CLICK, Effect::DOUBLE_CLICK, Effect::HEAVY_CLICK, + Effect::TICK, Effect::TEXTURE_TICK, Effect::THUD, Effect::POP, + Effect::RINGTONE_1, Effect::RINGTONE_2, Effect::RINGTONE_3, + Effect::RINGTONE_4, Effect::RINGTONE_5, Effect::RINGTONE_6, + Effect::RINGTONE_7, Effect::RINGTONE_7, Effect::RINGTONE_8, + Effect::RINGTONE_9, Effect::RINGTONE_10, Effect::RINGTONE_11, + Effect::RINGTONE_12, Effect::RINGTONE_13, Effect::RINGTONE_14, + Effect::RINGTONE_15}; return ndk::ScopedAStatus::ok(); } @@ -171,18 +191,15 @@ ndk::ScopedAStatus Vibrator::getCompositionSizeMax(int32_t* /*_aidl_return*/) { return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } -ndk::ScopedAStatus Vibrator::getSupportedPrimitives( - std::vector* /*_aidl_return*/) { +ndk::ScopedAStatus Vibrator::getSupportedPrimitives(std::vector* /*_aidl_return*/) { return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } -ndk::ScopedAStatus Vibrator::getPrimitiveDuration(CompositePrimitive /*primitive*/, - int32_t* /*_aidl_return*/) { +ndk::ScopedAStatus Vibrator::getPrimitiveDuration(CompositePrimitive /*primitive*/, int32_t* /*_aidl_return*/) { return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } -ndk::ScopedAStatus Vibrator::compose(const std::vector& /*composite*/, - const std::shared_ptr& /*callback*/) { +ndk::ScopedAStatus Vibrator::compose(const std::vector& /*composite*/, const std::shared_ptr& /*callback*/) { return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } @@ -190,8 +207,7 @@ ndk::ScopedAStatus Vibrator::getSupportedAlwaysOnEffects(std::vector* /* return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } -ndk::ScopedAStatus Vibrator::alwaysOnEnable(int32_t /*id*/, Effect /*effect*/, - EffectStrength /*strength*/) { +ndk::ScopedAStatus Vibrator::alwaysOnEnable(int32_t /*id*/, Effect /*effect*/, EffectStrength /*strength*/) { return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } @@ -231,8 +247,7 @@ ndk::ScopedAStatus Vibrator::getSupportedBraking(std::vector* /*_aidl_r return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } -ndk::ScopedAStatus Vibrator::composePwle(const std::vector& /*composite*/, - const std::shared_ptr& /*callback*/) { +ndk::ScopedAStatus Vibrator::composePwle(const std::vector& /*composite*/, const std::shared_ptr& /*callback*/) { return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } @@ -263,19 +278,18 @@ uint8_t Vibrator::strengthToAmplitude(EffectStrength strength, ndk::ScopedAStatu uint32_t Vibrator::effectToMs(Effect effect, ndk::ScopedAStatus* status) { *status = ndk::ScopedAStatus::ok(); - switch (effect) { case Effect::CLICK: - return 20; + return 10; case Effect::DOUBLE_CLICK: - return 25; - case Effect::HEAVY_CLICK: - return 30; + return 15; case Effect::TICK: case Effect::TEXTURE_TICK: case Effect::THUD: case Effect::POP: - return 15; + return 5; + case Effect::HEAVY_CLICK: + return 10; case Effect::RINGTONE_1: case Effect::RINGTONE_2: case Effect::RINGTONE_3: @@ -291,14 +305,13 @@ uint32_t Vibrator::effectToMs(Effect effect, ndk::ScopedAStatus* status) { case Effect::RINGTONE_13: case Effect::RINGTONE_14: case Effect::RINGTONE_15: - return 300; + return 30000; } - *status = ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); return 0; } -} // namespace vibrator -} // namespace hardware -} // namespace android -} // namespace aidl +} // namespace vibrator +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/universal7885-common/hardware/samsung/aidl/vibrator/Vibrator.h b/universal7885-common/hardware/samsung/aidl/vibrator/Vibrator.h index d7950bb..785ad9d 100644 --- a/universal7885-common/hardware/samsung/aidl/vibrator/Vibrator.h +++ b/universal7885-common/hardware/samsung/aidl/vibrator/Vibrator.h @@ -14,13 +14,14 @@ #define VIBRATOR_TIMEOUT_PATH "/sys/class/timed_output/vibrator/enable" #define VIBRATOR_INTENSITY_PATH "/sys/class/timed_output/vibrator/intensity" +#define VIBRATOR_CP_TRIGGER_PATH "/sys/class/timed_output/vibrator/cp_trigger_index" +using ::aidl::android::hardware::vibrator::IVibratorCallback; using ::aidl::android::hardware::vibrator::Braking; -using ::aidl::android::hardware::vibrator::CompositeEffect; -using ::aidl::android::hardware::vibrator::CompositePrimitive; using ::aidl::android::hardware::vibrator::Effect; using ::aidl::android::hardware::vibrator::EffectStrength; -using ::aidl::android::hardware::vibrator::IVibratorCallback; +using ::aidl::android::hardware::vibrator::CompositeEffect; +using ::aidl::android::hardware::vibrator::CompositePrimitive; using ::aidl::android::hardware::vibrator::PrimitivePwle; namespace aidl { @@ -29,26 +30,20 @@ namespace hardware { namespace vibrator { class Vibrator : public BnVibrator { - public: +public: Vibrator(); ndk::ScopedAStatus getCapabilities(int32_t* _aidl_return) override; ndk::ScopedAStatus off() override; - ndk::ScopedAStatus on(int32_t timeoutMs, - const std::shared_ptr& callback) override; - ndk::ScopedAStatus perform(Effect effect, EffectStrength strength, - const std::shared_ptr& callback, - int32_t* _aidl_return) override; + ndk::ScopedAStatus on(int32_t timeoutMs, const std::shared_ptr& callback) override; + ndk::ScopedAStatus perform(Effect effect, EffectStrength strength, const std::shared_ptr& callback, int32_t* _aidl_return) override; ndk::ScopedAStatus getSupportedEffects(std::vector* _aidl_return) override; ndk::ScopedAStatus setAmplitude(float amplitude) override; ndk::ScopedAStatus setExternalControl(bool enabled) override; ndk::ScopedAStatus getCompositionDelayMax(int32_t* _aidl_return) override; ndk::ScopedAStatus getCompositionSizeMax(int32_t* _aidl_return) override; - ndk::ScopedAStatus getSupportedPrimitives( - std::vector* _aidl_return) override; - ndk::ScopedAStatus getPrimitiveDuration(CompositePrimitive primitive, - int32_t* _aidl_return) override; - ndk::ScopedAStatus compose(const std::vector& composite, - const std::shared_ptr& callback) override; + ndk::ScopedAStatus getSupportedPrimitives(std::vector* _aidl_return) override; + ndk::ScopedAStatus getPrimitiveDuration(CompositePrimitive primitive, int32_t* _aidl_return) override; + ndk::ScopedAStatus compose(const std::vector& composite, const std::shared_ptr& callback) override; ndk::ScopedAStatus getSupportedAlwaysOnEffects(std::vector* _aidl_return) override; ndk::ScopedAStatus alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) override; ndk::ScopedAStatus alwaysOnDisable(int32_t id) override; @@ -60,10 +55,9 @@ class Vibrator : public BnVibrator { ndk::ScopedAStatus getPwlePrimitiveDurationMax(int32_t* _aidl_return) override; ndk::ScopedAStatus getPwleCompositionSizeMax(int32_t* _aidl_return) override; ndk::ScopedAStatus getSupportedBraking(std::vector* _aidl_return) override; - ndk::ScopedAStatus composePwle(const std::vector& composite, - const std::shared_ptr& callback) override; + ndk::ScopedAStatus composePwle(const std::vector& composite, const std::shared_ptr& callback) override; - private: +private: ndk::ScopedAStatus activate(uint32_t ms); static uint32_t effectToMs(Effect effect, ndk::ScopedAStatus* status); static uint8_t strengthToAmplitude(EffectStrength strength, ndk::ScopedAStatus* status); @@ -74,9 +68,10 @@ class Vibrator : public BnVibrator { bool mIsTimedOutVibrator; bool mHasTimedOutIntensity; + bool mHasTimedOutEffect; }; -} // namespace vibrator -} // namespace hardware -} // namespace android -} // namespace aidl +} // namespace vibrator +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/universal7885-common/hardware/samsung/aidl/vibrator/android.hardware.vibrator-service.samsung.rc b/universal7885-common/hardware/samsung/aidl/vibrator/android.hardware.vibrator-service.samsung.rc index 3f43905..f211456 100644 --- a/universal7885-common/hardware/samsung/aidl/vibrator/android.hardware.vibrator-service.samsung.rc +++ b/universal7885-common/hardware/samsung/aidl/vibrator/android.hardware.vibrator-service.samsung.rc @@ -1,3 +1,8 @@ +on init + chown system system /sys/class/timed_output/vibrator/cp_trigger_index + chown system system /sys/class/timed_output/vibrator/enable + chown system system /sys/class/timed_output/vibrator/intensity + service vendor.vibrator-default /vendor/bin/hw/android.hardware.vibrator-service.samsung class hal user system diff --git a/universal7885-common/hardware/samsung/aidl/vibrator/service.cpp b/universal7885-common/hardware/samsung/aidl/vibrator/service.cpp index cff4daf..db782c8 100644 --- a/universal7885-common/hardware/samsung/aidl/vibrator/service.cpp +++ b/universal7885-common/hardware/samsung/aidl/vibrator/service.cpp @@ -6,9 +6,9 @@ #include "Vibrator.h" -#include #include #include +#include using ::aidl::android::hardware::vibrator::Vibrator; @@ -17,10 +17,9 @@ int main() { std::shared_ptr vibrator = ndk::SharedRefBase::make(); const std::string instance = std::string() + Vibrator::descriptor + "/default"; - binder_status_t status = - AServiceManager_addService(vibrator->asBinder().get(), instance.c_str()); + binder_status_t status = AServiceManager_addService(vibrator->asBinder().get(), instance.c_str()); CHECK(status == STATUS_OK); ABinderProcess_joinThreadPool(); - return EXIT_FAILURE; // should not reach + return EXIT_FAILURE; // should not reach }