Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [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_BINDER_STATUS_H |
| 18 | #define ANDROID_BINDER_STATUS_H |
| 19 | |
| 20 | #include <cstdint> |
Ralph Nathan | 00cb980 | 2016-04-13 12:42:06 -0700 | [diff] [blame] | 21 | #include <sstream> |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 22 | |
| 23 | #include <binder/Parcel.h> |
| 24 | #include <utils/String8.h> |
| 25 | |
| 26 | namespace android { |
| 27 | namespace binder { |
| 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 | // |
| 52 | class Status final { |
| 53 | public: |
| 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, |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 64 | EX_SERVICE_SPECIFIC = -8, |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 65 | |
| 66 | // This is special and Java specific; see Parcel.java. |
| 67 | EX_HAS_REPLY_HEADER = -128, |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 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, |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 73 | // A more readable alias for the default constructor. |
| 74 | static Status ok(); |
Christopher Wiley | 1651ced | 2016-05-06 09:19:44 -0700 | [diff] [blame] | 75 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 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. |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 85 | static Status fromExceptionCode(int32_t exceptionCode); |
| 86 | static Status fromExceptionCode(int32_t exceptionCode, |
| 87 | const String8& message); |
Christopher Wiley | 1651ced | 2016-05-06 09:19:44 -0700 | [diff] [blame] | 88 | static Status fromExceptionCode(int32_t exceptionCode, |
| 89 | const char* message); |
| 90 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 91 | static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode); |
| 92 | static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode, |
| 93 | const String8& message); |
Christopher Wiley | 1651ced | 2016-05-06 09:19:44 -0700 | [diff] [blame] | 94 | static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode, |
| 95 | const char* message); |
| 96 | |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 97 | static Status fromStatusT(status_t status); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 98 | |
| 99 | Status() = default; |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 100 | ~Status() = default; |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 101 | |
| 102 | // Status objects are copyable and contain just simple data. |
| 103 | Status(const Status& status) = default; |
| 104 | Status(Status&& status) = default; |
| 105 | Status& operator=(const Status& status) = default; |
| 106 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 107 | // Bear in mind that if the client or service is a Java endpoint, this |
| 108 | // is not the logic which will provide/interpret the data here. |
| 109 | status_t readFromParcel(const Parcel& parcel); |
| 110 | status_t writeToParcel(Parcel* parcel) const; |
| 111 | |
| 112 | // Set one of the pre-defined exception types defined above. |
| 113 | void setException(int32_t ex, const String8& message); |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 114 | // Set a service specific exception with error code. |
| 115 | void setServiceSpecificError(int32_t errorCode, const String8& message); |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 116 | // Setting a |status| != OK causes generated code to return |status| |
| 117 | // from Binder transactions, rather than writing an exception into the |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 118 | // reply Parcel. This is the least preferable way of reporting errors. |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 119 | void setFromStatusT(status_t status); |
| 120 | |
| 121 | // Get information about an exception. |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 122 | int32_t exceptionCode() const { return mException; } |
| 123 | const String8& exceptionMessage() const { return mMessage; } |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 124 | status_t transactionError() const { |
| 125 | return mException == EX_TRANSACTION_FAILED ? mErrorCode : OK; |
| 126 | } |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 127 | int32_t serviceSpecificErrorCode() const { |
| 128 | return mException == EX_SERVICE_SPECIFIC ? mErrorCode : 0; |
| 129 | } |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 130 | |
| 131 | bool isOk() const { return mException == EX_NONE; } |
| 132 | |
| 133 | // For logging. |
| 134 | String8 toString8() const; |
| 135 | |
| 136 | private: |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 137 | Status(int32_t exceptionCode, int32_t errorCode); |
| 138 | Status(int32_t exceptionCode, int32_t errorCode, const String8& message); |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 139 | |
| 140 | // If |mException| == EX_TRANSACTION_FAILED, generated code will return |
| 141 | // |mErrorCode| as the result of the transaction rather than write an |
| 142 | // exception to the reply parcel. |
| 143 | // |
| 144 | // Otherwise, we always write |mException| to the parcel. |
| 145 | // If |mException| != EX_NONE, we write |mMessage| as well. |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 146 | // If |mException| == EX_SERVICE_SPECIFIC we write |mErrorCode| as well. |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 147 | int32_t mException = EX_NONE; |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 148 | int32_t mErrorCode = 0; |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 149 | String8 mMessage; |
| 150 | }; // class Status |
| 151 | |
Eino-Ville Talvala | bbab196 | 2016-02-03 13:19:49 -0800 | [diff] [blame] | 152 | // For gtest output logging |
Ralph Nathan | 00cb980 | 2016-04-13 12:42:06 -0700 | [diff] [blame] | 153 | std::stringstream& operator<< (std::stringstream& stream, const Status& s); |
Eino-Ville Talvala | bbab196 | 2016-02-03 13:19:49 -0800 | [diff] [blame] | 154 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 155 | } // namespace binder |
| 156 | } // namespace android |
| 157 | |
| 158 | #endif // ANDROID_BINDER_STATUS_H |