blob: f0189187d74dda49fc60a45561540326be8306fa [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,
31 const String8& message) {
32 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,
40 const String8& message) {
41 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
54Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message)
55 : mException(exceptionCode),
56 mErrorCode(errorCode),
57 mMessage(message) {}
58
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020059void Status::setException(int32_t ex, const String8& message) {
60 mException = ex;
61 mErrorCode = NO_ERROR; // an exception, not a transaction failure.
62 mMessage.setTo(message);
63}
64
65void Status::setServiceSpecificError(int32_t errorCode, const String8& message) {
66 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
76String8 Status::toString8() const {
77 String8 ret;
78 if (mException == EX_NONE) {
79 ret.append("No error");
80 } else {
81 ret.appendFormat("Status(%d): '", mException);
82 if (mException == EX_SERVICE_SPECIFIC ||
83 mException == EX_TRANSACTION_FAILED) {
84 ret.appendFormat("%d: ", mErrorCode);
85 }
86 ret.append(String8(mMessage));
87 ret.append("'");
88 }
89 return ret;
90}
91
92std::stringstream& operator<< (std::stringstream& stream, const Status& s) {
93 stream << s.toString8().string();
94 return stream;
95}
96
97} // namespace hardware
98} // namespace android