blob: be3386fb6ed40318e3e62f7ce0ad2fe895fac353 [file] [log] [blame]
Tomasz Wasilczykbff3b5b2021-10-18 16:53:40 -07001/*
2 * Copyright (C) 2021 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#pragma once
18
19#include <android/hidl/base/1.0/IBase.h>
20
Tomasz Wasilczyk9ddc87f2021-10-25 20:20:49 -070021#include <functional>
22
Tomasz Wasilczykbff3b5b2021-10-18 16:53:40 -070023namespace android::hardware::hidl_utils {
24
25/**
Tomasz Wasilczyk9ddc87f2021-10-25 20:20:49 -070026 * Helper functor to fetch results from multi-return HIDL calls.
27 * It's meant to be used in place of _hidl_cb callbacks.
Tomasz Wasilczykbff3b5b2021-10-18 16:53:40 -070028 *
Tomasz Wasilczyk9ddc87f2021-10-25 20:20:49 -070029 * Please note extracting these return variables outside of the callback scope requires making
30 * a copy of each return variable. This may be costly for frequently called HIDL methods with
31 * non-negligible return object size. Please be cautious about performance when using this.
32 *
33 * Example usage:
34 * Result result;
35 * sp<ISomeInterface> iface;
36 * hidlObject->someMethod(arg1, arg2, hidl_utils::fill(&result, &iface)).assertOk();
37 * // use result and iface
38 */
39template <typename... T>
40struct fill : public std::function<void(const T&...)> {
41 /**
42 * Create _hidl_cb functor that copies the call arguments to specified pointers.
43 *
44 * \param args... Targets to copy the call arguments to
45 */
46 fill(T*... args) : mTargets(args...) {}
47
48 void operator()(const T&... args) { copy<0, T...>(args...); }
49
50 private:
51 std::tuple<T*...> mTargets;
52
53 template <int Pos, typename First>
54 inline void copy(const First& first) {
55 *std::get<Pos>(mTargets) = first;
56 }
57
58 template <int Pos, typename First, typename... Rest>
59 inline void copy(const First& first, const Rest&... rest) {
60 *std::get<Pos>(mTargets) = first;
61 copy<Pos + 1, Rest...>(rest...);
62 }
63};
64
65/**
66 * Link to a given HALs death and restart the current process in such a case.
Tomasz Wasilczykbff3b5b2021-10-18 16:53:40 -070067 * \param hal HAL to which death to link
68 */
69void linkDeathToDeath(sp<hidl::base::V1_0::IBase> hal);
70
Tomasz Wasilczyk9ddc87f2021-10-25 20:20:49 -070071/**
72 * List HAL instances of a given interface.
73 *
74 * \descriptor HIDL HAL descriptor
75 */
76hidl_vec<hidl_string> listManifestByInterface(const char* descriptor);
77
Tomasz Wasilczykbff3b5b2021-10-18 16:53:40 -070078} // namespace android::hardware::hidl_utils