blob: a96050c348f0bf7a7744acc1633c76cdc60e7ef9 [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#define LOG_TAG "HidlSupport"
18
19#include <hidl/HidlBinderSupport.h>
20
Steven Moreland9cbef742018-06-26 16:23:50 -070021#include <InternalStatic.h> // TODO(b/69122224): remove this include, for getOrCreateCachedBinder
Steven Moreland986fa092018-09-27 10:38:03 -070022#include <android/hidl/base/1.0/BpHwBase.h>
Steven Morelandb9173182018-08-08 20:06:08 +000023#include <hwbinder/IPCThreadState.h>
Steven Moreland9cbef742018-06-26 16:23:50 -070024
Yifan Hong777bef92017-02-01 15:50:36 -080025// C includes
26#include <unistd.h>
27
28// C++ includes
29#include <fstream>
30#include <sstream>
31
Yifan Hong7f97f442016-11-14 18:31:05 -080032namespace android {
33namespace hardware {
34
Steven Moreland108d09d2017-05-05 16:15:38 -070035hidl_binder_death_recipient::hidl_binder_death_recipient(const sp<hidl_death_recipient> &recipient,
36 uint64_t cookie, const sp<::android::hidl::base::V1_0::IBase> &base) :
37 mRecipient(recipient), mCookie(cookie), mBase(base) {
38}
39
40void hidl_binder_death_recipient::binderDied(const wp<IBinder>& /*who*/) {
41 sp<hidl_death_recipient> recipient = mRecipient.promote();
Martijn Coenen25508802017-07-05 12:21:00 +020042 if (recipient != nullptr && mBase != nullptr) {
Steven Moreland108d09d2017-05-05 16:15:38 -070043 recipient->serviceDied(mCookie, mBase);
44 }
Martijn Coenen25508802017-07-05 12:21:00 +020045 mBase = nullptr;
Steven Moreland108d09d2017-05-05 16:15:38 -070046}
47
48wp<hidl_death_recipient> hidl_binder_death_recipient::getRecipient() {
49 return mRecipient;
50}
51
Nirav Atre564a8d22018-07-26 18:29:12 -070052const size_t hidl_handle::kOffsetOfNativeHandle = offsetof(hidl_handle, mHandle);
53static_assert(hidl_handle::kOffsetOfNativeHandle == 0, "wrong offset");
54
55status_t readEmbeddedFromParcel(const hidl_handle& /* handle */,
56 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
57 const native_handle_t *handle;
58 status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
59 parentHandle,
60 parentOffset + hidl_handle::kOffsetOfNativeHandle,
61 &handle);
62
63 return _hidl_err;
64}
65
66status_t writeEmbeddedToParcel(const hidl_handle &handle,
67 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
68 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
69 handle.getNativeHandle(),
70 parentHandle,
71 parentOffset + hidl_handle::kOffsetOfNativeHandle);
72
73 return _hidl_err;
74}
75
Martijn Coenen30791002016-12-01 15:40:46 +010076const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle);
77const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080078static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset");
79static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset");
Martijn Coenen30791002016-12-01 15:40:46 +010080
Martijn Coenen9d3eb352017-04-18 20:28:03 -070081status_t readEmbeddedFromParcel(const hidl_memory& memory,
Martijn Coenen30791002016-12-01 15:40:46 +010082 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Nirav Atre564a8d22018-07-26 18:29:12 -070083 // TODO(b/111883309): Invoke readEmbeddedFromParcel(hidl_handle, ...).
Steven Moreland1f535a22017-01-06 19:25:49 -080084 const native_handle_t *handle;
85 ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
Martijn Coenen30791002016-12-01 15:40:46 +010086 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080087 parentOffset + hidl_memory::kOffsetOfHandle,
88 &handle);
Martijn Coenen30791002016-12-01 15:40:46 +010089
Steven Moreland1f535a22017-01-06 19:25:49 -080090 if (_hidl_err == ::android::OK) {
91 _hidl_err = readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -070092 memory.name(),
Steven Moreland1f535a22017-01-06 19:25:49 -080093 parcel,
94 parentHandle,
95 parentOffset + hidl_memory::kOffsetOfName);
Martijn Coenen30791002016-12-01 15:40:46 +010096 }
97
Martijn Coenen30791002016-12-01 15:40:46 +010098 return _hidl_err;
99}
100
101status_t writeEmbeddedToParcel(const hidl_memory &memory,
102 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
Nirav Atre564a8d22018-07-26 18:29:12 -0700103 // TODO(b/111883309): Invoke writeEmbeddedToParcel(hidl_handle, ...).
Martijn Coenen30791002016-12-01 15:40:46 +0100104 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
105 memory.handle(),
106 parentHandle,
107 parentOffset + hidl_memory::kOffsetOfHandle);
108
109 if (_hidl_err == ::android::OK) {
110 _hidl_err = writeEmbeddedToParcel(
111 memory.name(),
112 parcel,
113 parentHandle,
114 parentOffset + hidl_memory::kOffsetOfName);
115 }
116
117 return _hidl_err;
118}
Yifan Hong7f97f442016-11-14 18:31:05 -0800119const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800120static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset");
Yifan Hong7f97f442016-11-14 18:31:05 -0800121
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700122status_t readEmbeddedFromParcel(const hidl_string &string ,
Yifan Hong7f97f442016-11-14 18:31:05 -0800123 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -0800124 const void *out;
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700125
126 status_t status = parcel.readEmbeddedBuffer(
127 string.size() + 1,
Yifan Hong7f97f442016-11-14 18:31:05 -0800128 nullptr /* buffer_handle */,
129 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -0800130 parentOffset + hidl_string::kOffsetOfBuffer,
131 &out);
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700132
133 if (status != OK) {
134 return status;
135 }
136
137 // Always safe to access out[string.size()] because we read size+1 bytes
138 if (static_cast<const char *>(out)[string.size()] != '\0') {
139 ALOGE("Received unterminated hidl_string buffer.");
140 return BAD_VALUE;
141 }
142
143 return OK;
Yifan Hong7f97f442016-11-14 18:31:05 -0800144}
145
146status_t writeEmbeddedToParcel(const hidl_string &string,
147 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
148 return parcel->writeEmbeddedBuffer(
149 string.c_str(),
150 string.size() + 1,
151 nullptr /* handle */,
152 parentHandle,
153 parentOffset + hidl_string::kOffsetOfBuffer);
154}
155
156android::status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel) {
157 return parcel.writeUint32(static_cast<uint32_t>(version.get_major()) << 16 | version.get_minor());
158}
159
160hidl_version* readFromParcel(const android::hardware::Parcel& parcel) {
161 uint32_t version;
162 android::status_t status = parcel.readUint32(&version);
163 if (status != OK) {
164 return nullptr;
165 } else {
166 return new hidl_version(version >> 16, version & 0xFFFF);
167 }
168}
169
170status_t readFromParcel(Status *s, const Parcel& parcel) {
171 int32_t exception;
Yifan Hong7f97f442016-11-14 18:31:05 -0800172 status_t status = parcel.readInt32(&exception);
173 if (status != OK) {
174 s->setFromStatusT(status);
175 return status;
176 }
177
178 // Skip over fat response headers. Not used (or propagated) in native code.
179 if (exception == Status::EX_HAS_REPLY_HEADER) {
180 // Note that the header size includes the 4 byte size field.
181 const int32_t header_start = parcel.dataPosition();
182 int32_t header_size;
183 status = parcel.readInt32(&header_size);
184 if (status != OK) {
185 s->setFromStatusT(status);
186 return status;
187 }
188 parcel.setDataPosition(header_start + header_size);
189 // And fat response headers are currently only used when there are no
190 // exceptions, so act like there was no error.
191 exception = Status::EX_NONE;
192 }
193
194 if (exception == Status::EX_NONE) {
195 *s = Status::ok();
196 return status;
197 }
198
199 // The remote threw an exception. Get the message back.
200 String16 message;
201 status = parcel.readString16(&message);
202 if (status != OK) {
203 s->setFromStatusT(status);
204 return status;
205 }
206
Steven Moreland72db40f2017-03-09 18:15:27 -0800207 s->setException(exception, String8(message));
Yifan Hong7f97f442016-11-14 18:31:05 -0800208
209 return status;
210}
211
212status_t writeToParcel(const Status &s, Parcel* parcel) {
213 // Something really bad has happened, and we're not going to even
214 // try returning rich error data.
215 if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
Steven Moreland662378e2018-09-25 09:23:54 -0700216 status_t status = s.transactionError();
217 return status == OK ? FAILED_TRANSACTION : status;
Yifan Hong7f97f442016-11-14 18:31:05 -0800218 }
219
220 status_t status = parcel->writeInt32(s.exceptionCode());
221 if (status != OK) { return status; }
222 if (s.exceptionCode() == Status::EX_NONE) {
223 // We have no more information to write.
224 return status;
225 }
226 status = parcel->writeString16(String16(s.exceptionMessage()));
Yifan Hong7f97f442016-11-14 18:31:05 -0800227 return status;
228}
229
Steven Moreland9cbef742018-06-26 16:23:50 -0700230sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr) {
Steven Moreland986fa092018-09-27 10:38:03 -0700231 if (ifacePtr == nullptr) {
232 return nullptr;
233 }
234
235 if (ifacePtr->isRemote()) {
236 using ::android::hidl::base::V1_0::BpHwBase;
237
238 BpHwBase* bpBase = static_cast<BpHwBase*>(ifacePtr);
239 BpHwRefBase* bpRefBase = static_cast<BpHwRefBase*>(bpBase);
240 return sp<IBinder>(bpRefBase->remote());
241 }
Steven Moreland9cbef742018-06-26 16:23:50 -0700242
243 std::string descriptor = details::getDescriptor(ifacePtr);
244 if (descriptor.empty()) {
245 // interfaceDescriptor fails
246 return nullptr;
247 }
248
249 // for get + set
250 std::unique_lock<std::mutex> _lock = details::gBnMap.lock();
251
252 wp<BHwBinder> wBnObj = details::gBnMap.getLocked(ifacePtr, nullptr);
253 sp<IBinder> sBnObj = wBnObj.promote();
254
255 if (sBnObj == nullptr) {
256 auto func = details::getBnConstructorMap().get(descriptor, nullptr);
257 if (!func) {
258 // TODO(b/69122224): remove this static variable when prebuilts updated
259 func = details::gBnConstructorMap.get(descriptor, nullptr);
Steven Moreland9cbef742018-06-26 16:23:50 -0700260 }
Steven Moreland9fd33872018-06-26 18:14:22 -0700261 LOG_ALWAYS_FATAL_IF(func == nullptr, "%s gBnConstructorMap returned null for %s", __func__,
262 descriptor.c_str());
Steven Moreland9cbef742018-06-26 16:23:50 -0700263
264 sBnObj = sp<IBinder>(func(static_cast<void*>(ifacePtr)));
Steven Moreland9fd33872018-06-26 18:14:22 -0700265 LOG_ALWAYS_FATAL_IF(sBnObj == nullptr, "%s Bn constructor function returned null for %s",
266 __func__, descriptor.c_str());
Steven Moreland9cbef742018-06-26 16:23:50 -0700267
Steven Moreland9fd33872018-06-26 18:14:22 -0700268 details::gBnMap.setLocked(ifacePtr, static_cast<BHwBinder*>(sBnObj.get()));
Steven Moreland9cbef742018-06-26 16:23:50 -0700269 }
270
271 return sBnObj;
272}
273
Steven Moreland50815be2018-05-24 12:35:44 -0700274static bool gThreadPoolConfigured = false;
275
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800276void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
Steven Moreland50815be2018-05-24 12:35:44 -0700277 status_t ret = ProcessState::self()->setThreadPoolConfiguration(
278 maxThreads, callerWillJoin /*callerJoinsPool*/);
279 LOG_ALWAYS_FATAL_IF(ret != OK, "Could not setThreadPoolConfiguration: %d", ret);
280
281 gThreadPoolConfigured = true;
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800282}
283
284void joinBinderRpcThreadpool() {
Steven Morelandcd329a82018-05-29 13:30:11 -0700285 LOG_ALWAYS_FATAL_IF(!gThreadPoolConfigured,
286 "HIDL joinRpcThreadpool without calling configureRpcThreadPool.");
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800287 IPCThreadState::self()->joinThreadPool();
288}
289
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800290int setupBinderPolling() {
291 int fd;
292 int err = IPCThreadState::self()->setupPolling(&fd);
293
Steven Morelandcd329a82018-05-29 13:30:11 -0700294 LOG_ALWAYS_FATAL_IF(err != OK, "Failed to setup binder polling: %d (%s)", err, strerror(err));
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800295
296 return err == OK ? fd : -1;
297}
298
299status_t handleBinderPoll() {
300 return IPCThreadState::self()->handlePolledCommands();
301}
302
Steven Morelandd40af8e2018-05-01 16:33:21 -0700303void addPostCommandTask(const std::function<void(void)> task) {
304 IPCThreadState::self()->addPostCommandTask(task);
305}
306
Yifan Hong7f97f442016-11-14 18:31:05 -0800307} // namespace hardware
308} // namespace android