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 | */ |
| 16 | |
| 17 | #ifndef FRAMEWORK_NATIVE_CMDS_IDLCLI_VIBRATOR_H_ |
| 18 | #define FRAMEWORK_NATIVE_CMDS_IDLCLI_VIBRATOR_H_ |
| 19 | |
| 20 | #include <android/hardware/vibrator/1.3/IVibrator.h> |
| 21 | |
| 22 | #include "utils.h" |
| 23 | |
| 24 | #include "log/log.h" |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | using hardware::Return; |
| 29 | |
| 30 | static constexpr int NUM_TRIES = 2; |
| 31 | |
| 32 | // Creates a Return<R> with STATUS::EX_NULL_POINTER. |
| 33 | template <class R> |
| 34 | inline Return<R> NullptrStatus() { |
| 35 | using ::android::hardware::Status; |
| 36 | return Return<R>{Status::fromExceptionCode(Status::EX_NULL_POINTER)}; |
| 37 | } |
| 38 | |
| 39 | template <typename I> |
| 40 | class HalWrapper { |
| 41 | public: |
| 42 | static std::unique_ptr<HalWrapper> Create() { |
| 43 | // Assume that if getService returns a nullptr, HAL is not available on the |
| 44 | // device. |
| 45 | auto hal = I::getService(); |
| 46 | return hal ? std::unique_ptr<HalWrapper>(new HalWrapper(std::move(hal))) : nullptr; |
| 47 | } |
| 48 | |
| 49 | template <class R, class... Args0, class... Args1> |
| 50 | Return<R> call(Return<R> (I::*fn)(Args0...), Args1&&... args1) { |
| 51 | return (*mHal.*fn)(std::forward<Args1>(args1)...); |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | HalWrapper(sp<I>&& hal) : mHal(std::move(hal)) {} |
| 56 | |
| 57 | private: |
| 58 | sp<I> mHal; |
| 59 | }; |
| 60 | |
| 61 | template <typename I> |
| 62 | static auto getHal() { |
| 63 | static auto sHalWrapper = HalWrapper<I>::Create(); |
| 64 | return sHalWrapper.get(); |
| 65 | } |
| 66 | |
| 67 | template <class R, class I, class... Args0, class... Args1> |
| 68 | Return<R> halCall(Return<R> (I::*fn)(Args0...), Args1&&... args1) { |
| 69 | auto hal = getHal<I>(); |
| 70 | return hal ? hal->call(fn, std::forward<Args1>(args1)...) : NullptrStatus<R>(); |
| 71 | } |
| 72 | |
| 73 | namespace idlcli { |
| 74 | namespace vibrator { |
| 75 | |
| 76 | namespace V1_0 = ::android::hardware::vibrator::V1_0; |
| 77 | namespace V1_1 = ::android::hardware::vibrator::V1_1; |
| 78 | namespace V1_2 = ::android::hardware::vibrator::V1_2; |
| 79 | namespace V1_3 = ::android::hardware::vibrator::V1_3; |
| 80 | |
| 81 | } // namespace vibrator |
| 82 | } // namespace idlcli |
| 83 | |
| 84 | } // namespace android |
| 85 | |
| 86 | #endif // FRAMEWORK_NATIVE_CMDS_IDLCLI_VIBRATOR_H_ |