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