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 int32_t header_start = parcel.dataPosition();
80         int32_t header_size;
81         status = parcel.readInt32(&header_size);
82         if (status != OK) {
83             setFromStatusT(status);
84             return status;
85         }
86         parcel.setDataPosition(header_start + header_size);
87         // And fat response headers are currently only used when there are no
88         // exceptions, so act like there was no error.
89         mException = EX_NONE;
90     }
91 
92     if (mException == EX_NONE) {
93         return status;
94     }
95 
96     // The remote threw an exception.  Get the message back.
97     String16 message;
98     status = parcel.readString16(&message);
99     if (status != OK) {
100         setFromStatusT(status);
101         return status;
102     }
103     mMessage = String8(message);
104 
105     // Skip over the remote stack trace data
106     int32_t remote_stack_trace_header_size;
107     status = parcel.readInt32(&remote_stack_trace_header_size);
108     if (status != OK) {
109         setFromStatusT(status);
110         return status;
111     }
112     parcel.setDataPosition(parcel.dataPosition() + remote_stack_trace_header_size);
113 
114     if (mException == EX_SERVICE_SPECIFIC) {
115         status = parcel.readInt32(&mErrorCode);
116     } else if (mException == EX_PARCELABLE) {
117         // Skip over the blob of Parcelable data
118         const int32_t header_start = parcel.dataPosition();
119         int32_t header_size;
120         status = parcel.readInt32(&header_size);
121         if (status != OK) {
122             setFromStatusT(status);
123             return status;
124         }
125         parcel.setDataPosition(header_start + header_size);
126     }
127     if (status != OK) {
128         setFromStatusT(status);
129         return status;
130     }
131 
132     return status;
133 }
134 
writeToParcel(Parcel * parcel) const135 status_t Status::writeToParcel(Parcel* parcel) const {
136     // Something really bad has happened, and we're not going to even
137     // try returning rich error data.
138     if (mException == EX_TRANSACTION_FAILED) {
139         return mErrorCode;
140     }
141 
142     status_t status = parcel->writeInt32(mException);
143     if (status != OK) { return status; }
144     if (mException == EX_NONE) {
145         // We have no more information to write.
146         return status;
147     }
148     status = parcel->writeString16(String16(mMessage));
149     status = parcel->writeInt32(0); // Empty remote stack trace header
150     if (mException == EX_SERVICE_SPECIFIC) {
151         status = parcel->writeInt32(mErrorCode);
152     } else if (mException == EX_PARCELABLE) {
153         // Sending Parcelable blobs currently not supported
154         status = parcel->writeInt32(0);
155     }
156     return status;
157 }
158 
setException(int32_t ex,const String8 & message)159 void Status::setException(int32_t ex, const String8& message) {
160     mException = ex;
161     mErrorCode = NO_ERROR;  // an exception, not a transaction failure.
162     mMessage.setTo(message);
163 }
164 
setServiceSpecificError(int32_t errorCode,const String8 & message)165 void Status::setServiceSpecificError(int32_t errorCode, const String8& message) {
166     setException(EX_SERVICE_SPECIFIC, message);
167     mErrorCode = errorCode;
168 }
169 
setFromStatusT(status_t status)170 void Status::setFromStatusT(status_t status) {
171     mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
172     mErrorCode = status;
173     mMessage.clear();
174 }
175 
toString8() const176 String8 Status::toString8() const {
177     String8 ret;
178     if (mException == EX_NONE) {
179         ret.append("No error");
180     } else {
181         ret.appendFormat("Status(%d): '", mException);
182         if (mException == EX_SERVICE_SPECIFIC ||
183             mException == EX_TRANSACTION_FAILED) {
184             ret.appendFormat("%d: ", mErrorCode);
185         }
186         ret.append(String8(mMessage));
187         ret.append("'");
188     }
189     return ret;
190 }
191 
operator <<(std::stringstream & stream,const Status & s)192 std::stringstream& operator<< (std::stringstream& stream, const Status& s) {
193     stream << s.toString8().string();
194     return stream;
195 }
196 
197 }  // namespace binder
198 }  // namespace android
199