HidlTransportSupport.h revision 00f4a391c6f9a698bbcbbd05bf3d80e213c82884
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_SUPPORT_H 18#define ANDROID_HIDL_TRANSPORT_SUPPORT_H 19 20#include <hidl/HidlSupport.h> 21#include <hidl/HidlBinderSupport.h> 22 23namespace android { 24namespace hardware { 25 26// cast the interface IParent to IChild. 27// Return nullptr if parent is null or any failure. 28template<typename IChild, typename IParent, typename BpChild, typename IHwParent> 29sp<IChild> castInterface(sp<IParent> parent, const char *childIndicator) { 30 if (parent.get() == nullptr) { 31 // casts always succeed with nullptrs. 32 return nullptr; 33 } 34 bool canCast = false; 35 parent->interfaceChain([&](const hidl_vec<hidl_string> &allowedCastTypes) { 36 for (size_t i = 0; i < allowedCastTypes.size(); i++) { 37 if (allowedCastTypes[i] == childIndicator) { 38 canCast = true; 39 break; 40 } 41 } 42 }); 43 44 if (!canCast) { 45 return sp<IChild>(nullptr); // cast failed. 46 } 47 // TODO b/32001926 Needs to be fixed for socket mode. 48 if (parent->isRemote()) { 49 // binderized mode. Got BpChild. grab the remote and wrap it. 50 return sp<IChild>(new BpChild(toBinder<IParent, IHwParent>(parent))); 51 } 52 // Passthrough mode. Got BnChild and BsChild. 53 return sp<IChild>(static_cast<IChild *>(parent.get())); 54} 55 56} // namespace hardware 57} // namespace android 58 59 60#endif // ANDROID_HIDL_TRANSPORT_SUPPORT_H 61