blob: 05f7fab2921ce0336874f508582ddd4e9bdf0c72 [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
Yifan Hong7f97f442016-11-14 18:31:05 -0800181// ---------------------- support for casting interfaces
182
Steven Moreland9cbef742018-06-26 16:23:50 -0700183// Constructs a binder for this interface and caches it. If it has already been created
Steven Moreland986fa092018-09-27 10:38:03 -0700184// then it returns it.
Steven Moreland9cbef742018-06-26 16:23:50 -0700185sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr);
186
Yifan Hong7f97f442016-11-14 18:31:05 -0800187// Construct a smallest possible binder from the given interface.
188// If it is remote, then its remote() will be retrieved.
189// Otherwise, the smallest possible BnChild is found where IChild is a subclass of IType
190// and iface is of class IChild. BnChild will be used to wrapped the given iface.
191// Return nullptr if iface is null or any failure.
Steven Moreland5f82d3e2017-07-27 13:57:56 -0700192template <typename IType,
193 typename = std::enable_if_t<std::is_same<details::i_tag, typename IType::_hidl_tag>::value>>
Yifan Hong7f97f442016-11-14 18:31:05 -0800194sp<IBinder> toBinder(sp<IType> iface) {
195 IType *ifacePtr = iface.get();
Steven Moreland986fa092018-09-27 10:38:03 -0700196 return getOrCreateCachedBinder(ifacePtr);
Yifan Hong7f97f442016-11-14 18:31:05 -0800197}
198
Martijn Coenen12f04d92016-12-07 17:29:41 +0100199template <typename IType, typename ProxyType, typename StubType>
200sp<IType> fromBinder(const sp<IBinder>& binderIface) {
201 using ::android::hidl::base::V1_0::IBase;
Yifan Hong4e925992017-01-09 17:47:17 -0800202 using ::android::hidl::base::V1_0::BnHwBase;
Martijn Coenen12f04d92016-12-07 17:29:41 +0100203
204 if (binderIface.get() == nullptr) {
205 return nullptr;
206 }
Steven Morelandde0f4132019-05-20 11:05:36 -0700207
Martijn Coenen12f04d92016-12-07 17:29:41 +0100208 if (binderIface->localBinder() == nullptr) {
209 return new ProxyType(binderIface);
210 }
Steven Morelandde0f4132019-05-20 11:05:36 -0700211
212 // Ensure that IBinder is BnHwBase (not JHwBinder, for instance)
213 if (!binderIface->checkSubclass(IBase::descriptor)) {
214 return new ProxyType(binderIface);
215 }
Yifan Hong4e925992017-01-09 17:47:17 -0800216 sp<IBase> base = static_cast<BnHwBase*>(binderIface.get())->getImpl();
Steven Morelandde0f4132019-05-20 11:05:36 -0700217
Yifan Hong2ac326f2017-02-22 18:13:06 -0800218 if (details::canCastInterface(base.get(), IType::descriptor)) {
Martijn Coenen12f04d92016-12-07 17:29:41 +0100219 StubType* stub = static_cast<StubType*>(binderIface.get());
220 return stub->getImpl();
221 } else {
222 return nullptr;
223 }
224}
225
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800226void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin);
227void joinBinderRpcThreadpool();
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800228int setupBinderPolling();
229status_t handleBinderPoll();
Martijn Coenene76c7a22016-11-22 14:53:47 +0100230
Steven Morelandd40af8e2018-05-01 16:33:21 -0700231void addPostCommandTask(const std::function<void(void)> task);
232
Yifan Hong7f97f442016-11-14 18:31:05 -0800233} // namespace hardware
234} // namespace android
235
236
237#endif // ANDROID_HIDL_BINDER_SUPPORT_H