1 /* 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 30 using android::hardware::vibrator::CompositeEffect; 31 using android::hardware::vibrator::CompositePrimitive; 32 using android::hardware::vibrator::Effect; 33 using android::hardware::vibrator::EffectStrength; 34 35 using std::chrono::milliseconds; 36 37 namespace V1_0 = android::hardware::vibrator::V1_0; 38 namespace V1_1 = android::hardware::vibrator::V1_1; 39 namespace V1_2 = android::hardware::vibrator::V1_2; 40 namespace V1_3 = android::hardware::vibrator::V1_3; 41 namespace Aidl = android::hardware::vibrator; 42 43 namespace android { 44 45 namespace vibrator { 46 47 // ------------------------------------------------------------------------------------------------- 48 connectHal(std::shared_ptr<CallbackScheduler> scheduler)49std::shared_ptr<HalWrapper> connectHal(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. 53 return nullptr; 54 } 55 56 sp<Aidl::IVibrator> aidlHal = waitForVintfService<Aidl::IVibrator>(); 57 if (aidlHal) { 58 ALOGV("Successfully connected to Vibrator HAL AIDL service."); 59 return std::make_shared<AidlHalWrapper>(std::move(scheduler), aidlHal); 60 } 61 62 sp<V1_0::IVibrator> halV1_0 = V1_0::IVibrator::getService(); 63 if (halV1_0 == nullptr) { 64 ALOGV("Vibrator HAL service not available."); 65 gHalExists = false; 66 return nullptr; 67 } 68 69 sp<V1_3::IVibrator> halV1_3 = V1_3::IVibrator::castFrom(halV1_0); 70 if (halV1_3) { 71 ALOGV("Successfully connected to Vibrator HAL v1.3 service."); 72 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) { 76 ALOGV("Successfully connected to Vibrator HAL v1.2 service."); 77 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) { 81 ALOGV("Successfully connected to Vibrator HAL v1.1 service."); 82 return std::make_shared<HidlHalWrapperV1_1>(std::move(scheduler), halV1_1); 83 } 84 ALOGV("Successfully connected to Vibrator HAL v1.0 service."); 85 return std::make_shared<HidlHalWrapperV1_0>(std::move(scheduler), halV1_0); 86 } 87 88 // ------------------------------------------------------------------------------------------------- 89 init()90bool HalController::init() { 91 std::lock_guard<std::mutex> lock(mConnectedHalMutex); 92 if (mConnectedHal == nullptr) { 93 mConnectedHal = mConnector(mCallbackScheduler); 94 } 95 return mConnectedHal != nullptr; 96 } 97 tryReconnect()98void HalController::tryReconnect() { 99 std::lock_guard<std::mutex> lock(mConnectedHalMutex); 100 if (mConnectedHal == nullptr) { 101 mConnectedHal = mConnector(mCallbackScheduler); 102 } else { 103 mConnectedHal->tryReconnect(); 104 } 105 } 106 107 }; // namespace vibrator 108 109 }; // namespace android 110