blob: a221b46e00ca08946ba70368d4ab9ed7d6d645e5 [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
22
Yifan Hong777bef92017-02-01 15:50:36 -080023// C includes
24#include <unistd.h>
25
26// C++ includes
27#include <fstream>
28#include <sstream>
29
Yifan Hong7f97f442016-11-14 18:31:05 -080030namespace android {
31namespace hardware {
32
Steven Moreland108d09d2017-05-05 16:15:38 -070033hidl_binder_death_recipient::hidl_binder_death_recipient(const sp<hidl_death_recipient> &recipient,
34 uint64_t cookie, const sp<::android::hidl::base::V1_0::IBase> &base) :
35 mRecipient(recipient), mCookie(cookie), mBase(base) {
36}
37
38void hidl_binder_death_recipient::binderDied(const wp<IBinder>& /*who*/) {
39 sp<hidl_death_recipient> recipient = mRecipient.promote();
Martijn Coenen25508802017-07-05 12:21:00 +020040 if (recipient != nullptr && mBase != nullptr) {
Steven Moreland108d09d2017-05-05 16:15:38 -070041 recipient->serviceDied(mCookie, mBase);
42 }
Martijn Coenen25508802017-07-05 12:21:00 +020043 mBase = nullptr;
Steven Moreland108d09d2017-05-05 16:15:38 -070044}
45
46wp<hidl_death_recipient> hidl_binder_death_recipient::getRecipient() {
47 return mRecipient;
48}
49
Martijn Coenen30791002016-12-01 15:40:46 +010050const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle);
51const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080052static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset");
53static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset");
Martijn Coenen30791002016-12-01 15:40:46 +010054
Martijn Coenen9d3eb352017-04-18 20:28:03 -070055status_t readEmbeddedFromParcel(const hidl_memory& memory,
Martijn Coenen30791002016-12-01 15:40:46 +010056 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -080057 const native_handle_t *handle;
58 ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
Martijn Coenen30791002016-12-01 15:40:46 +010059 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080060 parentOffset + hidl_memory::kOffsetOfHandle,
61 &handle);
Martijn Coenen30791002016-12-01 15:40:46 +010062
Steven Moreland1f535a22017-01-06 19:25:49 -080063 if (_hidl_err == ::android::OK) {
64 _hidl_err = readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -070065 memory.name(),
Steven Moreland1f535a22017-01-06 19:25:49 -080066 parcel,
67 parentHandle,
68 parentOffset + hidl_memory::kOffsetOfName);
Martijn Coenen30791002016-12-01 15:40:46 +010069 }
70
Martijn Coenen30791002016-12-01 15:40:46 +010071 return _hidl_err;
72}
73
74status_t writeEmbeddedToParcel(const hidl_memory &memory,
75 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
76 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
77 memory.handle(),
78 parentHandle,
79 parentOffset + hidl_memory::kOffsetOfHandle);
80
81 if (_hidl_err == ::android::OK) {
82 _hidl_err = writeEmbeddedToParcel(
83 memory.name(),
84 parcel,
85 parentHandle,
86 parentOffset + hidl_memory::kOffsetOfName);
87 }
88
89 return _hidl_err;
90}
Yifan Hong7f97f442016-11-14 18:31:05 -080091const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080092static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset");
Yifan Hong7f97f442016-11-14 18:31:05 -080093
Martijn Coenen9d3eb352017-04-18 20:28:03 -070094status_t readEmbeddedFromParcel(const hidl_string &string ,
Yifan Hong7f97f442016-11-14 18:31:05 -080095 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -080096 const void *out;
Martijn Coenen9d3eb352017-04-18 20:28:03 -070097
98 status_t status = parcel.readEmbeddedBuffer(
99 string.size() + 1,
Yifan Hong7f97f442016-11-14 18:31:05 -0800100 nullptr /* buffer_handle */,
101 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -0800102 parentOffset + hidl_string::kOffsetOfBuffer,
103 &out);
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700104
105 if (status != OK) {
106 return status;
107 }
108
109 // Always safe to access out[string.size()] because we read size+1 bytes
110 if (static_cast<const char *>(out)[string.size()] != '\0') {
111 ALOGE("Received unterminated hidl_string buffer.");
112 return BAD_VALUE;
113 }
114
115 return OK;
Yifan Hong7f97f442016-11-14 18:31:05 -0800116}
117
118status_t writeEmbeddedToParcel(const hidl_string &string,
119 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
120 return parcel->writeEmbeddedBuffer(
121 string.c_str(),
122 string.size() + 1,
123 nullptr /* handle */,
124 parentHandle,
125 parentOffset + hidl_string::kOffsetOfBuffer);
126}
127
128android::status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel) {
129 return parcel.writeUint32(static_cast<uint32_t>(version.get_major()) << 16 | version.get_minor());
130}
131
132hidl_version* readFromParcel(const android::hardware::Parcel& parcel) {
133 uint32_t version;
134 android::status_t status = parcel.readUint32(&version);
135 if (status != OK) {
136 return nullptr;
137 } else {
138 return new hidl_version(version >> 16, version & 0xFFFF);
139 }
140}
141
142status_t readFromParcel(Status *s, const Parcel& parcel) {
143 int32_t exception;
Yifan Hong7f97f442016-11-14 18:31:05 -0800144 status_t status = parcel.readInt32(&exception);
145 if (status != OK) {
146 s->setFromStatusT(status);
147 return status;
148 }
149
150 // Skip over fat response headers. Not used (or propagated) in native code.
151 if (exception == Status::EX_HAS_REPLY_HEADER) {
152 // Note that the header size includes the 4 byte size field.
153 const int32_t header_start = parcel.dataPosition();
154 int32_t header_size;
155 status = parcel.readInt32(&header_size);
156 if (status != OK) {
157 s->setFromStatusT(status);
158 return status;
159 }
160 parcel.setDataPosition(header_start + header_size);
161 // And fat response headers are currently only used when there are no
162 // exceptions, so act like there was no error.
163 exception = Status::EX_NONE;
164 }
165
166 if (exception == Status::EX_NONE) {
167 *s = Status::ok();
168 return status;
169 }
170
171 // The remote threw an exception. Get the message back.
172 String16 message;
173 status = parcel.readString16(&message);
174 if (status != OK) {
175 s->setFromStatusT(status);
176 return status;
177 }
178
Steven Moreland72db40f2017-03-09 18:15:27 -0800179 s->setException(exception, String8(message));
Yifan Hong7f97f442016-11-14 18:31:05 -0800180
181 return status;
182}
183
184status_t writeToParcel(const Status &s, Parcel* parcel) {
185 // Something really bad has happened, and we're not going to even
186 // try returning rich error data.
187 if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
188 return s.transactionError();
189 }
190
191 status_t status = parcel->writeInt32(s.exceptionCode());
192 if (status != OK) { return status; }
193 if (s.exceptionCode() == Status::EX_NONE) {
194 // We have no more information to write.
195 return status;
196 }
197 status = parcel->writeString16(String16(s.exceptionMessage()));
Yifan Hong7f97f442016-11-14 18:31:05 -0800198 return status;
199}
200
Steven Moreland9cbef742018-06-26 16:23:50 -0700201sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr) {
Steven Moreland9fd33872018-06-26 18:14:22 -0700202 LOG_ALWAYS_FATAL_IF(ifacePtr->isRemote(), "%s does not have a way to construct remote binders",
203 __func__);
Steven Moreland9cbef742018-06-26 16:23:50 -0700204
205 std::string descriptor = details::getDescriptor(ifacePtr);
206 if (descriptor.empty()) {
207 // interfaceDescriptor fails
208 return nullptr;
209 }
210
211 // for get + set
212 std::unique_lock<std::mutex> _lock = details::gBnMap.lock();
213
214 wp<BHwBinder> wBnObj = details::gBnMap.getLocked(ifacePtr, nullptr);
215 sp<IBinder> sBnObj = wBnObj.promote();
216
217 if (sBnObj == nullptr) {
218 auto func = details::getBnConstructorMap().get(descriptor, nullptr);
219 if (!func) {
220 // TODO(b/69122224): remove this static variable when prebuilts updated
221 func = details::gBnConstructorMap.get(descriptor, nullptr);
Steven Moreland9cbef742018-06-26 16:23:50 -0700222 }
Steven Moreland9fd33872018-06-26 18:14:22 -0700223 LOG_ALWAYS_FATAL_IF(func == nullptr, "%s gBnConstructorMap returned null for %s", __func__,
224 descriptor.c_str());
Steven Moreland9cbef742018-06-26 16:23:50 -0700225
226 sBnObj = sp<IBinder>(func(static_cast<void*>(ifacePtr)));
Steven Moreland9fd33872018-06-26 18:14:22 -0700227 LOG_ALWAYS_FATAL_IF(sBnObj == nullptr, "%s Bn constructor function returned null for %s",
228 __func__, descriptor.c_str());
Steven Moreland9cbef742018-06-26 16:23:50 -0700229
Steven Moreland9fd33872018-06-26 18:14:22 -0700230 details::gBnMap.setLocked(ifacePtr, static_cast<BHwBinder*>(sBnObj.get()));
Steven Moreland9cbef742018-06-26 16:23:50 -0700231 }
232
233 return sBnObj;
234}
235
Steven Moreland50815be2018-05-24 12:35:44 -0700236static bool gThreadPoolConfigured = false;
237
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800238void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
Steven Moreland50815be2018-05-24 12:35:44 -0700239 status_t ret = ProcessState::self()->setThreadPoolConfiguration(
240 maxThreads, callerWillJoin /*callerJoinsPool*/);
241 LOG_ALWAYS_FATAL_IF(ret != OK, "Could not setThreadPoolConfiguration: %d", ret);
242
243 gThreadPoolConfigured = true;
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800244}
245
246void joinBinderRpcThreadpool() {
Steven Morelandcd329a82018-05-29 13:30:11 -0700247 LOG_ALWAYS_FATAL_IF(!gThreadPoolConfigured,
248 "HIDL joinRpcThreadpool without calling configureRpcThreadPool.");
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800249 IPCThreadState::self()->joinThreadPool();
250}
251
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800252int setupBinderPolling() {
253 int fd;
254 int err = IPCThreadState::self()->setupPolling(&fd);
255
Steven Morelandcd329a82018-05-29 13:30:11 -0700256 LOG_ALWAYS_FATAL_IF(err != OK, "Failed to setup binder polling: %d (%s)", err, strerror(err));
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800257
258 return err == OK ? fd : -1;
259}
260
261status_t handleBinderPoll() {
262 return IPCThreadState::self()->handlePolledCommands();
263}
264
Steven Morelandd40af8e2018-05-01 16:33:21 -0700265void addPostCommandTask(const std::function<void(void)> task) {
266 IPCThreadState::self()->addPostCommandTask(task);
267}
268
Yifan Hong7f97f442016-11-14 18:31:05 -0800269} // namespace hardware
270} // namespace android