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