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