blob: ec236404efe8b70239fe23172937c19e69839817 [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
Yifan Hong777bef92017-02-01 15:50:36 -080021// C includes
Steven Moreland3645dc32018-06-06 16:28:29 -070022#include <inttypes.h>
Yifan Hong777bef92017-02-01 15:50:36 -080023#include <unistd.h>
24
25// C++ includes
26#include <fstream>
27#include <sstream>
28
Yifan Hong7f97f442016-11-14 18:31:05 -080029namespace android {
30namespace hardware {
31
Steven Moreland108d09d2017-05-05 16:15:38 -070032hidl_binder_death_recipient::hidl_binder_death_recipient(const sp<hidl_death_recipient> &recipient,
33 uint64_t cookie, const sp<::android::hidl::base::V1_0::IBase> &base) :
34 mRecipient(recipient), mCookie(cookie), mBase(base) {
35}
36
37void hidl_binder_death_recipient::binderDied(const wp<IBinder>& /*who*/) {
38 sp<hidl_death_recipient> recipient = mRecipient.promote();
Martijn Coenen25508802017-07-05 12:21:00 +020039 if (recipient != nullptr && mBase != nullptr) {
Steven Moreland108d09d2017-05-05 16:15:38 -070040 recipient->serviceDied(mCookie, mBase);
41 }
Martijn Coenen25508802017-07-05 12:21:00 +020042 mBase = nullptr;
Steven Moreland108d09d2017-05-05 16:15:38 -070043}
44
45wp<hidl_death_recipient> hidl_binder_death_recipient::getRecipient() {
46 return mRecipient;
47}
48
Martijn Coenen30791002016-12-01 15:40:46 +010049const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle);
50const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080051static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset");
52static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset");
Martijn Coenen30791002016-12-01 15:40:46 +010053
Martijn Coenen9d3eb352017-04-18 20:28:03 -070054status_t readEmbeddedFromParcel(const hidl_memory& memory,
Martijn Coenen30791002016-12-01 15:40:46 +010055 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -080056 const native_handle_t *handle;
57 ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
Martijn Coenen30791002016-12-01 15:40:46 +010058 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080059 parentOffset + hidl_memory::kOffsetOfHandle,
60 &handle);
Martijn Coenen30791002016-12-01 15:40:46 +010061
Steven Moreland1f535a22017-01-06 19:25:49 -080062 if (_hidl_err == ::android::OK) {
63 _hidl_err = readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -070064 memory.name(),
Steven Moreland1f535a22017-01-06 19:25:49 -080065 parcel,
66 parentHandle,
67 parentOffset + hidl_memory::kOffsetOfName);
Martijn Coenen30791002016-12-01 15:40:46 +010068 }
69
Steven Moreland3645dc32018-06-06 16:28:29 -070070 // hidl_memory's size is stored in uint64_t, but mapMemory's mmap will map
71 // size in size_t. If size is over SIZE_MAX, mapMemory could succeed
72 // but the mapped memory's actual size will be smaller than the reported size.
73 if (memory.size() > SIZE_MAX) {
74 ALOGE("Cannot use memory with %" PRId64 " bytes because it is too large.", memory.size());
75 android_errorWriteLog(0x534e4554, "79376389");
76 return BAD_VALUE;
77 }
78
Martijn Coenen30791002016-12-01 15:40:46 +010079 return _hidl_err;
80}
81
82status_t writeEmbeddedToParcel(const hidl_memory &memory,
83 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
84 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
85 memory.handle(),
86 parentHandle,
87 parentOffset + hidl_memory::kOffsetOfHandle);
88
89 if (_hidl_err == ::android::OK) {
90 _hidl_err = writeEmbeddedToParcel(
91 memory.name(),
92 parcel,
93 parentHandle,
94 parentOffset + hidl_memory::kOffsetOfName);
95 }
96
97 return _hidl_err;
98}
Yifan Hong7f97f442016-11-14 18:31:05 -080099const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800100static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset");
Yifan Hong7f97f442016-11-14 18:31:05 -0800101
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700102status_t readEmbeddedFromParcel(const hidl_string &string ,
Yifan Hong7f97f442016-11-14 18:31:05 -0800103 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -0800104 const void *out;
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700105
106 status_t status = parcel.readEmbeddedBuffer(
107 string.size() + 1,
Yifan Hong7f97f442016-11-14 18:31:05 -0800108 nullptr /* buffer_handle */,
109 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -0800110 parentOffset + hidl_string::kOffsetOfBuffer,
111 &out);
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700112
113 if (status != OK) {
114 return status;
115 }
116
117 // Always safe to access out[string.size()] because we read size+1 bytes
118 if (static_cast<const char *>(out)[string.size()] != '\0') {
119 ALOGE("Received unterminated hidl_string buffer.");
120 return BAD_VALUE;
121 }
122
123 return OK;
Yifan Hong7f97f442016-11-14 18:31:05 -0800124}
125
126status_t writeEmbeddedToParcel(const hidl_string &string,
127 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
128 return parcel->writeEmbeddedBuffer(
129 string.c_str(),
130 string.size() + 1,
131 nullptr /* handle */,
132 parentHandle,
133 parentOffset + hidl_string::kOffsetOfBuffer);
134}
135
136android::status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel) {
137 return parcel.writeUint32(static_cast<uint32_t>(version.get_major()) << 16 | version.get_minor());
138}
139
140hidl_version* readFromParcel(const android::hardware::Parcel& parcel) {
141 uint32_t version;
142 android::status_t status = parcel.readUint32(&version);
143 if (status != OK) {
144 return nullptr;
145 } else {
146 return new hidl_version(version >> 16, version & 0xFFFF);
147 }
148}
149
150status_t readFromParcel(Status *s, const Parcel& parcel) {
151 int32_t exception;
Yifan Hong7f97f442016-11-14 18:31:05 -0800152 status_t status = parcel.readInt32(&exception);
153 if (status != OK) {
154 s->setFromStatusT(status);
155 return status;
156 }
157
Yifan Hong7f97f442016-11-14 18:31:05 -0800158 if (exception == Status::EX_NONE) {
159 *s = Status::ok();
160 return status;
161 }
162
163 // The remote threw an exception. Get the message back.
164 String16 message;
165 status = parcel.readString16(&message);
166 if (status != OK) {
167 s->setFromStatusT(status);
168 return status;
169 }
170
Steven Moreland72db40f2017-03-09 18:15:27 -0800171 s->setException(exception, String8(message));
Yifan Hong7f97f442016-11-14 18:31:05 -0800172
173 return status;
174}
175
176status_t writeToParcel(const Status &s, Parcel* parcel) {
177 // Something really bad has happened, and we're not going to even
178 // try returning rich error data.
179 if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
180 return s.transactionError();
181 }
182
183 status_t status = parcel->writeInt32(s.exceptionCode());
184 if (status != OK) { return status; }
185 if (s.exceptionCode() == Status::EX_NONE) {
186 // We have no more information to write.
187 return status;
188 }
189 status = parcel->writeString16(String16(s.exceptionMessage()));
Yifan Hong7f97f442016-11-14 18:31:05 -0800190 return status;
191}
192
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800193void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
194 ProcessState::self()->setThreadPoolConfiguration(maxThreads, callerWillJoin /*callerJoinsPool*/);
195}
196
197void joinBinderRpcThreadpool() {
198 IPCThreadState::self()->joinThreadPool();
199}
200
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800201int setupBinderPolling() {
202 int fd;
203 int err = IPCThreadState::self()->setupPolling(&fd);
204
205 if (err != OK) {
206 ALOGE("Failed to setup binder polling: %d (%s)", err, strerror(err));
207 }
208
209 return err == OK ? fd : -1;
210}
211
212status_t handleBinderPoll() {
213 return IPCThreadState::self()->handlePolledCommands();
214}
215
Yifan Hong7f97f442016-11-14 18:31:05 -0800216} // namespace hardware
217} // namespace android