Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 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_HARDWARE_BINDER_STATUS_H |
| 18 | #define ANDROID_HARDWARE_BINDER_STATUS_H |
| 19 | |
| 20 | #include <cstdint> |
| 21 | #include <sstream> |
| 22 | |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 23 | #include <android-base/macros.h> |
Yifan Hong | a7b2bb7 | 2017-03-29 03:36:07 -0700 | [diff] [blame^] | 24 | #include <hidl/HidlInternal.h> |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 25 | #include <utils/Errors.h> |
Martijn Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 26 | #include <utils/StrongPointer.h> |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace 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 | // |
| 54 | class Status final { |
| 55 | public: |
| 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 Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 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) |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 78 | // - status_t |
| 79 | // |
Steven Moreland | 72db40f | 2017-03-09 18:15:27 -0800 | [diff] [blame] | 80 | // 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 Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 83 | static Status fromExceptionCode(int32_t exceptionCode); |
| 84 | static Status fromExceptionCode(int32_t exceptionCode, |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 85 | const char *message); |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 86 | 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 Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 96 | // Set one of the pre-defined exception types defined above. |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 97 | void setException(int32_t ex, const char *message); |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 98 | // 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 Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 105 | const char *exceptionMessage() const { return mMessage.c_str(); } |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 106 | status_t transactionError() const { |
| 107 | return mException == EX_TRANSACTION_FAILED ? mErrorCode : OK; |
| 108 | } |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 109 | |
| 110 | bool isOk() const { return mException == EX_NONE; } |
| 111 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 112 | // For debugging purposes only |
| 113 | std::string description() const; |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 114 | |
| 115 | private: |
| 116 | Status(int32_t exceptionCode, int32_t errorCode); |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 117 | Status(int32_t exceptionCode, int32_t errorCode, const char *message); |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 118 | |
| 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 Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 125 | int32_t mException = EX_NONE; |
| 126 | int32_t mErrorCode = 0; |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 127 | std::string mMessage; |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 128 | }; // class Status |
| 129 | |
| 130 | // For gtest output logging |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 131 | std::ostream& operator<< (std::ostream& stream, const Status& s); |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 132 | |
Yifan Hong | a7b2bb7 | 2017-03-29 03:36:07 -0700 | [diff] [blame^] | 133 | template<typename T> class Return; |
| 134 | |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 135 | namespace details { |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 136 | class return_status { |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 137 | private: |
| 138 | Status mStatus {}; |
| 139 | mutable bool mCheckedStatus = false; |
Yifan Hong | a7b2bb7 | 2017-03-29 03:36:07 -0700 | [diff] [blame^] | 140 | |
| 141 | template <typename T, typename U> |
| 142 | friend Return<U> StatusOf(const Return<T> &other); |
Martijn Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 143 | protected: |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame] | 144 | void assertOk() const; |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 145 | public: |
| 146 | return_status() {} |
| 147 | return_status(Status s) : mStatus(s) {} |
| 148 | |
Yifan Hong | 603cde9 | 2017-02-17 14:50:44 -0800 | [diff] [blame] | 149 | return_status(const return_status &) = delete; |
| 150 | return_status &operator=(const return_status &) = delete; |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 151 | |
Yifan Hong | c43bd9d | 2017-02-17 14:11:48 -0800 | [diff] [blame] | 152 | return_status(return_status &&other) { |
| 153 | *this = std::move(other); |
| 154 | } |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame] | 155 | return_status &operator=(return_status &&other); |
Yifan Hong | c43bd9d | 2017-02-17 14:11:48 -0800 | [diff] [blame] | 156 | |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 157 | ~return_status(); |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 158 | |
| 159 | bool isOk() const { |
| 160 | mCheckedStatus = true; |
| 161 | return mStatus.isOk(); |
| 162 | } |
| 163 | |
Yifan Hong | a7b2bb7 | 2017-03-29 03:36:07 -0700 | [diff] [blame^] | 164 | // 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 Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 170 | // For debugging purposes only |
| 171 | std::string description() const { |
| 172 | // Doesn't consider checked. |
| 173 | return mStatus.description(); |
| 174 | } |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 175 | }; |
| 176 | } // namespace details |
| 177 | |
| 178 | template<typename T> class Return : public details::return_status { |
Iliyan Malchev | 170c189 | 2016-09-08 13:55:53 -0700 | [diff] [blame] | 179 | private: |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 180 | T mVal {}; |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 181 | public: |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 182 | Return(T v) : details::return_status(), mVal{v} {} |
| 183 | Return(Status s) : details::return_status(s) {} |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 184 | |
Yifan Hong | c43bd9d | 2017-02-17 14:11:48 -0800 | [diff] [blame] | 185 | // 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 Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 191 | ~Return() = default; |
Steven Moreland | 1cfaee7 | 2016-12-01 08:34:19 -0800 | [diff] [blame] | 192 | |
| 193 | operator T() const { |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame] | 194 | assertOk(); |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 195 | return mVal; |
| 196 | } |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 197 | |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 198 | }; |
| 199 | |
Martijn Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 200 | template<typename T> class Return<sp<T>> : public details::return_status { |
| 201 | private: |
| 202 | sp<T> mVal {}; |
| 203 | public: |
| 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 Hong | c43bd9d | 2017-02-17 14:11:48 -0800 | [diff] [blame] | 211 | // 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 Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 217 | ~Return() = default; |
| 218 | |
| 219 | operator sp<T>() const { |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame] | 220 | assertOk(); |
Martijn Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 221 | return mVal; |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 226 | template<> class Return<void> : public details::return_status { |
Iliyan Malchev | 170c189 | 2016-09-08 13:55:53 -0700 | [diff] [blame] | 227 | public: |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 228 | Return() : details::return_status() {} |
| 229 | Return(Status s) : details::return_status(s) {} |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 230 | |
Yifan Hong | c43bd9d | 2017-02-17 14:11:48 -0800 | [diff] [blame] | 231 | // 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 Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 237 | ~Return() = default; |
Iliyan Malchev | 170c189 | 2016-09-08 13:55:53 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | static inline Return<void> Void() { |
| 241 | return Return<void>(); |
| 242 | } |
| 243 | |
Yifan Hong | a7b2bb7 | 2017-03-29 03:36:07 -0700 | [diff] [blame^] | 244 | namespace 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. |
| 247 | template <typename T, typename U> |
| 248 | Return<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 Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 256 | } // namespace hardware |
| 257 | } // namespace android |
| 258 | |
| 259 | #endif // ANDROID_HARDWARE_BINDER_STATUS_H |