• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 struct SchedPrio {
88     int sched_policy;
89     int prio;
90 };
91 
92 SchedPrio getMinSchedulerPolicy(const sp<::android::hidl::base::V1_0::IBase>& service);
93 
94 /**
95  * Sets whether or not this object should request security contexts to be populatd for incoming
96  * calls (e.g. with getCallingSid).
97  *
98  * This method MUST be called before passing this service to another process
99  * and/or registering it with registerAsService().
100  *
101  * @param service the service to set the policy for
102  * @param requesting whether or not to request sid (default is false)
103  */
104 bool setRequestingSid(const sp<::android::hidl::base::V1_0::IBase>& service, bool requesting);
105 
106 /**
107  * Says whether or not this service is requesting a SID. If this was set after the service was
108  * sent to another process, then it will not take effect.
109  */
110 bool getRequestingSid(const sp<::android::hidl::base::V1_0::IBase>& service);
111 
112 /**
113  * Returns whether two interfaces represent the same interface. References to interfaces in the same
114  * process will always be equivalent. However, in order to compare a service that is a proxy to a
115  * different process, its underlying structure may have to be checked.
116  */
117 bool interfacesEqual(const sp<::android::hidl::base::V1_0::IBase>& left,
118                      const sp<::android::hidl::base::V1_0::IBase>& right);
119 
120 namespace details {
121 
122 // Return PID on userdebug / eng builds and IServiceManager::PidConstant::NO_PID on user builds.
123 int32_t getPidIfSharable();
124 
125 // cast the interface IParent to IChild.
126 // Return nonnull if cast successful.
127 // Return nullptr if:
128 // 1. parent is null
129 // 2. cast failed because IChild is not a child type of IParent.
130 // 3. !emitError, calling into parent fails.
131 // Return an error Return object if:
132 // 1. emitError, calling into parent fails.
133 template <typename IChild, typename IParent, typename BpChild>
castInterface(sp<IParent> parent,const char * childIndicator,bool emitError)134 Return<sp<IChild>> castInterface(sp<IParent> parent, const char* childIndicator, bool emitError) {
135     if (parent.get() == nullptr) {
136         // casts always succeed with nullptrs.
137         return nullptr;
138     }
139     Return<bool> canCastRet = details::canCastInterface(parent.get(), childIndicator, emitError);
140     if (!canCastRet.isOk()) {
141         // call fails, propagate the error if emitError
142         return emitError
143                 ? details::StatusOf<bool, sp<IChild>>(canCastRet)
144                 : Return<sp<IChild>>(sp<IChild>(nullptr));
145     }
146 
147     if (!canCastRet) {
148         return sp<IChild>(nullptr); // cast failed.
149     }
150     // TODO b/32001926 Needs to be fixed for socket mode.
151     if (parent->isRemote()) {
152         // binderized mode. Got BpChild. grab the remote and wrap it.
153         return sp<IChild>(new BpChild(getOrCreateCachedBinder(parent.get())));
154     }
155     // Passthrough mode. Got BnChild or BsChild.
156     return sp<IChild>(static_cast<IChild *>(parent.get()));
157 }
158 
159 template <typename BpType, typename IType = typename BpType::Pure,
160           typename = std::enable_if_t<std::is_same<i_tag, typename IType::_hidl_tag>::value>,
161           typename = std::enable_if_t<std::is_same<bphw_tag, typename BpType::_hidl_tag>::value>>
getServiceInternal(const std::string & instance,bool retry,bool getStub)162 sp<IType> getServiceInternal(const std::string& instance, bool retry, bool getStub) {
163     using ::android::hidl::base::V1_0::IBase;
164 
165     sp<IBase> base = getRawServiceInternal(IType::descriptor, instance, retry, getStub);
166 
167     if (base == nullptr) {
168         return nullptr;
169     }
170 
171     if (base->isRemote()) {
172         // getRawServiceInternal guarantees we get the proper class
173         return sp<IType>(new BpType(getOrCreateCachedBinder(base.get())));
174     }
175 
176     return IType::castFrom(base);
177 }
178 
179 }  // namespace details
180 
181 }  // namespace hardware
182 }  // namespace android
183 
184 
185 #endif  // ANDROID_HIDL_TRANSPORT_SUPPORT_H
186