blob: add1f5efe99e51d1c649408701b0bebb4e2d3fc3 [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 Morelandb9173182018-08-08 20:06:08 +000022#include <hwbinder/IPCThreadState.h>
Steven Moreland9cbef742018-06-26 16:23:50 -070023
Yifan Hong777bef92017-02-01 15:50:36 -080024// C includes
25#include <unistd.h>
26
27// C++ includes
28#include <fstream>
29#include <sstream>
30
Yifan Hong7f97f442016-11-14 18:31:05 -080031namespace android {
32namespace hardware {
33
Steven Moreland108d09d2017-05-05 16:15:38 -070034hidl_binder_death_recipient::hidl_binder_death_recipient(const sp<hidl_death_recipient> &recipient,
35 uint64_t cookie, const sp<::android::hidl::base::V1_0::IBase> &base) :
36 mRecipient(recipient), mCookie(cookie), mBase(base) {
37}
38
39void hidl_binder_death_recipient::binderDied(const wp<IBinder>& /*who*/) {
40 sp<hidl_death_recipient> recipient = mRecipient.promote();
Martijn Coenen25508802017-07-05 12:21:00 +020041 if (recipient != nullptr && mBase != nullptr) {
Steven Moreland108d09d2017-05-05 16:15:38 -070042 recipient->serviceDied(mCookie, mBase);
43 }
Martijn Coenen25508802017-07-05 12:21:00 +020044 mBase = nullptr;
Steven Moreland108d09d2017-05-05 16:15:38 -070045}
46
47wp<hidl_death_recipient> hidl_binder_death_recipient::getRecipient() {
48 return mRecipient;
49}
50
Nirav Atre564a8d22018-07-26 18:29:12 -070051const size_t hidl_handle::kOffsetOfNativeHandle = offsetof(hidl_handle, mHandle);
52static_assert(hidl_handle::kOffsetOfNativeHandle == 0, "wrong offset");
53
54status_t readEmbeddedFromParcel(const hidl_handle& /* handle */,
55 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
56 const native_handle_t *handle;
57 status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
58 parentHandle,
59 parentOffset + hidl_handle::kOffsetOfNativeHandle,
60 &handle);
61
62 return _hidl_err;
63}
64
65status_t writeEmbeddedToParcel(const hidl_handle &handle,
66 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
67 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
68 handle.getNativeHandle(),
69 parentHandle,
70 parentOffset + hidl_handle::kOffsetOfNativeHandle);
71
72 return _hidl_err;
73}
74
Martijn Coenen30791002016-12-01 15:40:46 +010075const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle);
76const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080077static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset");
78static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset");
Martijn Coenen30791002016-12-01 15:40:46 +010079
Martijn Coenen9d3eb352017-04-18 20:28:03 -070080status_t readEmbeddedFromParcel(const hidl_memory& memory,
Martijn Coenen30791002016-12-01 15:40:46 +010081 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Nirav Atre564a8d22018-07-26 18:29:12 -070082 // TODO(b/111883309): Invoke readEmbeddedFromParcel(hidl_handle, ...).
Steven Moreland1f535a22017-01-06 19:25:49 -080083 const native_handle_t *handle;
84 ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
Martijn Coenen30791002016-12-01 15:40:46 +010085 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080086 parentOffset + hidl_memory::kOffsetOfHandle,
87 &handle);
Martijn Coenen30791002016-12-01 15:40:46 +010088
Steven Moreland1f535a22017-01-06 19:25:49 -080089 if (_hidl_err == ::android::OK) {
90 _hidl_err = readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -070091 memory.name(),
Steven Moreland1f535a22017-01-06 19:25:49 -080092 parcel,
93 parentHandle,
94 parentOffset + hidl_memory::kOffsetOfName);
Martijn Coenen30791002016-12-01 15:40:46 +010095 }
96
Martijn Coenen30791002016-12-01 15:40:46 +010097 return _hidl_err;
98}
99
100status_t writeEmbeddedToParcel(const hidl_memory &memory,
101 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
Nirav Atre564a8d22018-07-26 18:29:12 -0700102 // TODO(b/111883309): Invoke writeEmbeddedToParcel(hidl_handle, ...).
Martijn Coenen30791002016-12-01 15:40:46 +0100103 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
104 memory.handle(),
105 parentHandle,
106 parentOffset + hidl_memory::kOffsetOfHandle);
107
108 if (_hidl_err == ::android::OK) {
109 _hidl_err = writeEmbeddedToParcel(
110 memory.name(),
111 parcel,
112 parentHandle,
113 parentOffset + hidl_memory::kOffsetOfName);
114 }
115
116 return _hidl_err;
117}
Yifan Hong7f97f442016-11-14 18:31:05 -0800118const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800119static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset");
Yifan Hong7f97f442016-11-14 18:31:05 -0800120
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700121status_t readEmbeddedFromParcel(const hidl_string &string ,
Yifan Hong7f97f442016-11-14 18:31:05 -0800122 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -0800123 const void *out;
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700124
125 status_t status = parcel.readEmbeddedBuffer(
126 string.size() + 1,
Yifan Hong7f97f442016-11-14 18:31:05 -0800127 nullptr /* buffer_handle */,
128 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -0800129 parentOffset + hidl_string::kOffsetOfBuffer,
130 &out);
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700131
132 if (status != OK) {
133 return status;
134 }
135
136 // Always safe to access out[string.size()] because we read size+1 bytes
137 if (static_cast<const char *>(out)[string.size()] != '\0') {
138 ALOGE("Received unterminated hidl_string buffer.");
139 return BAD_VALUE;
140 }
141
142 return OK;
Yifan Hong7f97f442016-11-14 18:31:05 -0800143}
144
145status_t writeEmbeddedToParcel(const hidl_string &string,
146 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
147 return parcel->writeEmbeddedBuffer(
148 string.c_str(),
149 string.size() + 1,
150 nullptr /* handle */,
151 parentHandle,
152 parentOffset + hidl_string::kOffsetOfBuffer);
153}
154
155android::status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel) {
156 return parcel.writeUint32(static_cast<uint32_t>(version.get_major()) << 16 | version.get_minor());
157}
158
159hidl_version* readFromParcel(const android::hardware::Parcel& parcel) {
160 uint32_t version;
161 android::status_t status = parcel.readUint32(&version);
162 if (status != OK) {
163 return nullptr;
164 } else {
165 return new hidl_version(version >> 16, version & 0xFFFF);
166 }
167}
168
169status_t readFromParcel(Status *s, const Parcel& parcel) {
170 int32_t exception;
Yifan Hong7f97f442016-11-14 18:31:05 -0800171 status_t status = parcel.readInt32(&exception);
172 if (status != OK) {
173 s->setFromStatusT(status);
174 return status;
175 }
176
177 // Skip over fat response headers. Not used (or propagated) in native code.
178 if (exception == Status::EX_HAS_REPLY_HEADER) {
179 // Note that the header size includes the 4 byte size field.
180 const int32_t header_start = parcel.dataPosition();
181 int32_t header_size;
182 status = parcel.readInt32(&header_size);
183 if (status != OK) {
184 s->setFromStatusT(status);
185 return status;
186 }
187 parcel.setDataPosition(header_start + header_size);
188 // And fat response headers are currently only used when there are no
189 // exceptions, so act like there was no error.
190 exception = Status::EX_NONE;
191 }
192
193 if (exception == Status::EX_NONE) {
194 *s = Status::ok();
195 return status;
196 }
197
198 // The remote threw an exception. Get the message back.
199 String16 message;
200 status = parcel.readString16(&message);
201 if (status != OK) {
202 s->setFromStatusT(status);
203 return status;
204 }
205
Steven Moreland72db40f2017-03-09 18:15:27 -0800206 s->setException(exception, String8(message));
Yifan Hong7f97f442016-11-14 18:31:05 -0800207
208 return status;
209}
210
211status_t writeToParcel(const Status &s, Parcel* parcel) {
212 // Something really bad has happened, and we're not going to even
213 // try returning rich error data.
214 if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
215 return s.transactionError();
216 }
217
218 status_t status = parcel->writeInt32(s.exceptionCode());
219 if (status != OK) { return status; }
220 if (s.exceptionCode() == Status::EX_NONE) {
221 // We have no more information to write.
222 return status;
223 }
224 status = parcel->writeString16(String16(s.exceptionMessage()));
Yifan Hong7f97f442016-11-14 18:31:05 -0800225 return status;
226}
227
Steven Moreland9cbef742018-06-26 16:23:50 -0700228sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr) {
Steven Moreland9fd33872018-06-26 18:14:22 -0700229 LOG_ALWAYS_FATAL_IF(ifacePtr->isRemote(), "%s does not have a way to construct remote binders",
230 __func__);
Steven Moreland9cbef742018-06-26 16:23:50 -0700231
232 std::string descriptor = details::getDescriptor(ifacePtr);
233 if (descriptor.empty()) {
234 // interfaceDescriptor fails
235 return nullptr;
236 }
237
238 // for get + set
239 std::unique_lock<std::mutex> _lock = details::gBnMap.lock();
240
241 wp<BHwBinder> wBnObj = details::gBnMap.getLocked(ifacePtr, nullptr);
242 sp<IBinder> sBnObj = wBnObj.promote();
243
244 if (sBnObj == nullptr) {
245 auto func = details::getBnConstructorMap().get(descriptor, nullptr);
246 if (!func) {
247 // TODO(b/69122224): remove this static variable when prebuilts updated
248 func = details::gBnConstructorMap.get(descriptor, nullptr);
Steven Moreland9cbef742018-06-26 16:23:50 -0700249 }
Steven Moreland9fd33872018-06-26 18:14:22 -0700250 LOG_ALWAYS_FATAL_IF(func == nullptr, "%s gBnConstructorMap returned null for %s", __func__,
251 descriptor.c_str());
Steven Moreland9cbef742018-06-26 16:23:50 -0700252
253 sBnObj = sp<IBinder>(func(static_cast<void*>(ifacePtr)));
Steven Moreland9fd33872018-06-26 18:14:22 -0700254 LOG_ALWAYS_FATAL_IF(sBnObj == nullptr, "%s Bn constructor function returned null for %s",
255 __func__, descriptor.c_str());
Steven Moreland9cbef742018-06-26 16:23:50 -0700256
Steven Moreland9fd33872018-06-26 18:14:22 -0700257 details::gBnMap.setLocked(ifacePtr, static_cast<BHwBinder*>(sBnObj.get()));
Steven Moreland9cbef742018-06-26 16:23:50 -0700258 }
259
260 return sBnObj;
261}
262
Steven Moreland50815be2018-05-24 12:35:44 -0700263static bool gThreadPoolConfigured = false;
264
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800265void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
Steven Moreland50815be2018-05-24 12:35:44 -0700266 status_t ret = ProcessState::self()->setThreadPoolConfiguration(
267 maxThreads, callerWillJoin /*callerJoinsPool*/);
268 LOG_ALWAYS_FATAL_IF(ret != OK, "Could not setThreadPoolConfiguration: %d", ret);
269
270 gThreadPoolConfigured = true;
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800271}
272
273void joinBinderRpcThreadpool() {
Steven Morelandcd329a82018-05-29 13:30:11 -0700274 LOG_ALWAYS_FATAL_IF(!gThreadPoolConfigured,
275 "HIDL joinRpcThreadpool without calling configureRpcThreadPool.");
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800276 IPCThreadState::self()->joinThreadPool();
277}
278
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800279int setupBinderPolling() {
280 int fd;
281 int err = IPCThreadState::self()->setupPolling(&fd);
282
Steven Morelandcd329a82018-05-29 13:30:11 -0700283 LOG_ALWAYS_FATAL_IF(err != OK, "Failed to setup binder polling: %d (%s)", err, strerror(err));
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800284
285 return err == OK ? fd : -1;
286}
287
288status_t handleBinderPoll() {
289 return IPCThreadState::self()->handlePolledCommands();
290}
291
Steven Morelandd40af8e2018-05-01 16:33:21 -0700292void addPostCommandTask(const std::function<void(void)> task) {
293 IPCThreadState::self()->addPostCommandTask(task);
294}
295
Yifan Hong7f97f442016-11-14 18:31:05 -0800296} // namespace hardware
297} // namespace android