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