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) { |
| 27 | return Status(exceptionCode); |
| 28 | } |
| 29 | |
| 30 | Status Status::fromExceptionCode(int32_t exceptionCode, |
| 31 | const String8& message) { |
| 32 | return Status(exceptionCode, message); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Status Status::fromStatusT(status_t status) { |
| 36 | Status ret; |
| 37 | ret.setFromStatusT(status); |
| 38 | return ret; |
| 39 | } |
| 40 | |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 41 | Status::Status(int32_t exceptionCode) : mException(exceptionCode) {} |
| 42 | Status::Status(int32_t exceptionCode, const String8& message) |
| 43 | : mException(exceptionCode), |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 44 | mMessage(message) {} |
| 45 | |
| 46 | status_t Status::readFromParcel(const Parcel& parcel) { |
| 47 | status_t status = parcel.readInt32(&mException); |
| 48 | if (status != OK) { |
| 49 | setFromStatusT(status); |
| 50 | return status; |
| 51 | } |
| 52 | |
| 53 | // Skip over fat response headers. Not used (or propagated) in native code. |
| 54 | if (mException == EX_HAS_REPLY_HEADER) { |
| 55 | // Note that the header size includes the 4 byte size field. |
| 56 | const int32_t header_start = parcel.dataPosition(); |
| 57 | int32_t header_size; |
| 58 | status = parcel.readInt32(&header_size); |
| 59 | if (status != OK) { |
| 60 | setFromStatusT(status); |
| 61 | return status; |
| 62 | } |
| 63 | parcel.setDataPosition(header_start + header_size); |
| 64 | // And fat response headers are currently only used when there are no |
| 65 | // exceptions, so act like there was no error. |
| 66 | mException = EX_NONE; |
| 67 | } |
| 68 | |
| 69 | if (mException == EX_NONE) { |
| 70 | return status; |
| 71 | } |
| 72 | |
| 73 | // The remote threw an exception. Get the message back. |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 74 | String16 message; |
| 75 | status = parcel.readString16(&message); |
| 76 | if (status != OK) { |
| 77 | setFromStatusT(status); |
| 78 | return status; |
| 79 | } |
| 80 | mMessage = String8(message); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 81 | |
| 82 | return status; |
| 83 | } |
| 84 | |
| 85 | status_t Status::writeToParcel(Parcel* parcel) const { |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 86 | // Something really bad has happened, and we're not going to even |
| 87 | // try returning rich error data. |
| 88 | if (mException == EX_TRANSACTION_FAILED) { |
| 89 | return mErrorCode; |
| 90 | } |
| 91 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 92 | status_t status = parcel->writeInt32(mException); |
| 93 | if (status != OK) { return status; } |
| 94 | if (mException == EX_NONE) { |
| 95 | // We have no more information to write. |
| 96 | return status; |
| 97 | } |
| 98 | status = parcel->writeString16(String16(mMessage)); |
| 99 | return status; |
| 100 | } |
| 101 | |
| 102 | void Status::setFromStatusT(status_t status) { |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 103 | mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED; |
| 104 | mErrorCode = status; |
| 105 | mMessage.clear(); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void Status::setException(int32_t ex, const String8& message) { |
| 109 | mException = ex; |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 110 | mErrorCode = NO_ERROR; // an exception, not a transaction failure. |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 111 | mMessage.setTo(message); |
| 112 | } |
| 113 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 114 | String8 Status::toString8() const { |
| 115 | String8 ret; |
| 116 | if (mException == EX_NONE) { |
| 117 | ret.append("No error"); |
| 118 | } else { |
| 119 | ret.appendFormat("Status(%d): '", mException); |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 120 | if (mException == EX_TRANSACTION_FAILED) { |
| 121 | ret.appendFormat("%d: ", mErrorCode); |
| 122 | } |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 123 | ret.append(String8(mMessage)); |
| 124 | ret.append("'"); |
| 125 | } |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | } // namespace binder |
| 130 | } // namespace android |