blob: d1165985fea49fca8c583e173920e6b6ae0e9ffb [file] [log] [blame]
Yifan Hong7f97f442016-11-14 18:31:05 -08001/*
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
Martijn Coenen81ef4da2017-04-21 16:21:31 -070020#include <android/hidl/base/1.0/IBase.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080021#include <hidl/HidlBinderSupport.h>
Yifan Hongb2c9c752016-12-07 15:15:41 -080022#include <hidl/HidlSupport.h>
Martijn Coenen12f04d92016-12-07 17:29:41 +010023#include <hidl/HidlTransportUtils.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080024
25namespace android {
26namespace hardware {
27
Martijn Coenene76c7a22016-11-22 14:53:47 +010028/* Configures the threadpool used for handling incoming RPC calls in this process.
29 *
30 * This method MUST be called before interacting with any HIDL interfaces,
31 * including the IFoo::getService and IFoo::registerAsService methods.
32 *
33 * @param maxThreads maximum number of threads in this process
34 * @param callerWillJoin whether the caller will join the threadpool later.
35 *
36 * Note that maxThreads must include the caller thread if callerWillJoin is true;
37 *
38 * If you want to create a threadpool of 5 threads, without the caller ever joining:
39 * configureRpcThreadPool(5, false);
40 * If you want to create a threadpool of 1 thread, with the caller joining:
41 * configureRpcThreadPool(1, true); // transport won't launch any threads by itself
42 *
43 */
Steven Moreland11a732a2017-03-07 17:44:17 -080044void configureRpcThreadpool(size_t maxThreads, bool callerWillJoin);
Martijn Coenene76c7a22016-11-22 14:53:47 +010045
46/* Joins a threadpool that you configured earlier with
47 * configureRpcThreadPool(x, true);
48 */
Steven Moreland11a732a2017-03-07 17:44:17 -080049void joinRpcThreadpool();
Martijn Coenene76c7a22016-11-22 14:53:47 +010050
Martijn Coenen81ef4da2017-04-21 16:21:31 -070051/**
52 * Sets a minimum scheduler policy for all transactions coming into this
53 * service.
54 *
55 * This method MUST be called before passing this service to another process
56 * and/or registering it with registerAsService().
57 *
58 * @param service the service to set the policy for
59 * @param policy scheduler policy as defined in linux UAPI
60 * @param priority priority. [-20..19] for SCHED_NORMAL, [1..99] for RT
61 */
62bool setMinSchedulerPolicy(const sp<::android::hidl::base::V1_0::IBase>& service,
63 int policy, int priority);
64
Steven Morelande67a6762017-05-16 14:53:28 -070065template <typename ILeft, typename IRight>
66bool interfacesEqual(sp<ILeft> left, sp<IRight> right) {
67 if (left == nullptr || right == nullptr || !left->isRemote() || !right->isRemote()) {
68 return left == right;
69 }
70
71 return toBinder<ILeft>(left) == toBinder<IRight>(right);
72}
73
Yifan Hong8d498ef2017-03-13 17:47:10 -070074namespace details {
75
Yifan Hong7f97f442016-11-14 18:31:05 -080076// cast the interface IParent to IChild.
Yifan Hong6cd7d982017-03-29 03:36:07 -070077// Return nonnull if cast successful.
78// Return nullptr if:
79// 1. parent is null
80// 2. cast failed because IChild is not a child type of IParent.
81// 3. !emitError, calling into parent fails.
82// Return an error Return object if:
83// 1. emitError, calling into parent fails.
Steven Morelande67a6762017-05-16 14:53:28 -070084template <typename IChild, typename IParent, typename BpChild>
85Return<sp<IChild>> castInterface(sp<IParent> parent, const char* childIndicator, bool emitError) {
Yifan Hong7f97f442016-11-14 18:31:05 -080086 if (parent.get() == nullptr) {
87 // casts always succeed with nullptrs.
88 return nullptr;
89 }
Yifan Hong6cd7d982017-03-29 03:36:07 -070090 Return<bool> canCastRet = details::canCastInterface(parent.get(), childIndicator, emitError);
91 if (!canCastRet.isOk()) {
92 // call fails, propagate the error if emitError
93 return emitError
94 ? details::StatusOf<bool, sp<IChild>>(canCastRet)
95 : Return<sp<IChild>>(sp<IChild>(nullptr));
96 }
Yifan Hong7f97f442016-11-14 18:31:05 -080097
Yifan Hong6cd7d982017-03-29 03:36:07 -070098 if (!canCastRet) {
Yifan Hong7f97f442016-11-14 18:31:05 -080099 return sp<IChild>(nullptr); // cast failed.
100 }
101 // TODO b/32001926 Needs to be fixed for socket mode.
102 if (parent->isRemote()) {
103 // binderized mode. Got BpChild. grab the remote and wrap it.
Steven Morelande67a6762017-05-16 14:53:28 -0700104 return sp<IChild>(new BpChild(toBinder<IParent>(parent)));
Yifan Hong7f97f442016-11-14 18:31:05 -0800105 }
106 // Passthrough mode. Got BnChild and BsChild.
107 return sp<IChild>(static_cast<IChild *>(parent.get()));
108}
109
Yifan Hong8d498ef2017-03-13 17:47:10 -0700110} // namespace details
111
Yifan Hong7f97f442016-11-14 18:31:05 -0800112} // namespace hardware
113} // namespace android
114
115
116#endif // ANDROID_HIDL_TRANSPORT_SUPPORT_H