blob: fe0e5924c8df054d9cc84071b39699c708a18324 [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.
Steven Moreland00383642019-05-20 15:28:13 -070079 const size_t header_start = parcel.dataPosition();
80 // Get available size before reading more
81 const size_t header_avail = parcel.dataAvail();
82
Christopher Wiley09eb7492015-11-09 15:06:15 -080083 int32_t header_size;
84 status = parcel.readInt32(&header_size);
85 if (status != OK) {
86 setFromStatusT(status);
87 return status;
88 }
Steven Moreland00383642019-05-20 15:28:13 -070089
90 if (header_size < 0 || static_cast<size_t>(header_size) > header_avail) {
91 android_errorWriteLog(0x534e4554, "132650049");
92 setFromStatusT(UNKNOWN_ERROR);
93 return UNKNOWN_ERROR;
94 }
95
Christopher Wiley09eb7492015-11-09 15:06:15 -080096 parcel.setDataPosition(header_start + header_size);
97 // And fat response headers are currently only used when there are no
98 // exceptions, so act like there was no error.
99 mException = EX_NONE;
100 }
101
102 if (mException == EX_NONE) {
103 return status;
104 }
105
106 // The remote threw an exception. Get the message back.
Christopher Wileycff7f172015-11-22 16:29:58 -0800107 String16 message;
108 status = parcel.readString16(&message);
109 if (status != OK) {
110 setFromStatusT(status);
111 return status;
112 }
113 mMessage = String8(message);
Christopher Wiley09eb7492015-11-09 15:06:15 -0800114
Fyodor Kupolovd9ac3742017-11-13 15:51:03 -0800115 // Skip over the remote stack trace data
116 int32_t remote_stack_trace_header_size;
117 status = parcel.readInt32(&remote_stack_trace_header_size);
118 if (status != OK) {
119 setFromStatusT(status);
120 return status;
121 }
Steven Moreland00383642019-05-20 15:28:13 -0700122 if (remote_stack_trace_header_size < 0 ||
123 static_cast<size_t>(remote_stack_trace_header_size) > parcel.dataAvail()) {
124
125 android_errorWriteLog(0x534e4554, "132650049");
126 setFromStatusT(UNKNOWN_ERROR);
127 return UNKNOWN_ERROR;
128 }
Fyodor Kupolovd9ac3742017-11-13 15:51:03 -0800129 parcel.setDataPosition(parcel.dataPosition() + remote_stack_trace_header_size);
130
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800131 if (mException == EX_SERVICE_SPECIFIC) {
132 status = parcel.readInt32(&mErrorCode);
Jeff Sharkeyc8dc4f02017-01-17 13:53:04 -0700133 } else if (mException == EX_PARCELABLE) {
134 // Skip over the blob of Parcelable data
Steven Moreland00383642019-05-20 15:28:13 -0700135 const size_t header_start = parcel.dataPosition();
136 // Get available size before reading more
137 const size_t header_avail = parcel.dataAvail();
138
Jeff Sharkeyc8dc4f02017-01-17 13:53:04 -0700139 int32_t header_size;
140 status = parcel.readInt32(&header_size);
141 if (status != OK) {
142 setFromStatusT(status);
143 return status;
144 }
Steven Moreland00383642019-05-20 15:28:13 -0700145
146 if (header_size < 0 || static_cast<size_t>(header_size) > header_avail) {
147 android_errorWriteLog(0x534e4554, "132650049");
148 setFromStatusT(UNKNOWN_ERROR);
149 return UNKNOWN_ERROR;
150 }
151
Jeff Sharkeyc8dc4f02017-01-17 13:53:04 -0700152 parcel.setDataPosition(header_start + header_size);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800153 }
154 if (status != OK) {
155 setFromStatusT(status);
156 return status;
157 }
158
Christopher Wiley09eb7492015-11-09 15:06:15 -0800159 return status;
160}
161
162status_t Status::writeToParcel(Parcel* parcel) const {
Christopher Wileycff7f172015-11-22 16:29:58 -0800163 // Something really bad has happened, and we're not going to even
164 // try returning rich error data.
165 if (mException == EX_TRANSACTION_FAILED) {
166 return mErrorCode;
167 }
168
Christopher Wiley09eb7492015-11-09 15:06:15 -0800169 status_t status = parcel->writeInt32(mException);
170 if (status != OK) { return status; }
171 if (mException == EX_NONE) {
172 // We have no more information to write.
173 return status;
174 }
175 status = parcel->writeString16(String16(mMessage));
Fyodor Kupolovd9ac3742017-11-13 15:51:03 -0800176 status = parcel->writeInt32(0); // Empty remote stack trace header
Jeff Sharkeyc8dc4f02017-01-17 13:53:04 -0700177 if (mException == EX_SERVICE_SPECIFIC) {
178 status = parcel->writeInt32(mErrorCode);
179 } else if (mException == EX_PARCELABLE) {
180 // Sending Parcelable blobs currently not supported
181 status = parcel->writeInt32(0);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800182 }
Christopher Wiley09eb7492015-11-09 15:06:15 -0800183 return status;
184}
185
Christopher Wiley09eb7492015-11-09 15:06:15 -0800186void Status::setException(int32_t ex, const String8& message) {
187 mException = ex;
Christopher Wileycff7f172015-11-22 16:29:58 -0800188 mErrorCode = NO_ERROR; // an exception, not a transaction failure.
Christopher Wiley09eb7492015-11-09 15:06:15 -0800189 mMessage.setTo(message);
190}
191
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800192void Status::setServiceSpecificError(int32_t errorCode, const String8& message) {
193 setException(EX_SERVICE_SPECIFIC, message);
194 mErrorCode = errorCode;
195}
196
197void Status::setFromStatusT(status_t status) {
198 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
199 mErrorCode = status;
200 mMessage.clear();
201}
202
Christopher Wiley09eb7492015-11-09 15:06:15 -0800203String8 Status::toString8() const {
204 String8 ret;
205 if (mException == EX_NONE) {
206 ret.append("No error");
207 } else {
208 ret.appendFormat("Status(%d): '", mException);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800209 if (mException == EX_SERVICE_SPECIFIC ||
210 mException == EX_TRANSACTION_FAILED) {
Christopher Wileycff7f172015-11-22 16:29:58 -0800211 ret.appendFormat("%d: ", mErrorCode);
212 }
Christopher Wiley09eb7492015-11-09 15:06:15 -0800213 ret.append(String8(mMessage));
214 ret.append("'");
215 }
216 return ret;
217}
218
Ralph Nathan00cb9802016-04-13 12:42:06 -0700219std::stringstream& operator<< (std::stringstream& stream, const Status& s) {
220 stream << s.toString8().string();
221 return stream;
222}
223
Christopher Wiley09eb7492015-11-09 15:06:15 -0800224} // namespace binder
225} // namespace android