blob: 59e4d65bf32d847b896d6850379dbdcb26603369 [file] [log] [blame]
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +02001/*
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_HARDWARE_BINDER_STATUS_H
18#define ANDROID_HARDWARE_BINDER_STATUS_H
19
20#include <cstdint>
21#include <sstream>
22
23#include <hwbinder/Parcel.h>
24#include <utils/String8.h>
25#include <android-base/macros.h>
26
27namespace android {
28namespace hardware {
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//
53class Status final {
54public:
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
67 // This is special and Java specific; see Parcel.java.
68 EX_HAS_REPLY_HEADER = -128,
69 // This is special, and indicates to C++ binder proxies that the
70 // transaction has failed at a low level.
71 EX_TRANSACTION_FAILED = -129,
72 };
73
74 // A more readable alias for the default constructor.
75 static Status ok();
76 // Authors should explicitly pick whether their integer is:
77 // - an exception code (EX_* above)
78 // - service specific error code
79 // - status_t
80 //
81 // Prefer a generic exception code when possible, then a service specific
82 // code, and finally a status_t for low level failures or legacy support.
83 // Exception codes and service specific errors map to nicer exceptions for
84 // Java clients.
85 static Status fromExceptionCode(int32_t exceptionCode);
86 static Status fromExceptionCode(int32_t exceptionCode,
87 const String8& message);
88 static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode);
89 static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode,
90 const String8& message);
91 static Status fromStatusT(status_t status);
92
93 Status() = default;
94 ~Status() = default;
95
96 // Status objects are copyable and contain just simple data.
97 Status(const Status& status) = default;
98 Status(Status&& status) = default;
99 Status& operator=(const Status& status) = default;
100
101 // Bear in mind that if the client or service is a Java endpoint, this
102 // is not the logic which will provide/interpret the data here.
103 status_t readFromParcel(const Parcel& parcel);
104 status_t writeToParcel(Parcel* parcel) const;
105
106 // Set one of the pre-defined exception types defined above.
107 void setException(int32_t ex, const String8& message);
108 // Set a service specific exception with error code.
109 void setServiceSpecificError(int32_t errorCode, const String8& message);
110 // Setting a |status| != OK causes generated code to return |status|
111 // from Binder transactions, rather than writing an exception into the
112 // reply Parcel. This is the least preferable way of reporting errors.
113 void setFromStatusT(status_t status);
114
115 // Get information about an exception.
116 int32_t exceptionCode() const { return mException; }
117 const String8& exceptionMessage() const { return mMessage; }
118 status_t transactionError() const {
119 return mException == EX_TRANSACTION_FAILED ? mErrorCode : OK;
120 }
121 int32_t serviceSpecificErrorCode() const {
122 return mException == EX_SERVICE_SPECIFIC ? mErrorCode : 0;
123 }
124
125 bool isOk() const { return mException == EX_NONE; }
126
127 // For logging.
128 String8 toString8() const;
129
130private:
131 Status(int32_t exceptionCode, int32_t errorCode);
132 Status(int32_t exceptionCode, int32_t errorCode, const String8& message);
133
134 // If |mException| == EX_TRANSACTION_FAILED, generated code will return
135 // |mErrorCode| as the result of the transaction rather than write an
136 // exception to the reply parcel.
137 //
138 // Otherwise, we always write |mException| to the parcel.
139 // If |mException| != EX_NONE, we write |mMessage| as well.
140 // If |mException| == EX_SERVICE_SPECIFIC we write |mErrorCode| as well.
141 int32_t mException = EX_NONE;
142 int32_t mErrorCode = 0;
143 String8 mMessage;
144}; // class Status
145
146// For gtest output logging
147std::stringstream& operator<< (std::stringstream& stream, const Status& s);
148
149template<typename T> class Return {
Iliyan Malchev170c1892016-09-08 13:55:53 -0700150private:
151 T val {};
152 Status status {};
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200153public:
Iliyan Malchev170c1892016-09-08 13:55:53 -0700154 Return(T v) : val{v} {}
155 Return(Status s) : status(s) {}
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200156 operator T() { return val; }
Iliyan Malchev170c1892016-09-08 13:55:53 -0700157 const Status& getStatus() const {
158 return status;
159 }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200160};
161
Iliyan Malchev170c1892016-09-08 13:55:53 -0700162template<> class Return<void> {
163private:
164 Status status {};
165public:
166 Return() = default;
167 Return(Status s) : status(s) {}
168 const Status& getStatus() const {
169 return status;
170 }
171};
172
173static inline Return<void> Void() {
174 return Return<void>();
175}
176
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200177} // namespace hardware
178} // namespace android
179
180#endif // ANDROID_HARDWARE_BINDER_STATUS_H