1 /*
2 * Copyright (C) 2022 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 AIDL_RETURN_UTIL_H_
18 #define AIDL_RETURN_UTIL_H_
19
20 #include "aidl_sync_util.h"
21 #include "wifi_status_util.h"
22
23 namespace aidl {
24 namespace android {
25 namespace hardware {
26 namespace wifi {
27 namespace aidl_return_util {
28 using aidl::android::hardware::wifi::WifiStatusCode;
29 using aidl::android::hardware::wifi::aidl_sync_util::acquireGlobalLock;
30
31 /**
32 * These utility functions are used to invoke a method on the provided
33 * AIDL interface object.
34 * These functions checks if the provided AIDL interface object is valid.
35 * a) If valid, Invokes the corresponding internal implementation function of
36 * the AIDL method.
37 * b) If invalid, return without calling the internal implementation function.
38 */
39
40 // Use for AIDL methods which return only an AIDL status.
41 template <typename ObjT, typename WorkFuncT, typename... Args>
validateAndCall(ObjT * obj,WifiStatusCode status_code_if_invalid,WorkFuncT && work,Args &&...args)42 ::ndk::ScopedAStatus validateAndCall(ObjT* obj, WifiStatusCode status_code_if_invalid,
43 WorkFuncT&& work, Args&&... args) {
44 const auto lock = acquireGlobalLock();
45 if (obj->isValid()) {
46 return (obj->*work)(std::forward<Args>(args)...);
47 } else {
48 return createWifiStatus(status_code_if_invalid);
49 }
50 }
51
52 // Use for AIDL methods which return only an AIDL status.
53 // This version passes the global lock acquired to the body of the method.
54 template <typename ObjT, typename WorkFuncT, typename... Args>
validateAndCallWithLock(ObjT * obj,WifiStatusCode status_code_if_invalid,WorkFuncT && work,Args &&...args)55 ::ndk::ScopedAStatus validateAndCallWithLock(ObjT* obj, WifiStatusCode status_code_if_invalid,
56 WorkFuncT&& work, Args&&... args) {
57 auto lock = acquireGlobalLock();
58 if (obj->isValid()) {
59 return (obj->*work)(&lock, std::forward<Args>(args)...);
60 } else {
61 return createWifiStatus(status_code_if_invalid);
62 }
63 }
64
65 // Use for AIDL methods which have a return value along with the AIDL status
66 template <typename ObjT, typename WorkFuncT, typename ReturnT, typename... Args>
validateAndCall(ObjT * obj,WifiStatusCode status_code_if_invalid,WorkFuncT && work,ReturnT * ret_val,Args &&...args)67 ::ndk::ScopedAStatus validateAndCall(ObjT* obj, WifiStatusCode status_code_if_invalid,
68 WorkFuncT&& work, ReturnT* ret_val, Args&&... args) {
69 const auto lock = acquireGlobalLock();
70 if (obj->isValid()) {
71 auto call_pair = (obj->*work)(std::forward<Args>(args)...);
72 *ret_val = call_pair.first;
73 return std::forward<::ndk::ScopedAStatus>(call_pair.second);
74 } else {
75 return ndk::ScopedAStatus::fromServiceSpecificError(
76 static_cast<int32_t>(status_code_if_invalid));
77 }
78 }
79
80 } // namespace aidl_return_util
81 } // namespace wifi
82 } // namespace hardware
83 } // namespace android
84 } // namespace aidl
85 #endif // AIDL_RETURN_UTIL_H_
86