/* * Copyright (C) 2020 The Android Open Source 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 "VibratorHalWrapper" #include #include #include #include #include #include using aidl::android::hardware::vibrator::Braking; using aidl::android::hardware::vibrator::CompositeEffect; using aidl::android::hardware::vibrator::CompositePrimitive; using aidl::android::hardware::vibrator::CompositePwleV2; using aidl::android::hardware::vibrator::Effect; using aidl::android::hardware::vibrator::EffectStrength; using aidl::android::hardware::vibrator::FrequencyAccelerationMapEntry; using aidl::android::hardware::vibrator::IVibrator; using aidl::android::hardware::vibrator::PrimitivePwle; using aidl::android::hardware::vibrator::VendorEffect; using std::chrono::milliseconds; namespace android { namespace vibrator { // ------------------------------------------------------------------------------------------------- Info HalWrapper::getInfo() { getCapabilities(); getPrimitiveDurations(); std::lock_guard lock(mInfoMutex); if (mInfoCache.mSupportedEffects.isFailed()) { mInfoCache.mSupportedEffects = getSupportedEffectsInternal(); } if (mInfoCache.mSupportedBraking.isFailed()) { mInfoCache.mSupportedBraking = getSupportedBrakingInternal(); } if (mInfoCache.mPrimitiveDelayMax.isFailed()) { mInfoCache.mPrimitiveDelayMax = getPrimitiveDelayMaxInternal(); } if (mInfoCache.mPwlePrimitiveDurationMax.isFailed()) { mInfoCache.mPwlePrimitiveDurationMax = getPrimitiveDurationMaxInternal(); } if (mInfoCache.mCompositionSizeMax.isFailed()) { mInfoCache.mCompositionSizeMax = getCompositionSizeMaxInternal(); } if (mInfoCache.mPwleSizeMax.isFailed()) { mInfoCache.mPwleSizeMax = getPwleSizeMaxInternal(); } if (mInfoCache.mMinFrequency.isFailed()) { mInfoCache.mMinFrequency = getMinFrequencyInternal(); } if (mInfoCache.mResonantFrequency.isFailed()) { mInfoCache.mResonantFrequency = getResonantFrequencyInternal(); } if (mInfoCache.mFrequencyResolution.isFailed()) { mInfoCache.mFrequencyResolution = getFrequencyResolutionInternal(); } if (mInfoCache.mQFactor.isFailed()) { mInfoCache.mQFactor = getQFactorInternal(); } if (mInfoCache.mMaxAmplitudes.isFailed()) { mInfoCache.mMaxAmplitudes = getMaxAmplitudesInternal(); } if (mInfoCache.mMaxEnvelopeEffectSize.isFailed()) { mInfoCache.mMaxEnvelopeEffectSize = getMaxEnvelopeEffectSizeInternal(); } if (mInfoCache.mMinEnvelopeEffectControlPointDuration.isFailed()) { mInfoCache.mMinEnvelopeEffectControlPointDuration = getMinEnvelopeEffectControlPointDurationInternal(); } if (mInfoCache.mMaxEnvelopeEffectControlPointDuration.isFailed()) { mInfoCache.mMaxEnvelopeEffectControlPointDuration = getMaxEnvelopeEffectControlPointDurationInternal(); } if (mInfoCache.mFrequencyToOutputAccelerationMap.isFailed()) { mInfoCache.mFrequencyToOutputAccelerationMap = getFrequencyToOutputAccelerationMapInternal(); } return mInfoCache.get(); } HalResult HalWrapper::performVendorEffect(const VendorEffect&, const std::function&) { ALOGV("Skipped performVendorEffect because it's not available in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::performComposedEffect(const std::vector&, const std::function&) { ALOGV("Skipped performComposedEffect because it's not available in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::performPwleEffect(const std::vector&, const std::function&) { ALOGV("Skipped performPwleEffect because it's not available in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::composePwleV2(const CompositePwleV2&, const std::function&) { ALOGV("Skipped composePwleV2 because it's not available in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::getCapabilities() { std::lock_guard lock(mInfoMutex); if (mInfoCache.mCapabilities.isFailed()) { mInfoCache.mCapabilities = getCapabilitiesInternal(); } return mInfoCache.mCapabilities; } HalResult> HalWrapper::getPrimitiveDurations() { std::lock_guard lock(mInfoMutex); if (mInfoCache.mSupportedPrimitives.isFailed()) { mInfoCache.mSupportedPrimitives = getSupportedPrimitivesInternal(); if (mInfoCache.mSupportedPrimitives.isUnsupported()) { mInfoCache.mPrimitiveDurations = HalResult>::unsupported(); } } if (mInfoCache.mPrimitiveDurations.isFailed() && mInfoCache.mSupportedPrimitives.isOk()) { mInfoCache.mPrimitiveDurations = getPrimitiveDurationsInternal(mInfoCache.mSupportedPrimitives.value()); } return mInfoCache.mPrimitiveDurations; } HalResult> HalWrapper::getSupportedEffectsInternal() { ALOGV("Skipped getSupportedEffects because it's not available in Vibrator HAL"); return HalResult>::unsupported(); } HalResult> HalWrapper::getSupportedBrakingInternal() { ALOGV("Skipped getSupportedBraking because it's not available in Vibrator HAL"); return HalResult>::unsupported(); } HalResult> HalWrapper::getSupportedPrimitivesInternal() { ALOGV("Skipped getSupportedPrimitives because it's not available in Vibrator HAL"); return HalResult>::unsupported(); } HalResult> HalWrapper::getPrimitiveDurationsInternal( const std::vector&) { ALOGV("Skipped getPrimitiveDurations because it's not available in Vibrator HAL"); return HalResult>::unsupported(); } HalResult HalWrapper::getPrimitiveDelayMaxInternal() { ALOGV("Skipped getPrimitiveDelayMaxInternal because it's not available in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::getPrimitiveDurationMaxInternal() { ALOGV("Skipped getPrimitiveDurationMaxInternal because it's not available in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::getCompositionSizeMaxInternal() { ALOGV("Skipped getCompositionSizeMaxInternal because it's not available in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::getPwleSizeMaxInternal() { ALOGV("Skipped getPwleSizeMaxInternal because it's not available in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::getMinFrequencyInternal() { ALOGV("Skipped getMinFrequency because it's not available in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::getResonantFrequencyInternal() { ALOGV("Skipped getResonantFrequency because it's not available in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::getFrequencyResolutionInternal() { ALOGV("Skipped getFrequencyResolution because it's not available in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::getQFactorInternal() { ALOGV("Skipped getQFactor because it's not available in Vibrator HAL"); return HalResult::unsupported(); } HalResult> HalWrapper::getMaxAmplitudesInternal() { ALOGV("Skipped getMaxAmplitudes because it's not available in Vibrator HAL"); return HalResult>::unsupported(); } HalResult HalWrapper::getMaxEnvelopeEffectSizeInternal() { ALOGV("Skipped getMaxEnvelopeEffectSizeInternal because it's not available " "in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::getMinEnvelopeEffectControlPointDurationInternal() { ALOGV("Skipped getMinEnvelopeEffectControlPointDurationInternal because it's not " "available in Vibrator HAL"); return HalResult::unsupported(); } HalResult HalWrapper::getMaxEnvelopeEffectControlPointDurationInternal() { ALOGV("Skipped getMaxEnvelopeEffectControlPointDurationInternal because it's not " "available in Vibrator HAL"); return HalResult::unsupported(); } HalResult> HalWrapper::getFrequencyToOutputAccelerationMapInternal() { ALOGV("Skipped getFrequencyToOutputAccelerationMapInternal because it's not " "available in Vibrator HAL"); return HalResult>::unsupported(); } // ------------------------------------------------------------------------------------------------- HalResult AidlHalWrapper::ping() { return HalResultFactory::fromStatus(AIBinder_ping(getHal()->asBinder().get())); } void AidlHalWrapper::tryReconnect() { auto result = mReconnectFn(); if (!result.isOk()) { return; } std::shared_ptr newHandle = result.value(); if (newHandle) { std::lock_guard lock(mHandleMutex); mHandle = std::move(newHandle); } } HalResult AidlHalWrapper::on(milliseconds timeout, const std::function& completionCallback) { HalResult capabilities = getCapabilities(); bool supportsCallback = capabilities.isOk() && static_cast(capabilities.value() & Capabilities::ON_CALLBACK); auto cb = supportsCallback ? ndk::SharedRefBase::make(completionCallback) : nullptr; auto ret = HalResultFactory::fromStatus(getHal()->on(timeout.count(), cb)); if (!supportsCallback && ret.isOk()) { mCallbackScheduler->schedule(completionCallback, timeout); } return ret; } HalResult AidlHalWrapper::off() { return HalResultFactory::fromStatus(getHal()->off()); } HalResult AidlHalWrapper::setAmplitude(float amplitude) { return HalResultFactory::fromStatus(getHal()->setAmplitude(amplitude)); } HalResult AidlHalWrapper::setExternalControl(bool enabled) { return HalResultFactory::fromStatus(getHal()->setExternalControl(enabled)); } HalResult AidlHalWrapper::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) { return HalResultFactory::fromStatus(getHal()->alwaysOnEnable(id, effect, strength)); } HalResult AidlHalWrapper::alwaysOnDisable(int32_t id) { return HalResultFactory::fromStatus(getHal()->alwaysOnDisable(id)); } HalResult AidlHalWrapper::performEffect( Effect effect, EffectStrength strength, const std::function& completionCallback) { HalResult capabilities = getCapabilities(); bool supportsCallback = capabilities.isOk() && static_cast(capabilities.value() & Capabilities::PERFORM_CALLBACK); auto cb = supportsCallback ? ndk::SharedRefBase::make(completionCallback) : nullptr; int32_t lengthMs; auto status = getHal()->perform(effect, strength, cb, &lengthMs); milliseconds length = milliseconds(lengthMs); auto ret = HalResultFactory::fromStatus(std::move(status), length); if (!supportsCallback && ret.isOk()) { mCallbackScheduler->schedule(completionCallback, length); } return ret; } HalResult AidlHalWrapper::performVendorEffect( const VendorEffect& effect, const std::function& completionCallback) { // This method should always support callbacks, so no need to double check. auto cb = ndk::SharedRefBase::make(completionCallback); return HalResultFactory::fromStatus(getHal()->performVendorEffect(effect, cb)); } HalResult AidlHalWrapper::performComposedEffect( const std::vector& primitives, const std::function& completionCallback) { // This method should always support callbacks, so no need to double check. auto cb = ndk::SharedRefBase::make(completionCallback); auto durations = getPrimitiveDurations().valueOr({}); milliseconds duration(0); for (const auto& effect : primitives) { auto primitiveIdx = static_cast(effect.primitive); if (primitiveIdx < durations.size()) { duration += durations[primitiveIdx]; } else { // Make sure the returned duration is positive to indicate successful vibration. duration += milliseconds(1); } duration += milliseconds(effect.delayMs); } return HalResultFactory::fromStatus(getHal()->compose(primitives, cb), duration); } HalResult AidlHalWrapper::performPwleEffect(const std::vector& primitives, const std::function& completionCallback) { // This method should always support callbacks, so no need to double check. auto cb = ndk::SharedRefBase::make(completionCallback); return HalResultFactory::fromStatus(getHal()->composePwle(primitives, cb)); } HalResult AidlHalWrapper::composePwleV2( const CompositePwleV2& composite, const std::function& completionCallback) { // This method should always support callbacks, so no need to double check. auto cb = ndk::SharedRefBase::make(completionCallback); milliseconds totalDuration(0); for (const auto& primitive : composite.pwlePrimitives) { totalDuration += milliseconds(primitive.timeMillis); } return HalResultFactory::fromStatus(getHal()->composePwleV2(composite, cb), totalDuration); } HalResult AidlHalWrapper::getCapabilitiesInternal() { int32_t cap = 0; auto status = getHal()->getCapabilities(&cap); auto capabilities = static_cast(cap); return HalResultFactory::fromStatus(std::move(status), capabilities); } HalResult> AidlHalWrapper::getSupportedEffectsInternal() { std::vector supportedEffects; auto status = getHal()->getSupportedEffects(&supportedEffects); return HalResultFactory::fromStatus>(std::move(status), supportedEffects); } HalResult> AidlHalWrapper::getSupportedBrakingInternal() { std::vector supportedBraking; auto status = getHal()->getSupportedBraking(&supportedBraking); return HalResultFactory::fromStatus>(std::move(status), supportedBraking); } HalResult> AidlHalWrapper::getSupportedPrimitivesInternal() { std::vector supportedPrimitives; auto status = getHal()->getSupportedPrimitives(&supportedPrimitives); return HalResultFactory::fromStatus>(std::move(status), supportedPrimitives); } HalResult> AidlHalWrapper::getPrimitiveDurationsInternal( const std::vector& supportedPrimitives) { std::vector durations; constexpr auto primitiveRange = ndk::enum_range(); constexpr auto primitiveCount = std::distance(primitiveRange.begin(), primitiveRange.end()); durations.resize(primitiveCount); for (auto primitive : supportedPrimitives) { auto primitiveIdx = static_cast(primitive); if (primitiveIdx >= durations.size()) { // Safety check, should not happen if enum_range is correct. ALOGE("Supported primitive %zu is outside range [0,%zu), skipping load duration", primitiveIdx, durations.size()); continue; } int32_t duration = 0; auto status = getHal()->getPrimitiveDuration(primitive, &duration); auto halResult = HalResultFactory::fromStatus(std::move(status), duration); if (halResult.isUnsupported()) { // Should not happen, supported primitives should always support requesting duration. ALOGE("Supported primitive %zu returned unsupported for getPrimitiveDuration", primitiveIdx); } if (halResult.isFailed()) { // Fail entire request if one request has failed. return HalResult>::failed(halResult.errorMessage()); } durations[primitiveIdx] = milliseconds(duration); } return HalResult>::ok(durations); } HalResult AidlHalWrapper::getPrimitiveDelayMaxInternal() { int32_t delay = 0; auto status = getHal()->getCompositionDelayMax(&delay); return HalResultFactory::fromStatus(std::move(status), milliseconds(delay)); } HalResult AidlHalWrapper::getPrimitiveDurationMaxInternal() { int32_t delay = 0; auto status = getHal()->getPwlePrimitiveDurationMax(&delay); return HalResultFactory::fromStatus(std::move(status), milliseconds(delay)); } HalResult AidlHalWrapper::getCompositionSizeMaxInternal() { int32_t size = 0; auto status = getHal()->getCompositionSizeMax(&size); return HalResultFactory::fromStatus(std::move(status), size); } HalResult AidlHalWrapper::getPwleSizeMaxInternal() { int32_t size = 0; auto status = getHal()->getPwleCompositionSizeMax(&size); return HalResultFactory::fromStatus(std::move(status), size); } HalResult AidlHalWrapper::getMinFrequencyInternal() { float minFrequency = 0; auto status = getHal()->getFrequencyMinimum(&minFrequency); return HalResultFactory::fromStatus(std::move(status), minFrequency); } HalResult AidlHalWrapper::getResonantFrequencyInternal() { float f0 = 0; auto status = getHal()->getResonantFrequency(&f0); return HalResultFactory::fromStatus(std::move(status), f0); } HalResult AidlHalWrapper::getFrequencyResolutionInternal() { float frequencyResolution = 0; auto status = getHal()->getFrequencyResolution(&frequencyResolution); return HalResultFactory::fromStatus(std::move(status), frequencyResolution); } HalResult AidlHalWrapper::getQFactorInternal() { float qFactor = 0; auto status = getHal()->getQFactor(&qFactor); return HalResultFactory::fromStatus(std::move(status), qFactor); } HalResult> AidlHalWrapper::getMaxAmplitudesInternal() { std::vector amplitudes; auto status = getHal()->getBandwidthAmplitudeMap(&litudes); return HalResultFactory::fromStatus>(std::move(status), amplitudes); } HalResult AidlHalWrapper::getMaxEnvelopeEffectSizeInternal() { int32_t size = 0; auto status = getHal()->getPwleV2CompositionSizeMax(&size); return HalResultFactory::fromStatus(std::move(status), size); } HalResult AidlHalWrapper::getMinEnvelopeEffectControlPointDurationInternal() { int32_t durationMs = 0; auto status = getHal()->getPwleV2PrimitiveDurationMinMillis(&durationMs); return HalResultFactory::fromStatus(std::move(status), milliseconds(durationMs)); } HalResult AidlHalWrapper::getMaxEnvelopeEffectControlPointDurationInternal() { int32_t durationMs = 0; auto status = getHal()->getPwleV2PrimitiveDurationMaxMillis(&durationMs); return HalResultFactory::fromStatus(std::move(status), milliseconds(durationMs)); } HalResult> AidlHalWrapper::getFrequencyToOutputAccelerationMapInternal() { std::vector frequencyToOutputAccelerationMap; auto status = getHal()->getFrequencyToOutputAccelerationMap(&frequencyToOutputAccelerationMap); return HalResultFactory::fromStatus< std::vector>(std::move(status), frequencyToOutputAccelerationMap); } std::shared_ptr AidlHalWrapper::getHal() { std::lock_guard lock(mHandleMutex); return mHandle; } // ------------------------------------------------------------------------------------------------- }; // namespace vibrator }; // namespace android