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