blob: 765f0f7e8c7942a5fe068a1acef175233c4e6a36 [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
Yifan Honga7b2bb72017-03-29 03:36:07 -070023#include <hidl/HidlInternal.h>
Yifan Hong43298f92016-12-20 16:42:39 -080024#include <utils/Errors.h>
Martijn Coenend272cb92017-01-02 15:20:38 +010025#include <utils/StrongPointer.h>
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020026
27namespace android {
28namespace hardware {
29
Steven Moreland779a7142019-03-05 16:59:26 -080030// HIDL formally separates transport error codes from interface error codes. When developing a HIDL
31// interface, errors relevant to a service should be placed in the interface design for that HAL.
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020032//
Steven Moreland779a7142019-03-05 16:59:26 -080033// For instance:
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020034//
Steven Moreland779a7142019-03-05 16:59:26 -080035// interface I* {
36// enum FooStatus { NO_FOO, NO_BAR }; // service-specific errors
37// doFoo(...) generates (FooStatus foo);
38// };
39//
40// When calling into this interface, a Return<*> (in this case Return<FooStatus> object will be
41// returned). For most clients, it's expected that they'll just get the result from this function
42// and use it directly. If there is a transport error, the process will just abort. In general,
43// transport errors are expected only in extremely rare circumstances (bug in the
44// code/cosmic radiation/etc..). Aborting allows process to restart using their normal happy path
45// code.
46//
47// For certain processes though which are critical to the functionality of the phone (e.g.
48// hwservicemanager/init), these errors must be handled. Return<*>::isOk and
49// Return<*>::isDeadObject are provided for these cases. Whenever this is done, special attention
50// should be paid to testing the unhappy paths to make sure that error handling is handled
51// properly.
52
53// Transport implementation detail. HIDL implementors, see Return below. HAL implementations should
54// return HIDL-defined errors rather than use this.
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020055class Status final {
56public:
Steven Moreland779a7142019-03-05 16:59:26 -080057 // Note: forked from
58 // - frameworks/base/core/java/android/os/android/os/Parcel.java.
59 // - frameworks/native/libs/binder/include/binder/Status.h
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020060 enum Exception {
61 EX_NONE = 0,
62 EX_SECURITY = -1,
63 EX_BAD_PARCELABLE = -2,
64 EX_ILLEGAL_ARGUMENT = -3,
65 EX_NULL_POINTER = -4,
66 EX_ILLEGAL_STATE = -5,
67 EX_NETWORK_MAIN_THREAD = -6,
68 EX_UNSUPPORTED_OPERATION = -7,
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020069
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 // Authors should explicitly pick whether their integer is:
80 // - an exception code (EX_* above)
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020081 // - status_t
82 //
Steven Moreland72db40f2017-03-09 18:15:27 -080083 // Prefer a generic exception code when possible or a status_t
84 // for low level transport errors. Service specific errors
85 // should be at a higher level in HIDL.
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020086 static Status fromExceptionCode(int32_t exceptionCode);
87 static Status fromExceptionCode(int32_t exceptionCode,
Yifan Hong43298f92016-12-20 16:42:39 -080088 const char *message);
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020089 static Status fromStatusT(status_t status);
90
91 Status() = default;
92 ~Status() = default;
93
94 // Status objects are copyable and contain just simple data.
95 Status(const Status& status) = default;
96 Status(Status&& status) = default;
97 Status& operator=(const Status& status) = default;
98
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020099 // Set one of the pre-defined exception types defined above.
Yifan Hong43298f92016-12-20 16:42:39 -0800100 void setException(int32_t ex, const char *message);
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200101 // Setting a |status| != OK causes generated code to return |status|
102 // from Binder transactions, rather than writing an exception into the
103 // reply Parcel. This is the least preferable way of reporting errors.
104 void setFromStatusT(status_t status);
105
106 // Get information about an exception.
107 int32_t exceptionCode() const { return mException; }
Yifan Hong43298f92016-12-20 16:42:39 -0800108 const char *exceptionMessage() const { return mMessage.c_str(); }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200109 status_t transactionError() const {
110 return mException == EX_TRANSACTION_FAILED ? mErrorCode : OK;
111 }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200112
113 bool isOk() const { return mException == EX_NONE; }
114
Yifan Hong43298f92016-12-20 16:42:39 -0800115 // For debugging purposes only
116 std::string description() const;
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200117
118private:
119 Status(int32_t exceptionCode, int32_t errorCode);
Yifan Hong43298f92016-12-20 16:42:39 -0800120 Status(int32_t exceptionCode, int32_t errorCode, const char *message);
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200121
122 // If |mException| == EX_TRANSACTION_FAILED, generated code will return
123 // |mErrorCode| as the result of the transaction rather than write an
124 // exception to the reply parcel.
125 //
126 // Otherwise, we always write |mException| to the parcel.
127 // If |mException| != EX_NONE, we write |mMessage| as well.
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200128 int32_t mException = EX_NONE;
129 int32_t mErrorCode = 0;
Yifan Hong43298f92016-12-20 16:42:39 -0800130 std::string mMessage;
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200131}; // class Status
132
133// For gtest output logging
Yifan Hong43298f92016-12-20 16:42:39 -0800134std::ostream& operator<< (std::ostream& stream, const Status& s);
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200135
Yifan Honga7b2bb72017-03-29 03:36:07 -0700136template<typename T> class Return;
137
Steven Moreland75969332016-12-12 16:00:25 -0800138namespace details {
Steven Morelande780c452017-01-17 17:22:54 -0800139 class return_status {
Steven Moreland75969332016-12-12 16:00:25 -0800140 private:
141 Status mStatus {};
142 mutable bool mCheckedStatus = false;
Yifan Honga7b2bb72017-03-29 03:36:07 -0700143
144 template <typename T, typename U>
145 friend Return<U> StatusOf(const Return<T> &other);
Martijn Coenend272cb92017-01-02 15:20:38 +0100146 protected:
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800147 void assertOk() const;
Steven Moreland75969332016-12-12 16:00:25 -0800148 public:
149 return_status() {}
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700150 return_status(const Status& s) : mStatus(s) {}
Steven Moreland75969332016-12-12 16:00:25 -0800151
Yifan Hong603cde92017-02-17 14:50:44 -0800152 return_status(const return_status &) = delete;
153 return_status &operator=(const return_status &) = delete;
Steven Moreland75969332016-12-12 16:00:25 -0800154
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -0700155 return_status(return_status&& other) noexcept { *this = std::move(other); }
156 return_status& operator=(return_status&& other) noexcept;
Yifan Hongc43bd9d2017-02-17 14:11:48 -0800157
Steven Morelande780c452017-01-17 17:22:54 -0800158 ~return_status();
Steven Moreland75969332016-12-12 16:00:25 -0800159
Steven Morelandff189a02017-09-15 16:22:48 -0700160 bool isOkUnchecked() const {
161 // someone else will have to check
162 return mStatus.isOk();
163 }
164
Steven Moreland75969332016-12-12 16:00:25 -0800165 bool isOk() const {
166 mCheckedStatus = true;
167 return mStatus.isOk();
168 }
169
Yifan Honga7b2bb72017-03-29 03:36:07 -0700170 // Check if underlying error is DEAD_OBJECT.
Steven Moreland13f7f622017-06-19 18:02:27 -0700171 // Check mCheckedStatus only if this method returns true.
Yifan Honga7b2bb72017-03-29 03:36:07 -0700172 bool isDeadObject() const {
Steven Moreland13f7f622017-06-19 18:02:27 -0700173 bool dead = mStatus.transactionError() == DEAD_OBJECT;
174
175 // This way, if you only check isDeadObject your process will
176 // only be killed for more serious unchecked errors
177 if (dead) {
178 mCheckedStatus = true;
179 }
180
181 return dead;
Yifan Honga7b2bb72017-03-29 03:36:07 -0700182 }
183
Yifan Hong43298f92016-12-20 16:42:39 -0800184 // For debugging purposes only
185 std::string description() const {
186 // Doesn't consider checked.
187 return mStatus.description();
188 }
Steven Moreland75969332016-12-12 16:00:25 -0800189 };
190} // namespace details
191
192template<typename T> class Return : public details::return_status {
Iliyan Malchev170c1892016-09-08 13:55:53 -0700193private:
Steven Moreland337c3ae2016-11-22 13:37:32 -0800194 T mVal {};
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200195public:
Steven Moreland75969332016-12-12 16:00:25 -0800196 Return(T v) : details::return_status(), mVal{v} {}
197 Return(Status s) : details::return_status(s) {}
Steven Moreland337c3ae2016-11-22 13:37:32 -0800198
Yifan Hongc43bd9d2017-02-17 14:11:48 -0800199 // move-able.
200 // precondition: "this" has checked status
201 // postcondition: other is safe to destroy after moving to *this.
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -0700202 Return(Return&& other) noexcept = default;
203 Return& operator=(Return&&) noexcept = default;
Yifan Hongc43bd9d2017-02-17 14:11:48 -0800204
Steven Moreland75969332016-12-12 16:00:25 -0800205 ~Return() = default;
Steven Moreland1cfaee72016-12-01 08:34:19 -0800206
207 operator T() const {
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800208 assertOk();
Steven Moreland337c3ae2016-11-22 13:37:32 -0800209 return mVal;
210 }
Steven Moreland337c3ae2016-11-22 13:37:32 -0800211
Steven Moreland49172962017-04-28 16:08:43 -0700212 T withDefault(T t) {
213 return isOk() ? mVal : t;
214 }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200215};
216
Martijn Coenend272cb92017-01-02 15:20:38 +0100217template<typename T> class Return<sp<T>> : public details::return_status {
218private:
219 sp<T> mVal {};
220public:
221 Return(sp<T> v) : details::return_status(), mVal{v} {}
222 Return(T* v) : details::return_status(), mVal{v} {}
223 // Constructors matching a different type (that is related by inheritance)
224 template<typename U> Return(sp<U> v) : details::return_status(), mVal{v} {}
225 template<typename U> Return(U* v) : details::return_status(), mVal{v} {}
226 Return(Status s) : details::return_status(s) {}
227
Yifan Hongc43bd9d2017-02-17 14:11:48 -0800228 // move-able.
229 // precondition: "this" has checked status
230 // postcondition: other is safe to destroy after moving to *this.
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -0700231 Return(Return&& other) noexcept = default;
232 Return& operator=(Return&&) noexcept = default;
Yifan Hongc43bd9d2017-02-17 14:11:48 -0800233
Martijn Coenend272cb92017-01-02 15:20:38 +0100234 ~Return() = default;
235
236 operator sp<T>() const {
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800237 assertOk();
Martijn Coenend272cb92017-01-02 15:20:38 +0100238 return mVal;
239 }
Steven Moreland49172962017-04-28 16:08:43 -0700240
241 sp<T> withDefault(sp<T> t) {
242 return isOk() ? mVal : t;
243 }
Martijn Coenend272cb92017-01-02 15:20:38 +0100244};
245
246
Steven Moreland75969332016-12-12 16:00:25 -0800247template<> class Return<void> : public details::return_status {
Iliyan Malchev170c1892016-09-08 13:55:53 -0700248public:
Steven Moreland75969332016-12-12 16:00:25 -0800249 Return() : details::return_status() {}
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700250 Return(const Status& s) : details::return_status(s) {}
Steven Moreland337c3ae2016-11-22 13:37:32 -0800251
Yifan Hongc43bd9d2017-02-17 14:11:48 -0800252 // move-able.
253 // precondition: "this" has checked status
254 // postcondition: other is safe to destroy after moving to *this.
255 Return(Return &&) = default;
256 Return &operator=(Return &&) = default;
257
Steven Moreland75969332016-12-12 16:00:25 -0800258 ~Return() = default;
Iliyan Malchev170c1892016-09-08 13:55:53 -0700259};
260
261static inline Return<void> Void() {
262 return Return<void>();
263}
264
Yifan Honga7b2bb72017-03-29 03:36:07 -0700265namespace details {
266// Create a Return<U> from the Status of Return<T>. The provided
267// Return<T> must have an error status and have it checked.
268template <typename T, typename U>
269Return<U> StatusOf(const Return<T> &other) {
270 if (other.mStatus.isOk() || !other.mCheckedStatus) {
271 details::logAlwaysFatal("cannot call statusOf on an OK Status or an unchecked status");
272 }
273 return Return<U>{other.mStatus};
274}
275} // namespace details
276
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200277} // namespace hardware
278} // namespace android
279
280#endif // ANDROID_HARDWARE_BINDER_STATUS_H