blob: 5ae6db2f580e878c5ca0f20ca13be68e909ee857 [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
35Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) {
36 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode);
37}
38
39Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
40 const String8& message) {
41 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message);
Christopher Wiley09eb7492015-11-09 15:06:15 -080042}
43
44Status Status::fromStatusT(status_t status) {
45 Status ret;
46 ret.setFromStatusT(status);
47 return ret;
48}
49
Christopher Wileyc1e491d2015-11-20 17:48:27 -080050Status::Status(int32_t exceptionCode, int32_t errorCode)
Christopher Wileycff7f172015-11-22 16:29:58 -080051 : mException(exceptionCode),
Christopher Wileyc1e491d2015-11-20 17:48:27 -080052 mErrorCode(errorCode) {}
53
54Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message)
55 : mException(exceptionCode),
56 mErrorCode(errorCode),
Christopher Wiley09eb7492015-11-09 15:06:15 -080057 mMessage(message) {}
58
59status_t Status::readFromParcel(const Parcel& parcel) {
60 status_t status = parcel.readInt32(&mException);
61 if (status != OK) {
62 setFromStatusT(status);
63 return status;
64 }
65
66 // Skip over fat response headers. Not used (or propagated) in native code.
67 if (mException == EX_HAS_REPLY_HEADER) {
68 // Note that the header size includes the 4 byte size field.
69 const int32_t header_start = parcel.dataPosition();
70 int32_t header_size;
71 status = parcel.readInt32(&header_size);
72 if (status != OK) {
73 setFromStatusT(status);
74 return status;
75 }
76 parcel.setDataPosition(header_start + header_size);
77 // And fat response headers are currently only used when there are no
78 // exceptions, so act like there was no error.
79 mException = EX_NONE;
80 }
81
82 if (mException == EX_NONE) {
83 return status;
84 }
85
86 // The remote threw an exception. Get the message back.
Christopher Wileycff7f172015-11-22 16:29:58 -080087 String16 message;
88 status = parcel.readString16(&message);
89 if (status != OK) {
90 setFromStatusT(status);
91 return status;
92 }
93 mMessage = String8(message);
Christopher Wiley09eb7492015-11-09 15:06:15 -080094
Christopher Wileyc1e491d2015-11-20 17:48:27 -080095 if (mException == EX_SERVICE_SPECIFIC) {
96 status = parcel.readInt32(&mErrorCode);
97 }
98 if (status != OK) {
99 setFromStatusT(status);
100 return status;
101 }
102
Christopher Wiley09eb7492015-11-09 15:06:15 -0800103 return status;
104}
105
106status_t Status::writeToParcel(Parcel* parcel) const {
Christopher Wileycff7f172015-11-22 16:29:58 -0800107 // Something really bad has happened, and we're not going to even
108 // try returning rich error data.
109 if (mException == EX_TRANSACTION_FAILED) {
110 return mErrorCode;
111 }
112
Christopher Wiley09eb7492015-11-09 15:06:15 -0800113 status_t status = parcel->writeInt32(mException);
114 if (status != OK) { return status; }
115 if (mException == EX_NONE) {
116 // We have no more information to write.
117 return status;
118 }
119 status = parcel->writeString16(String16(mMessage));
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800120 if (mException != EX_SERVICE_SPECIFIC) {
121 // We have no more information to write.
122 return status;
123 }
124 status = parcel->writeInt32(mErrorCode);
Christopher Wiley09eb7492015-11-09 15:06:15 -0800125 return status;
126}
127
Christopher Wiley09eb7492015-11-09 15:06:15 -0800128void Status::setException(int32_t ex, const String8& message) {
129 mException = ex;
Christopher Wileycff7f172015-11-22 16:29:58 -0800130 mErrorCode = NO_ERROR; // an exception, not a transaction failure.
Christopher Wiley09eb7492015-11-09 15:06:15 -0800131 mMessage.setTo(message);
132}
133
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800134void Status::setServiceSpecificError(int32_t errorCode, const String8& message) {
135 setException(EX_SERVICE_SPECIFIC, message);
136 mErrorCode = errorCode;
137}
138
139void Status::setFromStatusT(status_t status) {
140 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
141 mErrorCode = status;
142 mMessage.clear();
143}
144
Christopher Wiley09eb7492015-11-09 15:06:15 -0800145String8 Status::toString8() const {
146 String8 ret;
147 if (mException == EX_NONE) {
148 ret.append("No error");
149 } else {
150 ret.appendFormat("Status(%d): '", mException);
Christopher Wileyc1e491d2015-11-20 17:48:27 -0800151 if (mException == EX_SERVICE_SPECIFIC ||
152 mException == EX_TRANSACTION_FAILED) {
Christopher Wileycff7f172015-11-22 16:29:58 -0800153 ret.appendFormat("%d: ", mErrorCode);
154 }
Christopher Wiley09eb7492015-11-09 15:06:15 -0800155 ret.append(String8(mMessage));
156 ret.append("'");
157 }
158 return ret;
159}
160
Ralph Nathan00cb9802016-04-13 12:42:06 -0700161std::stringstream& operator<< (std::stringstream& stream, const Status& s) {
162 stream << s.toString8().string();
163 return stream;
164}
165
Christopher Wiley09eb7492015-11-09 15:06:15 -0800166} // namespace binder
167} // namespace android