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 #define LOG_TAG "ShimUtils" 18 19 #include "ShimUtils.h" 20 #include <android-base/logging.h> 21 22 #include <string> 23 #include <vector> 24 25 namespace aidl::android::hardware::neuralnetworks { 26 27 using ::aidl::android::hardware::neuralnetworks::ErrorStatus; 28 using ::android::nn::wrapper::Result; 29 toAStatus(ErrorStatus errorStatus,const std::string & errorMessage)30ndk::ScopedAStatus toAStatus(ErrorStatus errorStatus, const std::string& errorMessage) { 31 if (errorStatus == ErrorStatus::NONE) { 32 return ndk::ScopedAStatus::ok(); 33 } 34 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage( 35 static_cast<int32_t>(errorStatus), errorMessage.c_str()); 36 } 37 toAStatus(ErrorStatus errorStatus)38ndk::ScopedAStatus toAStatus(ErrorStatus errorStatus) { 39 if (errorStatus == ErrorStatus::NONE) { 40 return ndk::ScopedAStatus::ok(); 41 } 42 return ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(errorStatus)); 43 } 44 convertResultToErrorStatus(Result status)45ErrorStatus convertResultToErrorStatus(Result status) { 46 switch (status) { 47 case Result::NO_ERROR: 48 return ErrorStatus::NONE; 49 case Result::OUTPUT_INSUFFICIENT_SIZE: 50 return ErrorStatus::OUTPUT_INSUFFICIENT_SIZE; 51 case Result::UNAVAILABLE_DEVICE: 52 return ErrorStatus::DEVICE_UNAVAILABLE; 53 case Result::MISSED_DEADLINE_TRANSIENT: 54 return ErrorStatus::MISSED_DEADLINE_TRANSIENT; 55 case Result::MISSED_DEADLINE_PERSISTENT: 56 return ErrorStatus::MISSED_DEADLINE_PERSISTENT; 57 case Result::BAD_DATA: 58 case Result::INCOMPLETE: 59 case Result::UNEXPECTED_NULL: 60 case Result::UNMAPPABLE: 61 case Result::OUT_OF_MEMORY: 62 case Result::BAD_STATE: 63 return ErrorStatus::INVALID_ARGUMENT; 64 case Result::OP_FAILED: 65 return ErrorStatus::GENERAL_FAILURE; 66 case Result::FEATURE_LEVEL_TOO_LOW: 67 return ErrorStatus::GENERAL_FAILURE; 68 } 69 70 LOG(FATAL) << "Invalid Result " << static_cast<std::underlying_type_t<Result>>(status); 71 return {}; 72 } 73 isValidDimension(const std::vector<int32_t> & v)74bool isValidDimension(const std::vector<int32_t>& v) { 75 return std::all_of(v.begin(), v.end(), [](int32_t i) { return i >= 0; }); 76 } 77 78 } // namespace aidl::android::hardware::neuralnetworks 79