blob: ca5142dee9ca96dfdbb9e23b811a0befab9bf792 [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 */
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090016#ifndef FRAMEWORK_NATIVE_CMDS_IDLCLI_VIBRATOR_H_
17#define FRAMEWORK_NATIVE_CMDS_IDLCLI_VIBRATOR_H_
18
Harpreet \"Eli\" Sangha6ce6f732019-11-28 16:53:41 +090019#include <aidl/android/hardware/vibrator/IVibrator.h>
20#include <android/binder_manager.h>
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090021#include <android/hardware/vibrator/1.3/IVibrator.h>
22
23#include "utils.h"
24
25#include "log/log.h"
26
27namespace android {
28
29using hardware::Return;
30
31static constexpr int NUM_TRIES = 2;
32
33// Creates a Return<R> with STATUS::EX_NULL_POINTER.
34template <class R>
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090035inline R NullptrStatus() {
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090036 using ::android::hardware::Status;
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090037 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
38}
39
40template <>
Harpreet \"Eli\" Sangha6ce6f732019-11-28 16:53:41 +090041inline ndk::ScopedAStatus NullptrStatus() {
42 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_NULL_POINTER));
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090043}
44
45template <typename I>
Harpreet \"Eli\" Sangha6ce6f732019-11-28 16:53:41 +090046inline auto getService() {
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090047 return I::getService();
48}
49
50template <>
Harpreet \"Eli\" Sangha6ce6f732019-11-28 16:53:41 +090051inline auto getService<aidl::android::hardware::vibrator::IVibrator>() {
52 const auto instance =
53 std::string() + aidl::android::hardware::vibrator::IVibrator::descriptor + "/default";
54 auto vibBinder = ndk::SpAIBinder(AServiceManager_getService(instance.c_str()));
55 return aidl::android::hardware::vibrator::IVibrator::fromBinder(vibBinder);
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090056}
57
58template <typename I>
Harpreet \"Eli\" Sangha6ce6f732019-11-28 16:53:41 +090059using shared_ptr = std::result_of_t<decltype(getService<I>)&()>;
60
61template <typename I>
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090062class HalWrapper {
63public:
64 static std::unique_ptr<HalWrapper> Create() {
65 // Assume that if getService returns a nullptr, HAL is not available on the
66 // device.
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090067 auto hal = getService<I>();
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090068 return hal ? std::unique_ptr<HalWrapper>(new HalWrapper(std::move(hal))) : nullptr;
69 }
70
71 template <class R, class... Args0, class... Args1>
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090072 R call(R (I::*fn)(Args0...), Args1&&... args1) {
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090073 return (*mHal.*fn)(std::forward<Args1>(args1)...);
74 }
75
76private:
Harpreet \"Eli\" Sangha6ce6f732019-11-28 16:53:41 +090077 HalWrapper(shared_ptr<I>&& hal) : mHal(std::move(hal)) {}
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090078
79private:
Harpreet \"Eli\" Sangha6ce6f732019-11-28 16:53:41 +090080 shared_ptr<I> mHal;
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090081};
82
83template <typename I>
84static auto getHal() {
85 static auto sHalWrapper = HalWrapper<I>::Create();
86 return sHalWrapper.get();
87}
88
89template <class R, class I, class... Args0, class... Args1>
Harpreet \"Eli\" Sanghacb350b82019-11-12 15:15:25 +090090R halCall(R (I::*fn)(Args0...), Args1&&... args1) {
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +090091 auto hal = getHal<I>();
92 return hal ? hal->call(fn, std::forward<Args1>(args1)...) : NullptrStatus<R>();
93}
94
95namespace idlcli {
96namespace vibrator {
97
98namespace V1_0 = ::android::hardware::vibrator::V1_0;
99namespace V1_1 = ::android::hardware::vibrator::V1_1;
100namespace V1_2 = ::android::hardware::vibrator::V1_2;
101namespace V1_3 = ::android::hardware::vibrator::V1_3;
Harpreet \"Eli\" Sangha6ce6f732019-11-28 16:53:41 +0900102namespace aidl = ::aidl::android::hardware::vibrator;
Harpreet "Eli" Sanghae47ae682019-06-05 16:52:47 +0900103
104} // namespace vibrator
105} // namespace idlcli
106
107} // namespace android
108
109#endif // FRAMEWORK_NATIVE_CMDS_IDLCLI_VIBRATOR_H_