blob: 46175ad834e588adf3963cea3cb588793a75ad4d [file] [log] [blame]
Lais Andrade921b6982020-06-04 16:17:53 +00001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "VibratorHalController"
18
19#include <android/hardware/vibrator/1.3/IVibrator.h>
20#include <android/hardware/vibrator/IVibrator.h>
21#include <binder/IServiceManager.h>
22#include <hardware/vibrator.h>
23
24#include <utils/Log.h>
25
26#include <vibratorservice/VibratorCallbackScheduler.h>
27#include <vibratorservice/VibratorHalController.h>
28#include <vibratorservice/VibratorHalWrapper.h>
29
30using android::hardware::vibrator::CompositeEffect;
Lais Andrade07f9c0e2020-08-11 16:22:12 +000031using android::hardware::vibrator::CompositePrimitive;
Lais Andrade921b6982020-06-04 16:17:53 +000032using android::hardware::vibrator::Effect;
33using android::hardware::vibrator::EffectStrength;
34
35using std::chrono::milliseconds;
36
37namespace V1_0 = android::hardware::vibrator::V1_0;
38namespace V1_1 = android::hardware::vibrator::V1_1;
39namespace V1_2 = android::hardware::vibrator::V1_2;
40namespace V1_3 = android::hardware::vibrator::V1_3;
41namespace Aidl = android::hardware::vibrator;
42
43namespace android {
44
45namespace vibrator {
46
47// -------------------------------------------------------------------------------------------------
48
Lais Andrade37d0f042020-06-18 16:39:55 +000049static constexpr int MAX_RETRIES = 1;
50
Lais Andradecfd81152020-07-01 09:00:26 +000051std::shared_ptr<HalWrapper> HalConnector::connect(std::shared_ptr<CallbackScheduler> scheduler) {
52 static bool gHalExists = true;
53 if (!gHalExists) {
54 // We already tried to connect to all of the vibrator HAL versions and none was available.
Lais Andrade921b6982020-06-04 16:17:53 +000055 return nullptr;
56 }
Lais Andrade921b6982020-06-04 16:17:53 +000057
Lais Andradecfd81152020-07-01 09:00:26 +000058 sp<Aidl::IVibrator> aidlHal = waitForVintfService<Aidl::IVibrator>();
Lais Andrade921b6982020-06-04 16:17:53 +000059 if (aidlHal) {
Lais Andradecfd81152020-07-01 09:00:26 +000060 ALOGV("Successfully connected to Vibrator HAL AIDL service.");
Lais Andrade921b6982020-06-04 16:17:53 +000061 return std::make_shared<AidlHalWrapper>(std::move(scheduler), aidlHal);
62 }
Lais Andradecfd81152020-07-01 09:00:26 +000063
64 sp<V1_0::IVibrator> halV1_0 = V1_0::IVibrator::getService();
Lais Andrade921b6982020-06-04 16:17:53 +000065 if (halV1_0 == nullptr) {
Lais Andradecfd81152020-07-01 09:00:26 +000066 ALOGV("Vibrator HAL service not available.");
67 gHalExists = false;
Lais Andrade921b6982020-06-04 16:17:53 +000068 return nullptr;
69 }
Lais Andradecfd81152020-07-01 09:00:26 +000070
Lais Andrade921b6982020-06-04 16:17:53 +000071 sp<V1_3::IVibrator> halV1_3 = V1_3::IVibrator::castFrom(halV1_0);
72 if (halV1_3) {
Lais Andradecfd81152020-07-01 09:00:26 +000073 ALOGV("Successfully connected to Vibrator HAL v1.3 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000074 return std::make_shared<HidlHalWrapperV1_3>(std::move(scheduler), halV1_3);
75 }
76 sp<V1_2::IVibrator> halV1_2 = V1_2::IVibrator::castFrom(halV1_0);
77 if (halV1_2) {
Lais Andradecfd81152020-07-01 09:00:26 +000078 ALOGV("Successfully connected to Vibrator HAL v1.2 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000079 return std::make_shared<HidlHalWrapperV1_2>(std::move(scheduler), halV1_2);
80 }
81 sp<V1_1::IVibrator> halV1_1 = V1_1::IVibrator::castFrom(halV1_0);
82 if (halV1_1) {
Lais Andradecfd81152020-07-01 09:00:26 +000083 ALOGV("Successfully connected to Vibrator HAL v1.1 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000084 return std::make_shared<HidlHalWrapperV1_1>(std::move(scheduler), halV1_1);
85 }
Lais Andradecfd81152020-07-01 09:00:26 +000086 ALOGV("Successfully connected to Vibrator HAL v1.0 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000087 return std::make_shared<HidlHalWrapperV1_0>(std::move(scheduler), halV1_0);
88}
89
90// -------------------------------------------------------------------------------------------------
91
92template <typename T>
93HalResult<T> HalController::processHalResult(HalResult<T> result, const char* functionName) {
94 if (result.isFailed()) {
Lais Andrade08666612020-08-07 16:16:31 +000095 ALOGE("%s failed: %s", functionName, result.errorMessage());
Lais Andrade921b6982020-06-04 16:17:53 +000096 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
Lais Andradecfd81152020-07-01 09:00:26 +000097 mConnectedHal->tryReconnect();
Lais Andrade921b6982020-06-04 16:17:53 +000098 }
99 return result;
100}
101
102template <typename T>
103HalResult<T> HalController::apply(HalController::hal_fn<T>& halFn, const char* functionName) {
Lais Andradecfd81152020-07-01 09:00:26 +0000104 std::shared_ptr<HalWrapper> hal = nullptr;
105 {
106 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
107 if (mConnectedHal == nullptr) {
108 // Init was never called, so connect to HAL for the first time during this call.
109 mConnectedHal = mHalConnector->connect(mCallbackScheduler);
110
111 if (mConnectedHal == nullptr) {
112 ALOGV("Skipped %s because Vibrator HAL is not available", functionName);
113 return HalResult<T>::unsupported();
114 }
115 }
116 hal = mConnectedHal;
Lais Andrade921b6982020-06-04 16:17:53 +0000117 }
118
Lais Andrade37d0f042020-06-18 16:39:55 +0000119 HalResult<T> ret = processHalResult(halFn(hal), functionName);
120 for (int i = 0; i < MAX_RETRIES && ret.isFailed(); i++) {
Lais Andradecfd81152020-07-01 09:00:26 +0000121 ret = processHalResult(halFn(hal), functionName);
Lais Andrade37d0f042020-06-18 16:39:55 +0000122 }
123
124 return ret;
Lais Andrade921b6982020-06-04 16:17:53 +0000125}
126
127// -------------------------------------------------------------------------------------------------
128
Lais Andradecfd81152020-07-01 09:00:26 +0000129void HalController::init() {
130 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
131 if (mConnectedHal == nullptr) {
132 mConnectedHal = mHalConnector->connect(mCallbackScheduler);
133 }
134}
135
Lais Andrade921b6982020-06-04 16:17:53 +0000136HalResult<void> HalController::ping() {
137 hal_fn<void> pingFn = [](std::shared_ptr<HalWrapper> hal) { return hal->ping(); };
138 return apply(pingFn, "ping");
139}
140
Lais Andradecfd81152020-07-01 09:00:26 +0000141void HalController::tryReconnect() {
142 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
143 if (mConnectedHal == nullptr) {
144 mConnectedHal = mHalConnector->connect(mCallbackScheduler);
145 } else {
146 mConnectedHal->tryReconnect();
147 }
148}
149
Lais Andrade921b6982020-06-04 16:17:53 +0000150HalResult<void> HalController::on(milliseconds timeout,
151 const std::function<void()>& completionCallback) {
152 hal_fn<void> onFn = [&](std::shared_ptr<HalWrapper> hal) {
153 return hal->on(timeout, completionCallback);
154 };
155 return apply(onFn, "on");
156}
157
158HalResult<void> HalController::off() {
159 hal_fn<void> offFn = [](std::shared_ptr<HalWrapper> hal) { return hal->off(); };
160 return apply(offFn, "off");
161}
162
163HalResult<void> HalController::setAmplitude(int32_t amplitude) {
164 hal_fn<void> setAmplitudeFn = [&](std::shared_ptr<HalWrapper> hal) {
165 return hal->setAmplitude(amplitude);
166 };
167 return apply(setAmplitudeFn, "setAmplitude");
168}
169
170HalResult<void> HalController::setExternalControl(bool enabled) {
171 hal_fn<void> setExternalControlFn = [&](std::shared_ptr<HalWrapper> hal) {
172 return hal->setExternalControl(enabled);
173 };
174 return apply(setExternalControlFn, "setExternalControl");
175}
176
177HalResult<void> HalController::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) {
178 hal_fn<void> alwaysOnEnableFn = [&](std::shared_ptr<HalWrapper> hal) {
179 return hal->alwaysOnEnable(id, effect, strength);
180 };
181 return apply(alwaysOnEnableFn, "alwaysOnEnable");
182}
183
184HalResult<void> HalController::alwaysOnDisable(int32_t id) {
185 hal_fn<void> alwaysOnDisableFn = [&](std::shared_ptr<HalWrapper> hal) {
186 return hal->alwaysOnDisable(id);
187 };
188 return apply(alwaysOnDisableFn, "alwaysOnDisable");
189}
190
191HalResult<Capabilities> HalController::getCapabilities() {
192 hal_fn<Capabilities> getCapabilitiesFn = [](std::shared_ptr<HalWrapper> hal) {
193 return hal->getCapabilities();
194 };
195 return apply(getCapabilitiesFn, "getCapabilities");
196}
197
198HalResult<std::vector<Effect>> HalController::getSupportedEffects() {
199 hal_fn<std::vector<Effect>> getSupportedEffectsFn = [](std::shared_ptr<HalWrapper> hal) {
200 return hal->getSupportedEffects();
201 };
202 return apply(getSupportedEffectsFn, "getSupportedEffects");
203}
204
Lais Andrade07f9c0e2020-08-11 16:22:12 +0000205HalResult<std::vector<CompositePrimitive>> HalController::getSupportedPrimitives() {
206 hal_fn<std::vector<CompositePrimitive>> getSupportedPrimitivesFn =
207 [](std::shared_ptr<HalWrapper> hal) { return hal->getSupportedPrimitives(); };
208 return apply(getSupportedPrimitivesFn, "getSupportedPrimitives");
209}
210
Lais Andrade921b6982020-06-04 16:17:53 +0000211HalResult<milliseconds> HalController::performEffect(
212 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
213 hal_fn<milliseconds> performEffectFn = [&](std::shared_ptr<HalWrapper> hal) {
214 return hal->performEffect(effect, strength, completionCallback);
215 };
216 return apply(performEffectFn, "performEffect");
217}
218
219HalResult<void> HalController::performComposedEffect(
220 const std::vector<CompositeEffect>& primitiveEffects,
221 const std::function<void()>& completionCallback) {
222 hal_fn<void> performComposedEffectFn = [&](std::shared_ptr<HalWrapper> hal) {
223 return hal->performComposedEffect(primitiveEffects, completionCallback);
224 };
225 return apply(performComposedEffectFn, "performComposedEffect");
226}
227
228}; // namespace vibrator
229
230}; // namespace android