HidlTransportUtils.h revision 2ac326f3a86f4c6c1c087ea4a565df5b918e4d2b
1/* 2 * Copyright (C) 2016 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 ANDROID_HIDL_TRANSPORT_UTILS_H 18#define ANDROID_HIDL_TRANSPORT_UTILS_H 19 20#include <android/hidl/base/1.0/IBase.h> 21 22namespace android { 23namespace hardware { 24namespace details { 25 26/* 27 * Verifies the interface chain of 'interface' contains 'castTo' 28 */ 29inline bool canCastInterface(::android::hidl::base::V1_0::IBase* interface, const char* castTo) { 30 if (interface == nullptr) { 31 return false; 32 } 33 34 bool canCast = false; 35 auto ret = interface->interfaceChain([&](const hidl_vec<hidl_string> &types) { 36 for (size_t i = 0; i < types.size(); i++) { 37 if (types[i] == castTo) { 38 canCast = true; 39 break; 40 } 41 } 42 }); 43 return ret.isOk() && canCast; 44} 45 46inline std::string getDescriptor(::android::hidl::base::V1_0::IBase* interface) { 47 std::string myDescriptor{}; 48 auto ret = interface->interfaceDescriptor([&](const hidl_string &types) { 49 myDescriptor = types.c_str(); 50 }); 51 ret.isOk(); // ignored, return empty string if not isOk() 52 return myDescriptor; 53} 54 55} // namespace details 56} // namespace hardware 57} // namespace android 58 59#endif //ANDROID_HIDL_TRANSPORT_UTILS_H 60