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

Commit 648e8e6f authored by Ahmad Khalil's avatar Ahmad Khalil
Browse files

Integrate the new frequency profile APIs with the vibrator HAL.

Bug: 347034419
Flag: android.os.vibrator.normalized_pwle_effects
Test: libvibratorservice_test
Change-Id: I59b280e4b22863daa62d5e32c6b1c566845fc786
parent afcfe0fe
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -107,6 +107,10 @@ Info HalWrapper::getInfo() {
        mInfoCache.mMaxEnvelopeEffectControlPointDuration =
                getMaxEnvelopeEffectControlPointDurationInternal();
    }
    if (mInfoCache.mFrequencyToOutputAccelerationMap.isFailed()) {
        mInfoCache.mFrequencyToOutputAccelerationMap =
                getFrequencyToOutputAccelerationMapInternal();
    }
    return mInfoCache.get();
}

@@ -239,6 +243,13 @@ HalResult<milliseconds> HalWrapper::getMaxEnvelopeEffectControlPointDurationInte
    return HalResult<milliseconds>::unsupported();
}

HalResult<std::vector<PwleV2OutputMapEntry>>
HalWrapper::getFrequencyToOutputAccelerationMapInternal() {
    ALOGV("Skipped getFrequencyToOutputAccelerationMapInternal because it's not "
          "available in Vibrator HAL");
    return HalResult<std::vector<PwleV2OutputMapEntry>>::unsupported();
}

// -------------------------------------------------------------------------------------------------

HalResult<void> AidlHalWrapper::ping() {
@@ -487,6 +498,15 @@ HalResult<milliseconds> AidlHalWrapper::getMaxEnvelopeEffectControlPointDuration
    return HalResultFactory::fromStatus<milliseconds>(std::move(status), milliseconds(durationMs));
}

HalResult<std::vector<PwleV2OutputMapEntry>>
AidlHalWrapper::getFrequencyToOutputAccelerationMapInternal() {
    std::vector<PwleV2OutputMapEntry> frequencyToOutputAccelerationMap;
    auto status =
            getHal()->getPwleV2FrequencyToOutputAccelerationMap(&frequencyToOutputAccelerationMap);
    return HalResultFactory::fromStatus<
            std::vector<PwleV2OutputMapEntry>>(std::move(status), frequencyToOutputAccelerationMap);
}

std::shared_ptr<Aidl::IVibrator> AidlHalWrapper::getHal() {
    std::lock_guard<std::mutex> lock(mHandleMutex);
    return mHandle;
+14 −4
Original line number Diff line number Diff line
@@ -243,6 +243,7 @@ public:
    using EffectStrength = aidl::android::hardware::vibrator::EffectStrength;
    using CompositePrimitive = aidl::android::hardware::vibrator::CompositePrimitive;
    using Braking = aidl::android::hardware::vibrator::Braking;
    using PwleV2OutputMapEntry = aidl::android::hardware::vibrator::PwleV2OutputMapEntry;

    const HalResult<Capabilities> capabilities;
    const HalResult<std::vector<Effect>> supportedEffects;
@@ -261,6 +262,7 @@ public:
    const HalResult<int32_t> maxEnvelopeEffectSize;
    const HalResult<std::chrono::milliseconds> minEnvelopeEffectControlPointDuration;
    const HalResult<std::chrono::milliseconds> maxEnvelopeEffectControlPointDuration;
    const HalResult<std::vector<PwleV2OutputMapEntry>> frequencyToOutputAccelerationMap;

    void logFailures() const {
        logFailure<Capabilities>(capabilities, "getCapabilities");
@@ -284,6 +286,8 @@ public:
                                              "getMinEnvelopeEffectControlPointDuration");
        logFailure<std::chrono::milliseconds>(maxEnvelopeEffectControlPointDuration,
                                              "getMaxEnvelopeEffectControlPointDuration");
        logFailure<std::vector<PwleV2OutputMapEntry>>(frequencyToOutputAccelerationMap,
                                                      "getfrequencyToOutputAccelerationMap");
    }

    bool shouldRetry() const {
@@ -296,7 +300,8 @@ public:
                qFactor.shouldRetry() || maxAmplitudes.shouldRetry() ||
                maxEnvelopeEffectSize.shouldRetry() ||
                minEnvelopeEffectControlPointDuration.shouldRetry() ||
                maxEnvelopeEffectControlPointDuration.shouldRetry();
                maxEnvelopeEffectControlPointDuration.shouldRetry() ||
                frequencyToOutputAccelerationMap.shouldRetry();
    }

private:
@@ -327,7 +332,8 @@ public:
                mMaxAmplitudes,
                mMaxEnvelopeEffectSize,
                mMinEnvelopeEffectControlPointDuration,
                mMaxEnvelopeEffectControlPointDuration};
                mMaxEnvelopeEffectControlPointDuration,
                mFrequencyToOutputAccelerationMap};
    }

