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 | |
| 35 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) { |
| 36 | return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode); |
| 37 | } |
| 38 | |
| 39 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode, |
| 40 | const String8& message) { |
| 41 | return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | Status Status::fromStatusT(status_t status) { |
| 45 | Status ret; |
| 46 | ret.setFromStatusT(status); |
| 47 | return ret; |
| 48 | } |
| 49 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 50 | Status::Status(int32_t exceptionCode, int32_t errorCode) |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 51 | : mException(exceptionCode), |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 52 | mErrorCode(errorCode) {} |
| 53 | |
| 54 | Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message) |
| 55 | : mException(exceptionCode), |
| 56 | mErrorCode(errorCode), |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 57 | mMessage(message) {} |
| 58 | |
| 59 | status_t Status::readFromParcel(const Parcel& parcel) { |
| 60 | status_t status = parcel.readInt32(&mException); |
| 61 | if (status != OK) { |
| 62 | setFromStatusT(status); |
| 63 | return status; |
| 64 | } |
| 65 | |
| 66 | // Skip over fat response headers. Not used (or propagated) in native code. |
| 67 | if (mException == EX_HAS_REPLY_HEADER) { |
| 68 | // Note that the header size includes the 4 byte size field. |
| 69 | const int32_t header_start = parcel.dataPosition(); |
| 70 | int32_t header_size; |
| 71 | status = parcel.readInt32(&header_size); |
| 72 | if (status != OK) { |
| 73 | setFromStatusT(status); |
| 74 | return status; |
| 75 | } |
| 76 | parcel.setDataPosition(header_start + header_size); |
| 77 | // And fat response headers are currently only used when there are no |
| 78 | // exceptions, so act like there was no error. |
| 79 | mException = EX_NONE; |
| 80 | } |
| 81 | |
| 82 | if (mException == EX_NONE) { |
| 83 | return status; |
| 84 | } |
| 85 | |
| 86 | // The remote threw an exception. Get the message back. |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 87 | String16 message; |
| 88 | status = parcel.readString16(&message); |
| 89 | if (status != OK) { |
| 90 | setFromStatusT(status); |
| 91 | return status; |
| 92 | } |
| 93 | mMessage = String8(message); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 94 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 95 | if (mException == EX_SERVICE_SPECIFIC) { |
| 96 | status = parcel.readInt32(&mErrorCode); |
| 97 | } |
| 98 | if (status != OK) { |
| 99 | setFromStatusT(status); |
| 100 | return status; |
| 101 | } |
| 102 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 103 | return status; |
| 104 | } |
| 105 | |
| 106 | status_t Status::writeToParcel(Parcel* parcel) const { |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 107 | // Something really bad has happened, and we're not going to even |
| 108 | // try returning rich error data. |
| 109 | if (mException == EX_TRANSACTION_FAILED) { |
| 110 | return mErrorCode; |
| 111 | } |
| 112 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 113 | status_t status = parcel->writeInt32(mException); |
| 114 | if (status != OK) { return status; } |
| 115 | if (mException == EX_NONE) { |
| 116 | // We have no more information to write. |
| 117 | return status; |
| 118 | } |
| 119 | status = parcel->writeString16(String16(mMessage)); |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 120 | if (mException != EX_SERVICE_SPECIFIC) { |
| 121 | // We have no more information to write. |
| 122 | return status; |
| 123 | } |
| 124 | status = parcel->writeInt32(mErrorCode); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 125 | return status; |
| 126 | } |
| 127 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 128 | void Status::setException(int32_t ex, const String8& message) { |
| 129 | mException = ex; |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 130 | mErrorCode = NO_ERROR; // an exception, not a transaction failure. |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 131 | mMessage.setTo(message); |
| 132 | } |
| 133 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 134 | void Status::setServiceSpecificError(int32_t errorCode, const String8& message) { |
| 135 | setException(EX_SERVICE_SPECIFIC, message); |
| 136 | mErrorCode = errorCode; |
| 137 | } |
| 138 | |
| 139 | void Status::setFromStatusT(status_t status) { |
| 140 | mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED; |
| 141 | mErrorCode = status; |
| 142 | mMessage.clear(); |
| 143 | } |
| 144 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 145 | String8 Status::toString8() const { |
| 146 | String8 ret; |
| 147 | if (mException == EX_NONE) { |
| 148 | ret.append("No error"); |
| 149 | } else { |
| 150 | ret.appendFormat("Status(%d): '", mException); |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 151 | if (mException == EX_SERVICE_SPECIFIC || |
| 152 | mException == EX_TRANSACTION_FAILED) { |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 153 | ret.appendFormat("%d: ", mErrorCode); |
| 154 | } |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 155 | ret.append(String8(mMessage)); |
| 156 | ret.append("'"); |
| 157 | } |
| 158 | return ret; |
| 159 | } |
| 160 | |
Ralph Nathan | 00cb980 | 2016-04-13 12:42:06 -0700 | [diff] [blame] | 161 | std::stringstream& operator<< (std::stringstream& stream, const Status& s) { |
| 162 | stream << s.toString8().string(); |
| 163 | return stream; |
| 164 | } |
| 165 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 166 | } // namespace binder |
| 167 | } // namespace android |