Move Status to libhidl.
Bug: 31226252
Change-Id: Ica4772243ca40f43d3077c68cb974066a133303d
diff --git a/Android.mk b/Android.mk
index 467090f..c35d2cd 100644
--- a/Android.mk
+++ b/Android.mk
@@ -25,7 +25,8 @@
LOCAL_SRC_FILES := \
HidlSupport.cpp \
IServiceManager.cpp \
- Static.cpp
+ Static.cpp \
+ Status.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MULTILIB := both
diff --git a/IServiceManager.cpp b/IServiceManager.cpp
index 477d1dd..fd0e485 100644
--- a/IServiceManager.cpp
+++ b/IServiceManager.cpp
@@ -18,6 +18,7 @@
#include <hidl/IServiceManager.h>
#include <hidl/Static.h>
+#include <hidl/Status.h>
#include <utils/Log.h>
#include <hwbinder/IPCThreadState.h>
@@ -91,7 +92,13 @@
version.writeToParcel(data);
data.writeInt32(allowIsolated ? 1 : 0);
status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
- return err == NO_ERROR ? reply.readExceptionCode() : err;
+ if (err == NO_ERROR) {
+ Status status;
+ status.readFromParcel(reply);
+ return status.exceptionCode();
+ } else {
+ return err;
+ }
}
virtual Vector<String16> listServices()
diff --git a/Status.cpp b/Status.cpp
new file mode 100644
index 0000000..0a9f513
--- /dev/null
+++ b/Status.cpp
@@ -0,0 +1,167 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <hidl/Status.h>
+
+namespace android {
+namespace hardware {
+
+Status Status::ok() {
+ return Status();
+}
+
+Status Status::fromExceptionCode(int32_t exceptionCode) {
+ return Status(exceptionCode, OK);
+}
+
+Status Status::fromExceptionCode(int32_t exceptionCode,
+ const String8& message) {
+ return Status(exceptionCode, OK, message);
+}
+
+Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) {
+ return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode);
+}
+
+Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
+ const String8& message) {
+ return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message);
+}
+
+Status Status::fromStatusT(status_t status) {
+ Status ret;
+ ret.setFromStatusT(status);
+ return ret;
+}
+
+Status::Status(int32_t exceptionCode, int32_t errorCode)
+ : mException(exceptionCode),
+ mErrorCode(errorCode) {}
+
+Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message)
+ : mException(exceptionCode),
+ mErrorCode(errorCode),
+ mMessage(message) {}
+
+status_t Status::readFromParcel(const Parcel& parcel) {
+ status_t status = parcel.readInt32(&mException);
+ if (status != OK) {
+ setFromStatusT(status);
+ return status;
+ }
+
+ // Skip over fat response headers. Not used (or propagated) in native code.
+ if (mException == EX_HAS_REPLY_HEADER) {
+ // Note that the header size includes the 4 byte size field.
+ const int32_t header_start = parcel.dataPosition();
+ int32_t header_size;
+ status = parcel.readInt32(&header_size);
+ if (status != OK) {
+ setFromStatusT(status);
+ return status;
+ }
+ parcel.setDataPosition(header_start + header_size);
+ // And fat response headers are currently only used when there are no
+ // exceptions, so act like there was no error.
+ mException = EX_NONE;
+ }
+
+ if (mException == EX_NONE) {
+ return status;
+ }
+
+ // The remote threw an exception. Get the message back.
+ String16 message;
+ status = parcel.readString16(&message);
+ if (status != OK) {
+ setFromStatusT(status);
+ return status;
+ }
+ mMessage = String8(message);
+
+ if (mException == EX_SERVICE_SPECIFIC) {
+ status = parcel.readInt32(&mErrorCode);
+ }
+ if (status != OK) {
+ setFromStatusT(status);
+ return status;
+ }
+
+ return status;
+}
+
+status_t Status::writeToParcel(Parcel* parcel) const {
+ // Something really bad has happened, and we're not going to even
+ // try returning rich error data.
+ if (mException == EX_TRANSACTION_FAILED) {
+ return mErrorCode;
+ }
+
+ status_t status = parcel->writeInt32(mException);
+ if (status != OK) { return status; }
+ if (mException == EX_NONE) {
+ // We have no more information to write.
+ return status;
+ }
+ status = parcel->writeString16(String16(mMessage));
+ if (mException != EX_SERVICE_SPECIFIC) {
+ // We have no more information to write.
+ return status;
+ }
+ status = parcel->writeInt32(mErrorCode);
+ return status;
+}
+
+void Status::setException(int32_t ex, const String8& message) {
+ mException = ex;
+ mErrorCode = NO_ERROR; // an exception, not a transaction failure.
+ mMessage.setTo(message);
+}
+
+void Status::setServiceSpecificError(int32_t errorCode, const String8& message) {
+ setException(EX_SERVICE_SPECIFIC, message);
+ mErrorCode = errorCode;
+}
+
+void Status::setFromStatusT(status_t status) {
+ mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
+ mErrorCode = status;
+ mMessage.clear();
+}
+
+String8 Status::toString8() const {
+ String8 ret;
+ if (mException == EX_NONE) {
+ ret.append("No error");
+ } else {
+ ret.appendFormat("Status(%d): '", mException);
+ if (mException == EX_SERVICE_SPECIFIC ||
+ mException == EX_TRANSACTION_FAILED) {
+ ret.appendFormat("%d: ", mErrorCode);
+ }
+ ret.append(String8(mMessage));
+ ret.append("'");
+ }
+ return ret;
+}
+
+std::stringstream& operator<< (std::stringstream& stream, const Status& s) {
+ stream << s.toString8().string();
+ return stream;
+}
+
+} // namespace hardware
+} // namespace android
diff --git a/include/hidl/Status.h b/include/hidl/Status.h
new file mode 100644
index 0000000..aa8f1b2
--- /dev/null
+++ b/include/hidl/Status.h
@@ -0,0 +1,167 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_HARDWARE_BINDER_STATUS_H
+#define ANDROID_HARDWARE_BINDER_STATUS_H
+
+#include <cstdint>
+#include <sstream>
+
+#include <hwbinder/Parcel.h>
+#include <utils/String8.h>
+#include <android-base/macros.h>
+
+namespace android {
+namespace hardware {
+
+// An object similar in function to a status_t except that it understands
+// how exceptions are encoded in the prefix of a Parcel. Used like:
+//
+// Parcel data;
+// Parcel reply;
+// status_t status;
+// binder::Status remote_exception;
+// if ((status = data.writeInterfaceToken(interface_descriptor)) != OK ||
+// (status = data.writeInt32(function_input)) != OK) {
+// // We failed to write into the memory of our local parcel?
+// }
+// if ((status = remote()->transact(transaction, data, &reply)) != OK) {
+// // Something has gone wrong in the binder driver or libbinder.
+// }
+// if ((status = remote_exception.readFromParcel(reply)) != OK) {
+// // The remote didn't correctly write the exception header to the
+// // reply.
+// }
+// if (!remote_exception.isOk()) {
+// // The transaction went through correctly, but the remote reported an
+// // exception during handling.
+// }
+//
+class Status final {
+public:
+ // Keep the exception codes in sync with android/os/Parcel.java.
+ enum Exception {
+ EX_NONE = 0,
+ EX_SECURITY = -1,
+ EX_BAD_PARCELABLE = -2,
+ EX_ILLEGAL_ARGUMENT = -3,
+ EX_NULL_POINTER = -4,
+ EX_ILLEGAL_STATE = -5,
+ EX_NETWORK_MAIN_THREAD = -6,
+ EX_UNSUPPORTED_OPERATION = -7,
+ EX_SERVICE_SPECIFIC = -8,
+
+ // This is special and Java specific; see Parcel.java.
+ EX_HAS_REPLY_HEADER = -128,
+ // This is special, and indicates to C++ binder proxies that the
+ // transaction has failed at a low level.
+ EX_TRANSACTION_FAILED = -129,
+ };
+
+ // A more readable alias for the default constructor.
+ static Status ok();
+ // Authors should explicitly pick whether their integer is:
+ // - an exception code (EX_* above)
+ // - service specific error code
+ // - status_t
+ //
+ // Prefer a generic exception code when possible, then a service specific
+ // code, and finally a status_t for low level failures or legacy support.
+ // Exception codes and service specific errors map to nicer exceptions for
+ // Java clients.
+ static Status fromExceptionCode(int32_t exceptionCode);
+ static Status fromExceptionCode(int32_t exceptionCode,
+ const String8& message);
+ static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode);
+ static Status fromServiceSpecificError(int32_t serviceSpecificErrorCode,
+ const String8& message);
+ static Status fromStatusT(status_t status);
+
+ Status() = default;
+ ~Status() = default;
+
+ // Status objects are copyable and contain just simple data.
+ Status(const Status& status) = default;
+ Status(Status&& status) = default;
+ Status& operator=(const Status& status) = default;
+
+ // Bear in mind that if the client or service is a Java endpoint, this
+ // is not the logic which will provide/interpret the data here.
+ status_t readFromParcel(const Parcel& parcel);
+ status_t writeToParcel(Parcel* parcel) const;
+
+ // Set one of the pre-defined exception types defined above.
+ void setException(int32_t ex, const String8& message);
+ // Set a service specific exception with error code.
+ void setServiceSpecificError(int32_t errorCode, const String8& message);
+ // Setting a |status| != OK causes generated code to return |status|
+ // from Binder transactions, rather than writing an exception into the
+ // reply Parcel. This is the least preferable way of reporting errors.
+ void setFromStatusT(status_t status);
+
+ // Get information about an exception.
+ int32_t exceptionCode() const { return mException; }
+ const String8& exceptionMessage() const { return mMessage; }
+ status_t transactionError() const {
+ return mException == EX_TRANSACTION_FAILED ? mErrorCode : OK;
+ }
+ int32_t serviceSpecificErrorCode() const {
+ return mException == EX_SERVICE_SPECIFIC ? mErrorCode : 0;
+ }
+
+ bool isOk() const { return mException == EX_NONE; }
+
+ // For logging.
+ String8 toString8() const;
+
+private:
+ Status(int32_t exceptionCode, int32_t errorCode);
+ Status(int32_t exceptionCode, int32_t errorCode, const String8& message);
+
+ // If |mException| == EX_TRANSACTION_FAILED, generated code will return
+ // |mErrorCode| as the result of the transaction rather than write an
+ // exception to the reply parcel.
+ //
+ // Otherwise, we always write |mException| to the parcel.
+ // If |mException| != EX_NONE, we write |mMessage| as well.
+ // If |mException| == EX_SERVICE_SPECIFIC we write |mErrorCode| as well.
+ int32_t mException = EX_NONE;
+ int32_t mErrorCode = 0;
+ String8 mMessage;
+}; // class Status
+
+// For gtest output logging
+std::stringstream& operator<< (std::stringstream& stream, const Status& s);
+
+template<typename T> class Return {
+public:
+ T val;
+ Status status;
+public:
+ Return(T v) : val(v), status(Status::ok()) { }
+ Return(Status s) : status(s) {
+ // TODO(malchev): Spew a LOG error here
+ }
+ ~Return() {
+ // TODO(malchev): Assert that status isOk() if it hasn't been checked
+ }
+ operator T() { return val; }
+};
+
+} // namespace hardware
+} // namespace android
+
+#endif // ANDROID_HARDWARE_BINDER_STATUS_H