HidlTransportSupport.h revision e67a676fdf3f0c565af64e375075e5386e6ab940
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 <android/hidl/base/1.0/IBase.h>
21#include <hidl/HidlBinderSupport.h>
22#include <hidl/HidlSupport.h>
23#include <hidl/HidlTransportUtils.h>
24
25namespace android {
26namespace hardware {
27
28/* 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 */
44void configureRpcThreadpool(size_t maxThreads, bool callerWillJoin);
45
46/* Joins a threadpool that you configured earlier with
47 * configureRpcThreadPool(x, true);
48 */
49void joinRpcThreadpool();
50
51/**
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
65template <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
74namespace details {
75
76// cast the interface IParent to IChild.
77// 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.
84template <typename IChild, typename IParent, typename BpChild>
85Return<sp<IChild>> castInterface(sp<IParent> parent, const char* childIndicator, bool emitError) {
86    if (parent.get() == nullptr) {
87        // casts always succeed with nullptrs.
88        return nullptr;
89    }
90    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    }
97
98    if (!canCastRet) {
99        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.
104        return sp<IChild>(new BpChild(toBinder<IParent>(parent)));
105    }
106    // Passthrough mode. Got BnChild and BsChild.
107    return sp<IChild>(static_cast<IChild *>(parent.get()));
108}
109
110}  // namespace details
111
112}  // namespace hardware
113}  // namespace android
114
115
116#endif  // ANDROID_HIDL_TRANSPORT_SUPPORT_H
117