Loading services/vibratorservice/VibratorHalWrapper.cpp +15 −0 Original line number Diff line number Diff line Loading @@ -32,6 +32,8 @@ using aidl::android::hardware::vibrator::CompositePrimitive; using aidl::android::hardware::vibrator::Effect; using aidl::android::hardware::vibrator::EffectStrength; 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 std::chrono::milliseconds; Loading Loading @@ -114,6 +116,12 @@ HalResult<void> HalWrapper::performPwleEffect(const std::vector<PrimitivePwle>&, return HalResult<void>::unsupported(); } HalResult<void> HalWrapper::composePwleV2(const std::vector<PwleV2Primitive>&, const std::function<void()>&) { ALOGV("Skipped composePwleV2 because it's not available in Vibrator HAL"); return HalResult<void>::unsupported(); } HalResult<Capabilities> HalWrapper::getCapabilities() { std::lock_guard<std::mutex> lock(mInfoMutex); if (mInfoCache.mCapabilities.isFailed()) { Loading Loading @@ -313,6 +321,13 @@ HalResult<void> AidlHalWrapper::performPwleEffect(const std::vector<PrimitivePwl return HalResultFactory::fromStatus(getHal()->composePwle(primitives, cb)); } HalResult<void> AidlHalWrapper::composePwleV2(const std::vector<PwleV2Primitive>& composite, const std::function<void()>& completionCallback) { // This method should always support callbacks, so no need to double check. auto cb = ndk::SharedRefBase::make<HalCallbackWrapper>(completionCallback); return HalResultFactory::fromStatus(getHal()->composePwleV2(composite, cb)); } HalResult<Capabilities> AidlHalWrapper::getCapabilitiesInternal() { int32_t cap = 0; auto status = getHal()->getCapabilities(&cap); Loading services/vibratorservice/include/vibratorservice/VibratorHalWrapper.h +8 −0 Original line number Diff line number Diff line Loading @@ -354,6 +354,8 @@ public: using CompositeEffect = aidl::android::hardware::vibrator::CompositeEffect; using Braking = aidl::android::hardware::vibrator::Braking; using PrimitivePwle = aidl::android::hardware::vibrator::PrimitivePwle; using PwleV2Primitive = aidl::android::hardware::vibrator::PwleV2Primitive; using PwleV2OutputMapEntry = aidl::android::hardware::vibrator::PwleV2OutputMapEntry; explicit HalWrapper(std::shared_ptr<CallbackScheduler> scheduler) : mCallbackScheduler(std::move(scheduler)) {} Loading Loading @@ -391,6 +393,9 @@ public: virtual HalResult<void> performPwleEffect(const std::vector<PrimitivePwle>& primitives, const std::function<void()>& completionCallback); virtual HalResult<void> composePwleV2(const std::vector<PwleV2Primitive>& composite, const std::function<void()>& completionCallback); protected: // Shared pointer to allow CallbackScheduler to outlive this wrapper. const std::shared_ptr<CallbackScheduler> mCallbackScheduler; Loading Loading @@ -471,6 +476,9 @@ public: const std::vector<PrimitivePwle>& primitives, const std::function<void()>& completionCallback) override final; HalResult<void> composePwleV2(const std::vector<PwleV2Primitive>& composite, const std::function<void()>& completionCallback) override final; protected: HalResult<Capabilities> getCapabilitiesInternal() override final; HalResult<std::vector<Effect>> getSupportedEffectsInternal() override final; Loading services/vibratorservice/test/VibratorHalWrapperAidlTest.cpp +36 −0 Original line number Diff line number Diff line Loading @@ -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::PwleV2Primitive; using aidl::android::hardware::vibrator::VendorEffect; using aidl::android::os::PersistableBundle; Loading Loading @@ -681,3 +682,38 @@ TEST_F(VibratorHalWrapperAidlTest, TestPerformPwleEffect) { ASSERT_TRUE(result.isOk()); ASSERT_EQ(1, *callbackCounter.get()); } TEST_F(VibratorHalWrapperAidlTest, TestComposePwleV2) { auto pwleEffect = { PwleV2Primitive(/*amplitude=*/0.2, /*frequency=*/50, /*time=*/100), PwleV2Primitive(/*amplitude=*/0.5, /*frequency=*/150, /*time=*/100), PwleV2Primitive(/*amplitude=*/0.8, /*frequency=*/250, /*time=*/100), }; { InSequence seq; EXPECT_CALL(*mMockHal.get(), composePwleV2(_, _)) .Times(Exactly(3)) .WillOnce(Return(ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION))) .WillOnce(Return(ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY))) .WillOnce(DoAll(WithArg<1>(vibrator::TriggerCallback()), Return(ndk::ScopedAStatus::ok()))); } std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>(); auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get()); auto result = mWrapper->composePwleV2(pwleEffect, callback); ASSERT_TRUE(result.isUnsupported()); // Callback not triggered on failure ASSERT_EQ(0, *callbackCounter.get()); result = mWrapper->composePwleV2(pwleEffect, callback); ASSERT_TRUE(result.isFailed()); // Callback not triggered for unsupported ASSERT_EQ(0, *callbackCounter.get()); result = mWrapper->composePwleV2(pwleEffect, callback); ASSERT_TRUE(result.isOk()); ASSERT_EQ(1, *callbackCounter.get()); } services/vibratorservice/test/VibratorHalWrapperHidlV1_0Test.cpp +17 −0 Original line number Diff line number Diff line Loading @@ -40,6 +40,7 @@ using aidl::android::hardware::vibrator::Effect; using aidl::android::hardware::vibrator::EffectStrength; using aidl::android::hardware::vibrator::IVibrator; using aidl::android::hardware::vibrator::PrimitivePwle; using aidl::android::hardware::vibrator::PwleV2Primitive; using aidl::android::hardware::vibrator::VendorEffect; using aidl::android::os::PersistableBundle; Loading Loading @@ -369,3 +370,19 @@ TEST_F(VibratorHalWrapperHidlV1_0Test, TestPerformPwleEffectUnsupported) { // No callback is triggered. ASSERT_EQ(0, *callbackCounter.get()); } TEST_F(VibratorHalWrapperHidlV1_0Test, TestComposePwleV2Unsupported) { auto pwleEffect = { PwleV2Primitive(/*amplitude=*/0.2, /*frequency=*/50, /*time=*/100), PwleV2Primitive(/*amplitude=*/0.5, /*frequency=*/150, /*time=*/100), PwleV2Primitive(/*amplitude=*/0.8, /*frequency=*/250, /*time=*/100), }; std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>(); auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get()); ASSERT_TRUE(mWrapper->composePwleV2(pwleEffect, callback).isUnsupported()); // No callback is triggered. ASSERT_EQ(0, *callbackCounter.get()); } services/vibratorservice/test/test_mocks.h +13 −0 Original line number Diff line number Diff line Loading @@ -41,6 +41,8 @@ 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; // ------------------------------------------------------------------------------------------------- Loading Loading @@ -89,6 +91,17 @@ public: MOCK_METHOD(ndk::ScopedAStatus, getPwlePrimitiveDurationMax, (int32_t * ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, getPwleCompositionSizeMax, (int32_t * ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, getSupportedBraking, (std::vector<Braking> * ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, getPwleV2FrequencyToOutputAccelerationMap, (std::vector<PwleV2OutputMapEntry> * ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, getPwleV2PrimitiveDurationMaxMillis, (int32_t* ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, getPwleV2PrimitiveDurationMinMillis, (int32_t* ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, getPwleV2CompositionSizeMax, (int32_t* ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, composePwleV2, (const std::vector<PwleV2Primitive>& e, const std::shared_ptr<IVibratorCallback>& cb), (override)); MOCK_METHOD(ndk::ScopedAStatus, getInterfaceVersion, (int32_t*), (override)); MOCK_METHOD(ndk::ScopedAStatus, getInterfaceHash, (std::string*), (override)); MOCK_METHOD(ndk::SpAIBinder, asBinder, (), (override)); Loading Loading
services/vibratorservice/VibratorHalWrapper.cpp +15 −0 Original line number Diff line number Diff line Loading @@ -32,6 +32,8 @@ using aidl::android::hardware::vibrator::CompositePrimitive; using aidl::android::hardware::vibrator::Effect; using aidl::android::hardware::vibrator::EffectStrength; 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 std::chrono::milliseconds; Loading Loading @@ -114,6 +116,12 @@ HalResult<void> HalWrapper::performPwleEffect(const std::vector<PrimitivePwle>&, return HalResult<void>::unsupported(); } HalResult<void> HalWrapper::composePwleV2(const std::vector<PwleV2Primitive>&, const std::function<void()>&) { ALOGV("Skipped composePwleV2 because it's not available in Vibrator HAL"); return HalResult<void>::unsupported(); } HalResult<Capabilities> HalWrapper::getCapabilities() { std::lock_guard<std::mutex> lock(mInfoMutex); if (mInfoCache.mCapabilities.isFailed()) { Loading Loading @@ -313,6 +321,13 @@ HalResult<void> AidlHalWrapper::performPwleEffect(const std::vector<PrimitivePwl return HalResultFactory::fromStatus(getHal()->composePwle(primitives, cb)); } HalResult<void> AidlHalWrapper::composePwleV2(const std::vector<PwleV2Primitive>& composite, const std::function<void()>& completionCallback) { // This method should always support callbacks, so no need to double check. auto cb = ndk::SharedRefBase::make<HalCallbackWrapper>(completionCallback); return HalResultFactory::fromStatus(getHal()->composePwleV2(composite, cb)); } HalResult<Capabilities> AidlHalWrapper::getCapabilitiesInternal() { int32_t cap = 0; auto status = getHal()->getCapabilities(&cap); Loading
services/vibratorservice/include/vibratorservice/VibratorHalWrapper.h +8 −0 Original line number Diff line number Diff line Loading @@ -354,6 +354,8 @@ public: using CompositeEffect = aidl::android::hardware::vibrator::CompositeEffect; using Braking = aidl::android::hardware::vibrator::Braking; using PrimitivePwle = aidl::android::hardware::vibrator::PrimitivePwle; using PwleV2Primitive = aidl::android::hardware::vibrator::PwleV2Primitive; using PwleV2OutputMapEntry = aidl::android::hardware::vibrator::PwleV2OutputMapEntry; explicit HalWrapper(std::shared_ptr<CallbackScheduler> scheduler) : mCallbackScheduler(std::move(scheduler)) {} Loading Loading @@ -391,6 +393,9 @@ public: virtual HalResult<void> performPwleEffect(const std::vector<PrimitivePwle>& primitives, const std::function<void()>& completionCallback); virtual HalResult<void> composePwleV2(const std::vector<PwleV2Primitive>& composite, const std::function<void()>& completionCallback); protected: // Shared pointer to allow CallbackScheduler to outlive this wrapper. const std::shared_ptr<CallbackScheduler> mCallbackScheduler; Loading Loading @@ -471,6 +476,9 @@ public: const std::vector<PrimitivePwle>& primitives, const std::function<void()>& completionCallback) override final; HalResult<void> composePwleV2(const std::vector<PwleV2Primitive>& composite, const std::function<void()>& completionCallback) override final; protected: HalResult<Capabilities> getCapabilitiesInternal() override final; HalResult<std::vector<Effect>> getSupportedEffectsInternal() override final; Loading
services/vibratorservice/test/VibratorHalWrapperAidlTest.cpp +36 −0 Original line number Diff line number Diff line Loading @@ -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::PwleV2Primitive; using aidl::android::hardware::vibrator::VendorEffect; using aidl::android::os::PersistableBundle; Loading Loading @@ -681,3 +682,38 @@ TEST_F(VibratorHalWrapperAidlTest, TestPerformPwleEffect) { ASSERT_TRUE(result.isOk()); ASSERT_EQ(1, *callbackCounter.get()); } TEST_F(VibratorHalWrapperAidlTest, TestComposePwleV2) { auto pwleEffect = { PwleV2Primitive(/*amplitude=*/0.2, /*frequency=*/50, /*time=*/100), PwleV2Primitive(/*amplitude=*/0.5, /*frequency=*/150, /*time=*/100), PwleV2Primitive(/*amplitude=*/0.8, /*frequency=*/250, /*time=*/100), }; { InSequence seq; EXPECT_CALL(*mMockHal.get(), composePwleV2(_, _)) .Times(Exactly(3)) .WillOnce(Return(ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION))) .WillOnce(Return(ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY))) .WillOnce(DoAll(WithArg<1>(vibrator::TriggerCallback()), Return(ndk::ScopedAStatus::ok()))); } std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>(); auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get()); auto result = mWrapper->composePwleV2(pwleEffect, callback); ASSERT_TRUE(result.isUnsupported()); // Callback not triggered on failure ASSERT_EQ(0, *callbackCounter.get()); result = mWrapper->composePwleV2(pwleEffect, callback); ASSERT_TRUE(result.isFailed()); // Callback not triggered for unsupported ASSERT_EQ(0, *callbackCounter.get()); result = mWrapper->composePwleV2(pwleEffect, callback); ASSERT_TRUE(result.isOk()); ASSERT_EQ(1, *callbackCounter.get()); }
services/vibratorservice/test/VibratorHalWrapperHidlV1_0Test.cpp +17 −0 Original line number Diff line number Diff line Loading @@ -40,6 +40,7 @@ using aidl::android::hardware::vibrator::Effect; using aidl::android::hardware::vibrator::EffectStrength; using aidl::android::hardware::vibrator::IVibrator; using aidl::android::hardware::vibrator::PrimitivePwle; using aidl::android::hardware::vibrator::PwleV2Primitive; using aidl::android::hardware::vibrator::VendorEffect; using aidl::android::os::PersistableBundle; Loading Loading @@ -369,3 +370,19 @@ TEST_F(VibratorHalWrapperHidlV1_0Test, TestPerformPwleEffectUnsupported) { // No callback is triggered. ASSERT_EQ(0, *callbackCounter.get()); } TEST_F(VibratorHalWrapperHidlV1_0Test, TestComposePwleV2Unsupported) { auto pwleEffect = { PwleV2Primitive(/*amplitude=*/0.2, /*frequency=*/50, /*time=*/100), PwleV2Primitive(/*amplitude=*/0.5, /*frequency=*/150, /*time=*/100), PwleV2Primitive(/*amplitude=*/0.8, /*frequency=*/250, /*time=*/100), }; std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>(); auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get()); ASSERT_TRUE(mWrapper->composePwleV2(pwleEffect, callback).isUnsupported()); // No callback is triggered. ASSERT_EQ(0, *callbackCounter.get()); }
services/vibratorservice/test/test_mocks.h +13 −0 Original line number Diff line number Diff line Loading @@ -41,6 +41,8 @@ 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; // ------------------------------------------------------------------------------------------------- Loading Loading @@ -89,6 +91,17 @@ public: MOCK_METHOD(ndk::ScopedAStatus, getPwlePrimitiveDurationMax, (int32_t * ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, getPwleCompositionSizeMax, (int32_t * ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, getSupportedBraking, (std::vector<Braking> * ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, getPwleV2FrequencyToOutputAccelerationMap, (std::vector<PwleV2OutputMapEntry> * ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, getPwleV2PrimitiveDurationMaxMillis, (int32_t* ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, getPwleV2PrimitiveDurationMinMillis, (int32_t* ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, getPwleV2CompositionSizeMax, (int32_t* ret), (override)); MOCK_METHOD(ndk::ScopedAStatus, composePwleV2, (const std::vector<PwleV2Primitive>& e, const std::shared_ptr<IVibratorCallback>& cb), (override)); MOCK_METHOD(ndk::ScopedAStatus, getInterfaceVersion, (int32_t*), (override)); MOCK_METHOD(ndk::ScopedAStatus, getInterfaceHash, (std::string*), (override)); MOCK_METHOD(ndk::SpAIBinder, asBinder, (), (override)); Loading