xref: /aosp_15_r20/hardware/interfaces/radio/aidl/compat/service/hidl-utils.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1*4d7e907cSAndroid Build Coastguard Worker /*
2*4d7e907cSAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*4d7e907cSAndroid Build Coastguard Worker  *
4*4d7e907cSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*4d7e907cSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*4d7e907cSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*4d7e907cSAndroid Build Coastguard Worker  *
8*4d7e907cSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*4d7e907cSAndroid Build Coastguard Worker  *
10*4d7e907cSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*4d7e907cSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*4d7e907cSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4d7e907cSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*4d7e907cSAndroid Build Coastguard Worker  * limitations under the License.
15*4d7e907cSAndroid Build Coastguard Worker  */
16*4d7e907cSAndroid Build Coastguard Worker 
17*4d7e907cSAndroid Build Coastguard Worker #pragma once
18*4d7e907cSAndroid Build Coastguard Worker 
19*4d7e907cSAndroid Build Coastguard Worker #include <android/hidl/base/1.0/IBase.h>
20*4d7e907cSAndroid Build Coastguard Worker 
21*4d7e907cSAndroid Build Coastguard Worker #include <functional>
22*4d7e907cSAndroid Build Coastguard Worker 
23*4d7e907cSAndroid Build Coastguard Worker namespace android::hardware::hidl_utils {
24*4d7e907cSAndroid Build Coastguard Worker 
25*4d7e907cSAndroid Build Coastguard Worker /**
26*4d7e907cSAndroid Build Coastguard Worker  * Helper functor to fetch results from multi-return HIDL calls.
27*4d7e907cSAndroid Build Coastguard Worker  * It's meant to be used in place of _hidl_cb callbacks.
28*4d7e907cSAndroid Build Coastguard Worker  *
29*4d7e907cSAndroid Build Coastguard Worker  * Please note extracting these return variables outside of the callback scope requires making
30*4d7e907cSAndroid Build Coastguard Worker  * a copy of each return variable. This may be costly for frequently called HIDL methods with
31*4d7e907cSAndroid Build Coastguard Worker  * non-negligible return object size. Please be cautious about performance when using this.
32*4d7e907cSAndroid Build Coastguard Worker  *
33*4d7e907cSAndroid Build Coastguard Worker  * Example usage:
34*4d7e907cSAndroid Build Coastguard Worker  *     Result result;
35*4d7e907cSAndroid Build Coastguard Worker  *     sp<ISomeInterface> iface;
36*4d7e907cSAndroid Build Coastguard Worker  *     hidlObject->someMethod(arg1, arg2, hidl_utils::fill(&result, &iface)).assertOk();
37*4d7e907cSAndroid Build Coastguard Worker  *     // use result and iface
38*4d7e907cSAndroid Build Coastguard Worker  */
39*4d7e907cSAndroid Build Coastguard Worker template <typename... T>
40*4d7e907cSAndroid Build Coastguard Worker struct fill : public std::function<void(const T&...)> {
41*4d7e907cSAndroid Build Coastguard Worker     /**
42*4d7e907cSAndroid Build Coastguard Worker      * Create _hidl_cb functor that copies the call arguments to specified pointers.
43*4d7e907cSAndroid Build Coastguard Worker      *
44*4d7e907cSAndroid Build Coastguard Worker      * \param args... Targets to copy the call arguments to
45*4d7e907cSAndroid Build Coastguard Worker      */
fillfill46*4d7e907cSAndroid Build Coastguard Worker     fill(T*... args) : mTargets(args...) {}
47*4d7e907cSAndroid Build Coastguard Worker 
operatorfill48*4d7e907cSAndroid Build Coastguard Worker     void operator()(const T&... args) { copy<0, T...>(args...); }
49*4d7e907cSAndroid Build Coastguard Worker 
50*4d7e907cSAndroid Build Coastguard Worker   private:
51*4d7e907cSAndroid Build Coastguard Worker     std::tuple<T*...> mTargets;
52*4d7e907cSAndroid Build Coastguard Worker 
53*4d7e907cSAndroid Build Coastguard Worker     template <int Pos, typename First>
copyfill54*4d7e907cSAndroid Build Coastguard Worker     inline void copy(const First& first) {
55*4d7e907cSAndroid Build Coastguard Worker         *std::get<Pos>(mTargets) = first;
56*4d7e907cSAndroid Build Coastguard Worker     }
57*4d7e907cSAndroid Build Coastguard Worker 
58*4d7e907cSAndroid Build Coastguard Worker     template <int Pos, typename First, typename... Rest>
copyfill59*4d7e907cSAndroid Build Coastguard Worker     inline void copy(const First& first, const Rest&... rest) {
60*4d7e907cSAndroid Build Coastguard Worker         *std::get<Pos>(mTargets) = first;
61*4d7e907cSAndroid Build Coastguard Worker         copy<Pos + 1, Rest...>(rest...);
62*4d7e907cSAndroid Build Coastguard Worker     }
63*4d7e907cSAndroid Build Coastguard Worker };
64*4d7e907cSAndroid Build Coastguard Worker 
65*4d7e907cSAndroid Build Coastguard Worker /**
66*4d7e907cSAndroid Build Coastguard Worker  * Link to a given HALs death and restart the current process in such a case.
67*4d7e907cSAndroid Build Coastguard Worker  * \param hal HAL to which death to link
68*4d7e907cSAndroid Build Coastguard Worker  */
69*4d7e907cSAndroid Build Coastguard Worker void linkDeathToDeath(sp<hidl::base::V1_0::IBase> hal);
70*4d7e907cSAndroid Build Coastguard Worker 
71*4d7e907cSAndroid Build Coastguard Worker /**
72*4d7e907cSAndroid Build Coastguard Worker  * List HAL instances of a given interface.
73*4d7e907cSAndroid Build Coastguard Worker  *
74*4d7e907cSAndroid Build Coastguard Worker  * \descriptor HIDL HAL descriptor
75*4d7e907cSAndroid Build Coastguard Worker  */
76*4d7e907cSAndroid Build Coastguard Worker hidl_vec<hidl_string> listManifestByInterface(const char* descriptor);
77*4d7e907cSAndroid Build Coastguard Worker 
78*4d7e907cSAndroid Build Coastguard Worker }  // namespace android::hardware::hidl_utils
79