blob: f10ba44d745d0d64b96e8ccfd78122e9f91ba208 [file] [log] [blame]
Lais Andrade9e9fcc92020-04-07 20:13:08 +01001/*
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 Andrade9e9fcc92020-04-07 20:13:08 +010020#include <android/hardware/vibrator/IVibrator.h>
21#include <hardware/vibrator.h>
Lais Andrade965284b2021-03-19 20:58:15 +000022#include <cmath>
Lais Andrade9e9fcc92020-04-07 20:13:08 +010023
24#include <utils/Log.h>
25
Lais Andrade10d9dc72020-05-20 12:00:49 +000026#include <vibratorservice/VibratorCallbackScheduler.h>
Lais Andrade9e9fcc92020-04-07 20:13:08 +010027#include <vibratorservice/VibratorHalWrapper.h>
28
Lais Andrade92f2af52021-03-22 16:12:50 +000029using android::hardware::vibrator::Braking;
Lais Andrade9e9fcc92020-04-07 20:13:08 +010030using android::hardware::vibrator::CompositeEffect;
Lais Andrade07f9c0e2020-08-11 16:22:12 +000031using android::hardware::vibrator::CompositePrimitive;
Lais Andrade9e9fcc92020-04-07 20:13:08 +010032using android::hardware::vibrator::Effect;
33using android::hardware::vibrator::EffectStrength;
Lais Andrade92f2af52021-03-22 16:12:50 +000034using android::hardware::vibrator::PrimitivePwle;
Lais Andrade9e9fcc92020-04-07 20:13:08 +010035
36using std::chrono::milliseconds;
37
38namespace V1_0 = android::hardware::vibrator::V1_0;
39namespace V1_1 = android::hardware::vibrator::V1_1;
40namespace V1_2 = android::hardware::vibrator::V1_2;
41namespace V1_3 = android::hardware::vibrator::V1_3;
42namespace Aidl = android::hardware::vibrator;
43
44namespace android {
45
46namespace vibrator {
47
48// -------------------------------------------------------------------------------------------------
49
50template <class T>
51bool isStaticCastValid(Effect effect) {
52 T castEffect = static_cast<T>(effect);
53 auto iter = hardware::hidl_enum_range<T>();
54 return castEffect >= *iter.begin() && castEffect <= *std::prev(iter.end());
55}
56
Lais Andrade9e9fcc92020-04-07 20:13:08 +010057// -------------------------------------------------------------------------------------------------
58
Lais Andrade965284b2021-03-19 20:58:15 +000059Info HalWrapper::getInfo() {
60 getCapabilities();
61 getPrimitiveDurations();
62 std::lock_guard<std::mutex> lock(mInfoMutex);
63 if (mInfoCache.mSupportedEffects.isFailed()) {
64 mInfoCache.mSupportedEffects = getSupportedEffectsInternal();
65 }
Lais Andrade92f2af52021-03-22 16:12:50 +000066 if (mInfoCache.mSupportedBraking.isFailed()) {
67 mInfoCache.mSupportedBraking = getSupportedBrakingInternal();
68 }
Lais Andrade4aa80c92021-06-03 17:20:16 +010069 if (mInfoCache.mPrimitiveDelayMax.isFailed()) {
70 mInfoCache.mPrimitiveDelayMax = getPrimitiveDelayMaxInternal();
71 }
72 if (mInfoCache.mPwlePrimitiveDurationMax.isFailed()) {
73 mInfoCache.mPwlePrimitiveDurationMax = getPrimitiveDurationMaxInternal();
74 }
75 if (mInfoCache.mCompositionSizeMax.isFailed()) {
76 mInfoCache.mCompositionSizeMax = getCompositionSizeMaxInternal();
77 }
78 if (mInfoCache.mPwleSizeMax.isFailed()) {
79 mInfoCache.mPwleSizeMax = getPwleSizeMaxInternal();
80 }
Lais Andrade92f2af52021-03-22 16:12:50 +000081 if (mInfoCache.mMinFrequency.isFailed()) {
82 mInfoCache.mMinFrequency = getMinFrequencyInternal();
83 }
Lais Andrade965284b2021-03-19 20:58:15 +000084 if (mInfoCache.mResonantFrequency.isFailed()) {
85 mInfoCache.mResonantFrequency = getResonantFrequencyInternal();
86 }
Lais Andrade92f2af52021-03-22 16:12:50 +000087 if (mInfoCache.mFrequencyResolution.isFailed()) {
88 mInfoCache.mFrequencyResolution = getFrequencyResolutionInternal();
89 }
Lais Andrade965284b2021-03-19 20:58:15 +000090 if (mInfoCache.mQFactor.isFailed()) {
91 mInfoCache.mQFactor = getQFactorInternal();
92 }
Lais Andrade92f2af52021-03-22 16:12:50 +000093 if (mInfoCache.mMaxAmplitudes.isFailed()) {
94 mInfoCache.mMaxAmplitudes = getMaxAmplitudesInternal();
95 }
Lais Andrade965284b2021-03-19 20:58:15 +000096 return mInfoCache.get();
97}
98
Lais Andrade92f2af52021-03-22 16:12:50 +000099HalResult<milliseconds> HalWrapper::performComposedEffect(const std::vector<CompositeEffect>&,
100 const std::function<void()>&) {
101 ALOGV("Skipped performComposedEffect because it's not available in Vibrator HAL");
102 return HalResult<milliseconds>::unsupported();
103}
104
105HalResult<void> HalWrapper::performPwleEffect(const std::vector<PrimitivePwle>&,
106 const std::function<void()>&) {
107 ALOGV("Skipped performPwleEffect because it's not available in Vibrator HAL");
108 return HalResult<void>::unsupported();
109}
110
Lais Andrade965284b2021-03-19 20:58:15 +0000111HalResult<Capabilities> HalWrapper::getCapabilities() {
112 std::lock_guard<std::mutex> lock(mInfoMutex);
113 if (mInfoCache.mCapabilities.isFailed()) {
114 mInfoCache.mCapabilities = getCapabilitiesInternal();
115 }
116 return mInfoCache.mCapabilities;
117}
118
119HalResult<std::vector<milliseconds>> HalWrapper::getPrimitiveDurations() {
120 std::lock_guard<std::mutex> lock(mInfoMutex);
121 if (mInfoCache.mSupportedPrimitives.isFailed()) {
122 mInfoCache.mSupportedPrimitives = getSupportedPrimitivesInternal();
123 if (mInfoCache.mSupportedPrimitives.isUnsupported()) {
124 mInfoCache.mPrimitiveDurations = HalResult<std::vector<milliseconds>>::unsupported();
125 }
126 }
127 if (mInfoCache.mPrimitiveDurations.isFailed() && mInfoCache.mSupportedPrimitives.isOk()) {
128 mInfoCache.mPrimitiveDurations =
129 getPrimitiveDurationsInternal(mInfoCache.mSupportedPrimitives.value());
130 }
131 return mInfoCache.mPrimitiveDurations;
132}
133
134HalResult<std::vector<Effect>> HalWrapper::getSupportedEffectsInternal() {
135 ALOGV("Skipped getSupportedEffects because it's not available in Vibrator HAL");
136 return HalResult<std::vector<Effect>>::unsupported();
137}
138
Lais Andrade92f2af52021-03-22 16:12:50 +0000139HalResult<std::vector<Braking>> HalWrapper::getSupportedBrakingInternal() {
140 ALOGV("Skipped getSupportedBraking because it's not available in Vibrator HAL");
141 return HalResult<std::vector<Braking>>::unsupported();
142}
143
Lais Andrade965284b2021-03-19 20:58:15 +0000144HalResult<std::vector<CompositePrimitive>> HalWrapper::getSupportedPrimitivesInternal() {
145 ALOGV("Skipped getSupportedPrimitives because it's not available in Vibrator HAL");
146 return HalResult<std::vector<CompositePrimitive>>::unsupported();
147}
148
149HalResult<std::vector<milliseconds>> HalWrapper::getPrimitiveDurationsInternal(
150 const std::vector<CompositePrimitive>&) {
151 ALOGV("Skipped getPrimitiveDurations because it's not available in Vibrator HAL");
152 return HalResult<std::vector<milliseconds>>::unsupported();
153}
154
Lais Andrade4aa80c92021-06-03 17:20:16 +0100155HalResult<milliseconds> HalWrapper::getPrimitiveDelayMaxInternal() {
156 ALOGV("Skipped getPrimitiveDelayMaxInternal because it's not available in Vibrator HAL");
157 return HalResult<milliseconds>::unsupported();
158}
159
160HalResult<milliseconds> HalWrapper::getPrimitiveDurationMaxInternal() {
161 ALOGV("Skipped getPrimitiveDurationMaxInternal because it's not available in Vibrator HAL");
162 return HalResult<milliseconds>::unsupported();
163}
164
165HalResult<int32_t> HalWrapper::getCompositionSizeMaxInternal() {
166 ALOGV("Skipped getCompositionSizeMaxInternal because it's not available in Vibrator HAL");
167 return HalResult<int32_t>::unsupported();
168}
169
170HalResult<int32_t> HalWrapper::getPwleSizeMaxInternal() {
171 ALOGV("Skipped getPwleSizeMaxInternal because it's not available in Vibrator HAL");
172 return HalResult<int32_t>::unsupported();
173}
174
Lais Andrade92f2af52021-03-22 16:12:50 +0000175HalResult<float> HalWrapper::getMinFrequencyInternal() {
176 ALOGV("Skipped getMinFrequency because it's not available in Vibrator HAL");
177 return HalResult<float>::unsupported();
178}
179
Lais Andrade965284b2021-03-19 20:58:15 +0000180HalResult<float> HalWrapper::getResonantFrequencyInternal() {
181 ALOGV("Skipped getResonantFrequency because it's not available in Vibrator HAL");
182 return HalResult<float>::unsupported();
183}
184
Lais Andrade92f2af52021-03-22 16:12:50 +0000185HalResult<float> HalWrapper::getFrequencyResolutionInternal() {
186 ALOGV("Skipped getFrequencyResolution because it's not available in Vibrator HAL");
187 return HalResult<float>::unsupported();
188}
189
Lais Andrade965284b2021-03-19 20:58:15 +0000190HalResult<float> HalWrapper::getQFactorInternal() {
191 ALOGV("Skipped getQFactor because it's not available in Vibrator HAL");
192 return HalResult<float>::unsupported();
193}
194
Lais Andrade92f2af52021-03-22 16:12:50 +0000195HalResult<std::vector<float>> HalWrapper::getMaxAmplitudesInternal() {
196 ALOGV("Skipped getMaxAmplitudes because it's not available in Vibrator HAL");
197 return HalResult<std::vector<float>>::unsupported();
198}
199
Lais Andrade965284b2021-03-19 20:58:15 +0000200// -------------------------------------------------------------------------------------------------
201
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100202HalResult<void> AidlHalWrapper::ping() {
Lais Andrade641248e2024-02-16 17:49:36 +0000203 return HalResultFactory::fromStatus(IInterface::asBinder(getHal())->pingBinder());
Lais Andradecfd81152020-07-01 09:00:26 +0000204}
205
206void AidlHalWrapper::tryReconnect() {
Lais Andrade98c97032020-11-17 19:23:01 +0000207 auto result = mReconnectFn();
208 if (!result.isOk()) {
209 return;
210 }
211 sp<Aidl::IVibrator> newHandle = result.value();
Lais Andradecfd81152020-07-01 09:00:26 +0000212 if (newHandle) {
213 std::lock_guard<std::mutex> lock(mHandleMutex);
214 mHandle = std::move(newHandle);
215 }
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100216}
217
218HalResult<void> AidlHalWrapper::on(milliseconds timeout,
219 const std::function<void()>& completionCallback) {
Lais Andrade10d9dc72020-05-20 12:00:49 +0000220 HalResult<Capabilities> capabilities = getCapabilities();
221 bool supportsCallback = capabilities.isOk() &&
222 static_cast<int32_t>(capabilities.value() & Capabilities::ON_CALLBACK);
223 auto cb = supportsCallback ? new HalCallbackWrapper(completionCallback) : nullptr;
224
Lais Andrade641248e2024-02-16 17:49:36 +0000225 auto ret = HalResultFactory::fromStatus(getHal()->on(timeout.count(), cb));
Lais Andrade10d9dc72020-05-20 12:00:49 +0000226 if (!supportsCallback && ret.isOk()) {
227 mCallbackScheduler->schedule(completionCallback, timeout);
228 }
229
230 return ret;
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100231}
232
233HalResult<void> AidlHalWrapper::off() {
Lais Andrade641248e2024-02-16 17:49:36 +0000234 return HalResultFactory::fromStatus(getHal()->off());
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100235}
236
Lais Andrade4e2b2d42021-02-15 20:58:51 +0000237HalResult<void> AidlHalWrapper::setAmplitude(float amplitude) {
Lais Andrade641248e2024-02-16 17:49:36 +0000238 return HalResultFactory::fromStatus(getHal()->setAmplitude(amplitude));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100239}
240
241HalResult<void> AidlHalWrapper::setExternalControl(bool enabled) {
Lais Andrade641248e2024-02-16 17:49:36 +0000242 return HalResultFactory::fromStatus(getHal()->setExternalControl(enabled));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100243}
244
245HalResult<void> AidlHalWrapper::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) {
Lais Andrade641248e2024-02-16 17:49:36 +0000246 return HalResultFactory::fromStatus(getHal()->alwaysOnEnable(id, effect, strength));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100247}
248
249HalResult<void> AidlHalWrapper::alwaysOnDisable(int32_t id) {
Lais Andrade641248e2024-02-16 17:49:36 +0000250 return HalResultFactory::fromStatus(getHal()->alwaysOnDisable(id));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100251}
252
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100253HalResult<milliseconds> AidlHalWrapper::performEffect(
254 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
Lais Andrade10d9dc72020-05-20 12:00:49 +0000255 HalResult<Capabilities> capabilities = getCapabilities();
256 bool supportsCallback = capabilities.isOk() &&
257 static_cast<int32_t>(capabilities.value() & Capabilities::PERFORM_CALLBACK);
258 auto cb = supportsCallback ? new HalCallbackWrapper(completionCallback) : nullptr;
259
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100260 int32_t lengthMs;
Lais Andradecfd81152020-07-01 09:00:26 +0000261 auto result = getHal()->perform(effect, strength, cb, &lengthMs);
Lais Andrade10d9dc72020-05-20 12:00:49 +0000262 milliseconds length = milliseconds(lengthMs);
263
Lais Andrade641248e2024-02-16 17:49:36 +0000264 auto ret = HalResultFactory::fromStatus<milliseconds>(result, length);
Lais Andrade10d9dc72020-05-20 12:00:49 +0000265 if (!supportsCallback && ret.isOk()) {
266 mCallbackScheduler->schedule(completionCallback, length);
267 }
268
269 return ret;
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100270}
271
Lais Andrade49b60b12021-02-23 13:27:41 +0000272HalResult<milliseconds> AidlHalWrapper::performComposedEffect(
Lais Andrade92f2af52021-03-22 16:12:50 +0000273 const std::vector<CompositeEffect>& primitives,
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100274 const std::function<void()>& completionCallback) {
Lais Andrade10d9dc72020-05-20 12:00:49 +0000275 // This method should always support callbacks, so no need to double check.
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100276 auto cb = new HalCallbackWrapper(completionCallback);
Lais Andrade965284b2021-03-19 20:58:15 +0000277
278 auto durations = getPrimitiveDurations().valueOr({});
Lais Andrade49b60b12021-02-23 13:27:41 +0000279 milliseconds duration(0);
Lais Andrade92f2af52021-03-22 16:12:50 +0000280 for (const auto& effect : primitives) {
Lais Andrade965284b2021-03-19 20:58:15 +0000281 auto primitiveIdx = static_cast<size_t>(effect.primitive);
282 if (primitiveIdx < durations.size()) {
283 duration += durations[primitiveIdx];
284 } else {
285 // Make sure the returned duration is positive to indicate successful vibration.
286 duration += milliseconds(1);
Lais Andrade49b60b12021-02-23 13:27:41 +0000287 }
288 duration += milliseconds(effect.delayMs);
289 }
Lais Andrade49b60b12021-02-23 13:27:41 +0000290
Lais Andrade641248e2024-02-16 17:49:36 +0000291 return HalResultFactory::fromStatus<milliseconds>(getHal()->compose(primitives, cb), duration);
Lais Andrade92f2af52021-03-22 16:12:50 +0000292}
293
294HalResult<void> AidlHalWrapper::performPwleEffect(const std::vector<PrimitivePwle>& primitives,
295 const std::function<void()>& completionCallback) {
296 // This method should always support callbacks, so no need to double check.
297 auto cb = new HalCallbackWrapper(completionCallback);
Lais Andrade641248e2024-02-16 17:49:36 +0000298 return HalResultFactory::fromStatus(getHal()->composePwle(primitives, cb));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100299}
300
Lais Andrade10d9dc72020-05-20 12:00:49 +0000301HalResult<Capabilities> AidlHalWrapper::getCapabilitiesInternal() {
302 int32_t capabilities = 0;
Lais Andradecfd81152020-07-01 09:00:26 +0000303 auto result = getHal()->getCapabilities(&capabilities);
Lais Andrade641248e2024-02-16 17:49:36 +0000304 return HalResultFactory::fromStatus<Capabilities>(result,
305 static_cast<Capabilities>(capabilities));
Lais Andrade10d9dc72020-05-20 12:00:49 +0000306}
307
308HalResult<std::vector<Effect>> AidlHalWrapper::getSupportedEffectsInternal() {
309 std::vector<Effect> supportedEffects;
Lais Andradecfd81152020-07-01 09:00:26 +0000310 auto result = getHal()->getSupportedEffects(&supportedEffects);
Lais Andrade641248e2024-02-16 17:49:36 +0000311 return HalResultFactory::fromStatus<std::vector<Effect>>(result, supportedEffects);
Lais Andrade10d9dc72020-05-20 12:00:49 +0000312}
313
Lais Andrade92f2af52021-03-22 16:12:50 +0000314HalResult<std::vector<Braking>> AidlHalWrapper::getSupportedBrakingInternal() {
315 std::vector<Braking> supportedBraking;
316 auto result = getHal()->getSupportedBraking(&supportedBraking);
Lais Andrade641248e2024-02-16 17:49:36 +0000317 return HalResultFactory::fromStatus<std::vector<Braking>>(result, supportedBraking);
Lais Andrade92f2af52021-03-22 16:12:50 +0000318}
319
Lais Andrade07f9c0e2020-08-11 16:22:12 +0000320HalResult<std::vector<CompositePrimitive>> AidlHalWrapper::getSupportedPrimitivesInternal() {
321 std::vector<CompositePrimitive> supportedPrimitives;
322 auto result = getHal()->getSupportedPrimitives(&supportedPrimitives);
Lais Andrade641248e2024-02-16 17:49:36 +0000323 return HalResultFactory::fromStatus<std::vector<CompositePrimitive>>(result,
324 supportedPrimitives);
Lais Andrade07f9c0e2020-08-11 16:22:12 +0000325}
326
Lais Andrade965284b2021-03-19 20:58:15 +0000327HalResult<std::vector<milliseconds>> AidlHalWrapper::getPrimitiveDurationsInternal(
328 const std::vector<CompositePrimitive>& supportedPrimitives) {
329 std::vector<milliseconds> durations;
330 constexpr auto primitiveRange = enum_range<CompositePrimitive>();
331 constexpr auto primitiveCount = std::distance(primitiveRange.begin(), primitiveRange.end());
332 durations.resize(primitiveCount);
333
334 for (auto primitive : supportedPrimitives) {
335 auto primitiveIdx = static_cast<size_t>(primitive);
336 if (primitiveIdx >= durations.size()) {
337 // Safety check, should not happen if enum_range is correct.
Lais Andrade4928bfd2021-08-25 18:21:12 +0100338 ALOGE("Supported primitive %zu is outside range [0,%zu), skipping load duration",
339 primitiveIdx, durations.size());
Lais Andrade965284b2021-03-19 20:58:15 +0000340 continue;
341 }
342 int32_t duration = 0;
Lais Andrade4928bfd2021-08-25 18:21:12 +0100343 auto result = getHal()->getPrimitiveDuration(primitive, &duration);
Lais Andrade641248e2024-02-16 17:49:36 +0000344 auto halResult = HalResultFactory::fromStatus<int32_t>(result, duration);
Lais Andrade4928bfd2021-08-25 18:21:12 +0100345 if (halResult.isUnsupported()) {
346 // Should not happen, supported primitives should always support requesting duration.
347 ALOGE("Supported primitive %zu returned unsupported for getPrimitiveDuration",
348 primitiveIdx);
349 }
350 if (halResult.isFailed()) {
351 // Fail entire request if one request has failed.
352 return HalResult<std::vector<milliseconds>>::failed(result.toString8().c_str());
Lais Andrade965284b2021-03-19 20:58:15 +0000353 }
354 durations[primitiveIdx] = milliseconds(duration);
355 }
356
357 return HalResult<std::vector<milliseconds>>::ok(durations);
358}
359
Lais Andrade4aa80c92021-06-03 17:20:16 +0100360HalResult<milliseconds> AidlHalWrapper::getPrimitiveDelayMaxInternal() {
361 int32_t delay = 0;
362 auto result = getHal()->getCompositionDelayMax(&delay);
Lais Andrade641248e2024-02-16 17:49:36 +0000363 return HalResultFactory::fromStatus<milliseconds>(result, milliseconds(delay));
Lais Andrade4aa80c92021-06-03 17:20:16 +0100364}
365
366HalResult<milliseconds> AidlHalWrapper::getPrimitiveDurationMaxInternal() {
367 int32_t delay = 0;
368 auto result = getHal()->getPwlePrimitiveDurationMax(&delay);
Lais Andrade641248e2024-02-16 17:49:36 +0000369 return HalResultFactory::fromStatus<milliseconds>(result, milliseconds(delay));
Lais Andrade4aa80c92021-06-03 17:20:16 +0100370}
371
372HalResult<int32_t> AidlHalWrapper::getCompositionSizeMaxInternal() {
373 int32_t size = 0;
374 auto result = getHal()->getCompositionSizeMax(&size);
Lais Andrade641248e2024-02-16 17:49:36 +0000375 return HalResultFactory::fromStatus<int32_t>(result, size);
Lais Andrade4aa80c92021-06-03 17:20:16 +0100376}
377
378HalResult<int32_t> AidlHalWrapper::getPwleSizeMaxInternal() {
379 int32_t size = 0;
380 auto result = getHal()->getPwleCompositionSizeMax(&size);
Lais Andrade641248e2024-02-16 17:49:36 +0000381 return HalResultFactory::fromStatus<int32_t>(result, size);
Lais Andrade4aa80c92021-06-03 17:20:16 +0100382}
383
Lais Andrade92f2af52021-03-22 16:12:50 +0000384HalResult<float> AidlHalWrapper::getMinFrequencyInternal() {
385 float minFrequency = 0;
386 auto result = getHal()->getFrequencyMinimum(&minFrequency);
Lais Andrade641248e2024-02-16 17:49:36 +0000387 return HalResultFactory::fromStatus<float>(result, minFrequency);
Lais Andrade92f2af52021-03-22 16:12:50 +0000388}
389
Michael Wrightba9a6ce2021-02-26 02:55:29 +0000390HalResult<float> AidlHalWrapper::getResonantFrequencyInternal() {
391 float f0 = 0;
392 auto result = getHal()->getResonantFrequency(&f0);
Lais Andrade641248e2024-02-16 17:49:36 +0000393 return HalResultFactory::fromStatus<float>(result, f0);
Michael Wrightba9a6ce2021-02-26 02:55:29 +0000394}
395
Lais Andrade92f2af52021-03-22 16:12:50 +0000396HalResult<float> AidlHalWrapper::getFrequencyResolutionInternal() {
397 float frequencyResolution = 0;
398 auto result = getHal()->getFrequencyResolution(&frequencyResolution);
Lais Andrade641248e2024-02-16 17:49:36 +0000399 return HalResultFactory::fromStatus<float>(result, frequencyResolution);
Lais Andrade92f2af52021-03-22 16:12:50 +0000400}
401
Michael Wrightba9a6ce2021-02-26 02:55:29 +0000402HalResult<float> AidlHalWrapper::getQFactorInternal() {
403 float qFactor = 0;
404 auto result = getHal()->getQFactor(&qFactor);
Lais Andrade641248e2024-02-16 17:49:36 +0000405 return HalResultFactory::fromStatus<float>(result, qFactor);
Michael Wrightba9a6ce2021-02-26 02:55:29 +0000406}
407
Lais Andrade92f2af52021-03-22 16:12:50 +0000408HalResult<std::vector<float>> AidlHalWrapper::getMaxAmplitudesInternal() {
409 std::vector<float> amplitudes;
410 auto result = getHal()->getBandwidthAmplitudeMap(&amplitudes);
Lais Andrade641248e2024-02-16 17:49:36 +0000411 return HalResultFactory::fromStatus<std::vector<float>>(result, amplitudes);
Lais Andrade92f2af52021-03-22 16:12:50 +0000412}
413
Lais Andradecfd81152020-07-01 09:00:26 +0000414sp<Aidl::IVibrator> AidlHalWrapper::getHal() {
415 std::lock_guard<std::mutex> lock(mHandleMutex);
416 return mHandle;
417}
418
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100419// -------------------------------------------------------------------------------------------------
420
Lais Andradecfd81152020-07-01 09:00:26 +0000421template <typename I>
422HalResult<void> HidlHalWrapper<I>::ping() {
423 auto result = getHal()->ping();
Lais Andrade641248e2024-02-16 17:49:36 +0000424 return HalResultFactory::fromReturn(result);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100425}
426
Lais Andradecfd81152020-07-01 09:00:26 +0000427template <typename I>
428void HidlHalWrapper<I>::tryReconnect() {
429 sp<I> newHandle = I::tryGetService();
430 if (newHandle) {
431 std::lock_guard<std::mutex> lock(mHandleMutex);
432 mHandle = std::move(newHandle);
433 }
434}
435
436template <typename I>
437HalResult<void> HidlHalWrapper<I>::on(milliseconds timeout,
438 const std::function<void()>& completionCallback) {
439 auto result = getHal()->on(timeout.count());
Lais Andrade641248e2024-02-16 17:49:36 +0000440 auto ret = HalResultFactory::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR));
Lais Andrade10d9dc72020-05-20 12:00:49 +0000441 if (ret.isOk()) {
442 mCallbackScheduler->schedule(completionCallback, timeout);
443 }
444 return ret;
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100445}
446
Lais Andradecfd81152020-07-01 09:00:26 +0000447template <typename I>
448HalResult<void> HidlHalWrapper<I>::off() {
449 auto result = getHal()->off();
Lais Andrade641248e2024-02-16 17:49:36 +0000450 return HalResultFactory::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100451}
452
Lais Andradecfd81152020-07-01 09:00:26 +0000453template <typename I>
Lais Andrade4e2b2d42021-02-15 20:58:51 +0000454HalResult<void> HidlHalWrapper<I>::setAmplitude(float amplitude) {
455 uint8_t amp = static_cast<uint8_t>(amplitude * std::numeric_limits<uint8_t>::max());
456 auto result = getHal()->setAmplitude(amp);
Lais Andrade641248e2024-02-16 17:49:36 +0000457 return HalResultFactory::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100458}
459
Lais Andradecfd81152020-07-01 09:00:26 +0000460template <typename I>
461HalResult<void> HidlHalWrapper<I>::setExternalControl(bool) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100462 ALOGV("Skipped setExternalControl because Vibrator HAL does not support it");
463 return HalResult<void>::unsupported();
464}
465
Lais Andradecfd81152020-07-01 09:00:26 +0000466template <typename I>
467HalResult<void> HidlHalWrapper<I>::alwaysOnEnable(int32_t, Effect, EffectStrength) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100468 ALOGV("Skipped alwaysOnEnable because Vibrator HAL AIDL is not available");
469 return HalResult<void>::unsupported();
470}
471
Lais Andradecfd81152020-07-01 09:00:26 +0000472template <typename I>
473HalResult<void> HidlHalWrapper<I>::alwaysOnDisable(int32_t) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100474 ALOGV("Skipped alwaysOnDisable because Vibrator HAL AIDL is not available");
475 return HalResult<void>::unsupported();
476}
477
Lais Andradecfd81152020-07-01 09:00:26 +0000478template <typename I>
Lais Andradecfd81152020-07-01 09:00:26 +0000479HalResult<Capabilities> HidlHalWrapper<I>::getCapabilitiesInternal() {
480 hardware::Return<bool> result = getHal()->supportsAmplitudeControl();
Lais Andraded39ff7d2020-05-19 10:42:51 +0000481 Capabilities capabilities =
482 result.withDefault(false) ? Capabilities::AMPLITUDE_CONTROL : Capabilities::NONE;
Lais Andrade641248e2024-02-16 17:49:36 +0000483 return HalResultFactory::fromReturn<Capabilities>(result, capabilities);
Lais Andraded39ff7d2020-05-19 10:42:51 +0000484}
485
Lais Andradecfd81152020-07-01 09:00:26 +0000486template <typename I>
487template <typename T>
488HalResult<milliseconds> HidlHalWrapper<I>::performInternal(
489 perform_fn<T> performFn, sp<I> handle, T effect, EffectStrength strength,
Lais Andrade10d9dc72020-05-20 12:00:49 +0000490 const std::function<void()>& completionCallback) {
491 V1_0::Status status;
492 int32_t lengthMs;
493 auto effectCallback = [&status, &lengthMs](V1_0::Status retStatus, uint32_t retLengthMs) {
494 status = retStatus;
495 lengthMs = retLengthMs;
496 };
497
498 V1_0::EffectStrength effectStrength = static_cast<V1_0::EffectStrength>(strength);
499 auto result = std::invoke(performFn, handle, effect, effectStrength, effectCallback);
500 milliseconds length = milliseconds(lengthMs);
501
Lais Andrade641248e2024-02-16 17:49:36 +0000502 auto ret = HalResultFactory::fromReturn<milliseconds>(result, status, length);
Lais Andrade10d9dc72020-05-20 12:00:49 +0000503 if (ret.isOk()) {
504 mCallbackScheduler->schedule(completionCallback, length);
505 }
506
507 return ret;
508}
509
Lais Andradecfd81152020-07-01 09:00:26 +0000510template <typename I>
511sp<I> HidlHalWrapper<I>::getHal() {
512 std::lock_guard<std::mutex> lock(mHandleMutex);
513 return mHandle;
514}
515
516// -------------------------------------------------------------------------------------------------
517
518HalResult<milliseconds> HidlHalWrapperV1_0::performEffect(
Lais Andrade10d9dc72020-05-20 12:00:49 +0000519 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
Lais Andradecfd81152020-07-01 09:00:26 +0000520 if (isStaticCastValid<V1_0::Effect>(effect)) {
521 return performInternal(&V1_0::IVibrator::perform, getHal(),
522 static_cast<V1_0::Effect>(effect), strength, completionCallback);
523 }
524
525 ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s",
526 Aidl::toString(effect).c_str());
527 return HalResult<milliseconds>::unsupported();
Lais Andrade10d9dc72020-05-20 12:00:49 +0000528}
529
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100530// -------------------------------------------------------------------------------------------------
531
Lais Andrade10d9dc72020-05-20 12:00:49 +0000532HalResult<milliseconds> HidlHalWrapperV1_1::performEffect(
533 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100534 if (isStaticCastValid<V1_0::Effect>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000535 return performInternal(&V1_1::IVibrator::perform, getHal(),
536 static_cast<V1_0::Effect>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100537 }
538 if (isStaticCastValid<V1_1::Effect_1_1>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000539 return performInternal(&V1_1::IVibrator::perform_1_1, getHal(),
540 static_cast<V1_1::Effect_1_1>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100541 }
542
543 ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s",
544 Aidl::toString(effect).c_str());
545 return HalResult<milliseconds>::unsupported();
546}
547
548// -------------------------------------------------------------------------------------------------
549
Lais Andrade10d9dc72020-05-20 12:00:49 +0000550HalResult<milliseconds> HidlHalWrapperV1_2::performEffect(
551 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100552 if (isStaticCastValid<V1_0::Effect>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000553 return performInternal(&V1_2::IVibrator::perform, getHal(),
554 static_cast<V1_0::Effect>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100555 }
556 if (isStaticCastValid<V1_1::Effect_1_1>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000557 return performInternal(&V1_2::IVibrator::perform_1_1, getHal(),
558 static_cast<V1_1::Effect_1_1>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100559 }
560 if (isStaticCastValid<V1_2::Effect>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000561 return performInternal(&V1_2::IVibrator::perform_1_2, getHal(),
562 static_cast<V1_2::Effect>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100563 }
564
565 ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s",
566 Aidl::toString(effect).c_str());
567 return HalResult<milliseconds>::unsupported();
568}
569
570// -------------------------------------------------------------------------------------------------
571
572HalResult<void> HidlHalWrapperV1_3::setExternalControl(bool enabled) {
Lais Andradecfd81152020-07-01 09:00:26 +0000573 auto result = getHal()->setExternalControl(static_cast<uint32_t>(enabled));
Lais Andrade641248e2024-02-16 17:49:36 +0000574 return HalResultFactory::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100575}
576
Lais Andrade10d9dc72020-05-20 12:00:49 +0000577HalResult<milliseconds> HidlHalWrapperV1_3::performEffect(
578 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100579 if (isStaticCastValid<V1_0::Effect>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000580 return performInternal(&V1_3::IVibrator::perform, getHal(),
581 static_cast<V1_0::Effect>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100582 }
583 if (isStaticCastValid<V1_1::Effect_1_1>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000584 return performInternal(&V1_3::IVibrator::perform_1_1, getHal(),
585 static_cast<V1_1::Effect_1_1>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100586 }
587 if (isStaticCastValid<V1_2::Effect>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000588 return performInternal(&V1_3::IVibrator::perform_1_2, getHal(),
589 static_cast<V1_2::Effect>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100590 }
591 if (isStaticCastValid<V1_3::Effect>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000592 return performInternal(&V1_3::IVibrator::perform_1_3, getHal(),
593 static_cast<V1_3::Effect>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100594 }
595
596 ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s",
597 Aidl::toString(effect).c_str());
598 return HalResult<milliseconds>::unsupported();
599}
600
Lais Andraded39ff7d2020-05-19 10:42:51 +0000601HalResult<Capabilities> HidlHalWrapperV1_3::getCapabilitiesInternal() {
Lais Andrade08666612020-08-07 16:16:31 +0000602 Capabilities capabilities = Capabilities::NONE;
603
Lais Andradecfd81152020-07-01 09:00:26 +0000604 sp<V1_3::IVibrator> hal = getHal();
605 auto amplitudeResult = hal->supportsAmplitudeControl();
606 if (!amplitudeResult.isOk()) {
Lais Andrade641248e2024-02-16 17:49:36 +0000607 return HalResultFactory::fromReturn<Capabilities>(amplitudeResult, capabilities);
Lais Andraded39ff7d2020-05-19 10:42:51 +0000608 }
609
Lais Andradecfd81152020-07-01 09:00:26 +0000610 auto externalControlResult = hal->supportsExternalControl();
Lais Andradecfd81152020-07-01 09:00:26 +0000611 if (amplitudeResult.withDefault(false)) {
612 capabilities |= Capabilities::AMPLITUDE_CONTROL;
613 }
614 if (externalControlResult.withDefault(false)) {
615 capabilities |= Capabilities::EXTERNAL_CONTROL;
Lais Andrade602ff962020-08-27 12:02:53 +0000616
617 if (amplitudeResult.withDefault(false)) {
618 capabilities |= Capabilities::EXTERNAL_AMPLITUDE_CONTROL;
619 }
Lais Andradecfd81152020-07-01 09:00:26 +0000620 }
621
Lais Andrade641248e2024-02-16 17:49:36 +0000622 return HalResultFactory::fromReturn<Capabilities>(externalControlResult, capabilities);
Lais Andrade10d9dc72020-05-20 12:00:49 +0000623}
624
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100625// -------------------------------------------------------------------------------------------------
626
627}; // namespace vibrator
628
629}; // namespace android