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