Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | */ |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 16 | #ifndef FRAMEWORK_NATIVE_CMDS_IDLCLI_VIBRATOR_H_ |
| 17 | #define FRAMEWORK_NATIVE_CMDS_IDLCLI_VIBRATOR_H_ |
| 18 | |
Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame^] | 19 | #include <future> |
| 20 | |
| 21 | #include <aidl/android/hardware/vibrator/BnVibratorCallback.h> |
Harpreet \"Eli\" Sangha | 6ce6f73 | 2019-11-28 16:53:41 +0900 | [diff] [blame] | 22 | #include <aidl/android/hardware/vibrator/IVibrator.h> |
| 23 | #include <android/binder_manager.h> |
Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame^] | 24 | #include <android/binder_process.h> |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 25 | #include <android/hardware/vibrator/1.3/IVibrator.h> |
| 26 | |
| 27 | #include "utils.h" |
| 28 | |
| 29 | #include "log/log.h" |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | using hardware::Return; |
| 34 | |
| 35 | static constexpr int NUM_TRIES = 2; |
| 36 | |
| 37 | // Creates a Return<R> with STATUS::EX_NULL_POINTER. |
| 38 | template <class R> |
Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 39 | inline R NullptrStatus() { |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 40 | using ::android::hardware::Status; |
Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 41 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 42 | } |
| 43 | |
| 44 | template <> |
Harpreet \"Eli\" Sangha | 6ce6f73 | 2019-11-28 16:53:41 +0900 | [diff] [blame] | 45 | inline ndk::ScopedAStatus NullptrStatus() { |
| 46 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_NULL_POINTER)); |
Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | template <typename I> |
Harpreet \"Eli\" Sangha | 6ce6f73 | 2019-11-28 16:53:41 +0900 | [diff] [blame] | 50 | inline auto getService() { |
Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 51 | return I::getService(); |
| 52 | } |
| 53 | |
| 54 | template <> |
Harpreet \"Eli\" Sangha | 6ce6f73 | 2019-11-28 16:53:41 +0900 | [diff] [blame] | 55 | inline auto getService<aidl::android::hardware::vibrator::IVibrator>() { |
| 56 | const auto instance = |
| 57 | std::string() + aidl::android::hardware::vibrator::IVibrator::descriptor + "/default"; |
| 58 | auto vibBinder = ndk::SpAIBinder(AServiceManager_getService(instance.c_str())); |
| 59 | return aidl::android::hardware::vibrator::IVibrator::fromBinder(vibBinder); |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | template <typename I> |
Harpreet \"Eli\" Sangha | 6ce6f73 | 2019-11-28 16:53:41 +0900 | [diff] [blame] | 63 | using shared_ptr = std::result_of_t<decltype(getService<I>)&()>; |
| 64 | |
| 65 | template <typename I> |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 66 | class HalWrapper { |
| 67 | public: |
| 68 | static std::unique_ptr<HalWrapper> Create() { |
| 69 | // Assume that if getService returns a nullptr, HAL is not available on the |
| 70 | // device. |
Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 71 | auto hal = getService<I>(); |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 72 | return hal ? std::unique_ptr<HalWrapper>(new HalWrapper(std::move(hal))) : nullptr; |
| 73 | } |
| 74 | |
| 75 | template <class R, class... Args0, class... Args1> |
Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 76 | R call(R (I::*fn)(Args0...), Args1&&... args1) { |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 77 | return (*mHal.*fn)(std::forward<Args1>(args1)...); |
| 78 | } |
| 79 | |
| 80 | private: |
Harpreet \"Eli\" Sangha | 6ce6f73 | 2019-11-28 16:53:41 +0900 | [diff] [blame] | 81 | HalWrapper(shared_ptr<I>&& hal) : mHal(std::move(hal)) {} |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 82 | |
| 83 | private: |
Harpreet \"Eli\" Sangha | 6ce6f73 | 2019-11-28 16:53:41 +0900 | [diff] [blame] | 84 | shared_ptr<I> mHal; |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | template <typename I> |
| 88 | static auto getHal() { |
| 89 | static auto sHalWrapper = HalWrapper<I>::Create(); |
| 90 | return sHalWrapper.get(); |
| 91 | } |
| 92 | |
| 93 | template <class R, class I, class... Args0, class... Args1> |
Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 94 | R halCall(R (I::*fn)(Args0...), Args1&&... args1) { |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 95 | auto hal = getHal<I>(); |
| 96 | return hal ? hal->call(fn, std::forward<Args1>(args1)...) : NullptrStatus<R>(); |
| 97 | } |
| 98 | |
| 99 | namespace idlcli { |
| 100 | namespace vibrator { |
| 101 | |
| 102 | namespace V1_0 = ::android::hardware::vibrator::V1_0; |
| 103 | namespace V1_1 = ::android::hardware::vibrator::V1_1; |
| 104 | namespace V1_2 = ::android::hardware::vibrator::V1_2; |
| 105 | namespace V1_3 = ::android::hardware::vibrator::V1_3; |
Harpreet \"Eli\" Sangha | 6ce6f73 | 2019-11-28 16:53:41 +0900 | [diff] [blame] | 106 | namespace aidl = ::aidl::android::hardware::vibrator; |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 107 | |
Harpreet \"Eli\" Sangha | bbe7855 | 2020-04-09 17:16:40 +0900 | [diff] [blame^] | 108 | class VibratorCallback : public aidl::BnVibratorCallback { |
| 109 | public: |
| 110 | ndk::ScopedAStatus onComplete() override { |
| 111 | mPromise.set_value(); |
| 112 | return ndk::ScopedAStatus::ok(); |
| 113 | } |
| 114 | void waitForComplete() { mPromise.get_future().wait(); } |
| 115 | |
| 116 | private: |
| 117 | std::promise<void> mPromise; |
| 118 | }; |
| 119 | |
Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 120 | } // namespace vibrator |
| 121 | } // namespace idlcli |
| 122 | |
| 123 | } // namespace android |
| 124 | |
| 125 | #endif // FRAMEWORK_NATIVE_CMDS_IDLCLI_VIBRATOR_H_ |