blob: 283a5f030181110a9d84c30616940cad74f1cb43 [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
Lais Andrade818a2252024-06-24 14:48:14 +010019#include <aidl/android/hardware/vibrator/IVibrator.h>
20#include <android/binder_manager.h>
Lais Andrade921b6982020-06-04 16:17:53 +000021#include <android/hardware/vibrator/1.3/IVibrator.h>
Lais Andrade921b6982020-06-04 16:17:53 +000022#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
Lais Andrade818a2252024-06-24 14:48:14 +010030using aidl::android::hardware::vibrator::CompositeEffect;
31using aidl::android::hardware::vibrator::CompositePrimitive;
32using aidl::android::hardware::vibrator::Effect;
33using aidl::android::hardware::vibrator::EffectStrength;
Lais Andrade921b6982020-06-04 16:17:53 +000034
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;
Lais Andrade818a2252024-06-24 14:48:14 +010041namespace Aidl = aidl::android::hardware::vibrator;
Lais Andrade921b6982020-06-04 16:17:53 +000042
43namespace android {
44
45namespace vibrator {
46
47// -------------------------------------------------------------------------------------------------
48
Lais Andradef20b1442020-11-19 15:14:10 +000049std::shared_ptr<HalWrapper> connectHal(std::shared_ptr<CallbackScheduler> scheduler) {
Lais Andradecfd81152020-07-01 09:00:26 +000050 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 Andrade818a2252024-06-24 14:48:14 +010056 auto serviceName = std::string(Aidl::IVibrator::descriptor) + "/default";
57 if (AServiceManager_isDeclared(serviceName.c_str())) {
58 std::shared_ptr<Aidl::IVibrator> hal = Aidl::IVibrator::fromBinder(
59 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
60 if (hal) {
61 ALOGV("Successfully connected to Vibrator HAL AIDL service.");
62 return std::make_shared<AidlHalWrapper>(std::move(scheduler), std::move(hal));
63 }
Lais Andrade921b6982020-06-04 16:17:53 +000064 }
Lais Andradecfd81152020-07-01 09:00:26 +000065
66 sp<V1_0::IVibrator> halV1_0 = V1_0::IVibrator::getService();
Lais Andrade921b6982020-06-04 16:17:53 +000067 if (halV1_0 == nullptr) {
Lais Andradecfd81152020-07-01 09:00:26 +000068 ALOGV("Vibrator HAL service not available.");
69 gHalExists = false;
Lais Andrade921b6982020-06-04 16:17:53 +000070 return nullptr;
71 }
Lais Andradecfd81152020-07-01 09:00:26 +000072
Lais Andrade921b6982020-06-04 16:17:53 +000073 sp<V1_3::IVibrator> halV1_3 = V1_3::IVibrator::castFrom(halV1_0);
74 if (halV1_3) {
Lais Andradecfd81152020-07-01 09:00:26 +000075 ALOGV("Successfully connected to Vibrator HAL v1.3 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000076 return std::make_shared<HidlHalWrapperV1_3>(std::move(scheduler), halV1_3);
77 }
78 sp<V1_2::IVibrator> halV1_2 = V1_2::IVibrator::castFrom(halV1_0);
79 if (halV1_2) {
Lais Andradecfd81152020-07-01 09:00:26 +000080 ALOGV("Successfully connected to Vibrator HAL v1.2 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000081 return std::make_shared<HidlHalWrapperV1_2>(std::move(scheduler), halV1_2);
82 }
83 sp<V1_1::IVibrator> halV1_1 = V1_1::IVibrator::castFrom(halV1_0);
84 if (halV1_1) {
Lais Andradecfd81152020-07-01 09:00:26 +000085 ALOGV("Successfully connected to Vibrator HAL v1.1 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000086 return std::make_shared<HidlHalWrapperV1_1>(std::move(scheduler), halV1_1);
87 }
Lais Andradecfd81152020-07-01 09:00:26 +000088 ALOGV("Successfully connected to Vibrator HAL v1.0 service.");
Lais Andrade921b6982020-06-04 16:17:53 +000089 return std::make_shared<HidlHalWrapperV1_0>(std::move(scheduler), halV1_0);
90}
91
92// -------------------------------------------------------------------------------------------------
93
Lais Andrade24b5b8f2020-08-27 15:55:54 +000094bool HalController::init() {
Lais Andradecfd81152020-07-01 09:00:26 +000095 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
96 if (mConnectedHal == nullptr) {
Lais Andradef20b1442020-11-19 15:14:10 +000097 mConnectedHal = mConnector(mCallbackScheduler);
Lais Andradecfd81152020-07-01 09:00:26 +000098 }
Lais Andrade24b5b8f2020-08-27 15:55:54 +000099 return mConnectedHal != nullptr;
Lais Andradecfd81152020-07-01 09:00:26 +0000100}
101
Lais Andradecfd81152020-07-01 09:00:26 +0000102void HalController::tryReconnect() {
103 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
104 if (mConnectedHal == nullptr) {
Lais Andradef20b1442020-11-19 15:14:10 +0000105 mConnectedHal = mConnector(mCallbackScheduler);
Lais Andradecfd81152020-07-01 09:00:26 +0000106 } else {
107 mConnectedHal->tryReconnect();
108 }
109}
110
Lais Andrade921b6982020-06-04 16:17:53 +0000111}; // namespace vibrator
112
113}; // namespace android