blob: 8b33a56484e8af058338a35be41a155a8b1ebc8f [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) {
Steven Morelandfb26e722018-10-05 13:43:24 -070027 if (exceptionCode == EX_TRANSACTION_FAILED) {
28 return Status(exceptionCode, FAILED_TRANSACTION);
29 }
Christopher Wileyc1e491d2015-11-20 17:48:27 -080030 return Status(exceptionCode, OK);
Christopher Wileycff7f172015-11-22 16:29:58 -080031}
32
33Status Status::fromExceptionCode(int32_t exceptionCode,
34 const String8& message) {
Steven Morelandfb26e722018-10-05 13:43:24 -070035 if (exceptionCode == EX_TRANSACTION_FAILED) {
36 return Status(exceptionCode, FAILED_TRANSACTION, message);
37 }
Christopher Wileyc1e491d2015-11-20 17:48:27 -080038 return Status(exceptionCode, OK, message);
39}
40
Christopher Wiley1651ced2016-05-06 09:19:44 -070041Status Status::fromExceptionCode(int32_t exceptionCode,
42 const char* message) {
43 return fromExceptionCode(exceptionCode, String8(message));
44}
45
Christopher Wileyc1e491d2015-11-20 17:48:27 -080046Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) {
47 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode);
48}
49
50Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
51 const String8& message) {
52 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message);
Christopher Wiley09eb7492015-11-09 15:06:15 -080053}
54
Christopher Wiley1651ced2016-05-06 09:19:44 -070055Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
56 const char* message) {
57 return fromServiceSpecificError(serviceSpecificErrorCode, String8(message));
58}
59
Christopher Wiley09eb7492015-11-09 15:06:15 -080060Status Status::fromStatusT(status_t status) {
61 Status ret;
62 ret.setFromStatusT(status);
63 return ret;
64}
65
Jeongik Cha89b3f8b2019-03-18 14:38:33 +090066std::string Status::exceptionToString(int32_t exceptionCode) {
67 switch (exceptionCode) {
68 #define EXCEPTION_TO_CASE(EXCEPTION) case EXCEPTION: return #EXCEPTION;
69 EXCEPTION_TO_CASE(EX_NONE)
70 EXCEPTION_TO_CASE(EX_SECURITY)
71 EXCEPTION_TO_CASE(EX_BAD_PARCELABLE)
72 EXCEPTION_TO_CASE(EX_ILLEGAL_ARGUMENT)
73 EXCEPTION_TO_CASE(EX_NULL_POINTER)
74 EXCEPTION_TO_CASE(EX_ILLEGAL_STATE)
75 EXCEPTION_TO_CASE(EX_NETWORK_MAIN_THREAD)
76 EXCEPTION_TO_CASE(EX_UNSUPPORTED_OPERATION)
77 EXCEPTION_TO_CASE(EX_SERVICE_SPECIFIC)
78 EXCEPTION_TO_CASE(EX_PARCELABLE)
79 EXCEPTION_TO_CASE(EX_HAS_REPLY_HEADER)
80 EXCEPTION_TO_CASE(EX_TRANSACTION_FAILED)
81 #undef EXCEPTION_TO_CASE
82 default: return std::to_string(exceptionCode);
83 }
84}
85
Christopher Wileyc1e491d2015-11-20 17:48:27 -080086Status::Status(int32_t exceptionCode, int32_t errorCode)
Christopher Wileycff7f172015-11-22 16:29:58 -080087 : mException(exceptionCode),
Christopher Wileyc1e491d2015-11-20 17:48:27 -080088 mErrorCode(errorCode) {}
89
90Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message)
91 : mException(exceptionCode),
92 mErrorCode(errorCode),
Christopher Wiley09eb7492015-11-09 15:06:15 -080093 mMessage(message) {}
94
95status_t Status::readFromParcel(const Parcel& parcel) {
96 status_t status = parcel.readInt32(&mException);
97 if (status != OK) {
98 setFromStatusT(status);
99 return status;
100 }
101
102 // Skip over fat response headers. Not used (or propagated) in native code.
103 if (mException == EX_HAS_REPLY_HEADER) {
104 // Note that the header size includes the 4 byte size field.
105 const int32_t header_start = parcel.dataPosition();
106 int32_t header_size;
107 status = parcel.readInt32(&header_size);
108 if (status != OK) {
109 setFromStatusT(status);
110 return status;
111 }
112 parcel.setDataPosition(header_start + header_size);
113 // And fat response headers are currently only used when there are no
114 // exceptions, so act like there was no error.
115 mException = EX_NONE;
116 }
117
118 if (mException == EX_NONE) {
119 return status;
120 }
121
122 // The remote threw an exception. Get the message back.
Christopher Wileycff7f172015-11-22 16:29:58 -0800123 String16 message;
124 status = parcel.readString16(&message);
125 if (status != OK) {
126 setFromStatusT(status);
127 return status;
128 }
129 mMessage = String8(message);
Christopher Wiley09eb7492015-11-09 15:06:15 -0800130
Fyodor Kupolovd9ac3742017-11-13 15:51:03 -0800131 // Skip over the remote stack trace data
132 int32_t remote_stack_trace_header_size;
133 status = parcel.readInt32(&remote_stack_trace_header_size);
134 if (status != OK) {
135 setFromStatusT(status);
136 return status;
137 }
138 parcel.setDataPosition(parcel.dataPosition() + remote_stack_trace_header_size);
139
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800140 if (mException == EX_SERVICE_SPECIFIC) {
141 status = parcel.readInt32(&mErrorCode);
Jeff Sharkeyc8dc4f02017-01-17 13:53:04 -0700142 } else if (mException == EX_PARCELABLE) {
143 // Skip over the blob of Parcelable data
144 const int32_t header_start = parcel.dataPosition();
145 int32_t header_size;
146 status = parcel.readInt32(&header_size);
147 if (status != OK) {
148 setFromStatusT(status);
149 return status;
150 }
151 parcel.setDataPosition(header_start + header_size);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800152 }
153 if (status != OK) {
154 setFromStatusT(status);
155 return status;
156 }
157
Christopher Wiley09eb7492015-11-09 15:06:15 -0800158 return status;
159}
160
161status_t Status::writeToParcel(Parcel* parcel) const {
Christopher Wileycff7f172015-11-22 16:29:58 -0800162 // Something really bad has happened, and we're not going to even
163 // try returning rich error data.
164 if (mException == EX_TRANSACTION_FAILED) {
Steven Morelandfb26e722018-10-05 13:43:24 -0700165 return mErrorCode;
Christopher Wileycff7f172015-11-22 16:29:58 -0800166 }
167
Christopher Wiley09eb7492015-11-09 15:06:15 -0800168 status_t status = parcel->writeInt32(mException);
169 if (status != OK) { return status; }
170 if (mException == EX_NONE) {
171 // We have no more information to write.
172 return status;
173 }
174 status = parcel->writeString16(String16(mMessage));
Fyodor Kupolovd9ac3742017-11-13 15:51:03 -0800175 status = parcel->writeInt32(0); // Empty remote stack trace header
Jeff Sharkeyc8dc4f02017-01-17 13:53:04 -0700176 if (mException == EX_SERVICE_SPECIFIC) {
177 status = parcel->writeInt32(mErrorCode);
178 } else if (mException == EX_PARCELABLE) {
179 // Sending Parcelable blobs currently not supported
180 status = parcel->writeInt32(0);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800181 }
Christopher Wiley09eb7492015-11-09 15:06:15 -0800182 return status;
183}
184
Christopher Wiley09eb7492015-11-09 15:06:15 -0800185void Status::setException(int32_t ex, const String8& message) {
186 mException = ex;
Steven Morelandfb26e722018-10-05 13:43:24 -0700187 mErrorCode = ex == EX_TRANSACTION_FAILED ? FAILED_TRANSACTION : NO_ERROR;
Christopher Wiley09eb7492015-11-09 15:06:15 -0800188 mMessage.setTo(message);
189}
190
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800191void Status::setServiceSpecificError(int32_t errorCode, const String8& message) {
192 setException(EX_SERVICE_SPECIFIC, message);
193 mErrorCode = errorCode;
194}
195
196void Status::setFromStatusT(status_t status) {
197 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
198 mErrorCode = status;
199 mMessage.clear();
200}
201
Christopher Wiley09eb7492015-11-09 15:06:15 -0800202String8 Status::toString8() const {
203 String8 ret;
204 if (mException == EX_NONE) {
205 ret.append("No error");
206 } else {
Jeongik Cha89b3f8b2019-03-18 14:38:33 +0900207 ret.appendFormat("Status(%d, %s): '", mException, exceptionToString(mException).c_str());
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800208 if (mException == EX_SERVICE_SPECIFIC ||
209 mException == EX_TRANSACTION_FAILED) {
Christopher Wileycff7f172015-11-22 16:29:58 -0800210 ret.appendFormat("%d: ", mErrorCode);
211 }
Christopher Wiley09eb7492015-11-09 15:06:15 -0800212 ret.append(String8(mMessage));
213 ret.append("'");
214 }
215 return ret;
216}
217
Ralph Nathan00cb9802016-04-13 12:42:06 -0700218std::stringstream& operator<< (std::stringstream& stream, const Status& s) {
219 stream << s.toString8().string();
220 return stream;
221}
222
Christopher Wiley09eb7492015-11-09 15:06:15 -0800223} // namespace binder
224} // namespace android