17f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong/* 27f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * Copyright (C) 2016 The Android Open Source Project 37f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * 47f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * Licensed under the Apache License, Version 2.0 (the "License"); 57f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * you may not use this file except in compliance with the License. 67f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * You may obtain a copy of the License at 77f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * 87f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * http://www.apache.org/licenses/LICENSE-2.0 97f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * 107f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * Unless required by applicable law or agreed to in writing, software 117f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * distributed under the License is distributed on an "AS IS" BASIS, 127f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * See the License for the specific language governing permissions and 147f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * limitations under the License. 157f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong */ 167f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong 177f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong#ifndef ANDROID_HIDL_TRANSPORT_SUPPORT_H 187f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong#define ANDROID_HIDL_TRANSPORT_SUPPORT_H 197f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong 2081ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen#include <android/hidl/base/1.0/IBase.h> 217f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong#include <hidl/HidlBinderSupport.h> 22b2c9c75b74c87e651dd08a100015eab17319556eYifan Hong#include <hidl/HidlSupport.h> 2312f04d913dae2fb287089f6db24f144731c79d9cMartijn Coenen#include <hidl/HidlTransportUtils.h> 247f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong 257f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hongnamespace android { 267f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hongnamespace hardware { 277f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong 28e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen/* Configures the threadpool used for handling incoming RPC calls in this process. 29e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * 30e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * This method MUST be called before interacting with any HIDL interfaces, 31e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * including the IFoo::getService and IFoo::registerAsService methods. 32e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * 33e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * @param maxThreads maximum number of threads in this process 34e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * @param callerWillJoin whether the caller will join the threadpool later. 35e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * 36e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * Note that maxThreads must include the caller thread if callerWillJoin is true; 37e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * 38e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * If you want to create a threadpool of 5 threads, without the caller ever joining: 39e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * configureRpcThreadPool(5, false); 40e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * If you want to create a threadpool of 1 thread, with the caller joining: 41e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * configureRpcThreadPool(1, true); // transport won't launch any threads by itself 42e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * 43e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen */ 4411a732a9acc36245ccfe99f99a755b3e5db125a3Steven Morelandvoid configureRpcThreadpool(size_t maxThreads, bool callerWillJoin); 45e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen 46e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen/* Joins a threadpool that you configured earlier with 47e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen * configureRpcThreadPool(x, true); 48e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen */ 4911a732a9acc36245ccfe99f99a755b3e5db125a3Steven Morelandvoid joinRpcThreadpool(); 50e76c7a27eeb3763a2185c1502fd4572df1513b9aMartijn Coenen 5181ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen/** 5281ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen * Sets a minimum scheduler policy for all transactions coming into this 5381ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen * service. 5481ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen * 5581ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen * This method MUST be called before passing this service to another process 5681ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen * and/or registering it with registerAsService(). 5781ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen * 5881ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen * @param service the service to set the policy for 5981ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen * @param policy scheduler policy as defined in linux UAPI 6081ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen * @param priority priority. [-20..19] for SCHED_NORMAL, [1..99] for RT 6181ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen */ 6281ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenenbool setMinSchedulerPolicy(const sp<::android::hidl::base::V1_0::IBase>& service, 6381ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen int policy, int priority); 6481ef4da81353cf86d185e90239e4f06d2930c2baMartijn Coenen 65e67a676fdf3f0c565af64e375075e5386e6ab940Steven Morelandtemplate <typename ILeft, typename IRight> 66e67a676fdf3f0c565af64e375075e5386e6ab940Steven Morelandbool interfacesEqual(sp<ILeft> left, sp<IRight> right) { 67e67a676fdf3f0c565af64e375075e5386e6ab940Steven Moreland if (left == nullptr || right == nullptr || !left->isRemote() || !right->isRemote()) { 68e67a676fdf3f0c565af64e375075e5386e6ab940Steven Moreland return left == right; 69e67a676fdf3f0c565af64e375075e5386e6ab940Steven Moreland } 70e67a676fdf3f0c565af64e375075e5386e6ab940Steven Moreland 71e67a676fdf3f0c565af64e375075e5386e6ab940Steven Moreland return toBinder<ILeft>(left) == toBinder<IRight>(right); 72e67a676fdf3f0c565af64e375075e5386e6ab940Steven Moreland} 73e67a676fdf3f0c565af64e375075e5386e6ab940Steven Moreland 748d498ef0fb85d583b1a30d7fe1d2c2b153fa7407Yifan Hongnamespace details { 758d498ef0fb85d583b1a30d7fe1d2c2b153fa7407Yifan Hong 767f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong// cast the interface IParent to IChild. 776cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong// Return nonnull if cast successful. 786cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong// Return nullptr if: 796cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong// 1. parent is null 806cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong// 2. cast failed because IChild is not a child type of IParent. 816cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong// 3. !emitError, calling into parent fails. 826cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong// Return an error Return object if: 836cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong// 1. emitError, calling into parent fails. 84e67a676fdf3f0c565af64e375075e5386e6ab940Steven Morelandtemplate <typename IChild, typename IParent, typename BpChild> 85e67a676fdf3f0c565af64e375075e5386e6ab940Steven MorelandReturn<sp<IChild>> castInterface(sp<IParent> parent, const char* childIndicator, bool emitError) { 867f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong if (parent.get() == nullptr) { 877f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong // casts always succeed with nullptrs. 887f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong return nullptr; 897f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong } 906cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong Return<bool> canCastRet = details::canCastInterface(parent.get(), childIndicator, emitError); 916cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong if (!canCastRet.isOk()) { 926cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong // call fails, propagate the error if emitError 936cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong return emitError 946cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong ? details::StatusOf<bool, sp<IChild>>(canCastRet) 956cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong : Return<sp<IChild>>(sp<IChild>(nullptr)); 966cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong } 977f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong 986cd7d98e15913f69b100f10d88389b028e88f288Yifan Hong if (!canCastRet) { 997f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong return sp<IChild>(nullptr); // cast failed. 1007f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong } 1017f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong // TODO b/32001926 Needs to be fixed for socket mode. 1027f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong if (parent->isRemote()) { 1037f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong // binderized mode. Got BpChild. grab the remote and wrap it. 104e67a676fdf3f0c565af64e375075e5386e6ab940Steven Moreland return sp<IChild>(new BpChild(toBinder<IParent>(parent))); 1057f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong } 1067f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong // Passthrough mode. Got BnChild and BsChild. 1077f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong return sp<IChild>(static_cast<IChild *>(parent.get())); 1087f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong} 1097f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong 1108d498ef0fb85d583b1a30d7fe1d2c2b153fa7407Yifan Hong} // namespace details 1118d498ef0fb85d583b1a30d7fe1d2c2b153fa7407Yifan Hong 1127f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong} // namespace hardware 1137f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong} // namespace android 1147f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong 1157f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong 1167f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong#endif // ANDROID_HIDL_TRANSPORT_SUPPORT_H 117