Status.cpp revision cff7f175c1a4f790fdc64a56695c5b4b08b6bb6e
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
19namespace android {
20namespace binder {
21
22Status Status::ok() {
23    return Status();
24}
25
26Status Status::fromExceptionCode(int32_t exceptionCode) {
27    return Status(exceptionCode);
28}
29
30Status Status::fromExceptionCode(int32_t exceptionCode,
31                                 const String8& message) {
32    return Status(exceptionCode, message);
33}
34
35Status Status::fromStatusT(status_t status) {
36    Status ret;
37    ret.setFromStatusT(status);
38    return ret;
39}
40
41Status::Status(int32_t exceptionCode) : mException(exceptionCode) {}
42Status::Status(int32_t exceptionCode, const String8& message)
43    : mException(exceptionCode),
44      mMessage(message) {}
45
46status_t Status::readFromParcel(const Parcel& parcel) {
47    status_t status = parcel.readInt32(&mException);
48    if (status != OK) {
49        setFromStatusT(status);
50        return status;
51    }
52
53    // Skip over fat response headers.  Not used (or propagated) in native code.
54    if (mException == EX_HAS_REPLY_HEADER) {
55        // Note that the header size includes the 4 byte size field.
56        const int32_t header_start = parcel.dataPosition();
57        int32_t header_size;
58        status = parcel.readInt32(&header_size);
59        if (status != OK) {
60            setFromStatusT(status);
61            return status;
62        }
63        parcel.setDataPosition(header_start + header_size);
64        // And fat response headers are currently only used when there are no
65        // exceptions, so act like there was no error.
66        mException = EX_NONE;
67    }
68
69    if (mException == EX_NONE) {
70        return status;
71    }
72
73    // The remote threw an exception.  Get the message back.
74    String16 message;
75    status = parcel.readString16(&message);
76    if (status != OK) {
77        setFromStatusT(status);
78        return status;
79    }
80    mMessage = String8(message);
81
82    return status;
83}
84
85status_t Status::writeToParcel(Parcel* parcel) const {
86    // Something really bad has happened, and we're not going to even
87    // try returning rich error data.
88    if (mException == EX_TRANSACTION_FAILED) {
89        return mErrorCode;
90    }
91
92    status_t status = parcel->writeInt32(mException);
93    if (status != OK) { return status; }
94    if (mException == EX_NONE) {
95        // We have no more information to write.
96        return status;
97    }
98    status = parcel->writeString16(String16(mMessage));
99    return status;
100}
101
102void Status::setFromStatusT(status_t status) {
103    mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
104    mErrorCode = status;
105    mMessage.clear();
106}
107
108void Status::setException(int32_t ex, const String8& message) {
109    mException = ex;
110    mErrorCode = NO_ERROR;  // an exception, not a transaction failure.
111    mMessage.setTo(message);
112}
113
114String8 Status::toString8() const {
115    String8 ret;
116    if (mException == EX_NONE) {
117        ret.append("No error");
118    } else {
119        ret.appendFormat("Status(%d): '", mException);
120        if (mException == EX_TRANSACTION_FAILED) {
121            ret.appendFormat("%d: ", mErrorCode);
122        }
123        ret.append(String8(mMessage));
124        ret.append("'");
125    }
126    return ret;
127}
128
129}  // namespace binder
130}  // namespace android
131