xref: /aosp_15_r20/frameworks/native/libs/binder/ndk/status.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include <android/binder_status.h>
18*38e8c45fSAndroid Build Coastguard Worker #include "status_internal.h"
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker using ::android::status_t;
21*38e8c45fSAndroid Build Coastguard Worker using ::android::statusToString;
22*38e8c45fSAndroid Build Coastguard Worker using ::android::binder::Status;
23*38e8c45fSAndroid Build Coastguard Worker 
AStatus_newOk()24*38e8c45fSAndroid Build Coastguard Worker AStatus* AStatus_newOk() {
25*38e8c45fSAndroid Build Coastguard Worker     static AStatus status = AStatus();
26*38e8c45fSAndroid Build Coastguard Worker     return &status;
27*38e8c45fSAndroid Build Coastguard Worker }
28*38e8c45fSAndroid Build Coastguard Worker 
AStatus_fromExceptionCode(binder_exception_t exception)29*38e8c45fSAndroid Build Coastguard Worker AStatus* AStatus_fromExceptionCode(binder_exception_t exception) {
30*38e8c45fSAndroid Build Coastguard Worker     return new AStatus(Status::fromExceptionCode(PruneException(exception)));
31*38e8c45fSAndroid Build Coastguard Worker }
32*38e8c45fSAndroid Build Coastguard Worker 
AStatus_fromExceptionCodeWithMessage(binder_exception_t exception,const char * message)33*38e8c45fSAndroid Build Coastguard Worker AStatus* AStatus_fromExceptionCodeWithMessage(binder_exception_t exception, const char* message) {
34*38e8c45fSAndroid Build Coastguard Worker     return new AStatus(Status::fromExceptionCode(PruneException(exception), message));
35*38e8c45fSAndroid Build Coastguard Worker }
36*38e8c45fSAndroid Build Coastguard Worker 
AStatus_fromServiceSpecificError(int32_t serviceSpecific)37*38e8c45fSAndroid Build Coastguard Worker AStatus* AStatus_fromServiceSpecificError(int32_t serviceSpecific) {
38*38e8c45fSAndroid Build Coastguard Worker     return new AStatus(Status::fromServiceSpecificError(serviceSpecific));
39*38e8c45fSAndroid Build Coastguard Worker }
40*38e8c45fSAndroid Build Coastguard Worker 
AStatus_fromServiceSpecificErrorWithMessage(int32_t serviceSpecific,const char * message)41*38e8c45fSAndroid Build Coastguard Worker AStatus* AStatus_fromServiceSpecificErrorWithMessage(int32_t serviceSpecific, const char* message) {
42*38e8c45fSAndroid Build Coastguard Worker     return new AStatus(Status::fromServiceSpecificError(serviceSpecific, message));
43*38e8c45fSAndroid Build Coastguard Worker }
44*38e8c45fSAndroid Build Coastguard Worker 
AStatus_fromStatus(binder_status_t status)45*38e8c45fSAndroid Build Coastguard Worker AStatus* AStatus_fromStatus(binder_status_t status) {
46*38e8c45fSAndroid Build Coastguard Worker     return new AStatus(Status::fromStatusT(PruneStatusT(status)));
47*38e8c45fSAndroid Build Coastguard Worker }
48*38e8c45fSAndroid Build Coastguard Worker 
AStatus_isOk(const AStatus * status)49*38e8c45fSAndroid Build Coastguard Worker bool AStatus_isOk(const AStatus* status) {
50*38e8c45fSAndroid Build Coastguard Worker     return status->get().isOk();
51*38e8c45fSAndroid Build Coastguard Worker }
52*38e8c45fSAndroid Build Coastguard Worker 
AStatus_getExceptionCode(const AStatus * status)53*38e8c45fSAndroid Build Coastguard Worker binder_exception_t AStatus_getExceptionCode(const AStatus* status) {
54*38e8c45fSAndroid Build Coastguard Worker     return PruneException(status->get().exceptionCode());
55*38e8c45fSAndroid Build Coastguard Worker }
56*38e8c45fSAndroid Build Coastguard Worker 
AStatus_getServiceSpecificError(const AStatus * status)57*38e8c45fSAndroid Build Coastguard Worker int32_t AStatus_getServiceSpecificError(const AStatus* status) {
58*38e8c45fSAndroid Build Coastguard Worker     return status->get().serviceSpecificErrorCode();
59*38e8c45fSAndroid Build Coastguard Worker }
60*38e8c45fSAndroid Build Coastguard Worker 
AStatus_getStatus(const AStatus * status)61*38e8c45fSAndroid Build Coastguard Worker binder_status_t AStatus_getStatus(const AStatus* status) {
62*38e8c45fSAndroid Build Coastguard Worker     return PruneStatusT(status->get().transactionError());
63*38e8c45fSAndroid Build Coastguard Worker }
64*38e8c45fSAndroid Build Coastguard Worker 
AStatus_getMessage(const AStatus * status)65*38e8c45fSAndroid Build Coastguard Worker const char* AStatus_getMessage(const AStatus* status) {
66*38e8c45fSAndroid Build Coastguard Worker     return status->get().exceptionMessage().c_str();
67*38e8c45fSAndroid Build Coastguard Worker }
68*38e8c45fSAndroid Build Coastguard Worker 
AStatus_getDescription(const AStatus * status)69*38e8c45fSAndroid Build Coastguard Worker const char* AStatus_getDescription(const AStatus* status) {
70*38e8c45fSAndroid Build Coastguard Worker     android::String8 description = status->get().toString8();
71*38e8c45fSAndroid Build Coastguard Worker     char* cStr = new char[description.size() + 1];
72*38e8c45fSAndroid Build Coastguard Worker     memcpy(cStr, description.c_str(), description.size() + 1);
73*38e8c45fSAndroid Build Coastguard Worker     return cStr;
74*38e8c45fSAndroid Build Coastguard Worker }
75*38e8c45fSAndroid Build Coastguard Worker 
AStatus_deleteDescription(const char * description)76*38e8c45fSAndroid Build Coastguard Worker void AStatus_deleteDescription(const char* description) {
77*38e8c45fSAndroid Build Coastguard Worker     delete[] const_cast<char*>(description);
78*38e8c45fSAndroid Build Coastguard Worker }
79*38e8c45fSAndroid Build Coastguard Worker 
AStatus_delete(AStatus * status)80*38e8c45fSAndroid Build Coastguard Worker void AStatus_delete(AStatus* status) {
81*38e8c45fSAndroid Build Coastguard Worker     if (status != AStatus_newOk()) {
82*38e8c45fSAndroid Build Coastguard Worker         delete status;
83*38e8c45fSAndroid Build Coastguard Worker     }
84*38e8c45fSAndroid Build Coastguard Worker }
85*38e8c45fSAndroid Build Coastguard Worker 
PruneStatusT(status_t status)86*38e8c45fSAndroid Build Coastguard Worker binder_status_t PruneStatusT(status_t status) {
87*38e8c45fSAndroid Build Coastguard Worker     switch (status) {
88*38e8c45fSAndroid Build Coastguard Worker         case ::android::OK:
89*38e8c45fSAndroid Build Coastguard Worker             return STATUS_OK;
90*38e8c45fSAndroid Build Coastguard Worker         case ::android::NO_MEMORY:
91*38e8c45fSAndroid Build Coastguard Worker             return STATUS_NO_MEMORY;
92*38e8c45fSAndroid Build Coastguard Worker         case ::android::INVALID_OPERATION:
93*38e8c45fSAndroid Build Coastguard Worker             return STATUS_INVALID_OPERATION;
94*38e8c45fSAndroid Build Coastguard Worker         case ::android::BAD_VALUE:
95*38e8c45fSAndroid Build Coastguard Worker             return STATUS_BAD_VALUE;
96*38e8c45fSAndroid Build Coastguard Worker         case ::android::BAD_TYPE:
97*38e8c45fSAndroid Build Coastguard Worker             return STATUS_BAD_TYPE;
98*38e8c45fSAndroid Build Coastguard Worker         case ::android::NAME_NOT_FOUND:
99*38e8c45fSAndroid Build Coastguard Worker             return STATUS_NAME_NOT_FOUND;
100*38e8c45fSAndroid Build Coastguard Worker         case ::android::PERMISSION_DENIED:
101*38e8c45fSAndroid Build Coastguard Worker             return STATUS_PERMISSION_DENIED;
102*38e8c45fSAndroid Build Coastguard Worker         case ::android::NO_INIT:
103*38e8c45fSAndroid Build Coastguard Worker             return STATUS_NO_INIT;
104*38e8c45fSAndroid Build Coastguard Worker         case ::android::ALREADY_EXISTS:
105*38e8c45fSAndroid Build Coastguard Worker             return STATUS_ALREADY_EXISTS;
106*38e8c45fSAndroid Build Coastguard Worker         case ::android::DEAD_OBJECT:
107*38e8c45fSAndroid Build Coastguard Worker             return STATUS_DEAD_OBJECT;
108*38e8c45fSAndroid Build Coastguard Worker         case ::android::FAILED_TRANSACTION:
109*38e8c45fSAndroid Build Coastguard Worker             return STATUS_FAILED_TRANSACTION;
110*38e8c45fSAndroid Build Coastguard Worker         case ::android::BAD_INDEX:
111*38e8c45fSAndroid Build Coastguard Worker             return STATUS_BAD_INDEX;
112*38e8c45fSAndroid Build Coastguard Worker         case ::android::NOT_ENOUGH_DATA:
113*38e8c45fSAndroid Build Coastguard Worker             return STATUS_NOT_ENOUGH_DATA;
114*38e8c45fSAndroid Build Coastguard Worker         case ::android::WOULD_BLOCK:
115*38e8c45fSAndroid Build Coastguard Worker             return STATUS_WOULD_BLOCK;
116*38e8c45fSAndroid Build Coastguard Worker         case ::android::TIMED_OUT:
117*38e8c45fSAndroid Build Coastguard Worker             return STATUS_TIMED_OUT;
118*38e8c45fSAndroid Build Coastguard Worker         case ::android::UNKNOWN_TRANSACTION:
119*38e8c45fSAndroid Build Coastguard Worker             return STATUS_UNKNOWN_TRANSACTION;
120*38e8c45fSAndroid Build Coastguard Worker         case ::android::FDS_NOT_ALLOWED:
121*38e8c45fSAndroid Build Coastguard Worker             return STATUS_FDS_NOT_ALLOWED;
122*38e8c45fSAndroid Build Coastguard Worker         case ::android::UNEXPECTED_NULL:
123*38e8c45fSAndroid Build Coastguard Worker             return STATUS_UNEXPECTED_NULL;
124*38e8c45fSAndroid Build Coastguard Worker         case ::android::UNKNOWN_ERROR:
125*38e8c45fSAndroid Build Coastguard Worker             return STATUS_UNKNOWN_ERROR;
126*38e8c45fSAndroid Build Coastguard Worker 
127*38e8c45fSAndroid Build Coastguard Worker         default:
128*38e8c45fSAndroid Build Coastguard Worker             ALOGW("%s: Unknown status_t (%s) pruned into STATUS_UNKNOWN_ERROR", __func__,
129*38e8c45fSAndroid Build Coastguard Worker                   statusToString(status).c_str());
130*38e8c45fSAndroid Build Coastguard Worker             return STATUS_UNKNOWN_ERROR;
131*38e8c45fSAndroid Build Coastguard Worker     }
132*38e8c45fSAndroid Build Coastguard Worker }
133*38e8c45fSAndroid Build Coastguard Worker 
PruneException(int32_t exception)134*38e8c45fSAndroid Build Coastguard Worker binder_exception_t PruneException(int32_t exception) {
135*38e8c45fSAndroid Build Coastguard Worker     switch (exception) {
136*38e8c45fSAndroid Build Coastguard Worker         case Status::EX_NONE:
137*38e8c45fSAndroid Build Coastguard Worker             return EX_NONE;
138*38e8c45fSAndroid Build Coastguard Worker         case Status::EX_SECURITY:
139*38e8c45fSAndroid Build Coastguard Worker             return EX_SECURITY;
140*38e8c45fSAndroid Build Coastguard Worker         case Status::EX_BAD_PARCELABLE:
141*38e8c45fSAndroid Build Coastguard Worker             return EX_BAD_PARCELABLE;
142*38e8c45fSAndroid Build Coastguard Worker         case Status::EX_ILLEGAL_ARGUMENT:
143*38e8c45fSAndroid Build Coastguard Worker             return EX_ILLEGAL_ARGUMENT;
144*38e8c45fSAndroid Build Coastguard Worker         case Status::EX_NULL_POINTER:
145*38e8c45fSAndroid Build Coastguard Worker             return EX_NULL_POINTER;
146*38e8c45fSAndroid Build Coastguard Worker         case Status::EX_ILLEGAL_STATE:
147*38e8c45fSAndroid Build Coastguard Worker             return EX_ILLEGAL_STATE;
148*38e8c45fSAndroid Build Coastguard Worker         case Status::EX_NETWORK_MAIN_THREAD:
149*38e8c45fSAndroid Build Coastguard Worker             return EX_NETWORK_MAIN_THREAD;
150*38e8c45fSAndroid Build Coastguard Worker         case Status::EX_UNSUPPORTED_OPERATION:
151*38e8c45fSAndroid Build Coastguard Worker             return EX_UNSUPPORTED_OPERATION;
152*38e8c45fSAndroid Build Coastguard Worker         case Status::EX_SERVICE_SPECIFIC:
153*38e8c45fSAndroid Build Coastguard Worker             return EX_SERVICE_SPECIFIC;
154*38e8c45fSAndroid Build Coastguard Worker         case Status::EX_PARCELABLE:
155*38e8c45fSAndroid Build Coastguard Worker             return EX_PARCELABLE;
156*38e8c45fSAndroid Build Coastguard Worker         case Status::EX_TRANSACTION_FAILED:
157*38e8c45fSAndroid Build Coastguard Worker             return EX_TRANSACTION_FAILED;
158*38e8c45fSAndroid Build Coastguard Worker 
159*38e8c45fSAndroid Build Coastguard Worker         default:
160*38e8c45fSAndroid Build Coastguard Worker             ALOGW("%s: Unknown binder exception (%d) pruned into EX_TRANSACTION_FAILED", __func__,
161*38e8c45fSAndroid Build Coastguard Worker                   exception);
162*38e8c45fSAndroid Build Coastguard Worker             return EX_TRANSACTION_FAILED;
163*38e8c45fSAndroid Build Coastguard Worker     }
164*38e8c45fSAndroid Build Coastguard Worker }
165