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 22 Status Status::ok() { 23 return Status(); 24 } 25 26 Status Status::fromExceptionCode(int32_t exceptionCode) { 27 return Status(exceptionCode, OK); 28 } 29 30 Status Status::fromExceptionCode(int32_t exceptionCode, 31 const String8& message) { 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); 42 } 43 44 Status Status::fromStatusT(status_t status) { 45 Status ret; 46 ret.setFromStatusT(status); 47 return ret; 48 } 49 50 Status::Status(int32_t exceptionCode, int32_t errorCode) 51 : mException(exceptionCode), 52 mErrorCode(errorCode) {} 53 54 Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message) 55 : mException(exceptionCode), 56 mErrorCode(errorCode), 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. 87 String16 message; 88 status = parcel.readString16(&message); 89 if (status != OK) { 90 setFromStatusT(status); 91 return status; 92 } 93 mMessage = String8(message); 94 95 if (mException == EX_SERVICE_SPECIFIC) { 96 status = parcel.readInt32(&mErrorCode); 97 } 98 if (status != OK) { 99 setFromStatusT(status); 100 return status; 101 } 102 103 return status; 104 } 105 106 status_t Status::writeToParcel(Parcel* parcel) const { 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 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)); 120 if (mException != EX_SERVICE_SPECIFIC) { 121 // We have no more information to write. 122 return status; 123 } 124 status = parcel->writeInt32(mErrorCode); 125 return status; 126 } 127 128 void Status::setException(int32_t ex, const String8& message) { 129 mException = ex; 130 mErrorCode = NO_ERROR; // an exception, not a transaction failure. 131 mMessage.setTo(message); 132 } 133 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 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); 151 if (mException == EX_SERVICE_SPECIFIC || 152 mException == EX_TRANSACTION_FAILED) { 153 ret.appendFormat("%d: ", mErrorCode); 154 } 155 ret.append(String8(mMessage)); 156 ret.append("'"); 157 } 158 return ret; 159 } 160 161 } // namespace binder 162 } // namespace android 163