blob: 8367fb8acfa63df5ab1b34c2f81f0b9ecf84c5b3 [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 */
Steven Morelande780c452017-01-17 17:22:54 -080016#define LOG_TAG "HidlStatus"
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020017
18#include <hidl/Status.h>
19
Steven Morelande780c452017-01-17 17:22:54 -080020#include <android-base/logging.h>
21
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020022namespace android {
23namespace hardware {
24
25Status Status::ok() {
26 return Status();
27}
28
29Status Status::fromExceptionCode(int32_t exceptionCode) {
30 return Status(exceptionCode, OK);
31}
32
33Status Status::fromExceptionCode(int32_t exceptionCode,
Yifan Hong43298f92016-12-20 16:42:39 -080034 const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020035 return Status(exceptionCode, OK, message);
36}
37
38Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) {
39 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode);
40}
41
42Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
Yifan Hong43298f92016-12-20 16:42:39 -080043 const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020044 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message);
45}
46
47Status Status::fromStatusT(status_t status) {
48 Status ret;
49 ret.setFromStatusT(status);
50 return ret;
51}
52
53Status::Status(int32_t exceptionCode, int32_t errorCode)
54 : mException(exceptionCode),
55 mErrorCode(errorCode) {}
56
Yifan Hong43298f92016-12-20 16:42:39 -080057Status::Status(int32_t exceptionCode, int32_t errorCode, const char *message)
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020058 : mException(exceptionCode),
59 mErrorCode(errorCode),
60 mMessage(message) {}
61
Yifan Hong43298f92016-12-20 16:42:39 -080062void Status::setException(int32_t ex, const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020063 mException = ex;
64 mErrorCode = NO_ERROR; // an exception, not a transaction failure.
Yifan Hong43298f92016-12-20 16:42:39 -080065 mMessage = message;
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020066}
67
Yifan Hong43298f92016-12-20 16:42:39 -080068void Status::setServiceSpecificError(int32_t errorCode, const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020069 setException(EX_SERVICE_SPECIFIC, message);
70 mErrorCode = errorCode;
71}
72
73void Status::setFromStatusT(status_t status) {
74 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
75 mErrorCode = status;
76 mMessage.clear();
77}
78
Yifan Hong43298f92016-12-20 16:42:39 -080079std::string Status::description() const {
80 std::ostringstream oss;
81 oss << (*this);
82 return oss.str();
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020083}
84
Yifan Hong43298f92016-12-20 16:42:39 -080085std::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 Coenenbb5e9bb2016-09-01 01:36:18 +020097 return stream;
98}
99
Steven Morelande780c452017-01-17 17:22:54 -0800100namespace details {
101 void return_status::checkStatus() const {
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 }
113} // namespace details
114
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200115} // namespace hardware
116} // namespace android