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