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 | |
Steven Moreland | 108d09d | 2017-05-05 16:15:38 -0700 | [diff] [blame] | 31 | hidl_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 | |
| 36 | void hidl_binder_death_recipient::binderDied(const wp<IBinder>& /*who*/) { |
| 37 | sp<hidl_death_recipient> recipient = mRecipient.promote(); |
Martijn Coenen | 2550880 | 2017-07-05 12:21:00 +0200 | [diff] [blame] | 38 | if (recipient != nullptr && mBase != nullptr) { |
Steven Moreland | 108d09d | 2017-05-05 16:15:38 -0700 | [diff] [blame] | 39 | recipient->serviceDied(mCookie, mBase); |
| 40 | } |
Martijn Coenen | 2550880 | 2017-07-05 12:21:00 +0200 | [diff] [blame] | 41 | mBase = nullptr; |
Steven Moreland | 108d09d | 2017-05-05 16:15:38 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | wp<hidl_death_recipient> hidl_binder_death_recipient::getRecipient() { |
| 45 | return mRecipient; |
| 46 | } |
| 47 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 48 | const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle); |
| 49 | const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName); |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 50 | static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset"); |
| 51 | static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset"); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 52 | |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 53 | status_t readEmbeddedFromParcel(const hidl_memory& memory, |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 54 | const Parcel &parcel, size_t parentHandle, size_t parentOffset) { |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 55 | const native_handle_t *handle; |
| 56 | ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle( |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 57 | parentHandle, |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 58 | parentOffset + hidl_memory::kOffsetOfHandle, |
| 59 | &handle); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 60 | |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 61 | if (_hidl_err == ::android::OK) { |
| 62 | _hidl_err = readEmbeddedFromParcel( |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 63 | memory.name(), |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 64 | parcel, |
| 65 | parentHandle, |
| 66 | parentOffset + hidl_memory::kOffsetOfName); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 69 | return _hidl_err; |
| 70 | } |
| 71 | |
| 72 | status_t writeEmbeddedToParcel(const hidl_memory &memory, |
| 73 | Parcel *parcel, size_t parentHandle, size_t parentOffset) { |
| 74 | status_t _hidl_err = parcel->writeEmbeddedNativeHandle( |
| 75 | memory.handle(), |
| 76 | parentHandle, |
| 77 | parentOffset + hidl_memory::kOffsetOfHandle); |
| 78 | |
| 79 | if (_hidl_err == ::android::OK) { |
| 80 | _hidl_err = writeEmbeddedToParcel( |
| 81 | memory.name(), |
| 82 | parcel, |
| 83 | parentHandle, |
| 84 | parentOffset + hidl_memory::kOffsetOfName); |
| 85 | } |
| 86 | |
| 87 | return _hidl_err; |
| 88 | } |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 89 | const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer); |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 90 | static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset"); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 91 | |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 92 | status_t readEmbeddedFromParcel(const hidl_string &string , |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 93 | const Parcel &parcel, size_t parentHandle, size_t parentOffset) { |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 94 | const void *out; |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 95 | |
| 96 | status_t status = parcel.readEmbeddedBuffer( |
| 97 | string.size() + 1, |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 98 | nullptr /* buffer_handle */, |
| 99 | parentHandle, |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 100 | parentOffset + hidl_string::kOffsetOfBuffer, |
| 101 | &out); |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 102 | |
| 103 | if (status != OK) { |
| 104 | return status; |
| 105 | } |
| 106 | |
| 107 | // Always safe to access out[string.size()] because we read size+1 bytes |
| 108 | if (static_cast<const char *>(out)[string.size()] != '\0') { |
| 109 | ALOGE("Received unterminated hidl_string buffer."); |
| 110 | return BAD_VALUE; |
| 111 | } |
| 112 | |
| 113 | return OK; |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | status_t writeEmbeddedToParcel(const hidl_string &string, |
| 117 | Parcel *parcel, size_t parentHandle, size_t parentOffset) { |
| 118 | return parcel->writeEmbeddedBuffer( |
| 119 | string.c_str(), |
| 120 | string.size() + 1, |
| 121 | nullptr /* handle */, |
| 122 | parentHandle, |
| 123 | parentOffset + hidl_string::kOffsetOfBuffer); |
| 124 | } |
| 125 | |
| 126 | android::status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel) { |
| 127 | return parcel.writeUint32(static_cast<uint32_t>(version.get_major()) << 16 | version.get_minor()); |
| 128 | } |
| 129 | |
| 130 | hidl_version* readFromParcel(const android::hardware::Parcel& parcel) { |
| 131 | uint32_t version; |
| 132 | android::status_t status = parcel.readUint32(&version); |
| 133 | if (status != OK) { |
| 134 | return nullptr; |
| 135 | } else { |
| 136 | return new hidl_version(version >> 16, version & 0xFFFF); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | status_t readFromParcel(Status *s, const Parcel& parcel) { |
| 141 | int32_t exception; |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 142 | status_t status = parcel.readInt32(&exception); |
| 143 | if (status != OK) { |
| 144 | s->setFromStatusT(status); |
| 145 | return status; |
| 146 | } |
| 147 | |
| 148 | // Skip over fat response headers. Not used (or propagated) in native code. |
| 149 | if (exception == Status::EX_HAS_REPLY_HEADER) { |
| 150 | // Note that the header size includes the 4 byte size field. |
| 151 | const int32_t header_start = parcel.dataPosition(); |
| 152 | int32_t header_size; |
| 153 | status = parcel.readInt32(&header_size); |
| 154 | if (status != OK) { |
| 155 | s->setFromStatusT(status); |
| 156 | return status; |
| 157 | } |
| 158 | parcel.setDataPosition(header_start + header_size); |
| 159 | // And fat response headers are currently only used when there are no |
| 160 | // exceptions, so act like there was no error. |
| 161 | exception = Status::EX_NONE; |
| 162 | } |
| 163 | |
| 164 | if (exception == Status::EX_NONE) { |
| 165 | *s = Status::ok(); |
| 166 | return status; |
| 167 | } |
| 168 | |
| 169 | // The remote threw an exception. Get the message back. |
| 170 | String16 message; |
| 171 | status = parcel.readString16(&message); |
| 172 | if (status != OK) { |
| 173 | s->setFromStatusT(status); |
| 174 | return status; |
| 175 | } |
| 176 | |
Steven Moreland | 72db40f | 2017-03-09 18:15:27 -0800 | [diff] [blame] | 177 | s->setException(exception, String8(message)); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 178 | |
| 179 | return status; |
| 180 | } |
| 181 | |
| 182 | status_t writeToParcel(const Status &s, Parcel* parcel) { |
| 183 | // Something really bad has happened, and we're not going to even |
| 184 | // try returning rich error data. |
| 185 | if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) { |
| 186 | return s.transactionError(); |
| 187 | } |
| 188 | |
| 189 | status_t status = parcel->writeInt32(s.exceptionCode()); |
| 190 | if (status != OK) { return status; } |
| 191 | if (s.exceptionCode() == Status::EX_NONE) { |
| 192 | // We have no more information to write. |
| 193 | return status; |
| 194 | } |
| 195 | status = parcel->writeString16(String16(s.exceptionMessage())); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 196 | return status; |
| 197 | } |
| 198 | |
Steven Moreland | 6cf8fa2 | 2017-02-21 13:38:17 -0800 | [diff] [blame] | 199 | void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) { |
| 200 | ProcessState::self()->setThreadPoolConfiguration(maxThreads, callerWillJoin /*callerJoinsPool*/); |
| 201 | } |
| 202 | |
| 203 | void joinBinderRpcThreadpool() { |
| 204 | IPCThreadState::self()->joinThreadPool(); |
| 205 | } |
| 206 | |
Martijn Coenen | 3f5ac4c | 2017-11-27 15:09:28 -0800 | [diff] [blame] | 207 | int setupBinderPolling() { |
| 208 | int fd; |
| 209 | int err = IPCThreadState::self()->setupPolling(&fd); |
| 210 | |
| 211 | if (err != OK) { |
| 212 | ALOGE("Failed to setup binder polling: %d (%s)", err, strerror(err)); |
| 213 | } |
| 214 | |
| 215 | return err == OK ? fd : -1; |
| 216 | } |
| 217 | |
| 218 | status_t handleBinderPoll() { |
| 219 | return IPCThreadState::self()->handlePolledCommands(); |
| 220 | } |
| 221 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 222 | } // namespace hardware |
| 223 | } // namespace android |