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 | */ |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 16 | #define LOG_TAG "HidlStatus" |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 17 | |
| 18 | #include <hidl/Status.h> |
| 19 | |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
| 21 | |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 22 | namespace android { |
| 23 | namespace hardware { |
| 24 | |
| 25 | Status Status::ok() { |
| 26 | return Status(); |
| 27 | } |
| 28 | |
| 29 | Status Status::fromExceptionCode(int32_t exceptionCode) { |
| 30 | return Status(exceptionCode, OK); |
| 31 | } |
| 32 | |
| 33 | Status Status::fromExceptionCode(int32_t exceptionCode, |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 34 | const char *message) { |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 35 | return Status(exceptionCode, OK, message); |
| 36 | } |
| 37 | |
| 38 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) { |
| 39 | return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode); |
| 40 | } |
| 41 | |
| 42 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode, |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 43 | const char *message) { |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 44 | return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message); |
| 45 | } |
| 46 | |
| 47 | Status Status::fromStatusT(status_t status) { |
| 48 | Status ret; |
| 49 | ret.setFromStatusT(status); |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | Status::Status(int32_t exceptionCode, int32_t errorCode) |
| 54 | : mException(exceptionCode), |
| 55 | mErrorCode(errorCode) {} |
| 56 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 57 | Status::Status(int32_t exceptionCode, int32_t errorCode, const char *message) |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 58 | : mException(exceptionCode), |
| 59 | mErrorCode(errorCode), |
| 60 | mMessage(message) {} |
| 61 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 62 | void Status::setException(int32_t ex, const char *message) { |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 63 | mException = ex; |
| 64 | mErrorCode = NO_ERROR; // an exception, not a transaction failure. |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 65 | mMessage = message; |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 68 | void Status::setServiceSpecificError(int32_t errorCode, const char *message) { |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 69 | setException(EX_SERVICE_SPECIFIC, message); |
| 70 | mErrorCode = errorCode; |
| 71 | } |
| 72 | |
| 73 | void Status::setFromStatusT(status_t status) { |
| 74 | mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED; |
| 75 | mErrorCode = status; |
| 76 | mMessage.clear(); |
| 77 | } |
| 78 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 79 | std::string Status::description() const { |
| 80 | std::ostringstream oss; |
| 81 | oss << (*this); |
| 82 | return oss.str(); |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 85 | std::ostream& operator<< (std::ostream& stream, const Status& s) { |
| 86 | if (s.exceptionCode() == Status::EX_NONE) { |
| 87 | stream << "No error"; |
| 88 | } else { |
| 89 | stream << "Status(" << s.exceptionCode() << "): '"; |
| 90 | if (s.exceptionCode() == Status::EX_SERVICE_SPECIFIC) { |
| 91 | stream << s.serviceSpecificErrorCode() << ": "; |
| 92 | } else if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) { |
| 93 | stream << s.transactionError() << ": "; |
| 94 | } |
| 95 | stream << s.exceptionMessage() << "'"; |
| 96 | } |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 97 | return stream; |
| 98 | } |
| 99 | |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 100 | namespace details { |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame^] | 101 | void return_status::assertOk() const { |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 102 | if (!isOk()) { |
| 103 | LOG(FATAL) << "Attempted to retrieve value from failed HIDL call: " << description(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return_status::~return_status() { |
| 108 | // mCheckedStatus must be checked before isOk since isOk modifies mCheckedStatus |
| 109 | if (!mCheckedStatus && !isOk()) { |
| 110 | LOG(FATAL) << "Failed HIDL return status not checked: " << description(); |
| 111 | } |
| 112 | } |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame^] | 113 | |
| 114 | return_status &return_status::operator=(return_status &&other) { |
| 115 | if (!mCheckedStatus && !isOk()) { |
| 116 | LOG(FATAL) << "Failed HIDL return status not checked: " << description(); |
| 117 | } |
| 118 | std::swap(mStatus, other.mStatus); |
| 119 | std::swap(mCheckedStatus, other.mCheckedStatus); |
| 120 | return *this; |
| 121 | } |
| 122 | |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 123 | } // namespace details |
| 124 | |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 125 | } // namespace hardware |
| 126 | } // namespace android |