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 | */ |
| 16 | |
| 17 | #include <hidl/Status.h> |
| 18 | |
| 19 | namespace android { |
| 20 | namespace hardware { |
| 21 | |
| 22 | Status Status::ok() { |
| 23 | return Status(); |
| 24 | } |
| 25 | |
| 26 | Status Status::fromExceptionCode(int32_t exceptionCode) { |
| 27 | return Status(exceptionCode, OK); |
| 28 | } |
| 29 | |
| 30 | Status Status::fromExceptionCode(int32_t exceptionCode, |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 31 | const char *message) { |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 32 | return Status(exceptionCode, OK, message); |
| 33 | } |
| 34 | |
| 35 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) { |
| 36 | return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode); |
| 37 | } |
| 38 | |
| 39 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode, |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 40 | const char *message) { |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 41 | return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message); |
| 42 | } |
| 43 | |
| 44 | Status Status::fromStatusT(status_t status) { |
| 45 | Status ret; |
| 46 | ret.setFromStatusT(status); |
| 47 | return ret; |
| 48 | } |
| 49 | |
| 50 | Status::Status(int32_t exceptionCode, int32_t errorCode) |
| 51 | : mException(exceptionCode), |
| 52 | mErrorCode(errorCode) {} |
| 53 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 54 | Status::Status(int32_t exceptionCode, int32_t errorCode, const char *message) |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 55 | : mException(exceptionCode), |
| 56 | mErrorCode(errorCode), |
| 57 | mMessage(message) {} |
| 58 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 59 | void Status::setException(int32_t ex, const char *message) { |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 60 | mException = ex; |
| 61 | mErrorCode = NO_ERROR; // an exception, not a transaction failure. |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 62 | mMessage = message; |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 65 | void Status::setServiceSpecificError(int32_t errorCode, const char *message) { |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 66 | setException(EX_SERVICE_SPECIFIC, message); |
| 67 | mErrorCode = errorCode; |
| 68 | } |
| 69 | |
| 70 | void Status::setFromStatusT(status_t status) { |
| 71 | mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED; |
| 72 | mErrorCode = status; |
| 73 | mMessage.clear(); |
| 74 | } |
| 75 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 76 | std::string Status::description() const { |
| 77 | std::ostringstream oss; |
| 78 | oss << (*this); |
| 79 | return oss.str(); |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 82 | std::ostream& operator<< (std::ostream& stream, const Status& s) { |
| 83 | if (s.exceptionCode() == Status::EX_NONE) { |
| 84 | stream << "No error"; |
| 85 | } else { |
| 86 | stream << "Status(" << s.exceptionCode() << "): '"; |
| 87 | if (s.exceptionCode() == Status::EX_SERVICE_SPECIFIC) { |
| 88 | stream << s.serviceSpecificErrorCode() << ": "; |
| 89 | } else if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) { |
| 90 | stream << s.transactionError() << ": "; |
| 91 | } |
| 92 | stream << s.exceptionMessage() << "'"; |
| 93 | } |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 94 | return stream; |
| 95 | } |
| 96 | |
| 97 | } // namespace hardware |
| 98 | } // namespace android |