blob: 97d185ea1d0c31969af1a70871375ca2374c4e47 [file] [log] [blame]
Yifan Hong7f97f442016-11-14 18:31:05 -08001/*
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_BINDER_SUPPORT_H
18#define ANDROID_HIDL_BINDER_SUPPORT_H
19
Yifan Hong777bef92017-02-01 15:50:36 -080020#include <sys/types.h>
21
22#include <android/hidl/base/1.0/BnHwBase.h>
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010023#include <android/hidl/base/1.0/IBase.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080024#include <hidl/HidlSupport.h>
Martijn Coenen12f04d92016-12-07 17:29:41 +010025#include <hidl/HidlTransportUtils.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080026#include <hidl/MQDescriptor.h>
Yifan Hongb2c9c752016-12-07 15:15:41 -080027#include <hidl/Static.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080028#include <hwbinder/IBinder.h>
Martijn Coenene76c7a22016-11-22 14:53:47 +010029#include <hwbinder/IPCThreadState.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080030#include <hwbinder/Parcel.h>
Martijn Coenene76c7a22016-11-22 14:53:47 +010031#include <hwbinder/ProcessState.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080032// Defines functions for hidl_string, hidl_version, Status, hidl_vec, MQDescriptor,
33// etc. to interact with Parcel.
34
35namespace android {
36namespace hardware {
37
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010038// hidl_binder_death_recipient wraps a transport-independent
39// hidl_death_recipient, and implements the binder-specific
40// DeathRecipient interface.
41struct hidl_binder_death_recipient : IBinder::DeathRecipient {
42 hidl_binder_death_recipient(const sp<hidl_death_recipient> &recipient,
Steven Moreland108d09d2017-05-05 16:15:38 -070043 uint64_t cookie, const sp<::android::hidl::base::V1_0::IBase> &base);
44 virtual void binderDied(const wp<IBinder>& /*who*/);
45 wp<hidl_death_recipient> getRecipient();
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010046private:
47 wp<hidl_death_recipient> mRecipient;
48 uint64_t mCookie;
49 wp<::android::hidl::base::V1_0::IBase> mBase;
50};
51
Nirav Atre564a8d22018-07-26 18:29:12 -070052// ---------------------- hidl_handle
53
54status_t readEmbeddedFromParcel(const hidl_handle &handle,
55 const Parcel &parcel, size_t parentHandle, size_t parentOffset);
56
57status_t writeEmbeddedToParcel(const hidl_handle &handle,
58 Parcel *parcel, size_t parentHandle, size_t parentOffset);
59
Martijn Coenen30791002016-12-01 15:40:46 +010060// ---------------------- hidl_memory
61
Martijn Coenen9d3eb352017-04-18 20:28:03 -070062status_t readEmbeddedFromParcel(const hidl_memory &memory,
Martijn Coenen30791002016-12-01 15:40:46 +010063 const Parcel &parcel, size_t parentHandle, size_t parentOffset);
64
65status_t writeEmbeddedToParcel(const hidl_memory &memory,
66 Parcel *parcel, size_t parentHandle, size_t parentOffset);
67
Yifan Hong7f97f442016-11-14 18:31:05 -080068// ---------------------- hidl_string
69
Martijn Coenen9d3eb352017-04-18 20:28:03 -070070status_t readEmbeddedFromParcel(const hidl_string &string,
Yifan Hong7f97f442016-11-14 18:31:05 -080071 const Parcel &parcel, size_t parentHandle, size_t parentOffset);
72
73status_t writeEmbeddedToParcel(const hidl_string &string,
74 Parcel *parcel, size_t parentHandle, size_t parentOffset);
75
76// ---------------------- hidl_version
77
78status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel);
79
80// Caller is responsible for freeing the returned object.
81hidl_version* readFromParcel(const android::hardware::Parcel& parcel);
82
83// ---------------------- Status
84
85// Bear in mind that if the client or service is a Java endpoint, this
86// is not the logic which will provide/interpret the data here.
87status_t readFromParcel(Status *status, const Parcel& parcel);
88status_t writeToParcel(const Status &status, Parcel* parcel);
89
90// ---------------------- hidl_vec
91
92template<typename T>
93status_t readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -070094 const hidl_vec<T> &vec,
Yifan Hong7f97f442016-11-14 18:31:05 -080095 const Parcel &parcel,
96 size_t parentHandle,
97 size_t parentOffset,
98 size_t *handle) {
Steven Moreland1f535a22017-01-06 19:25:49 -080099 const void *out;
Martijn Coenen6091d182017-01-13 00:18:54 +0100100 return parcel.readNullableEmbeddedBuffer(
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700101 vec.size() * sizeof(T),
Yifan Hong7f97f442016-11-14 18:31:05 -0800102 handle,
103 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -0800104 parentOffset + hidl_vec<T>::kOffsetOfBuffer,
105 &out);
Yifan Hong7f97f442016-11-14 18:31:05 -0800106}
107
108template<typename T>
109status_t writeEmbeddedToParcel(
110 const hidl_vec<T> &vec,
111 Parcel *parcel,
112 size_t parentHandle,
113 size_t parentOffset,
114 size_t *handle) {
115 return parcel->writeEmbeddedBuffer(
116 vec.data(),
117 sizeof(T) * vec.size(),
118 handle,
119 parentHandle,
120 parentOffset + hidl_vec<T>::kOffsetOfBuffer);
121}
122
123template<typename T>
124status_t findInParcel(const hidl_vec<T> &vec, const Parcel &parcel, size_t *handle) {
125 return parcel.quickFindBuffer(vec.data(), handle);
126}
127
128// ---------------------- MQDescriptor
129
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800130template<typename T, MQFlavor flavor>
Yifan Hong7f97f442016-11-14 18:31:05 -0800131::android::status_t readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700132 MQDescriptor<T, flavor> &obj,
Yifan Hong7f97f442016-11-14 18:31:05 -0800133 const ::android::hardware::Parcel &parcel,
134 size_t parentHandle,
135 size_t parentOffset) {
136 ::android::status_t _hidl_err = ::android::OK;
137
138 size_t _hidl_grantors_child;
139
140 _hidl_err = ::android::hardware::readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700141 obj.grantors(),
Yifan Hong7f97f442016-11-14 18:31:05 -0800142 parcel,
143 parentHandle,
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800144 parentOffset + MQDescriptor<T, flavor>::kOffsetOfGrantors,
Yifan Hong7f97f442016-11-14 18:31:05 -0800145 &_hidl_grantors_child);
146
147 if (_hidl_err != ::android::OK) { return _hidl_err; }
148
Steven Moreland1f535a22017-01-06 19:25:49 -0800149 const native_handle_t *_hidl_mq_handle_ptr;
Hridya Valsarajuf6fa2a92017-03-08 15:20:07 -0800150 _hidl_err = parcel.readNullableEmbeddedNativeHandle(
Yifan Hong7f97f442016-11-14 18:31:05 -0800151 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -0800152 parentOffset + MQDescriptor<T, flavor>::kOffsetOfHandle,
153 &_hidl_mq_handle_ptr);
Yifan Hong7f97f442016-11-14 18:31:05 -0800154
Steven Moreland1f535a22017-01-06 19:25:49 -0800155 if (_hidl_err != ::android::OK) { return _hidl_err; }
Yifan Hong7f97f442016-11-14 18:31:05 -0800156
157 return _hidl_err;
158}
159
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800160template<typename T, MQFlavor flavor>
Yifan Hong7f97f442016-11-14 18:31:05 -0800161::android::status_t writeEmbeddedToParcel(
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800162 const MQDescriptor<T, flavor> &obj,
Yifan Hong7f97f442016-11-14 18:31:05 -0800163 ::android::hardware::Parcel *parcel,
164 size_t parentHandle,
165 size_t parentOffset) {
166 ::android::status_t _hidl_err = ::android::OK;
167
168 size_t _hidl_grantors_child;
169
170 _hidl_err = ::android::hardware::writeEmbeddedToParcel(
171 obj.grantors(),
172 parcel,
173 parentHandle,
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800174 parentOffset + MQDescriptor<T, flavor>::kOffsetOfGrantors,
Yifan Hong7f97f442016-11-14 18:31:05 -0800175 &_hidl_grantors_child);
176
177 if (_hidl_err != ::android::OK) { return _hidl_err; }
178
179 _hidl_err = parcel->writeEmbeddedNativeHandle(
180 obj.handle(),
181 parentHandle,
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800182 parentOffset + MQDescriptor<T, flavor>::kOffsetOfHandle);
Yifan Hong7f97f442016-11-14 18:31:05 -0800183
184 if (_hidl_err != ::android::OK) { return _hidl_err; }
185
186 return _hidl_err;
187}
188
189// ---------------------- pointers for HIDL
190
191template <typename T>
192static status_t readEmbeddedReferenceFromParcel(
193 T const* * /* bufptr */,
194 const Parcel & parcel,
195 size_t parentHandle,
196 size_t parentOffset,
197 size_t *handle,
198 bool *shouldResolveRefInBuffer
199 ) {
200 // *bufptr is ignored because, if I am embedded in some
201 // other buffer, the kernel should have fixed me up already.
202 bool isPreviouslyWritten;
203 status_t result = parcel.readEmbeddedReference(
204 nullptr, // ignored, not written to bufptr.
205 handle,
206 parentHandle,
207 parentOffset,
208 &isPreviouslyWritten);
209 // tell caller to run T::readEmbeddedToParcel and
210 // T::readEmbeddedReferenceToParcel if necessary.
211 // It is not called here because we don't know if these two are valid methods.
212 *shouldResolveRefInBuffer = !isPreviouslyWritten;
213 return result;
214}
215
216template <typename T>
217static status_t writeEmbeddedReferenceToParcel(
218 T const* buf,
219 Parcel *parcel, size_t parentHandle, size_t parentOffset,
220 size_t *handle,
221 bool *shouldResolveRefInBuffer
222 ) {
223
224 if(buf == nullptr) {
225 *shouldResolveRefInBuffer = false;
226 return parcel->writeEmbeddedNullReference(handle, parentHandle, parentOffset);
227 }
228
229 // find whether the buffer exists
230 size_t childHandle, childOffset;
231 status_t result;
232 bool found;
233
234 result = parcel->findBuffer(buf, sizeof(T), &found, &childHandle, &childOffset);
235
236 // tell caller to run T::writeEmbeddedToParcel and
237 // T::writeEmbeddedReferenceToParcel if necessary.
238 // It is not called here because we don't know if these two are valid methods.
239 *shouldResolveRefInBuffer = !found;
240
241 if(result != OK) {
242 return result; // bad pointers and length given
243 }
244 if(!found) { // did not find it.
245 return parcel->writeEmbeddedBuffer(buf, sizeof(T), handle,
246 parentHandle, parentOffset);
247 }
248 // found the buffer. easy case.
249 return parcel->writeEmbeddedReference(
250 handle,
251 childHandle,
252 childOffset,
253 parentHandle,
254 parentOffset);
255}
256
257template <typename T>
258static status_t readReferenceFromParcel(
259 T const* *bufptr,
260 const Parcel & parcel,
261 size_t *handle,
262 bool *shouldResolveRefInBuffer
263 ) {
264 bool isPreviouslyWritten;
265 status_t result = parcel.readReference(reinterpret_cast<void const* *>(bufptr),
266 handle, &isPreviouslyWritten);
267 // tell caller to run T::readEmbeddedToParcel and
268 // T::readEmbeddedReferenceToParcel if necessary.
269 // It is not called here because we don't know if these two are valid methods.
270 *shouldResolveRefInBuffer = !isPreviouslyWritten;
271 return result;
272}
273
274template <typename T>
275static status_t writeReferenceToParcel(
276 T const *buf,
277 Parcel * parcel,
278 size_t *handle,
279 bool *shouldResolveRefInBuffer
280 ) {
281
282 if(buf == nullptr) {
283 *shouldResolveRefInBuffer = false;
284 return parcel->writeNullReference(handle);
285 }
286
287 // find whether the buffer exists
288 size_t childHandle, childOffset;
289 status_t result;
290 bool found;
291
292 result = parcel->findBuffer(buf, sizeof(T), &found, &childHandle, &childOffset);
293
294 // tell caller to run T::writeEmbeddedToParcel and
295 // T::writeEmbeddedReferenceToParcel if necessary.
296 // It is not called here because we don't know if these two are valid methods.
297 *shouldResolveRefInBuffer = !found;
298
299 if(result != OK) {
300 return result; // bad pointers and length given
301 }
302 if(!found) { // did not find it.
303 return parcel->writeBuffer(buf, sizeof(T), handle);
304 }
305 // found the buffer. easy case.
306 return parcel->writeReference(handle,
307 childHandle, childOffset);
308}
309
310// ---------------------- support for casting interfaces
311
Steven Moreland9cbef742018-06-26 16:23:50 -0700312// Constructs a binder for this interface and caches it. If it has already been created
313// then it returns it. ifacePtr must be a local object.
314sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr);
315
Yifan Hong7f97f442016-11-14 18:31:05 -0800316// Construct a smallest possible binder from the given interface.
317// If it is remote, then its remote() will be retrieved.
318// Otherwise, the smallest possible BnChild is found where IChild is a subclass of IType
319// and iface is of class IChild. BnChild will be used to wrapped the given iface.
320// Return nullptr if iface is null or any failure.
Steven Moreland5f82d3e2017-07-27 13:57:56 -0700321template <typename IType,
322 typename = std::enable_if_t<std::is_same<details::i_tag, typename IType::_hidl_tag>::value>>
Yifan Hong7f97f442016-11-14 18:31:05 -0800323sp<IBinder> toBinder(sp<IType> iface) {
324 IType *ifacePtr = iface.get();
325 if (ifacePtr == nullptr) {
326 return nullptr;
327 }
328 if (ifacePtr->isRemote()) {
Steven Moreland2932c132017-07-21 19:29:54 +0000329 return ::android::hardware::IInterface::asBinder(
330 static_cast<BpInterface<IType>*>(ifacePtr));
Yifan Hong7f97f442016-11-14 18:31:05 -0800331 } else {
Steven Moreland9cbef742018-06-26 16:23:50 -0700332 return getOrCreateCachedBinder(ifacePtr);
Yifan Hong7f97f442016-11-14 18:31:05 -0800333 }
334}
335
Martijn Coenen12f04d92016-12-07 17:29:41 +0100336template <typename IType, typename ProxyType, typename StubType>
337sp<IType> fromBinder(const sp<IBinder>& binderIface) {
338 using ::android::hidl::base::V1_0::IBase;
Yifan Hong4e925992017-01-09 17:47:17 -0800339 using ::android::hidl::base::V1_0::BnHwBase;
Martijn Coenen12f04d92016-12-07 17:29:41 +0100340
341 if (binderIface.get() == nullptr) {
342 return nullptr;
343 }
344 if (binderIface->localBinder() == nullptr) {
345 return new ProxyType(binderIface);
346 }
Yifan Hong4e925992017-01-09 17:47:17 -0800347 sp<IBase> base = static_cast<BnHwBase*>(binderIface.get())->getImpl();
Yifan Hong2ac326f2017-02-22 18:13:06 -0800348 if (details::canCastInterface(base.get(), IType::descriptor)) {
Martijn Coenen12f04d92016-12-07 17:29:41 +0100349 StubType* stub = static_cast<StubType*>(binderIface.get());
350 return stub->getImpl();
351 } else {
352 return nullptr;
353 }
354}
355
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800356void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin);
357void joinBinderRpcThreadpool();
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800358int setupBinderPolling();
359status_t handleBinderPoll();
Martijn Coenene76c7a22016-11-22 14:53:47 +0100360
Steven Morelandd40af8e2018-05-01 16:33:21 -0700361void addPostCommandTask(const std::function<void(void)> task);
362
Yifan Hong7f97f442016-11-14 18:31:05 -0800363} // namespace hardware
364} // namespace android
365
366
367#endif // ANDROID_HIDL_BINDER_SUPPORT_H