blob: 44a56b4fe2d75c3bcd6695ac02552f67d21af7bf [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>
27#include <hwbinder/IBinder.h>
28#include <hwbinder/Parcel.h>
Steven Morelandb9173182018-08-08 20:06:08 +000029#include <log/log.h> // TODO(b/65843592): remove. Too many users depending on this transitively.
30
Steven Morelande7838342019-05-30 11:29:16 -070031// Defines functions for hidl_string, Status, hidl_vec, MQDescriptor,
Yifan Hong7f97f442016-11-14 18:31:05 -080032// etc. to interact with Parcel.
33
34namespace android {
35namespace hardware {
36
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010037// hidl_binder_death_recipient wraps a transport-independent
38// hidl_death_recipient, and implements the binder-specific
39// DeathRecipient interface.
40struct hidl_binder_death_recipient : IBinder::DeathRecipient {
41 hidl_binder_death_recipient(const sp<hidl_death_recipient> &recipient,
Steven Moreland108d09d2017-05-05 16:15:38 -070042 uint64_t cookie, const sp<::android::hidl::base::V1_0::IBase> &base);
43 virtual void binderDied(const wp<IBinder>& /*who*/);
44 wp<hidl_death_recipient> getRecipient();
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010045private:
46 wp<hidl_death_recipient> mRecipient;
47 uint64_t mCookie;
48 wp<::android::hidl::base::V1_0::IBase> mBase;
49};
50
Nirav Atre564a8d22018-07-26 18:29:12 -070051// ---------------------- hidl_handle
52
53status_t readEmbeddedFromParcel(const hidl_handle &handle,
54 const Parcel &parcel, size_t parentHandle, size_t parentOffset);
55
56status_t writeEmbeddedToParcel(const hidl_handle &handle,
57 Parcel *parcel, size_t parentHandle, size_t parentOffset);
58
Martijn Coenen30791002016-12-01 15:40:46 +010059// ---------------------- hidl_memory
60
Martijn Coenen9d3eb352017-04-18 20:28:03 -070061status_t readEmbeddedFromParcel(const hidl_memory &memory,
Martijn Coenen30791002016-12-01 15:40:46 +010062 const Parcel &parcel, size_t parentHandle, size_t parentOffset);
63
64status_t writeEmbeddedToParcel(const hidl_memory &memory,
65 Parcel *parcel, size_t parentHandle, size_t parentOffset);
66
Yifan Hong7f97f442016-11-14 18:31:05 -080067// ---------------------- hidl_string
68
Martijn Coenen9d3eb352017-04-18 20:28:03 -070069status_t readEmbeddedFromParcel(const hidl_string &string,
Yifan Hong7f97f442016-11-14 18:31:05 -080070 const Parcel &parcel, size_t parentHandle, size_t parentOffset);
71
72status_t writeEmbeddedToParcel(const hidl_string &string,
73 Parcel *parcel, size_t parentHandle, size_t parentOffset);
74
Yifan Hong7f97f442016-11-14 18:31:05 -080075// ---------------------- Status
76
77// Bear in mind that if the client or service is a Java endpoint, this
78// is not the logic which will provide/interpret the data here.
79status_t readFromParcel(Status *status, const Parcel& parcel);
80status_t writeToParcel(const Status &status, Parcel* parcel);
81
82// ---------------------- hidl_vec
83
84template<typename T>
85status_t readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -070086 const hidl_vec<T> &vec,
Yifan Hong7f97f442016-11-14 18:31:05 -080087 const Parcel &parcel,
88 size_t parentHandle,
89 size_t parentOffset,
90 size_t *handle) {
Steven Moreland1f535a22017-01-06 19:25:49 -080091 const void *out;
Martijn Coenen6091d182017-01-13 00:18:54 +010092 return parcel.readNullableEmbeddedBuffer(
Martijn Coenen9d3eb352017-04-18 20:28:03 -070093 vec.size() * sizeof(T),
Yifan Hong7f97f442016-11-14 18:31:05 -080094 handle,
95 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080096 parentOffset + hidl_vec<T>::kOffsetOfBuffer,
97 &out);
Yifan Hong7f97f442016-11-14 18:31:05 -080098}
99
100template<typename T>
101status_t writeEmbeddedToParcel(
102 const hidl_vec<T> &vec,
103 Parcel *parcel,
104 size_t parentHandle,
105 size_t parentOffset,
106 size_t *handle) {
107 return parcel->writeEmbeddedBuffer(
108 vec.data(),
109 sizeof(T) * vec.size(),
110 handle,
111 parentHandle,
112 parentOffset + hidl_vec<T>::kOffsetOfBuffer);
113}
114
115template<typename T>
116status_t findInParcel(const hidl_vec<T> &vec, const Parcel &parcel, size_t *handle) {
117 return parcel.quickFindBuffer(vec.data(), handle);
118}
119
120// ---------------------- MQDescriptor
121
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800122template<typename T, MQFlavor flavor>
Yifan Hong7f97f442016-11-14 18:31:05 -0800123::android::status_t readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700124 MQDescriptor<T, flavor> &obj,
Yifan Hong7f97f442016-11-14 18:31:05 -0800125 const ::android::hardware::Parcel &parcel,
126 size_t parentHandle,
127 size_t parentOffset) {
128 ::android::status_t _hidl_err = ::android::OK;
129
130 size_t _hidl_grantors_child;
131
132 _hidl_err = ::android::hardware::readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700133 obj.grantors(),
Yifan Hong7f97f442016-11-14 18:31:05 -0800134 parcel,
135 parentHandle,
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800136 parentOffset + MQDescriptor<T, flavor>::kOffsetOfGrantors,
Yifan Hong7f97f442016-11-14 18:31:05 -0800137 &_hidl_grantors_child);
138
139 if (_hidl_err != ::android::OK) { return _hidl_err; }
140
Steven Moreland1f535a22017-01-06 19:25:49 -0800141 const native_handle_t *_hidl_mq_handle_ptr;
Hridya Valsarajuf6fa2a92017-03-08 15:20:07 -0800142 _hidl_err = parcel.readNullableEmbeddedNativeHandle(
Yifan Hong7f97f442016-11-14 18:31:05 -0800143 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -0800144 parentOffset + MQDescriptor<T, flavor>::kOffsetOfHandle,
145 &_hidl_mq_handle_ptr);
Yifan Hong7f97f442016-11-14 18:31:05 -0800146
Steven Moreland1f535a22017-01-06 19:25:49 -0800147 if (_hidl_err != ::android::OK) { return _hidl_err; }
Yifan Hong7f97f442016-11-14 18:31:05 -0800148
149 return _hidl_err;
150}
151
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800152template<typename T, MQFlavor flavor>
Yifan Hong7f97f442016-11-14 18:31:05 -0800153::android::status_t writeEmbeddedToParcel(
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800154 const MQDescriptor<T, flavor> &obj,
Yifan Hong7f97f442016-11-14 18:31:05 -0800155 ::android::hardware::Parcel *parcel,
156 size_t parentHandle,
157 size_t parentOffset) {
158 ::android::status_t _hidl_err = ::android::OK;
159
160 size_t _hidl_grantors_child;
161
162 _hidl_err = ::android::hardware::writeEmbeddedToParcel(
163 obj.grantors(),
164 parcel,
165 parentHandle,
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800166 parentOffset + MQDescriptor<T, flavor>::kOffsetOfGrantors,
Yifan Hong7f97f442016-11-14 18:31:05 -0800167 &_hidl_grantors_child);
168
169 if (_hidl_err != ::android::OK) { return _hidl_err; }
170
171 _hidl_err = parcel->writeEmbeddedNativeHandle(
172 obj.handle(),
173 parentHandle,
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800174 parentOffset + MQDescriptor<T, flavor>::kOffsetOfHandle);
Yifan Hong7f97f442016-11-14 18:31:05 -0800175
176 if (_hidl_err != ::android::OK) { return _hidl_err; }
177
178 return _hidl_err;
179}
180
181// ---------------------- pointers for HIDL
182
183template <typename T>
184static status_t readEmbeddedReferenceFromParcel(
185 T const* * /* bufptr */,
186 const Parcel & parcel,
187 size_t parentHandle,
188 size_t parentOffset,
189 size_t *handle,
190 bool *shouldResolveRefInBuffer
191 ) {
192 // *bufptr is ignored because, if I am embedded in some
193 // other buffer, the kernel should have fixed me up already.
194 bool isPreviouslyWritten;
195 status_t result = parcel.readEmbeddedReference(
196 nullptr, // ignored, not written to bufptr.
197 handle,
198 parentHandle,
199 parentOffset,
200 &isPreviouslyWritten);
201 // tell caller to run T::readEmbeddedToParcel and
202 // T::readEmbeddedReferenceToParcel if necessary.
203 // It is not called here because we don't know if these two are valid methods.
204 *shouldResolveRefInBuffer = !isPreviouslyWritten;
205 return result;
206}
207
208template <typename T>
209static status_t writeEmbeddedReferenceToParcel(
210 T const* buf,
211 Parcel *parcel, size_t parentHandle, size_t parentOffset,
212 size_t *handle,
213 bool *shouldResolveRefInBuffer
214 ) {
215
216 if(buf == nullptr) {
217 *shouldResolveRefInBuffer = false;
218 return parcel->writeEmbeddedNullReference(handle, parentHandle, parentOffset);
219 }
220
221 // find whether the buffer exists
222 size_t childHandle, childOffset;
223 status_t result;
224 bool found;
225
226 result = parcel->findBuffer(buf, sizeof(T), &found, &childHandle, &childOffset);
227
228 // tell caller to run T::writeEmbeddedToParcel and
229 // T::writeEmbeddedReferenceToParcel if necessary.
230 // It is not called here because we don't know if these two are valid methods.
231 *shouldResolveRefInBuffer = !found;
232
233 if(result != OK) {
234 return result; // bad pointers and length given
235 }
236 if(!found) { // did not find it.
237 return parcel->writeEmbeddedBuffer(buf, sizeof(T), handle,
238 parentHandle, parentOffset);
239 }
240 // found the buffer. easy case.
241 return parcel->writeEmbeddedReference(
242 handle,
243 childHandle,
244 childOffset,
245 parentHandle,
246 parentOffset);
247}
248
249template <typename T>
250static status_t readReferenceFromParcel(
251 T const* *bufptr,
252 const Parcel & parcel,
253 size_t *handle,
254 bool *shouldResolveRefInBuffer
255 ) {
256 bool isPreviouslyWritten;
257 status_t result = parcel.readReference(reinterpret_cast<void const* *>(bufptr),
258 handle, &isPreviouslyWritten);
259 // tell caller to run T::readEmbeddedToParcel and
260 // T::readEmbeddedReferenceToParcel if necessary.
261 // It is not called here because we don't know if these two are valid methods.
262 *shouldResolveRefInBuffer = !isPreviouslyWritten;
263 return result;
264}
265
266template <typename T>
267static status_t writeReferenceToParcel(
268 T const *buf,
269 Parcel * parcel,
270 size_t *handle,
271 bool *shouldResolveRefInBuffer
272 ) {
273
274 if(buf == nullptr) {
275 *shouldResolveRefInBuffer = false;
276 return parcel->writeNullReference(handle);
277 }
278
279 // find whether the buffer exists
280 size_t childHandle, childOffset;
281 status_t result;
282 bool found;
283
284 result = parcel->findBuffer(buf, sizeof(T), &found, &childHandle, &childOffset);
285
286 // tell caller to run T::writeEmbeddedToParcel and
287 // T::writeEmbeddedReferenceToParcel if necessary.
288 // It is not called here because we don't know if these two are valid methods.
289 *shouldResolveRefInBuffer = !found;
290
291 if(result != OK) {
292 return result; // bad pointers and length given
293 }
294 if(!found) { // did not find it.
295 return parcel->writeBuffer(buf, sizeof(T), handle);
296 }
297 // found the buffer. easy case.
298 return parcel->writeReference(handle,
299 childHandle, childOffset);
300}
301
302// ---------------------- support for casting interfaces
303
Steven Moreland9cbef742018-06-26 16:23:50 -0700304// Constructs a binder for this interface and caches it. If it has already been created
Steven Moreland986fa092018-09-27 10:38:03 -0700305// then it returns it.
Steven Moreland9cbef742018-06-26 16:23:50 -0700306sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr);
307
Yifan Hong7f97f442016-11-14 18:31:05 -0800308// Construct a smallest possible binder from the given interface.
309// If it is remote, then its remote() will be retrieved.
310// Otherwise, the smallest possible BnChild is found where IChild is a subclass of IType
311// and iface is of class IChild. BnChild will be used to wrapped the given iface.
312// Return nullptr if iface is null or any failure.
Steven Moreland5f82d3e2017-07-27 13:57:56 -0700313template <typename IType,
314 typename = std::enable_if_t<std::is_same<details::i_tag, typename IType::_hidl_tag>::value>>
Yifan Hong7f97f442016-11-14 18:31:05 -0800315sp<IBinder> toBinder(sp<IType> iface) {
316 IType *ifacePtr = iface.get();
Steven Moreland986fa092018-09-27 10:38:03 -0700317 return getOrCreateCachedBinder(ifacePtr);
Yifan Hong7f97f442016-11-14 18:31:05 -0800318}
319
Martijn Coenen12f04d92016-12-07 17:29:41 +0100320template <typename IType, typename ProxyType, typename StubType>
321sp<IType> fromBinder(const sp<IBinder>& binderIface) {
322 using ::android::hidl::base::V1_0::IBase;
Yifan Hong4e925992017-01-09 17:47:17 -0800323 using ::android::hidl::base::V1_0::BnHwBase;
Martijn Coenen12f04d92016-12-07 17:29:41 +0100324
325 if (binderIface.get() == nullptr) {
326 return nullptr;
327 }
328 if (binderIface->localBinder() == nullptr) {
329 return new ProxyType(binderIface);
330 }
Yifan Hong4e925992017-01-09 17:47:17 -0800331 sp<IBase> base = static_cast<BnHwBase*>(binderIface.get())->getImpl();
Yifan Hong2ac326f2017-02-22 18:13:06 -0800332 if (details::canCastInterface(base.get(), IType::descriptor)) {
Martijn Coenen12f04d92016-12-07 17:29:41 +0100333 StubType* stub = static_cast<StubType*>(binderIface.get());
334 return stub->getImpl();
335 } else {
336 return nullptr;
337 }
338}
339
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800340void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin);
341void joinBinderRpcThreadpool();
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800342int setupBinderPolling();
343status_t handleBinderPoll();
Martijn Coenene76c7a22016-11-22 14:53:47 +0100344
Steven Morelandd40af8e2018-05-01 16:33:21 -0700345void addPostCommandTask(const std::function<void(void)> task);
346
Yifan Hong7f97f442016-11-14 18:31:05 -0800347} // namespace hardware
348} // namespace android
349
350
351#endif // ANDROID_HIDL_BINDER_SUPPORT_H