blob: 84668638653ac410fcae845e445802f3de16b78d [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) {
Christopher Wileyc1e491d2015-11-20 17:48:27 -080027 return Status(exceptionCode, OK);
Christopher Wileycff7f172015-11-22 16:29:58 -080028}
29
30Status Status::fromExceptionCode(int32_t exceptionCode,
31 const String8& message) {
Christopher Wileyc1e491d2015-11-20 17:48:27 -080032 return Status(exceptionCode, OK, message);
33}
34
Christopher Wiley1651ced2016-05-06 09:19:44 -070035Status Status::fromExceptionCode(int32_t exceptionCode,
36 const char* message) {
37 return fromExceptionCode(exceptionCode, String8(message));
38}
39
Christopher Wileyc1e491d2015-11-20 17:48:27 -080040Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) {
41 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode);
42}
43
44Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
45 const String8& message) {
46 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message);
Christopher Wiley09eb7492015-11-09 15:06:15 -080047}
48
Christopher Wiley1651ced2016-05-06 09:19:44 -070049Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
50 const char* message) {
51 return fromServiceSpecificError(serviceSpecificErrorCode, String8(message));
52}
53
Christopher Wiley09eb7492015-11-09 15:06:15 -080054Status Status::fromStatusT(status_t status) {
55 Status ret;
56 ret.setFromStatusT(status);
57 return ret;
58}
59
Christopher Wileyc1e491d2015-11-20 17:48:27 -080060Status::Status(int32_t exceptionCode, int32_t errorCode)
Christopher Wileycff7f172015-11-22 16:29:58 -080061 : mException(exceptionCode),
Christopher Wileyc1e491d2015-11-20 17:48:27 -080062 mErrorCode(errorCode) {}
63
64Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message)
65 : mException(exceptionCode),
66 mErrorCode(errorCode),
Christopher Wiley09eb7492015-11-09 15:06:15 -080067 mMessage(message) {}
68
69status_t Status::readFromParcel(const Parcel& parcel) {
70 status_t status = parcel.readInt32(&mException);
71 if (status != OK) {
72 setFromStatusT(status);
73 return status;
74 }
75
76 // Skip over fat response headers. Not used (or propagated) in native code.
77 if (mException == EX_HAS_REPLY_HEADER) {
78 // Note that the header size includes the 4 byte size field.
79 const int32_t header_start = parcel.dataPosition();
80 int32_t header_size;
81 status = parcel.readInt32(&header_size);
82 if (status != OK) {
83 setFromStatusT(status);
84 return status;
85 }
86 parcel.setDataPosition(header_start + header_size);
87 // And fat response headers are currently only used when there are no
88 // exceptions, so act like there was no error.
89 mException = EX_NONE;
90 }
91
92 if (mException == EX_NONE) {
93 return status;
94 }
95
96 // The remote threw an exception. Get the message back.
Christopher Wileycff7f172015-11-22 16:29:58 -080097 String16 message;
98 status = parcel.readString16(&message);
99 if (status != OK) {
100 setFromStatusT(status);
101 return status;
102 }
103 mMessage = String8(message);
Christopher Wiley09eb7492015-11-09 15:06:15 -0800104
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800105 if (mException == EX_SERVICE_SPECIFIC) {
106 status = parcel.readInt32(&mErrorCode);
107 }
108 if (status != OK) {
109 setFromStatusT(status);
110 return status;
111 }
112
Christopher Wiley09eb7492015-11-09 15:06:15 -0800113 return status;
114}
115
116status_t Status::writeToParcel(Parcel* parcel) const {
Christopher Wileycff7f172015-11-22 16:29:58 -0800117 // Something really bad has happened, and we're not going to even
118 // try returning rich error data.
119 if (mException == EX_TRANSACTION_FAILED) {
120 return mErrorCode;
121 }
122
Christopher Wiley09eb7492015-11-09 15:06:15 -0800123 status_t status = parcel->writeInt32(mException);
124 if (status != OK) { return status; }
125 if (mException == EX_NONE) {
126 // We have no more information to write.
127 return status;
128 }
129 status = parcel->writeString16(String16(mMessage));
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800130 if (mException != EX_SERVICE_SPECIFIC) {
131 // We have no more information to write.
132 return status;
133 }
134 status = parcel->writeInt32(mErrorCode);
Christopher Wiley09eb7492015-11-09 15:06:15 -0800135 return status;
136}
137
Christopher Wiley09eb7492015-11-09 15:06:15 -0800138void Status::setException(int32_t ex, const String8& message) {
139 mException = ex;
Christopher Wileycff7f172015-11-22 16:29:58 -0800140 mErrorCode = NO_ERROR; // an exception, not a transaction failure.
Christopher Wiley09eb7492015-11-09 15:06:15 -0800141 mMessage.setTo(message);
142}
143
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800144void Status::setServiceSpecificError(int32_t errorCode, const String8& message) {
145 setException(EX_SERVICE_SPECIFIC, message);
146 mErrorCode = errorCode;
147}
148
149void Status::setFromStatusT(status_t status) {
150 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
151 mErrorCode = status;
152 mMessage.clear();
153}
154
Christopher Wiley09eb7492015-11-09 15:06:15 -0800155String8 Status::toString8() const {
156 String8 ret;
157 if (mException == EX_NONE) {
158 ret.append("No error");
159 } else {
160 ret.appendFormat("Status(%d): '", mException);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800161 if (mException == EX_SERVICE_SPECIFIC ||
162 mException == EX_TRANSACTION_FAILED) {
Christopher Wileycff7f172015-11-22 16:29:58 -0800163 ret.appendFormat("%d: ", mErrorCode);
164 }
Christopher Wiley09eb7492015-11-09 15:06:15 -0800165 ret.append(String8(mMessage));
166 ret.append("'");
167 }
168 return ret;
169}
170
Ralph Nathan00cb9802016-04-13 12:42:06 -0700171std::stringstream& operator<< (std::stringstream& stream, const Status& s) {
172 stream << s.toString8().string();
173 return stream;
174}
175
Christopher Wiley09eb7492015-11-09 15:06:15 -0800176} // namespace binder
177} // namespace android