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 #include <hidl/HidlTransportUtils.h> 18 19 namespace android { 20 namespace hardware { 21 namespace details { 22 23 Return<bool> canCastInterface(::android::hidl::base::V1_0::IBase* interface, 24 const char* castTo, bool emitError) { 25 if (interface == nullptr) { 26 return false; 27 } 28 29 bool canCast = false; 30 auto chainRet = interface->interfaceChain([&](const hidl_vec<hidl_string> &types) { 31 for (size_t i = 0; i < types.size(); i++) { 32 if (types[i] == castTo) { 33 canCast = true; 34 break; 35 } 36 } 37 }); 38 39 if (!chainRet.isOk()) { 40 // call fails, propagate the error if emitError 41 return emitError 42 ? details::StatusOf<void, bool>(chainRet) 43 : Return<bool>(false); 44 } 45 46 return canCast; 47 } 48 49 std::string getDescriptor(::android::hidl::base::V1_0::IBase* interface) { 50 std::string myDescriptor{}; 51 auto ret = interface->interfaceDescriptor([&](const hidl_string &types) { 52 myDescriptor = types.c_str(); 53 }); 54 ret.isOk(); // ignored, return empty string if not isOk() 55 return myDescriptor; 56 } 57 58 } // namespace details 59 } // namespace hardware 60 } // namespace android 61