Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "VibratorHalWrapper" |
| 18 | |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 19 | #include <aidl/android/hardware/vibrator/IVibrator.h> |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 20 | #include <android/hardware/vibrator/1.3/IVibrator.h> |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 21 | #include <hardware/vibrator.h> |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 22 | #include <cmath> |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 23 | |
| 24 | #include <utils/Log.h> |
| 25 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 26 | #include <vibratorservice/VibratorCallbackScheduler.h> |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 27 | #include <vibratorservice/VibratorHalWrapper.h> |
| 28 | |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 29 | using aidl::android::hardware::vibrator::Braking; |
| 30 | using aidl::android::hardware::vibrator::CompositeEffect; |
| 31 | using aidl::android::hardware::vibrator::CompositePrimitive; |
| 32 | using aidl::android::hardware::vibrator::Effect; |
| 33 | using aidl::android::hardware::vibrator::EffectStrength; |
| 34 | using aidl::android::hardware::vibrator::PrimitivePwle; |
Ahmad Khalil | 1111189 | 2024-08-05 17:40:17 +0000 | [diff] [blame] | 35 | using aidl::android::hardware::vibrator::PwleV2OutputMapEntry; |
| 36 | using aidl::android::hardware::vibrator::PwleV2Primitive; |
Lais Andrade | f172ea1 | 2024-07-12 13:45:01 +0100 | [diff] [blame] | 37 | using aidl::android::hardware::vibrator::VendorEffect; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 38 | |
| 39 | using std::chrono::milliseconds; |
| 40 | |
| 41 | namespace V1_0 = android::hardware::vibrator::V1_0; |
| 42 | namespace V1_1 = android::hardware::vibrator::V1_1; |
| 43 | namespace V1_2 = android::hardware::vibrator::V1_2; |
| 44 | namespace V1_3 = android::hardware::vibrator::V1_3; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 45 | namespace Aidl = aidl::android::hardware::vibrator; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 46 | |
| 47 | namespace android { |
| 48 | |
| 49 | namespace vibrator { |
| 50 | |
| 51 | // ------------------------------------------------------------------------------------------------- |
| 52 | |
| 53 | template <class T> |
| 54 | bool isStaticCastValid(Effect effect) { |
| 55 | T castEffect = static_cast<T>(effect); |
| 56 | auto iter = hardware::hidl_enum_range<T>(); |
| 57 | return castEffect >= *iter.begin() && castEffect <= *std::prev(iter.end()); |
| 58 | } |
| 59 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 60 | // ------------------------------------------------------------------------------------------------- |
| 61 | |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 62 | Info HalWrapper::getInfo() { |
| 63 | getCapabilities(); |
| 64 | getPrimitiveDurations(); |
| 65 | std::lock_guard<std::mutex> lock(mInfoMutex); |
| 66 | if (mInfoCache.mSupportedEffects.isFailed()) { |
| 67 | mInfoCache.mSupportedEffects = getSupportedEffectsInternal(); |
| 68 | } |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 69 | if (mInfoCache.mSupportedBraking.isFailed()) { |
| 70 | mInfoCache.mSupportedBraking = getSupportedBrakingInternal(); |
| 71 | } |
Lais Andrade | 4aa80c9 | 2021-06-03 17:20:16 +0100 | [diff] [blame] | 72 | if (mInfoCache.mPrimitiveDelayMax.isFailed()) { |
| 73 | mInfoCache.mPrimitiveDelayMax = getPrimitiveDelayMaxInternal(); |
| 74 | } |
| 75 | if (mInfoCache.mPwlePrimitiveDurationMax.isFailed()) { |
| 76 | mInfoCache.mPwlePrimitiveDurationMax = getPrimitiveDurationMaxInternal(); |
| 77 | } |
| 78 | if (mInfoCache.mCompositionSizeMax.isFailed()) { |
| 79 | mInfoCache.mCompositionSizeMax = getCompositionSizeMaxInternal(); |
| 80 | } |
| 81 | if (mInfoCache.mPwleSizeMax.isFailed()) { |
| 82 | mInfoCache.mPwleSizeMax = getPwleSizeMaxInternal(); |
| 83 | } |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 84 | if (mInfoCache.mMinFrequency.isFailed()) { |
| 85 | mInfoCache.mMinFrequency = getMinFrequencyInternal(); |
| 86 | } |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 87 | if (mInfoCache.mResonantFrequency.isFailed()) { |
| 88 | mInfoCache.mResonantFrequency = getResonantFrequencyInternal(); |
| 89 | } |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 90 | if (mInfoCache.mFrequencyResolution.isFailed()) { |
| 91 | mInfoCache.mFrequencyResolution = getFrequencyResolutionInternal(); |
| 92 | } |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 93 | if (mInfoCache.mQFactor.isFailed()) { |
| 94 | mInfoCache.mQFactor = getQFactorInternal(); |
| 95 | } |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 96 | if (mInfoCache.mMaxAmplitudes.isFailed()) { |
| 97 | mInfoCache.mMaxAmplitudes = getMaxAmplitudesInternal(); |
| 98 | } |
Ahmad Khalil | 193e37b | 2024-09-02 10:05:50 +0000 | [diff] [blame^] | 99 | if (mInfoCache.mMaxEnvelopeEffectSize.isFailed()) { |
| 100 | mInfoCache.mMaxEnvelopeEffectSize = getMaxEnvelopeEffectSizeInternal(); |
| 101 | } |
| 102 | if (mInfoCache.mMinEnvelopeEffectControlPointDuration.isFailed()) { |
| 103 | mInfoCache.mMinEnvelopeEffectControlPointDuration = |
| 104 | getMinEnvelopeEffectControlPointDurationInternal(); |
| 105 | } |
| 106 | if (mInfoCache.mMaxEnvelopeEffectControlPointDuration.isFailed()) { |
| 107 | mInfoCache.mMaxEnvelopeEffectControlPointDuration = |
| 108 | getMaxEnvelopeEffectControlPointDurationInternal(); |
| 109 | } |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 110 | return mInfoCache.get(); |
| 111 | } |
| 112 | |
Lais Andrade | f172ea1 | 2024-07-12 13:45:01 +0100 | [diff] [blame] | 113 | HalResult<void> HalWrapper::performVendorEffect(const VendorEffect&, const std::function<void()>&) { |
| 114 | ALOGV("Skipped performVendorEffect because it's not available in Vibrator HAL"); |
| 115 | return HalResult<void>::unsupported(); |
| 116 | } |
| 117 | |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 118 | HalResult<milliseconds> HalWrapper::performComposedEffect(const std::vector<CompositeEffect>&, |
| 119 | const std::function<void()>&) { |
| 120 | ALOGV("Skipped performComposedEffect because it's not available in Vibrator HAL"); |
| 121 | return HalResult<milliseconds>::unsupported(); |
| 122 | } |
| 123 | |
| 124 | HalResult<void> HalWrapper::performPwleEffect(const std::vector<PrimitivePwle>&, |
| 125 | const std::function<void()>&) { |
| 126 | ALOGV("Skipped performPwleEffect because it's not available in Vibrator HAL"); |
| 127 | return HalResult<void>::unsupported(); |
| 128 | } |
| 129 | |
Ahmad Khalil | 1111189 | 2024-08-05 17:40:17 +0000 | [diff] [blame] | 130 | HalResult<void> HalWrapper::composePwleV2(const std::vector<PwleV2Primitive>&, |
| 131 | const std::function<void()>&) { |
| 132 | ALOGV("Skipped composePwleV2 because it's not available in Vibrator HAL"); |
| 133 | return HalResult<void>::unsupported(); |
| 134 | } |
| 135 | |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 136 | HalResult<Capabilities> HalWrapper::getCapabilities() { |
| 137 | std::lock_guard<std::mutex> lock(mInfoMutex); |
| 138 | if (mInfoCache.mCapabilities.isFailed()) { |
| 139 | mInfoCache.mCapabilities = getCapabilitiesInternal(); |
| 140 | } |
| 141 | return mInfoCache.mCapabilities; |
| 142 | } |
| 143 | |
| 144 | HalResult<std::vector<milliseconds>> HalWrapper::getPrimitiveDurations() { |
| 145 | std::lock_guard<std::mutex> lock(mInfoMutex); |
| 146 | if (mInfoCache.mSupportedPrimitives.isFailed()) { |
| 147 | mInfoCache.mSupportedPrimitives = getSupportedPrimitivesInternal(); |
| 148 | if (mInfoCache.mSupportedPrimitives.isUnsupported()) { |
| 149 | mInfoCache.mPrimitiveDurations = HalResult<std::vector<milliseconds>>::unsupported(); |
| 150 | } |
| 151 | } |
| 152 | if (mInfoCache.mPrimitiveDurations.isFailed() && mInfoCache.mSupportedPrimitives.isOk()) { |
| 153 | mInfoCache.mPrimitiveDurations = |
| 154 | getPrimitiveDurationsInternal(mInfoCache.mSupportedPrimitives.value()); |
| 155 | } |
| 156 | return mInfoCache.mPrimitiveDurations; |
| 157 | } |
| 158 | |
| 159 | HalResult<std::vector<Effect>> HalWrapper::getSupportedEffectsInternal() { |
| 160 | ALOGV("Skipped getSupportedEffects because it's not available in Vibrator HAL"); |
| 161 | return HalResult<std::vector<Effect>>::unsupported(); |
| 162 | } |
| 163 | |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 164 | HalResult<std::vector<Braking>> HalWrapper::getSupportedBrakingInternal() { |
| 165 | ALOGV("Skipped getSupportedBraking because it's not available in Vibrator HAL"); |
| 166 | return HalResult<std::vector<Braking>>::unsupported(); |
| 167 | } |
| 168 | |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 169 | HalResult<std::vector<CompositePrimitive>> HalWrapper::getSupportedPrimitivesInternal() { |
| 170 | ALOGV("Skipped getSupportedPrimitives because it's not available in Vibrator HAL"); |
| 171 | return HalResult<std::vector<CompositePrimitive>>::unsupported(); |
| 172 | } |
| 173 | |
| 174 | HalResult<std::vector<milliseconds>> HalWrapper::getPrimitiveDurationsInternal( |
| 175 | const std::vector<CompositePrimitive>&) { |
| 176 | ALOGV("Skipped getPrimitiveDurations because it's not available in Vibrator HAL"); |
| 177 | return HalResult<std::vector<milliseconds>>::unsupported(); |
| 178 | } |
| 179 | |
Lais Andrade | 4aa80c9 | 2021-06-03 17:20:16 +0100 | [diff] [blame] | 180 | HalResult<milliseconds> HalWrapper::getPrimitiveDelayMaxInternal() { |
| 181 | ALOGV("Skipped getPrimitiveDelayMaxInternal because it's not available in Vibrator HAL"); |
| 182 | return HalResult<milliseconds>::unsupported(); |
| 183 | } |
| 184 | |
| 185 | HalResult<milliseconds> HalWrapper::getPrimitiveDurationMaxInternal() { |
| 186 | ALOGV("Skipped getPrimitiveDurationMaxInternal because it's not available in Vibrator HAL"); |
| 187 | return HalResult<milliseconds>::unsupported(); |
| 188 | } |
| 189 | |
| 190 | HalResult<int32_t> HalWrapper::getCompositionSizeMaxInternal() { |
| 191 | ALOGV("Skipped getCompositionSizeMaxInternal because it's not available in Vibrator HAL"); |
| 192 | return HalResult<int32_t>::unsupported(); |
| 193 | } |
| 194 | |
| 195 | HalResult<int32_t> HalWrapper::getPwleSizeMaxInternal() { |
| 196 | ALOGV("Skipped getPwleSizeMaxInternal because it's not available in Vibrator HAL"); |
| 197 | return HalResult<int32_t>::unsupported(); |
| 198 | } |
| 199 | |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 200 | HalResult<float> HalWrapper::getMinFrequencyInternal() { |
| 201 | ALOGV("Skipped getMinFrequency because it's not available in Vibrator HAL"); |
| 202 | return HalResult<float>::unsupported(); |
| 203 | } |
| 204 | |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 205 | HalResult<float> HalWrapper::getResonantFrequencyInternal() { |
| 206 | ALOGV("Skipped getResonantFrequency because it's not available in Vibrator HAL"); |
| 207 | return HalResult<float>::unsupported(); |
| 208 | } |
| 209 | |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 210 | HalResult<float> HalWrapper::getFrequencyResolutionInternal() { |
| 211 | ALOGV("Skipped getFrequencyResolution because it's not available in Vibrator HAL"); |
| 212 | return HalResult<float>::unsupported(); |
| 213 | } |
| 214 | |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 215 | HalResult<float> HalWrapper::getQFactorInternal() { |
| 216 | ALOGV("Skipped getQFactor because it's not available in Vibrator HAL"); |
| 217 | return HalResult<float>::unsupported(); |
| 218 | } |
| 219 | |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 220 | HalResult<std::vector<float>> HalWrapper::getMaxAmplitudesInternal() { |
| 221 | ALOGV("Skipped getMaxAmplitudes because it's not available in Vibrator HAL"); |
| 222 | return HalResult<std::vector<float>>::unsupported(); |
| 223 | } |
Ahmad Khalil | 193e37b | 2024-09-02 10:05:50 +0000 | [diff] [blame^] | 224 | HalResult<int32_t> HalWrapper::getMaxEnvelopeEffectSizeInternal() { |
| 225 | ALOGV("Skipped getMaxEnvelopeEffectSizeInternal because it's not available " |
| 226 | "in Vibrator HAL"); |
| 227 | return HalResult<int32_t>::unsupported(); |
| 228 | } |
| 229 | |
| 230 | HalResult<milliseconds> HalWrapper::getMinEnvelopeEffectControlPointDurationInternal() { |
| 231 | ALOGV("Skipped getMinEnvelopeEffectControlPointDurationInternal because it's not " |
| 232 | "available in Vibrator HAL"); |
| 233 | return HalResult<milliseconds>::unsupported(); |
| 234 | } |
| 235 | |
| 236 | HalResult<milliseconds> HalWrapper::getMaxEnvelopeEffectControlPointDurationInternal() { |
| 237 | ALOGV("Skipped getMaxEnvelopeEffectControlPointDurationInternal because it's not " |
| 238 | "available in Vibrator HAL"); |
| 239 | return HalResult<milliseconds>::unsupported(); |
| 240 | } |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 241 | |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 242 | // ------------------------------------------------------------------------------------------------- |
| 243 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 244 | HalResult<void> AidlHalWrapper::ping() { |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 245 | return HalResultFactory::fromStatus(AIBinder_ping(getHal()->asBinder().get())); |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void AidlHalWrapper::tryReconnect() { |
Lais Andrade | 98c9703 | 2020-11-17 19:23:01 +0000 | [diff] [blame] | 249 | auto result = mReconnectFn(); |
| 250 | if (!result.isOk()) { |
| 251 | return; |
| 252 | } |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 253 | std::shared_ptr<Aidl::IVibrator> newHandle = result.value(); |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 254 | if (newHandle) { |
| 255 | std::lock_guard<std::mutex> lock(mHandleMutex); |
| 256 | mHandle = std::move(newHandle); |
| 257 | } |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | HalResult<void> AidlHalWrapper::on(milliseconds timeout, |
| 261 | const std::function<void()>& completionCallback) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 262 | HalResult<Capabilities> capabilities = getCapabilities(); |
| 263 | bool supportsCallback = capabilities.isOk() && |
| 264 | static_cast<int32_t>(capabilities.value() & Capabilities::ON_CALLBACK); |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 265 | auto cb = supportsCallback ? ndk::SharedRefBase::make<HalCallbackWrapper>(completionCallback) |
| 266 | : nullptr; |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 267 | |
Lais Andrade | 641248e | 2024-02-16 17:49:36 +0000 | [diff] [blame] | 268 | auto ret = HalResultFactory::fromStatus(getHal()->on(timeout.count(), cb)); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 269 | if (!supportsCallback && ret.isOk()) { |
| 270 | mCallbackScheduler->schedule(completionCallback, timeout); |
| 271 | } |
| 272 | |
| 273 | return ret; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | HalResult<void> AidlHalWrapper::off() { |
Lais Andrade | 641248e | 2024-02-16 17:49:36 +0000 | [diff] [blame] | 277 | return HalResultFactory::fromStatus(getHal()->off()); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Lais Andrade | 4e2b2d4 | 2021-02-15 20:58:51 +0000 | [diff] [blame] | 280 | HalResult<void> AidlHalWrapper::setAmplitude(float amplitude) { |
Lais Andrade | 641248e | 2024-02-16 17:49:36 +0000 | [diff] [blame] | 281 | return HalResultFactory::fromStatus(getHal()->setAmplitude(amplitude)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | HalResult<void> AidlHalWrapper::setExternalControl(bool enabled) { |
Lais Andrade | 641248e | 2024-02-16 17:49:36 +0000 | [diff] [blame] | 285 | return HalResultFactory::fromStatus(getHal()->setExternalControl(enabled)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | HalResult<void> AidlHalWrapper::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) { |
Lais Andrade | 641248e | 2024-02-16 17:49:36 +0000 | [diff] [blame] | 289 | return HalResultFactory::fromStatus(getHal()->alwaysOnEnable(id, effect, strength)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | HalResult<void> AidlHalWrapper::alwaysOnDisable(int32_t id) { |
Lais Andrade | 641248e | 2024-02-16 17:49:36 +0000 | [diff] [blame] | 293 | return HalResultFactory::fromStatus(getHal()->alwaysOnDisable(id)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 296 | HalResult<milliseconds> AidlHalWrapper::performEffect( |
| 297 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 298 | HalResult<Capabilities> capabilities = getCapabilities(); |
| 299 | bool supportsCallback = capabilities.isOk() && |
| 300 | static_cast<int32_t>(capabilities.value() & Capabilities::PERFORM_CALLBACK); |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 301 | auto cb = supportsCallback ? ndk::SharedRefBase::make<HalCallbackWrapper>(completionCallback) |
| 302 | : nullptr; |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 303 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 304 | int32_t lengthMs; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 305 | auto status = getHal()->perform(effect, strength, cb, &lengthMs); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 306 | milliseconds length = milliseconds(lengthMs); |
| 307 | |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 308 | auto ret = HalResultFactory::fromStatus<milliseconds>(std::move(status), length); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 309 | if (!supportsCallback && ret.isOk()) { |
| 310 | mCallbackScheduler->schedule(completionCallback, length); |
| 311 | } |
| 312 | |
| 313 | return ret; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Lais Andrade | f172ea1 | 2024-07-12 13:45:01 +0100 | [diff] [blame] | 316 | HalResult<void> AidlHalWrapper::performVendorEffect( |
| 317 | const VendorEffect& effect, const std::function<void()>& completionCallback) { |
| 318 | // This method should always support callbacks, so no need to double check. |
| 319 | auto cb = ndk::SharedRefBase::make<HalCallbackWrapper>(completionCallback); |
| 320 | return HalResultFactory::fromStatus(getHal()->performVendorEffect(effect, cb)); |
| 321 | } |
| 322 | |
Lais Andrade | 49b60b1 | 2021-02-23 13:27:41 +0000 | [diff] [blame] | 323 | HalResult<milliseconds> AidlHalWrapper::performComposedEffect( |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 324 | const std::vector<CompositeEffect>& primitives, |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 325 | const std::function<void()>& completionCallback) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 326 | // This method should always support callbacks, so no need to double check. |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 327 | auto cb = ndk::SharedRefBase::make<HalCallbackWrapper>(completionCallback); |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 328 | |
| 329 | auto durations = getPrimitiveDurations().valueOr({}); |
Lais Andrade | 49b60b1 | 2021-02-23 13:27:41 +0000 | [diff] [blame] | 330 | milliseconds duration(0); |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 331 | for (const auto& effect : primitives) { |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 332 | auto primitiveIdx = static_cast<size_t>(effect.primitive); |
| 333 | if (primitiveIdx < durations.size()) { |
| 334 | duration += durations[primitiveIdx]; |
| 335 | } else { |
| 336 | // Make sure the returned duration is positive to indicate successful vibration. |
| 337 | duration += milliseconds(1); |
Lais Andrade | 49b60b1 | 2021-02-23 13:27:41 +0000 | [diff] [blame] | 338 | } |
| 339 | duration += milliseconds(effect.delayMs); |
| 340 | } |
Lais Andrade | 49b60b1 | 2021-02-23 13:27:41 +0000 | [diff] [blame] | 341 | |
Lais Andrade | 641248e | 2024-02-16 17:49:36 +0000 | [diff] [blame] | 342 | return HalResultFactory::fromStatus<milliseconds>(getHal()->compose(primitives, cb), duration); |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | HalResult<void> AidlHalWrapper::performPwleEffect(const std::vector<PrimitivePwle>& primitives, |
| 346 | const std::function<void()>& completionCallback) { |
| 347 | // This method should always support callbacks, so no need to double check. |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 348 | auto cb = ndk::SharedRefBase::make<HalCallbackWrapper>(completionCallback); |
Lais Andrade | 641248e | 2024-02-16 17:49:36 +0000 | [diff] [blame] | 349 | return HalResultFactory::fromStatus(getHal()->composePwle(primitives, cb)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 350 | } |
| 351 | |
Ahmad Khalil | 1111189 | 2024-08-05 17:40:17 +0000 | [diff] [blame] | 352 | HalResult<void> AidlHalWrapper::composePwleV2(const std::vector<PwleV2Primitive>& composite, |
| 353 | const std::function<void()>& completionCallback) { |
| 354 | // This method should always support callbacks, so no need to double check. |
| 355 | auto cb = ndk::SharedRefBase::make<HalCallbackWrapper>(completionCallback); |
| 356 | return HalResultFactory::fromStatus(getHal()->composePwleV2(composite, cb)); |
| 357 | } |
| 358 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 359 | HalResult<Capabilities> AidlHalWrapper::getCapabilitiesInternal() { |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 360 | int32_t cap = 0; |
| 361 | auto status = getHal()->getCapabilities(&cap); |
| 362 | auto capabilities = static_cast<Capabilities>(cap); |
| 363 | return HalResultFactory::fromStatus<Capabilities>(std::move(status), capabilities); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | HalResult<std::vector<Effect>> AidlHalWrapper::getSupportedEffectsInternal() { |
| 367 | std::vector<Effect> supportedEffects; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 368 | auto status = getHal()->getSupportedEffects(&supportedEffects); |
| 369 | return HalResultFactory::fromStatus<std::vector<Effect>>(std::move(status), supportedEffects); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 372 | HalResult<std::vector<Braking>> AidlHalWrapper::getSupportedBrakingInternal() { |
| 373 | std::vector<Braking> supportedBraking; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 374 | auto status = getHal()->getSupportedBraking(&supportedBraking); |
| 375 | return HalResultFactory::fromStatus<std::vector<Braking>>(std::move(status), supportedBraking); |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Lais Andrade | 07f9c0e | 2020-08-11 16:22:12 +0000 | [diff] [blame] | 378 | HalResult<std::vector<CompositePrimitive>> AidlHalWrapper::getSupportedPrimitivesInternal() { |
| 379 | std::vector<CompositePrimitive> supportedPrimitives; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 380 | auto status = getHal()->getSupportedPrimitives(&supportedPrimitives); |
| 381 | return HalResultFactory::fromStatus<std::vector<CompositePrimitive>>(std::move(status), |
Lais Andrade | 641248e | 2024-02-16 17:49:36 +0000 | [diff] [blame] | 382 | supportedPrimitives); |
Lais Andrade | 07f9c0e | 2020-08-11 16:22:12 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 385 | HalResult<std::vector<milliseconds>> AidlHalWrapper::getPrimitiveDurationsInternal( |
| 386 | const std::vector<CompositePrimitive>& supportedPrimitives) { |
| 387 | std::vector<milliseconds> durations; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 388 | constexpr auto primitiveRange = ndk::enum_range<CompositePrimitive>(); |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 389 | constexpr auto primitiveCount = std::distance(primitiveRange.begin(), primitiveRange.end()); |
| 390 | durations.resize(primitiveCount); |
| 391 | |
| 392 | for (auto primitive : supportedPrimitives) { |
| 393 | auto primitiveIdx = static_cast<size_t>(primitive); |
| 394 | if (primitiveIdx >= durations.size()) { |
| 395 | // Safety check, should not happen if enum_range is correct. |
Lais Andrade | 4928bfd | 2021-08-25 18:21:12 +0100 | [diff] [blame] | 396 | ALOGE("Supported primitive %zu is outside range [0,%zu), skipping load duration", |
| 397 | primitiveIdx, durations.size()); |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 398 | continue; |
| 399 | } |
| 400 | int32_t duration = 0; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 401 | auto status = getHal()->getPrimitiveDuration(primitive, &duration); |
| 402 | auto halResult = HalResultFactory::fromStatus<int32_t>(std::move(status), duration); |
Lais Andrade | 4928bfd | 2021-08-25 18:21:12 +0100 | [diff] [blame] | 403 | if (halResult.isUnsupported()) { |
| 404 | // Should not happen, supported primitives should always support requesting duration. |
| 405 | ALOGE("Supported primitive %zu returned unsupported for getPrimitiveDuration", |
| 406 | primitiveIdx); |
| 407 | } |
| 408 | if (halResult.isFailed()) { |
| 409 | // Fail entire request if one request has failed. |
Lais Andrade | f172ea1 | 2024-07-12 13:45:01 +0100 | [diff] [blame] | 410 | return HalResult<std::vector<milliseconds>>::failed(halResult.errorMessage()); |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame] | 411 | } |
| 412 | durations[primitiveIdx] = milliseconds(duration); |
| 413 | } |
| 414 | |
| 415 | return HalResult<std::vector<milliseconds>>::ok(durations); |
| 416 | } |
| 417 | |
Lais Andrade | 4aa80c9 | 2021-06-03 17:20:16 +0100 | [diff] [blame] | 418 | HalResult<milliseconds> AidlHalWrapper::getPrimitiveDelayMaxInternal() { |
| 419 | int32_t delay = 0; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 420 | auto status = getHal()->getCompositionDelayMax(&delay); |
| 421 | return HalResultFactory::fromStatus<milliseconds>(std::move(status), milliseconds(delay)); |
Lais Andrade | 4aa80c9 | 2021-06-03 17:20:16 +0100 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | HalResult<milliseconds> AidlHalWrapper::getPrimitiveDurationMaxInternal() { |
| 425 | int32_t delay = 0; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 426 | auto status = getHal()->getPwlePrimitiveDurationMax(&delay); |
| 427 | return HalResultFactory::fromStatus<milliseconds>(std::move(status), milliseconds(delay)); |
Lais Andrade | 4aa80c9 | 2021-06-03 17:20:16 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | HalResult<int32_t> AidlHalWrapper::getCompositionSizeMaxInternal() { |
| 431 | int32_t size = 0; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 432 | auto status = getHal()->getCompositionSizeMax(&size); |
| 433 | return HalResultFactory::fromStatus<int32_t>(std::move(status), size); |
Lais Andrade | 4aa80c9 | 2021-06-03 17:20:16 +0100 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | HalResult<int32_t> AidlHalWrapper::getPwleSizeMaxInternal() { |
| 437 | int32_t size = 0; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 438 | auto status = getHal()->getPwleCompositionSizeMax(&size); |
| 439 | return HalResultFactory::fromStatus<int32_t>(std::move(status), size); |
Lais Andrade | 4aa80c9 | 2021-06-03 17:20:16 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 442 | HalResult<float> AidlHalWrapper::getMinFrequencyInternal() { |
| 443 | float minFrequency = 0; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 444 | auto status = getHal()->getFrequencyMinimum(&minFrequency); |
| 445 | return HalResultFactory::fromStatus<float>(std::move(status), minFrequency); |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Michael Wright | ba9a6ce | 2021-02-26 02:55:29 +0000 | [diff] [blame] | 448 | HalResult<float> AidlHalWrapper::getResonantFrequencyInternal() { |
| 449 | float f0 = 0; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 450 | auto status = getHal()->getResonantFrequency(&f0); |
| 451 | return HalResultFactory::fromStatus<float>(std::move(status), f0); |
Michael Wright | ba9a6ce | 2021-02-26 02:55:29 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 454 | HalResult<float> AidlHalWrapper::getFrequencyResolutionInternal() { |
| 455 | float frequencyResolution = 0; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 456 | auto status = getHal()->getFrequencyResolution(&frequencyResolution); |
| 457 | return HalResultFactory::fromStatus<float>(std::move(status), frequencyResolution); |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Michael Wright | ba9a6ce | 2021-02-26 02:55:29 +0000 | [diff] [blame] | 460 | HalResult<float> AidlHalWrapper::getQFactorInternal() { |
| 461 | float qFactor = 0; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 462 | auto status = getHal()->getQFactor(&qFactor); |
| 463 | return HalResultFactory::fromStatus<float>(std::move(status), qFactor); |
Michael Wright | ba9a6ce | 2021-02-26 02:55:29 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 466 | HalResult<std::vector<float>> AidlHalWrapper::getMaxAmplitudesInternal() { |
| 467 | std::vector<float> amplitudes; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 468 | auto status = getHal()->getBandwidthAmplitudeMap(&litudes); |
| 469 | return HalResultFactory::fromStatus<std::vector<float>>(std::move(status), amplitudes); |
Lais Andrade | 92f2af5 | 2021-03-22 16:12:50 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Ahmad Khalil | 193e37b | 2024-09-02 10:05:50 +0000 | [diff] [blame^] | 472 | HalResult<int32_t> AidlHalWrapper::getMaxEnvelopeEffectSizeInternal() { |
| 473 | int32_t size = 0; |
| 474 | auto status = getHal()->getPwleV2CompositionSizeMax(&size); |
| 475 | return HalResultFactory::fromStatus<int32_t>(std::move(status), size); |
| 476 | } |
| 477 | |
| 478 | HalResult<milliseconds> AidlHalWrapper::getMinEnvelopeEffectControlPointDurationInternal() { |
| 479 | int32_t durationMs = 0; |
| 480 | auto status = getHal()->getPwleV2PrimitiveDurationMinMillis(&durationMs); |
| 481 | return HalResultFactory::fromStatus<milliseconds>(std::move(status), milliseconds(durationMs)); |
| 482 | } |
| 483 | |
| 484 | HalResult<milliseconds> AidlHalWrapper::getMaxEnvelopeEffectControlPointDurationInternal() { |
| 485 | int32_t durationMs = 0; |
| 486 | auto status = getHal()->getPwleV2PrimitiveDurationMaxMillis(&durationMs); |
| 487 | return HalResultFactory::fromStatus<milliseconds>(std::move(status), milliseconds(durationMs)); |
| 488 | } |
| 489 | |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 490 | std::shared_ptr<Aidl::IVibrator> AidlHalWrapper::getHal() { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 491 | std::lock_guard<std::mutex> lock(mHandleMutex); |
| 492 | return mHandle; |
| 493 | } |
| 494 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 495 | // ------------------------------------------------------------------------------------------------- |
| 496 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 497 | template <typename I> |
| 498 | HalResult<void> HidlHalWrapper<I>::ping() { |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 499 | return HalResultFactory::fromReturn(getHal()->ping()); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 502 | template <typename I> |
| 503 | void HidlHalWrapper<I>::tryReconnect() { |
| 504 | sp<I> newHandle = I::tryGetService(); |
| 505 | if (newHandle) { |
| 506 | std::lock_guard<std::mutex> lock(mHandleMutex); |
| 507 | mHandle = std::move(newHandle); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | template <typename I> |
| 512 | HalResult<void> HidlHalWrapper<I>::on(milliseconds timeout, |
| 513 | const std::function<void()>& completionCallback) { |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 514 | auto status = getHal()->on(timeout.count()); |
| 515 | auto ret = HalResultFactory::fromStatus(status.withDefault(V1_0::Status::UNKNOWN_ERROR)); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 516 | if (ret.isOk()) { |
| 517 | mCallbackScheduler->schedule(completionCallback, timeout); |
| 518 | } |
| 519 | return ret; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 520 | } |
| 521 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 522 | template <typename I> |
| 523 | HalResult<void> HidlHalWrapper<I>::off() { |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 524 | auto status = getHal()->off(); |
| 525 | return HalResultFactory::fromStatus(status.withDefault(V1_0::Status::UNKNOWN_ERROR)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 526 | } |
| 527 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 528 | template <typename I> |
Lais Andrade | 4e2b2d4 | 2021-02-15 20:58:51 +0000 | [diff] [blame] | 529 | HalResult<void> HidlHalWrapper<I>::setAmplitude(float amplitude) { |
| 530 | uint8_t amp = static_cast<uint8_t>(amplitude * std::numeric_limits<uint8_t>::max()); |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 531 | auto status = getHal()->setAmplitude(amp); |
| 532 | return HalResultFactory::fromStatus(status.withDefault(V1_0::Status::UNKNOWN_ERROR)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 533 | } |
| 534 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 535 | template <typename I> |
| 536 | HalResult<void> HidlHalWrapper<I>::setExternalControl(bool) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 537 | ALOGV("Skipped setExternalControl because Vibrator HAL does not support it"); |
| 538 | return HalResult<void>::unsupported(); |
| 539 | } |
| 540 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 541 | template <typename I> |
| 542 | HalResult<void> HidlHalWrapper<I>::alwaysOnEnable(int32_t, Effect, EffectStrength) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 543 | ALOGV("Skipped alwaysOnEnable because Vibrator HAL AIDL is not available"); |
| 544 | return HalResult<void>::unsupported(); |
| 545 | } |
| 546 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 547 | template <typename I> |
| 548 | HalResult<void> HidlHalWrapper<I>::alwaysOnDisable(int32_t) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 549 | ALOGV("Skipped alwaysOnDisable because Vibrator HAL AIDL is not available"); |
| 550 | return HalResult<void>::unsupported(); |
| 551 | } |
| 552 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 553 | template <typename I> |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 554 | HalResult<Capabilities> HidlHalWrapper<I>::getCapabilitiesInternal() { |
| 555 | hardware::Return<bool> result = getHal()->supportsAmplitudeControl(); |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 556 | Capabilities capabilities = |
| 557 | result.withDefault(false) ? Capabilities::AMPLITUDE_CONTROL : Capabilities::NONE; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 558 | return HalResultFactory::fromReturn<Capabilities>(std::move(result), capabilities); |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 561 | template <typename I> |
| 562 | template <typename T> |
| 563 | HalResult<milliseconds> HidlHalWrapper<I>::performInternal( |
| 564 | perform_fn<T> performFn, sp<I> handle, T effect, EffectStrength strength, |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 565 | const std::function<void()>& completionCallback) { |
| 566 | V1_0::Status status; |
| 567 | int32_t lengthMs; |
| 568 | auto effectCallback = [&status, &lengthMs](V1_0::Status retStatus, uint32_t retLengthMs) { |
| 569 | status = retStatus; |
| 570 | lengthMs = retLengthMs; |
| 571 | }; |
| 572 | |
| 573 | V1_0::EffectStrength effectStrength = static_cast<V1_0::EffectStrength>(strength); |
| 574 | auto result = std::invoke(performFn, handle, effect, effectStrength, effectCallback); |
| 575 | milliseconds length = milliseconds(lengthMs); |
| 576 | |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 577 | auto ret = HalResultFactory::fromReturn<milliseconds>(std::move(result), status, length); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 578 | if (ret.isOk()) { |
| 579 | mCallbackScheduler->schedule(completionCallback, length); |
| 580 | } |
| 581 | |
| 582 | return ret; |
| 583 | } |
| 584 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 585 | template <typename I> |
| 586 | sp<I> HidlHalWrapper<I>::getHal() { |
| 587 | std::lock_guard<std::mutex> lock(mHandleMutex); |
| 588 | return mHandle; |
| 589 | } |
| 590 | |
| 591 | // ------------------------------------------------------------------------------------------------- |
| 592 | |
| 593 | HalResult<milliseconds> HidlHalWrapperV1_0::performEffect( |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 594 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 595 | if (isStaticCastValid<V1_0::Effect>(effect)) { |
| 596 | return performInternal(&V1_0::IVibrator::perform, getHal(), |
| 597 | static_cast<V1_0::Effect>(effect), strength, completionCallback); |
| 598 | } |
| 599 | |
| 600 | ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", |
| 601 | Aidl::toString(effect).c_str()); |
| 602 | return HalResult<milliseconds>::unsupported(); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 605 | // ------------------------------------------------------------------------------------------------- |
| 606 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 607 | HalResult<milliseconds> HidlHalWrapperV1_1::performEffect( |
| 608 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 609 | if (isStaticCastValid<V1_0::Effect>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 610 | return performInternal(&V1_1::IVibrator::perform, getHal(), |
| 611 | static_cast<V1_0::Effect>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 612 | } |
| 613 | if (isStaticCastValid<V1_1::Effect_1_1>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 614 | return performInternal(&V1_1::IVibrator::perform_1_1, getHal(), |
| 615 | static_cast<V1_1::Effect_1_1>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", |
| 619 | Aidl::toString(effect).c_str()); |
| 620 | return HalResult<milliseconds>::unsupported(); |
| 621 | } |
| 622 | |
| 623 | // ------------------------------------------------------------------------------------------------- |
| 624 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 625 | HalResult<milliseconds> HidlHalWrapperV1_2::performEffect( |
| 626 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 627 | if (isStaticCastValid<V1_0::Effect>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 628 | return performInternal(&V1_2::IVibrator::perform, getHal(), |
| 629 | static_cast<V1_0::Effect>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 630 | } |
| 631 | if (isStaticCastValid<V1_1::Effect_1_1>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 632 | return performInternal(&V1_2::IVibrator::perform_1_1, getHal(), |
| 633 | static_cast<V1_1::Effect_1_1>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 634 | } |
| 635 | if (isStaticCastValid<V1_2::Effect>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 636 | return performInternal(&V1_2::IVibrator::perform_1_2, getHal(), |
| 637 | static_cast<V1_2::Effect>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", |
| 641 | Aidl::toString(effect).c_str()); |
| 642 | return HalResult<milliseconds>::unsupported(); |
| 643 | } |
| 644 | |
| 645 | // ------------------------------------------------------------------------------------------------- |
| 646 | |
| 647 | HalResult<void> HidlHalWrapperV1_3::setExternalControl(bool enabled) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 648 | auto result = getHal()->setExternalControl(static_cast<uint32_t>(enabled)); |
Lais Andrade | 641248e | 2024-02-16 17:49:36 +0000 | [diff] [blame] | 649 | return HalResultFactory::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 652 | HalResult<milliseconds> HidlHalWrapperV1_3::performEffect( |
| 653 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 654 | if (isStaticCastValid<V1_0::Effect>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 655 | return performInternal(&V1_3::IVibrator::perform, getHal(), |
| 656 | static_cast<V1_0::Effect>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 657 | } |
| 658 | if (isStaticCastValid<V1_1::Effect_1_1>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 659 | return performInternal(&V1_3::IVibrator::perform_1_1, getHal(), |
| 660 | static_cast<V1_1::Effect_1_1>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 661 | } |
| 662 | if (isStaticCastValid<V1_2::Effect>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 663 | return performInternal(&V1_3::IVibrator::perform_1_2, getHal(), |
| 664 | static_cast<V1_2::Effect>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 665 | } |
| 666 | if (isStaticCastValid<V1_3::Effect>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 667 | return performInternal(&V1_3::IVibrator::perform_1_3, getHal(), |
| 668 | static_cast<V1_3::Effect>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", |
| 672 | Aidl::toString(effect).c_str()); |
| 673 | return HalResult<milliseconds>::unsupported(); |
| 674 | } |
| 675 | |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 676 | HalResult<Capabilities> HidlHalWrapperV1_3::getCapabilitiesInternal() { |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame] | 677 | Capabilities capabilities = Capabilities::NONE; |
| 678 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 679 | sp<V1_3::IVibrator> hal = getHal(); |
| 680 | auto amplitudeResult = hal->supportsAmplitudeControl(); |
| 681 | if (!amplitudeResult.isOk()) { |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 682 | return HalResultFactory::fromReturn<Capabilities>(std::move(amplitudeResult), capabilities); |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 685 | auto externalControlResult = hal->supportsExternalControl(); |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 686 | if (amplitudeResult.withDefault(false)) { |
| 687 | capabilities |= Capabilities::AMPLITUDE_CONTROL; |
| 688 | } |
| 689 | if (externalControlResult.withDefault(false)) { |
| 690 | capabilities |= Capabilities::EXTERNAL_CONTROL; |
Lais Andrade | 602ff96 | 2020-08-27 12:02:53 +0000 | [diff] [blame] | 691 | |
| 692 | if (amplitudeResult.withDefault(false)) { |
| 693 | capabilities |= Capabilities::EXTERNAL_AMPLITUDE_CONTROL; |
| 694 | } |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 697 | return HalResultFactory::fromReturn<Capabilities>(std::move(externalControlResult), |
| 698 | capabilities); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 701 | // ------------------------------------------------------------------------------------------------- |
| 702 | |
| 703 | }; // namespace vibrator |
| 704 | |
| 705 | }; // namespace android |