blob: d2935427584e72646f055fd12ab5983edf08a6dd [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
22#include <unistd.h>
23
24// C++ includes
25#include <fstream>
26#include <sstream>
27
Yifan Hong7f97f442016-11-14 18:31:05 -080028namespace android {
29namespace hardware {
30
Steven Moreland108d09d2017-05-05 16:15:38 -070031hidl_binder_death_recipient::hidl_binder_death_recipient(const sp<hidl_death_recipient> &recipient,
32 uint64_t cookie, const sp<::android::hidl::base::V1_0::IBase> &base) :
33 mRecipient(recipient), mCookie(cookie), mBase(base) {
34}
35
36void hidl_binder_death_recipient::binderDied(const wp<IBinder>& /*who*/) {
37 sp<hidl_death_recipient> recipient = mRecipient.promote();
38 if (recipient != nullptr) {
39 recipient->serviceDied(mCookie, mBase);
40 }
41}
42
43wp<hidl_death_recipient> hidl_binder_death_recipient::getRecipient() {
44 return mRecipient;
45}
46
Martijn Coenen30791002016-12-01 15:40:46 +010047const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle);
48const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080049static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset");
50static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset");
Martijn Coenen30791002016-12-01 15:40:46 +010051
Martijn Coenen9d3eb352017-04-18 20:28:03 -070052status_t readEmbeddedFromParcel(const hidl_memory& memory,
Martijn Coenen30791002016-12-01 15:40:46 +010053 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -080054 const native_handle_t *handle;
55 ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
Martijn Coenen30791002016-12-01 15:40:46 +010056 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080057 parentOffset + hidl_memory::kOffsetOfHandle,
58 &handle);
Martijn Coenen30791002016-12-01 15:40:46 +010059
Steven Moreland1f535a22017-01-06 19:25:49 -080060 if (_hidl_err == ::android::OK) {
61 _hidl_err = readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -070062 memory.name(),
Steven Moreland1f535a22017-01-06 19:25:49 -080063 parcel,
64 parentHandle,
65 parentOffset + hidl_memory::kOffsetOfName);
Martijn Coenen30791002016-12-01 15:40:46 +010066 }
67
Martijn Coenen30791002016-12-01 15:40:46 +010068 return _hidl_err;
69}
70
71status_t writeEmbeddedToParcel(const hidl_memory &memory,
72 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
73 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
74 memory.handle(),
75 parentHandle,
76 parentOffset + hidl_memory::kOffsetOfHandle);
77
78 if (_hidl_err == ::android::OK) {
79 _hidl_err = writeEmbeddedToParcel(
80 memory.name(),
81 parcel,
82 parentHandle,
83 parentOffset + hidl_memory::kOffsetOfName);
84 }
85
86 return _hidl_err;
87}
Yifan Hong7f97f442016-11-14 18:31:05 -080088const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080089static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset");
Yifan Hong7f97f442016-11-14 18:31:05 -080090
Martijn Coenen9d3eb352017-04-18 20:28:03 -070091status_t readEmbeddedFromParcel(const hidl_string &string ,
Yifan Hong7f97f442016-11-14 18:31:05 -080092 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -080093 const void *out;
Martijn Coenen9d3eb352017-04-18 20:28:03 -070094
95 status_t status = parcel.readEmbeddedBuffer(
96 string.size() + 1,
Yifan Hong7f97f442016-11-14 18:31:05 -080097 nullptr /* buffer_handle */,
98 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080099 parentOffset + hidl_string::kOffsetOfBuffer,
100 &out);
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700101
102 if (status != OK) {
103 return status;
104 }
105
106 // Always safe to access out[string.size()] because we read size+1 bytes
107 if (static_cast<const char *>(out)[string.size()] != '\0') {
108 ALOGE("Received unterminated hidl_string buffer.");
109 return BAD_VALUE;
110 }
111
112 return OK;
Yifan Hong7f97f442016-11-14 18:31:05 -0800113}
114
115status_t writeEmbeddedToParcel(const hidl_string &string,
116 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
117 return parcel->writeEmbeddedBuffer(
118 string.c_str(),
119 string.size() + 1,
120 nullptr /* handle */,
121 parentHandle,
122 parentOffset + hidl_string::kOffsetOfBuffer);
123}
124
125android::status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel) {
126 return parcel.writeUint32(static_cast<uint32_t>(version.get_major()) << 16 | version.get_minor());
127}
128
129hidl_version* readFromParcel(const android::hardware::Parcel& parcel) {
130 uint32_t version;
131 android::status_t status = parcel.readUint32(&version);
132 if (status != OK) {
133 return nullptr;
134 } else {
135 return new hidl_version(version >> 16, version & 0xFFFF);
136 }
137}
138
139status_t readFromParcel(Status *s, const Parcel& parcel) {
140 int32_t exception;
Yifan Hong7f97f442016-11-14 18:31:05 -0800141 status_t status = parcel.readInt32(&exception);
142 if (status != OK) {
143 s->setFromStatusT(status);
144 return status;
145 }
146
147 // Skip over fat response headers. Not used (or propagated) in native code.
148 if (exception == Status::EX_HAS_REPLY_HEADER) {
149 // Note that the header size includes the 4 byte size field.
150 const int32_t header_start = parcel.dataPosition();
151 int32_t header_size;
152 status = parcel.readInt32(&header_size);
153 if (status != OK) {
154 s->setFromStatusT(status);
155 return status;
156 }
157 parcel.setDataPosition(header_start + header_size);
158 // And fat response headers are currently only used when there are no
159 // exceptions, so act like there was no error.
160 exception = Status::EX_NONE;
161 }
162
163 if (exception == Status::EX_NONE) {
164 *s = Status::ok();
165 return status;
166 }
167
168 // The remote threw an exception. Get the message back.
169 String16 message;
170 status = parcel.readString16(&message);
171 if (status != OK) {
172 s->setFromStatusT(status);
173 return status;
174 }
175
Steven Moreland72db40f2017-03-09 18:15:27 -0800176 s->setException(exception, String8(message));
Yifan Hong7f97f442016-11-14 18:31:05 -0800177
178 return status;
179}
180
181status_t writeToParcel(const Status &s, Parcel* parcel) {
182 // Something really bad has happened, and we're not going to even
183 // try returning rich error data.
184 if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
185 return s.transactionError();
186 }
187
188 status_t status = parcel->writeInt32(s.exceptionCode());
189 if (status != OK) { return status; }
190 if (s.exceptionCode() == Status::EX_NONE) {
191 // We have no more information to write.
192 return status;
193 }
194 status = parcel->writeString16(String16(s.exceptionMessage()));
Yifan Hong7f97f442016-11-14 18:31:05 -0800195 return status;
196}
197
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800198void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
199 ProcessState::self()->setThreadPoolConfiguration(maxThreads, callerWillJoin /*callerJoinsPool*/);
200}
201
202void joinBinderRpcThreadpool() {
203 IPCThreadState::self()->joinThreadPool();
204}
205
Yifan Hong7f97f442016-11-14 18:31:05 -0800206} // namespace hardware
207} // namespace android