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