HidlTransportSupport.h revision 11a732a9acc36245ccfe99f99a755b3e5db125a3
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 */
43void configureRpcThreadpool(size_t maxThreads, bool callerWillJoin);
44
45/* Joins a threadpool that you configured earlier with
46 * configureRpcThreadPool(x, true);
47 */
48void joinRpcThreadpool();
49
50// cast the interface IParent to IChild.
51// Return nullptr if parent is null or any failure.
52template<typename IChild, typename IParent, typename BpChild, typename BpParent>
53sp<IChild> castInterface(sp<IParent> parent, const char *childIndicator) {
54    if (parent.get() == nullptr) {
55        // casts always succeed with nullptrs.
56        return nullptr;
57    }
58    bool canCast = details::canCastInterface(parent.get(), childIndicator);
59
60    if (!canCast) {
61        return sp<IChild>(nullptr); // cast failed.
62    }
63    // TODO b/32001926 Needs to be fixed for socket mode.
64    if (parent->isRemote()) {
65        // binderized mode. Got BpChild. grab the remote and wrap it.
66        return sp<IChild>(new BpChild(toBinder<IParent, BpParent>(parent)));
67    }
68    // Passthrough mode. Got BnChild and BsChild.
69    return sp<IChild>(static_cast<IChild *>(parent.get()));
70}
71
72}  // namespace hardware
73}  // namespace android
74
75
76#endif  // ANDROID_HIDL_TRANSPORT_SUPPORT_H
77