blob: 04738f8dd1fad1205d5b409faf35d3616657df22 [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,
63 EX_TRANSACTION_FAILED = -8,
64
65 // This is special and Java specific; see Parcel.java.
66 EX_HAS_REPLY_HEADER = -128,
67 };
68
69 // Allow authors to explicitly pick whether their integer is a status_t or
70 // exception code.
71 static Status fromExceptionCode(int32_t exception_code);
72 static Status fromStatusT(status_t status);
73 // A more readable alias for the default constructor.
74 static Status ok();
75
76 Status() = default;
77 Status(int32_t exception_code, const String8& message);
78 Status(int32_t exception_code, const char* message);
79
80
81 // Status objects are copyable and contain just simple data.
82 Status(const Status& status) = default;
83 Status(Status&& status) = default;
84 Status& operator=(const Status& status) = default;
85
86 ~Status() = default;
87
88 // 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);
95 // A few of the status_t values map to exception codes, but most of them
96 // simply map to "transaction failed."
97 void setFromStatusT(status_t status);
98
99 // Get information about an exception.
100 // Any argument may be given as nullptr.
101 void getException(int32_t* returned_exception,
102 String8* returned_message) const;
103 int32_t exceptionCode() const { return mException; }
104 const String8& exceptionMessage() const { return mMessage; }
105
106 bool isOk() const { return mException == EX_NONE; }
107
108 // For logging.
109 String8 toString8() const;
110
111private:
112 // We always write |mException| to the parcel.
113 // If |mException| != EX_NONE, we write message as well.
114 int32_t mException = EX_NONE;
115 String8 mMessage;
116}; // class Status
117
118} // namespace binder
119} // namespace android
120
121#endif // ANDROID_BINDER_STATUS_H