Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 07f9c0e2 authored by Lais Andrade's avatar Lais Andrade
Browse files

Add getSupportedPrimitives to vibrator HAL wrapper

Expose the IVibrator.aidl getSupportedPrimitives method to be used by
the VibratorService.

Fix: b/163484172
Test: atest libvibratorservice_test
Change-Id: I6dfa0df6993837c45365a2f2c051cb1462b9d155
parent 0866661c
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#include <vibratorservice/VibratorHalWrapper.h>

using android::hardware::vibrator::CompositeEffect;
using android::hardware::vibrator::CompositePrimitive;
using android::hardware::vibrator::Effect;
using android::hardware::vibrator::EffectStrength;

@@ -201,6 +202,12 @@ HalResult<std::vector<Effect>> HalController::getSupportedEffects() {
    return apply(getSupportedEffectsFn, "getSupportedEffects");
}

HalResult<std::vector<CompositePrimitive>> HalController::getSupportedPrimitives() {
    hal_fn<std::vector<CompositePrimitive>> getSupportedPrimitivesFn =
            [](std::shared_ptr<HalWrapper> hal) { return hal->getSupportedPrimitives(); };
    return apply(getSupportedPrimitivesFn, "getSupportedPrimitives");
}

HalResult<milliseconds> HalController::performEffect(
        Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
    hal_fn<milliseconds> performEffectFn = [&](std::shared_ptr<HalWrapper> hal) {
+20 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
#include <vibratorservice/VibratorHalWrapper.h>

using android::hardware::vibrator::CompositeEffect;
using android::hardware::vibrator::CompositePrimitive;
using android::hardware::vibrator::Effect;
using android::hardware::vibrator::EffectStrength;

@@ -221,6 +222,13 @@ HalResult<std::vector<Effect>> AidlHalWrapper::getSupportedEffects() {
                                           mSupportedEffects);
}

HalResult<std::vector<CompositePrimitive>> AidlHalWrapper::getSupportedPrimitives() {
    std::lock_guard<std::mutex> lock(mSupportedPrimitivesMutex);
    return loadCached<std::vector<
            CompositePrimitive>>(std::bind(&AidlHalWrapper::getSupportedPrimitivesInternal, this),
                                 mSupportedPrimitives);
}

HalResult<milliseconds> AidlHalWrapper::performEffect(
        Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
    HalResult<Capabilities> capabilities = getCapabilities();
@@ -260,6 +268,12 @@ HalResult<std::vector<Effect>> AidlHalWrapper::getSupportedEffectsInternal() {
    return HalResult<std::vector<Effect>>::fromStatus(result, supportedEffects);
}

HalResult<std::vector<CompositePrimitive>> AidlHalWrapper::getSupportedPrimitivesInternal() {
    std::vector<CompositePrimitive> supportedPrimitives;
    auto result = getHal()->getSupportedPrimitives(&supportedPrimitives);
    return HalResult<std::vector<CompositePrimitive>>::fromStatus(result, supportedPrimitives);
}

sp<Aidl::IVibrator> AidlHalWrapper::getHal() {
    std::lock_guard<std::mutex> lock(mHandleMutex);
    return mHandle;
@@ -336,6 +350,12 @@ HalResult<std::vector<Effect>> HidlHalWrapper<I>::getSupportedEffects() {
    return HalResult<std::vector<Effect>>::unsupported();
}

template <typename I>
HalResult<std::vector<CompositePrimitive>> HidlHalWrapper<I>::getSupportedPrimitives() {
    ALOGV("Skipped getSupportedPrimitives because Vibrator HAL AIDL is not available");
    return HalResult<std::vector<CompositePrimitive>>::unsupported();
}

template <typename I>
HalResult<void> HidlHalWrapper<I>::performComposedEffect(const std::vector<CompositeEffect>&,
                                                         const std::function<void()>&) {
+2 −0
Original line number Diff line number Diff line
@@ -69,6 +69,8 @@ public:

    HalResult<Capabilities> getCapabilities() final override;
    HalResult<std::vector<hardware::vibrator::Effect>> getSupportedEffects() final override;
    HalResult<std::vector<hardware::vibrator::CompositePrimitive>> getSupportedPrimitives()
            final override;

    HalResult<std::chrono::milliseconds> performEffect(
            hardware::vibrator::Effect effect, hardware::vibrator::EffectStrength strength,
+10 −0
Original line number Diff line number Diff line
@@ -156,6 +156,8 @@ public:

    virtual HalResult<Capabilities> getCapabilities() = 0;
    virtual HalResult<std::vector<hardware::vibrator::Effect>> getSupportedEffects() = 0;
    virtual HalResult<std::vector<hardware::vibrator::CompositePrimitive>>
    getSupportedPrimitives() = 0;

    virtual HalResult<std::chrono::milliseconds> performEffect(
            hardware::vibrator::Effect effect, hardware::vibrator::EffectStrength strength,
@@ -194,6 +196,8 @@ public:

    HalResult<Capabilities> getCapabilities() override final;
    HalResult<std::vector<hardware::vibrator::Effect>> getSupportedEffects() override final;
    HalResult<std::vector<hardware::vibrator::CompositePrimitive>> getSupportedPrimitives()
            override final;

    HalResult<std::chrono::milliseconds> performEffect(
            hardware::vibrator::Effect effect, hardware::vibrator::EffectStrength strength,
@@ -207,14 +211,18 @@ private:
    std::mutex mHandleMutex;
    std::mutex mCapabilitiesMutex;
    std::mutex mSupportedEffectsMutex;
    std::mutex mSupportedPrimitivesMutex;
    sp<hardware::vibrator::IVibrator> mHandle GUARDED_BY(mHandleMutex);
    std::optional<Capabilities> mCapabilities GUARDED_BY(mCapabilitiesMutex);
    std::optional<std::vector<hardware::vibrator::Effect>> mSupportedEffects
            GUARDED_BY(mSupportedEffectsMutex);
    std::optional<std::vector<hardware::vibrator::CompositePrimitive>> mSupportedPrimitives
            GUARDED_BY(mSupportedPrimitivesMutex);

    // Loads directly from IVibrator handle, skipping caches.
    HalResult<Capabilities> getCapabilitiesInternal();
    HalResult<std::vector<hardware::vibrator::Effect>> getSupportedEffectsInternal();
    HalResult<std::vector<hardware::vibrator::CompositePrimitive>> getSupportedPrimitivesInternal();
    sp<hardware::vibrator::IVibrator> getHal();
};

@@ -242,6 +250,8 @@ public:

    HalResult<Capabilities> getCapabilities() override final;
    HalResult<std::vector<hardware::vibrator::Effect>> getSupportedEffects() override final;
    HalResult<std::vector<hardware::vibrator::CompositePrimitive>> getSupportedPrimitives()
            override final;

    HalResult<void> performComposedEffect(
            const std::vector<hardware::vibrator::CompositeEffect>& primitiveEffects,
+26 −7
Original line number Diff line number Diff line
@@ -65,6 +65,8 @@ public:
    MOCK_METHOD(vibrator::HalResult<void>, alwaysOnDisable, (int32_t id), (override));
    MOCK_METHOD(vibrator::HalResult<vibrator::Capabilities>, getCapabilities, (), (override));
    MOCK_METHOD(vibrator::HalResult<std::vector<Effect>>, getSupportedEffects, (), (override));
    MOCK_METHOD(vibrator::HalResult<std::vector<CompositePrimitive>>, getSupportedPrimitives, (),
                (override));
    MOCK_METHOD(vibrator::HalResult<milliseconds>, performEffect,
                (Effect effect, EffectStrength strength,
                 const std::function<void()>& completionCallback),
@@ -132,6 +134,7 @@ protected:
                            vibrator::HalResult<void> voidResult,
                            vibrator::HalResult<vibrator::Capabilities> capabilitiesResult,
                            vibrator::HalResult<std::vector<Effect>> effectsResult,
                            vibrator::HalResult<std::vector<CompositePrimitive>> primitivesResult,
                            vibrator::HalResult<milliseconds> durationResult) {
        EXPECT_CALL(*mMockHal.get(), ping())
                .Times(Exactly(cardinality))
@@ -161,6 +164,9 @@ protected:
        EXPECT_CALL(*mMockHal.get(), getSupportedEffects())
                .Times(Exactly(cardinality))
                .WillRepeatedly(Return(effectsResult));
        EXPECT_CALL(*mMockHal.get(), getSupportedPrimitives())
                .Times(Exactly(cardinality))
                .WillRepeatedly(Return(primitivesResult));
        EXPECT_CALL(*mMockHal.get(), performEffect(Eq(Effect::CLICK), Eq(EffectStrength::LIGHT), _))
                .Times(Exactly(cardinality))
                .WillRepeatedly(Return(durationResult));
@@ -170,7 +176,7 @@ protected:

        if (cardinality > 1) {
            // One reconnection call after each failure.
            EXPECT_CALL(*mMockHal.get(), tryReconnect()).Times(Exactly(11 * cardinality));
            EXPECT_CALL(*mMockHal.get(), tryReconnect()).Times(Exactly(12 * cardinality));
        }
    }
};
@@ -187,9 +193,12 @@ TEST_F(VibratorHalControllerTest, TestInit) {
}

TEST_F(VibratorHalControllerTest, TestApiCallsAreForwardedToHal) {
    std::vector<Effect> supportedEffects;
    supportedEffects.push_back(Effect::CLICK);
    supportedEffects.push_back(Effect::TICK);
    std::vector<Effect> effects;
    effects.push_back(Effect::CLICK);
    effects.push_back(Effect::TICK);
    std::vector<CompositePrimitive> primitives;
    primitives.push_back(CompositePrimitive::CLICK);
    primitives.push_back(CompositePrimitive::THUD);
    std::vector<CompositeEffect> compositeEffects;
    compositeEffects.push_back(
            vibrator::TestFactory::createCompositeEffect(CompositePrimitive::SPIN, 100ms, 0.5f));
@@ -199,7 +208,8 @@ TEST_F(VibratorHalControllerTest, TestApiCallsAreForwardedToHal) {
    setHalExpectations(/* cardinality= */ 1, compositeEffects, vibrator::HalResult<void>::ok(),
                       vibrator::HalResult<vibrator::Capabilities>::ok(
                               vibrator::Capabilities::ON_CALLBACK),
                       vibrator::HalResult<std::vector<Effect>>::ok(supportedEffects),
                       vibrator::HalResult<std::vector<Effect>>::ok(effects),
                       vibrator::HalResult<std::vector<CompositePrimitive>>::ok(primitives),
                       vibrator::HalResult<milliseconds>::ok(100ms));

    ASSERT_TRUE(mController->ping().isOk());
@@ -216,7 +226,11 @@ TEST_F(VibratorHalControllerTest, TestApiCallsAreForwardedToHal) {

    auto getSupportedEffectsResult = mController->getSupportedEffects();
    ASSERT_TRUE(getSupportedEffectsResult.isOk());
    ASSERT_EQ(supportedEffects, getSupportedEffectsResult.value());
    ASSERT_EQ(effects, getSupportedEffectsResult.value());

    auto getSupportedPrimitivesResult = mController->getSupportedPrimitives();
    ASSERT_TRUE(getSupportedPrimitivesResult.isOk());
    ASSERT_EQ(primitives, getSupportedPrimitivesResult.value());

    auto performEffectResult =
            mController->performEffect(Effect::CLICK, EffectStrength::LIGHT, []() {});
@@ -233,6 +247,7 @@ TEST_F(VibratorHalControllerTest, TestUnsupportedApiResultDoNotResetHalConnectio
                       vibrator::HalResult<void>::unsupported(),
                       vibrator::HalResult<vibrator::Capabilities>::unsupported(),
                       vibrator::HalResult<std::vector<Effect>>::unsupported(),
                       vibrator::HalResult<std::vector<CompositePrimitive>>::unsupported(),
                       vibrator::HalResult<milliseconds>::unsupported());

    ASSERT_EQ(0, mConnectCounter);
@@ -247,6 +262,7 @@ TEST_F(VibratorHalControllerTest, TestUnsupportedApiResultDoNotResetHalConnectio
    ASSERT_TRUE(mController->alwaysOnDisable(1).isUnsupported());
    ASSERT_TRUE(mController->getCapabilities().isUnsupported());
    ASSERT_TRUE(mController->getSupportedEffects().isUnsupported());
    ASSERT_TRUE(mController->getSupportedPrimitives().isUnsupported());
    ASSERT_TRUE(mController->performEffect(Effect::CLICK, EffectStrength::LIGHT, []() {})
                        .isUnsupported());
    ASSERT_TRUE(mController->performComposedEffect(std::vector<CompositeEffect>(), []() {})
@@ -260,6 +276,7 @@ TEST_F(VibratorHalControllerTest, TestFailedApiResultResetsHalConnection) {
                       vibrator::HalResult<void>::failed("message"),
                       vibrator::HalResult<vibrator::Capabilities>::failed("message"),
                       vibrator::HalResult<std::vector<Effect>>::failed("message"),
                       vibrator::HalResult<std::vector<CompositePrimitive>>::failed("message"),
                       vibrator::HalResult<milliseconds>::failed("message"));

    ASSERT_EQ(0, mConnectCounter);
@@ -273,6 +290,7 @@ TEST_F(VibratorHalControllerTest, TestFailedApiResultResetsHalConnection) {
    ASSERT_TRUE(mController->alwaysOnDisable(1).isFailed());
    ASSERT_TRUE(mController->getCapabilities().isFailed());
    ASSERT_TRUE(mController->getSupportedEffects().isFailed());
    ASSERT_TRUE(mController->getSupportedPrimitives().isFailed());
    ASSERT_TRUE(
            mController->performEffect(Effect::CLICK, EffectStrength::LIGHT, []() {}).isFailed());
    ASSERT_TRUE(
@@ -331,13 +349,14 @@ TEST_F(VibratorHalControllerTest, TestNoVibratorReturnsUnsupportedAndAttemptsToR
    ASSERT_TRUE(mController->alwaysOnDisable(1).isUnsupported());
    ASSERT_TRUE(mController->getCapabilities().isUnsupported());
    ASSERT_TRUE(mController->getSupportedEffects().isUnsupported());
    ASSERT_TRUE(mController->getSupportedPrimitives().isUnsupported());
    ASSERT_TRUE(mController->performEffect(Effect::CLICK, EffectStrength::LIGHT, []() {})
                        .isUnsupported());
    ASSERT_TRUE(mController->performComposedEffect(std::vector<CompositeEffect>(), []() {})
                        .isUnsupported());

    // One connection attempt per api call.
    ASSERT_EQ(11, mConnectCounter);
    ASSERT_EQ(12, mConnectCounter);
}

TEST_F(VibratorHalControllerTest, TestScheduledCallbackSurvivesReconnection) {
Loading