blob: 03e51aea7c0c55031184c7680b69d38a94e9c3cb [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;
31using android::hardware::vibrator::Effect;
32using android::hardware::vibrator::EffectStrength;
33
34using std::chrono::milliseconds;
35
36namespace V1_0 = android::hardware::vibrator::V1_0;
37namespace V1_1 = android::hardware::vibrator::V1_1;
38namespace V1_2 = android::hardware::vibrator::V1_2;
39namespace V1_3 = android::hardware::vibrator::V1_3;
40namespace Aidl = android::hardware::vibrator;
41
42namespace android {
43
44namespace vibrator {
45
46// -------------------------------------------------------------------------------------------------
47
Lais Andrade37d0f042020-06-18 16:39:55 +000048static constexpr int MAX_RETRIES = 1;
49
Lais Andradecfd81152020-07-01 09:00:26 +000050std::shared_ptr<HalWrapper> HalConnector::connect(std::shared_ptr<CallbackScheduler> scheduler) {
51 static bool gHalExists = true;
52 if (!gHalExists) {
53 // We already tried to connect to all of the vibrator HAL versions and none was available.
Lais Andrade921b6982020-06-04 16:17:53 +000054 return nullptr;
55 }
Lais Andrade921b6982020-06-04 16:17:53 +000056
Lais Andradecfd81152020-07-01 09:00:26 +000057 sp<Aidl::IVibrator> aidlHal = waitForVintfService<Aidl::IVibrator>();
Lais Andrade921b6982020-06-04 16:17:53 +000058 if (aidlHal) {
Lais Andradecfd81152020-07-01 09:00:26 +000059 ALOGV("Successfully connected to Vibrator HAL AIDL service.");
Lais Andrade921b6982020-06-04 16:17:53 +000060 return std::make_shared<AidlHalWrapper>(std::move(scheduler), aidlHal);
61 }
Lais Andradecfd81152020-07-01 09:00:26 +000062
63 sp<V1_0::IVibrator> halV1_0 = V1_0::IVibrator::getService();
Lais Andrade921b6982020-06-04 16:17:53 +000064 if (halV1_0 == nullptr) {
Lais Andradecfd81152020-07-01 09:00:26 +000065 ALOGV("Vibrator HAL service not available.");
66 gHalExists = false;
Lais Andrade921b6982020-06-04 16:17:53 +000067 return nullptr;
68 }
Lais Andradecfd81152020-07-01 09:00:26 +000069
Lais Andrade921b6982020-06-04 16:17:53 +000070 sp<V1_3::IVibrator> halV1_3 = V1_3::IVibrator::castFrom(halV1_0);
71 if (halV1_3) {
Lais Andradecfd81152020-07-01 09:00:26 +000072 ALOGV("Successfully connected to Vibrator HAL v1.3 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000073 return std::make_shared<HidlHalWrapperV1_3>(std::move(scheduler), halV1_3);
74 }
75 sp<V1_2::IVibrator> halV1_2 = V1_2::IVibrator::castFrom(halV1_0);
76 if (halV1_2) {
Lais Andradecfd81152020-07-01 09:00:26 +000077 ALOGV("Successfully connected to Vibrator HAL v1.2 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000078 return std::make_shared<HidlHalWrapperV1_2>(std::move(scheduler), halV1_2);
79 }
80 sp<V1_1::IVibrator> halV1_1 = V1_1::IVibrator::castFrom(halV1_0);
81 if (halV1_1) {
Lais Andradecfd81152020-07-01 09:00:26 +000082 ALOGV("Successfully connected to Vibrator HAL v1.1 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000083 return std::make_shared<HidlHalWrapperV1_1>(std::move(scheduler), halV1_1);
84 }
Lais Andradecfd81152020-07-01 09:00:26 +000085 ALOGV("Successfully connected to Vibrator HAL v1.0 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000086 return std::make_shared<HidlHalWrapperV1_0>(std::move(scheduler), halV1_0);
87}
88
89// -------------------------------------------------------------------------------------------------
90
91template <typename T>
92HalResult<T> HalController::processHalResult(HalResult<T> result, const char* functionName) {
93 if (result.isFailed()) {
94 ALOGE("%s failed: Vibrator HAL not available", functionName);
95 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
Lais Andradecfd81152020-07-01 09:00:26 +000096 mConnectedHal->tryReconnect();
Lais Andrade921b6982020-06-04 16:17:53 +000097 }
98 return result;
99}
100
101template <typename T>
102HalResult<T> HalController::apply(HalController::hal_fn<T>& halFn, const char* functionName) {
Lais Andradecfd81152020-07-01 09:00:26 +0000103 std::shared_ptr<HalWrapper> hal = nullptr;
104 {
105 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
106 if (mConnectedHal == nullptr) {
107 // Init was never called, so connect to HAL for the first time during this call.
108 mConnectedHal = mHalConnector->connect(mCallbackScheduler);
109
110 if (mConnectedHal == nullptr) {
111 ALOGV("Skipped %s because Vibrator HAL is not available", functionName);
112 return HalResult<T>::unsupported();
113 }
114 }
115 hal = mConnectedHal;
Lais Andrade921b6982020-06-04 16:17:53 +0000116 }
117
Lais Andrade37d0f042020-06-18 16:39:55 +0000118 HalResult<T> ret = processHalResult(halFn(hal), functionName);
119 for (int i = 0; i < MAX_RETRIES && ret.isFailed(); i++) {
Lais Andradecfd81152020-07-01 09:00:26 +0000120 ret = processHalResult(halFn(hal), functionName);
Lais Andrade37d0f042020-06-18 16:39:55 +0000121 }
122
123 return ret;
Lais Andrade921b6982020-06-04 16:17:53 +0000124}
125
126// -------------------------------------------------------------------------------------------------
127
Lais Andradecfd81152020-07-01 09:00:26 +0000128void HalController::init() {
129 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
130 if (mConnectedHal == nullptr) {
131 mConnectedHal = mHalConnector->connect(mCallbackScheduler);
132 }
133}
134
Lais Andrade921b6982020-06-04 16:17:53 +0000135HalResult<void> HalController::ping() {
136 hal_fn<void> pingFn = [](std::shared_ptr<HalWrapper> hal) { return hal->ping(); };
137 return apply(pingFn, "ping");
138}
139
Lais Andradecfd81152020-07-01 09:00:26 +0000140void HalController::tryReconnect() {
141 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
142 if (mConnectedHal == nullptr) {
143 mConnectedHal = mHalConnector->connect(mCallbackScheduler);
144 } else {
145 mConnectedHal->tryReconnect();
146 }
147}
148
Lais Andrade921b6982020-06-04 16:17:53 +0000149HalResult<void> HalController::on(milliseconds timeout,
150 const std::function<void()>& completionCallback) {
151 hal_fn<void> onFn = [&](std::shared_ptr<HalWrapper> hal) {
152 return hal->on(timeout, completionCallback);
153 };
154 return apply(onFn, "on");
155}
156
157HalResult<void> HalController::off() {
158 hal_fn<void> offFn = [](std::shared_ptr<HalWrapper> hal) { return hal->off(); };
159 return apply(offFn, "off");
160}
161
162HalResult<void> HalController::setAmplitude(int32_t amplitude) {
163 hal_fn<void> setAmplitudeFn = [&](std::shared_ptr<HalWrapper> hal) {
164 return hal->setAmplitude(amplitude);
165 };
166 return apply(setAmplitudeFn, "setAmplitude");
167}
168
169HalResult<void> HalController::setExternalControl(bool enabled) {
170 hal_fn<void> setExternalControlFn = [&](std::shared_ptr<HalWrapper> hal) {
171 return hal->setExternalControl(enabled);
172 };
173 return apply(setExternalControlFn, "setExternalControl");
174}
175
176HalResult<void> HalController::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) {
177 hal_fn<void> alwaysOnEnableFn = [&](std::shared_ptr<HalWrapper> hal) {
178 return hal->alwaysOnEnable(id, effect, strength);
179 };
180 return apply(alwaysOnEnableFn, "alwaysOnEnable");
181}
182
183HalResult<void> HalController::alwaysOnDisable(int32_t id) {
184 hal_fn<void> alwaysOnDisableFn = [&](std::shared_ptr<HalWrapper> hal) {
185 return hal->alwaysOnDisable(id);
186 };
187 return apply(alwaysOnDisableFn, "alwaysOnDisable");
188}
189
190HalResult<Capabilities> HalController::getCapabilities() {
191 hal_fn<Capabilities> getCapabilitiesFn = [](std::shared_ptr<HalWrapper> hal) {
192 return hal->getCapabilities();
193 };
194 return apply(getCapabilitiesFn, "getCapabilities");
195}
196
197HalResult<std::vector<Effect>> HalController::getSupportedEffects() {
198 hal_fn<std::vector<Effect>> getSupportedEffectsFn = [](std::shared_ptr<HalWrapper> hal) {
199 return hal->getSupportedEffects();
200 };
201 return apply(getSupportedEffectsFn, "getSupportedEffects");
202}
203
204HalResult<milliseconds> HalController::performEffect(
205 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
206 hal_fn<milliseconds> performEffectFn = [&](std::shared_ptr<HalWrapper> hal) {
207 return hal->performEffect(effect, strength, completionCallback);
208 };
209 return apply(performEffectFn, "performEffect");
210}
211
212HalResult<void> HalController::performComposedEffect(
213 const std::vector<CompositeEffect>& primitiveEffects,
214 const std::function<void()>& completionCallback) {
215 hal_fn<void> performComposedEffectFn = [&](std::shared_ptr<HalWrapper> hal) {
216 return hal->performComposedEffect(primitiveEffects, completionCallback);
217 };
218 return apply(performComposedEffectFn, "performComposedEffect");
219}
220
221}; // namespace vibrator
222
223}; // namespace android