blob: b24e5c49b976d122b6eacc9cb2c68b33422fb9d1 [file] [log] [blame]
Lais Andradef20b1442020-11-19 15:14:10 +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 "VibratorManagerHalController"
18
19#include <utils/Log.h>
20
21#include <vibratorservice/VibratorManagerHalController.h>
22
23namespace Aidl = android::hardware::vibrator;
24
25namespace android {
26
27namespace vibrator {
28
29std::shared_ptr<ManagerHalWrapper> connectHal(std::shared_ptr<CallbackScheduler> scheduler) {
30 static bool gHalExists = true;
31 if (gHalExists) {
32 sp<Aidl::IVibratorManager> hal = waitForVintfService<Aidl::IVibratorManager>();
33 if (hal) {
34 ALOGV("Successfully connected to VibratorManager HAL AIDL service.");
35 return std::make_shared<AidlManagerHalWrapper>(std::move(scheduler), aidlHal);
36 }
37 }
38
39 gHalExists = false;
40 return std::make_shared<LegacyManagerHalWrapper>();
41}
42
43static constexpr int MAX_RETRIES = 1;
44
45template <typename T>
46HalResult<T> ManagerHalController::processHalResult(HalResult<T> result, const char* functionName) {
47 if (result.isFailed()) {
48 ALOGE("%s failed: %s", functionName, result.errorMessage());
49 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
50 mConnectedHal->tryReconnect();
51 }
52 return result;
53}
54
55template <typename T>
56HalResult<T> ManagerHalController::apply(ManagerHalController::hal_fn<T>& halFn,
57 const char* functionName) {
58 std::shared_ptr<ManagerHalWrapper> hal = nullptr;
59 {
60 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
61 if (mConnectedHal == nullptr) {
62 // Init was never called, so connect to HAL for the first time during this call.
63 mConnectedHal = mConnector(mCallbackScheduler);
64
65 if (mConnectedHal == nullptr) {
66 ALOGV("Skipped %s because VibratorManager HAL is not available", functionName);
67 return HalResult<T>::unsupported();
68 }
69 }
70 hal = mConnectedHal;
71 }
72
73 HalResult<T> ret = processHalResult(halFn(hal), functionName);
74 for (int i = 0; i < MAX_RETRIES && ret.isFailed(); i++) {
75 ret = processHalResult(halFn(hal), functionName);
76 }
77
78 return ret;
79}
80
81// -------------------------------------------------------------------------------------------------
82
83bool ManagerHalController::init() {
84 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
85 if (mConnectedHal == nullptr) {
86 mConnectedHal = mConnector(mCallbackScheduler);
87 }
88 return mConnectedHal != nullptr;
89}
90
91HalResult<void> ManagerHalController::ping() {
92 hal_fn<void> pingFn = [](std::shared_ptr<ManagerHalWrapper> hal) { return hal->ping(); };
93 return apply(pingFn, "ping");
94}
95
96void ManagerHalController::tryReconnect() {
97 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
98 if (mConnectedHal == nullptr) {
99 mConnectedHal = mConnector(mCallbackScheduler);
100 } else {
101 mConnectedHal->tryReconnect();
102 }
103}
104
105HalResult<ManagerCapabilities> ManagerHalController::getCapabilities() {
106 hal_fn<ManagerCapabilities> getCapabilitiesFn = [](std::shared_ptr<ManagerHalWrapper> hal) {
107 return hal->getCapabilities();
108 };
109 return apply(getCapabilitiesFn, "getCapabilities");
110}
111
112HalResult<std::vector<int32_t>> ManagerHalController::getVibratorIds() {
113 hal_fn<std::vector<int32_t>> getVibratorIdsFn = [](std::shared_ptr<ManagerHalWrapper> hal) {
114 return hal->getVibratorIds();
115 };
116 return apply(getVibratorIdsFn, "getVibratorIds");
117}
118
119HalResult<std::shared_ptr<HalController>> ManagerHalController::getVibrator(int32_t id) {
120 hal_fn<std::shared_ptr<HalController>> getVibratorFn =
121 [&](std::shared_ptr<ManagerHalWrapper> hal) { return hal->getVibrator(id); };
122 return apply(getVibratorFn, "getVibrator");
123}
124
125HalResult<void> ManagerHalController::prepareSynced(const std::vector<int32_t>& ids) {
126 hal_fn<void> prepareSyncedFn = [&](std::shared_ptr<ManagerHalWrapper> hal) {
127 return hal->prepareSynced(ids);
128 };
129 return apply(prepareSyncedFn, "prepareSynced");
130}
131
132HalResult<void> ManagerHalController::triggerSynced(
133 const std::function<void()>& completionCallback) {
134 hal_fn<void> triggerSyncedFn = [&](std::shared_ptr<ManagerHalWrapper> hal) {
135 return hal->triggerSynced(completionCallback);
136 };
137 return apply(triggerSyncedFn, "triggerSynced");
138}
139
140HalResult<void> ManagerHalController::cancelSynced() {
141 hal_fn<void> cancelSyncedFn = [](std::shared_ptr<ManagerHalWrapper> hal) {
142 return hal->cancelSynced();
143 };
144 return apply(cancelSyncedFn, "cancelSynced");
145}
146
147}; // namespace vibrator
148
149}; // namespace android