Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [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 "VibratorHalWrapper" |
| 18 | |
| 19 | #include <android/hardware/vibrator/1.3/IVibrator.h> |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 20 | #include <android/hardware/vibrator/IVibrator.h> |
| 21 | #include <hardware/vibrator.h> |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame^] | 22 | #include <cmath> |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 23 | |
| 24 | #include <utils/Log.h> |
| 25 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 26 | #include <vibratorservice/VibratorCallbackScheduler.h> |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 27 | #include <vibratorservice/VibratorHalWrapper.h> |
| 28 | |
| 29 | using android::hardware::vibrator::CompositeEffect; |
Lais Andrade | 07f9c0e | 2020-08-11 16:22:12 +0000 | [diff] [blame] | 30 | using android::hardware::vibrator::CompositePrimitive; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 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 <class T> |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 49 | HalResult<T> loadCached(const std::function<HalResult<T>()>& loadFn, std::optional<T>& cache) { |
| 50 | if (cache.has_value()) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 51 | // Return copy of cached value. |
| 52 | return HalResult<T>::ok(*cache); |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 53 | } |
| 54 | HalResult<T> ret = loadFn(); |
| 55 | if (ret.isOk()) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 56 | // Cache copy of returned value. |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 57 | cache.emplace(ret.value()); |
| 58 | } |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | template <class T> |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 63 | bool isStaticCastValid(Effect effect) { |
| 64 | T castEffect = static_cast<T>(effect); |
| 65 | auto iter = hardware::hidl_enum_range<T>(); |
| 66 | return castEffect >= *iter.begin() && castEffect <= *std::prev(iter.end()); |
| 67 | } |
| 68 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 69 | // ------------------------------------------------------------------------------------------------- |
| 70 | |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame] | 71 | const constexpr char* STATUS_T_ERROR_MESSAGE_PREFIX = "status_t = "; |
| 72 | const constexpr char* STATUS_V_1_0_ERROR_MESSAGE_PREFIX = |
| 73 | "android::hardware::vibrator::V1_0::Status = "; |
| 74 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 75 | template <typename T> |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 76 | HalResult<T> HalResult<T>::fromStatus(V1_0::Status status, T data) { |
| 77 | switch (status) { |
| 78 | case V1_0::Status::OK: |
| 79 | return HalResult<T>::ok(data); |
| 80 | case V1_0::Status::UNSUPPORTED_OPERATION: |
| 81 | return HalResult<T>::unsupported(); |
| 82 | default: |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame] | 83 | return HalResult<T>::failed(STATUS_V_1_0_ERROR_MESSAGE_PREFIX + toString(status)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | template <typename T> |
| 88 | template <typename R> |
| 89 | HalResult<T> HalResult<T>::fromReturn(hardware::Return<R>& ret, T data) { |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame] | 90 | return ret.isOk() ? HalResult<T>::ok(data) : HalResult<T>::failed(ret.description()); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | template <typename T> |
| 94 | template <typename R> |
| 95 | HalResult<T> HalResult<T>::fromReturn(hardware::Return<R>& ret, V1_0::Status status, T data) { |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame] | 96 | return ret.isOk() ? HalResult<T>::fromStatus(status, data) |
| 97 | : HalResult<T>::failed(ret.description()); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // ------------------------------------------------------------------------------------------------- |
| 101 | |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame] | 102 | HalResult<void> HalResult<void>::fromStatus(status_t status) { |
| 103 | if (status == android::OK) { |
| 104 | return HalResult<void>::ok(); |
| 105 | } |
| 106 | return HalResult<void>::failed(STATUS_T_ERROR_MESSAGE_PREFIX + statusToString(status)); |
| 107 | } |
| 108 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 109 | HalResult<void> HalResult<void>::fromStatus(binder::Status status) { |
| 110 | if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
| 111 | return HalResult<void>::unsupported(); |
| 112 | } |
| 113 | if (status.isOk()) { |
| 114 | return HalResult<void>::ok(); |
| 115 | } |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame] | 116 | return HalResult<void>::failed(std::string(status.toString8().c_str())); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | HalResult<void> HalResult<void>::fromStatus(V1_0::Status status) { |
| 120 | switch (status) { |
| 121 | case V1_0::Status::OK: |
| 122 | return HalResult<void>::ok(); |
| 123 | case V1_0::Status::UNSUPPORTED_OPERATION: |
| 124 | return HalResult<void>::unsupported(); |
| 125 | default: |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame] | 126 | return HalResult<void>::failed(STATUS_V_1_0_ERROR_MESSAGE_PREFIX + toString(status)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | template <typename R> |
| 131 | HalResult<void> HalResult<void>::fromReturn(hardware::Return<R>& ret) { |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame] | 132 | return ret.isOk() ? HalResult<void>::ok() : HalResult<void>::failed(ret.description()); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // ------------------------------------------------------------------------------------------------- |
| 136 | |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame^] | 137 | Info HalWrapper::getInfo() { |
| 138 | getCapabilities(); |
| 139 | getPrimitiveDurations(); |
| 140 | std::lock_guard<std::mutex> lock(mInfoMutex); |
| 141 | if (mInfoCache.mSupportedEffects.isFailed()) { |
| 142 | mInfoCache.mSupportedEffects = getSupportedEffectsInternal(); |
| 143 | } |
| 144 | if (mInfoCache.mResonantFrequency.isFailed()) { |
| 145 | mInfoCache.mResonantFrequency = getResonantFrequencyInternal(); |
| 146 | } |
| 147 | if (mInfoCache.mQFactor.isFailed()) { |
| 148 | mInfoCache.mQFactor = getQFactorInternal(); |
| 149 | } |
| 150 | return mInfoCache.get(); |
| 151 | } |
| 152 | |
| 153 | HalResult<Capabilities> HalWrapper::getCapabilities() { |
| 154 | std::lock_guard<std::mutex> lock(mInfoMutex); |
| 155 | if (mInfoCache.mCapabilities.isFailed()) { |
| 156 | mInfoCache.mCapabilities = getCapabilitiesInternal(); |
| 157 | } |
| 158 | return mInfoCache.mCapabilities; |
| 159 | } |
| 160 | |
| 161 | HalResult<std::vector<milliseconds>> HalWrapper::getPrimitiveDurations() { |
| 162 | std::lock_guard<std::mutex> lock(mInfoMutex); |
| 163 | if (mInfoCache.mSupportedPrimitives.isFailed()) { |
| 164 | mInfoCache.mSupportedPrimitives = getSupportedPrimitivesInternal(); |
| 165 | if (mInfoCache.mSupportedPrimitives.isUnsupported()) { |
| 166 | mInfoCache.mPrimitiveDurations = HalResult<std::vector<milliseconds>>::unsupported(); |
| 167 | } |
| 168 | } |
| 169 | if (mInfoCache.mPrimitiveDurations.isFailed() && mInfoCache.mSupportedPrimitives.isOk()) { |
| 170 | mInfoCache.mPrimitiveDurations = |
| 171 | getPrimitiveDurationsInternal(mInfoCache.mSupportedPrimitives.value()); |
| 172 | } |
| 173 | return mInfoCache.mPrimitiveDurations; |
| 174 | } |
| 175 | |
| 176 | HalResult<std::vector<Effect>> HalWrapper::getSupportedEffectsInternal() { |
| 177 | ALOGV("Skipped getSupportedEffects because it's not available in Vibrator HAL"); |
| 178 | return HalResult<std::vector<Effect>>::unsupported(); |
| 179 | } |
| 180 | |
| 181 | HalResult<std::vector<CompositePrimitive>> HalWrapper::getSupportedPrimitivesInternal() { |
| 182 | ALOGV("Skipped getSupportedPrimitives because it's not available in Vibrator HAL"); |
| 183 | return HalResult<std::vector<CompositePrimitive>>::unsupported(); |
| 184 | } |
| 185 | |
| 186 | HalResult<std::vector<milliseconds>> HalWrapper::getPrimitiveDurationsInternal( |
| 187 | const std::vector<CompositePrimitive>&) { |
| 188 | ALOGV("Skipped getPrimitiveDurations because it's not available in Vibrator HAL"); |
| 189 | return HalResult<std::vector<milliseconds>>::unsupported(); |
| 190 | } |
| 191 | |
| 192 | HalResult<float> HalWrapper::getResonantFrequencyInternal() { |
| 193 | ALOGV("Skipped getResonantFrequency because it's not available in Vibrator HAL"); |
| 194 | return HalResult<float>::unsupported(); |
| 195 | } |
| 196 | |
| 197 | HalResult<float> HalWrapper::getQFactorInternal() { |
| 198 | ALOGV("Skipped getQFactor because it's not available in Vibrator HAL"); |
| 199 | return HalResult<float>::unsupported(); |
| 200 | } |
| 201 | |
| 202 | // ------------------------------------------------------------------------------------------------- |
| 203 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 204 | HalResult<void> AidlHalWrapper::ping() { |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame] | 205 | return HalResult<void>::fromStatus(IInterface::asBinder(getHal())->pingBinder()); |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void AidlHalWrapper::tryReconnect() { |
Lais Andrade | 98c9703 | 2020-11-17 19:23:01 +0000 | [diff] [blame] | 209 | auto result = mReconnectFn(); |
| 210 | if (!result.isOk()) { |
| 211 | return; |
| 212 | } |
| 213 | sp<Aidl::IVibrator> newHandle = result.value(); |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 214 | if (newHandle) { |
| 215 | std::lock_guard<std::mutex> lock(mHandleMutex); |
| 216 | mHandle = std::move(newHandle); |
| 217 | } |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | HalResult<void> AidlHalWrapper::on(milliseconds timeout, |
| 221 | const std::function<void()>& completionCallback) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 222 | HalResult<Capabilities> capabilities = getCapabilities(); |
| 223 | bool supportsCallback = capabilities.isOk() && |
| 224 | static_cast<int32_t>(capabilities.value() & Capabilities::ON_CALLBACK); |
| 225 | auto cb = supportsCallback ? new HalCallbackWrapper(completionCallback) : nullptr; |
| 226 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 227 | auto ret = HalResult<void>::fromStatus(getHal()->on(timeout.count(), cb)); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 228 | if (!supportsCallback && ret.isOk()) { |
| 229 | mCallbackScheduler->schedule(completionCallback, timeout); |
| 230 | } |
| 231 | |
| 232 | return ret; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | HalResult<void> AidlHalWrapper::off() { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 236 | return HalResult<void>::fromStatus(getHal()->off()); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Lais Andrade | 4e2b2d4 | 2021-02-15 20:58:51 +0000 | [diff] [blame] | 239 | HalResult<void> AidlHalWrapper::setAmplitude(float amplitude) { |
| 240 | return HalResult<void>::fromStatus(getHal()->setAmplitude(amplitude)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | HalResult<void> AidlHalWrapper::setExternalControl(bool enabled) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 244 | return HalResult<void>::fromStatus(getHal()->setExternalControl(enabled)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | HalResult<void> AidlHalWrapper::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 248 | return HalResult<void>::fromStatus(getHal()->alwaysOnEnable(id, effect, strength)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | HalResult<void> AidlHalWrapper::alwaysOnDisable(int32_t id) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 252 | return HalResult<void>::fromStatus(getHal()->alwaysOnDisable(id)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 255 | HalResult<milliseconds> AidlHalWrapper::performEffect( |
| 256 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 257 | HalResult<Capabilities> capabilities = getCapabilities(); |
| 258 | bool supportsCallback = capabilities.isOk() && |
| 259 | static_cast<int32_t>(capabilities.value() & Capabilities::PERFORM_CALLBACK); |
| 260 | auto cb = supportsCallback ? new HalCallbackWrapper(completionCallback) : nullptr; |
| 261 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 262 | int32_t lengthMs; |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 263 | auto result = getHal()->perform(effect, strength, cb, &lengthMs); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 264 | milliseconds length = milliseconds(lengthMs); |
| 265 | |
| 266 | auto ret = HalResult<milliseconds>::fromStatus(result, length); |
| 267 | if (!supportsCallback && ret.isOk()) { |
| 268 | mCallbackScheduler->schedule(completionCallback, length); |
| 269 | } |
| 270 | |
| 271 | return ret; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Lais Andrade | 49b60b1 | 2021-02-23 13:27:41 +0000 | [diff] [blame] | 274 | HalResult<milliseconds> AidlHalWrapper::performComposedEffect( |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 275 | const std::vector<CompositeEffect>& primitiveEffects, |
| 276 | const std::function<void()>& completionCallback) { |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 277 | // This method should always support callbacks, so no need to double check. |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 278 | auto cb = new HalCallbackWrapper(completionCallback); |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame^] | 279 | |
| 280 | auto durations = getPrimitiveDurations().valueOr({}); |
Lais Andrade | 49b60b1 | 2021-02-23 13:27:41 +0000 | [diff] [blame] | 281 | milliseconds duration(0); |
| 282 | for (const auto& effect : primitiveEffects) { |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame^] | 283 | auto primitiveIdx = static_cast<size_t>(effect.primitive); |
| 284 | if (primitiveIdx < durations.size()) { |
| 285 | duration += durations[primitiveIdx]; |
| 286 | } else { |
| 287 | // Make sure the returned duration is positive to indicate successful vibration. |
| 288 | duration += milliseconds(1); |
Lais Andrade | 49b60b1 | 2021-02-23 13:27:41 +0000 | [diff] [blame] | 289 | } |
| 290 | duration += milliseconds(effect.delayMs); |
| 291 | } |
Lais Andrade | 49b60b1 | 2021-02-23 13:27:41 +0000 | [diff] [blame] | 292 | |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame^] | 293 | return HalResult<milliseconds>::fromStatus(getHal()->compose(primitiveEffects, cb), duration); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 296 | HalResult<Capabilities> AidlHalWrapper::getCapabilitiesInternal() { |
| 297 | int32_t capabilities = 0; |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 298 | auto result = getHal()->getCapabilities(&capabilities); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 299 | return HalResult<Capabilities>::fromStatus(result, static_cast<Capabilities>(capabilities)); |
| 300 | } |
| 301 | |
| 302 | HalResult<std::vector<Effect>> AidlHalWrapper::getSupportedEffectsInternal() { |
| 303 | std::vector<Effect> supportedEffects; |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 304 | auto result = getHal()->getSupportedEffects(&supportedEffects); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 305 | return HalResult<std::vector<Effect>>::fromStatus(result, supportedEffects); |
| 306 | } |
| 307 | |
Lais Andrade | 07f9c0e | 2020-08-11 16:22:12 +0000 | [diff] [blame] | 308 | HalResult<std::vector<CompositePrimitive>> AidlHalWrapper::getSupportedPrimitivesInternal() { |
| 309 | std::vector<CompositePrimitive> supportedPrimitives; |
| 310 | auto result = getHal()->getSupportedPrimitives(&supportedPrimitives); |
| 311 | return HalResult<std::vector<CompositePrimitive>>::fromStatus(result, supportedPrimitives); |
| 312 | } |
| 313 | |
Lais Andrade | 965284b | 2021-03-19 20:58:15 +0000 | [diff] [blame^] | 314 | HalResult<std::vector<milliseconds>> AidlHalWrapper::getPrimitiveDurationsInternal( |
| 315 | const std::vector<CompositePrimitive>& supportedPrimitives) { |
| 316 | std::vector<milliseconds> durations; |
| 317 | constexpr auto primitiveRange = enum_range<CompositePrimitive>(); |
| 318 | constexpr auto primitiveCount = std::distance(primitiveRange.begin(), primitiveRange.end()); |
| 319 | durations.resize(primitiveCount); |
| 320 | |
| 321 | for (auto primitive : supportedPrimitives) { |
| 322 | auto primitiveIdx = static_cast<size_t>(primitive); |
| 323 | if (primitiveIdx >= durations.size()) { |
| 324 | // Safety check, should not happen if enum_range is correct. |
| 325 | continue; |
| 326 | } |
| 327 | int32_t duration = 0; |
| 328 | auto status = getHal()->getPrimitiveDuration(primitive, &duration); |
| 329 | if (!status.isOk()) { |
| 330 | return HalResult<std::vector<milliseconds>>::failed(status.toString8().c_str()); |
| 331 | } |
| 332 | durations[primitiveIdx] = milliseconds(duration); |
| 333 | } |
| 334 | |
| 335 | return HalResult<std::vector<milliseconds>>::ok(durations); |
| 336 | } |
| 337 | |
Michael Wright | ba9a6ce | 2021-02-26 02:55:29 +0000 | [diff] [blame] | 338 | HalResult<float> AidlHalWrapper::getResonantFrequencyInternal() { |
| 339 | float f0 = 0; |
| 340 | auto result = getHal()->getResonantFrequency(&f0); |
| 341 | return HalResult<float>::fromStatus(result, f0); |
| 342 | } |
| 343 | |
| 344 | HalResult<float> AidlHalWrapper::getQFactorInternal() { |
| 345 | float qFactor = 0; |
| 346 | auto result = getHal()->getQFactor(&qFactor); |
| 347 | return HalResult<float>::fromStatus(result, qFactor); |
| 348 | } |
| 349 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 350 | sp<Aidl::IVibrator> AidlHalWrapper::getHal() { |
| 351 | std::lock_guard<std::mutex> lock(mHandleMutex); |
| 352 | return mHandle; |
| 353 | } |
| 354 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 355 | // ------------------------------------------------------------------------------------------------- |
| 356 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 357 | template <typename I> |
| 358 | HalResult<void> HidlHalWrapper<I>::ping() { |
| 359 | auto result = getHal()->ping(); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 360 | return HalResult<void>::fromReturn(result); |
| 361 | } |
| 362 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 363 | template <typename I> |
| 364 | void HidlHalWrapper<I>::tryReconnect() { |
| 365 | sp<I> newHandle = I::tryGetService(); |
| 366 | if (newHandle) { |
| 367 | std::lock_guard<std::mutex> lock(mHandleMutex); |
| 368 | mHandle = std::move(newHandle); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | template <typename I> |
| 373 | HalResult<void> HidlHalWrapper<I>::on(milliseconds timeout, |
| 374 | const std::function<void()>& completionCallback) { |
| 375 | auto result = getHal()->on(timeout.count()); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 376 | auto ret = HalResult<void>::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); |
| 377 | if (ret.isOk()) { |
| 378 | mCallbackScheduler->schedule(completionCallback, timeout); |
| 379 | } |
| 380 | return ret; |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 383 | template <typename I> |
| 384 | HalResult<void> HidlHalWrapper<I>::off() { |
| 385 | auto result = getHal()->off(); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 386 | return HalResult<void>::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); |
| 387 | } |
| 388 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 389 | template <typename I> |
Lais Andrade | 4e2b2d4 | 2021-02-15 20:58:51 +0000 | [diff] [blame] | 390 | HalResult<void> HidlHalWrapper<I>::setAmplitude(float amplitude) { |
| 391 | uint8_t amp = static_cast<uint8_t>(amplitude * std::numeric_limits<uint8_t>::max()); |
| 392 | auto result = getHal()->setAmplitude(amp); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 393 | return HalResult<void>::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); |
| 394 | } |
| 395 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 396 | template <typename I> |
| 397 | HalResult<void> HidlHalWrapper<I>::setExternalControl(bool) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 398 | ALOGV("Skipped setExternalControl because Vibrator HAL does not support it"); |
| 399 | return HalResult<void>::unsupported(); |
| 400 | } |
| 401 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 402 | template <typename I> |
| 403 | HalResult<void> HidlHalWrapper<I>::alwaysOnEnable(int32_t, Effect, EffectStrength) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 404 | ALOGV("Skipped alwaysOnEnable because Vibrator HAL AIDL is not available"); |
| 405 | return HalResult<void>::unsupported(); |
| 406 | } |
| 407 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 408 | template <typename I> |
| 409 | HalResult<void> HidlHalWrapper<I>::alwaysOnDisable(int32_t) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 410 | ALOGV("Skipped alwaysOnDisable because Vibrator HAL AIDL is not available"); |
| 411 | return HalResult<void>::unsupported(); |
| 412 | } |
| 413 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 414 | template <typename I> |
Lais Andrade | 49b60b1 | 2021-02-23 13:27:41 +0000 | [diff] [blame] | 415 | HalResult<std::chrono::milliseconds> HidlHalWrapper<I>::performComposedEffect( |
| 416 | const std::vector<CompositeEffect>&, const std::function<void()>&) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 417 | ALOGV("Skipped composed effect because Vibrator HAL AIDL is not available"); |
Lais Andrade | 49b60b1 | 2021-02-23 13:27:41 +0000 | [diff] [blame] | 418 | return HalResult<std::chrono::milliseconds>::unsupported(); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 421 | template <typename I> |
| 422 | HalResult<Capabilities> HidlHalWrapper<I>::getCapabilitiesInternal() { |
| 423 | hardware::Return<bool> result = getHal()->supportsAmplitudeControl(); |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 424 | Capabilities capabilities = |
| 425 | result.withDefault(false) ? Capabilities::AMPLITUDE_CONTROL : Capabilities::NONE; |
| 426 | return HalResult<Capabilities>::fromReturn(result, capabilities); |
| 427 | } |
| 428 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 429 | template <typename I> |
| 430 | template <typename T> |
| 431 | HalResult<milliseconds> HidlHalWrapper<I>::performInternal( |
| 432 | perform_fn<T> performFn, sp<I> handle, T effect, EffectStrength strength, |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 433 | const std::function<void()>& completionCallback) { |
| 434 | V1_0::Status status; |
| 435 | int32_t lengthMs; |
| 436 | auto effectCallback = [&status, &lengthMs](V1_0::Status retStatus, uint32_t retLengthMs) { |
| 437 | status = retStatus; |
| 438 | lengthMs = retLengthMs; |
| 439 | }; |
| 440 | |
| 441 | V1_0::EffectStrength effectStrength = static_cast<V1_0::EffectStrength>(strength); |
| 442 | auto result = std::invoke(performFn, handle, effect, effectStrength, effectCallback); |
| 443 | milliseconds length = milliseconds(lengthMs); |
| 444 | |
| 445 | auto ret = HalResult<milliseconds>::fromReturn(result, status, length); |
| 446 | if (ret.isOk()) { |
| 447 | mCallbackScheduler->schedule(completionCallback, length); |
| 448 | } |
| 449 | |
| 450 | return ret; |
| 451 | } |
| 452 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 453 | template <typename I> |
| 454 | sp<I> HidlHalWrapper<I>::getHal() { |
| 455 | std::lock_guard<std::mutex> lock(mHandleMutex); |
| 456 | return mHandle; |
| 457 | } |
| 458 | |
| 459 | // ------------------------------------------------------------------------------------------------- |
| 460 | |
| 461 | HalResult<milliseconds> HidlHalWrapperV1_0::performEffect( |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 462 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 463 | if (isStaticCastValid<V1_0::Effect>(effect)) { |
| 464 | return performInternal(&V1_0::IVibrator::perform, getHal(), |
| 465 | static_cast<V1_0::Effect>(effect), strength, completionCallback); |
| 466 | } |
| 467 | |
| 468 | ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", |
| 469 | Aidl::toString(effect).c_str()); |
| 470 | return HalResult<milliseconds>::unsupported(); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 473 | // ------------------------------------------------------------------------------------------------- |
| 474 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 475 | HalResult<milliseconds> HidlHalWrapperV1_1::performEffect( |
| 476 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 477 | if (isStaticCastValid<V1_0::Effect>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 478 | return performInternal(&V1_1::IVibrator::perform, getHal(), |
| 479 | static_cast<V1_0::Effect>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 480 | } |
| 481 | if (isStaticCastValid<V1_1::Effect_1_1>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 482 | return performInternal(&V1_1::IVibrator::perform_1_1, getHal(), |
| 483 | static_cast<V1_1::Effect_1_1>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", |
| 487 | Aidl::toString(effect).c_str()); |
| 488 | return HalResult<milliseconds>::unsupported(); |
| 489 | } |
| 490 | |
| 491 | // ------------------------------------------------------------------------------------------------- |
| 492 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 493 | HalResult<milliseconds> HidlHalWrapperV1_2::performEffect( |
| 494 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 495 | if (isStaticCastValid<V1_0::Effect>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 496 | return performInternal(&V1_2::IVibrator::perform, getHal(), |
| 497 | static_cast<V1_0::Effect>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 498 | } |
| 499 | if (isStaticCastValid<V1_1::Effect_1_1>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 500 | return performInternal(&V1_2::IVibrator::perform_1_1, getHal(), |
| 501 | static_cast<V1_1::Effect_1_1>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 502 | } |
| 503 | if (isStaticCastValid<V1_2::Effect>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 504 | return performInternal(&V1_2::IVibrator::perform_1_2, getHal(), |
| 505 | static_cast<V1_2::Effect>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", |
| 509 | Aidl::toString(effect).c_str()); |
| 510 | return HalResult<milliseconds>::unsupported(); |
| 511 | } |
| 512 | |
| 513 | // ------------------------------------------------------------------------------------------------- |
| 514 | |
| 515 | HalResult<void> HidlHalWrapperV1_3::setExternalControl(bool enabled) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 516 | auto result = getHal()->setExternalControl(static_cast<uint32_t>(enabled)); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 517 | return HalResult<void>::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR)); |
| 518 | } |
| 519 | |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 520 | HalResult<milliseconds> HidlHalWrapperV1_3::performEffect( |
| 521 | Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) { |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 522 | if (isStaticCastValid<V1_0::Effect>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 523 | return performInternal(&V1_3::IVibrator::perform, getHal(), |
| 524 | static_cast<V1_0::Effect>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 525 | } |
| 526 | if (isStaticCastValid<V1_1::Effect_1_1>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 527 | return performInternal(&V1_3::IVibrator::perform_1_1, getHal(), |
| 528 | static_cast<V1_1::Effect_1_1>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 529 | } |
| 530 | if (isStaticCastValid<V1_2::Effect>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 531 | return performInternal(&V1_3::IVibrator::perform_1_2, getHal(), |
| 532 | static_cast<V1_2::Effect>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 533 | } |
| 534 | if (isStaticCastValid<V1_3::Effect>(effect)) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 535 | return performInternal(&V1_3::IVibrator::perform_1_3, getHal(), |
| 536 | static_cast<V1_3::Effect>(effect), strength, completionCallback); |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s", |
| 540 | Aidl::toString(effect).c_str()); |
| 541 | return HalResult<milliseconds>::unsupported(); |
| 542 | } |
| 543 | |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 544 | HalResult<Capabilities> HidlHalWrapperV1_3::getCapabilitiesInternal() { |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame] | 545 | Capabilities capabilities = Capabilities::NONE; |
| 546 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 547 | sp<V1_3::IVibrator> hal = getHal(); |
| 548 | auto amplitudeResult = hal->supportsAmplitudeControl(); |
| 549 | if (!amplitudeResult.isOk()) { |
Lais Andrade | 0866661 | 2020-08-07 16:16:31 +0000 | [diff] [blame] | 550 | return HalResult<Capabilities>::fromReturn(amplitudeResult, capabilities); |
Lais Andrade | d39ff7d | 2020-05-19 10:42:51 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 553 | auto externalControlResult = hal->supportsExternalControl(); |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 554 | if (amplitudeResult.withDefault(false)) { |
| 555 | capabilities |= Capabilities::AMPLITUDE_CONTROL; |
| 556 | } |
| 557 | if (externalControlResult.withDefault(false)) { |
| 558 | capabilities |= Capabilities::EXTERNAL_CONTROL; |
Lais Andrade | 602ff96 | 2020-08-27 12:02:53 +0000 | [diff] [blame] | 559 | |
| 560 | if (amplitudeResult.withDefault(false)) { |
| 561 | capabilities |= Capabilities::EXTERNAL_AMPLITUDE_CONTROL; |
| 562 | } |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | return HalResult<Capabilities>::fromReturn(externalControlResult, capabilities); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Lais Andrade | 9e9fcc9 | 2020-04-07 20:13:08 +0100 | [diff] [blame] | 568 | // ------------------------------------------------------------------------------------------------- |
| 569 | |
| 570 | }; // namespace vibrator |
| 571 | |
| 572 | }; // namespace android |