blob: 7fee82fb19decc1804b4b7c167bc36fe5fd658c9 [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>
22
23#include <utils/Log.h>
24
Lais Andrade10d9dc72020-05-20 12:00:49 +000025#include <vibratorservice/VibratorCallbackScheduler.h>
Lais Andrade9e9fcc92020-04-07 20:13:08 +010026#include <vibratorservice/VibratorHalWrapper.h>
27
28using android::hardware::vibrator::CompositeEffect;
Lais Andrade07f9c0e2020-08-11 16:22:12 +000029using android::hardware::vibrator::CompositePrimitive;
Lais Andrade9e9fcc92020-04-07 20:13:08 +010030using android::hardware::vibrator::Effect;
31using android::hardware::vibrator::EffectStrength;
32
33using std::chrono::milliseconds;
34
35namespace V1_0 = android::hardware::vibrator::V1_0;
36namespace V1_1 = android::hardware::vibrator::V1_1;
37namespace V1_2 = android::hardware::vibrator::V1_2;
38namespace V1_3 = android::hardware::vibrator::V1_3;
39namespace Aidl = android::hardware::vibrator;
40
41namespace android {
42
43namespace vibrator {
44
45// -------------------------------------------------------------------------------------------------
46
47template <class T>
Lais Andraded39ff7d2020-05-19 10:42:51 +000048HalResult<T> loadCached(const std::function<HalResult<T>()>& loadFn, std::optional<T>& cache) {
49 if (cache.has_value()) {
Lais Andrade10d9dc72020-05-20 12:00:49 +000050 // Return copy of cached value.
51 return HalResult<T>::ok(*cache);
Lais Andraded39ff7d2020-05-19 10:42:51 +000052 }
53 HalResult<T> ret = loadFn();
54 if (ret.isOk()) {
Lais Andrade10d9dc72020-05-20 12:00:49 +000055 // Cache copy of returned value.
Lais Andraded39ff7d2020-05-19 10:42:51 +000056 cache.emplace(ret.value());
57 }
58 return ret;
59}
60
61template <class T>
Lais Andrade9e9fcc92020-04-07 20:13:08 +010062bool isStaticCastValid(Effect effect) {
63 T castEffect = static_cast<T>(effect);
64 auto iter = hardware::hidl_enum_range<T>();
65 return castEffect >= *iter.begin() && castEffect <= *std::prev(iter.end());
66}
67
Lais Andrade9e9fcc92020-04-07 20:13:08 +010068// -------------------------------------------------------------------------------------------------
69
Lais Andrade08666612020-08-07 16:16:31 +000070const constexpr char* STATUS_T_ERROR_MESSAGE_PREFIX = "status_t = ";
71const constexpr char* STATUS_V_1_0_ERROR_MESSAGE_PREFIX =
72 "android::hardware::vibrator::V1_0::Status = ";
73
Lais Andrade9e9fcc92020-04-07 20:13:08 +010074template <typename T>
Lais Andrade9e9fcc92020-04-07 20:13:08 +010075HalResult<T> HalResult<T>::fromStatus(V1_0::Status status, T data) {
76 switch (status) {
77 case V1_0::Status::OK:
78 return HalResult<T>::ok(data);
79 case V1_0::Status::UNSUPPORTED_OPERATION:
80 return HalResult<T>::unsupported();
81 default:
Lais Andrade08666612020-08-07 16:16:31 +000082 return HalResult<T>::failed(STATUS_V_1_0_ERROR_MESSAGE_PREFIX + toString(status));
Lais Andrade9e9fcc92020-04-07 20:13:08 +010083 }
84}
85
86template <typename T>
87template <typename R>
88HalResult<T> HalResult<T>::fromReturn(hardware::Return<R>& ret, T data) {
Lais Andrade08666612020-08-07 16:16:31 +000089 return ret.isOk() ? HalResult<T>::ok(data) : HalResult<T>::failed(ret.description());
Lais Andrade9e9fcc92020-04-07 20:13:08 +010090}
91
92template <typename T>
93template <typename R>
94HalResult<T> HalResult<T>::fromReturn(hardware::Return<R>& ret, V1_0::Status status, T data) {
Lais Andrade08666612020-08-07 16:16:31 +000095 return ret.isOk() ? HalResult<T>::fromStatus(status, data)
96 : HalResult<T>::failed(ret.description());
Lais Andrade9e9fcc92020-04-07 20:13:08 +010097}
98
99// -------------------------------------------------------------------------------------------------
100
Lais Andrade08666612020-08-07 16:16:31 +0000101HalResult<void> HalResult<void>::fromStatus(status_t status) {
102 if (status == android::OK) {
103 return HalResult<void>::ok();
104 }
105 return HalResult<void>::failed(STATUS_T_ERROR_MESSAGE_PREFIX + statusToString(status));
106}
107
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100108HalResult<void> HalResult<void>::fromStatus(binder::Status status) {
109 if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) {
110 return HalResult<void>::unsupported();
111 }
112 if (status.isOk()) {
113 return HalResult<void>::ok();
114 }
Lais Andrade08666612020-08-07 16:16:31 +0000115 return HalResult<void>::failed(std::string(status.toString8().c_str()));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100116}
117
118HalResult<void> HalResult<void>::fromStatus(V1_0::Status status) {
119 switch (status) {
120 case V1_0::Status::OK:
121 return HalResult<void>::ok();
122 case V1_0::Status::UNSUPPORTED_OPERATION:
123 return HalResult<void>::unsupported();
124 default:
Lais Andrade08666612020-08-07 16:16:31 +0000125 return HalResult<void>::failed(STATUS_V_1_0_ERROR_MESSAGE_PREFIX + toString(status));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100126 }
127}
128
129template <typename R>
130HalResult<void> HalResult<void>::fromReturn(hardware::Return<R>& ret) {
Lais Andrade08666612020-08-07 16:16:31 +0000131 return ret.isOk() ? HalResult<void>::ok() : HalResult<void>::failed(ret.description());
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100132}
133
134// -------------------------------------------------------------------------------------------------
135
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100136HalResult<void> AidlHalWrapper::ping() {
Lais Andrade08666612020-08-07 16:16:31 +0000137 return HalResult<void>::fromStatus(IInterface::asBinder(getHal())->pingBinder());
Lais Andradecfd81152020-07-01 09:00:26 +0000138}
139
140void AidlHalWrapper::tryReconnect() {
Lais Andrade98c97032020-11-17 19:23:01 +0000141 auto result = mReconnectFn();
142 if (!result.isOk()) {
143 return;
144 }
145 sp<Aidl::IVibrator> newHandle = result.value();
Lais Andradecfd81152020-07-01 09:00:26 +0000146 if (newHandle) {
147 std::lock_guard<std::mutex> lock(mHandleMutex);
148 mHandle = std::move(newHandle);
149 }
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100150}
151
152HalResult<void> AidlHalWrapper::on(milliseconds timeout,
153 const std::function<void()>& completionCallback) {
Lais Andrade10d9dc72020-05-20 12:00:49 +0000154 HalResult<Capabilities> capabilities = getCapabilities();
155 bool supportsCallback = capabilities.isOk() &&
156 static_cast<int32_t>(capabilities.value() & Capabilities::ON_CALLBACK);
157 auto cb = supportsCallback ? new HalCallbackWrapper(completionCallback) : nullptr;
158
Lais Andradecfd81152020-07-01 09:00:26 +0000159 auto ret = HalResult<void>::fromStatus(getHal()->on(timeout.count(), cb));
Lais Andrade10d9dc72020-05-20 12:00:49 +0000160 if (!supportsCallback && ret.isOk()) {
161 mCallbackScheduler->schedule(completionCallback, timeout);
162 }
163
164 return ret;
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100165}
166
167HalResult<void> AidlHalWrapper::off() {
Lais Andradecfd81152020-07-01 09:00:26 +0000168 return HalResult<void>::fromStatus(getHal()->off());
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100169}
170
171HalResult<void> AidlHalWrapper::setAmplitude(int32_t amplitude) {
172 float convertedAmplitude = static_cast<float>(amplitude) / std::numeric_limits<uint8_t>::max();
Lais Andradecfd81152020-07-01 09:00:26 +0000173 return HalResult<void>::fromStatus(getHal()->setAmplitude(convertedAmplitude));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100174}
175
176HalResult<void> AidlHalWrapper::setExternalControl(bool enabled) {
Lais Andradecfd81152020-07-01 09:00:26 +0000177 return HalResult<void>::fromStatus(getHal()->setExternalControl(enabled));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100178}
179
180HalResult<void> AidlHalWrapper::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) {
Lais Andradecfd81152020-07-01 09:00:26 +0000181 return HalResult<void>::fromStatus(getHal()->alwaysOnEnable(id, effect, strength));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100182}
183
184HalResult<void> AidlHalWrapper::alwaysOnDisable(int32_t id) {
Lais Andradecfd81152020-07-01 09:00:26 +0000185 return HalResult<void>::fromStatus(getHal()->alwaysOnDisable(id));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100186}
187
188HalResult<Capabilities> AidlHalWrapper::getCapabilities() {
Lais Andraded39ff7d2020-05-19 10:42:51 +0000189 std::lock_guard<std::mutex> lock(mCapabilitiesMutex);
Lais Andrade10d9dc72020-05-20 12:00:49 +0000190 return loadCached<Capabilities>(std::bind(&AidlHalWrapper::getCapabilitiesInternal, this),
191 mCapabilities);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100192}
193
194HalResult<std::vector<Effect>> AidlHalWrapper::getSupportedEffects() {
Lais Andraded39ff7d2020-05-19 10:42:51 +0000195 std::lock_guard<std::mutex> lock(mSupportedEffectsMutex);
Lais Andrade10d9dc72020-05-20 12:00:49 +0000196 return loadCached<std::vector<Effect>>(std::bind(&AidlHalWrapper::getSupportedEffectsInternal,
197 this),
198 mSupportedEffects);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100199}
200
Lais Andrade07f9c0e2020-08-11 16:22:12 +0000201HalResult<std::vector<CompositePrimitive>> AidlHalWrapper::getSupportedPrimitives() {
202 std::lock_guard<std::mutex> lock(mSupportedPrimitivesMutex);
203 return loadCached<std::vector<
204 CompositePrimitive>>(std::bind(&AidlHalWrapper::getSupportedPrimitivesInternal, this),
205 mSupportedPrimitives);
206}
207
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100208HalResult<milliseconds> AidlHalWrapper::performEffect(
209 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
Lais Andrade10d9dc72020-05-20 12:00:49 +0000210 HalResult<Capabilities> capabilities = getCapabilities();
211 bool supportsCallback = capabilities.isOk() &&
212 static_cast<int32_t>(capabilities.value() & Capabilities::PERFORM_CALLBACK);
213 auto cb = supportsCallback ? new HalCallbackWrapper(completionCallback) : nullptr;
214
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100215 int32_t lengthMs;
Lais Andradecfd81152020-07-01 09:00:26 +0000216 auto result = getHal()->perform(effect, strength, cb, &lengthMs);
Lais Andrade10d9dc72020-05-20 12:00:49 +0000217 milliseconds length = milliseconds(lengthMs);
218
219 auto ret = HalResult<milliseconds>::fromStatus(result, length);
220 if (!supportsCallback && ret.isOk()) {
221 mCallbackScheduler->schedule(completionCallback, length);
222 }
223
224 return ret;
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100225}
226
227HalResult<void> AidlHalWrapper::performComposedEffect(
228 const std::vector<CompositeEffect>& primitiveEffects,
229 const std::function<void()>& completionCallback) {
Lais Andrade10d9dc72020-05-20 12:00:49 +0000230 // This method should always support callbacks, so no need to double check.
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100231 auto cb = new HalCallbackWrapper(completionCallback);
Lais Andradecfd81152020-07-01 09:00:26 +0000232 return HalResult<void>::fromStatus(getHal()->compose(primitiveEffects, cb));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100233}
234
Lais Andrade10d9dc72020-05-20 12:00:49 +0000235HalResult<Capabilities> AidlHalWrapper::getCapabilitiesInternal() {
236 int32_t capabilities = 0;
Lais Andradecfd81152020-07-01 09:00:26 +0000237 auto result = getHal()->getCapabilities(&capabilities);
Lais Andrade10d9dc72020-05-20 12:00:49 +0000238 return HalResult<Capabilities>::fromStatus(result, static_cast<Capabilities>(capabilities));
239}
240
241HalResult<std::vector<Effect>> AidlHalWrapper::getSupportedEffectsInternal() {
242 std::vector<Effect> supportedEffects;
Lais Andradecfd81152020-07-01 09:00:26 +0000243 auto result = getHal()->getSupportedEffects(&supportedEffects);
Lais Andrade10d9dc72020-05-20 12:00:49 +0000244 return HalResult<std::vector<Effect>>::fromStatus(result, supportedEffects);
245}
246
Lais Andrade07f9c0e2020-08-11 16:22:12 +0000247HalResult<std::vector<CompositePrimitive>> AidlHalWrapper::getSupportedPrimitivesInternal() {
248 std::vector<CompositePrimitive> supportedPrimitives;
249 auto result = getHal()->getSupportedPrimitives(&supportedPrimitives);
250 return HalResult<std::vector<CompositePrimitive>>::fromStatus(result, supportedPrimitives);
251}
252
Lais Andradecfd81152020-07-01 09:00:26 +0000253sp<Aidl::IVibrator> AidlHalWrapper::getHal() {
254 std::lock_guard<std::mutex> lock(mHandleMutex);
255 return mHandle;
256}
257
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100258// -------------------------------------------------------------------------------------------------
259
Lais Andradecfd81152020-07-01 09:00:26 +0000260template <typename I>
261HalResult<void> HidlHalWrapper<I>::ping() {
262 auto result = getHal()->ping();
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100263 return HalResult<void>::fromReturn(result);
264}
265
Lais Andradecfd81152020-07-01 09:00:26 +0000266template <typename I>
267void HidlHalWrapper<I>::tryReconnect() {
268 sp<I> newHandle = I::tryGetService();
269 if (newHandle) {
270 std::lock_guard<std::mutex> lock(mHandleMutex);
271 mHandle = std::move(newHandle);
272 }
273}
274
275template <typename I>
276HalResult<void> HidlHalWrapper<I>::on(milliseconds timeout,
277 const std::function<void()>& completionCallback) {
278 auto result = getHal()->on(timeout.count());
Lais Andrade10d9dc72020-05-20 12:00:49 +0000279 auto ret = HalResult<void>::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR));
280 if (ret.isOk()) {
281 mCallbackScheduler->schedule(completionCallback, timeout);
282 }
283 return ret;
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100284}
285
Lais Andradecfd81152020-07-01 09:00:26 +0000286template <typename I>
287HalResult<void> HidlHalWrapper<I>::off() {
288 auto result = getHal()->off();
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100289 return HalResult<void>::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR));
290}
291
Lais Andradecfd81152020-07-01 09:00:26 +0000292template <typename I>
293HalResult<void> HidlHalWrapper<I>::setAmplitude(int32_t amplitude) {
294 auto result = getHal()->setAmplitude(static_cast<uint8_t>(amplitude));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100295 return HalResult<void>::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR));
296}
297
Lais Andradecfd81152020-07-01 09:00:26 +0000298template <typename I>
299HalResult<void> HidlHalWrapper<I>::setExternalControl(bool) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100300 ALOGV("Skipped setExternalControl because Vibrator HAL does not support it");
301 return HalResult<void>::unsupported();
302}
303
Lais Andradecfd81152020-07-01 09:00:26 +0000304template <typename I>
305HalResult<void> HidlHalWrapper<I>::alwaysOnEnable(int32_t, Effect, EffectStrength) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100306 ALOGV("Skipped alwaysOnEnable because Vibrator HAL AIDL is not available");
307 return HalResult<void>::unsupported();
308}
309
Lais Andradecfd81152020-07-01 09:00:26 +0000310template <typename I>
311HalResult<void> HidlHalWrapper<I>::alwaysOnDisable(int32_t) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100312 ALOGV("Skipped alwaysOnDisable because Vibrator HAL AIDL is not available");
313 return HalResult<void>::unsupported();
314}
315
Lais Andradecfd81152020-07-01 09:00:26 +0000316template <typename I>
317HalResult<Capabilities> HidlHalWrapper<I>::getCapabilities() {
Lais Andraded39ff7d2020-05-19 10:42:51 +0000318 std::lock_guard<std::mutex> lock(mCapabilitiesMutex);
Lais Andradecfd81152020-07-01 09:00:26 +0000319 return loadCached<Capabilities>(std::bind(&HidlHalWrapper<I>::getCapabilitiesInternal, this),
Lais Andraded39ff7d2020-05-19 10:42:51 +0000320 mCapabilities);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100321}
322
Lais Andradecfd81152020-07-01 09:00:26 +0000323template <typename I>
324HalResult<std::vector<Effect>> HidlHalWrapper<I>::getSupportedEffects() {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100325 ALOGV("Skipped getSupportedEffects because Vibrator HAL AIDL is not available");
326 return HalResult<std::vector<Effect>>::unsupported();
327}
328
Lais Andradecfd81152020-07-01 09:00:26 +0000329template <typename I>
Lais Andrade07f9c0e2020-08-11 16:22:12 +0000330HalResult<std::vector<CompositePrimitive>> HidlHalWrapper<I>::getSupportedPrimitives() {
331 ALOGV("Skipped getSupportedPrimitives because Vibrator HAL AIDL is not available");
332 return HalResult<std::vector<CompositePrimitive>>::unsupported();
333}
334
335template <typename I>
Lais Andradecfd81152020-07-01 09:00:26 +0000336HalResult<void> HidlHalWrapper<I>::performComposedEffect(const std::vector<CompositeEffect>&,
337 const std::function<void()>&) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100338 ALOGV("Skipped composed effect because Vibrator HAL AIDL is not available");
339 return HalResult<void>::unsupported();
340}
341
Lais Andradecfd81152020-07-01 09:00:26 +0000342template <typename I>
343HalResult<Capabilities> HidlHalWrapper<I>::getCapabilitiesInternal() {
344 hardware::Return<bool> result = getHal()->supportsAmplitudeControl();
Lais Andraded39ff7d2020-05-19 10:42:51 +0000345 Capabilities capabilities =
346 result.withDefault(false) ? Capabilities::AMPLITUDE_CONTROL : Capabilities::NONE;
347 return HalResult<Capabilities>::fromReturn(result, capabilities);
348}
349
Lais Andradecfd81152020-07-01 09:00:26 +0000350template <typename I>
351template <typename T>
352HalResult<milliseconds> HidlHalWrapper<I>::performInternal(
353 perform_fn<T> performFn, sp<I> handle, T effect, EffectStrength strength,
Lais Andrade10d9dc72020-05-20 12:00:49 +0000354 const std::function<void()>& completionCallback) {
355 V1_0::Status status;
356 int32_t lengthMs;
357 auto effectCallback = [&status, &lengthMs](V1_0::Status retStatus, uint32_t retLengthMs) {
358 status = retStatus;
359 lengthMs = retLengthMs;
360 };
361
362 V1_0::EffectStrength effectStrength = static_cast<V1_0::EffectStrength>(strength);
363 auto result = std::invoke(performFn, handle, effect, effectStrength, effectCallback);
364 milliseconds length = milliseconds(lengthMs);
365
366 auto ret = HalResult<milliseconds>::fromReturn(result, status, length);
367 if (ret.isOk()) {
368 mCallbackScheduler->schedule(completionCallback, length);
369 }
370
371 return ret;
372}
373
Lais Andradecfd81152020-07-01 09:00:26 +0000374template <typename I>
375sp<I> HidlHalWrapper<I>::getHal() {
376 std::lock_guard<std::mutex> lock(mHandleMutex);
377 return mHandle;
378}
379
380// -------------------------------------------------------------------------------------------------
381
382HalResult<milliseconds> HidlHalWrapperV1_0::performEffect(
Lais Andrade10d9dc72020-05-20 12:00:49 +0000383 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
Lais Andradecfd81152020-07-01 09:00:26 +0000384 if (isStaticCastValid<V1_0::Effect>(effect)) {
385 return performInternal(&V1_0::IVibrator::perform, getHal(),
386 static_cast<V1_0::Effect>(effect), strength, completionCallback);
387 }
388
389 ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s",
390 Aidl::toString(effect).c_str());
391 return HalResult<milliseconds>::unsupported();
Lais Andrade10d9dc72020-05-20 12:00:49 +0000392}
393
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100394// -------------------------------------------------------------------------------------------------
395
Lais Andrade10d9dc72020-05-20 12:00:49 +0000396HalResult<milliseconds> HidlHalWrapperV1_1::performEffect(
397 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100398 if (isStaticCastValid<V1_0::Effect>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000399 return performInternal(&V1_1::IVibrator::perform, getHal(),
400 static_cast<V1_0::Effect>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100401 }
402 if (isStaticCastValid<V1_1::Effect_1_1>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000403 return performInternal(&V1_1::IVibrator::perform_1_1, getHal(),
404 static_cast<V1_1::Effect_1_1>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100405 }
406
407 ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s",
408 Aidl::toString(effect).c_str());
409 return HalResult<milliseconds>::unsupported();
410}
411
412// -------------------------------------------------------------------------------------------------
413
Lais Andrade10d9dc72020-05-20 12:00:49 +0000414HalResult<milliseconds> HidlHalWrapperV1_2::performEffect(
415 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100416 if (isStaticCastValid<V1_0::Effect>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000417 return performInternal(&V1_2::IVibrator::perform, getHal(),
418 static_cast<V1_0::Effect>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100419 }
420 if (isStaticCastValid<V1_1::Effect_1_1>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000421 return performInternal(&V1_2::IVibrator::perform_1_1, getHal(),
422 static_cast<V1_1::Effect_1_1>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100423 }
424 if (isStaticCastValid<V1_2::Effect>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000425 return performInternal(&V1_2::IVibrator::perform_1_2, getHal(),
426 static_cast<V1_2::Effect>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100427 }
428
429 ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s",
430 Aidl::toString(effect).c_str());
431 return HalResult<milliseconds>::unsupported();
432}
433
434// -------------------------------------------------------------------------------------------------
435
436HalResult<void> HidlHalWrapperV1_3::setExternalControl(bool enabled) {
Lais Andradecfd81152020-07-01 09:00:26 +0000437 auto result = getHal()->setExternalControl(static_cast<uint32_t>(enabled));
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100438 return HalResult<void>::fromStatus(result.withDefault(V1_0::Status::UNKNOWN_ERROR));
439}
440
Lais Andrade10d9dc72020-05-20 12:00:49 +0000441HalResult<milliseconds> HidlHalWrapperV1_3::performEffect(
442 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100443 if (isStaticCastValid<V1_0::Effect>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000444 return performInternal(&V1_3::IVibrator::perform, getHal(),
445 static_cast<V1_0::Effect>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100446 }
447 if (isStaticCastValid<V1_1::Effect_1_1>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000448 return performInternal(&V1_3::IVibrator::perform_1_1, getHal(),
449 static_cast<V1_1::Effect_1_1>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100450 }
451 if (isStaticCastValid<V1_2::Effect>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000452 return performInternal(&V1_3::IVibrator::perform_1_2, getHal(),
453 static_cast<V1_2::Effect>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100454 }
455 if (isStaticCastValid<V1_3::Effect>(effect)) {
Lais Andradecfd81152020-07-01 09:00:26 +0000456 return performInternal(&V1_3::IVibrator::perform_1_3, getHal(),
457 static_cast<V1_3::Effect>(effect), strength, completionCallback);
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100458 }
459
460 ALOGV("Skipped performEffect because Vibrator HAL does not support effect %s",
461 Aidl::toString(effect).c_str());
462 return HalResult<milliseconds>::unsupported();
463}
464
Lais Andraded39ff7d2020-05-19 10:42:51 +0000465HalResult<Capabilities> HidlHalWrapperV1_3::getCapabilitiesInternal() {
Lais Andrade08666612020-08-07 16:16:31 +0000466 Capabilities capabilities = Capabilities::NONE;
467
Lais Andradecfd81152020-07-01 09:00:26 +0000468 sp<V1_3::IVibrator> hal = getHal();
469 auto amplitudeResult = hal->supportsAmplitudeControl();
470 if (!amplitudeResult.isOk()) {
Lais Andrade08666612020-08-07 16:16:31 +0000471 return HalResult<Capabilities>::fromReturn(amplitudeResult, capabilities);
Lais Andraded39ff7d2020-05-19 10:42:51 +0000472 }
473
Lais Andradecfd81152020-07-01 09:00:26 +0000474 auto externalControlResult = hal->supportsExternalControl();
Lais Andradecfd81152020-07-01 09:00:26 +0000475 if (amplitudeResult.withDefault(false)) {
476 capabilities |= Capabilities::AMPLITUDE_CONTROL;
477 }
478 if (externalControlResult.withDefault(false)) {
479 capabilities |= Capabilities::EXTERNAL_CONTROL;
Lais Andrade602ff962020-08-27 12:02:53 +0000480
481 if (amplitudeResult.withDefault(false)) {
482 capabilities |= Capabilities::EXTERNAL_AMPLITUDE_CONTROL;
483 }
Lais Andradecfd81152020-07-01 09:00:26 +0000484 }
485
486 return HalResult<Capabilities>::fromReturn(externalControlResult, capabilities);
Lais Andrade10d9dc72020-05-20 12:00:49 +0000487}
488
Lais Andrade9e9fcc92020-04-07 20:13:08 +0100489// -------------------------------------------------------------------------------------------------
490
491}; // namespace vibrator
492
493}; // namespace android