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 #ifndef ANDROID_BINDER_STATUS_H
18 #define ANDROID_BINDER_STATUS_H
19 
20 #include <cstdint>
21 #include <sstream> // historical
22 #include <ostream>
23 
24 #include <binder/Common.h>
25 #include <binder/Parcel.h>
26 #include <utils/String8.h>
27 #include <string>
28 
29 namespace android {
30 namespace binder {
31 
32 // An object similar in function to a status_t except that it understands
33 // how exceptions are encoded in the prefix of a Parcel. Used like:
34 //
35 //     Parcel data;
36 //     Parcel reply;
37 //     status_t status;
38 //     binder::Status remote_exception;
39 //     if ((status = data.writeInterfaceToken(interface_descriptor)) != OK ||
40 //         (status = data.writeInt32(function_input)) != OK) {
41 //         // We failed to write into the memory of our local parcel?
42 //     }
43 //     if ((status = remote()->transact(transaction, data, &reply)) != OK) {
44 //        // Something has gone wrong in the binder driver or libbinder.
45 //     }
46 //     if ((status = remote_exception.readFromParcel(reply)) != OK) {
47 //         // The remote didn't correctly write the exception header to the
48 //         // reply.
49 //     }
50 //     if (!remote_exception.isOk()) {
51 //         // The transaction went through correctly, but the remote reported an
52 //         // exception during handling.
53 //     }
54 //
55 class LIBBINDER_EXPORTED Status final {
56 public:
57     // Keep the exception codes in sync with android/os/Parcel.java.
58     enum Exception {
59         EX_NONE = 0,
60         EX_SECURITY = -1,
61         EX_BAD_PARCELABLE = -2,
62         EX_ILLEGAL_ARGUMENT = -3,
63         EX_NULL_POINTER = -4,
64         EX_ILLEGAL_STATE = -5,
65         EX_NETWORK_MAIN_THREAD = -6,
66         EX_UNSUPPORTED_OPERATION = -7,
67         EX_SERVICE_SPECIFIC = -8,
68         EX_PARCELABLE = -9,
69 
70         // This is special and Java specific; see Parcel.java.
71         EX_HAS_REPLY_HEADER = -128,
72         // This is special, and indicates to C++ binder proxies that the
73         // transaction has failed at a low level.
74         EX_TRANSACTION_FAILED = -129,
75     };
76 
77     // A more readable alias for the default constructor.
78     static Status ok();
79 
80     // Authors should explicitly pick whether their integer is:
81     //  - an exception code (EX_* above)
82     //  - service specific error code
83     //  - status_t
84     //
85     //  Prefer a generic exception code when possible, then a service specific
86     //  code, and finally a status_t for low level failures or legacy support.
87     //  Exception codes and service specific errors map to nicer exceptions for
88     //  Java clients.
89     static Status fromExceptionCode(int32_t exceptionCode);
90     static Status fromExceptionCode(int32_t exceptionCode,
91                                     const String8& message);
92     static Status fromExceptionCode(int32_t exceptionCode,
93                                     const char* message);
94 
95     // warning: this is still considered an error if it is constructed with a
96     // zero value error code. Please use Status::ok() instead and avoid zero
97     // error codes
98     static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode);
99     static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode,
100                                            const String8& message);
101     static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode,
102                                            const char* message);
103 
104     static Status fromStatusT(status_t status);
105 
106     static std::string exceptionToString(status_t exceptionCode);
107 
108     Status() = default;
109     ~Status() = default;
110 
111     // Status objects are copyable and contain just simple data.
112     Status(const Status& status) = default;
113     Status(Status&& status) = default;
114     Status& operator=(const Status& status) = default;
115 
116     // Bear in mind that if the client or service is a Java endpoint, this
117     // is not the logic which will provide/interpret the data here.
118     status_t readFromParcel(const Parcel& parcel);
119     status_t writeToParcel(Parcel* parcel) const;
120 
121     // Convenience API to replace a Parcel with a status value, w/o requiring
122     // calling multiple APIs (makes generated code smaller).
123     status_t writeOverParcel(Parcel* parcel) const;
124 
125     // Set one of the pre-defined exception types defined above.
126     void setException(int32_t ex, const String8& message);
127     // Set a service specific exception with error code.
128     void setServiceSpecificError(int32_t errorCode, const String8& message);
129     // Setting a |status| != OK causes generated code to return |status|
130     // from Binder transactions, rather than writing an exception into the
131     // reply Parcel.  This is the least preferable way of reporting errors.
132     void setFromStatusT(status_t status);
133 
134     // Get information about an exception.
exceptionCode()135     int32_t exceptionCode() const  { return mException; }
exceptionMessage()136     const String8& exceptionMessage() const { return mMessage; }
transactionError()137     status_t transactionError() const {
138         return mException == EX_TRANSACTION_FAILED ? mErrorCode : OK;
139     }
serviceSpecificErrorCode()140     int32_t serviceSpecificErrorCode() const {
141         return mException == EX_SERVICE_SPECIFIC ? mErrorCode : 0;
142     }
143 
isOk()144     bool isOk() const { return mException == EX_NONE; }
145 
146     // For logging.
147     String8 toString8() const;
148 
149 private:
150     Status(int32_t exceptionCode, int32_t errorCode);
151     Status(int32_t exceptionCode, int32_t errorCode, const String8& message);
152 
153     // If |mException| == EX_TRANSACTION_FAILED, generated code will return
154     // |mErrorCode| as the result of the transaction rather than write an
155     // exception to the reply parcel.
156     //
157     // Otherwise, we always write |mException| to the parcel.
158     // If |mException| !=  EX_NONE, we write |mMessage| as well.
159     // If |mException| == EX_SERVICE_SPECIFIC we write |mErrorCode| as well.
160     int32_t mException = EX_NONE;
161     int32_t mErrorCode = 0;
162     String8 mMessage;
163 };  // class Status
164 
165 static inline std::ostream& operator<< (std::ostream& o, const Status& s) {
166     return o << s.toString8();
167 }
168 
169 }  // namespace binder
170 }  // namespace android
171 
172 #endif // ANDROID_BINDER_STATUS_H
173