blob: 8b9dee86280215205097bbaca0107a75e3ae95f0 [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 Moreland00d43582019-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 Moreland00d43582019-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
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800115 if (mException == EX_SERVICE_SPECIFIC) {
116 status = parcel.readInt32(&mErrorCode);
Jeff Sharkeyc8dc4f02017-01-17 13:53:04 -0700117 } else if (mException == EX_PARCELABLE) {
118 // Skip over the blob of Parcelable data
Steven Moreland00d43582019-05-20 15:28:13 -0700119 const size_t header_start = parcel.dataPosition();
120 // Get available size before reading more
121 const size_t header_avail = parcel.dataAvail();
122
Jeff Sharkeyc8dc4f02017-01-17 13:53:04 -0700123 int32_t header_size;
124 status = parcel.readInt32(&header_size);
125 if (status != OK) {
126 setFromStatusT(status);
127 return status;
128 }
Steven Moreland00d43582019-05-20 15:28:13 -0700129
130 if (header_size < 0 || static_cast<size_t>(header_size) > header_avail) {
131 android_errorWriteLog(0x534e4554, "132650049");
132 setFromStatusT(UNKNOWN_ERROR);
133 return UNKNOWN_ERROR;
134 }
135
Jeff Sharkeyc8dc4f02017-01-17 13:53:04 -0700136 parcel.setDataPosition(header_start + header_size);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800137 }
138 if (status != OK) {
139 setFromStatusT(status);
140 return status;
141 }
142
Christopher Wiley09eb7492015-11-09 15:06:15 -0800143 return status;
144}
145
146status_t Status::writeToParcel(Parcel* parcel) const {
Christopher Wileycff7f172015-11-22 16:29:58 -0800147 // Something really bad has happened, and we're not going to even
148 // try returning rich error data.
149 if (mException == EX_TRANSACTION_FAILED) {
150 return mErrorCode;
151 }
152
Christopher Wiley09eb7492015-11-09 15:06:15 -0800153 status_t status = parcel->writeInt32(mException);
154 if (status != OK) { return status; }
155 if (mException == EX_NONE) {
156 // We have no more information to write.
157 return status;
158 }
159 status = parcel->writeString16(String16(mMessage));
Jeff Sharkeyc8dc4f02017-01-17 13:53:04 -0700160 if (mException == EX_SERVICE_SPECIFIC) {
161 status = parcel->writeInt32(mErrorCode);
162 } else if (mException == EX_PARCELABLE) {
163 // Sending Parcelable blobs currently not supported
164 status = parcel->writeInt32(0);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800165 }
Christopher Wiley09eb7492015-11-09 15:06:15 -0800166 return status;
167}
168
Christopher Wiley09eb7492015-11-09 15:06:15 -0800169void Status::setException(int32_t ex, const String8& message) {
170 mException = ex;
Christopher Wileycff7f172015-11-22 16:29:58 -0800171 mErrorCode = NO_ERROR; // an exception, not a transaction failure.
Christopher Wiley09eb7492015-11-09 15:06:15 -0800172 mMessage.setTo(message);
173}
174
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800175void Status::setServiceSpecificError(int32_t errorCode, const String8& message) {
176 setException(EX_SERVICE_SPECIFIC, message);
177 mErrorCode = errorCode;
178}
179
180void Status::setFromStatusT(status_t status) {
181 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
182 mErrorCode = status;
183 mMessage.clear();
184}
185
Christopher Wiley09eb7492015-11-09 15:06:15 -0800186String8 Status::toString8() const {
187 String8 ret;
188 if (mException == EX_NONE) {
189 ret.append("No error");
190 } else {
191 ret.appendFormat("Status(%d): '", mException);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800192 if (mException == EX_SERVICE_SPECIFIC ||
193 mException == EX_TRANSACTION_FAILED) {
Christopher Wileycff7f172015-11-22 16:29:58 -0800194 ret.appendFormat("%d: ", mErrorCode);
195 }
Christopher Wiley09eb7492015-11-09 15:06:15 -0800196 ret.append(String8(mMessage));
197 ret.append("'");
198 }
199 return ret;
200}
201
Ralph Nathan00cb9802016-04-13 12:42:06 -0700202std::stringstream& operator<< (std::stringstream& stream, const Status& s) {
203 stream << s.toString8().string();
204 return stream;
205}
206
Christopher Wiley09eb7492015-11-09 15:06:15 -0800207} // namespace binder
208} // namespace android