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> 21 #include <sstream> // historical 22 #include <ostream> 23 24 #include <binder/Parcel.h> 25 #include <utils/String8.h> 26 #include <string> 27 28 namespace android { 29 namespace binder { 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, 66 EX_SERVICE_SPECIFIC = -8, 67 EX_PARCELABLE = -9, 68 69 // This is special and Java specific; see Parcel.java. 70 EX_HAS_REPLY_HEADER = -128, 71 // This is special, and indicates to C++ binder proxies that the 72 // transaction has failed at a low level. 73 EX_TRANSACTION_FAILED = -129, 74 }; 75 76 // A more readable alias for the default constructor. 77 static Status ok(); 78 79 // Authors should explicitly pick whether their integer is: 80 // - an exception code (EX_* above) 81 // - service specific error code 82 // - status_t 83 // 84 // Prefer a generic exception code when possible, then a service specific 85 // code, and finally a status_t for low level failures or legacy support. 86 // Exception codes and service specific errors map to nicer exceptions for 87 // Java clients. 88 static Status fromExceptionCode(int32_t exceptionCode); 89 static Status fromExceptionCode(int32_t exceptionCode, 90 const String8& message); 91 static Status fromExceptionCode(int32_t exceptionCode, 92 const char* message); 93 94 // warning: this is still considered an error if it is constructed with a 95 // zero value error code. Please use Status::ok() instead and avoid zero 96 // error codes 97 static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode); 98 static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode, 99 const String8& message); 100 static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode, 101 const char* message); 102 103 static Status fromStatusT(status_t status); 104 105 static std::string exceptionToString(status_t exceptionCode); 106 107 Status() = default; 108 ~Status() = default; 109 110 // Status objects are copyable and contain just simple data. 111 Status(const Status& status) = default; 112 Status(Status&& status) = default; 113 Status& operator=(const Status& status) = default; 114 115 // Bear in mind that if the client or service is a Java endpoint, this 116 // is not the logic which will provide/interpret the data here. 117 status_t readFromParcel(const Parcel& parcel); 118 status_t writeToParcel(Parcel* parcel) const; 119 120 // Set one of the pre-defined exception types defined above. 121 void setException(int32_t ex, const String8& message); 122 // Set a service specific exception with error code. 123 void setServiceSpecificError(int32_t errorCode, const String8& message); 124 // Setting a |status| != OK causes generated code to return |status| 125 // from Binder transactions, rather than writing an exception into the 126 // reply Parcel. This is the least preferable way of reporting errors. 127 void setFromStatusT(status_t status); 128 129 // Get information about an exception. exceptionCode()130 int32_t exceptionCode() const { return mException; } exceptionMessage()131 const String8& exceptionMessage() const { return mMessage; } transactionError()132 status_t transactionError() const { 133 return mException == EX_TRANSACTION_FAILED ? mErrorCode : OK; 134 } serviceSpecificErrorCode()135 int32_t serviceSpecificErrorCode() const { 136 return mException == EX_SERVICE_SPECIFIC ? mErrorCode : 0; 137 } 138 isOk()139 bool isOk() const { return mException == EX_NONE; } 140 141 // For logging. 142 String8 toString8() const; 143 144 private: 145 Status(int32_t exceptionCode, int32_t errorCode); 146 Status(int32_t exceptionCode, int32_t errorCode, const String8& message); 147 148 // If |mException| == EX_TRANSACTION_FAILED, generated code will return 149 // |mErrorCode| as the result of the transaction rather than write an 150 // exception to the reply parcel. 151 // 152 // Otherwise, we always write |mException| to the parcel. 153 // If |mException| != EX_NONE, we write |mMessage| as well. 154 // If |mException| == EX_SERVICE_SPECIFIC we write |mErrorCode| as well. 155 int32_t mException = EX_NONE; 156 int32_t mErrorCode = 0; 157 String8 mMessage; 158 }; // class Status 159 160 static inline std::ostream& operator<< (std::ostream& o, const Status& s) { 161 return o << s.toString8(); 162 } 163 164 } // namespace binder 165 } // namespace android 166 167 #endif // ANDROID_BINDER_STATUS_H 168