blob: 7698ff829d5f645bca8a24d2ef7042a5263d6466 [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>
Steven Morelandeda1f922019-03-05 19:37:51 -080021#include <utils/CallStack.h>
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020022
Yifan Hong2be94182017-03-03 19:07:38 -080023#include <unordered_map>
Steven Morelande780c452017-01-17 17:22:54 -080024
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020025namespace android {
26namespace hardware {
27
Yifan Hong2be94182017-03-03 19:07:38 -080028static std::string statusToString(status_t s) {
29 const std::unordered_map<status_t, std::string> statusStrings{{
30 #define STATUS_TO_STRING_PAIR(STATUS) {STATUS, #STATUS}
31 STATUS_TO_STRING_PAIR(OK),
32 STATUS_TO_STRING_PAIR(UNKNOWN_ERROR),
33 STATUS_TO_STRING_PAIR(NO_MEMORY),
34 STATUS_TO_STRING_PAIR(INVALID_OPERATION),
35 STATUS_TO_STRING_PAIR(BAD_VALUE),
36 STATUS_TO_STRING_PAIR(BAD_TYPE),
37 STATUS_TO_STRING_PAIR(NAME_NOT_FOUND),
38 STATUS_TO_STRING_PAIR(PERMISSION_DENIED),
39 STATUS_TO_STRING_PAIR(NO_INIT),
40 STATUS_TO_STRING_PAIR(ALREADY_EXISTS),
41 STATUS_TO_STRING_PAIR(DEAD_OBJECT),
42 STATUS_TO_STRING_PAIR(FAILED_TRANSACTION),
43 STATUS_TO_STRING_PAIR(BAD_INDEX),
44 STATUS_TO_STRING_PAIR(NOT_ENOUGH_DATA),
45 STATUS_TO_STRING_PAIR(WOULD_BLOCK),
46 STATUS_TO_STRING_PAIR(TIMED_OUT),
47 STATUS_TO_STRING_PAIR(UNKNOWN_TRANSACTION),
48 STATUS_TO_STRING_PAIR(FDS_NOT_ALLOWED),
49 STATUS_TO_STRING_PAIR(UNEXPECTED_NULL)
50 }};
51 auto it = statusStrings.find(s);
52 if (it != statusStrings.end()) {
53 return it->second;
54 }
55 std::string str = std::to_string(s);
56 char *err = strerror(-s);
Stephen Hinesfd9ecee2017-09-27 18:52:52 -070057 if (err != nullptr) {
Yifan Hong2be94182017-03-03 19:07:38 -080058 str.append(1, ' ').append(err);
59 }
60 return str;
61}
62
63static std::string exceptionToString(int32_t ex) {
64 const std::unordered_map<int32_t, std::string> exceptionStrings{{
65 #define EXCEPTION_TO_STRING_PAIR(EXCEPTION) {Status::Exception::EXCEPTION, #EXCEPTION}
66 EXCEPTION_TO_STRING_PAIR(EX_NONE),
67 EXCEPTION_TO_STRING_PAIR(EX_SECURITY),
68 EXCEPTION_TO_STRING_PAIR(EX_BAD_PARCELABLE),
69 EXCEPTION_TO_STRING_PAIR(EX_ILLEGAL_ARGUMENT),
70 EXCEPTION_TO_STRING_PAIR(EX_NULL_POINTER),
71 EXCEPTION_TO_STRING_PAIR(EX_ILLEGAL_STATE),
72 EXCEPTION_TO_STRING_PAIR(EX_NETWORK_MAIN_THREAD),
73 EXCEPTION_TO_STRING_PAIR(EX_UNSUPPORTED_OPERATION),
Yifan Hong2be94182017-03-03 19:07:38 -080074 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) {
Steven Morelanddd772b42018-10-12 15:15:06 -070086 if (exceptionCode == EX_TRANSACTION_FAILED) {
87 return Status(exceptionCode, FAILED_TRANSACTION);
88 }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020089 return Status(exceptionCode, OK);
90}
91
92Status Status::fromExceptionCode(int32_t exceptionCode,
Yifan Hong43298f92016-12-20 16:42:39 -080093 const char *message) {
Steven Morelanddd772b42018-10-12 15:15:06 -070094 if (exceptionCode == EX_TRANSACTION_FAILED) {
95 return Status(exceptionCode, FAILED_TRANSACTION, message);
96 }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020097 return Status(exceptionCode, OK, message);
98}
99
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200100Status Status::fromStatusT(status_t status) {
101 Status ret;
102 ret.setFromStatusT(status);
103 return ret;
104}
105
106Status::Status(int32_t exceptionCode, int32_t errorCode)
107 : mException(exceptionCode),
108 mErrorCode(errorCode) {}
109
Yifan Hong43298f92016-12-20 16:42:39 -0800110Status::Status(int32_t exceptionCode, int32_t errorCode, const char *message)
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200111 : mException(exceptionCode),
112 mErrorCode(errorCode),
113 mMessage(message) {}
114
Yifan Hong43298f92016-12-20 16:42:39 -0800115void Status::setException(int32_t ex, const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200116 mException = ex;
Steven Morelanddd772b42018-10-12 15:15:06 -0700117 mErrorCode = ex == EX_TRANSACTION_FAILED ? FAILED_TRANSACTION : NO_ERROR;
Yifan Hong43298f92016-12-20 16:42:39 -0800118 mMessage = message;
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200119}
120
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200121void Status::setFromStatusT(status_t status) {
122 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
123 mErrorCode = status;
124 mMessage.clear();
125}
126
Yifan Hong43298f92016-12-20 16:42:39 -0800127std::string Status::description() const {
128 std::ostringstream oss;
129 oss << (*this);
130 return oss.str();
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200131}
132
Yifan Hong43298f92016-12-20 16:42:39 -0800133std::ostream& operator<< (std::ostream& stream, const Status& s) {
134 if (s.exceptionCode() == Status::EX_NONE) {
135 stream << "No error";
136 } else {
Yifan Hong2be94182017-03-03 19:07:38 -0800137 stream << "Status(" << exceptionToString(s.exceptionCode()) << "): '";
Steven Moreland72db40f2017-03-09 18:15:27 -0800138 if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
Yifan Hong2be94182017-03-03 19:07:38 -0800139 stream << statusToString(s.transactionError()) << ": ";
Yifan Hong43298f92016-12-20 16:42:39 -0800140 }
141 stream << s.exceptionMessage() << "'";
142 }
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200143 return stream;
144}
145
Steven Morelandeda1f922019-03-05 19:37:51 -0800146static HidlReturnRestriction gReturnRestriction = HidlReturnRestriction::NONE;
147void setProcessHidlReturnRestriction(HidlReturnRestriction restriction) {
148 gReturnRestriction = restriction;
149}
150
Steven Morelande780c452017-01-17 17:22:54 -0800151namespace details {
Steven Moreland94c43742019-05-21 17:47:12 -0700152 void return_status::onValueRetrieval() const {
Steven Morelande780c452017-01-17 17:22:54 -0800153 if (!isOk()) {
154 LOG(FATAL) << "Attempted to retrieve value from failed HIDL call: " << description();
155 }
156 }
157
Steven Morelandd85fcd12019-11-05 16:39:53 -0800158 void return_status::onIgnored() const {
159 if (gReturnRestriction == HidlReturnRestriction::NONE) {
160 return;
161 }
162
163 if (gReturnRestriction == HidlReturnRestriction::ERROR_IF_UNCHECKED) {
164 LOG(ERROR) << "Failed to check status of HIDL Return.";
165 CallStack::logStack("unchecked HIDL return", CallStack::getCurrent(10).get(),
166 ANDROID_LOG_ERROR);
167 } else {
168 LOG(FATAL) << "Failed to check status of HIDL Return.";
169 }
170 }
171
Steven Moreland94c43742019-05-21 17:47:12 -0700172 void return_status::assertOk() const {
173 if (!isOk()) {
174 LOG(FATAL) << "Failed HIDL return status not checked. Usually this happens because of "
175 "a transport error (error parceling, binder driver, or from unparceling)"
176 ". If you see this in code calling into \"Bn\" classes in for a HAL "
177 "server process, then it is likely that the code there is returning "
178 "transport errors there (as opposed to errors defined within its "
179 "protocol). Error is: " << description();
180 }
181 }
182
Steven Morelande780c452017-01-17 17:22:54 -0800183 return_status::~return_status() {
184 // mCheckedStatus must be checked before isOk since isOk modifies mCheckedStatus
Steven Morelandeda1f922019-03-05 19:37:51 -0800185 if (mCheckedStatus) return;
186
Steven Moreland94c43742019-05-21 17:47:12 -0700187 assertOk();
Steven Morelandd85fcd12019-11-05 16:39:53 -0800188 onIgnored();
Steven Morelande780c452017-01-17 17:22:54 -0800189 }
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800190
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -0700191 return_status& return_status::operator=(return_status&& other) noexcept {
Steven Moreland94c43742019-05-21 17:47:12 -0700192 if (!mCheckedStatus) {
193 assertOk();
Steven Morelandd85fcd12019-11-05 16:39:53 -0800194 onIgnored();
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800195 }
Steven Moreland94c43742019-05-21 17:47:12 -0700196
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800197 std::swap(mStatus, other.mStatus);
198 std::swap(mCheckedStatus, other.mCheckedStatus);
199 return *this;
200 }
201
Steven Morelande780c452017-01-17 17:22:54 -0800202} // namespace details
203
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200204} // namespace hardware
205} // namespace android