Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [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 "VibratorHalController" |
| 18 | |
| 19 | #include <android/hardware/vibrator/1.3/IVibrator.h> |
| 20 | #include <android/hardware/vibrator/IVibrator.h> |
| 21 | #include <binder/IServiceManager.h> |
| 22 | #include <hardware/vibrator.h> |
| 23 | |
| 24 | #include <utils/Log.h> |
| 25 | |
| 26 | #include <vibratorservice/VibratorCallbackScheduler.h> |
| 27 | #include <vibratorservice/VibratorHalController.h> |
| 28 | #include <vibratorservice/VibratorHalWrapper.h> |
| 29 | |
| 30 | using android::hardware::vibrator::CompositeEffect; |
| 31 | using android::hardware::vibrator::Effect; |
| 32 | using android::hardware::vibrator::EffectStrength; |
| 33 | |
| 34 | using std::chrono::milliseconds; |
| 35 | |
| 36 | namespace V1_0 = android::hardware::vibrator::V1_0; |
| 37 | namespace V1_1 = android::hardware::vibrator::V1_1; |
| 38 | namespace V1_2 = android::hardware::vibrator::V1_2; |
| 39 | namespace V1_3 = android::hardware::vibrator::V1_3; |
| 40 | namespace Aidl = android::hardware::vibrator; |
| 41 | |
| 42 | namespace android { |
| 43 | |
| 44 | namespace vibrator { |
| 45 | |
| 46 | // ------------------------------------------------------------------------------------------------- |
| 47 | |
Lais Andrade | 37d0f04 | 2020-06-18 16:39:55 +0000 | [diff] [blame] | 48 | static constexpr int MAX_RETRIES = 1; |
| 49 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 50 | std::shared_ptr<HalWrapper> HalConnector::connect(std::shared_ptr<CallbackScheduler> scheduler) { |
| 51 | static bool gHalExists = true; |
| 52 | if (!gHalExists) { |
| 53 | // We already tried to connect to all of the vibrator HAL versions and none was available. |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 54 | return nullptr; |
| 55 | } |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 56 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 57 | sp<Aidl::IVibrator> aidlHal = waitForVintfService<Aidl::IVibrator>(); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 58 | if (aidlHal) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 59 | ALOGV("Successfully connected to Vibrator HAL AIDL service."); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 60 | return std::make_shared<AidlHalWrapper>(std::move(scheduler), aidlHal); |
| 61 | } |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 62 | |
| 63 | sp<V1_0::IVibrator> halV1_0 = V1_0::IVibrator::getService(); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 64 | if (halV1_0 == nullptr) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 65 | ALOGV("Vibrator HAL service not available."); |
| 66 | gHalExists = false; |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 67 | return nullptr; |
| 68 | } |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 69 | |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 70 | sp<V1_3::IVibrator> halV1_3 = V1_3::IVibrator::castFrom(halV1_0); |
| 71 | if (halV1_3) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 72 | ALOGV("Successfully connected to Vibrator HAL v1.3 service."); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 73 | return std::make_shared<HidlHalWrapperV1_3>(std::move(scheduler), halV1_3); |
| 74 | } |
| 75 | sp<V1_2::IVibrator> halV1_2 = V1_2::IVibrator::castFrom(halV1_0); |
| 76 | if (halV1_2) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 77 | ALOGV("Successfully connected to Vibrator HAL v1.2 service."); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 78 | return std::make_shared<HidlHalWrapperV1_2>(std::move(scheduler), halV1_2); |
| 79 | } |
| 80 | sp<V1_1::IVibrator> halV1_1 = V1_1::IVibrator::castFrom(halV1_0); |
| 81 | if (halV1_1) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 82 | ALOGV("Successfully connected to Vibrator HAL v1.1 service."); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 83 | return std::make_shared<HidlHalWrapperV1_1>(std::move(scheduler), halV1_1); |
| 84 | } |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 85 | ALOGV("Successfully connected to Vibrator HAL v1.0 service."); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 86 | return std::make_shared<HidlHalWrapperV1_0>(std::move(scheduler), halV1_0); |
| 87 | } |
| 88 | |
| 89 | // ------------------------------------------------------------------------------------------------- |
| 90 | |
| 91 | template <typename T> |
| 92 | HalResult<T> HalController::processHalResult(HalResult<T> result, const char* functionName) { |
| 93 | if (result.isFailed()) { |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame^] | 94 | ALOGE("%s failed: %s", functionName, result.errorMessage()); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 95 | std::lock_guard<std::mutex> lock(mConnectedHalMutex); |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 96 | mConnectedHal->tryReconnect(); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 97 | } |
| 98 | return result; |
| 99 | } |
| 100 | |
| 101 | template <typename T> |
| 102 | HalResult<T> HalController::apply(HalController::hal_fn<T>& halFn, const char* functionName) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 103 | std::shared_ptr<HalWrapper> hal = nullptr; |
| 104 | { |
| 105 | std::lock_guard<std::mutex> lock(mConnectedHalMutex); |
| 106 | if (mConnectedHal == nullptr) { |
| 107 | // Init was never called, so connect to HAL for the first time during this call. |
| 108 | mConnectedHal = mHalConnector->connect(mCallbackScheduler); |
| 109 | |
| 110 | if (mConnectedHal == nullptr) { |
| 111 | ALOGV("Skipped %s because Vibrator HAL is not available", functionName); |
| 112 | return HalResult<T>::unsupported(); |
| 113 | } |
| 114 | } |
| 115 | hal = mConnectedHal; |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Lais Andrade | 37d0f04 | 2020-06-18 16:39:55 +0000 | [diff] [blame] | 118 | HalResult<T> ret = processHalResult(halFn(hal), functionName); |
| 119 | for (int i = 0; i < MAX_RETRIES && ret.isFailed(); i++) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 120 | ret = processHalResult(halFn(hal), functionName); |
Lais Andrade | 37d0f04 | 2020-06-18 16:39:55 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | return ret; |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // ------------------------------------------------------------------------------------------------- |
| 127 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 128 | void HalController::init() { |
| 129 | std::lock_guard<std::mutex> lock(mConnectedHalMutex); |
| 130 | if (mConnectedHal == nullptr) { |
| 131 | mConnectedHal = mHalConnector->connect(mCallbackScheduler); |
| 132 | } |
| 133 | } |
| 134 | |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 135 | HalResult<void> HalController::ping() { |
| 136 | hal_fn<void> pingFn = [](std::shared_ptr<HalWrapper> hal) { return hal->ping(); }; |
| 137 | return apply(pingFn, "ping"); |
| 138 | } |
| 139 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 140 | void HalController::tryReconnect() { |
| 141 | std::lock_guard<std::mutex> lock(mConnectedHalMutex); |
| 142 | if (mConnectedHal == nullptr) { |
| 143 | mConnectedHal = mHalConnector->connect(mCallbackScheduler); |
| 144 | } else { |
| 145 | mConnectedHal->tryReconnect(); |
| 146 | } |
| 147 | } |
| 148 | |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 149 | HalResult<void> HalController::on(milliseconds timeout, |
| 150 | const std::function<void()>& completionCallback) { |
| 151 | hal_fn<void> onFn = [&](std::shared_ptr<HalWrapper> hal) { |
| 152 | return hal->on(timeout, completionCallback); |
| 153 | }; |
| 154 | return apply(onFn, "on"); |
| 155 | } |
| 156 | |
| 157 | HalResult<void> HalController::off() { |
| 158 | hal_fn<void> offFn = [](std::shared_ptr<HalWrapper> hal) { return hal->off(); }; |
| 159 | return apply(offFn, "off"); |
| 160 | } |
| 161 | |
| 162 | HalResult<void> HalController::setAmplitude(int32_t amplitude) { |
| 163 | hal_fn<void> setAmplitudeFn = [&](std::shared_ptr<HalWrapper> hal) { |
| 164 | return hal->setAmplitude(amplitude); |
| 165 | }; |
| 166 | return apply(setAmplitudeFn, "setAmplitude"); |
| 167 | } |
| 168 | |
| 169 | HalResult<void> HalController::setExternalControl(bool enabled) { |
| 170 | hal_fn<void> setExternalControlFn = [&](std::shared_ptr<HalWrapper> hal) { |
| 171 | return hal->setExternalControl(enabled); |
| 172 | }; |
| 173 | return apply(setExternalControlFn, "setExternalControl"); |
| 174 | } |
| 175 | |
| 176 | HalResult<void> HalController::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) { |
| 177 | hal_fn<void> alwaysOnEnableFn = [&](std::shared_ptr<HalWrapper> hal) { |
| 178 | return hal->alwaysOnEnable(id, effect, strength); |
| 179 | }; |
| 180 | return apply(alwaysOnEnableFn, "alwaysOnEnable"); |
| 181 | } |
| 182 | |
| 183 | HalResult<void> HalController::alwaysOnDisable(int32_t id) { |
| 184 | hal_fn<void> alwaysOnDisableFn = [&](std::shared_ptr<HalWrapper> hal) { |
| 185 | return hal->alwaysOnDisable(id); |
| 186 | }; |
| 187 | return apply(alwaysOnDisableFn, "alwaysOnDisable"); |
| 188 | } |
| 189 | |
| 190 | HalResult<Capabilities> HalController::getCapabilities() { |
| 191 | hal_fn<Capabilities> getCapabilitiesFn = [](std::shared_ptr<HalWrapper> hal) { |
| 192 | return hal->getCapabilities(); |
| 193 | }; |
| 194 | return apply(getCapabilitiesFn, "getCapabilities"); |
| 195 | } |
| 196 | |
| 197 | HalResult<std::vector<Effect>> HalController::getSupportedEffects() { |
| 198 | hal_fn<std::vector<Effect>> getSupportedEffectsFn = [](std::shared_ptr<HalWrapper> hal) { |
| 199 | return hal->getSupportedEffects(); |
| 200 | }; |
| 201 | return apply(getSupportedEffectsFn, "getSupportedEffects"); |
| 202 | } |
| 203 | |
| 204 | HalResult<milliseconds> HalController::performEffect( |
| 205 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
| 206 | hal_fn<milliseconds> performEffectFn = [&](std::shared_ptr<HalWrapper> hal) { |
| 207 | return hal->performEffect(effect, strength, completionCallback); |
| 208 | }; |
| 209 | return apply(performEffectFn, "performEffect"); |
| 210 | } |
| 211 | |
| 212 | HalResult<void> HalController::performComposedEffect( |
| 213 | const std::vector<CompositeEffect>& primitiveEffects, |
| 214 | const std::function<void()>& completionCallback) { |
| 215 | hal_fn<void> performComposedEffectFn = [&](std::shared_ptr<HalWrapper> hal) { |
| 216 | return hal->performComposedEffect(primitiveEffects, completionCallback); |
| 217 | }; |
| 218 | return apply(performComposedEffectFn, "performComposedEffect"); |
| 219 | } |
| 220 | |
| 221 | }; // namespace vibrator |
| 222 | |
| 223 | }; // namespace android |