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 #ifndef ANDROID_OS_VIBRATORHALWRAPPER_H 18 #define ANDROID_OS_VIBRATORHALWRAPPER_H 19 20 #include <android-base/thread_annotations.h> 21 #include <android/hardware/vibrator/1.3/IVibrator.h> 22 #include <android/hardware/vibrator/BnVibratorCallback.h> 23 #include <android/hardware/vibrator/IVibrator.h> 24 #include <binder/IServiceManager.h> 25 26 #include <vibratorservice/VibratorCallbackScheduler.h> 27 28 namespace android { 29 30 namespace vibrator { 31 32 // ------------------------------------------------------------------------------------------------- 33 34 // Base class to represent a generic result of a call to the Vibrator HAL wrapper. 35 class BaseHalResult { 36 public: isOk()37 bool isOk() const { return mStatus == SUCCESS; } isFailed()38 bool isFailed() const { return mStatus == FAILED; } isUnsupported()39 bool isUnsupported() const { return mStatus == UNSUPPORTED; } shouldRetry()40 bool shouldRetry() const { return isFailed() && mDeadObject; } errorMessage()41 const char* errorMessage() const { return mErrorMessage.c_str(); } 42 43 protected: 44 enum Status { SUCCESS, UNSUPPORTED, FAILED }; 45 Status mStatus; 46 std::string mErrorMessage; 47 bool mDeadObject; 48 49 explicit BaseHalResult(Status status, const char* errorMessage = "", bool deadObject = false) mStatus(status)50 : mStatus(status), mErrorMessage(errorMessage), mDeadObject(deadObject) {} 51 virtual ~BaseHalResult() = default; 52 }; 53 54 // Result of a call to the Vibrator HAL wrapper, holding data if successful. 55 template <typename T> 56 class HalResult : public BaseHalResult { 57 public: ok(T value)58 static HalResult<T> ok(T value) { return HalResult(value); } unsupported()59 static HalResult<T> unsupported() { return HalResult(Status::UNSUPPORTED); } failed(const char * msg)60 static HalResult<T> failed(const char* msg) { return HalResult(Status::FAILED, msg); } transactionFailed(const char * msg)61 static HalResult<T> transactionFailed(const char* msg) { 62 return HalResult(Status::FAILED, msg, /* deadObject= */ true); 63 } 64 65 // This will throw std::bad_optional_access if this result is not ok. value()66 const T& value() const { return mValue.value(); } valueOr(T && defaultValue)67 const T valueOr(T&& defaultValue) const { return mValue.value_or(defaultValue); } 68 69 private: 70 std::optional<T> mValue; 71 HalResult(T value)72 explicit HalResult(T value) 73 : BaseHalResult(Status::SUCCESS), mValue(std::make_optional(value)) {} 74 explicit HalResult(Status status, const char* errorMessage = "", bool deadObject = false) BaseHalResult(status,errorMessage,deadObject)75 : BaseHalResult(status, errorMessage, deadObject), mValue() {} 76 }; 77 78 // Empty result of a call to the Vibrator HAL wrapper. 79 template <> 80 class HalResult<void> : public BaseHalResult { 81 public: ok()82 static HalResult<void> ok() { return HalResult(Status::SUCCESS); } unsupported()83 static HalResult<void> unsupported() { return HalResult(Status::UNSUPPORTED); } failed(const char * msg)84 static HalResult<void> failed(const char* msg) { return HalResult(Status::FAILED, msg); } transactionFailed(const char * msg)85 static HalResult<void> transactionFailed(const char* msg) { 86 return HalResult(Status::FAILED, msg, /* deadObject= */ true); 87 } 88 89 private: 90 explicit HalResult(Status status, const char* errorMessage = "", bool deadObject = false) BaseHalResult(status,errorMessage,deadObject)91 : BaseHalResult(status, errorMessage, deadObject) {} 92 }; 93 94 // ------------------------------------------------------------------------------------------------- 95 96 // Factory functions that convert failed HIDL/AIDL results into HalResult instances. 97 // Implementation of static template functions needs to be in this header file for the linker. 98 class HalResultFactory { 99 public: 100 template <typename T> fromStatus(binder::Status status,T data)101 static HalResult<T> fromStatus(binder::Status status, T data) { 102 return status.isOk() ? HalResult<T>::ok(data) : fromFailedStatus<T>(status); 103 } 104 105 template <typename T> fromStatus(hardware::vibrator::V1_0::Status status,T data)106 static HalResult<T> fromStatus(hardware::vibrator::V1_0::Status status, T data) { 107 return (status == hardware::vibrator::V1_0::Status::OK) ? HalResult<T>::ok(data) 108 : fromFailedStatus<T>(status); 109 } 110 111 template <typename T, typename R> fromReturn(hardware::Return<R> & ret,T data)112 static HalResult<T> fromReturn(hardware::Return<R>& ret, T data) { 113 return ret.isOk() ? HalResult<T>::ok(data) : fromFailedReturn<T, R>(ret); 114 } 115 116 template <typename T, typename R> fromReturn(hardware::Return<R> & ret,hardware::vibrator::V1_0::Status status,T data)117 static HalResult<T> fromReturn(hardware::Return<R>& ret, 118 hardware::vibrator::V1_0::Status status, T data) { 119 return ret.isOk() ? fromStatus<T>(status, data) : fromFailedReturn<T, R>(ret); 120 } 121 fromStatus(status_t status)122 static HalResult<void> fromStatus(status_t status) { 123 return (status == android::OK) ? HalResult<void>::ok() : fromFailedStatus<void>(status); 124 } 125 fromStatus(binder::Status status)126 static HalResult<void> fromStatus(binder::Status status) { 127 return status.isOk() ? HalResult<void>::ok() : fromFailedStatus<void>(status); 128 } 129 fromStatus(hardware::vibrator::V1_0::Status status)130 static HalResult<void> fromStatus(hardware::vibrator::V1_0::Status status) { 131 return (status == hardware::vibrator::V1_0::Status::OK) ? HalResult<void>::ok() 132 : fromFailedStatus<void>(status); 133 } 134 135 template <typename R> fromReturn(hardware::Return<R> & ret)136 static HalResult<void> fromReturn(hardware::Return<R>& ret) { 137 return ret.isOk() ? HalResult<void>::ok() : fromFailedReturn<void, R>(ret); 138 } 139 140 private: 141 template <typename T> fromFailedStatus(status_t status)142 static HalResult<T> fromFailedStatus(status_t status) { 143 auto msg = "status_t = " + statusToString(status); 144 return (status == android::DEAD_OBJECT) ? HalResult<T>::transactionFailed(msg.c_str()) 145 : HalResult<T>::failed(msg.c_str()); 146 } 147 148 template <typename T> fromFailedStatus(binder::Status status)149 static HalResult<T> fromFailedStatus(binder::Status status) { 150 if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION || 151 status.transactionError() == android::UNKNOWN_TRANSACTION) { 152 // UNKNOWN_TRANSACTION means the HAL implementation is an older version, so this is 153 // the same as the operation being unsupported by this HAL. Should not retry. 154 return HalResult<T>::unsupported(); 155 } 156 if (status.exceptionCode() == binder::Status::EX_TRANSACTION_FAILED) { 157 return HalResult<T>::transactionFailed(status.toString8().c_str()); 158 } 159 return HalResult<T>::failed(status.toString8().c_str()); 160 } 161 162 template <typename T> fromFailedStatus(hardware::vibrator::V1_0::Status status)163 static HalResult<T> fromFailedStatus(hardware::vibrator::V1_0::Status status) { 164 switch (status) { 165 case hardware::vibrator::V1_0::Status::UNSUPPORTED_OPERATION: 166 return HalResult<T>::unsupported(); 167 default: 168 auto msg = "android::hardware::vibrator::V1_0::Status = " + toString(status); 169 return HalResult<T>::failed(msg.c_str()); 170 } 171 } 172 173 template <typename T, typename R> fromFailedReturn(hardware::Return<R> & ret)174 static HalResult<T> fromFailedReturn(hardware::Return<R>& ret) { 175 return ret.isDeadObject() ? HalResult<T>::transactionFailed(ret.description().c_str()) 176 : HalResult<T>::failed(ret.description().c_str()); 177 } 178 }; 179 180 // ------------------------------------------------------------------------------------------------- 181 182 class HalCallbackWrapper : public hardware::vibrator::BnVibratorCallback { 183 public: HalCallbackWrapper(std::function<void ()> completionCallback)184 HalCallbackWrapper(std::function<void()> completionCallback) 185 : mCompletionCallback(completionCallback) {} 186 onComplete()187 binder::Status onComplete() override { 188 mCompletionCallback(); 189 return binder::Status::ok(); 190 } 191 192 private: 193 const std::function<void()> mCompletionCallback; 194 }; 195 196 // ------------------------------------------------------------------------------------------------- 197 198 // Vibrator HAL capabilities. 199 enum class Capabilities : int32_t { 200 NONE = 0, 201 ON_CALLBACK = hardware::vibrator::IVibrator::CAP_ON_CALLBACK, 202 PERFORM_CALLBACK = hardware::vibrator::IVibrator::CAP_PERFORM_CALLBACK, 203 AMPLITUDE_CONTROL = hardware::vibrator::IVibrator::CAP_AMPLITUDE_CONTROL, 204 EXTERNAL_CONTROL = hardware::vibrator::IVibrator::CAP_EXTERNAL_CONTROL, 205 EXTERNAL_AMPLITUDE_CONTROL = hardware::vibrator::IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL, 206 COMPOSE_EFFECTS = hardware::vibrator::IVibrator::CAP_COMPOSE_EFFECTS, 207 COMPOSE_PWLE_EFFECTS = hardware::vibrator::IVibrator::CAP_COMPOSE_PWLE_EFFECTS, 208 ALWAYS_ON_CONTROL = hardware::vibrator::IVibrator::CAP_ALWAYS_ON_CONTROL, 209 }; 210 211 inline Capabilities operator|(Capabilities lhs, Capabilities rhs) { 212 using underlying = typename std::underlying_type<Capabilities>::type; 213 return static_cast<Capabilities>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs)); 214 } 215 216 inline Capabilities& operator|=(Capabilities& lhs, Capabilities rhs) { 217 return lhs = lhs | rhs; 218 } 219 220 inline Capabilities operator&(Capabilities lhs, Capabilities rhs) { 221 using underlying = typename std::underlying_type<Capabilities>::type; 222 return static_cast<Capabilities>(static_cast<underlying>(lhs) & static_cast<underlying>(rhs)); 223 } 224 225 inline Capabilities& operator&=(Capabilities& lhs, Capabilities rhs) { 226 return lhs = lhs & rhs; 227 } 228 229 // ------------------------------------------------------------------------------------------------- 230 231 class Info { 232 public: 233 const HalResult<Capabilities> capabilities; 234 const HalResult<std::vector<hardware::vibrator::Effect>> supportedEffects; 235 const HalResult<std::vector<hardware::vibrator::Braking>> supportedBraking; 236 const HalResult<std::vector<hardware::vibrator::CompositePrimitive>> supportedPrimitives; 237 const HalResult<std::vector<std::chrono::milliseconds>> primitiveDurations; 238 const HalResult<std::chrono::milliseconds> primitiveDelayMax; 239 const HalResult<std::chrono::milliseconds> pwlePrimitiveDurationMax; 240 const HalResult<int32_t> compositionSizeMax; 241 const HalResult<int32_t> pwleSizeMax; 242 const HalResult<float> minFrequency; 243 const HalResult<float> resonantFrequency; 244 const HalResult<float> frequencyResolution; 245 const HalResult<float> qFactor; 246 const HalResult<std::vector<float>> maxAmplitudes; 247 logFailures()248 void logFailures() const { 249 logFailure<Capabilities>(capabilities, "getCapabilities"); 250 logFailure<std::vector<hardware::vibrator::Effect>>(supportedEffects, 251 "getSupportedEffects"); 252 logFailure<std::vector<hardware::vibrator::Braking>>(supportedBraking, 253 "getSupportedBraking"); 254 logFailure<std::vector<hardware::vibrator::CompositePrimitive>>(supportedPrimitives, 255 "getSupportedPrimitives"); 256 logFailure<std::vector<std::chrono::milliseconds>>(primitiveDurations, 257 "getPrimitiveDuration"); 258 logFailure<std::chrono::milliseconds>(primitiveDelayMax, "getPrimitiveDelayMax"); 259 logFailure<std::chrono::milliseconds>(pwlePrimitiveDurationMax, 260 "getPwlePrimitiveDurationMax"); 261 logFailure<int32_t>(compositionSizeMax, "getCompositionSizeMax"); 262 logFailure<int32_t>(pwleSizeMax, "getPwleSizeMax"); 263 logFailure<float>(minFrequency, "getMinFrequency"); 264 logFailure<float>(resonantFrequency, "getResonantFrequency"); 265 logFailure<float>(frequencyResolution, "getFrequencyResolution"); 266 logFailure<float>(qFactor, "getQFactor"); 267 logFailure<std::vector<float>>(maxAmplitudes, "getMaxAmplitudes"); 268 } 269 shouldRetry()270 bool shouldRetry() const { 271 return capabilities.shouldRetry() || supportedEffects.shouldRetry() || 272 supportedBraking.shouldRetry() || supportedPrimitives.shouldRetry() || 273 primitiveDurations.shouldRetry() || primitiveDelayMax.shouldRetry() || 274 pwlePrimitiveDurationMax.shouldRetry() || compositionSizeMax.shouldRetry() || 275 pwleSizeMax.shouldRetry() || minFrequency.shouldRetry() || 276 resonantFrequency.shouldRetry() || frequencyResolution.shouldRetry() || 277 qFactor.shouldRetry() || maxAmplitudes.shouldRetry(); 278 } 279 280 private: 281 template <typename T> logFailure(HalResult<T> result,const char * functionName)282 void logFailure(HalResult<T> result, const char* functionName) const { 283 if (result.isFailed()) { 284 ALOGE("Vibrator HAL %s failed: %s", functionName, result.errorMessage()); 285 } 286 } 287 }; 288 289 class InfoCache { 290 public: get()291 Info get() { 292 return {mCapabilities, 293 mSupportedEffects, 294 mSupportedBraking, 295 mSupportedPrimitives, 296 mPrimitiveDurations, 297 mPrimitiveDelayMax, 298 mPwlePrimitiveDurationMax, 299 mCompositionSizeMax, 300 mPwleSizeMax, 301 mMinFrequency, 302 mResonantFrequency, 303 mFrequencyResolution, 304 mQFactor, 305 mMaxAmplitudes}; 306 } 307 308 private: 309 // Create a transaction failed results as default so we can retry on the first time we get them. 310 static const constexpr char* MSG = "never loaded"; 311 HalResult<Capabilities> mCapabilities = HalResult<Capabilities>::transactionFailed(MSG); 312 HalResult<std::vector<hardware::vibrator::Effect>> mSupportedEffects = 313 HalResult<std::vector<hardware::vibrator::Effect>>::transactionFailed(MSG); 314 HalResult<std::vector<hardware::vibrator::Braking>> mSupportedBraking = 315 HalResult<std::vector<hardware::vibrator::Braking>>::transactionFailed(MSG); 316 HalResult<std::vector<hardware::vibrator::CompositePrimitive>> mSupportedPrimitives = 317 HalResult<std::vector<hardware::vibrator::CompositePrimitive>>::transactionFailed(MSG); 318 HalResult<std::vector<std::chrono::milliseconds>> mPrimitiveDurations = 319 HalResult<std::vector<std::chrono::milliseconds>>::transactionFailed(MSG); 320 HalResult<std::chrono::milliseconds> mPrimitiveDelayMax = 321 HalResult<std::chrono::milliseconds>::transactionFailed(MSG); 322 HalResult<std::chrono::milliseconds> mPwlePrimitiveDurationMax = 323 HalResult<std::chrono::milliseconds>::transactionFailed(MSG); 324 HalResult<int32_t> mCompositionSizeMax = HalResult<int>::transactionFailed(MSG); 325 HalResult<int32_t> mPwleSizeMax = HalResult<int>::transactionFailed(MSG); 326 HalResult<float> mMinFrequency = HalResult<float>::transactionFailed(MSG); 327 HalResult<float> mResonantFrequency = HalResult<float>::transactionFailed(MSG); 328 HalResult<float> mFrequencyResolution = HalResult<float>::transactionFailed(MSG); 329 HalResult<float> mQFactor = HalResult<float>::transactionFailed(MSG); 330 HalResult<std::vector<float>> mMaxAmplitudes = 331 HalResult<std::vector<float>>::transactionFailed(MSG); 332 333 friend class HalWrapper; 334 }; 335 336 // Wrapper for Vibrator HAL handlers. 337 class HalWrapper { 338 public: HalWrapper(std::shared_ptr<CallbackScheduler> scheduler)339 explicit HalWrapper(std::shared_ptr<CallbackScheduler> scheduler) 340 : mCallbackScheduler(std::move(scheduler)) {} 341 virtual ~HalWrapper() = default; 342 343 /* reloads wrapped HAL service instance without waiting. This can be used to reconnect when the 344 * service restarts, to rapidly retry after a failure. 345 */ 346 virtual void tryReconnect() = 0; 347 348 Info getInfo(); 349 350 virtual HalResult<void> ping() = 0; 351 virtual HalResult<void> on(std::chrono::milliseconds timeout, 352 const std::function<void()>& completionCallback) = 0; 353 virtual HalResult<void> off() = 0; 354 355 virtual HalResult<void> setAmplitude(float amplitude) = 0; 356 virtual HalResult<void> setExternalControl(bool enabled) = 0; 357 358 virtual HalResult<void> alwaysOnEnable(int32_t id, hardware::vibrator::Effect effect, 359 hardware::vibrator::EffectStrength strength) = 0; 360 virtual HalResult<void> alwaysOnDisable(int32_t id) = 0; 361 362 virtual HalResult<std::chrono::milliseconds> performEffect( 363 hardware::vibrator::Effect effect, hardware::vibrator::EffectStrength strength, 364 const std::function<void()>& completionCallback) = 0; 365 366 virtual HalResult<std::chrono::milliseconds> performComposedEffect( 367 const std::vector<hardware::vibrator::CompositeEffect>& primitives, 368 const std::function<void()>& completionCallback); 369 370 virtual HalResult<void> performPwleEffect( 371 const std::vector<hardware::vibrator::PrimitivePwle>& primitives, 372 const std::function<void()>& completionCallback); 373 374 protected: 375 // Shared pointer to allow CallbackScheduler to outlive this wrapper. 376 const std::shared_ptr<CallbackScheduler> mCallbackScheduler; 377 378 // Load and cache vibrator info, returning cached result is present. 379 HalResult<Capabilities> getCapabilities(); 380 HalResult<std::vector<std::chrono::milliseconds>> getPrimitiveDurations(); 381 382 // Request vibrator info to HAL skipping cache. 383 virtual HalResult<Capabilities> getCapabilitiesInternal() = 0; 384 virtual HalResult<std::vector<hardware::vibrator::Effect>> getSupportedEffectsInternal(); 385 virtual HalResult<std::vector<hardware::vibrator::Braking>> getSupportedBrakingInternal(); 386 virtual HalResult<std::vector<hardware::vibrator::CompositePrimitive>> 387 getSupportedPrimitivesInternal(); 388 virtual HalResult<std::vector<std::chrono::milliseconds>> getPrimitiveDurationsInternal( 389 const std::vector<hardware::vibrator::CompositePrimitive>& supportedPrimitives); 390 virtual HalResult<std::chrono::milliseconds> getPrimitiveDelayMaxInternal(); 391 virtual HalResult<std::chrono::milliseconds> getPrimitiveDurationMaxInternal(); 392 virtual HalResult<int32_t> getCompositionSizeMaxInternal(); 393 virtual HalResult<int32_t> getPwleSizeMaxInternal(); 394 virtual HalResult<float> getMinFrequencyInternal(); 395 virtual HalResult<float> getResonantFrequencyInternal(); 396 virtual HalResult<float> getFrequencyResolutionInternal(); 397 virtual HalResult<float> getQFactorInternal(); 398 virtual HalResult<std::vector<float>> getMaxAmplitudesInternal(); 399 400 private: 401 std::mutex mInfoMutex; 402 InfoCache mInfoCache GUARDED_BY(mInfoMutex); 403 }; 404 405 // Wrapper for the AIDL Vibrator HAL. 406 class AidlHalWrapper : public HalWrapper { 407 public: 408 AidlHalWrapper( 409 std::shared_ptr<CallbackScheduler> scheduler, sp<hardware::vibrator::IVibrator> handle, 410 std::function<HalResult<sp<hardware::vibrator::IVibrator>>()> reconnectFn = 411 []() { 412 return HalResult<sp<hardware::vibrator::IVibrator>>::ok( 413 checkVintfService<hardware::vibrator::IVibrator>()); 414 }) HalWrapper(std::move (scheduler))415 : HalWrapper(std::move(scheduler)), 416 mReconnectFn(reconnectFn), 417 mHandle(std::move(handle)) {} 418 virtual ~AidlHalWrapper() = default; 419 420 HalResult<void> ping() override final; 421 void tryReconnect() override final; 422 423 HalResult<void> on(std::chrono::milliseconds timeout, 424 const std::function<void()>& completionCallback) override final; 425 HalResult<void> off() override final; 426 427 HalResult<void> setAmplitude(float amplitude) override final; 428 HalResult<void> setExternalControl(bool enabled) override final; 429 430 HalResult<void> alwaysOnEnable(int32_t id, hardware::vibrator::Effect effect, 431 hardware::vibrator::EffectStrength strength) override final; 432 HalResult<void> alwaysOnDisable(int32_t id) override final; 433 434 HalResult<std::chrono::milliseconds> performEffect( 435 hardware::vibrator::Effect effect, hardware::vibrator::EffectStrength strength, 436 const std::function<void()>& completionCallback) override final; 437 438 HalResult<std::chrono::milliseconds> performComposedEffect( 439 const std::vector<hardware::vibrator::CompositeEffect>& primitives, 440 const std::function<void()>& completionCallback) override final; 441 442 HalResult<void> performPwleEffect( 443 const std::vector<hardware::vibrator::PrimitivePwle>& primitives, 444 const std::function<void()>& completionCallback) override final; 445 446 protected: 447 HalResult<Capabilities> getCapabilitiesInternal() override final; 448 HalResult<std::vector<hardware::vibrator::Effect>> getSupportedEffectsInternal() override final; 449 HalResult<std::vector<hardware::vibrator::Braking>> getSupportedBrakingInternal() 450 override final; 451 HalResult<std::vector<hardware::vibrator::CompositePrimitive>> getSupportedPrimitivesInternal() 452 override final; 453 HalResult<std::vector<std::chrono::milliseconds>> getPrimitiveDurationsInternal( 454 const std::vector<hardware::vibrator::CompositePrimitive>& supportedPrimitives) 455 override final; 456 HalResult<std::chrono::milliseconds> getPrimitiveDelayMaxInternal() override final; 457 HalResult<std::chrono::milliseconds> getPrimitiveDurationMaxInternal() override final; 458 HalResult<int32_t> getCompositionSizeMaxInternal() override final; 459 HalResult<int32_t> getPwleSizeMaxInternal() override final; 460 HalResult<float> getMinFrequencyInternal() override final; 461 HalResult<float> getResonantFrequencyInternal() override final; 462 HalResult<float> getFrequencyResolutionInternal() override final; 463 HalResult<float> getQFactorInternal() override final; 464 HalResult<std::vector<float>> getMaxAmplitudesInternal() override final; 465 466 private: 467 const std::function<HalResult<sp<hardware::vibrator::IVibrator>>()> mReconnectFn; 468 std::mutex mHandleMutex; 469 sp<hardware::vibrator::IVibrator> mHandle GUARDED_BY(mHandleMutex); 470 471 sp<hardware::vibrator::IVibrator> getHal(); 472 }; 473 474 // Wrapper for the HDIL Vibrator HALs. 475 template <typename I> 476 class HidlHalWrapper : public HalWrapper { 477 public: HidlHalWrapper(std::shared_ptr<CallbackScheduler> scheduler,sp<I> handle)478 HidlHalWrapper(std::shared_ptr<CallbackScheduler> scheduler, sp<I> handle) 479 : HalWrapper(std::move(scheduler)), mHandle(std::move(handle)) {} 480 virtual ~HidlHalWrapper() = default; 481 482 HalResult<void> ping() override final; 483 void tryReconnect() override final; 484 485 HalResult<void> on(std::chrono::milliseconds timeout, 486 const std::function<void()>& completionCallback) override final; 487 HalResult<void> off() override final; 488 489 HalResult<void> setAmplitude(float amplitude) override final; 490 virtual HalResult<void> setExternalControl(bool enabled) override; 491 492 HalResult<void> alwaysOnEnable(int32_t id, hardware::vibrator::Effect effect, 493 hardware::vibrator::EffectStrength strength) override final; 494 HalResult<void> alwaysOnDisable(int32_t id) override final; 495 496 protected: 497 std::mutex mHandleMutex; 498 sp<I> mHandle GUARDED_BY(mHandleMutex); 499 500 virtual HalResult<Capabilities> getCapabilitiesInternal() override; 501 502 template <class T> 503 using perform_fn = 504 hardware::Return<void> (I::*)(T, hardware::vibrator::V1_0::EffectStrength, 505 hardware::vibrator::V1_0::IVibrator::perform_cb); 506 507 template <class T> 508 HalResult<std::chrono::milliseconds> performInternal( 509 perform_fn<T> performFn, sp<I> handle, T effect, 510 hardware::vibrator::EffectStrength strength, 511 const std::function<void()>& completionCallback); 512 513 sp<I> getHal(); 514 }; 515 516 // Wrapper for the HDIL Vibrator HAL v1.0. 517 class HidlHalWrapperV1_0 : public HidlHalWrapper<hardware::vibrator::V1_0::IVibrator> { 518 public: HidlHalWrapperV1_0(std::shared_ptr<CallbackScheduler> scheduler,sp<hardware::vibrator::V1_0::IVibrator> handle)519 HidlHalWrapperV1_0(std::shared_ptr<CallbackScheduler> scheduler, 520 sp<hardware::vibrator::V1_0::IVibrator> handle) 521 : HidlHalWrapper<hardware::vibrator::V1_0::IVibrator>(std::move(scheduler), 522 std::move(handle)) {} 523 virtual ~HidlHalWrapperV1_0() = default; 524 525 HalResult<std::chrono::milliseconds> performEffect( 526 hardware::vibrator::Effect effect, hardware::vibrator::EffectStrength strength, 527 const std::function<void()>& completionCallback) override final; 528 }; 529 530 // Wrapper for the HDIL Vibrator HAL v1.1. 531 class HidlHalWrapperV1_1 : public HidlHalWrapper<hardware::vibrator::V1_1::IVibrator> { 532 public: HidlHalWrapperV1_1(std::shared_ptr<CallbackScheduler> scheduler,sp<hardware::vibrator::V1_1::IVibrator> handle)533 HidlHalWrapperV1_1(std::shared_ptr<CallbackScheduler> scheduler, 534 sp<hardware::vibrator::V1_1::IVibrator> handle) 535 : HidlHalWrapper<hardware::vibrator::V1_1::IVibrator>(std::move(scheduler), 536 std::move(handle)) {} 537 virtual ~HidlHalWrapperV1_1() = default; 538 539 HalResult<std::chrono::milliseconds> performEffect( 540 hardware::vibrator::Effect effect, hardware::vibrator::EffectStrength strength, 541 const std::function<void()>& completionCallback) override final; 542 }; 543 544 // Wrapper for the HDIL Vibrator HAL v1.2. 545 class HidlHalWrapperV1_2 : public HidlHalWrapper<hardware::vibrator::V1_2::IVibrator> { 546 public: HidlHalWrapperV1_2(std::shared_ptr<CallbackScheduler> scheduler,sp<hardware::vibrator::V1_2::IVibrator> handle)547 HidlHalWrapperV1_2(std::shared_ptr<CallbackScheduler> scheduler, 548 sp<hardware::vibrator::V1_2::IVibrator> handle) 549 : HidlHalWrapper<hardware::vibrator::V1_2::IVibrator>(std::move(scheduler), 550 std::move(handle)) {} 551 virtual ~HidlHalWrapperV1_2() = default; 552 553 HalResult<std::chrono::milliseconds> performEffect( 554 hardware::vibrator::Effect effect, hardware::vibrator::EffectStrength strength, 555 const std::function<void()>& completionCallback) override final; 556 }; 557 558 // Wrapper for the HDIL Vibrator HAL v1.3. 559 class HidlHalWrapperV1_3 : public HidlHalWrapper<hardware::vibrator::V1_3::IVibrator> { 560 public: HidlHalWrapperV1_3(std::shared_ptr<CallbackScheduler> scheduler,sp<hardware::vibrator::V1_3::IVibrator> handle)561 HidlHalWrapperV1_3(std::shared_ptr<CallbackScheduler> scheduler, 562 sp<hardware::vibrator::V1_3::IVibrator> handle) 563 : HidlHalWrapper<hardware::vibrator::V1_3::IVibrator>(std::move(scheduler), 564 std::move(handle)) {} 565 virtual ~HidlHalWrapperV1_3() = default; 566 567 HalResult<void> setExternalControl(bool enabled) override final; 568 569 HalResult<std::chrono::milliseconds> performEffect( 570 hardware::vibrator::Effect effect, hardware::vibrator::EffectStrength strength, 571 const std::function<void()>& completionCallback) override final; 572 573 protected: 574 HalResult<Capabilities> getCapabilitiesInternal() override final; 575 }; 576 577 // ------------------------------------------------------------------------------------------------- 578 579 }; // namespace vibrator 580 581 }; // namespace android 582 583 #endif // ANDROID_OS_VIBRATORHALWRAPPER_H 584