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/HidlPassthroughSupport.h>
23 #include <hidl/HidlSupport.h>
24 #include <hidl/HidlTransportUtils.h>
25 #include <hidl/ServiceManagement.h>
26
27 namespace android {
28 namespace hardware {
29
30 /* Configures the threadpool used for handling incoming RPC calls in this process.
31 *
32 * This method MUST be called before interacting with any HIDL interfaces,
33 * including the IFoo::getService and IFoo::registerAsService methods.
34 *
35 * @param maxThreads maximum number of threads in this process
36 * @param callerWillJoin whether the caller will join the threadpool later.
37 *
38 * Note that maxThreads must include the caller thread if callerWillJoin is true;
39 *
40 * If you want to create a threadpool of 5 threads, without the caller ever joining:
41 * configureRpcThreadPool(5, false);
42 * If you want to create a threadpool of 1 thread, with the caller joining:
43 * configureRpcThreadPool(1, true); // transport won't launch any threads by itself
44 *
45 */
46 void configureRpcThreadpool(size_t maxThreads, bool callerWillJoin);
47
48 /* Joins a threadpool that you configured earlier with
49 * configureRpcThreadPool(x, true);
50 */
51 void joinRpcThreadpool();
52
53 /**
54 * Sets up the transport for use with (e)poll.
55 *
56 * Note that all currently supported transports can only be polled
57 * from a single thread. When poll() on the returned fd returns,
58 * the caller must call handleTransportPoll() to handle the result.
59 *
60 * @return the file descriptor to be used with (e)poll, or -1 in case of error.
61 */
62 int setupTransportPolling();
63
64 /**
65 * Handles transport work after poll() returns.
66 *
67 * @param fd returned from setupTransportPolling()
68 *
69 * @return OK when successful
70 */
71 status_t handleTransportPoll(int fd);
72
73 /**
74 * Sets a minimum scheduler policy for all transactions coming into this
75 * service.
76 *
77 * This method MUST be called before passing this service to another process
78 * and/or registering it with registerAsService().
79 *
80 * @param service the service to set the policy for
81 * @param policy scheduler policy as defined in linux UAPI
82 * @param priority priority. [-20..19] for SCHED_NORMAL, [1..99] for RT
83 */
84 bool setMinSchedulerPolicy(const sp<::android::hidl::base::V1_0::IBase>& service,
85 int policy, int priority);
86
87 /**
88 * Sets whether or not this object should request security contexts to be populatd for incoming
89 * calls (e.g. with getCallingSid).
90 *
91 * This method MUST be called before passing this service to another process
92 * and/or registering it with registerAsService().
93 *
94 * @param service the service to set the policy for
95 * @param requesting whether or not to request sid (default is false)
96 */
97 bool setRequestingSid(const sp<::android::hidl::base::V1_0::IBase>& service, bool requesting);
98
99 /**
100 * Returns whether two interfaces represent the same interface. References to interfaces in the same
101 * process will always be equivalent. However, in order to compare a service that is a proxy to a
102 * different process, its underlying structure may have to be checked.
103 */
104 bool interfacesEqual(const sp<::android::hidl::base::V1_0::IBase>& left,
105 const sp<::android::hidl::base::V1_0::IBase>& right);
106
107 namespace details {
108
109 // Return PID on userdebug / eng builds and IServiceManager::PidConstant::NO_PID on user builds.
110 int32_t getPidIfSharable();
111
112 // cast the interface IParent to IChild.
113 // Return nonnull if cast successful.
114 // Return nullptr if:
115 // 1. parent is null
116 // 2. cast failed because IChild is not a child type of IParent.
117 // 3. !emitError, calling into parent fails.
118 // Return an error Return object if:
119 // 1. emitError, calling into parent fails.
120 template <typename IChild, typename IParent, typename BpChild>
castInterface(sp<IParent> parent,const char * childIndicator,bool emitError)121 Return<sp<IChild>> castInterface(sp<IParent> parent, const char* childIndicator, bool emitError) {
122 if (parent.get() == nullptr) {
123 // casts always succeed with nullptrs.
124 return nullptr;
125 }
126 Return<bool> canCastRet = details::canCastInterface(parent.get(), childIndicator, emitError);
127 if (!canCastRet.isOk()) {
128 // call fails, propagate the error if emitError
129 return emitError
130 ? details::StatusOf<bool, sp<IChild>>(canCastRet)
131 : Return<sp<IChild>>(sp<IChild>(nullptr));
132 }
133
134 if (!canCastRet) {
135 return sp<IChild>(nullptr); // cast failed.
136 }
137 // TODO b/32001926 Needs to be fixed for socket mode.
138 if (parent->isRemote()) {
139 // binderized mode. Got BpChild. grab the remote and wrap it.
140 return sp<IChild>(new BpChild(getOrCreateCachedBinder(parent.get())));
141 }
142 // Passthrough mode. Got BnChild or BsChild.
143 return sp<IChild>(static_cast<IChild *>(parent.get()));
144 }
145
146 template <typename BpType, typename IType = typename BpType::Pure,
147 typename = std::enable_if_t<std::is_same<i_tag, typename IType::_hidl_tag>::value>,
148 typename = std::enable_if_t<std::is_same<bphw_tag, typename BpType::_hidl_tag>::value>>
getServiceInternal(const std::string & instance,bool retry,bool getStub)149 sp<IType> getServiceInternal(const std::string& instance, bool retry, bool getStub) {
150 using ::android::hidl::base::V1_0::IBase;
151
152 sp<IBase> base = getRawServiceInternal(IType::descriptor, instance, retry, getStub);
153
154 if (base == nullptr) {
155 return nullptr;
156 }
157
158 if (base->isRemote()) {
159 // getRawServiceInternal guarantees we get the proper class
160 return sp<IType>(new BpType(getOrCreateCachedBinder(base.get())));
161 }
162
163 return IType::castFrom(base);
164 }
165
166 } // namespace details
167
168 } // namespace hardware
169 } // namespace android
170
171
172 #endif // ANDROID_HIDL_TRANSPORT_SUPPORT_H
173