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 #pragma once 18 19 #include <aidl/android/hardware/neuralnetworks/ErrorStatus.h> 20 #include <android/binder_auto_utils.h> 21 #include <string> 22 #include <vector> 23 #include "SupportLibraryWrapper.h" 24 25 namespace aidl::android::hardware::neuralnetworks { 26 27 ErrorStatus convertResultToErrorStatus(::android::nn::wrapper::Result status); 28 bool isValidDimension(const std::vector<int32_t>& v); 29 ndk::ScopedAStatus toAStatus(ErrorStatus errorStatus, const std::string& errorMessage); 30 ndk::ScopedAStatus toAStatus(ErrorStatus errorStatus); 31 32 #define SLW2SAS_RETURN_IF_ERROR(expr) \ 33 do { \ 34 const Result nnReturnIfErrorErrorCode = static_cast<Result>(expr); \ 35 if (nnReturnIfErrorErrorCode != Result::NO_ERROR) { \ 36 const auto nnReturnIfErrorErrorCodeConverted = \ 37 convertResultToErrorStatus(nnReturnIfErrorErrorCode); \ 38 return toAStatus(nnReturnIfErrorErrorCodeConverted); \ 39 } \ 40 } while (0) 41 42 #define SLW2SAS_RETURN_AND_CALLBACK_IF_ERROR(expr, callback) \ 43 do { \ 44 const Result nnReturnIfErrorErrorCode = static_cast<Result>(expr); \ 45 if (nnReturnIfErrorErrorCode != Result::NO_ERROR) { \ 46 const auto nnReturnIfErrorErrorCodeConverted = \ 47 convertResultToErrorStatus(nnReturnIfErrorErrorCode); \ 48 callback->notify(nnReturnIfErrorErrorCodeConverted, nullptr); \ 49 return toAStatus(nnReturnIfErrorErrorCodeConverted); \ 50 } \ 51 } while (0) 52 53 #define SLW2SAS_OK_RETURN_AND_ERROR_CALLBACK_IF_ERROR(expr, callback) \ 54 do { \ 55 const Result nnReturnIfErrorErrorCode = static_cast<Result>(expr); \ 56 if (nnReturnIfErrorErrorCode != Result::NO_ERROR) { \ 57 const auto nnReturnIfErrorErrorCodeConverted = \ 58 convertResultToErrorStatus(nnReturnIfErrorErrorCode); \ 59 callback->notify(nnReturnIfErrorErrorCodeConverted, nullptr); \ 60 return ndk::ScopedAStatus::ok(); \ 61 } \ 62 } while (0) 63 64 } // namespace aidl::android::hardware::neuralnetworks 65