blob: 55f9a91d50c4ae29c6fd67c65ba2c43669d65cff [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
Martijn Coenen30791002016-12-01 15:40:46 +010031const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle);
32const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080033static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset");
34static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset");
Martijn Coenen30791002016-12-01 15:40:46 +010035
36status_t readEmbeddedFromParcel(hidl_memory * /* memory */,
37 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -080038 const native_handle_t *handle;
39 ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
Martijn Coenen30791002016-12-01 15:40:46 +010040 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080041 parentOffset + hidl_memory::kOffsetOfHandle,
42 &handle);
Martijn Coenen30791002016-12-01 15:40:46 +010043
Steven Moreland1f535a22017-01-06 19:25:49 -080044 if (_hidl_err == ::android::OK) {
45 _hidl_err = readEmbeddedFromParcel(
46 (hidl_string*) nullptr,
47 parcel,
48 parentHandle,
49 parentOffset + hidl_memory::kOffsetOfName);
Martijn Coenen30791002016-12-01 15:40:46 +010050 }
51
Martijn Coenen30791002016-12-01 15:40:46 +010052 return _hidl_err;
53}
54
55status_t writeEmbeddedToParcel(const hidl_memory &memory,
56 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
57 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
58 memory.handle(),
59 parentHandle,
60 parentOffset + hidl_memory::kOffsetOfHandle);
61
62 if (_hidl_err == ::android::OK) {
63 _hidl_err = writeEmbeddedToParcel(
64 memory.name(),
65 parcel,
66 parentHandle,
67 parentOffset + hidl_memory::kOffsetOfName);
68 }
69
70 return _hidl_err;
71}
Yifan Hong7f97f442016-11-14 18:31:05 -080072// static
73const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080074static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset");
Yifan Hong7f97f442016-11-14 18:31:05 -080075
76status_t readEmbeddedFromParcel(hidl_string * /* string */,
77 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -080078 const void *out;
79 return parcel.readEmbeddedBuffer(
Yifan Hong7f97f442016-11-14 18:31:05 -080080 nullptr /* buffer_handle */,
81 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080082 parentOffset + hidl_string::kOffsetOfBuffer,
83 &out);
Yifan Hong7f97f442016-11-14 18:31:05 -080084}
85
86status_t writeEmbeddedToParcel(const hidl_string &string,
87 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
88 return parcel->writeEmbeddedBuffer(
89 string.c_str(),
90 string.size() + 1,
91 nullptr /* handle */,
92 parentHandle,
93 parentOffset + hidl_string::kOffsetOfBuffer);
94}
95
96android::status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel) {
97 return parcel.writeUint32(static_cast<uint32_t>(version.get_major()) << 16 | version.get_minor());
98}
99
100hidl_version* readFromParcel(const android::hardware::Parcel& parcel) {
101 uint32_t version;
102 android::status_t status = parcel.readUint32(&version);
103 if (status != OK) {
104 return nullptr;
105 } else {
106 return new hidl_version(version >> 16, version & 0xFFFF);
107 }
108}
109
110status_t readFromParcel(Status *s, const Parcel& parcel) {
111 int32_t exception;
112 int32_t errorCode;
113 status_t status = parcel.readInt32(&exception);
114 if (status != OK) {
115 s->setFromStatusT(status);
116 return status;
117 }
118
119 // Skip over fat response headers. Not used (or propagated) in native code.
120 if (exception == Status::EX_HAS_REPLY_HEADER) {
121 // Note that the header size includes the 4 byte size field.
122 const int32_t header_start = parcel.dataPosition();
123 int32_t header_size;
124 status = parcel.readInt32(&header_size);
125 if (status != OK) {
126 s->setFromStatusT(status);
127 return status;
128 }
129 parcel.setDataPosition(header_start + header_size);
130 // And fat response headers are currently only used when there are no
131 // exceptions, so act like there was no error.
132 exception = Status::EX_NONE;
133 }
134
135 if (exception == Status::EX_NONE) {
136 *s = Status::ok();
137 return status;
138 }
139
140 // The remote threw an exception. Get the message back.
141 String16 message;
142 status = parcel.readString16(&message);
143 if (status != OK) {
144 s->setFromStatusT(status);
145 return status;
146 }
147
148 if (exception == Status::EX_SERVICE_SPECIFIC) {
149 status = parcel.readInt32(&errorCode);
150 }
151 if (status != OK) {
152 s->setFromStatusT(status);
153 return status;
154 }
155
156 if (exception == Status::EX_SERVICE_SPECIFIC) {
157 s->setServiceSpecificError(errorCode, String8(message));
158 } else {
159 s->setException(exception, String8(message));
160 }
161
162 return status;
163}
164
165status_t writeToParcel(const Status &s, Parcel* parcel) {
166 // Something really bad has happened, and we're not going to even
167 // try returning rich error data.
168 if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
169 return s.transactionError();
170 }
171
172 status_t status = parcel->writeInt32(s.exceptionCode());
173 if (status != OK) { return status; }
174 if (s.exceptionCode() == Status::EX_NONE) {
175 // We have no more information to write.
176 return status;
177 }
178 status = parcel->writeString16(String16(s.exceptionMessage()));
179 if (s.exceptionCode() != Status::EX_SERVICE_SPECIFIC) {
180 // We have no more information to write.
181 return status;
182 }
183 status = parcel->writeInt32(s.serviceSpecificErrorCode());
184 return status;
185}
186
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800187void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
188 ProcessState::self()->setThreadPoolConfiguration(maxThreads, callerWillJoin /*callerJoinsPool*/);
189}
190
191void joinBinderRpcThreadpool() {
192 IPCThreadState::self()->joinThreadPool();
193}
194
Yifan Hong7f97f442016-11-14 18:31:05 -0800195} // namespace hardware
196} // namespace android