HidlTransportSupport.h revision e76c7a27eeb3763a2185c1502fd4572df1513b9a
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/HidlBinderSupport.h> 21#include <hidl/HidlSupport.h> 22#include <hidl/HidlTransportUtils.h> 23 24namespace android { 25namespace hardware { 26 27/* Configures the threadpool used for handling incoming RPC calls in this process. 28 * 29 * This method MUST be called before interacting with any HIDL interfaces, 30 * including the IFoo::getService and IFoo::registerAsService methods. 31 * 32 * @param maxThreads maximum number of threads in this process 33 * @param callerWillJoin whether the caller will join the threadpool later. 34 * 35 * Note that maxThreads must include the caller thread if callerWillJoin is true; 36 * 37 * If you want to create a threadpool of 5 threads, without the caller ever joining: 38 * configureRpcThreadPool(5, false); 39 * If you want to create a threadpool of 1 thread, with the caller joining: 40 * configureRpcThreadPool(1, true); // transport won't launch any threads by itself 41 * 42 */ 43inline void configureRpcThreadpool(size_t maxThreads, bool callerWillJoin) { 44 // TODO(b/32756130) this should be transport-dependent 45 configureBinderRpcThreadpool(maxThreads, callerWillJoin); 46} 47 48/* Joins a threadpool that you configured earlier with 49 * configureRpcThreadPool(x, true); 50 */ 51inline void joinRpcThreadpool() { 52 // TODO(b/32756130) this should be transport-dependent 53 joinBinderRpcThreadpool(); 54} 55 56// cast the interface IParent to IChild. 57// Return nullptr if parent is null or any failure. 58template<typename IChild, typename IParent, typename BpChild, typename BpParent> 59sp<IChild> castInterface(sp<IParent> parent, const char *childIndicator) { 60 if (parent.get() == nullptr) { 61 // casts always succeed with nullptrs. 62 return nullptr; 63 } 64 bool canCast = canCastInterface(parent.get(), childIndicator); 65 66 if (!canCast) { 67 return sp<IChild>(nullptr); // cast failed. 68 } 69 // TODO b/32001926 Needs to be fixed for socket mode. 70 if (parent->isRemote()) { 71 // binderized mode. Got BpChild. grab the remote and wrap it. 72 return sp<IChild>(new BpChild(toBinder<IParent, BpParent>(parent))); 73 } 74 // Passthrough mode. Got BnChild and BsChild. 75 return sp<IChild>(static_cast<IChild *>(parent.get())); 76} 77 78} // namespace hardware 79} // namespace android 80 81 82#endif // ANDROID_HIDL_TRANSPORT_SUPPORT_H 83