blob: 41c964050a901e2be90deeb3698dd88764c7e68f [file] [log] [blame]
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +02001/*
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 */
Yifan Hong2be94182017-03-03 19:07:38 -080016
Steven Morelande780c452017-01-17 17:22:54 -080017#define LOG_TAG "HidlStatus"
Yifan Hong2be94182017-03-03 19:07:38 -080018#include <android-base/logging.h>
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020019
20#include <hidl/Status.h>
Steven Morelandeda1f922019-03-05 19:37:51 -080021#include <utils/CallStack.h>
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020022
Yifan Hong2be94182017-03-03 19:07:38 -080023#include <unordered_map>
Steven Morelande780c452017-01-17 17:22:54 -080024
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020025namespace android {
26namespace hardware {
27
Yifan Hong2be94182017-03-03 19:07:38 -080028
29static std::string exceptionToString(int32_t ex) {
Steven Morelandab060ef2019-11-15 00:06:47 -080030#define EXCEPTION_CASE(EXCEPTION) \
31 case Status::Exception::EXCEPTION: \
32 return #EXCEPTION
33
34 switch (ex) {
35 EXCEPTION_CASE(EX_NONE);
36 EXCEPTION_CASE(EX_SECURITY);
37 EXCEPTION_CASE(EX_BAD_PARCELABLE);
38 EXCEPTION_CASE(EX_ILLEGAL_ARGUMENT);
39 EXCEPTION_CASE(EX_NULL_POINTER);
40 EXCEPTION_CASE(EX_ILLEGAL_STATE);
41 EXCEPTION_CASE(EX_NETWORK_MAIN_THREAD);
42 EXCEPTION_CASE(EX_UNSUPPORTED_OPERATION);
43 EXCEPTION_CASE(EX_HAS_REPLY_HEADER);
44 EXCEPTION_CASE(EX_TRANSACTION_FAILED);
45#undef EXCEPTION_CASE
46 }
47 return std::to_string(ex);
Yifan Hong2be94182017-03-03 19:07:38 -080048}
49
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020050Status Status::ok() {
51 return Status();
52}
53
54Status Status::fromExceptionCode(int32_t exceptionCode) {
Steven Morelanddd772b42018-10-12 15:15:06 -070055 if (exceptionCode == EX_TRANSACTION_FAILED) {
56 return Status(exceptionCode, FAILED_TRANSACTION);
57 }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020058 return Status(exceptionCode, OK);
59}
60
61Status Status::fromExceptionCode(int32_t exceptionCode,
Yifan Hong43298f92016-12-20 16:42:39 -080062 const char *message) {
Steven Morelanddd772b42018-10-12 15:15:06 -070063 if (exceptionCode == EX_TRANSACTION_FAILED) {
64 return Status(exceptionCode, FAILED_TRANSACTION, message);
65 }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020066 return Status(exceptionCode, OK, message);
67}
68
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020069Status Status::fromStatusT(status_t status) {
70 Status ret;
71 ret.setFromStatusT(status);
72 return ret;
73}
74
75Status::Status(int32_t exceptionCode, int32_t errorCode)
76 : mException(exceptionCode),
77 mErrorCode(errorCode) {}
78
Yifan Hong43298f92016-12-20 16:42:39 -080079Status::Status(int32_t exceptionCode, int32_t errorCode, const char *message)
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020080 : mException(exceptionCode),
81 mErrorCode(errorCode),
82 mMessage(message) {}
83
Yifan Hong43298f92016-12-20 16:42:39 -080084void Status::setException(int32_t ex, const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020085 mException = ex;
Steven Morelanddd772b42018-10-12 15:15:06 -070086 mErrorCode = ex == EX_TRANSACTION_FAILED ? FAILED_TRANSACTION : NO_ERROR;
Yifan Hong43298f92016-12-20 16:42:39 -080087 mMessage = message;
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020088}
89
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020090void Status::setFromStatusT(status_t status) {
91 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
92 mErrorCode = status;
93 mMessage.clear();
94}
95
Yifan Hong43298f92016-12-20 16:42:39 -080096std::string Status::description() const {
97 std::ostringstream oss;
98 oss << (*this);
99 return oss.str();
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200100}
101
Yifan Hong43298f92016-12-20 16:42:39 -0800102std::ostream& operator<< (std::ostream& stream, const Status& s) {
103 if (s.exceptionCode() == Status::EX_NONE) {
104 stream << "No error";
105 } else {
Yifan Hong2be94182017-03-03 19:07:38 -0800106 stream << "Status(" << exceptionToString(s.exceptionCode()) << "): '";
Steven Moreland72db40f2017-03-09 18:15:27 -0800107 if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
Yifan Hong2be94182017-03-03 19:07:38 -0800108 stream << statusToString(s.transactionError()) << ": ";
Yifan Hong43298f92016-12-20 16:42:39 -0800109 }
110 stream << s.exceptionMessage() << "'";
111 }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200112 return stream;
113}
114
Steven Morelandeda1f922019-03-05 19:37:51 -0800115static HidlReturnRestriction gReturnRestriction = HidlReturnRestriction::NONE;
116void setProcessHidlReturnRestriction(HidlReturnRestriction restriction) {
117 gReturnRestriction = restriction;
118}
119
Steven Morelande780c452017-01-17 17:22:54 -0800120namespace details {
Steven Moreland94c43742019-05-21 17:47:12 -0700121 void return_status::onValueRetrieval() const {
Steven Morelande780c452017-01-17 17:22:54 -0800122 if (!isOk()) {
123 LOG(FATAL) << "Attempted to retrieve value from failed HIDL call: " << description();
124 }
125 }
126
Steven Morelandd85fcd12019-11-05 16:39:53 -0800127 void return_status::onIgnored() const {
128 if (gReturnRestriction == HidlReturnRestriction::NONE) {
129 return;
130 }
131
132 if (gReturnRestriction == HidlReturnRestriction::ERROR_IF_UNCHECKED) {
133 LOG(ERROR) << "Failed to check status of HIDL Return.";
134 CallStack::logStack("unchecked HIDL return", CallStack::getCurrent(10).get(),
135 ANDROID_LOG_ERROR);
136 } else {
137 LOG(FATAL) << "Failed to check status of HIDL Return.";
138 }
139 }
140
Steven Moreland94c43742019-05-21 17:47:12 -0700141 void return_status::assertOk() const {
142 if (!isOk()) {
143 LOG(FATAL) << "Failed HIDL return status not checked. Usually this happens because of "
144 "a transport error (error parceling, binder driver, or from unparceling)"
145 ". If you see this in code calling into \"Bn\" classes in for a HAL "
146 "server process, then it is likely that the code there is returning "
147 "transport errors there (as opposed to errors defined within its "
148 "protocol). Error is: " << description();
149 }
150 }
151
Steven Morelande780c452017-01-17 17:22:54 -0800152 return_status::~return_status() {
153 // mCheckedStatus must be checked before isOk since isOk modifies mCheckedStatus
Steven Morelandeda1f922019-03-05 19:37:51 -0800154 if (mCheckedStatus) return;
155
Steven Moreland94c43742019-05-21 17:47:12 -0700156 assertOk();
Steven Morelandd85fcd12019-11-05 16:39:53 -0800157 onIgnored();
Steven Morelande780c452017-01-17 17:22:54 -0800158 }
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800159
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -0700160 return_status& return_status::operator=(return_status&& other) noexcept {
Steven Moreland94c43742019-05-21 17:47:12 -0700161 if (!mCheckedStatus) {
162 assertOk();
Steven Morelandd85fcd12019-11-05 16:39:53 -0800163 onIgnored();
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800164 }
Steven Moreland94c43742019-05-21 17:47:12 -0700165
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800166 std::swap(mStatus, other.mStatus);
167 std::swap(mCheckedStatus, other.mCheckedStatus);
168 return *this;
169 }
170
Steven Morelande780c452017-01-17 17:22:54 -0800171} // namespace details
172
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200173} // namespace hardware
174} // namespace android