1 /* 2 * Copyright (C) 2021 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_AIDL_UTILS_H 18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_H 19 20 #include "nnapi/hal/aidl/Conversions.h" 21 22 #include <android-base/logging.h> 23 #include <nnapi/Result.h> 24 #include <nnapi/TypeUtils.h> 25 #include <nnapi/Types.h> 26 #include <nnapi/Validation.h> 27 #include <nnapi/hal/HandleError.h> 28 29 namespace aidl::android::hardware::neuralnetworks::utils { 30 31 constexpr auto kDefaultPriority = Priority::MEDIUM; 32 constexpr auto kVersion = nn::Version::ANDROID_S; 33 34 template <typename Type> validate(const Type & halObject)35nn::Result<void> validate(const Type& halObject) { 36 const auto maybeCanonical = nn::convert(halObject); 37 if (!maybeCanonical.has_value()) { 38 return nn::error() << maybeCanonical.error().message; 39 } 40 return {}; 41 } 42 43 template <typename Type> valid(const Type & halObject)44bool valid(const Type& halObject) { 45 const auto result = utils::validate(halObject); 46 if (!result.has_value()) { 47 LOG(ERROR) << result.error(); 48 } 49 return result.has_value(); 50 } 51 52 template <typename Type> compliantVersion(const Type & canonical)53nn::GeneralResult<void> compliantVersion(const Type& canonical) { 54 const auto version = NN_TRY(::android::hardware::neuralnetworks::utils::makeGeneralFailure( 55 nn::validate(canonical))); 56 if (version > kVersion) { 57 return NN_ERROR() << "Insufficient version: " << version << " vs required " << kVersion; 58 } 59 return {}; 60 } 61 62 template <typename Type> 63 auto convertFromNonCanonical(const Type& nonCanonicalObject) 64 -> decltype(convert(nn::convert(nonCanonicalObject).value())) { 65 return convert(NN_TRY(nn::convert(nonCanonicalObject))); 66 } 67 68 nn::GeneralResult<Memory> clone(const Memory& memory); 69 nn::GeneralResult<Request> clone(const Request& request); 70 nn::GeneralResult<RequestMemoryPool> clone(const RequestMemoryPool& requestPool); 71 nn::GeneralResult<Model> clone(const Model& model); 72 73 nn::GeneralResult<void> handleTransportError(const ndk::ScopedAStatus& ret); 74 75 #define HANDLE_ASTATUS(ret) \ 76 for (const auto status = handleTransportError(ret); !status.ok();) \ 77 return NN_ERROR(status.error().code) << status.error().message << ": " 78 79 } // namespace aidl::android::hardware::neuralnetworks::utils 80 81 #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_H 82