1 /* 2 * Copyright (C) 2025 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_VIBRATOR_CONTROLLER_H 18 #define ANDROID_OS_VIBRATOR_CONTROLLER_H 19 20 #include <aidl/android/hardware/vibrator/IVibrator.h> 21 22 #include <android-base/thread_annotations.h> 23 24 namespace android { 25 26 namespace vibrator { 27 28 // ------------------------------------------------------------------------------------------------- 29 30 /* Provider for IVibrator HAL service instances. */ 31 class VibratorProvider { 32 public: 33 using IVibrator = ::aidl::android::hardware::vibrator::IVibrator; 34 VibratorProvider()35 VibratorProvider() : mServiceName(std::string(IVibrator::descriptor) + "/default") {} 36 virtual ~VibratorProvider() = default; 37 38 /* Returns true if vibrator HAL service is declared in the device, false otherwise. */ 39 virtual bool isDeclared(); 40 41 /* Connects to vibrator HAL, possibly waiting for the declared service to become available. */ 42 virtual std::shared_ptr<IVibrator> waitForVibrator(); 43 44 /* Connects to vibrator HAL if declared and available, without waiting. */ 45 virtual std::shared_ptr<IVibrator> checkForVibrator(); 46 47 private: 48 std::mutex mMutex; 49 const std::string mServiceName; 50 std::optional<bool> mIsDeclared GUARDED_BY(mMutex); 51 }; 52 53 // ------------------------------------------------------------------------------------------------- 54 55 /* Controller for Vibrator HAL handle. 56 * This relies on VibratorProvider to connect to the underlying Vibrator HAL service and reconnects 57 * after each transaction failed call. This also ensures connecting to the service is thread-safe. 58 */ 59 class VibratorController { 60 public: 61 using Effect = ::aidl::android::hardware::vibrator::Effect; 62 using EffectStrength = ::aidl::android::hardware::vibrator::EffectStrength; 63 using IVibrator = ::aidl::android::hardware::vibrator::IVibrator; 64 using Status = ::ndk::ScopedAStatus; 65 using VibratorOp = std::function<Status(IVibrator*)>; 66 VibratorController()67 VibratorController() : VibratorController(std::make_shared<VibratorProvider>()) {} VibratorController(std::shared_ptr<VibratorProvider> vibratorProvider)68 VibratorController(std::shared_ptr<VibratorProvider> vibratorProvider) 69 : mVibratorProvider(std::move(vibratorProvider)), mVibrator(nullptr) {} 70 virtual ~VibratorController() = default; 71 72 /* Connects HAL service, possibly waiting for the declared service to become available. 73 * This will automatically be called at the first API usage if it was not manually called 74 * beforehand. Call this manually during the setup phase to avoid slowing the first API call. 75 * Returns true if HAL service is declared, false otherwise. 76 */ 77 bool init(); 78 79 /* Turn vibrator off. */ 80 Status off(); 81 82 /* Set vibration amplitude in [0,1]. */ 83 Status setAmplitude(float amplitude); 84 85 /* Enable/disable external control. */ 86 Status setExternalControl(bool enabled); 87 88 /* Enable always-on for given id, with given effect and strength. */ 89 Status alwaysOnEnable(int32_t id, const Effect& effect, const EffectStrength& strength); 90 91 /* Disable always-on for given id. */ 92 Status alwaysOnDisable(int32_t id); 93 94 private: 95 /* Max number of attempts to perform an operation when it fails with transaction error. */ 96 static constexpr int MAX_ATTEMPTS = 2; 97 98 std::mutex mMutex; 99 std::shared_ptr<VibratorProvider> mVibratorProvider; 100 std::shared_ptr<IVibrator> mVibrator GUARDED_BY(mMutex); 101 102 /* Reconnects HAL service without waiting for the service to become available. */ 103 std::shared_ptr<IVibrator> reconnectToVibrator(); 104 105 /* Perform given operation on HAL with retries on transaction failures. */ 106 Status doWithRetries(const VibratorOp& op, const char* logLabel); 107 108 /* Perform given operation on HAL with logs for error/unsupported results. */ 109 static Status doOnce(IVibrator* vibrator, const VibratorOp& op, const char* logLabel); 110 }; 111 112 // ------------------------------------------------------------------------------------------------- 113 114 }; // namespace vibrator 115 116 }; // namespace android 117 118 #endif // ANDROID_OS_VIBRATOR_CONTROLLER_H 119