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 <aidl/android/hardware/neuralnetworks/IDevice.h> 23 #include <android-base/logging.h> 24 #include <nnapi/Result.h> 25 #include <nnapi/TypeUtils.h> 26 #include <nnapi/Types.h> 27 #include <nnapi/Validation.h> 28 29 #include <type_traits> 30 31 namespace aidl::android::hardware::neuralnetworks::utils { 32 33 constexpr auto kDefaultPriority = Priority::MEDIUM; 34 aidlVersionToCanonicalVersion(int aidlVersion)35constexpr std::optional<nn::Version> aidlVersionToCanonicalVersion(int aidlVersion) { 36 switch (aidlVersion) { 37 case 1: 38 return nn::kVersionFeatureLevel5; 39 case 2: 40 return nn::kVersionFeatureLevel6; 41 case 3: 42 return nn::kVersionFeatureLevel7; 43 case 4: 44 return nn::kVersionFeatureLevel8; 45 default: 46 return std::nullopt; 47 } 48 } 49 50 constexpr auto kVersion = aidlVersionToCanonicalVersion(IDevice::version).value(); 51 52 template <typename Type> validate(const Type & halObject)53nn::Result<void> validate(const Type& halObject) { 54 const auto maybeCanonical = nn::convert(halObject); 55 if (!maybeCanonical.has_value()) { 56 return nn::error() << maybeCanonical.error().message; 57 } 58 return {}; 59 } 60 61 template <typename Type> valid(const Type & halObject)62bool valid(const Type& halObject) { 63 const auto result = utils::validate(halObject); 64 if (!result.has_value()) { 65 LOG(ERROR) << result.error(); 66 } 67 return result.has_value(); 68 } 69 70 template <typename Type> compliantVersion(const Type & canonical)71nn::Result<void> compliantVersion(const Type& canonical) { 72 const auto version = NN_TRY(nn::validate(canonical)); 73 if (!nn::isCompliantVersion(version, kVersion)) { 74 return NN_ERROR() << "Insufficient version: " << version << " vs required " << kVersion; 75 } 76 return {}; 77 } 78 79 template <typename Type> 80 auto convertFromNonCanonical(const Type& nonCanonicalObject) 81 -> decltype(convert(nn::convert(nonCanonicalObject).value())) { 82 return convert(NN_TRY(nn::convert(nonCanonicalObject))); 83 } 84 85 template <typename Type> underlyingType(Type value)86constexpr std::underlying_type_t<Type> underlyingType(Type value) { 87 return static_cast<std::underlying_type_t<Type>>(value); 88 } 89 90 nn::GeneralResult<Memory> clone(const Memory& memory); 91 nn::GeneralResult<Request> clone(const Request& request); 92 nn::GeneralResult<RequestMemoryPool> clone(const RequestMemoryPool& requestPool); 93 nn::GeneralResult<Model> clone(const Model& model); 94 95 nn::GeneralResult<void> handleTransportError(const ndk::ScopedAStatus& ret); 96 97 #define HANDLE_ASTATUS(ret) \ 98 for (const auto status = handleTransportError(ret); !status.ok();) \ 99 return NN_ERROR(status.error().code) << status.error().message << ": " 100 101 #define HANDLE_STATUS_AIDL(status) \ 102 if (const ::android::nn::ErrorStatus canonical = ::android::nn::convert(status).value_or( \ 103 ::android::nn::ErrorStatus::GENERAL_FAILURE); \ 104 canonical == ::android::nn::ErrorStatus::NONE) { \ 105 } else \ 106 return NN_ERROR(canonical) 107 108 } // namespace aidl::android::hardware::neuralnetworks::utils 109 110 #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_H 111