Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 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 | */ |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 16 | |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 17 | #define LOG_TAG "HidlStatus" |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 18 | #include <android-base/logging.h> |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 19 | |
| 20 | #include <hidl/Status.h> |
Steven Moreland | eda1f92 | 2019-03-05 19:37:51 -0800 | [diff] [blame] | 21 | #include <utils/CallStack.h> |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 22 | |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 23 | #include <unordered_map> |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 24 | |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 28 | |
| 29 | static std::string exceptionToString(int32_t ex) { |
Steven Moreland | ab060ef | 2019-11-15 00:06:47 -0800 | [diff] [blame^] | 30 | #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 Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 50 | Status Status::ok() { |
| 51 | return Status(); |
| 52 | } |
| 53 | |
| 54 | Status Status::fromExceptionCode(int32_t exceptionCode) { |
Steven Moreland | dd772b4 | 2018-10-12 15:15:06 -0700 | [diff] [blame] | 55 | if (exceptionCode == EX_TRANSACTION_FAILED) { |
| 56 | return Status(exceptionCode, FAILED_TRANSACTION); |
| 57 | } |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 58 | return Status(exceptionCode, OK); |
| 59 | } |
| 60 | |
| 61 | Status Status::fromExceptionCode(int32_t exceptionCode, |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 62 | const char *message) { |
Steven Moreland | dd772b4 | 2018-10-12 15:15:06 -0700 | [diff] [blame] | 63 | if (exceptionCode == EX_TRANSACTION_FAILED) { |
| 64 | return Status(exceptionCode, FAILED_TRANSACTION, message); |
| 65 | } |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 66 | return Status(exceptionCode, OK, message); |
| 67 | } |
| 68 | |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 69 | Status Status::fromStatusT(status_t status) { |
| 70 | Status ret; |
| 71 | ret.setFromStatusT(status); |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | Status::Status(int32_t exceptionCode, int32_t errorCode) |
| 76 | : mException(exceptionCode), |
| 77 | mErrorCode(errorCode) {} |
| 78 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 79 | Status::Status(int32_t exceptionCode, int32_t errorCode, const char *message) |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 80 | : mException(exceptionCode), |
| 81 | mErrorCode(errorCode), |
| 82 | mMessage(message) {} |
| 83 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 84 | void Status::setException(int32_t ex, const char *message) { |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 85 | mException = ex; |
Steven Moreland | dd772b4 | 2018-10-12 15:15:06 -0700 | [diff] [blame] | 86 | mErrorCode = ex == EX_TRANSACTION_FAILED ? FAILED_TRANSACTION : NO_ERROR; |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 87 | mMessage = message; |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 90 | void Status::setFromStatusT(status_t status) { |
| 91 | mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED; |
| 92 | mErrorCode = status; |
| 93 | mMessage.clear(); |
| 94 | } |
| 95 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 96 | std::string Status::description() const { |
| 97 | std::ostringstream oss; |
| 98 | oss << (*this); |
| 99 | return oss.str(); |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 102 | std::ostream& operator<< (std::ostream& stream, const Status& s) { |
| 103 | if (s.exceptionCode() == Status::EX_NONE) { |
| 104 | stream << "No error"; |
| 105 | } else { |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 106 | stream << "Status(" << exceptionToString(s.exceptionCode()) << "): '"; |
Steven Moreland | 72db40f | 2017-03-09 18:15:27 -0800 | [diff] [blame] | 107 | if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) { |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 108 | stream << statusToString(s.transactionError()) << ": "; |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 109 | } |
| 110 | stream << s.exceptionMessage() << "'"; |
| 111 | } |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 112 | return stream; |
| 113 | } |
| 114 | |
Steven Moreland | eda1f92 | 2019-03-05 19:37:51 -0800 | [diff] [blame] | 115 | static HidlReturnRestriction gReturnRestriction = HidlReturnRestriction::NONE; |
| 116 | void setProcessHidlReturnRestriction(HidlReturnRestriction restriction) { |
| 117 | gReturnRestriction = restriction; |
| 118 | } |
| 119 | |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 120 | namespace details { |
Steven Moreland | 94c4374 | 2019-05-21 17:47:12 -0700 | [diff] [blame] | 121 | void return_status::onValueRetrieval() const { |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 122 | if (!isOk()) { |
| 123 | LOG(FATAL) << "Attempted to retrieve value from failed HIDL call: " << description(); |
| 124 | } |
| 125 | } |
| 126 | |
Steven Moreland | d85fcd1 | 2019-11-05 16:39:53 -0800 | [diff] [blame] | 127 | 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 Moreland | 94c4374 | 2019-05-21 17:47:12 -0700 | [diff] [blame] | 141 | 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 Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 152 | return_status::~return_status() { |
| 153 | // mCheckedStatus must be checked before isOk since isOk modifies mCheckedStatus |
Steven Moreland | eda1f92 | 2019-03-05 19:37:51 -0800 | [diff] [blame] | 154 | if (mCheckedStatus) return; |
| 155 | |
Steven Moreland | 94c4374 | 2019-05-21 17:47:12 -0700 | [diff] [blame] | 156 | assertOk(); |
Steven Moreland | d85fcd1 | 2019-11-05 16:39:53 -0800 | [diff] [blame] | 157 | onIgnored(); |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 158 | } |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame] | 159 | |
Chih-Hung Hsieh | 3833f20 | 2018-09-25 12:03:06 -0700 | [diff] [blame] | 160 | return_status& return_status::operator=(return_status&& other) noexcept { |
Steven Moreland | 94c4374 | 2019-05-21 17:47:12 -0700 | [diff] [blame] | 161 | if (!mCheckedStatus) { |
| 162 | assertOk(); |
Steven Moreland | d85fcd1 | 2019-11-05 16:39:53 -0800 | [diff] [blame] | 163 | onIgnored(); |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame] | 164 | } |
Steven Moreland | 94c4374 | 2019-05-21 17:47:12 -0700 | [diff] [blame] | 165 | |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame] | 166 | std::swap(mStatus, other.mStatus); |
| 167 | std::swap(mCheckedStatus, other.mCheckedStatus); |
| 168 | return *this; |
| 169 | } |
| 170 | |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 171 | } // namespace details |
| 172 | |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 173 | } // namespace hardware |
| 174 | } // namespace android |