blob: a04cf77ff515735fbae79402fdcf0041d9f08388 [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 <android-base/macros.h>
Yifan Honga7b2bb72017-03-29 03:36:07 -070024#include <hidl/HidlInternal.h>
Yifan Hong43298f92016-12-20 16:42:39 -080025#include <utils/Errors.h>
Martijn Coenend272cb92017-01-02 15:20:38 +010026#include <utils/StrongPointer.h>
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020027
28namespace android {
29namespace hardware {
30
31// An object similar in function to a status_t except that it understands
32// how exceptions are encoded in the prefix of a Parcel. Used like:
33//
34// Parcel data;
35// Parcel reply;
36// status_t status;
37// binder::Status remote_exception;
38// if ((status = data.writeInterfaceToken(interface_descriptor)) != OK ||
39// (status = data.writeInt32(function_input)) != OK) {
40// // We failed to write into the memory of our local parcel?
41// }
42// if ((status = remote()->transact(transaction, data, &reply)) != OK) {
43// // Something has gone wrong in the binder driver or libbinder.
44// }
45// if ((status = remote_exception.readFromParcel(reply)) != OK) {
46// // The remote didn't correctly write the exception header to the
47// // reply.
48// }
49// if (!remote_exception.isOk()) {
50// // The transaction went through correctly, but the remote reported an
51// // exception during handling.
52// }
53//
54class Status final {
55public:
56 // Keep the exception codes in sync with android/os/Parcel.java.
57 enum Exception {
58 EX_NONE = 0,
59 EX_SECURITY = -1,
60 EX_BAD_PARCELABLE = -2,
61 EX_ILLEGAL_ARGUMENT = -3,
62 EX_NULL_POINTER = -4,
63 EX_ILLEGAL_STATE = -5,
64 EX_NETWORK_MAIN_THREAD = -6,
65 EX_UNSUPPORTED_OPERATION = -7,
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020066
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)
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020078 // - status_t
79 //
Steven Moreland72db40f2017-03-09 18:15:27 -080080 // Prefer a generic exception code when possible or a status_t
81 // for low level transport errors. Service specific errors
82 // should be at a higher level in HIDL.
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020083 static Status fromExceptionCode(int32_t exceptionCode);
84 static Status fromExceptionCode(int32_t exceptionCode,
Yifan Hong43298f92016-12-20 16:42:39 -080085 const char *message);
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020086 static Status fromStatusT(status_t status);
87
88 Status() = default;
89 ~Status() = default;
90
91 // Status objects are copyable and contain just simple data.
92 Status(const Status& status) = default;
93 Status(Status&& status) = default;
94 Status& operator=(const Status& status) = default;
95
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020096 // Set one of the pre-defined exception types defined above.
Yifan Hong43298f92016-12-20 16:42:39 -080097 void setException(int32_t ex, const char *message);
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020098 // Setting a |status| != OK causes generated code to return |status|
99 // from Binder transactions, rather than writing an exception into the
100 // reply Parcel. This is the least preferable way of reporting errors.
101 void setFromStatusT(status_t status);
102
103 // Get information about an exception.
104 int32_t exceptionCode() const { return mException; }
Yifan Hong43298f92016-12-20 16:42:39 -0800105 const char *exceptionMessage() const { return mMessage.c_str(); }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200106 status_t transactionError() const {
107 return mException == EX_TRANSACTION_FAILED ? mErrorCode : OK;
108 }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200109
110 bool isOk() const { return mException == EX_NONE; }
111
Yifan Hong43298f92016-12-20 16:42:39 -0800112 // For debugging purposes only
113 std::string description() const;
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200114
115private:
116 Status(int32_t exceptionCode, int32_t errorCode);
Yifan Hong43298f92016-12-20 16:42:39 -0800117 Status(int32_t exceptionCode, int32_t errorCode, const char *message);
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200118
119 // If |mException| == EX_TRANSACTION_FAILED, generated code will return
120 // |mErrorCode| as the result of the transaction rather than write an
121 // exception to the reply parcel.
122 //
123 // Otherwise, we always write |mException| to the parcel.
124 // If |mException| != EX_NONE, we write |mMessage| as well.
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200125 int32_t mException = EX_NONE;
126 int32_t mErrorCode = 0;
Yifan Hong43298f92016-12-20 16:42:39 -0800127 std::string mMessage;
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200128}; // class Status
129
130// For gtest output logging
Yifan Hong43298f92016-12-20 16:42:39 -0800131std::ostream& operator<< (std::ostream& stream, const Status& s);
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200132
Yifan Honga7b2bb72017-03-29 03:36:07 -0700133template<typename T> class Return;
134
Steven Moreland75969332016-12-12 16:00:25 -0800135namespace details {
Steven Morelande780c452017-01-17 17:22:54 -0800136 class return_status {
Steven Moreland75969332016-12-12 16:00:25 -0800137 private:
138 Status mStatus {};
139 mutable bool mCheckedStatus = false;
Yifan Honga7b2bb72017-03-29 03:36:07 -0700140
141 template <typename T, typename U>
142 friend Return<U> StatusOf(const Return<T> &other);
Martijn Coenend272cb92017-01-02 15:20:38 +0100143 protected:
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800144 void assertOk() const;
Steven Moreland75969332016-12-12 16:00:25 -0800145 public:
146 return_status() {}
147 return_status(Status s) : mStatus(s) {}
148
Yifan Hong603cde92017-02-17 14:50:44 -0800149 return_status(const return_status &) = delete;
150 return_status &operator=(const return_status &) = delete;
Steven Moreland75969332016-12-12 16:00:25 -0800151
Yifan Hongc43bd9d2017-02-17 14:11:48 -0800152 return_status(return_status &&other) {
153 *this = std::move(other);
154 }
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800155 return_status &operator=(return_status &&other);
Yifan Hongc43bd9d2017-02-17 14:11:48 -0800156
Steven Morelande780c452017-01-17 17:22:54 -0800157 ~return_status();
Steven Moreland75969332016-12-12 16:00:25 -0800158
159 bool isOk() const {
160 mCheckedStatus = true;
161 return mStatus.isOk();
162 }
163
Yifan Honga7b2bb72017-03-29 03:36:07 -0700164 // Check if underlying error is DEAD_OBJECT.
165 // Does not set mCheckedStatus.
166 bool isDeadObject() const {
167 return mStatus.transactionError() == DEAD_OBJECT;
168 }
169
Yifan Hong43298f92016-12-20 16:42:39 -0800170 // For debugging purposes only
171 std::string description() const {
172 // Doesn't consider checked.
173 return mStatus.description();
174 }
Steven Moreland75969332016-12-12 16:00:25 -0800175 };
176} // namespace details
177
178template<typename T> class Return : public details::return_status {
Iliyan Malchev170c1892016-09-08 13:55:53 -0700179private:
Steven Moreland337c3ae2016-11-22 13:37:32 -0800180 T mVal {};
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200181public:
Steven Moreland75969332016-12-12 16:00:25 -0800182 Return(T v) : details::return_status(), mVal{v} {}
183 Return(Status s) : details::return_status(s) {}
Steven Moreland337c3ae2016-11-22 13:37:32 -0800184
Yifan Hongc43bd9d2017-02-17 14:11:48 -0800185 // move-able.
186 // precondition: "this" has checked status
187 // postcondition: other is safe to destroy after moving to *this.
188 Return(Return &&other) = default;
189 Return &operator=(Return &&) = default;
190
Steven Moreland75969332016-12-12 16:00:25 -0800191 ~Return() = default;
Steven Moreland1cfaee72016-12-01 08:34:19 -0800192
193 operator T() const {
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800194 assertOk();
Steven Moreland337c3ae2016-11-22 13:37:32 -0800195 return mVal;
196 }
Steven Moreland337c3ae2016-11-22 13:37:32 -0800197
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200198};
199
Martijn Coenend272cb92017-01-02 15:20:38 +0100200template<typename T> class Return<sp<T>> : public details::return_status {
201private:
202 sp<T> mVal {};
203public:
204 Return(sp<T> v) : details::return_status(), mVal{v} {}
205 Return(T* v) : details::return_status(), mVal{v} {}
206 // Constructors matching a different type (that is related by inheritance)
207 template<typename U> Return(sp<U> v) : details::return_status(), mVal{v} {}
208 template<typename U> Return(U* v) : details::return_status(), mVal{v} {}
209 Return(Status s) : details::return_status(s) {}
210
Yifan Hongc43bd9d2017-02-17 14:11:48 -0800211 // move-able.
212 // precondition: "this" has checked status
213 // postcondition: other is safe to destroy after moving to *this.
214 Return(Return &&other) = default;
215 Return &operator=(Return &&) = default;
216
Martijn Coenend272cb92017-01-02 15:20:38 +0100217 ~Return() = default;
218
219 operator sp<T>() const {
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800220 assertOk();
Martijn Coenend272cb92017-01-02 15:20:38 +0100221 return mVal;
222 }
223};
224
225
Steven Moreland75969332016-12-12 16:00:25 -0800226template<> class Return<void> : public details::return_status {
Iliyan Malchev170c1892016-09-08 13:55:53 -0700227public:
Steven Moreland75969332016-12-12 16:00:25 -0800228 Return() : details::return_status() {}
229 Return(Status s) : details::return_status(s) {}
Steven Moreland337c3ae2016-11-22 13:37:32 -0800230
Yifan Hongc43bd9d2017-02-17 14:11:48 -0800231 // move-able.
232 // precondition: "this" has checked status
233 // postcondition: other is safe to destroy after moving to *this.
234 Return(Return &&) = default;
235 Return &operator=(Return &&) = default;
236
Steven Moreland75969332016-12-12 16:00:25 -0800237 ~Return() = default;
Iliyan Malchev170c1892016-09-08 13:55:53 -0700238};
239
240static inline Return<void> Void() {
241 return Return<void>();
242}
243
Yifan Honga7b2bb72017-03-29 03:36:07 -0700244namespace details {
245// Create a Return<U> from the Status of Return<T>. The provided
246// Return<T> must have an error status and have it checked.
247template <typename T, typename U>
248Return<U> StatusOf(const Return<T> &other) {
249 if (other.mStatus.isOk() || !other.mCheckedStatus) {
250 details::logAlwaysFatal("cannot call statusOf on an OK Status or an unchecked status");
251 }
252 return Return<U>{other.mStatus};
253}
254} // namespace details
255
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200256} // namespace hardware
257} // namespace android
258
259#endif // ANDROID_HARDWARE_BINDER_STATUS_H