blob: 449aff3b43f5fe00d0c4f5620aa5abbb0f21f93b [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>
21
Yifan Hong2be94182017-03-03 19:07:38 -080022#include <unordered_map>
Steven Morelande780c452017-01-17 17:22:54 -080023
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020024namespace android {
25namespace hardware {
26
Yifan Hong2be94182017-03-03 19:07:38 -080027static std::string statusToString(status_t s) {
28 const std::unordered_map<status_t, std::string> statusStrings{{
29 #define STATUS_TO_STRING_PAIR(STATUS) {STATUS, #STATUS}
30 STATUS_TO_STRING_PAIR(OK),
31 STATUS_TO_STRING_PAIR(UNKNOWN_ERROR),
32 STATUS_TO_STRING_PAIR(NO_MEMORY),
33 STATUS_TO_STRING_PAIR(INVALID_OPERATION),
34 STATUS_TO_STRING_PAIR(BAD_VALUE),
35 STATUS_TO_STRING_PAIR(BAD_TYPE),
36 STATUS_TO_STRING_PAIR(NAME_NOT_FOUND),
37 STATUS_TO_STRING_PAIR(PERMISSION_DENIED),
38 STATUS_TO_STRING_PAIR(NO_INIT),
39 STATUS_TO_STRING_PAIR(ALREADY_EXISTS),
40 STATUS_TO_STRING_PAIR(DEAD_OBJECT),
41 STATUS_TO_STRING_PAIR(FAILED_TRANSACTION),
42 STATUS_TO_STRING_PAIR(BAD_INDEX),
43 STATUS_TO_STRING_PAIR(NOT_ENOUGH_DATA),
44 STATUS_TO_STRING_PAIR(WOULD_BLOCK),
45 STATUS_TO_STRING_PAIR(TIMED_OUT),
46 STATUS_TO_STRING_PAIR(UNKNOWN_TRANSACTION),
47 STATUS_TO_STRING_PAIR(FDS_NOT_ALLOWED),
48 STATUS_TO_STRING_PAIR(UNEXPECTED_NULL)
49 }};
50 auto it = statusStrings.find(s);
51 if (it != statusStrings.end()) {
52 return it->second;
53 }
54 std::string str = std::to_string(s);
55 char *err = strerror(-s);
56 if (err != NULL) {
57 str.append(1, ' ').append(err);
58 }
59 return str;
60}
61
62static std::string exceptionToString(int32_t ex) {
63 const std::unordered_map<int32_t, std::string> exceptionStrings{{
64 #define EXCEPTION_TO_STRING_PAIR(EXCEPTION) {Status::Exception::EXCEPTION, #EXCEPTION}
65 EXCEPTION_TO_STRING_PAIR(EX_NONE),
66 EXCEPTION_TO_STRING_PAIR(EX_SECURITY),
67 EXCEPTION_TO_STRING_PAIR(EX_BAD_PARCELABLE),
68 EXCEPTION_TO_STRING_PAIR(EX_ILLEGAL_ARGUMENT),
69 EXCEPTION_TO_STRING_PAIR(EX_NULL_POINTER),
70 EXCEPTION_TO_STRING_PAIR(EX_ILLEGAL_STATE),
71 EXCEPTION_TO_STRING_PAIR(EX_NETWORK_MAIN_THREAD),
72 EXCEPTION_TO_STRING_PAIR(EX_UNSUPPORTED_OPERATION),
73 EXCEPTION_TO_STRING_PAIR(EX_SERVICE_SPECIFIC),
74 EXCEPTION_TO_STRING_PAIR(EX_HAS_REPLY_HEADER),
75 EXCEPTION_TO_STRING_PAIR(EX_TRANSACTION_FAILED)
76 }};
77 auto it = exceptionStrings.find(ex);
78 return it == exceptionStrings.end() ? std::to_string(ex) : it->second;
79}
80
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020081Status Status::ok() {
82 return Status();
83}
84
85Status Status::fromExceptionCode(int32_t exceptionCode) {
86 return Status(exceptionCode, OK);
87}
88
89Status Status::fromExceptionCode(int32_t exceptionCode,
Yifan Hong43298f92016-12-20 16:42:39 -080090 const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020091 return Status(exceptionCode, OK, message);
92}
93
94Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) {
95 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode);
96}
97
98Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
Yifan Hong43298f92016-12-20 16:42:39 -080099 const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200100 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message);
101}
102
103Status Status::fromStatusT(status_t status) {
104 Status ret;
105 ret.setFromStatusT(status);
106 return ret;
107}
108
109Status::Status(int32_t exceptionCode, int32_t errorCode)
110 : mException(exceptionCode),
111 mErrorCode(errorCode) {}
112
Yifan Hong43298f92016-12-20 16:42:39 -0800113Status::Status(int32_t exceptionCode, int32_t errorCode, const char *message)
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200114 : mException(exceptionCode),
115 mErrorCode(errorCode),
116 mMessage(message) {}
117
Yifan Hong43298f92016-12-20 16:42:39 -0800118void Status::setException(int32_t ex, const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200119 mException = ex;
120 mErrorCode = NO_ERROR; // an exception, not a transaction failure.
Yifan Hong43298f92016-12-20 16:42:39 -0800121 mMessage = message;
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200122}
123
Yifan Hong43298f92016-12-20 16:42:39 -0800124void Status::setServiceSpecificError(int32_t errorCode, const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200125 setException(EX_SERVICE_SPECIFIC, message);
126 mErrorCode = errorCode;
127}
128
129void Status::setFromStatusT(status_t status) {
130 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
131 mErrorCode = status;
132 mMessage.clear();
133}
134
Yifan Hong43298f92016-12-20 16:42:39 -0800135std::string Status::description() const {
136 std::ostringstream oss;
137 oss << (*this);
138 return oss.str();
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200139}
140
Yifan Hong43298f92016-12-20 16:42:39 -0800141std::ostream& operator<< (std::ostream& stream, const Status& s) {
142 if (s.exceptionCode() == Status::EX_NONE) {
143 stream << "No error";
144 } else {
Yifan Hong2be94182017-03-03 19:07:38 -0800145 stream << "Status(" << exceptionToString(s.exceptionCode()) << "): '";
Yifan Hong43298f92016-12-20 16:42:39 -0800146 if (s.exceptionCode() == Status::EX_SERVICE_SPECIFIC) {
147 stream << s.serviceSpecificErrorCode() << ": ";
148 } else if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
Yifan Hong2be94182017-03-03 19:07:38 -0800149 stream << statusToString(s.transactionError()) << ": ";
Yifan Hong43298f92016-12-20 16:42:39 -0800150 }
151 stream << s.exceptionMessage() << "'";
152 }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200153 return stream;
154}
155
Steven Morelande780c452017-01-17 17:22:54 -0800156namespace details {
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800157 void return_status::assertOk() const {
Steven Morelande780c452017-01-17 17:22:54 -0800158 if (!isOk()) {
159 LOG(FATAL) << "Attempted to retrieve value from failed HIDL call: " << description();
160 }
161 }
162
163 return_status::~return_status() {
164 // mCheckedStatus must be checked before isOk since isOk modifies mCheckedStatus
165 if (!mCheckedStatus && !isOk()) {
166 LOG(FATAL) << "Failed HIDL return status not checked: " << description();
167 }
168 }
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800169
170 return_status &return_status::operator=(return_status &&other) {
171 if (!mCheckedStatus && !isOk()) {
172 LOG(FATAL) << "Failed HIDL return status not checked: " << description();
173 }
174 std::swap(mStatus, other.mStatus);
175 std::swap(mCheckedStatus, other.mCheckedStatus);
176 return *this;
177 }
178
Steven Morelande780c452017-01-17 17:22:54 -0800179} // namespace details
180
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200181} // namespace hardware
182} // namespace android