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 template <typename ILeft, typename IRight>
interfacesEqual(sp<ILeft> left,sp<IRight> right)66 bool 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
74 /**
75 * Sets whether or not this object should request security contexts to be populatd for incoming
76 * calls (e.g. with getCallingSid).
77 *
78 * This method MUST be called before passing this service to another process
79 * and/or registering it with registerAsService().
80 *
81 * @param service the service to set the policy for
82 * @param requesting whether or not to request sid (default is false)
83 */
84 bool setRequestingSid(const sp<::android::hidl::base::V1_0::IBase>& service, bool requesting);
85
86 namespace details {
87
88 // cast the interface IParent to IChild.
89 // Return nonnull if cast successful.
90 // Return nullptr if:
91 // 1. parent is null
92 // 2. cast failed because IChild is not a child type of IParent.
93 // 3. !emitError, calling into parent fails.
94 // Return an error Return object if:
95 // 1. emitError, calling into parent fails.
96 template <typename IChild, typename IParent, typename BpChild>
castInterface(sp<IParent> parent,const char * childIndicator,bool emitError)97 Return<sp<IChild>> castInterface(sp<IParent> parent, const char* childIndicator, bool emitError) {
98 if (parent.get() == nullptr) {
99 // casts always succeed with nullptrs.
100 return nullptr;
101 }
102 Return<bool> canCastRet = details::canCastInterface(parent.get(), childIndicator, emitError);
103 if (!canCastRet.isOk()) {
104 // call fails, propagate the error if emitError
105 return emitError
106 ? details::StatusOf<bool, sp<IChild>>(canCastRet)
107 : Return<sp<IChild>>(sp<IChild>(nullptr));
108 }
109
110 if (!canCastRet) {
111 return sp<IChild>(nullptr); // cast failed.
112 }
113 // TODO b/32001926 Needs to be fixed for socket mode.
114 if (parent->isRemote()) {
115 // binderized mode. Got BpChild. grab the remote and wrap it.
116 return sp<IChild>(new BpChild(toBinder<IParent>(parent)));
117 }
118 // Passthrough mode. Got BnChild and BsChild.
119 return sp<IChild>(static_cast<IChild *>(parent.get()));
120 }
121
122 } // namespace details
123
124 } // namespace hardware
125 } // namespace android
126
127
128 #endif // ANDROID_HIDL_TRANSPORT_SUPPORT_H
129