Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 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 | |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 19 | #include <aidl/android/hardware/vibrator/IVibrator.h> |
| 20 | #include <android/binder_manager.h> |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 21 | #include <android/hardware/vibrator/1.3/IVibrator.h> |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 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 | |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 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; |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 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; |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 41 | namespace Aidl = aidl::android::hardware::vibrator; |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 42 | |
| 43 | namespace android { |
| 44 | |
| 45 | namespace vibrator { |
| 46 | |
| 47 | // ------------------------------------------------------------------------------------------------- |
| 48 | |
Lais Andrade | f20b144 | 2020-11-19 15:14:10 +0000 | [diff] [blame] | 49 | std::shared_ptr<HalWrapper> connectHal(std::shared_ptr<CallbackScheduler> scheduler) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 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. |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 53 | return nullptr; |
| 54 | } |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 55 | |
Lais Andrade | 818a225 | 2024-06-24 14:48:14 +0100 | [diff] [blame] | 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 | } |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 64 | } |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 65 | |
| 66 | sp<V1_0::IVibrator> halV1_0 = V1_0::IVibrator::getService(); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 67 | if (halV1_0 == nullptr) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 68 | ALOGV("Vibrator HAL service not available."); |
| 69 | gHalExists = false; |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 70 | return nullptr; |
| 71 | } |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 72 | |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 73 | sp<V1_3::IVibrator> halV1_3 = V1_3::IVibrator::castFrom(halV1_0); |
| 74 | if (halV1_3) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 75 | ALOGV("Successfully connected to Vibrator HAL v1.3 service."); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 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) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 80 | ALOGV("Successfully connected to Vibrator HAL v1.2 service."); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 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) { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 85 | ALOGV("Successfully connected to Vibrator HAL v1.1 service."); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 86 | return std::make_shared<HidlHalWrapperV1_1>(std::move(scheduler), halV1_1); |
| 87 | } |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 88 | ALOGV("Successfully connected to Vibrator HAL v1.0 service."); |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 89 | return std::make_shared<HidlHalWrapperV1_0>(std::move(scheduler), halV1_0); |
| 90 | } |
| 91 | |
| 92 | // ------------------------------------------------------------------------------------------------- |
| 93 | |
Lais Andrade | 24b5b8f | 2020-08-27 15:55:54 +0000 | [diff] [blame] | 94 | bool HalController::init() { |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 95 | std::lock_guard<std::mutex> lock(mConnectedHalMutex); |
| 96 | if (mConnectedHal == nullptr) { |
Lais Andrade | f20b144 | 2020-11-19 15:14:10 +0000 | [diff] [blame] | 97 | mConnectedHal = mConnector(mCallbackScheduler); |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 98 | } |
Lais Andrade | 24b5b8f | 2020-08-27 15:55:54 +0000 | [diff] [blame] | 99 | return mConnectedHal != nullptr; |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 102 | void HalController::tryReconnect() { |
| 103 | std::lock_guard<std::mutex> lock(mConnectedHalMutex); |
| 104 | if (mConnectedHal == nullptr) { |
Lais Andrade | f20b144 | 2020-11-19 15:14:10 +0000 | [diff] [blame] | 105 | mConnectedHal = mConnector(mCallbackScheduler); |
Lais Andrade | cfd8115 | 2020-07-01 09:00:26 +0000 | [diff] [blame] | 106 | } else { |
| 107 | mConnectedHal->tryReconnect(); |
| 108 | } |
| 109 | } |
| 110 | |
Lais Andrade | 921b698 | 2020-06-04 16:17:53 +0000 | [diff] [blame] | 111 | }; // namespace vibrator |
| 112 | |
| 113 | }; // namespace android |