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 <aidl/android/hardware/vibrator/IVibrator.h> 20 #include <android/binder_manager.h> 21 #include <android/hardware/vibrator/1.3/IVibrator.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 aidl::android::hardware::vibrator::CompositeEffect; 31 using aidl::android::hardware::vibrator::CompositePrimitive; 32 using aidl::android::hardware::vibrator::Effect; 33 using aidl::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 = 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 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 } 64 } 65 66 sp<V1_0::IVibrator> halV1_0 = V1_0::IVibrator::getService(); 67 if (halV1_0 == nullptr) { 68 ALOGV("Vibrator HAL service not available."); 69 gHalExists = false; 70 return nullptr; 71 } 72 73 sp<V1_3::IVibrator> halV1_3 = V1_3::IVibrator::castFrom(halV1_0); 74 if (halV1_3) { 75 ALOGV("Successfully connected to Vibrator HAL v1.3 service."); 76 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) { 80 ALOGV("Successfully connected to Vibrator HAL v1.2 service."); 81 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) { 85 ALOGV("Successfully connected to Vibrator HAL v1.1 service."); 86 return std::make_shared<HidlHalWrapperV1_1>(std::move(scheduler), halV1_1); 87 } 88 ALOGV("Successfully connected to Vibrator HAL v1.0 service."); 89 return std::make_shared<HidlHalWrapperV1_0>(std::move(scheduler), halV1_0); 90 } 91 92 // ------------------------------------------------------------------------------------------------- 93 init()94bool HalController::init() { 95 std::lock_guard<std::mutex> lock(mConnectedHalMutex); 96 if (mConnectedHal == nullptr) { 97 mConnectedHal = mConnector(mCallbackScheduler); 98 } 99 return mConnectedHal != nullptr; 100 } 101 tryReconnect()102void HalController::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 111 }; // namespace vibrator 112 113 }; // namespace android 114