blob: 3aac3c05455c71c79d06793cc1081fc08074f99d [file] [log] [blame]
Steven Moreland5d62e442018-09-13 15:01:02 -07001/*
2 * Copyright (C) 2018 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 <android/binder_status.h>
18#include "status_internal.h"
19
Steven Moreland5d62e442018-09-13 15:01:02 -070020using ::android::status_t;
Steven Moreland6d9a2922021-09-22 10:16:45 -070021using ::android::statusToString;
Steven Moreland5d62e442018-09-13 15:01:02 -070022using ::android::binder::Status;
23
Steven Moreland9a51db82018-09-14 10:59:35 -070024AStatus* AStatus_newOk() {
Steven Moreland4a1026a2020-09-22 22:21:18 +000025 static AStatus status = AStatus();
26 return &status;
Steven Moreland9a51db82018-09-14 10:59:35 -070027}
28
29AStatus* AStatus_fromExceptionCode(binder_exception_t exception) {
Steven Moreland164636c2018-10-05 13:42:53 -070030 return new AStatus(Status::fromExceptionCode(PruneException(exception)));
Steven Moreland9a51db82018-09-14 10:59:35 -070031}
32
33AStatus* AStatus_fromExceptionCodeWithMessage(binder_exception_t exception, const char* message) {
Steven Moreland164636c2018-10-05 13:42:53 -070034 return new AStatus(Status::fromExceptionCode(PruneException(exception), message));
Steven Moreland9a51db82018-09-14 10:59:35 -070035}
36
37AStatus* AStatus_fromServiceSpecificError(int32_t serviceSpecific) {
38 return new AStatus(Status::fromServiceSpecificError(serviceSpecific));
39}
40
41AStatus* AStatus_fromServiceSpecificErrorWithMessage(int32_t serviceSpecific, const char* message) {
42 return new AStatus(Status::fromServiceSpecificError(serviceSpecific, message));
43}
44
45AStatus* AStatus_fromStatus(binder_status_t status) {
Steven Moreland164636c2018-10-05 13:42:53 -070046 return new AStatus(Status::fromStatusT(PruneStatusT(status)));
Steven Moreland9a51db82018-09-14 10:59:35 -070047}
48
49bool AStatus_isOk(const AStatus* status) {
Steven Moreland85c138a2020-09-22 22:00:52 +000050 return status->get().isOk();
Steven Moreland9a51db82018-09-14 10:59:35 -070051}
52
53binder_exception_t AStatus_getExceptionCode(const AStatus* status) {
Steven Moreland85c138a2020-09-22 22:00:52 +000054 return PruneException(status->get().exceptionCode());
Steven Moreland9a51db82018-09-14 10:59:35 -070055}
56
57int32_t AStatus_getServiceSpecificError(const AStatus* status) {
Steven Moreland85c138a2020-09-22 22:00:52 +000058 return status->get().serviceSpecificErrorCode();
Steven Moreland9a51db82018-09-14 10:59:35 -070059}
60
61binder_status_t AStatus_getStatus(const AStatus* status) {
Steven Moreland85c138a2020-09-22 22:00:52 +000062 return PruneStatusT(status->get().transactionError());
Steven Moreland9a51db82018-09-14 10:59:35 -070063}
64
65const char* AStatus_getMessage(const AStatus* status) {
Steven Moreland85c138a2020-09-22 22:00:52 +000066 return status->get().exceptionMessage().c_str();
Steven Moreland9a51db82018-09-14 10:59:35 -070067}
68
Steven Moreland74772162019-12-11 16:42:43 -080069const char* AStatus_getDescription(const AStatus* status) {
Steven Moreland85c138a2020-09-22 22:00:52 +000070 android::String8 description = status->get().toString8();
Steven Moreland74772162019-12-11 16:42:43 -080071 char* cStr = new char[description.size() + 1];
72 memcpy(cStr, description.c_str(), description.size() + 1);
73 return cStr;
74}
75
76void AStatus_deleteDescription(const char* description) {
77 delete[] const_cast<char*>(description);
78}
79
Steven Moreland9b80e282018-09-19 13:57:23 -070080void AStatus_delete(AStatus* status) {
Steven Moreland4a1026a2020-09-22 22:21:18 +000081 if (status != AStatus_newOk()) {
82 delete status;
83 }
Steven Moreland9a51db82018-09-14 10:59:35 -070084}
85
Steven Moreland5d62e442018-09-13 15:01:02 -070086binder_status_t PruneStatusT(status_t status) {
Steven Moreland5d62e442018-09-13 15:01:02 -070087 switch (status) {
88 case ::android::OK:
89 return STATUS_OK;
90 case ::android::NO_MEMORY:
91 return STATUS_NO_MEMORY;
92 case ::android::INVALID_OPERATION:
93 return STATUS_INVALID_OPERATION;
94 case ::android::BAD_VALUE:
95 return STATUS_BAD_VALUE;
96 case ::android::BAD_TYPE:
97 return STATUS_BAD_TYPE;
98 case ::android::NAME_NOT_FOUND:
99 return STATUS_NAME_NOT_FOUND;
100 case ::android::PERMISSION_DENIED:
101 return STATUS_PERMISSION_DENIED;
102 case ::android::NO_INIT:
103 return STATUS_NO_INIT;
104 case ::android::ALREADY_EXISTS:
105 return STATUS_ALREADY_EXISTS;
106 case ::android::DEAD_OBJECT:
107 return STATUS_DEAD_OBJECT;
108 case ::android::FAILED_TRANSACTION:
109 return STATUS_FAILED_TRANSACTION;
110 case ::android::BAD_INDEX:
111 return STATUS_BAD_INDEX;
112 case ::android::NOT_ENOUGH_DATA:
113 return STATUS_NOT_ENOUGH_DATA;
114 case ::android::WOULD_BLOCK:
115 return STATUS_WOULD_BLOCK;
116 case ::android::TIMED_OUT:
117 return STATUS_TIMED_OUT;
118 case ::android::UNKNOWN_TRANSACTION:
119 return STATUS_UNKNOWN_TRANSACTION;
120 case ::android::FDS_NOT_ALLOWED:
121 return STATUS_FDS_NOT_ALLOWED;
122 case ::android::UNEXPECTED_NULL:
123 return STATUS_UNEXPECTED_NULL;
124 case ::android::UNKNOWN_ERROR:
125 return STATUS_UNKNOWN_ERROR;
126
127 default:
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700128 ALOGW("%s: Unknown status_t (%s) pruned into STATUS_UNKNOWN_ERROR", __func__,
129 statusToString(status).c_str());
Steven Moreland5d62e442018-09-13 15:01:02 -0700130 return STATUS_UNKNOWN_ERROR;
131 }
132}
133
134binder_exception_t PruneException(int32_t exception) {
135 switch (exception) {
136 case Status::EX_NONE:
137 return EX_NONE;
138 case Status::EX_SECURITY:
139 return EX_SECURITY;
140 case Status::EX_BAD_PARCELABLE:
141 return EX_BAD_PARCELABLE;
142 case Status::EX_ILLEGAL_ARGUMENT:
143 return EX_ILLEGAL_ARGUMENT;
144 case Status::EX_NULL_POINTER:
145 return EX_NULL_POINTER;
146 case Status::EX_ILLEGAL_STATE:
147 return EX_ILLEGAL_STATE;
148 case Status::EX_NETWORK_MAIN_THREAD:
149 return EX_NETWORK_MAIN_THREAD;
150 case Status::EX_UNSUPPORTED_OPERATION:
151 return EX_UNSUPPORTED_OPERATION;
152 case Status::EX_SERVICE_SPECIFIC:
153 return EX_SERVICE_SPECIFIC;
154 case Status::EX_PARCELABLE:
155 return EX_PARCELABLE;
156 case Status::EX_TRANSACTION_FAILED:
157 return EX_TRANSACTION_FAILED;
158
159 default:
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700160 ALOGW("%s: Unknown binder exception (%d) pruned into EX_TRANSACTION_FAILED", __func__,
161 exception);
Steven Moreland5d62e442018-09-13 15:01:02 -0700162 return EX_TRANSACTION_FAILED;
163 }
164}