blob: a3f87557ab8cb5f3e962d590caca8fe95a07261b [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
Christopher Wileyc1e491d2015-11-20 17:48:27 -080066Status::Status(int32_t exceptionCode, int32_t errorCode)
Christopher Wileycff7f172015-11-22 16:29:58 -080067 : mException(exceptionCode),
Christopher Wileyc1e491d2015-11-20 17:48:27 -080068 mErrorCode(errorCode) {}
69
70Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message)
71 : mException(exceptionCode),
72 mErrorCode(errorCode),
Christopher Wiley09eb7492015-11-09 15:06:15 -080073 mMessage(message) {}
74
75status_t Status::readFromParcel(const Parcel& parcel) {
76 status_t status = parcel.readInt32(&mException);
77 if (status != OK) {
78 setFromStatusT(status);
79 return status;
80 }
81
82 // Skip over fat response headers. Not used (or propagated) in native code.
83 if (mException == EX_HAS_REPLY_HEADER) {
84 // Note that the header size includes the 4 byte size field.
85 const int32_t header_start = parcel.dataPosition();
86 int32_t header_size;
87 status = parcel.readInt32(&header_size);
88 if (status != OK) {
89 setFromStatusT(status);
90 return status;
91 }
92 parcel.setDataPosition(header_start + header_size);
93 // And fat response headers are currently only used when there are no
94 // exceptions, so act like there was no error.
95 mException = EX_NONE;
96 }
97
98 if (mException == EX_NONE) {
99 return status;
100 }
101
102 // The remote threw an exception. Get the message back.
Christopher Wileycff7f172015-11-22 16:29:58 -0800103 String16 message;
104 status = parcel.readString16(&message);
105 if (status != OK) {
106 setFromStatusT(status);
107 return status;
108 }
109 mMessage = String8(message);
Christopher Wiley09eb7492015-11-09 15:06:15 -0800110
Fyodor Kupolovd9ac3742017-11-13 15:51:03 -0800111 // Skip over the remote stack trace data
112 int32_t remote_stack_trace_header_size;
113 status = parcel.readInt32(&remote_stack_trace_header_size);
114 if (status != OK) {
115 setFromStatusT(status);
116 return status;
117 }
118 parcel.setDataPosition(parcel.dataPosition() + remote_stack_trace_header_size);
119
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800120 if (mException == EX_SERVICE_SPECIFIC) {
121 status = parcel.readInt32(&mErrorCode);
Jeff Sharkeyc8dc4f02017-01-17 13:53:04 -0700122 } else if (mException == EX_PARCELABLE) {
123 // Skip over the blob of Parcelable data
124 const int32_t header_start = parcel.dataPosition();
125 int32_t header_size;
126 status = parcel.readInt32(&header_size);
127 if (status != OK) {
128 setFromStatusT(status);
129 return status;
130 }
131 parcel.setDataPosition(header_start + header_size);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800132 }
133 if (status != OK) {
134 setFromStatusT(status);
135 return status;
136 }
137
Christopher Wiley09eb7492015-11-09 15:06:15 -0800138 return status;
139}
140
141status_t Status::writeToParcel(Parcel* parcel) const {
Christopher Wileycff7f172015-11-22 16:29:58 -0800142 // Something really bad has happened, and we're not going to even
143 // try returning rich error data.
144 if (mException == EX_TRANSACTION_FAILED) {
Steven Morelandfb26e722018-10-05 13:43:24 -0700145 return mErrorCode;
Christopher Wileycff7f172015-11-22 16:29:58 -0800146 }
147
Christopher Wiley09eb7492015-11-09 15:06:15 -0800148 status_t status = parcel->writeInt32(mException);
149 if (status != OK) { return status; }
150 if (mException == EX_NONE) {
151 // We have no more information to write.
152 return status;
153 }
154 status = parcel->writeString16(String16(mMessage));
Fyodor Kupolovd9ac3742017-11-13 15:51:03 -0800155 status = parcel->writeInt32(0); // Empty remote stack trace header
Jeff Sharkeyc8dc4f02017-01-17 13:53:04 -0700156 if (mException == EX_SERVICE_SPECIFIC) {
157 status = parcel->writeInt32(mErrorCode);
158 } else if (mException == EX_PARCELABLE) {
159 // Sending Parcelable blobs currently not supported
160 status = parcel->writeInt32(0);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800161 }
Christopher Wiley09eb7492015-11-09 15:06:15 -0800162 return status;
163}
164
Christopher Wiley09eb7492015-11-09 15:06:15 -0800165void Status::setException(int32_t ex, const String8& message) {
166 mException = ex;
Steven Morelandfb26e722018-10-05 13:43:24 -0700167 mErrorCode = ex == EX_TRANSACTION_FAILED ? FAILED_TRANSACTION : NO_ERROR;
Christopher Wiley09eb7492015-11-09 15:06:15 -0800168 mMessage.setTo(message);
169}
170
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800171void Status::setServiceSpecificError(int32_t errorCode, const String8& message) {
172 setException(EX_SERVICE_SPECIFIC, message);
173 mErrorCode = errorCode;
174}
175
176void Status::setFromStatusT(status_t status) {
177 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
178 mErrorCode = status;
179 mMessage.clear();
180}
181
Christopher Wiley09eb7492015-11-09 15:06:15 -0800182String8 Status::toString8() const {
183 String8 ret;
184 if (mException == EX_NONE) {
185 ret.append("No error");
186 } else {
187 ret.appendFormat("Status(%d): '", mException);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800188 if (mException == EX_SERVICE_SPECIFIC ||
189 mException == EX_TRANSACTION_FAILED) {
Christopher Wileycff7f172015-11-22 16:29:58 -0800190 ret.appendFormat("%d: ", mErrorCode);
191 }
Christopher Wiley09eb7492015-11-09 15:06:15 -0800192 ret.append(String8(mMessage));
193 ret.append("'");
194 }
195 return ret;
196}
197
Ralph Nathan00cb9802016-04-13 12:42:06 -0700198std::stringstream& operator<< (std::stringstream& stream, const Status& s) {
199 stream << s.toString8().string();
200 return stream;
201}
202
Christopher Wiley09eb7492015-11-09 15:06:15 -0800203} // namespace binder
204} // namespace android