/* * 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 #include using android::hardware::vibrator::Braking; using android::hardware::vibrator::CompositeEffect; using android::hardware::vibrator::CompositePrimitive; using android::hardware::vibrator::Effect; using android::hardware::vibrator::EffectStrength; using android::hardware::vibrator::PrimitivePwle; using std::chrono::milliseconds; namespace V1_0 = android::hardware::vibrator::V1_0; namespace V1_1 = android::hardware::vibrator::V1_1; namespace V1_2 = android::hardware::vibrator::V1_2; namespace V1_3 = android::hardware::vibrator::V1_3; namespace Aidl = android::hardware::vibrator; namespace android { namespace vibrator { // ------------------------------------------------------------------------------------------------- template bool isStaticCastValid(Effect effect) { T castEffect = static_cast(effect); auto iter = hardware::hidl_enum_range(); return castEffect >= *iter.begin() && castEffect <= *std::prev(iter.end()); } // ------------------------------------------------------------------------------------------------- const constexpr char* STATUS_T_ERROR_MESSAGE_PREFIX = "status_t = "; const constexpr char* STATUS_V_1_0_ERROR_MESSAGE_PREFIX = "android::hardware::vibrator::V1_0::Status = "; template HalResult HalResult::fromStatus(V1_0::Status status, T data) { switch (status) { case V1_0::Status::OK: return HalResult::ok(data); case V1_0::Status::UNSUPPORTED_OPERATION: return HalResult::unsupported(); default: return HalResult::failed(STATUS_V_1_0_ERROR_MESSAGE_PREFIX + toString(status)); } } template template HalResult HalResult::fromReturn(hardware::Return& ret, T data) { return ret.isOk() ? HalResult::ok(data) : HalResult::failed(ret.description()); } template template HalResult HalResult::fromReturn(hardware::Return& ret, V1_0::Status status, T data) { return ret.isOk() ? HalResult::fromStatus(status, data) : HalResult::failed(ret.description()); } // ------------------------------------------------------------------------------------------------- HalResult HalResult::fromStatus(status_t status) { if (status == android::OK) { return HalResult::ok(); } return HalResult::failed(STATUS_T_ERROR_MESSAGE_PREFIX + statusToString(status)); } HalResult HalResult::fromStatus(binder::Status status) { if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION || status.transactionError() == android::UNKNOWN_TRANSACTION) { // UNKNOWN_TRANSACTION means the HAL implementation is an older version, so this is // the same as the operation being unsupported by this HAL. Should not retry. return HalResult::unsupported(); } if (status.isOk()) { return HalResult::ok(); } return HalResult::failed(std::string(status.toString8().c_str())); } HalResult HalResult::fromStatus(V1_0::Status status) { switch (status) { case V1_0::Status::OK: return HalResult::ok(); case V1_0::Status::UNSUPPORTED_OPERATION: return HalResult::unsupported(); default: return HalResult::failed(STATUS_V_1_0_ERROR_MESSAGE_PREFIX + toString(status)); } } template HalResult HalResult::fromReturn(hardware::Return& ret) { return ret.isOk() ? HalResult::ok() : HalResult::failed(ret.description()); } // ------------------------------------------------------------------------------------------------- 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(); } return mInfoCache.get(); } 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::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 AidlHalWrapper::ping() { return HalResult::fromStatus(IInterface::asBinder(getHal())->pingBinder()); } void AidlHalWrapper::tryReconnect() { auto result = mReconnectFn(); if (!result.isOk()) { return; } sp 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 ? new HalCallbackWrapper(completionCallback) : nullptr; auto ret = HalResult::fromStatus(getHal()->on(timeout.count(), cb)); if (!supportsCallback && ret.isOk()) { mCallbackScheduler->schedule(completionCallback, timeout); } return ret; } HalResult AidlHalWrapper::off() { return HalResult::fromStatus(getHal()->off()); } HalResult AidlHalWrapper::setAmplitude(float amplitude) { return HalResult::fromStatus(getHal()->setAmplitude(amplitude)); } HalResult AidlHalWrapper::setExternalControl(bool enabled) { return HalResult::fromStatus(getHal()->setExternalControl(enabled)); } HalResult AidlHalWrapper::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) { return HalResult::fromStatus(getHal()->alwaysOnEnable(id, effect, strength)); } HalResult AidlHalWrapper::alwaysOnDisable(int32_t id) { return HalResult::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 ? new HalCallbackWrapper(completionCallback) : nullptr; int32_t lengthMs; auto result = getHal()->perform(effect, strength, cb, &lengthMs); milliseconds length = milliseconds(lengthMs); auto ret = HalResult::fromStatus(result, length); if (!supportsCallback && ret.isOk()) { mCallbackScheduler->schedule(completionCallback, length); } return ret; } 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 = new HalCallbackWrapper(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 HalResult::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 = new HalCallbackWrapper(completionCallback); return HalResult::fromStatus(getHal()->composePwle(primitives, cb)); } HalResult AidlHalWrapper::getCapabilitiesInternal() { int32_t capabilities = 0; auto result = getHal()->getCapabilities(&capabilities); return HalResult::fromStatus(result, static_cast(capabilities)); } HalResult> AidlHalWrapper::getSupportedEffectsInternal() { std::vector supportedEffects; auto result = getHal()->getSupportedEffects(&supportedEffects); return HalResult>::fromStatus(result, supportedEffects); } HalResult> AidlHalWrapper::getSupportedBrakingInternal() { std::vector supportedBraking; auto result = getHal()->getSupportedBraking(&supportedBraking); return HalResult>::fromStatus(result, supportedBraking); } HalResult> AidlHalWrapper::getSupportedPrimitivesInternal() { std::vector supportedPrimitives; auto result = getHal()->getSupportedPrimitives(&supportedPrimitives); return HalResult>::fromStatus(result, supportedPrimitives); } HalResult> AidlHalWrapper::getPrimitiveDurationsInternal( const std::vector& supportedPrimitives) { std::vector durations; constexpr auto primitiveRange = 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. continue; } int32_t duration = 0; auto status = getHal()->getPrimitiveDuration(primitive, &duration); if (!status.isOk()) { return HalResult>::failed(status.toString8().c_str()); } durations[primitiveIdx] = milliseconds(duration); } return HalResult>::ok(durations); } HalResult AidlHalWrapper::getPrimitiveDelayMaxInternal() { int32_t delay = 0; auto result = getHal()->getCompositionDelayMax(&delay); return HalResult::fromStatus(result, milliseconds(delay)); } HalResult AidlHalWrapper::getPrimitiveDurationMaxInternal() { int32_t delay = 0; auto result = getHal()->getPwlePrimitiveDurationMax(&delay); return HalResult::fromStatus(result, milliseconds(delay)); } HalResult AidlHalWrapper::getCompositionSizeMaxInternal() { int32_t size = 0; auto result = getHal()->getCompositionSizeMax(&size); return HalResult::fromStatus(result, size); } HalResult AidlHalWrapper::getPwleSizeMaxInternal() { int32_t size = 0; auto result = getHal()->getPwleCompositionSizeMax(&size); return HalResult::fromStatus(result, size); } HalResult AidlHalWrapper::getMinFrequencyInternal() { float minFrequency = 0; auto result = getHal()->getFrequencyMinimum(&minFrequency); return HalResult::fromStatus(result, minFrequency); } HalResult AidlHalWrapper::getResonantFrequencyInternal() { float f0 = 0; auto result = getHal()->getResonantFrequency(&f0); return HalResult::fromStatus(result, f0); } HalResult AidlHalWrapper::getFrequencyResolutionInternal() { float frequencyResolution = 0; auto result = getHal()->getFrequencyResolution(&frequencyResolution); return HalResult::fromStatus(result, frequencyResolution); } HalResult AidlHalWrapper::getQFactorInternal() { float qFactor = 0; auto result = getHal()->getQFactor(&qFactor); return HalResult::fromStatus(result, qFactor); } HalResult> AidlHalWrapper::getMaxAmplitudesInternal() { std::vector amplitudes; auto result = getHal()->getBandwidthAmplitudeMap(&litudes); return HalResult>::fromStatus(result, amplitudes); } sp AidlHalWrapper::getHal() { std::lock_guard lock(mHandleMutex); return mHandle; } // ------------------------------------------------------------------------------------------------- template HalResult HidlHalWrapper::ping() { auto result = getHal()->ping(); return HalResult::fromReturn(result); } template void HidlHalWrapper::tryReconnect() { sp newHandle = I::tryGetService(); if (newHandle) { std::lock_guard lock(mHandleMutex); mHandle = std::move(newHandle); } } template HalResult HidlHalWrapper::on(milliseconds timeout, const std::function& completionCallback) { auto result = getHal()->on(timeout.count()); auto ret = HalResult::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); if (ret.isOk()) { mCallbackScheduler->schedule(completionCallback, timeout); } return ret; } template HalResult HidlHalWrapper::off() { auto result = getHal()->off(); return HalResult::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); } template HalResult HidlHalWrapper::setAmplitude(float amplitude) { uint8_t amp = static_cast(amplitude * std::numeric_limits::max()); auto result = getHal()->setAmplitude(amp); return HalResult::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); } template HalResult HidlHalWrapper::setExternalControl(bool) { ALOGV("Skipped setExternalControl because Vibrator HAL does not support it"); return HalResult::unsupported(); } template HalResult HidlHalWrapper::alwaysOnEnable(int32_t, Effect, EffectStrength) { ALOGV("Skipped alwaysOnEnable because Vibrator HAL AIDL is not available"); return HalResult::unsupported(); } template HalResult HidlHalWrapper::alwaysOnDisable(int32_t) { ALOGV("Skipped alwaysOnDisable because Vibrator HAL AIDL is not available"); return HalResult::unsupported(); } template HalResult HidlHalWrapper::getCapabilitiesInternal() { hardware::Return result = getHal()->supportsAmplitudeControl(); Capabilities capabilities = result.withDefault(false) ? Capabilities::AMPLITUDE_CONTROL : Capabilities::NONE; return HalResult::fromReturn(result, capabilities); } template template HalResult HidlHalWrapper::performInternal( perform_fn performFn, sp handle, T effect, EffectStrength strength, const std::function& completionCallback) { V1_0::Status status; int32_t lengthMs; auto effectCallback = [&status, &lengthMs](V1_0::Status retStatus, uint32_t retLengthMs) { status = retStatus; lengthMs = retLengthMs; }; V1_0::EffectStrength effectStrength = static_cast(strength); auto result = std::invoke(performFn, handle, effect, effectStrength, effectCallback); milliseconds length = milliseconds(lengthMs); auto ret = HalResult::fromReturn(result, status, length); if (ret.isOk()) { mCallbackScheduler->schedule(completionCallback, length); } return ret; } template sp HidlHalWrapper::getHal() { std::lock_guard lock(mHandleMutex); return mHandle; } // ------------------------------------------------------------------------------------------------- HalResult HidlHalWrapperV1_0::performEffect( Effect effect, EffectStrength strength, const std::function& completionCallback) { if (isStaticCastValid(effect)) { return performInternal(&V1_0::IVibrator::perform, getHal(), static_cast(effect), strength, completionCallback); } ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", Aidl::toString(effect).c_str()); return HalResult::unsupported(); } // ------------------------------------------------------------------------------------------------- HalResult HidlHalWrapperV1_1::performEffect( Effect effect, EffectStrength strength, const std::function& completionCallback) { if (isStaticCastValid(effect)) { return performInternal(&V1_1::IVibrator::perform, getHal(), static_cast(effect), strength, completionCallback); } if (isStaticCastValid(effect)) { return performInternal(&V1_1::IVibrator::perform_1_1, getHal(), static_cast(effect), strength, completionCallback); } ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", Aidl::toString(effect).c_str()); return HalResult::unsupported(); } // ------------------------------------------------------------------------------------------------- HalResult HidlHalWrapperV1_2::performEffect( Effect effect, EffectStrength strength, const std::function& completionCallback) { if (isStaticCastValid(effect)) { return performInternal(&V1_2::IVibrator::perform, getHal(), static_cast(effect), strength, completionCallback); } if (isStaticCastValid(effect)) { return performInternal(&V1_2::IVibrator::perform_1_1, getHal(), static_cast(effect), strength, completionCallback); } if (isStaticCastValid(effect)) { return performInternal(&V1_2::IVibrator::perform_1_2, getHal(), static_cast(effect), strength, completionCallback); } ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", Aidl::toString(effect).c_str()); return HalResult::unsupported(); } // ------------------------------------------------------------------------------------------------- HalResult HidlHalWrapperV1_3::setExternalControl(bool enabled) { auto result = getHal()->setExternalControl(static_cast(enabled)); return HalResult::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); } HalResult HidlHalWrapperV1_3::performEffect( Effect effect, EffectStrength strength, const std::function& completionCallback) { if (isStaticCastValid(effect)) { return performInternal(&V1_3::IVibrator::perform, getHal(), static_cast(effect), strength, completionCallback); } if (isStaticCastValid(effect)) { return performInternal(&V1_3::IVibrator::perform_1_1, getHal(), static_cast(effect), strength, completionCallback); } if (isStaticCastValid(effect)) { return performInternal(&V1_3::IVibrator::perform_1_2, getHal(), static_cast(effect), strength, completionCallback); } if (isStaticCastValid(effect)) { return performInternal(&V1_3::IVibrator::perform_1_3, getHal(), static_cast(effect), strength, completionCallback); } ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", Aidl::toString(effect).c_str()); return HalResult::unsupported(); } HalResult HidlHalWrapperV1_3::getCapabilitiesInternal() { Capabilities capabilities = Capabilities::NONE; sp hal = getHal(); auto amplitudeResult = hal->supportsAmplitudeControl(); if (!amplitudeResult.isOk()) { return HalResult::fromReturn(amplitudeResult, capabilities); } auto externalControlResult = hal->supportsExternalControl(); if (amplitudeResult.withDefault(false)) { capabilities |= Capabilities::AMPLITUDE_CONTROL; } if (externalControlResult.withDefault(false)) { capabilities |= Capabilities::EXTERNAL_CONTROL; if (amplitudeResult.withDefault(false)) { capabilities |= Capabilities::EXTERNAL_AMPLITUDE_CONTROL; } } return HalResult::fromReturn(externalControlResult, capabilities); } // ------------------------------------------------------------------------------------------------- }; // namespace vibrator }; // namespace android