private:
@@ -359,6 +365,8 @@ private:
            HalResult<std::chrono::milliseconds>::transactionFailed(MSG);
    HalResult<std::chrono::milliseconds> mMaxEnvelopeEffectControlPointDuration =
            HalResult<std::chrono::milliseconds>::transactionFailed(MSG);
    HalResult<std::vector<Info::PwleV2OutputMapEntry>> mFrequencyToOutputAccelerationMap =
            HalResult<std::vector<Info::PwleV2OutputMapEntry>>::transactionFailed(MSG);

    friend class HalWrapper;
};
@@ -442,6 +450,8 @@ protected:
    virtual HalResult<int32_t> getMaxEnvelopeEffectSizeInternal();
    virtual HalResult<std::chrono::milliseconds> getMinEnvelopeEffectControlPointDurationInternal();
    virtual HalResult<std::chrono::milliseconds> getMaxEnvelopeEffectControlPointDurationInternal();
    virtual HalResult<std::vector<PwleV2OutputMapEntry>>
    getFrequencyToOutputAccelerationMapInternal();

private:
    std::mutex mInfoMutex;
@@ -518,12 +528,12 @@ protected:
    HalResult<float> getQFactorInternal() override final;
    HalResult<std::vector<float>> getMaxAmplitudesInternal() override final;
    HalResult<int32_t> getMaxEnvelopeEffectSizeInternal() override final;

    HalResult<std::chrono::milliseconds> getMinEnvelopeEffectControlPointDurationInternal()
            override final;

    HalResult<std::chrono::milliseconds> getMaxEnvelopeEffectControlPointDurationInternal()
            override final;
    HalResult<std::vector<PwleV2OutputMapEntry>> getFrequencyToOutputAccelerationMapInternal()
            override final;

private:
    const reconnect_fn mReconnectFn;
+24 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ using aidl::android::hardware::vibrator::EffectStrength;
using aidl::android::hardware::vibrator::IVibrator;
using aidl::android::hardware::vibrator::IVibratorCallback;
using aidl::android::hardware::vibrator::PrimitivePwle;
using aidl::android::hardware::vibrator::PwleV2OutputMapEntry;
using aidl::android::hardware::vibrator::PwleV2Primitive;
using aidl::android::hardware::vibrator::VendorEffect;
using aidl::android::os::PersistableBundle;
@@ -242,6 +243,11 @@ TEST_F(VibratorHalWrapperAidlTest, TestGetInfoDoesNotCacheFailedResult) {
    std::vector<CompositePrimitive> supportedPrimitives = {CompositePrimitive::CLICK};
    std::vector<Braking> supportedBraking = {Braking::CLAB};
    std::vector<float> amplitudes = {0.f, 1.f, 0.f};
    std::vector<PwleV2OutputMapEntry>
            frequencyToOutputAccelerationMap{PwleV2OutputMapEntry(/*frequency=*/30.0f,
                                                                  /*maxOutputAcceleration=*/0.2),
                                             PwleV2OutputMapEntry(/*frequency=*/60.0f,
                                                                  /*maxOutputAcceleration=*/0.8)};

    std::vector<std::chrono::milliseconds> primitiveDurations;
    constexpr auto primitiveRange = ndk::enum_range<CompositePrimitive>();
@@ -323,6 +329,11 @@ TEST_F(VibratorHalWrapperAidlTest, TestGetInfoDoesNotCacheFailedResult) {
            .WillOnce(Return(ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY)))
            .WillOnce(DoAll(SetArgPointee<0>(PWLE_V2_MIN_REQUIRED_PRIMITIVE_MAX_DURATION_MS),
                            Return(ndk::ScopedAStatus::ok())));
    EXPECT_CALL(*mMockHal.get(), getPwleV2FrequencyToOutputAccelerationMap(_))
            .Times(Exactly(2))
            .WillOnce(Return(ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY)))
            .WillOnce(DoAll(SetArgPointee<0>(frequencyToOutputAccelerationMap),
                            Return(ndk::ScopedAStatus::ok())));

    vibrator::Info failed = mWrapper->getInfo();
    ASSERT_TRUE(failed.capabilities.isFailed());
