1 /* 2 * Copyright (C) 2020 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 #ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_HANDLE_ERROR_H 18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_HANDLE_ERROR_H 19 20 #include <android/hidl/base/1.0/IBase.h> 21 #include <hidl/HidlSupport.h> 22 #include <nnapi/Result.h> 23 #include <nnapi/Types.h> 24 25 #include <type_traits> 26 27 namespace android::hardware::neuralnetworks::utils { 28 29 template <typename Type> handleTransportError(const Return<Type> & ret)30nn::GeneralResult<Type> handleTransportError(const Return<Type>& ret) { 31 if (ret.isDeadObject()) { 32 return nn::error(nn::ErrorStatus::DEAD_OBJECT) 33 << "Return<>::isDeadObject returned true: " << ret.description(); 34 } 35 if (!ret.isOk()) { 36 return nn::error(nn::ErrorStatus::GENERAL_FAILURE) 37 << "Return<>::isOk returned false: " << ret.description(); 38 } 39 if constexpr (!std::is_same_v<Type, void>) { 40 return static_cast<Type>(ret); 41 } else { 42 return {}; 43 } 44 } 45 46 #define HANDLE_TRANSPORT_FAILURE(ret) \ 47 ({ \ 48 auto result = ::android::hardware::neuralnetworks::utils::handleTransportError(ret); \ 49 if (!result.has_value()) { \ 50 return NN_ERROR(result.error().code) << result.error().message; \ 51 } \ 52 std::move(result).value(); \ 53 }) 54 55 #define HANDLE_STATUS_HIDL(status) \ 56 if (const ::android::nn::ErrorStatus canonical = ::android::nn::convert(status).value_or( \ 57 ::android::nn::ErrorStatus::GENERAL_FAILURE); \ 58 canonical == ::android::nn::ErrorStatus::NONE) { \ 59 } else \ 60 return NN_ERROR(canonical) 61 62 } // namespace android::hardware::neuralnetworks::utils 63 64 #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_HANDLE_ERROR_H 65