blob: 2f119234c17a9c1bea003a5b931e000e9fab28bd [file] [log] [blame]
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +09001/*
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>
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090021#include <android/hardware/vibrator/IVibrator.h>
22#include <binder/IServiceManager.h>
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090023
24#include "utils.h"
25
26#include "log/log.h"
27
28namespace android {
29
30using hardware::Return;
31
32static constexpr int NUM_TRIES = 2;
33
34// Creates a Return<R> with STATUS::EX_NULL_POINTER.
35template <class R>
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090036inline R NullptrStatus() {
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090037 using ::android::hardware::Status;
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090038 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
39}
40
41template <>
42inline binder::Status NullptrStatus() {
43 using binder::Status;
44 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
45}
46
47template <typename I>
48inline sp<I> getService() {
49 return I::getService();
50}
51
52template <>
53inline sp<hardware::vibrator::IVibrator> getService() {
54 return waitForVintfService<hardware::vibrator::IVibrator>();
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090055}
56
57template <typename I>
58class HalWrapper {
59public:
60 static std::unique_ptr<HalWrapper> Create() {
61 // Assume that if getService returns a nullptr, HAL is not available on the
62 // device.
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090063 auto hal = getService<I>();
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090064 return hal ? std::unique_ptr<HalWrapper>(new HalWrapper(std::move(hal))) : nullptr;
65 }
66
67 template <class R, class... Args0, class... Args1>
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090068 R call(R (I::*fn)(Args0...), Args1&&... args1) {
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090069 return (*mHal.*fn)(std::forward<Args1>(args1)...);
70 }
71
72private:
73 HalWrapper(sp<I>&& hal) : mHal(std::move(hal)) {}
74
75private:
76 sp<I> mHal;
77};
78
79template <typename I>
80static auto getHal() {
81 static auto sHalWrapper = HalWrapper<I>::Create();
82 return sHalWrapper.get();
83}
84
85template <class R, class I, class... Args0, class... Args1>
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090086R halCall(R (I::*fn)(Args0...), Args1&&... args1) {
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090087 auto hal = getHal<I>();
88 return hal ? hal->call(fn, std::forward<Args1>(args1)...) : NullptrStatus<R>();
89}
90
91namespace idlcli {
92namespace vibrator {
93
94namespace V1_0 = ::android::hardware::vibrator::V1_0;
95namespace V1_1 = ::android::hardware::vibrator::V1_1;
96namespace V1_2 = ::android::hardware::vibrator::V1_2;
97namespace V1_3 = ::android::hardware::vibrator::V1_3;
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090098namespace aidl = ::android::hardware::vibrator;
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090099
100} // namespace vibrator
101} // namespace idlcli
102
103} // namespace android
104
105#endif // FRAMEWORK_NATIVE_CMDS_IDLCLI_VIBRATOR_H_