blob: c1795f5c329f47ced53a87fb711b7db4db0743fd [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 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 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 +000090bool HalController::init() {
Lais Andradecfd81152020-07-01 09:00:26 +000091 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
92 if (mConnectedHal == nullptr) {
Lais Andradef20b1442020-11-19 15:14:10 +000093 mConnectedHal = mConnector(mCallbackScheduler);
Lais Andradecfd81152020-07-01 09:00:26 +000094 }
Lais Andrade24b5b8f2020-08-27 15:55:54 +000095 return mConnectedHal != nullptr;
Lais Andradecfd81152020-07-01 09:00:26 +000096}
97
Lais Andradecfd81152020-07-01 09:00:26 +000098void HalController::tryReconnect() {
99 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
100 if (mConnectedHal == nullptr) {
Lais Andradef20b1442020-11-19 15:14:10 +0000101 mConnectedHal = mConnector(mCallbackScheduler);
Lais Andradecfd81152020-07-01 09:00:26 +0000102 } else {
103 mConnectedHal->tryReconnect();
104 }
105}
106
Lais Andrade921b6982020-06-04 16:17:53 +0000107}; // namespace vibrator
108
109}; // namespace android