1 /*
2 * WPA Supplicant - Validate interfaces before calling methods
3 * Copyright (c) 2021, Google Inc. All rights reserved.
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #ifndef AIDL_RETURN_UTIL_H_
10 #define AIDL_RETURN_UTIL_H_
11
12 #include <aidl/android/hardware/wifi/supplicant/SupplicantStatusCode.h>
13
14 namespace aidl {
15 namespace android {
16 namespace hardware {
17 namespace wifi {
18 namespace supplicant {
19 namespace aidl_return_util {
20
21 /**
22 * These utility functions are used to invoke a method on the provided
23 * AIDL interface object.
24 * These functions check if the provided AIDL interface object is valid.
25 * a) If valid, invokes the corresponding internal implementation function of
26 * the AIDL method.
27 * b) If invalid, return without calling the internal implementation function.
28 */
29
30 // Use for AIDL methods which only return an AIDL status
31 template <typename ObjT, typename WorkFuncT, typename... Args>
validateAndCall(ObjT * obj,SupplicantStatusCode status_code_if_invalid,WorkFuncT && work,Args &&...args)32 ::ndk::ScopedAStatus validateAndCall(
33 ObjT* obj, SupplicantStatusCode status_code_if_invalid, WorkFuncT&& work,
34 Args&&... args)
35 {
36 if (obj->isValid()) {
37 return (obj->*work)(std::forward<Args>(args)...);
38 } else {
39 return ndk::ScopedAStatus::fromServiceSpecificError(
40 static_cast<int32_t>(status_code_if_invalid));
41 }
42 }
43
44 // Use for AIDL methods which have a return value along with the AIDL status
45 template <typename ObjT, typename WorkFuncT, typename ReturnT, typename... Args>
validateAndCall(ObjT * obj,SupplicantStatusCode status_code_if_invalid,WorkFuncT && work,ReturnT * ret_val,Args &&...args)46 ::ndk::ScopedAStatus validateAndCall(
47 ObjT* obj, SupplicantStatusCode status_code_if_invalid, WorkFuncT&& work,
48 ReturnT* ret_val, Args&&... args)
49 {
50 if (obj->isValid()) {
51 auto call_pair = (obj->*work)(std::forward<Args>(args)...);
52 *ret_val = call_pair.first;
53 return std::forward<::ndk::ScopedAStatus>(call_pair.second);
54 } else {
55 return ndk::ScopedAStatus::fromServiceSpecificError(
56 static_cast<int32_t>(status_code_if_invalid));
57 }
58 }
59
60 } // namespace aidl_return_util
61 } // namespace supplicant
62 } // namespace wifi
63 } // namespace hardware
64 } // namespace android
65 } // namespace aidl
66 #endif // AIDL_RETURN_UTIL_H_
67