@@ -342,6 +353,7 @@ TEST_F(VibratorHalWrapperAidlTest, TestGetInfoDoesNotCacheFailedResult) {
    ASSERT_TRUE(failed.maxEnvelopeEffectSize.isFailed());
    ASSERT_TRUE(failed.minEnvelopeEffectControlPointDuration.isFailed());
    ASSERT_TRUE(failed.maxEnvelopeEffectControlPointDuration.isFailed());
    ASSERT_TRUE(failed.frequencyToOutputAccelerationMap.isFailed());

    vibrator::Info successful = mWrapper->getInfo();
    ASSERT_EQ(vibrator::Capabilities::ON_CALLBACK, successful.capabilities.value());
@@ -364,6 +376,8 @@ TEST_F(VibratorHalWrapperAidlTest, TestGetInfoDoesNotCacheFailedResult) {
              successful.minEnvelopeEffectControlPointDuration.value());
    ASSERT_EQ(std::chrono::milliseconds(PWLE_V2_MIN_REQUIRED_PRIMITIVE_MAX_DURATION_MS),
              successful.maxEnvelopeEffectControlPointDuration.value());
    ASSERT_EQ(frequencyToOutputAccelerationMap,
              successful.frequencyToOutputAccelerationMap.value());
}

TEST_F(VibratorHalWrapperAidlTest, TestGetInfoCachesResult) {
@@ -377,6 +391,11 @@ TEST_F(VibratorHalWrapperAidlTest, TestGetInfoCachesResult) {
    constexpr int32_t PWLE_V2_MAX_ALLOWED_PRIMITIVE_MIN_DURATION_MS = 20;
    constexpr int32_t PWLE_V2_MIN_REQUIRED_PRIMITIVE_MAX_DURATION_MS = 1000;
    std::vector<Effect> supportedEffects = {Effect::CLICK, Effect::TICK};
    std::vector<PwleV2OutputMapEntry>
            frequencyToOutputAccelerationMap{PwleV2OutputMapEntry(/*frequency=*/30.0f,
                                                                  /*maxOutputAcceleration=*/0.2),
                                             PwleV2OutputMapEntry(/*frequency=*/60.0f,
                                                                  /*maxOutputAcceleration=*/0.8)};

    EXPECT_CALL(*mMockHal.get(), getCapabilities(_))
            .Times(Exactly(1))
@@ -432,6 +451,10 @@ TEST_F(VibratorHalWrapperAidlTest, TestGetInfoCachesResult) {
            .Times(Exactly(1))
            .WillOnce(DoAll(SetArgPointee<0>(PWLE_V2_MIN_REQUIRED_PRIMITIVE_MAX_DURATION_MS),
                            Return(ndk::ScopedAStatus::ok())));
    EXPECT_CALL(*mMockHal.get(), getPwleV2FrequencyToOutputAccelerationMap(_))
            .Times(Exactly(1))
            .WillOnce(DoAll(SetArgPointee<0>(frequencyToOutputAccelerationMap),
                            Return(ndk::ScopedAStatus::ok())));

    std::vector<std::thread> threads;
    for (int i = 0; i < 10; i++) {
@@ -460,6 +483,7 @@ TEST_F(VibratorHalWrapperAidlTest, TestGetInfoCachesResult) {
              info.minEnvelopeEffectControlPointDuration.value());
    ASSERT_EQ(std::chrono::milliseconds(PWLE_V2_MIN_REQUIRED_PRIMITIVE_MAX_DURATION_MS),
              info.maxEnvelopeEffectControlPointDuration.value());
    ASSERT_EQ(frequencyToOutputAccelerationMap, info.frequencyToOutputAccelerationMap.value());
}

TEST_F(VibratorHalWrapperAidlTest, TestPerformEffectWithCallbackSupport) {
+2 −0
Original line number Diff line number Diff line
@@ -223,6 +223,7 @@ TEST_F(VibratorHalWrapperHidlV1_0Test, TestGetInfoDoesNotCacheFailedResult) {
    ASSERT_TRUE(info.maxEnvelopeEffectSize.isUnsupported());
    ASSERT_TRUE(info.minEnvelopeEffectControlPointDuration.isUnsupported());
    ASSERT_TRUE(info.maxEnvelopeEffectControlPointDuration.isUnsupported());
    ASSERT_TRUE(info.frequencyToOutputAccelerationMap.isUnsupported());
}

TEST_F(VibratorHalWrapperHidlV1_0Test, TestGetInfoWithoutAmplitudeControl) {
@@ -259,6 +260,7 @@ TEST_F(VibratorHalWrapperHidlV1_0Test, TestGetInfoCachesResult) {
    ASSERT_TRUE(info.maxEnvelopeEffectSize.isUnsupported());
    ASSERT_TRUE(info.minEnvelopeEffectControlPointDuration.isUnsupported());
    ASSERT_TRUE(info.maxEnvelopeEffectControlPointDuration.isUnsupported());
    ASSERT_TRUE(info.frequencyToOutputAccelerationMap.isUnsupported());
}

TEST_F(VibratorHalWrapperHidlV1_0Test, TestPerformEffect) {