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
25 namespace android {
26 namespace 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 */
44 void configureRpcThreadpool(size_t maxThreads, bool callerWillJoin);
45
46 /* Joins a threadpool that you configured earlier with
47 * configureRpcThreadPool(x, true);
48 */
49 void 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 */
62 bool setMinSchedulerPolicy(const sp<::android::hidl::base::V1_0::IBase>& service,
63 int policy, int priority);
64
65 namespace details {
66
67 // cast the interface IParent to IChild.
68 // Return nonnull if cast successful.
69 // Return nullptr if:
70 // 1. parent is null
71 // 2. cast failed because IChild is not a child type of IParent.
72 // 3. !emitError, calling into parent fails.
73 // Return an error Return object if:
74 // 1. emitError, calling into parent fails.
75 template<typename IChild, typename IParent, typename BpChild, typename BpParent>
castInterface(sp<IParent> parent,const char * childIndicator,bool emitError)76 Return<sp<IChild>> castInterface(sp<IParent> parent, const char *childIndicator, bool emitError) {
77 if (parent.get() == nullptr) {
78 // casts always succeed with nullptrs.
79 return nullptr;
80 }
81 Return<bool> canCastRet = details::canCastInterface(parent.get(), childIndicator, emitError);
82 if (!canCastRet.isOk()) {
83 // call fails, propagate the error if emitError
84 return emitError
85 ? details::StatusOf<bool, sp<IChild>>(canCastRet)
86 : Return<sp<IChild>>(sp<IChild>(nullptr));
87 }
88
89 if (!canCastRet) {
90 return sp<IChild>(nullptr); // cast failed.
91 }
92 // TODO b/32001926 Needs to be fixed for socket mode.
93 if (parent->isRemote()) {
94 // binderized mode. Got BpChild. grab the remote and wrap it.
95 return sp<IChild>(new BpChild(toBinder<IParent, BpParent>(parent)));
96 }
97 // Passthrough mode. Got BnChild and BsChild.
98 return sp<IChild>(static_cast<IChild *>(parent.get()));
99 }
100
101 } // namespace details
102
103 } // namespace hardware
104 } // namespace android
105
106
107 #endif // ANDROID_HIDL_TRANSPORT_SUPPORT_H
108