1 /*
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
19 namespace android {
20 namespace binder {
21
ok()22 Status Status::ok() {
23 return Status();
24 }
25
fromExceptionCode(int32_t exceptionCode)26 Status Status::fromExceptionCode(int32_t exceptionCode) {
27 return Status(exceptionCode, OK);
28 }
29
fromExceptionCode(int32_t exceptionCode,const String8 & message)30 Status Status::fromExceptionCode(int32_t exceptionCode,
31 const String8& message) {
32 return Status(exceptionCode, OK, message);
33 }
34
fromExceptionCode(int32_t exceptionCode,const char * message)35 Status Status::fromExceptionCode(int32_t exceptionCode,
36 const char* message) {
37 return fromExceptionCode(exceptionCode, String8(message));
38 }
39
fromServiceSpecificError(int32_t serviceSpecificErrorCode)40 Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) {
41 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode);
42 }
43
fromServiceSpecificError(int32_t serviceSpecificErrorCode,const String8 & message)44 Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
45 const String8& message) {
46 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message);
47 }
48
fromServiceSpecificError(int32_t serviceSpecificErrorCode,const char * message)49 Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
50 const char* message) {
51 return fromServiceSpecificError(serviceSpecificErrorCode, String8(message));
52 }
53
fromStatusT(status_t status)54 Status Status::fromStatusT(status_t status) {
55 Status ret;
56 ret.setFromStatusT(status);
57 return ret;
58 }
59
Status(int32_t exceptionCode,int32_t errorCode)60 Status::Status(int32_t exceptionCode, int32_t errorCode)
61 : mException(exceptionCode),
62 mErrorCode(errorCode) {}
63
Status(int32_t exceptionCode,int32_t errorCode,const String8 & message)64 Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message)
65 : mException(exceptionCode),
66 mErrorCode(errorCode),
67 mMessage(message) {}
68
readFromParcel(const Parcel & parcel)69 status_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.
79 const size_t header_start = parcel.dataPosition();
80 // Get available size before reading more
81 const size_t header_avail = parcel.dataAvail();
82
83 int32_t header_size;
84 status = parcel.readInt32(&header_size);
85 if (status != OK) {
86 setFromStatusT(status);
87 return status;
88 }
89
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
96 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.
107 String16 message;
108 status = parcel.readString16(&message);
109 if (status != OK) {
110 setFromStatusT(status);
111 return status;
112 }
113 mMessage = String8(message);
114
115 if (mException == EX_SERVICE_SPECIFIC) {
116 status = parcel.readInt32(&mErrorCode);
117 } else if (mException == EX_PARCELABLE) {
118 // Skip over the blob of Parcelable data
119 const size_t header_start = parcel.dataPosition();
120 // Get available size before reading more
121 const size_t header_avail = parcel.dataAvail();
122
123 int32_t header_size;
124 status = parcel.readInt32(&header_size);
125 if (status != OK) {
126 setFromStatusT(status);
127 return status;
128 }
129
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
136 parcel.setDataPosition(header_start + header_size);
137 }
138 if (status != OK) {
139 setFromStatusT(status);
140 return status;
141 }
142
143 return status;
144 }
145
writeToParcel(Parcel * parcel) const146 status_t Status::writeToParcel(Parcel* parcel) const {
147 // 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
153 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));
160 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);
165 }
166 return status;
167 }
168
setException(int32_t ex,const String8 & message)169 void Status::setException(int32_t ex, const String8& message) {
170 mException = ex;
171 mErrorCode = NO_ERROR; // an exception, not a transaction failure.
172 mMessage.setTo(message);
173 }
174
setServiceSpecificError(int32_t errorCode,const String8 & message)175 void Status::setServiceSpecificError(int32_t errorCode, const String8& message) {
176 setException(EX_SERVICE_SPECIFIC, message);
177 mErrorCode = errorCode;
178 }
179
setFromStatusT(status_t status)180 void Status::setFromStatusT(status_t status) {
181 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
182 mErrorCode = status;
183 mMessage.clear();
184 }
185
toString8() const186 String8 Status::toString8() const {
187 String8 ret;
188 if (mException == EX_NONE) {
189 ret.append("No error");
190 } else {
191 ret.appendFormat("Status(%d): '", mException);
192 if (mException == EX_SERVICE_SPECIFIC ||
193 mException == EX_TRANSACTION_FAILED) {
194 ret.appendFormat("%d: ", mErrorCode);
195 }
196 ret.append(String8(mMessage));
197 ret.append("'");
198 }
199 return ret;
200 }
201
operator <<(std::stringstream & stream,const Status & s)202 std::stringstream& operator<< (std::stringstream& stream, const Status& s) {
203 stream << s.toString8().string();
204 return stream;
205 }
206
207 } // namespace binder
208 } // namespace android
209