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