blob: ba35d15bf202c8704ecc77a61d3d0f6261447088 [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
Lais Andrade818a2252024-06-24 14:48:14 +010023namespace Aidl = aidl::android::hardware::vibrator;
Lais Andradef20b1442020-11-19 15:14:10 +000024
25namespace android {
26
27namespace vibrator {
28
Lais Andrade468f8062020-12-03 13:49:17 +000029std::shared_ptr<ManagerHalWrapper> connectManagerHal(std::shared_ptr<CallbackScheduler> scheduler) {
Lais Andradef20b1442020-11-19 15:14:10 +000030 static bool gHalExists = true;
31 if (gHalExists) {
Lais Andrade818a2252024-06-24 14:48:14 +010032 auto serviceName = std::string(Aidl::IVibratorManager::descriptor) + "/default";
33 if (AServiceManager_isDeclared(serviceName.c_str())) {
34 std::shared_ptr<Aidl::IVibratorManager> hal = Aidl::IVibratorManager::fromBinder(
35 ndk::SpAIBinder(AServiceManager_checkService(serviceName.c_str())));
36 if (hal) {
37 ALOGV("Successfully connected to VibratorManager HAL AIDL service.");
38 return std::make_shared<AidlManagerHalWrapper>(std::move(scheduler),
39 std::move(hal));
40 }
Lais Andradef20b1442020-11-19 15:14:10 +000041 }
42 }
43
44 gHalExists = false;
45 return std::make_shared<LegacyManagerHalWrapper>();
46}
47
48static constexpr int MAX_RETRIES = 1;
49
50template <typename T>
51HalResult<T> ManagerHalController::processHalResult(HalResult<T> result, const char* functionName) {
52 if (result.isFailed()) {
Lais Andrade4928bfd2021-08-25 18:21:12 +010053 ALOGE("VibratorManager HAL %s failed: %s", functionName, result.errorMessage());
Lais Andradef20b1442020-11-19 15:14:10 +000054 }
55 return result;
56}
57
58template <typename T>
59HalResult<T> ManagerHalController::apply(ManagerHalController::hal_fn<T>& halFn,
60 const char* functionName) {
61 std::shared_ptr<ManagerHalWrapper> hal = nullptr;
62 {
63 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
64 if (mConnectedHal == nullptr) {
65 // Init was never called, so connect to HAL for the first time during this call.
66 mConnectedHal = mConnector(mCallbackScheduler);
67
68 if (mConnectedHal == nullptr) {
69 ALOGV("Skipped %s because VibratorManager HAL is not available", functionName);
70 return HalResult<T>::unsupported();
71 }
72 }
73 hal = mConnectedHal;
74 }
75
Lais Andrade641248e2024-02-16 17:49:36 +000076 HalResult<T> result = processHalResult(halFn(hal), functionName);
77 for (int i = 0; i < MAX_RETRIES && result.shouldRetry(); i++) {
78 {
79 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
80 mConnectedHal->tryReconnect();
81 }
82 result = processHalResult(halFn(hal), functionName);
Lais Andradef20b1442020-11-19 15:14:10 +000083 }
84
Lais Andrade641248e2024-02-16 17:49:36 +000085 return result;
Lais Andradef20b1442020-11-19 15:14:10 +000086}
87
88// -------------------------------------------------------------------------------------------------
89
Lais Andrade468f8062020-12-03 13:49:17 +000090void ManagerHalController::init() {
Lais Andradef20b1442020-11-19 15:14:10 +000091 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
92 if (mConnectedHal == nullptr) {
93 mConnectedHal = mConnector(mCallbackScheduler);
94 }
Lais Andradef20b1442020-11-19 15:14:10 +000095}
96
97HalResult<void> ManagerHalController::ping() {
98 hal_fn<void> pingFn = [](std::shared_ptr<ManagerHalWrapper> hal) { return hal->ping(); };
99 return apply(pingFn, "ping");
100}
101
102void ManagerHalController::tryReconnect() {
103 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
104 if (mConnectedHal == nullptr) {
105 mConnectedHal = mConnector(mCallbackScheduler);
106 } else {
107 mConnectedHal->tryReconnect();
108 }
109}
110
111HalResult<ManagerCapabilities> ManagerHalController::getCapabilities() {
112 hal_fn<ManagerCapabilities> getCapabilitiesFn = [](std::shared_ptr<ManagerHalWrapper> hal) {
113 return hal->getCapabilities();
114 };
115 return apply(getCapabilitiesFn, "getCapabilities");
116}
117
118HalResult<std::vector<int32_t>> ManagerHalController::getVibratorIds() {
119 hal_fn<std::vector<int32_t>> getVibratorIdsFn = [](std::shared_ptr<ManagerHalWrapper> hal) {
120 return hal->getVibratorIds();
121 };
122 return apply(getVibratorIdsFn, "getVibratorIds");
123}
124
125HalResult<std::shared_ptr<HalController>> ManagerHalController::getVibrator(int32_t id) {
126 hal_fn<std::shared_ptr<HalController>> getVibratorFn =
127 [&](std::shared_ptr<ManagerHalWrapper> hal) { return hal->getVibrator(id); };
128 return apply(getVibratorFn, "getVibrator");
129}
130
131HalResult<void> ManagerHalController::prepareSynced(const std::vector<int32_t>& ids) {
132 hal_fn<void> prepareSyncedFn = [&](std::shared_ptr<ManagerHalWrapper> hal) {
133 return hal->prepareSynced(ids);
134 };
135 return apply(prepareSyncedFn, "prepareSynced");
136}
137
138HalResult<void> ManagerHalController::triggerSynced(
139 const std::function<void()>& completionCallback) {
140 hal_fn<void> triggerSyncedFn = [&](std::shared_ptr<ManagerHalWrapper> hal) {
141 return hal->triggerSynced(completionCallback);
142 };
143 return apply(triggerSyncedFn, "triggerSynced");
144}
145
146HalResult<void> ManagerHalController::cancelSynced() {
147 hal_fn<void> cancelSyncedFn = [](std::shared_ptr<ManagerHalWrapper> hal) {
148 return hal->cancelSynced();
149 };
150 return apply(cancelSyncedFn, "cancelSynced");
151}
152
153}; // namespace vibrator
154
155}; // namespace android