blob: a82f977d3cd78b840403744eb194b09ac8838bc3 [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,
43 uint64_t cookie, const sp<::android::hidl::base::V1_0::IBase> &base) :
44 mRecipient(recipient), mCookie(cookie), mBase(base) {
45 }
46 virtual void binderDied(const wp<IBinder>& /*who*/) {
47 sp<hidl_death_recipient> recipient = mRecipient.promote();
48 if (recipient != nullptr) {
49 recipient->serviceDied(mCookie, mBase);
50 }
51 }
52 wp<hidl_death_recipient> getRecipient() {
53 return mRecipient;
54 }
55private:
56 wp<hidl_death_recipient> mRecipient;
57 uint64_t mCookie;
58 wp<::android::hidl::base::V1_0::IBase> mBase;
59};
60
Martijn Coenen30791002016-12-01 15:40:46 +010061// ---------------------- hidl_memory
62
Martijn Coenen9d3eb352017-04-18 20:28:03 -070063status_t readEmbeddedFromParcel(const hidl_memory &memory,
Martijn Coenen30791002016-12-01 15:40:46 +010064 const Parcel &parcel, size_t parentHandle, size_t parentOffset);
65
66status_t writeEmbeddedToParcel(const hidl_memory &memory,
67 Parcel *parcel, size_t parentHandle, size_t parentOffset);
68
Yifan Hong7f97f442016-11-14 18:31:05 -080069// ---------------------- hidl_string
70
Martijn Coenen9d3eb352017-04-18 20:28:03 -070071status_t readEmbeddedFromParcel(const hidl_string &string,
Yifan Hong7f97f442016-11-14 18:31:05 -080072 const Parcel &parcel, size_t parentHandle, size_t parentOffset);
73
74status_t writeEmbeddedToParcel(const hidl_string &string,
75 Parcel *parcel, size_t parentHandle, size_t parentOffset);
76
77// ---------------------- hidl_version
78
79status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel);
80
81// Caller is responsible for freeing the returned object.
82hidl_version* readFromParcel(const android::hardware::Parcel& parcel);
83
84// ---------------------- Status
85
86// Bear in mind that if the client or service is a Java endpoint, this
87// is not the logic which will provide/interpret the data here.
88status_t readFromParcel(Status *status, const Parcel& parcel);
89status_t writeToParcel(const Status &status, Parcel* parcel);
90
91// ---------------------- hidl_vec
92
93template<typename T>
94status_t readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -070095 const hidl_vec<T> &vec,
Yifan Hong7f97f442016-11-14 18:31:05 -080096 const Parcel &parcel,
97 size_t parentHandle,
98 size_t parentOffset,
99 size_t *handle) {
Steven Moreland1f535a22017-01-06 19:25:49 -0800100 const void *out;
Martijn Coenen6091d182017-01-13 00:18:54 +0100101 return parcel.readNullableEmbeddedBuffer(
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700102 vec.size() * sizeof(T),
Yifan Hong7f97f442016-11-14 18:31:05 -0800103 handle,
104 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -0800105 parentOffset + hidl_vec<T>::kOffsetOfBuffer,
106 &out);
Yifan Hong7f97f442016-11-14 18:31:05 -0800107}
108
109template<typename T>
110status_t writeEmbeddedToParcel(
111 const hidl_vec<T> &vec,
112 Parcel *parcel,
113 size_t parentHandle,
114 size_t parentOffset,
115 size_t *handle) {
116 return parcel->writeEmbeddedBuffer(
117 vec.data(),
118 sizeof(T) * vec.size(),
119 handle,
120 parentHandle,
121 parentOffset + hidl_vec<T>::kOffsetOfBuffer);
122}
123
124template<typename T>
125status_t findInParcel(const hidl_vec<T> &vec, const Parcel &parcel, size_t *handle) {
126 return parcel.quickFindBuffer(vec.data(), handle);
127}
128
129// ---------------------- MQDescriptor
130
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800131template<typename T, MQFlavor flavor>
Yifan Hong7f97f442016-11-14 18:31:05 -0800132::android::status_t readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700133 MQDescriptor<T, flavor> &obj,
Yifan Hong7f97f442016-11-14 18:31:05 -0800134 const ::android::hardware::Parcel &parcel,
135 size_t parentHandle,
136 size_t parentOffset) {
137 ::android::status_t _hidl_err = ::android::OK;
138
139 size_t _hidl_grantors_child;
140
141 _hidl_err = ::android::hardware::readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700142 obj.grantors(),
Yifan Hong7f97f442016-11-14 18:31:05 -0800143 parcel,
144 parentHandle,
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800145 parentOffset + MQDescriptor<T, flavor>::kOffsetOfGrantors,
Yifan Hong7f97f442016-11-14 18:31:05 -0800146 &_hidl_grantors_child);
147
148 if (_hidl_err != ::android::OK) { return _hidl_err; }
149
Steven Moreland1f535a22017-01-06 19:25:49 -0800150 const native_handle_t *_hidl_mq_handle_ptr;
Hridya Valsarajuf6fa2a92017-03-08 15:20:07 -0800151 _hidl_err = parcel.readNullableEmbeddedNativeHandle(
Yifan Hong7f97f442016-11-14 18:31:05 -0800152 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -0800153 parentOffset + MQDescriptor<T, flavor>::kOffsetOfHandle,
154 &_hidl_mq_handle_ptr);
Yifan Hong7f97f442016-11-14 18:31:05 -0800155
Steven Moreland1f535a22017-01-06 19:25:49 -0800156 if (_hidl_err != ::android::OK) { return _hidl_err; }
Yifan Hong7f97f442016-11-14 18:31:05 -0800157
158 return _hidl_err;
159}
160
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800161template<typename T, MQFlavor flavor>
Yifan Hong7f97f442016-11-14 18:31:05 -0800162::android::status_t writeEmbeddedToParcel(
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800163 const MQDescriptor<T, flavor> &obj,
Yifan Hong7f97f442016-11-14 18:31:05 -0800164 ::android::hardware::Parcel *parcel,
165 size_t parentHandle,
166 size_t parentOffset) {
167 ::android::status_t _hidl_err = ::android::OK;
168
169 size_t _hidl_grantors_child;
170
171 _hidl_err = ::android::hardware::writeEmbeddedToParcel(
172 obj.grantors(),
173 parcel,
174 parentHandle,
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800175 parentOffset + MQDescriptor<T, flavor>::kOffsetOfGrantors,
Yifan Hong7f97f442016-11-14 18:31:05 -0800176 &_hidl_grantors_child);
177
178 if (_hidl_err != ::android::OK) { return _hidl_err; }
179
180 _hidl_err = parcel->writeEmbeddedNativeHandle(
181 obj.handle(),
182 parentHandle,
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800183 parentOffset + MQDescriptor<T, flavor>::kOffsetOfHandle);
Yifan Hong7f97f442016-11-14 18:31:05 -0800184
185 if (_hidl_err != ::android::OK) { return _hidl_err; }
186
187 return _hidl_err;
188}
189
190// ---------------------- pointers for HIDL
191
192template <typename T>
193static status_t readEmbeddedReferenceFromParcel(
194 T const* * /* bufptr */,
195 const Parcel & parcel,
196 size_t parentHandle,
197 size_t parentOffset,
198 size_t *handle,
199 bool *shouldResolveRefInBuffer
200 ) {
201 // *bufptr is ignored because, if I am embedded in some
202 // other buffer, the kernel should have fixed me up already.
203 bool isPreviouslyWritten;
204 status_t result = parcel.readEmbeddedReference(
205 nullptr, // ignored, not written to bufptr.
206 handle,
207 parentHandle,
208 parentOffset,
209 &isPreviouslyWritten);
210 // tell caller to run T::readEmbeddedToParcel and
211 // T::readEmbeddedReferenceToParcel if necessary.
212 // It is not called here because we don't know if these two are valid methods.
213 *shouldResolveRefInBuffer = !isPreviouslyWritten;
214 return result;
215}
216
217template <typename T>
218static status_t writeEmbeddedReferenceToParcel(
219 T const* buf,
220 Parcel *parcel, size_t parentHandle, size_t parentOffset,
221 size_t *handle,
222 bool *shouldResolveRefInBuffer
223 ) {
224
225 if(buf == nullptr) {
226 *shouldResolveRefInBuffer = false;
227 return parcel->writeEmbeddedNullReference(handle, parentHandle, parentOffset);
228 }
229
230 // find whether the buffer exists
231 size_t childHandle, childOffset;
232 status_t result;
233 bool found;
234
235 result = parcel->findBuffer(buf, sizeof(T), &found, &childHandle, &childOffset);
236
237 // tell caller to run T::writeEmbeddedToParcel and
238 // T::writeEmbeddedReferenceToParcel if necessary.
239 // It is not called here because we don't know if these two are valid methods.
240 *shouldResolveRefInBuffer = !found;
241
242 if(result != OK) {
243 return result; // bad pointers and length given
244 }
245 if(!found) { // did not find it.
246 return parcel->writeEmbeddedBuffer(buf, sizeof(T), handle,
247 parentHandle, parentOffset);
248 }
249 // found the buffer. easy case.
250 return parcel->writeEmbeddedReference(
251 handle,
252 childHandle,
253 childOffset,
254 parentHandle,
255 parentOffset);
256}
257
258template <typename T>
259static status_t readReferenceFromParcel(
260 T const* *bufptr,
261 const Parcel & parcel,
262 size_t *handle,
263 bool *shouldResolveRefInBuffer
264 ) {
265 bool isPreviouslyWritten;
266 status_t result = parcel.readReference(reinterpret_cast<void const* *>(bufptr),
267 handle, &isPreviouslyWritten);
268 // tell caller to run T::readEmbeddedToParcel and
269 // T::readEmbeddedReferenceToParcel if necessary.
270 // It is not called here because we don't know if these two are valid methods.
271 *shouldResolveRefInBuffer = !isPreviouslyWritten;
272 return result;
273}
274
275template <typename T>
276static status_t writeReferenceToParcel(
277 T const *buf,
278 Parcel * parcel,
279 size_t *handle,
280 bool *shouldResolveRefInBuffer
281 ) {
282
283 if(buf == nullptr) {
284 *shouldResolveRefInBuffer = false;
285 return parcel->writeNullReference(handle);
286 }
287
288 // find whether the buffer exists
289 size_t childHandle, childOffset;
290 status_t result;
291 bool found;
292
293 result = parcel->findBuffer(buf, sizeof(T), &found, &childHandle, &childOffset);
294
295 // tell caller to run T::writeEmbeddedToParcel and
296 // T::writeEmbeddedReferenceToParcel if necessary.
297 // It is not called here because we don't know if these two are valid methods.
298 *shouldResolveRefInBuffer = !found;
299
300 if(result != OK) {
301 return result; // bad pointers and length given
302 }
303 if(!found) { // did not find it.
304 return parcel->writeBuffer(buf, sizeof(T), handle);
305 }
306 // found the buffer. easy case.
307 return parcel->writeReference(handle,
308 childHandle, childOffset);
309}
310
311// ---------------------- support for casting interfaces
312
Yifan Hong7f97f442016-11-14 18:31:05 -0800313// Construct a smallest possible binder from the given interface.
314// If it is remote, then its remote() will be retrieved.
315// Otherwise, the smallest possible BnChild is found where IChild is a subclass of IType
316// and iface is of class IChild. BnChild will be used to wrapped the given iface.
317// Return nullptr if iface is null or any failure.
Martijn Coenen12f04d92016-12-07 17:29:41 +0100318template <typename IType, typename ProxyType>
Yifan Hong7f97f442016-11-14 18:31:05 -0800319sp<IBinder> toBinder(sp<IType> iface) {
320 IType *ifacePtr = iface.get();
321 if (ifacePtr == nullptr) {
322 return nullptr;
323 }
324 if (ifacePtr->isRemote()) {
Martijn Coenen12f04d92016-12-07 17:29:41 +0100325 return ::android::hardware::IInterface::asBinder(static_cast<ProxyType *>(ifacePtr));
Yifan Hong7f97f442016-11-14 18:31:05 -0800326 } else {
Yifan Hong2ac326f2017-02-22 18:13:06 -0800327 std::string myDescriptor = details::getDescriptor(ifacePtr);
Yifan Hong7f97f442016-11-14 18:31:05 -0800328 if (myDescriptor.empty()) {
Yifan Hong2ac326f2017-02-22 18:13:06 -0800329 // interfaceDescriptor fails
Yifan Hong7f97f442016-11-14 18:31:05 -0800330 return nullptr;
331 }
Yifan Hong953e6b02017-03-16 14:52:40 -0700332 auto func = details::gBnConstructorMap.get(myDescriptor, nullptr);
Yifan Hong84888d32017-02-06 15:32:41 -0800333 if (!func) {
Yifan Hong7f97f442016-11-14 18:31:05 -0800334 return nullptr;
335 }
Yifan Hong08607dc2017-04-20 18:10:50 -0700336 return sp<IBinder>(func(static_cast<void *>(ifacePtr)));
Yifan Hong7f97f442016-11-14 18:31:05 -0800337 }
338}
339
Martijn Coenen12f04d92016-12-07 17:29:41 +0100340template <typename IType, typename ProxyType, typename StubType>
341sp<IType> fromBinder(const sp<IBinder>& binderIface) {
342 using ::android::hidl::base::V1_0::IBase;
Yifan Hong4e925992017-01-09 17:47:17 -0800343 using ::android::hidl::base::V1_0::BnHwBase;
Martijn Coenen12f04d92016-12-07 17:29:41 +0100344
345 if (binderIface.get() == nullptr) {
346 return nullptr;
347 }
348 if (binderIface->localBinder() == nullptr) {
349 return new ProxyType(binderIface);
350 }
Yifan Hong4e925992017-01-09 17:47:17 -0800351 sp<IBase> base = static_cast<BnHwBase*>(binderIface.get())->getImpl();
Yifan Hong2ac326f2017-02-22 18:13:06 -0800352 if (details::canCastInterface(base.get(), IType::descriptor)) {
Martijn Coenen12f04d92016-12-07 17:29:41 +0100353 StubType* stub = static_cast<StubType*>(binderIface.get());
354 return stub->getImpl();
355 } else {
356 return nullptr;
357 }
358}
359
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800360void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin);
361void joinBinderRpcThreadpool();
Martijn Coenene76c7a22016-11-22 14:53:47 +0100362
Yifan Hong7f97f442016-11-14 18:31:05 -0800363} // namespace hardware
364} // namespace android
365
366
367#endif // ANDROID_HIDL_BINDER_SUPPORT_H