1 /*
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
20 #include <android-base/logging.h>
21
22 using ::android::status_t;
23 using ::android::statusToString;
24 using ::android::binder::Status;
25
AStatus_newOk()26 AStatus* AStatus_newOk() {
27 static AStatus status = AStatus();
28 return &status;
29 }
30
AStatus_fromExceptionCode(binder_exception_t exception)31 AStatus* AStatus_fromExceptionCode(binder_exception_t exception) {
32 return new AStatus(Status::fromExceptionCode(PruneException(exception)));
33 }
34
AStatus_fromExceptionCodeWithMessage(binder_exception_t exception,const char * message)35 AStatus* AStatus_fromExceptionCodeWithMessage(binder_exception_t exception, const char* message) {
36 return new AStatus(Status::fromExceptionCode(PruneException(exception), message));
37 }
38
AStatus_fromServiceSpecificError(int32_t serviceSpecific)39 AStatus* AStatus_fromServiceSpecificError(int32_t serviceSpecific) {
40 return new AStatus(Status::fromServiceSpecificError(serviceSpecific));
41 }
42
AStatus_fromServiceSpecificErrorWithMessage(int32_t serviceSpecific,const char * message)43 AStatus* AStatus_fromServiceSpecificErrorWithMessage(int32_t serviceSpecific, const char* message) {
44 return new AStatus(Status::fromServiceSpecificError(serviceSpecific, message));
45 }
46
AStatus_fromStatus(binder_status_t status)47 AStatus* AStatus_fromStatus(binder_status_t status) {
48 return new AStatus(Status::fromStatusT(PruneStatusT(status)));
49 }
50
AStatus_isOk(const AStatus * status)51 bool AStatus_isOk(const AStatus* status) {
52 return status->get().isOk();
53 }
54
AStatus_getExceptionCode(const AStatus * status)55 binder_exception_t AStatus_getExceptionCode(const AStatus* status) {
56 return PruneException(status->get().exceptionCode());
57 }
58
AStatus_getServiceSpecificError(const AStatus * status)59 int32_t AStatus_getServiceSpecificError(const AStatus* status) {
60 return status->get().serviceSpecificErrorCode();
61 }
62
AStatus_getStatus(const AStatus * status)63 binder_status_t AStatus_getStatus(const AStatus* status) {
64 return PruneStatusT(status->get().transactionError());
65 }
66
AStatus_getMessage(const AStatus * status)67 const char* AStatus_getMessage(const AStatus* status) {
68 return status->get().exceptionMessage().c_str();
69 }
70
AStatus_getDescription(const AStatus * status)71 const char* AStatus_getDescription(const AStatus* status) {
72 android::String8 description = status->get().toString8();
73 char* cStr = new char[description.size() + 1];
74 memcpy(cStr, description.c_str(), description.size() + 1);
75 return cStr;
76 }
77
AStatus_deleteDescription(const char * description)78 void AStatus_deleteDescription(const char* description) {
79 delete[] const_cast<char*>(description);
80 }
81
AStatus_delete(AStatus * status)82 void AStatus_delete(AStatus* status) {
83 if (status != AStatus_newOk()) {
84 delete status;
85 }
86 }
87
PruneStatusT(status_t status)88 binder_status_t PruneStatusT(status_t status) {
89 switch (status) {
90 case ::android::OK:
91 return STATUS_OK;
92 case ::android::NO_MEMORY:
93 return STATUS_NO_MEMORY;
94 case ::android::INVALID_OPERATION:
95 return STATUS_INVALID_OPERATION;
96 case ::android::BAD_VALUE:
97 return STATUS_BAD_VALUE;
98 case ::android::BAD_TYPE:
99 return STATUS_BAD_TYPE;
100 case ::android::NAME_NOT_FOUND:
101 return STATUS_NAME_NOT_FOUND;
102 case ::android::PERMISSION_DENIED:
103 return STATUS_PERMISSION_DENIED;
104 case ::android::NO_INIT:
105 return STATUS_NO_INIT;
106 case ::android::ALREADY_EXISTS:
107 return STATUS_ALREADY_EXISTS;
108 case ::android::DEAD_OBJECT:
109 return STATUS_DEAD_OBJECT;
110 case ::android::FAILED_TRANSACTION:
111 return STATUS_FAILED_TRANSACTION;
112 case ::android::BAD_INDEX:
113 return STATUS_BAD_INDEX;
114 case ::android::NOT_ENOUGH_DATA:
115 return STATUS_NOT_ENOUGH_DATA;
116 case ::android::WOULD_BLOCK:
117 return STATUS_WOULD_BLOCK;
118 case ::android::TIMED_OUT:
119 return STATUS_TIMED_OUT;
120 case ::android::UNKNOWN_TRANSACTION:
121 return STATUS_UNKNOWN_TRANSACTION;
122 case ::android::FDS_NOT_ALLOWED:
123 return STATUS_FDS_NOT_ALLOWED;
124 case ::android::UNEXPECTED_NULL:
125 return STATUS_UNEXPECTED_NULL;
126 case ::android::UNKNOWN_ERROR:
127 return STATUS_UNKNOWN_ERROR;
128
129 default:
130 LOG(WARNING) << __func__ << ": Unknown status_t (" << statusToString(status)
131 << ") pruned into STATUS_UNKNOWN_ERROR";
132 return STATUS_UNKNOWN_ERROR;
133 }
134 }
135
PruneException(int32_t exception)136 binder_exception_t PruneException(int32_t exception) {
137 switch (exception) {
138 case Status::EX_NONE:
139 return EX_NONE;
140 case Status::EX_SECURITY:
141 return EX_SECURITY;
142 case Status::EX_BAD_PARCELABLE:
143 return EX_BAD_PARCELABLE;
144 case Status::EX_ILLEGAL_ARGUMENT:
145 return EX_ILLEGAL_ARGUMENT;
146 case Status::EX_NULL_POINTER:
147 return EX_NULL_POINTER;
148 case Status::EX_ILLEGAL_STATE:
149 return EX_ILLEGAL_STATE;
150 case Status::EX_NETWORK_MAIN_THREAD:
151 return EX_NETWORK_MAIN_THREAD;
152 case Status::EX_UNSUPPORTED_OPERATION:
153 return EX_UNSUPPORTED_OPERATION;
154 case Status::EX_SERVICE_SPECIFIC:
155 return EX_SERVICE_SPECIFIC;
156 case Status::EX_PARCELABLE:
157 return EX_PARCELABLE;
158 case Status::EX_TRANSACTION_FAILED:
159 return EX_TRANSACTION_FAILED;
160
161 default:
162 LOG(WARNING) << __func__ << ": Unknown binder exception (" << exception
163 << ") pruned into EX_TRANSACTION_FAILED";
164 return EX_TRANSACTION_FAILED;
165 }
166 }
167