1 /*
2  * Copyright (C) 2020 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_UTILS_COMMON_HANDLE_ERROR_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_HANDLE_ERROR_H
19 
20 #include <android/hidl/base/1.0/IBase.h>
21 #include <hidl/HidlSupport.h>
22 #include <nnapi/Result.h>
23 #include <nnapi/Types.h>
24 
25 #include <type_traits>
26 
27 namespace android::hardware::neuralnetworks::utils {
28 
29 template <typename Type>
handleTransportError(const hardware::Return<Type> & ret)30 nn::GeneralResult<Type> handleTransportError(const hardware::Return<Type>& ret) {
31     if (ret.isDeadObject()) {
32         return nn::error(nn::ErrorStatus::DEAD_OBJECT)
33                << "Return<>::isDeadObject returned true: " << ret.description();
34     }
35     if (!ret.isOk()) {
36         return nn::error(nn::ErrorStatus::GENERAL_FAILURE)
37                << "Return<>::isOk returned false: " << ret.description();
38     }
39     if constexpr (!std::is_same_v<Type, void>) {
40         return static_cast<Type>(ret);
41     } else {
42         return {};
43     }
44 }
45 
46 #define HANDLE_TRANSPORT_FAILURE(ret)                                                        \
47     ({                                                                                       \
48         auto result = ::android::hardware::neuralnetworks::utils::handleTransportError(ret); \
49         if (!result.has_value()) {                                                           \
50             return NN_ERROR(result.error().code) << result.error().message;                  \
51         }                                                                                    \
52         std::move(result).value();                                                           \
53     })
54 
55 template <typename Type>
56 nn::GeneralResult<Type> makeGeneralFailure(
57         nn::Result<Type> result, nn::ErrorStatus status = nn::ErrorStatus::GENERAL_FAILURE) {
58     if (!result.has_value()) {
59         return nn::error(status) << std::move(result).error();
60     }
61     if constexpr (!std::is_same_v<Type, void>) {
62         return std::move(result).value();
63     } else {
64         return {};
65     }
66 }
67 
68 template <typename Type>
makeExecutionFailure(nn::GeneralResult<Type> result)69 nn::ExecutionResult<Type> makeExecutionFailure(nn::GeneralResult<Type> result) {
70     if (!result.has_value()) {
71         const auto [message, status] = std::move(result).error();
72         return nn::error(status) << message;
73     }
74     if constexpr (!std::is_same_v<Type, void>) {
75         return std::move(result).value();
76     } else {
77         return {};
78     }
79 }
80 
81 template <typename Type>
82 nn::ExecutionResult<Type> makeExecutionFailure(
83         nn::Result<Type> result, nn::ErrorStatus status = nn::ErrorStatus::GENERAL_FAILURE) {
84     return makeExecutionFailure(makeGeneralFailure(result, status));
85 }
86 
87 #define HANDLE_HAL_STATUS(status)                                       \
88     if (const auto canonical = ::android::nn::convert(status).value_or( \
89                 ::android::nn::ErrorStatus::GENERAL_FAILURE);           \
90         canonical == ::android::nn::ErrorStatus::NONE) {                \
91     } else                                                              \
92         return NN_ERROR(canonical)
93 
94 }  // namespace android::hardware::neuralnetworks::utils
95 
96 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_HANDLE_ERROR_H
97