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 #pragma once
18 
19 #include <OffloadControlTestV1_0.h>
20 #include <android/hardware/tetheroffload/control/1.1/IOffloadControl.h>
21 #include <android/hardware/tetheroffload/control/1.1/ITetheringOffloadCallback.h>
22 #include <gtest/gtest.h>
23 
24 constexpr char kCallbackOnEvent_1_1[] = "onEvent_1_1";
25 
26 class TetheringOffloadCallbackArgsV1_1 {
27   public:
28     android::hardware::tetheroffload::control::V1_1::OffloadCallbackEvent last_event;
29 };
30 
31 class OffloadControlTestV1_1_HalNotStarted : public OffloadControlTestV1_0_HalNotStarted {
32   public:
createControl(const std::string & serviceName)33     virtual sp<android::hardware::tetheroffload::control::V1_0::IOffloadControl> createControl(
34             const std::string& serviceName) override {
35         return android::hardware::tetheroffload::control::V1_1::IOffloadControl::getService(
36                 serviceName);
37     };
38 
prepareControlHal()39     void prepareControlHal() override {
40         control = createControl(std::get<1>(GetParam()));
41         ASSERT_NE(nullptr, control.get()) << "Could not get HIDL instance";
42 
43         control_cb_1_1 = new TetheringOffloadCallbackV1_1();
44         ASSERT_NE(nullptr, control_cb_1_1.get()) << "Could not get offload callback";
45     };
46 
initOffload(const bool expected_result)47     void initOffload(const bool expected_result) override {
48         auto init_cb = [&](bool success, std::string errMsg) {
49             std::string msg = StringPrintf("Unexpectedly %s to init offload: %s",
50                                            success ? "succeeded" : "failed", errMsg.c_str());
51             ASSERT_EQ(expected_result, success) << msg;
52         };
53         auto control = getControlV1_1();
54         ASSERT_NE(control, nullptr);
55         const Return<void> ret = control->initOffload(control_cb_1_1, init_cb);
56         ASSERT_TRUE(ret.isOk());
57     };
58 
getControlV1_1()59     sp<android::hardware::tetheroffload::control::V1_1::IOffloadControl> getControlV1_1() {
60         // The cast is safe since only devices with V1.1+ HAL will be enumerated and pass in to the
61         // test.
62         return android::hardware::tetheroffload::control::V1_1::IOffloadControl::castFrom(control)
63                 .withDefault(nullptr);
64     };
65 
66     // Callback class for both new events.
67     class TetheringOffloadCallbackV1_1
68         : public testing::VtsHalHidlTargetCallbackBase<TetheringOffloadCallbackArgsV1_1>,
69           public android::hardware::tetheroffload::control::V1_1::ITetheringOffloadCallback {
70       public:
onEvent_1_1(android::hardware::tetheroffload::control::V1_1::OffloadCallbackEvent event)71         Return<void> onEvent_1_1(
72                 android::hardware::tetheroffload::control::V1_1::OffloadCallbackEvent event)
73                 override {
74             const TetheringOffloadCallbackArgsV1_1 args{.last_event = event};
75             NotifyFromCallback(kCallbackOnEvent_1_1, args);
76             return Void();
77         };
78 
onEvent(OffloadCallbackEvent event)79         Return<void> onEvent([[maybe_unused]] OffloadCallbackEvent event) override {
80             // Tested only in IOffloadControl 1.0.
81             return Void();
82         };
83 
updateTimeout(const NatTimeoutUpdate & params)84         Return<void> updateTimeout([[maybe_unused]] const NatTimeoutUpdate& params) override {
85             // Tested only in IOffloadControl 1.0.
86             return Void();
87         };
88     };
89 
90     sp<TetheringOffloadCallbackV1_1> control_cb_1_1;
91 };
92 
93 class OffloadControlTestV1_1_HalStarted : public OffloadControlTestV1_1_HalNotStarted {
94   public:
SetUp()95     virtual void SetUp() override {
96         setupConfigHal();
97         setupControlHal();
98     }
99 };