Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include <binder/Status.h> |
| 18 | |
| 19 | namespace android { |
| 20 | namespace binder { |
| 21 | |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 22 | Status Status::ok() { |
| 23 | return Status(); |
| 24 | } |
| 25 | |
| 26 | Status Status::fromExceptionCode(int32_t exceptionCode) { |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 27 | return Status(exceptionCode, OK); |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | Status Status::fromExceptionCode(int32_t exceptionCode, |
| 31 | const String8& message) { |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 32 | return Status(exceptionCode, OK, message); |
| 33 | } |
| 34 | |
Christopher Wiley | 1651ced | 2016-05-06 09:19:44 -0700 | [diff] [blame] | 35 | Status Status::fromExceptionCode(int32_t exceptionCode, |
| 36 | const char* message) { |
| 37 | return fromExceptionCode(exceptionCode, String8(message)); |
| 38 | } |
| 39 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 40 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) { |
| 41 | return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode); |
| 42 | } |
| 43 | |
| 44 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode, |
| 45 | const String8& message) { |
| 46 | return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Christopher Wiley | 1651ced | 2016-05-06 09:19:44 -0700 | [diff] [blame] | 49 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode, |
| 50 | const char* message) { |
| 51 | return fromServiceSpecificError(serviceSpecificErrorCode, String8(message)); |
| 52 | } |
| 53 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 54 | Status Status::fromStatusT(status_t status) { |
| 55 | Status ret; |
| 56 | ret.setFromStatusT(status); |
| 57 | return ret; |
| 58 | } |
| 59 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 60 | Status::Status(int32_t exceptionCode, int32_t errorCode) |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 61 | : mException(exceptionCode), |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 62 | mErrorCode(errorCode) {} |
| 63 | |
| 64 | Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message) |
| 65 | : mException(exceptionCode), |
| 66 | mErrorCode(errorCode), |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 67 | mMessage(message) {} |
| 68 | |
| 69 | status_t Status::readFromParcel(const Parcel& parcel) { |
| 70 | status_t status = parcel.readInt32(&mException); |
| 71 | if (status != OK) { |
| 72 | setFromStatusT(status); |
| 73 | return status; |
| 74 | } |
| 75 | |
| 76 | // Skip over fat response headers. Not used (or propagated) in native code. |
| 77 | if (mException == EX_HAS_REPLY_HEADER) { |
| 78 | // Note that the header size includes the 4 byte size field. |
Steven Moreland | 0038364 | 2019-05-20 15:28:13 -0700 | [diff] [blame^] | 79 | const size_t header_start = parcel.dataPosition(); |
| 80 | // Get available size before reading more |
| 81 | const size_t header_avail = parcel.dataAvail(); |
| 82 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 83 | int32_t header_size; |
| 84 | status = parcel.readInt32(&header_size); |
| 85 | if (status != OK) { |
| 86 | setFromStatusT(status); |
| 87 | return status; |
| 88 | } |
Steven Moreland | 0038364 | 2019-05-20 15:28:13 -0700 | [diff] [blame^] | 89 | |
| 90 | if (header_size < 0 || static_cast<size_t>(header_size) > header_avail) { |
| 91 | android_errorWriteLog(0x534e4554, "132650049"); |
| 92 | setFromStatusT(UNKNOWN_ERROR); |
| 93 | return UNKNOWN_ERROR; |
| 94 | } |
| 95 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 96 | parcel.setDataPosition(header_start + header_size); |
| 97 | // And fat response headers are currently only used when there are no |
| 98 | // exceptions, so act like there was no error. |
| 99 | mException = EX_NONE; |
| 100 | } |
| 101 | |
| 102 | if (mException == EX_NONE) { |
| 103 | return status; |
| 104 | } |
| 105 | |
| 106 | // The remote threw an exception. Get the message back. |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 107 | String16 message; |
| 108 | status = parcel.readString16(&message); |
| 109 | if (status != OK) { |
| 110 | setFromStatusT(status); |
| 111 | return status; |
| 112 | } |
| 113 | mMessage = String8(message); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 114 | |
Fyodor Kupolov | d9ac374 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 115 | // Skip over the remote stack trace data |
| 116 | int32_t remote_stack_trace_header_size; |
| 117 | status = parcel.readInt32(&remote_stack_trace_header_size); |
| 118 | if (status != OK) { |
| 119 | setFromStatusT(status); |
| 120 | return status; |
| 121 | } |
Steven Moreland | 0038364 | 2019-05-20 15:28:13 -0700 | [diff] [blame^] | 122 | if (remote_stack_trace_header_size < 0 || |
| 123 | static_cast<size_t>(remote_stack_trace_header_size) > parcel.dataAvail()) { |
| 124 | |
| 125 | android_errorWriteLog(0x534e4554, "132650049"); |
| 126 | setFromStatusT(UNKNOWN_ERROR); |
| 127 | return UNKNOWN_ERROR; |
| 128 | } |
Fyodor Kupolov | d9ac374 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 129 | parcel.setDataPosition(parcel.dataPosition() + remote_stack_trace_header_size); |
| 130 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 131 | if (mException == EX_SERVICE_SPECIFIC) { |
| 132 | status = parcel.readInt32(&mErrorCode); |
Jeff Sharkey | c8dc4f0 | 2017-01-17 13:53:04 -0700 | [diff] [blame] | 133 | } else if (mException == EX_PARCELABLE) { |
| 134 | // Skip over the blob of Parcelable data |
Steven Moreland | 0038364 | 2019-05-20 15:28:13 -0700 | [diff] [blame^] | 135 | const size_t header_start = parcel.dataPosition(); |
| 136 | // Get available size before reading more |
| 137 | const size_t header_avail = parcel.dataAvail(); |
| 138 | |
Jeff Sharkey | c8dc4f0 | 2017-01-17 13:53:04 -0700 | [diff] [blame] | 139 | int32_t header_size; |
| 140 | status = parcel.readInt32(&header_size); |
| 141 | if (status != OK) { |
| 142 | setFromStatusT(status); |
| 143 | return status; |
| 144 | } |
Steven Moreland | 0038364 | 2019-05-20 15:28:13 -0700 | [diff] [blame^] | 145 | |
| 146 | if (header_size < 0 || static_cast<size_t>(header_size) > header_avail) { |
| 147 | android_errorWriteLog(0x534e4554, "132650049"); |
| 148 | setFromStatusT(UNKNOWN_ERROR); |
| 149 | return UNKNOWN_ERROR; |
| 150 | } |
| 151 | |
Jeff Sharkey | c8dc4f0 | 2017-01-17 13:53:04 -0700 | [diff] [blame] | 152 | parcel.setDataPosition(header_start + header_size); |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 153 | } |
| 154 | if (status != OK) { |
| 155 | setFromStatusT(status); |
| 156 | return status; |
| 157 | } |
| 158 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 159 | return status; |
| 160 | } |
| 161 | |
| 162 | status_t Status::writeToParcel(Parcel* parcel) const { |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 163 | // Something really bad has happened, and we're not going to even |
| 164 | // try returning rich error data. |
| 165 | if (mException == EX_TRANSACTION_FAILED) { |
| 166 | return mErrorCode; |
| 167 | } |
| 168 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 169 | status_t status = parcel->writeInt32(mException); |
| 170 | if (status != OK) { return status; } |
| 171 | if (mException == EX_NONE) { |
| 172 | // We have no more information to write. |
| 173 | return status; |
| 174 | } |
| 175 | status = parcel->writeString16(String16(mMessage)); |
Fyodor Kupolov | d9ac374 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 176 | status = parcel->writeInt32(0); // Empty remote stack trace header |
Jeff Sharkey | c8dc4f0 | 2017-01-17 13:53:04 -0700 | [diff] [blame] | 177 | if (mException == EX_SERVICE_SPECIFIC) { |
| 178 | status = parcel->writeInt32(mErrorCode); |
| 179 | } else if (mException == EX_PARCELABLE) { |
| 180 | // Sending Parcelable blobs currently not supported |
| 181 | status = parcel->writeInt32(0); |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 182 | } |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 183 | return status; |
| 184 | } |
| 185 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 186 | void Status::setException(int32_t ex, const String8& message) { |
| 187 | mException = ex; |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 188 | mErrorCode = NO_ERROR; // an exception, not a transaction failure. |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 189 | mMessage.setTo(message); |
| 190 | } |
| 191 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 192 | void Status::setServiceSpecificError(int32_t errorCode, const String8& message) { |
| 193 | setException(EX_SERVICE_SPECIFIC, message); |
| 194 | mErrorCode = errorCode; |
| 195 | } |
| 196 | |
| 197 | void Status::setFromStatusT(status_t status) { |
| 198 | mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED; |
| 199 | mErrorCode = status; |
| 200 | mMessage.clear(); |
| 201 | } |
| 202 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 203 | String8 Status::toString8() const { |
| 204 | String8 ret; |
| 205 | if (mException == EX_NONE) { |
| 206 | ret.append("No error"); |
| 207 | } else { |
| 208 | ret.appendFormat("Status(%d): '", mException); |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 209 | if (mException == EX_SERVICE_SPECIFIC || |
| 210 | mException == EX_TRANSACTION_FAILED) { |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 211 | ret.appendFormat("%d: ", mErrorCode); |
| 212 | } |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 213 | ret.append(String8(mMessage)); |
| 214 | ret.append("'"); |
| 215 | } |
| 216 | return ret; |
| 217 | } |
| 218 | |
Ralph Nathan | 00cb980 | 2016-04-13 12:42:06 -0700 | [diff] [blame] | 219 | std::stringstream& operator<< (std::stringstream& stream, const Status& s) { |
| 220 | stream << s.toString8().string(); |
| 221 | return stream; |
| 222 | } |
| 223 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 224 | } // namespace binder |
| 225 | } // namespace android |