blob: d19824d8949ce9e264a005286184f121da3d4fb4 [file] [log] [blame]
Christopher Wiley09eb7492015-11-09 15:06:15 -08001/*
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
22#include <binder/Parcel.h>
23#include <utils/String8.h>
24
25namespace android {
26namespace binder {
27
28// An object similar in function to a status_t except that it understands
29// how exceptions are encoded in the prefix of a Parcel. Used like:
30//
31// Parcel data;
32// Parcel reply;
33// status_t status;
34// binder::Status remote_exception;
35// if ((status = data.writeInterfaceToken(interface_descriptor)) != OK ||
36// (status = data.writeInt32(function_input)) != OK) {
37// // We failed to write into the memory of our local parcel?
38// }
39// if ((status = remote()->transact(transaction, data, &reply)) != OK) {
40// // Something has gone wrong in the binder driver or libbinder.
41// }
42// if ((status = remote_exception.readFromParcel(reply)) != OK) {
43// // The remote didn't correctly write the exception header to the
44// // reply.
45// }
46// if (!remote_exception.isOk()) {
47// // The transaction went through correctly, but the remote reported an
48// // exception during handling.
49// }
50//
51class Status final {
52public:
53 // Keep the exception codes in sync with android/os/Parcel.java.
54 enum Exception {
55 EX_NONE = 0,
56 EX_SECURITY = -1,
57 EX_BAD_PARCELABLE = -2,
58 EX_ILLEGAL_ARGUMENT = -3,
59 EX_NULL_POINTER = -4,
60 EX_ILLEGAL_STATE = -5,
61 EX_NETWORK_MAIN_THREAD = -6,
62 EX_UNSUPPORTED_OPERATION = -7,
Christopher Wiley09eb7492015-11-09 15:06:15 -080063
64 // This is special and Java specific; see Parcel.java.
65 EX_HAS_REPLY_HEADER = -128,
Christopher Wileycff7f172015-11-22 16:29:58 -080066 // This is special, and indicates to C++ binder proxies that the
67 // transaction has failed at a low level.
68 EX_TRANSACTION_FAILED = -129,
Christopher Wiley09eb7492015-11-09 15:06:15 -080069 };
70
Christopher Wiley09eb7492015-11-09 15:06:15 -080071 // A more readable alias for the default constructor.
72 static Status ok();
Christopher Wileycff7f172015-11-22 16:29:58 -080073 // Allow authors to explicitly pick whether their integer is a status_t or
74 // exception code.
75 static Status fromExceptionCode(int32_t exceptionCode);
76 static Status fromExceptionCode(int32_t exceptionCode,
77 const String8& message);
78 static Status fromStatusT(status_t status);
Christopher Wiley09eb7492015-11-09 15:06:15 -080079
80 Status() = default;
Christopher Wileycff7f172015-11-22 16:29:58 -080081 ~Status() = default;
Christopher Wiley09eb7492015-11-09 15:06:15 -080082
83 // Status objects are copyable and contain just simple data.
84 Status(const Status& status) = default;
85 Status(Status&& status) = default;
86 Status& operator=(const Status& status) = default;
87
Christopher Wiley09eb7492015-11-09 15:06:15 -080088 // Bear in mind that if the client or service is a Java endpoint, this
89 // is not the logic which will provide/interpret the data here.
90 status_t readFromParcel(const Parcel& parcel);
91 status_t writeToParcel(Parcel* parcel) const;
92
93 // Set one of the pre-defined exception types defined above.
94 void setException(int32_t ex, const String8& message);
Christopher Wileycff7f172015-11-22 16:29:58 -080095 // Setting a |status| != OK causes generated code to return |status|
96 // from Binder transactions, rather than writing an exception into the
97 // reply Parcel.
Christopher Wiley09eb7492015-11-09 15:06:15 -080098 void setFromStatusT(status_t status);
99
100 // Get information about an exception.
Christopher Wiley09eb7492015-11-09 15:06:15 -0800101 int32_t exceptionCode() const { return mException; }
102 const String8& exceptionMessage() const { return mMessage; }
Christopher Wileycff7f172015-11-22 16:29:58 -0800103 status_t transactionError() const {
104 return mException == EX_TRANSACTION_FAILED ? mErrorCode : OK;
105 }
Christopher Wiley09eb7492015-11-09 15:06:15 -0800106
107 bool isOk() const { return mException == EX_NONE; }
108
109 // For logging.
110 String8 toString8() const;
111
112private:
Christopher Wileycff7f172015-11-22 16:29:58 -0800113 Status(int32_t exception_code);
114 Status(int32_t exception_code, const String8& message);
115
116 // If |mException| == EX_TRANSACTION_FAILED, generated code will return
117 // |mErrorCode| as the result of the transaction rather than write an
118 // exception to the reply parcel.
119 //
120 // Otherwise, we always write |mException| to the parcel.
121 // If |mException| != EX_NONE, we write |mMessage| as well.
Christopher Wiley09eb7492015-11-09 15:06:15 -0800122 int32_t mException = EX_NONE;
Christopher Wileycff7f172015-11-22 16:29:58 -0800123 int32_t mErrorCode = 0;
Christopher Wiley09eb7492015-11-09 15:06:15 -0800124 String8 mMessage;
125}; // class Status
126
127} // namespace binder
128} // namespace android
129
130#endif // ANDROID_BINDER_STATUS_H