Status.cpp revision 09eb749704afd9e226e1347cb20c90be2016cd21
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 19namespace android { 20namespace binder { 21 22Status Status::fromExceptionCode(int32_t exception_code) { 23 return Status(exception_code, ""); 24} 25 26Status Status::fromStatusT(status_t status) { 27 Status ret; 28 ret.setFromStatusT(status); 29 return ret; 30} 31 32Status Status::ok() { 33 return Status(); 34} 35 36Status::Status(int32_t exception_code, const String8& message) 37 : mException(exception_code), 38 mMessage(message) {} 39 40Status::Status(int32_t exception_code, const char* message) 41 : mException(exception_code), 42 mMessage(message) {} 43 44status_t Status::readFromParcel(const Parcel& parcel) { 45 status_t status = parcel.readInt32(&mException); 46 if (status != OK) { 47 setFromStatusT(status); 48 return status; 49 } 50 51 // Skip over fat response headers. Not used (or propagated) in native code. 52 if (mException == EX_HAS_REPLY_HEADER) { 53 // Note that the header size includes the 4 byte size field. 54 const int32_t header_start = parcel.dataPosition(); 55 int32_t header_size; 56 status = parcel.readInt32(&header_size); 57 if (status != OK) { 58 setFromStatusT(status); 59 return status; 60 } 61 parcel.setDataPosition(header_start + header_size); 62 // And fat response headers are currently only used when there are no 63 // exceptions, so act like there was no error. 64 mException = EX_NONE; 65 } 66 67 if (mException == EX_NONE) { 68 return status; 69 } 70 71 // The remote threw an exception. Get the message back. 72 mMessage = String8(parcel.readString16()); 73 74 return status; 75} 76 77status_t Status::writeToParcel(Parcel* parcel) const { 78 status_t status = parcel->writeInt32(mException); 79 if (status != OK) { return status; } 80 if (mException == EX_NONE) { 81 // We have no more information to write. 82 return status; 83 } 84 status = parcel->writeString16(String16(mMessage)); 85 return status; 86} 87 88void Status::setFromStatusT(status_t status) { 89 switch (status) { 90 case NO_ERROR: 91 mException = EX_NONE; 92 mMessage.clear(); 93 break; 94 case UNEXPECTED_NULL: 95 mException = EX_NULL_POINTER; 96 mMessage.setTo("Unexpected null reference in Parcel"); 97 break; 98 default: 99 mException = EX_TRANSACTION_FAILED; 100 mMessage.setTo("Transaction failed"); 101 break; 102 } 103} 104 105void Status::setException(int32_t ex, const String8& message) { 106 mException = ex; 107 mMessage.setTo(message); 108} 109 110void Status::getException(int32_t* returned_exception, 111 String8* returned_message) const { 112 if (returned_exception) { 113 *returned_exception = mException; 114 } 115 if (returned_message) { 116 returned_message->setTo(mMessage); 117 } 118} 119 120String8 Status::toString8() const { 121 String8 ret; 122 if (mException == EX_NONE) { 123 ret.append("No error"); 124 } else { 125 ret.appendFormat("Status(%d): '", mException); 126 ret.append(String8(mMessage)); 127 ret.append("'"); 128 } 129 return ret; 130} 131 132} // namespace binder 133} // namespace android 134