blob: 90474a01fb58dd0d0aad81275863f121750dac42 [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 Moreland23a901c2019-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 Moreland23a901c2019-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 {
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800152 void return_status::assertOk() 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
158 return_status::~return_status() {
159 // mCheckedStatus must be checked before isOk since isOk modifies mCheckedStatus
Steven Moreland23a901c2019-03-05 19:37:51 -0800160 if (mCheckedStatus) return;
161
162 if (!isOk()) {
Steven Morelande780c452017-01-17 17:22:54 -0800163 LOG(FATAL) << "Failed HIDL return status not checked: " << description();
164 }
Steven Moreland23a901c2019-03-05 19:37:51 -0800165
166 if (gReturnRestriction == HidlReturnRestriction::NONE) {
167 return;
168 }
169
170 if (gReturnRestriction == HidlReturnRestriction::ERROR_IF_UNCHECKED) {
171 LOG(ERROR) << "Failed to check status of HIDL Return.";
172 CallStack::logStack("unchecked HIDL return", CallStack::getCurrent(10).get(), ANDROID_LOG_ERROR);
173 } else {
174 LOG(FATAL) << "Failed to check status of HIDL Return.";
175 }
Steven Morelande780c452017-01-17 17:22:54 -0800176 }
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800177
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -0700178 return_status& return_status::operator=(return_status&& other) noexcept {
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800179 if (!mCheckedStatus && !isOk()) {
180 LOG(FATAL) << "Failed HIDL return status not checked: " << description();
181 }
182 std::swap(mStatus, other.mStatus);
183 std::swap(mCheckedStatus, other.mCheckedStatus);
184 return *this;
185 }
186
Steven Morelande780c452017-01-17 17:22:54 -0800187} // namespace details
188
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +0200189} // namespace hardware
190} // namespace android