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