blob: 4ce26eea2c34f97cf2b1c7d70391da9f2b5fe8f1 [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 */
16
17#include <hidl/Status.h>
18
19namespace android {
20namespace hardware {
21
22Status Status::ok() {
23 return Status();
24}
25
26Status Status::fromExceptionCode(int32_t exceptionCode) {
27 return Status(exceptionCode, OK);
28}
29
30Status Status::fromExceptionCode(int32_t exceptionCode,
Yifan Hong43298f92016-12-20 16:42:39 -080031 const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020032 return Status(exceptionCode, OK, message);
33}
34
35Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) {
36 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode);
37}
38
39Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
Yifan Hong43298f92016-12-20 16:42:39 -080040 const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020041 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message);
42}
43
44Status Status::fromStatusT(status_t status) {
45 Status ret;
46 ret.setFromStatusT(status);
47 return ret;
48}
49
50Status::Status(int32_t exceptionCode, int32_t errorCode)
51 : mException(exceptionCode),
52 mErrorCode(errorCode) {}
53
Yifan Hong43298f92016-12-20 16:42:39 -080054Status::Status(int32_t exceptionCode, int32_t errorCode, const char *message)
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020055 : mException(exceptionCode),
56 mErrorCode(errorCode),
57 mMessage(message) {}
58
Yifan Hong43298f92016-12-20 16:42:39 -080059void Status::setException(int32_t ex, const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020060 mException = ex;
61 mErrorCode = NO_ERROR; // an exception, not a transaction failure.
Yifan Hong43298f92016-12-20 16:42:39 -080062 mMessage = message;
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020063}
64
Yifan Hong43298f92016-12-20 16:42:39 -080065void Status::setServiceSpecificError(int32_t errorCode, const char *message) {
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020066 setException(EX_SERVICE_SPECIFIC, message);
67 mErrorCode = errorCode;
68}
69
70void Status::setFromStatusT(status_t status) {
71 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
72 mErrorCode = status;
73 mMessage.clear();
74}
75
Yifan Hong43298f92016-12-20 16:42:39 -080076std::string Status::description() const {
77 std::ostringstream oss;
78 oss << (*this);
79 return oss.str();
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020080}
81
Yifan Hong43298f92016-12-20 16:42:39 -080082std::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 Coenenbb5e9bb2016-09-01 01:36:18 +020094 return stream;
95}
96
97} // namespace hardware
98} // namespace android