blob: e8606caffd7fae7cefee6c8a26222b5c6c70941e [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 Andradecfd81152020-07-01 09:00:26 +000049std::shared_ptr<HalWrapper> HalConnector::connect(std::shared_ptr<CallbackScheduler> scheduler) {
50 static bool gHalExists = true;
51 if (!gHalExists) {
52 // We already tried to connect to all of the vibrator HAL versions and none was available.
Lais Andrade921b6982020-06-04 16:17:53 +000053 return nullptr;
54 }
Lais Andrade921b6982020-06-04 16:17:53 +000055
Lais Andradecfd81152020-07-01 09:00:26 +000056 sp<Aidl::IVibrator> aidlHal = waitForVintfService<Aidl::IVibrator>();
Lais Andrade921b6982020-06-04 16:17:53 +000057 if (aidlHal) {
Lais Andradecfd81152020-07-01 09:00:26 +000058 ALOGV("Successfully connected to Vibrator HAL AIDL service.");
Lais Andrade921b6982020-06-04 16:17:53 +000059 return std::make_shared<AidlHalWrapper>(std::move(scheduler), aidlHal);
60 }
Lais Andradecfd81152020-07-01 09:00:26 +000061
62 sp<V1_0::IVibrator> halV1_0 = V1_0::IVibrator::getService();
Lais Andrade921b6982020-06-04 16:17:53 +000063 if (halV1_0 == nullptr) {
Lais Andradecfd81152020-07-01 09:00:26 +000064 ALOGV("Vibrator HAL service not available.");
65 gHalExists = false;
Lais Andrade921b6982020-06-04 16:17:53 +000066 return nullptr;
67 }
Lais Andradecfd81152020-07-01 09:00:26 +000068
Lais Andrade921b6982020-06-04 16:17:53 +000069 sp<V1_3::IVibrator> halV1_3 = V1_3::IVibrator::castFrom(halV1_0);
70 if (halV1_3) {
Lais Andradecfd81152020-07-01 09:00:26 +000071 ALOGV("Successfully connected to Vibrator HAL v1.3 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000072 return std::make_shared<HidlHalWrapperV1_3>(std::move(scheduler), halV1_3);
73 }
74 sp<V1_2::IVibrator> halV1_2 = V1_2::IVibrator::castFrom(halV1_0);
75 if (halV1_2) {
Lais Andradecfd81152020-07-01 09:00:26 +000076 ALOGV("Successfully connected to Vibrator HAL v1.2 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000077 return std::make_shared<HidlHalWrapperV1_2>(std::move(scheduler), halV1_2);
78 }
79 sp<V1_1::IVibrator> halV1_1 = V1_1::IVibrator::castFrom(halV1_0);
80 if (halV1_1) {
Lais Andradecfd81152020-07-01 09:00:26 +000081 ALOGV("Successfully connected to Vibrator HAL v1.1 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000082 return std::make_shared<HidlHalWrapperV1_1>(std::move(scheduler), halV1_1);
83 }
Lais Andradecfd81152020-07-01 09:00:26 +000084 ALOGV("Successfully connected to Vibrator HAL v1.0 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000085 return std::make_shared<HidlHalWrapperV1_0>(std::move(scheduler), halV1_0);
86}
87
88// -------------------------------------------------------------------------------------------------
89
Lais Andrade24b5b8f2020-08-27 15:55:54 +000090static constexpr int MAX_RETRIES = 1;
91
Lais Andrade921b6982020-06-04 16:17:53 +000092template <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 Andrade24b5b8f2020-08-27 15:55:54 +0000129bool HalController::init() {
Lais Andradecfd81152020-07-01 09:00:26 +0000130 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
131 if (mConnectedHal == nullptr) {
132 mConnectedHal = mHalConnector->connect(mCallbackScheduler);
133 }
Lais Andrade24b5b8f2020-08-27 15:55:54 +0000134 return mConnectedHal != nullptr;
Lais Andradecfd81152020-07-01 09:00:26 +0000135}
136
Lais Andrade921b6982020-06-04 16:17:53 +0000137HalResult<void> HalController::ping() {
138 hal_fn<void> pingFn = [](std::shared_ptr<HalWrapper> hal) { return hal->ping(); };
139 return apply(pingFn, "ping");
140}
141
Lais Andradecfd81152020-07-01 09:00:26 +0000142void HalController::tryReconnect() {
143 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
144 if (mConnectedHal == nullptr) {
145 mConnectedHal = mHalConnector->connect(mCallbackScheduler);
146 } else {
147 mConnectedHal->tryReconnect();
148 }
149}
150
Lais Andrade921b6982020-06-04 16:17:53 +0000151HalResult<void> HalController::on(milliseconds timeout,
152 const std::function<void()>& completionCallback) {
153 hal_fn<void> onFn = [&](std::shared_ptr<HalWrapper> hal) {
154 return hal->on(timeout, completionCallback);
155 };
156 return apply(onFn, "on");
157}
158
159HalResult<void> HalController::off() {
160 hal_fn<void> offFn = [](std::shared_ptr<HalWrapper> hal) { return hal->off(); };
161 return apply(offFn, "off");
162}
163
164HalResult<void> HalController::setAmplitude(int32_t amplitude) {
165 hal_fn<void> setAmplitudeFn = [&](std::shared_ptr<HalWrapper> hal) {
166 return hal->setAmplitude(amplitude);
167 };
168 return apply(setAmplitudeFn, "setAmplitude");
169}
170
171HalResult<void> HalController::setExternalControl(bool enabled) {
172 hal_fn<void> setExternalControlFn = [&](std::shared_ptr<HalWrapper> hal) {
173 return hal->setExternalControl(enabled);
174 };
175 return apply(setExternalControlFn, "setExternalControl");
176}
177
178HalResult<void> HalController::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) {
179 hal_fn<void> alwaysOnEnableFn = [&](std::shared_ptr<HalWrapper> hal) {
180 return hal->alwaysOnEnable(id, effect, strength);
181 };
182 return apply(alwaysOnEnableFn, "alwaysOnEnable");
183}
184
185HalResult<void> HalController::alwaysOnDisable(int32_t id) {
186 hal_fn<void> alwaysOnDisableFn = [&](std::shared_ptr<HalWrapper> hal) {
187 return hal->alwaysOnDisable(id);
188 };
189 return apply(alwaysOnDisableFn, "alwaysOnDisable");
190}
191
192HalResult<Capabilities> HalController::getCapabilities() {
193 hal_fn<Capabilities> getCapabilitiesFn = [](std::shared_ptr<HalWrapper> hal) {
194 return hal->getCapabilities();
195 };
196 return apply(getCapabilitiesFn, "getCapabilities");
197}
198
199HalResult<std::vector<Effect>> HalController::getSupportedEffects() {
200 hal_fn<std::vector<Effect>> getSupportedEffectsFn = [](std::shared_ptr<HalWrapper> hal) {
201 return hal->getSupportedEffects();
202 };
203 return apply(getSupportedEffectsFn, "getSupportedEffects");
204}
205
Lais Andrade07f9c0e2020-08-11 16:22:12 +0000206HalResult<std::vector<CompositePrimitive>> HalController::getSupportedPrimitives() {
207 hal_fn<std::vector<CompositePrimitive>> getSupportedPrimitivesFn =
208 [](std::shared_ptr<HalWrapper> hal) { return hal->getSupportedPrimitives(); };
209 return apply(getSupportedPrimitivesFn, "getSupportedPrimitives");
210}
211
Lais Andrade921b6982020-06-04 16:17:53 +0000212HalResult<milliseconds> HalController::performEffect(
213 Effect effect, EffectStrength strength, const std::function<void()>& completionCallback) {
214 hal_fn<milliseconds> performEffectFn = [&](std::shared_ptr<HalWrapper> hal) {
215 return hal->performEffect(effect, strength, completionCallback);
216 };
217 return apply(performEffectFn, "performEffect");
218}
219
220HalResult<void> HalController::performComposedEffect(
221 const std::vector<CompositeEffect>& primitiveEffects,
222 const std::function<void()>& completionCallback) {
223 hal_fn<void> performComposedEffectFn = [&](std::shared_ptr<HalWrapper> hal) {
224 return hal->performComposedEffect(primitiveEffects, completionCallback);
225 };
226 return apply(performComposedEffectFn, "performComposedEffect");
227}
228
229}; // namespace vibrator
230
231}; // namespace android