blob: 67f0d5966946e4271b7d890fd95479b3842de41e [file] [log] [blame]
Christopher Wiley09eb7492015-11-09 15:06:15 -08001/*
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
Christopher Wileycff7f172015-11-22 16:29:58 -080022Status Status::ok() {
23 return Status();
24}
25
26Status Status::fromExceptionCode(int32_t exceptionCode) {
27 return Status(exceptionCode);
28}
29
30Status Status::fromExceptionCode(int32_t exceptionCode,
31 const String8& message) {
32 return Status(exceptionCode, message);
Christopher Wiley09eb7492015-11-09 15:06:15 -080033}
34
35Status Status::fromStatusT(status_t status) {
36 Status ret;
37 ret.setFromStatusT(status);
38 return ret;
39}
40
Christopher Wileycff7f172015-11-22 16:29:58 -080041Status::Status(int32_t exceptionCode) : mException(exceptionCode) {}
42Status::Status(int32_t exceptionCode, const String8& message)
43 : mException(exceptionCode),
Christopher Wiley09eb7492015-11-09 15:06:15 -080044 mMessage(message) {}
45
46status_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 Wileycff7f172015-11-22 16:29:58 -080074 String16 message;
75 status = parcel.readString16(&message);
76 if (status != OK) {
77 setFromStatusT(status);
78 return status;
79 }
80 mMessage = String8(message);
Christopher Wiley09eb7492015-11-09 15:06:15 -080081
82 return status;
83}
84
85status_t Status::writeToParcel(Parcel* parcel) const {
Christopher Wileycff7f172015-11-22 16:29:58 -080086 // 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 Wiley09eb7492015-11-09 15:06:15 -080092 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
102void Status::setFromStatusT(status_t status) {
Christopher Wileycff7f172015-11-22 16:29:58 -0800103 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
104 mErrorCode = status;
105 mMessage.clear();
Christopher Wiley09eb7492015-11-09 15:06:15 -0800106}
107
108void Status::setException(int32_t ex, const String8& message) {
109 mException = ex;
Christopher Wileycff7f172015-11-22 16:29:58 -0800110 mErrorCode = NO_ERROR; // an exception, not a transaction failure.
Christopher Wiley09eb7492015-11-09 15:06:15 -0800111 mMessage.setTo(message);
112}
113
Christopher Wiley09eb7492015-11-09 15:06:15 -0800114String8 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 Wileycff7f172015-11-22 16:29:58 -0800120 if (mException == EX_TRANSACTION_FAILED) {
121 ret.appendFormat("%d: ", mErrorCode);
122 }
Christopher Wiley09eb7492015-11-09 15:06:15 -0800123 ret.append(String8(mMessage));
124 ret.append("'");
125 }
126 return ret;
127}
128
129} // namespace binder
130} // namespace android