1 /* 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 21 #include <functional> 22 23 namespace android::hardware::hidl_utils { 24 25 /** 26 * Helper functor to fetch results from multi-return HIDL calls. 27 * It's meant to be used in place of _hidl_cb callbacks. 28 * 29 * 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 */ 39 template <typename... T> 40 struct 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 */ fillfill46 fill(T*... args) : mTargets(args...) {} 47 operatorfill48 void operator()(const T&... args) { copy<0, T...>(args...); } 49 50 private: 51 std::tuple<T*...> mTargets; 52 53 template <int Pos, typename First> copyfill54 inline void copy(const First& first) { 55 *std::get<Pos>(mTargets) = first; 56 } 57 58 template <int Pos, typename First, typename... Rest> copyfill59 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. 67 * \param hal HAL to which death to link 68 */ 69 void linkDeathToDeath(sp<hidl::base::V1_0::IBase> hal); 70 71 /** 72 * List HAL instances of a given interface. 73 * 74 * \descriptor HIDL HAL descriptor 75 */ 76 hidl_vec<hidl_string> listManifestByInterface(const char* descriptor); 77 78 } // namespace android::hardware::hidl_utils 79