blob: bcb207b7d017f8807752ad6aa5af1b92ec2a88d6 [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>
21
22#include "utils.h"
23
24#include "log/log.h"
25
26namespace android {
27
28using hardware::Return;
29
30static constexpr int NUM_TRIES = 2;
31
32// Creates a Return<R> with STATUS::EX_NULL_POINTER.
33template <class R>
34inline Return<R> NullptrStatus() {
35 using ::android::hardware::Status;
36 return Return<R>{Status::fromExceptionCode(Status::EX_NULL_POINTER)};
37}
38
39template <typename I>
40class HalWrapper {
41public:
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
54private:
55 HalWrapper(sp<I>&& hal) : mHal(std::move(hal)) {}
56
57private:
58 sp<I> mHal;
59};
60
61template <typename I>
62static auto getHal() {
63 static auto sHalWrapper = HalWrapper<I>::Create();
64 return sHalWrapper.get();
65}
66
67template <class R, class I, class... Args0, class... Args1>
68Return<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
73namespace idlcli {
74namespace vibrator {
75
76namespace V1_0 = ::android::hardware::vibrator::V1_0;
77namespace V1_1 = ::android::hardware::vibrator::V1_1;
78namespace V1_2 = ::android::hardware::vibrator::V1_2;
79namespace 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_