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> |
| 20 | #include <android/hardware/vibrator/BnVibratorCallback.h> |
| 21 | #include <android/hardware/vibrator/IVibrator.h> |
| 22 | #include <hardware/vibrator.h> |
| 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 | |
| 29 | using android::hardware::vibrator::CompositeEffect; |
| 30 | using android::hardware::vibrator::Effect; |
| 31 | using android::hardware::vibrator::EffectStrength; |
| 32 | |
| 33 | using std::chrono::milliseconds; |
| 34 | |
| 35 | namespace V1_0 = android::hardware::vibrator::V1_0; |
| 36 | namespace V1_1 = android::hardware::vibrator::V1_1; |
| 37 | namespace V1_2 = android::hardware::vibrator::V1_2; |
| 38 | namespace V1_3 = android::hardware::vibrator::V1_3; |
| 39 | namespace Aidl = android::hardware::vibrator; |
| 40 | |
| 41 | namespace android { |
| 42 | |
| 43 | namespace vibrator { |
| 44 | |
| 45 | // ------------------------------------------------------------------------------------------------- |
| 46 | |
| 47 | template <class T> |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 48 | HalResult<T> loadCached(const std::function<HalResult<T>()>& loadFn, std::optional<T>& cache) { |
| 49 | if (cache.has_value()) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 50 | // Return copy of cached value. |
| 51 | return HalResult<T>::ok(*cache); |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 52 | } |
| 53 | HalResult<T> ret = loadFn(); |
| 54 | if (ret.isOk()) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 55 | // Cache copy of returned value. |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 56 | cache.emplace(ret.value()); |
| 57 | } |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | template <class T> |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 62 | bool isStaticCastValid(Effect effect) { |
| 63 | T castEffect = static_cast<T>(effect); |
| 64 | auto iter = hardware::hidl_enum_range<T>(); |
| 65 | return castEffect >= *iter.begin() && castEffect <= *std::prev(iter.end()); |
| 66 | } |
| 67 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 68 | // ------------------------------------------------------------------------------------------------- |
| 69 | |
| 70 | template <typename T> |
| 71 | HalResult<T> HalResult<T>::ok(T value) { |
| 72 | return HalResult(value); |
| 73 | } |
| 74 | |
| 75 | template <typename T> |
| 76 | HalResult<T> HalResult<T>::failed() { |
| 77 | return HalResult(/* unsupported= */ false); |
| 78 | } |
| 79 | |
| 80 | template <typename T> |
| 81 | HalResult<T> HalResult<T>::unsupported() { |
| 82 | return HalResult(/* unsupported= */ true); |
| 83 | } |
| 84 | |
| 85 | template <typename T> |
| 86 | HalResult<T> HalResult<T>::fromStatus(binder::Status status, T data) { |
| 87 | if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
| 88 | return HalResult<T>::unsupported(); |
| 89 | } |
| 90 | if (status.isOk()) { |
| 91 | return HalResult<T>::ok(data); |
| 92 | } |
| 93 | return HalResult<T>::failed(); |
| 94 | } |
| 95 | |
| 96 | template <typename T> |
| 97 | HalResult<T> HalResult<T>::fromStatus(V1_0::Status status, T data) { |
| 98 | switch (status) { |
| 99 | case V1_0::Status::OK: |
| 100 | return HalResult<T>::ok(data); |
| 101 | case V1_0::Status::UNSUPPORTED_OPERATION: |
| 102 | return HalResult<T>::unsupported(); |
| 103 | default: |
| 104 | return HalResult<T>::failed(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | template <typename T> |
| 109 | template <typename R> |
| 110 | HalResult<T> HalResult<T>::fromReturn(hardware::Return<R>& ret, T data) { |
| 111 | return ret.isOk() ? HalResult<T>::ok(data) : HalResult<T>::failed(); |
| 112 | } |
| 113 | |
| 114 | template <typename T> |
| 115 | template <typename R> |
| 116 | HalResult<T> HalResult<T>::fromReturn(hardware::Return<R>& ret, V1_0::Status status, T data) { |
| 117 | return ret.isOk() ? HalResult<T>::fromStatus(status, data) : HalResult<T>::failed(); |
| 118 | } |
| 119 | |
| 120 | // ------------------------------------------------------------------------------------------------- |
| 121 | |
| 122 | HalResult<void> HalResult<void>::ok() { |
| 123 | return HalResult(); |
| 124 | } |
| 125 | |
| 126 | HalResult<void> HalResult<void>::failed() { |
| 127 | return HalResult(/* failed= */ true); |
| 128 | } |
| 129 | |
| 130 | HalResult<void> HalResult<void>::unsupported() { |
| 131 | return HalResult(/* failed= */ false, /* unsupported= */ true); |
| 132 | } |
| 133 | |
| 134 | HalResult<void> HalResult<void>::fromStatus(binder::Status status) { |
| 135 | if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
| 136 | return HalResult<void>::unsupported(); |
| 137 | } |
| 138 | if (status.isOk()) { |
| 139 | return HalResult<void>::ok(); |
| 140 | } |
| 141 | return HalResult<void>::failed(); |
| 142 | } |
| 143 | |
| 144 | HalResult<void> HalResult<void>::fromStatus(V1_0::Status status) { |
| 145 | switch (status) { |
| 146 | case V1_0::Status::OK: |
| 147 | return HalResult<void>::ok(); |
| 148 | case V1_0::Status::UNSUPPORTED_OPERATION: |
| 149 | return HalResult<void>::unsupported(); |
| 150 | default: |
| 151 | return HalResult<void>::failed(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | template <typename R> |
| 156 | HalResult<void> HalResult<void>::fromReturn(hardware::Return<R>& ret) { |
| 157 | return ret.isOk() ? HalResult<void>::ok() : HalResult<void>::failed(); |
| 158 | } |
| 159 | |
| 160 | // ------------------------------------------------------------------------------------------------- |
| 161 | |
| 162 | class HalCallbackWrapper : public Aidl::BnVibratorCallback { |
| 163 | public: |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 164 | HalCallbackWrapper(std::function<void()> completionCallback) |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 165 | : mCompletionCallback(completionCallback) {} |
| 166 | |
| 167 | binder::Status onComplete() override { |
| 168 | mCompletionCallback(); |
| 169 | return binder::Status::ok(); |
| 170 | } |
| 171 | |
| 172 | private: |
| 173 | const std::function<void()> mCompletionCallback; |
| 174 | }; |
| 175 | |
| 176 | // ------------------------------------------------------------------------------------------------- |
| 177 | |
| 178 | HalResult<void> AidlHalWrapper::ping() { |
| 179 | return IInterface::asBinder(mHandle)->pingBinder() ? HalResult<void>::ok() |
| 180 | : HalResult<void>::failed(); |
| 181 | } |
| 182 | |
| 183 | HalResult<void> AidlHalWrapper::on(milliseconds timeout, |
| 184 | const std::function<void()>& completionCallback) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 185 | HalResult<Capabilities> capabilities = getCapabilities(); |
| 186 | bool supportsCallback = capabilities.isOk() && |
| 187 | static_cast<int32_t>(capabilities.value() & Capabilities::ON_CALLBACK); |
| 188 | auto cb = supportsCallback ? new HalCallbackWrapper(completionCallback) : nullptr; |
| 189 | |
| 190 | auto ret = HalResult<void>::fromStatus(mHandle->on(timeout.count(), cb)); |
| 191 | if (!supportsCallback && ret.isOk()) { |
| 192 | mCallbackScheduler->schedule(completionCallback, timeout); |
| 193 | } |
| 194 | |
| 195 | return ret; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | HalResult<void> AidlHalWrapper::off() { |
| 199 | return HalResult<void>::fromStatus(mHandle->off()); |
| 200 | } |
| 201 | |
| 202 | HalResult<void> AidlHalWrapper::setAmplitude(int32_t amplitude) { |
| 203 | float convertedAmplitude = static_cast<float>(amplitude) / std::numeric_limits<uint8_t>::max(); |
| 204 | return HalResult<void>::fromStatus(mHandle->setAmplitude(convertedAmplitude)); |
| 205 | } |
| 206 | |
| 207 | HalResult<void> AidlHalWrapper::setExternalControl(bool enabled) { |
| 208 | return HalResult<void>::fromStatus(mHandle->setExternalControl(enabled)); |
| 209 | } |
| 210 | |
| 211 | HalResult<void> AidlHalWrapper::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) { |
| 212 | return HalResult<void>::fromStatus(mHandle->alwaysOnEnable(id, effect, strength)); |
| 213 | } |
| 214 | |
| 215 | HalResult<void> AidlHalWrapper::alwaysOnDisable(int32_t id) { |
| 216 | return HalResult<void>::fromStatus(mHandle->alwaysOnDisable(id)); |
| 217 | } |
| 218 | |
| 219 | HalResult<Capabilities> AidlHalWrapper::getCapabilities() { |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 220 | std::lock_guard<std::mutex> lock(mCapabilitiesMutex); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 221 | return loadCached<Capabilities>(std::bind(&AidlHalWrapper::getCapabilitiesInternal, this), |
| 222 | mCapabilities); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | HalResult<std::vector<Effect>> AidlHalWrapper::getSupportedEffects() { |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 226 | std::lock_guard<std::mutex> lock(mSupportedEffectsMutex); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 227 | return loadCached<std::vector<Effect>>(std::bind(&AidlHalWrapper::getSupportedEffectsInternal, |
| 228 | this), |
| 229 | mSupportedEffects); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | HalResult<milliseconds> AidlHalWrapper::performEffect( |
| 233 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 234 | HalResult<Capabilities> capabilities = getCapabilities(); |
| 235 | bool supportsCallback = capabilities.isOk() && |
| 236 | static_cast<int32_t>(capabilities.value() & Capabilities::PERFORM_CALLBACK); |
| 237 | auto cb = supportsCallback ? new HalCallbackWrapper(completionCallback) : nullptr; |
| 238 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 239 | int32_t lengthMs; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 240 | auto result = mHandle->perform(effect, strength, cb, &lengthMs); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 241 | milliseconds length = milliseconds(lengthMs); |
| 242 | |
| 243 | auto ret = HalResult<milliseconds>::fromStatus(result, length); |
| 244 | if (!supportsCallback && ret.isOk()) { |
| 245 | mCallbackScheduler->schedule(completionCallback, length); |
| 246 | } |
| 247 | |
| 248 | return ret; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | HalResult<void> AidlHalWrapper::performComposedEffect( |
| 252 | const std::vector<CompositeEffect>& primitiveEffects, |
| 253 | const std::function<void()>& completionCallback) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 254 | // This method should always support callbacks, so no need to double check. |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 255 | auto cb = new HalCallbackWrapper(completionCallback); |
| 256 | return HalResult<void>::fromStatus(mHandle->compose(primitiveEffects, cb)); |
| 257 | } |
| 258 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 259 | HalResult<Capabilities> AidlHalWrapper::getCapabilitiesInternal() { |
| 260 | int32_t capabilities = 0; |
| 261 | auto result = mHandle->getCapabilities(&capabilities); |
| 262 | return HalResult<Capabilities>::fromStatus(result, static_cast<Capabilities>(capabilities)); |
| 263 | } |
| 264 | |
| 265 | HalResult<std::vector<Effect>> AidlHalWrapper::getSupportedEffectsInternal() { |
| 266 | std::vector<Effect> supportedEffects; |
| 267 | auto result = mHandle->getSupportedEffects(&supportedEffects); |
| 268 | return HalResult<std::vector<Effect>>::fromStatus(result, supportedEffects); |
| 269 | } |
| 270 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 271 | // ------------------------------------------------------------------------------------------------- |
| 272 | |
| 273 | HalResult<void> HidlHalWrapperV1_0::ping() { |
| 274 | auto result = mHandleV1_0->ping(); |
| 275 | return HalResult<void>::fromReturn(result); |
| 276 | } |
| 277 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 278 | HalResult<void> HidlHalWrapperV1_0::on(milliseconds timeout, |
| 279 | const std::function<void()>& completionCallback) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 280 | auto result = mHandleV1_0->on(timeout.count()); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 281 | auto ret = HalResult<void>::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); |
| 282 | if (ret.isOk()) { |
| 283 | mCallbackScheduler->schedule(completionCallback, timeout); |
| 284 | } |
| 285 | return ret; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | HalResult<void> HidlHalWrapperV1_0::off() { |
| 289 | auto result = mHandleV1_0->off(); |
| 290 | return HalResult<void>::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); |
| 291 | } |
| 292 | |
| 293 | HalResult<void> HidlHalWrapperV1_0::setAmplitude(int32_t amplitude) { |
| 294 | auto result = mHandleV1_0->setAmplitude(static_cast<uint8_t>(amplitude)); |
| 295 | return HalResult<void>::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); |
| 296 | } |
| 297 | |
| 298 | HalResult<void> HidlHalWrapperV1_0::setExternalControl(bool) { |
| 299 | ALOGV("Skipped setExternalControl because Vibrator HAL does not support it"); |
| 300 | return HalResult<void>::unsupported(); |
| 301 | } |
| 302 | |
| 303 | HalResult<void> HidlHalWrapperV1_0::alwaysOnEnable(int32_t, Effect, EffectStrength) { |
| 304 | ALOGV("Skipped alwaysOnEnable because Vibrator HAL AIDL is not available"); |
| 305 | return HalResult<void>::unsupported(); |
| 306 | } |
| 307 | |
| 308 | HalResult<void> HidlHalWrapperV1_0::alwaysOnDisable(int32_t) { |
| 309 | ALOGV("Skipped alwaysOnDisable because Vibrator HAL AIDL is not available"); |
| 310 | return HalResult<void>::unsupported(); |
| 311 | } |
| 312 | |
| 313 | HalResult<Capabilities> HidlHalWrapperV1_0::getCapabilities() { |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 314 | std::lock_guard<std::mutex> lock(mCapabilitiesMutex); |
| 315 | return loadCached<Capabilities>(std::bind(&HidlHalWrapperV1_0::getCapabilitiesInternal, this), |
| 316 | mCapabilities); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | HalResult<std::vector<Effect>> HidlHalWrapperV1_0::getSupportedEffects() { |
| 320 | ALOGV("Skipped getSupportedEffects because Vibrator HAL AIDL is not available"); |
| 321 | return HalResult<std::vector<Effect>>::unsupported(); |
| 322 | } |
| 323 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 324 | HalResult<milliseconds> HidlHalWrapperV1_0::performEffect( |
| 325 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 326 | if (isStaticCastValid<V1_0::Effect>(effect)) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 327 | return performInternalV1_0(effect, strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", |
| 331 | Aidl::toString(effect).c_str()); |
| 332 | return HalResult<milliseconds>::unsupported(); |
| 333 | } |
| 334 | |
| 335 | HalResult<void> HidlHalWrapperV1_0::performComposedEffect(const std::vector<CompositeEffect>&, |
| 336 | const std::function<void()>&) { |
| 337 | ALOGV("Skipped composed effect because Vibrator HAL AIDL is not available"); |
| 338 | return HalResult<void>::unsupported(); |
| 339 | } |
| 340 | |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 341 | HalResult<Capabilities> HidlHalWrapperV1_0::getCapabilitiesInternal() { |
| 342 | hardware::Return<bool> result = mHandleV1_0->supportsAmplitudeControl(); |
| 343 | Capabilities capabilities = |
| 344 | result.withDefault(false) ? Capabilities::AMPLITUDE_CONTROL : Capabilities::NONE; |
| 345 | return HalResult<Capabilities>::fromReturn(result, capabilities); |
| 346 | } |
| 347 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 348 | template <class I, class T> |
| 349 | HalResult<milliseconds> HidlHalWrapperV1_0::performInternal( |
| 350 | perform_fn<I, T> performFn, sp<I> handle, T effect, EffectStrength strength, |
| 351 | const std::function<void()>& completionCallback) { |
| 352 | V1_0::Status status; |
| 353 | int32_t lengthMs; |
| 354 | auto effectCallback = [&status, &lengthMs](V1_0::Status retStatus, uint32_t retLengthMs) { |
| 355 | status = retStatus; |
| 356 | lengthMs = retLengthMs; |
| 357 | }; |
| 358 | |
| 359 | V1_0::EffectStrength effectStrength = static_cast<V1_0::EffectStrength>(strength); |
| 360 | auto result = std::invoke(performFn, handle, effect, effectStrength, effectCallback); |
| 361 | milliseconds length = milliseconds(lengthMs); |
| 362 | |
| 363 | auto ret = HalResult<milliseconds>::fromReturn(result, status, length); |
| 364 | if (ret.isOk()) { |
| 365 | mCallbackScheduler->schedule(completionCallback, length); |
| 366 | } |
| 367 | |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | HalResult<milliseconds> HidlHalWrapperV1_0::performInternalV1_0( |
| 372 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
| 373 | V1_0::Effect e = static_cast<V1_0::Effect>(effect); |
| 374 | return performInternal(&V1_0::IVibrator::perform, mHandleV1_0, e, strength, completionCallback); |
| 375 | } |
| 376 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 377 | // ------------------------------------------------------------------------------------------------- |
| 378 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 379 | HalResult<milliseconds> HidlHalWrapperV1_1::performEffect( |
| 380 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 381 | if (isStaticCastValid<V1_0::Effect>(effect)) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 382 | return performInternalV1_0(effect, strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 383 | } |
| 384 | if (isStaticCastValid<V1_1::Effect_1_1>(effect)) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 385 | return performInternalV1_1(effect, strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", |
| 389 | Aidl::toString(effect).c_str()); |
| 390 | return HalResult<milliseconds>::unsupported(); |
| 391 | } |
| 392 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 393 | HalResult<milliseconds> HidlHalWrapperV1_1::performInternalV1_1( |
| 394 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
| 395 | V1_1::Effect_1_1 e = static_cast<V1_1::Effect_1_1>(effect); |
| 396 | return performInternal(&V1_1::IVibrator::perform_1_1, mHandleV1_1, e, strength, |
| 397 | completionCallback); |
| 398 | } |
| 399 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 400 | // ------------------------------------------------------------------------------------------------- |
| 401 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 402 | HalResult<milliseconds> HidlHalWrapperV1_2::performEffect( |
| 403 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 404 | if (isStaticCastValid<V1_0::Effect>(effect)) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 405 | return performInternalV1_0(effect, strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 406 | } |
| 407 | if (isStaticCastValid<V1_1::Effect_1_1>(effect)) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 408 | return performInternalV1_1(effect, strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 409 | } |
| 410 | if (isStaticCastValid<V1_2::Effect>(effect)) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 411 | return performInternalV1_2(effect, strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", |
| 415 | Aidl::toString(effect).c_str()); |
| 416 | return HalResult<milliseconds>::unsupported(); |
| 417 | } |
| 418 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 419 | HalResult<milliseconds> HidlHalWrapperV1_2::performInternalV1_2( |
| 420 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
| 421 | V1_2::Effect e = static_cast<V1_2::Effect>(effect); |
| 422 | return performInternal(&V1_2::IVibrator::perform_1_2, mHandleV1_2, e, strength, |
| 423 | completionCallback); |
| 424 | } |
| 425 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 426 | // ------------------------------------------------------------------------------------------------- |
| 427 | |
| 428 | HalResult<void> HidlHalWrapperV1_3::setExternalControl(bool enabled) { |
| 429 | auto result = mHandleV1_3->setExternalControl(static_cast<uint32_t>(enabled)); |
| 430 | return HalResult<void>::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); |
| 431 | } |
| 432 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 433 | HalResult<milliseconds> HidlHalWrapperV1_3::performEffect( |
| 434 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 435 | if (isStaticCastValid<V1_0::Effect>(effect)) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 436 | return performInternalV1_0(effect, strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 437 | } |
| 438 | if (isStaticCastValid<V1_1::Effect_1_1>(effect)) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 439 | return performInternalV1_1(effect, strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 440 | } |
| 441 | if (isStaticCastValid<V1_2::Effect>(effect)) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 442 | return performInternalV1_2(effect, strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 443 | } |
| 444 | if (isStaticCastValid<V1_3::Effect>(effect)) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 445 | return performInternalV1_3(effect, strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", |
| 449 | Aidl::toString(effect).c_str()); |
| 450 | return HalResult<milliseconds>::unsupported(); |
| 451 | } |
| 452 | |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 453 | HalResult<Capabilities> HidlHalWrapperV1_3::getCapabilitiesInternal() { |
| 454 | HalResult<Capabilities> parentResult = HidlHalWrapperV1_2::getCapabilitiesInternal(); |
| 455 | if (!parentResult.isOk()) { |
| 456 | // Loading for previous HAL versions already failed, so propagate failure. |
| 457 | return parentResult; |
| 458 | } |
| 459 | |
| 460 | Capabilities capabilities = parentResult.value(); |
| 461 | auto result = mHandleV1_3->supportsExternalControl(); |
| 462 | capabilities |= result.withDefault(false) ? Capabilities::EXTERNAL_CONTROL : Capabilities::NONE; |
| 463 | return HalResult<Capabilities>::fromReturn(result, capabilities); |
| 464 | } |
| 465 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame^] | 466 | HalResult<milliseconds> HidlHalWrapperV1_3::performInternalV1_3( |
| 467 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
| 468 | V1_3::Effect e = static_cast<V1_3::Effect>(effect); |
| 469 | return performInternal(&V1_3::IVibrator::perform_1_3, mHandleV1_3, e, strength, |
| 470 | completionCallback); |
| 471 | } |
| 472 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 473 | // ------------------------------------------------------------------------------------------------- |
| 474 | |
| 475 | }; // namespace vibrator |
| 476 | |
| 477 | }; // namespace android |