blob: c0601cadefd30ae853efe45c359e60bd722950ec [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;
Yifan Hong7f97f442016-11-14 18:31:05 -0800112 status_t status = parcel.readInt32(&exception);
113 if (status != OK) {
114 s->setFromStatusT(status);
115 return status;
116 }
117
118 // Skip over fat response headers. Not used (or propagated) in native code.
119 if (exception == Status::EX_HAS_REPLY_HEADER) {
120 // Note that the header size includes the 4 byte size field.
121 const int32_t header_start = parcel.dataPosition();
122 int32_t header_size;
123 status = parcel.readInt32(&header_size);
124 if (status != OK) {
125 s->setFromStatusT(status);
126 return status;
127 }
128 parcel.setDataPosition(header_start + header_size);
129 // And fat response headers are currently only used when there are no
130 // exceptions, so act like there was no error.
131 exception = Status::EX_NONE;
132 }
133
134 if (exception == Status::EX_NONE) {
135 *s = Status::ok();
136 return status;
137 }
138
139 // The remote threw an exception. Get the message back.
140 String16 message;
141 status = parcel.readString16(&message);
142 if (status != OK) {
143 s->setFromStatusT(status);
144 return status;
145 }
146
Steven Moreland72db40f2017-03-09 18:15:27 -0800147 s->setException(exception, String8(message));
Yifan Hong7f97f442016-11-14 18:31:05 -0800148
149 return status;
150}
151
152status_t writeToParcel(const Status &s, Parcel* parcel) {
153 // Something really bad has happened, and we're not going to even
154 // try returning rich error data.
155 if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
156 return s.transactionError();
157 }
158
159 status_t status = parcel->writeInt32(s.exceptionCode());
160 if (status != OK) { return status; }
161 if (s.exceptionCode() == Status::EX_NONE) {
162 // We have no more information to write.
163 return status;
164 }
165 status = parcel->writeString16(String16(s.exceptionMessage()));
Yifan Hong7f97f442016-11-14 18:31:05 -0800166 return status;
167}
168
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800169void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
170 ProcessState::self()->setThreadPoolConfiguration(maxThreads, callerWillJoin /*callerJoinsPool*/);
171}
172
173void joinBinderRpcThreadpool() {
174 IPCThreadState::self()->joinThreadPool();
175}
176
Yifan Hong7f97f442016-11-14 18:31:05 -0800177} // namespace hardware
178} // namespace android