Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 1 | /* |
| 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 Hong | 777bef9 | 2017-02-01 15:50:36 -0800 | [diff] [blame] | 21 | // C includes |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | // C++ includes |
| 25 | #include <fstream> |
| 26 | #include <sstream> |
| 27 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace hardware { |
| 30 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 31 | const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle); |
| 32 | const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName); |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 33 | static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset"); |
| 34 | static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset"); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 35 | |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame^] | 36 | status_t readEmbeddedFromParcel(const hidl_memory& memory, |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 37 | const Parcel &parcel, size_t parentHandle, size_t parentOffset) { |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 38 | const native_handle_t *handle; |
| 39 | ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle( |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 40 | parentHandle, |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 41 | parentOffset + hidl_memory::kOffsetOfHandle, |
| 42 | &handle); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 43 | |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 44 | if (_hidl_err == ::android::OK) { |
| 45 | _hidl_err = readEmbeddedFromParcel( |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame^] | 46 | memory.name(), |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 47 | parcel, |
| 48 | parentHandle, |
| 49 | parentOffset + hidl_memory::kOffsetOfName); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 52 | return _hidl_err; |
| 53 | } |
| 54 | |
| 55 | status_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 Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 72 | // static |
| 73 | const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer); |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 74 | static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset"); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 75 | |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame^] | 76 | status_t readEmbeddedFromParcel(const hidl_string &string , |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 77 | const Parcel &parcel, size_t parentHandle, size_t parentOffset) { |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 78 | const void *out; |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame^] | 79 | |
| 80 | status_t status = parcel.readEmbeddedBuffer( |
| 81 | string.size() + 1, |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 82 | nullptr /* buffer_handle */, |
| 83 | parentHandle, |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 84 | parentOffset + hidl_string::kOffsetOfBuffer, |
| 85 | &out); |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame^] | 86 | |
| 87 | if (status != OK) { |
| 88 | return status; |
| 89 | } |
| 90 | |
| 91 | // Always safe to access out[string.size()] because we read size+1 bytes |
| 92 | if (static_cast<const char *>(out)[string.size()] != '\0') { |
| 93 | ALOGE("Received unterminated hidl_string buffer."); |
| 94 | return BAD_VALUE; |
| 95 | } |
| 96 | |
| 97 | return OK; |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | status_t writeEmbeddedToParcel(const hidl_string &string, |
| 101 | Parcel *parcel, size_t parentHandle, size_t parentOffset) { |
| 102 | return parcel->writeEmbeddedBuffer( |
| 103 | string.c_str(), |
| 104 | string.size() + 1, |
| 105 | nullptr /* handle */, |
| 106 | parentHandle, |
| 107 | parentOffset + hidl_string::kOffsetOfBuffer); |
| 108 | } |
| 109 | |
| 110 | android::status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel) { |
| 111 | return parcel.writeUint32(static_cast<uint32_t>(version.get_major()) << 16 | version.get_minor()); |
| 112 | } |
| 113 | |
| 114 | hidl_version* readFromParcel(const android::hardware::Parcel& parcel) { |
| 115 | uint32_t version; |
| 116 | android::status_t status = parcel.readUint32(&version); |
| 117 | if (status != OK) { |
| 118 | return nullptr; |
| 119 | } else { |
| 120 | return new hidl_version(version >> 16, version & 0xFFFF); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | status_t readFromParcel(Status *s, const Parcel& parcel) { |
| 125 | int32_t exception; |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 126 | status_t status = parcel.readInt32(&exception); |
| 127 | if (status != OK) { |
| 128 | s->setFromStatusT(status); |
| 129 | return status; |
| 130 | } |
| 131 | |
| 132 | // Skip over fat response headers. Not used (or propagated) in native code. |
| 133 | if (exception == Status::EX_HAS_REPLY_HEADER) { |
| 134 | // Note that the header size includes the 4 byte size field. |
| 135 | const int32_t header_start = parcel.dataPosition(); |
| 136 | int32_t header_size; |
| 137 | status = parcel.readInt32(&header_size); |
| 138 | if (status != OK) { |
| 139 | s->setFromStatusT(status); |
| 140 | return status; |
| 141 | } |
| 142 | parcel.setDataPosition(header_start + header_size); |
| 143 | // And fat response headers are currently only used when there are no |
| 144 | // exceptions, so act like there was no error. |
| 145 | exception = Status::EX_NONE; |
| 146 | } |
| 147 | |
| 148 | if (exception == Status::EX_NONE) { |
| 149 | *s = Status::ok(); |
| 150 | return status; |
| 151 | } |
| 152 | |
| 153 | // The remote threw an exception. Get the message back. |
| 154 | String16 message; |
| 155 | status = parcel.readString16(&message); |
| 156 | if (status != OK) { |
| 157 | s->setFromStatusT(status); |
| 158 | return status; |
| 159 | } |
| 160 | |
Steven Moreland | 72db40f | 2017-03-09 18:15:27 -0800 | [diff] [blame] | 161 | s->setException(exception, String8(message)); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 162 | |
| 163 | return status; |
| 164 | } |
| 165 | |
| 166 | status_t writeToParcel(const Status &s, Parcel* parcel) { |
| 167 | // Something really bad has happened, and we're not going to even |
| 168 | // try returning rich error data. |
| 169 | if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) { |
| 170 | return s.transactionError(); |
| 171 | } |
| 172 | |
| 173 | status_t status = parcel->writeInt32(s.exceptionCode()); |
| 174 | if (status != OK) { return status; } |
| 175 | if (s.exceptionCode() == Status::EX_NONE) { |
| 176 | // We have no more information to write. |
| 177 | return status; |
| 178 | } |
| 179 | status = parcel->writeString16(String16(s.exceptionMessage())); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 180 | return status; |
| 181 | } |
| 182 | |
Steven Moreland | 6cf8fa2 | 2017-02-21 13:38:17 -0800 | [diff] [blame] | 183 | void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) { |
| 184 | ProcessState::self()->setThreadPoolConfiguration(maxThreads, callerWillJoin /*callerJoinsPool*/); |
| 185 | } |
| 186 | |
| 187 | void joinBinderRpcThreadpool() { |
| 188 | IPCThreadState::self()->joinThreadPool(); |
| 189 | } |
| 190 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 191 | } // namespace hardware |
| 192 | } // namespace android |