1 /*
2  * Copyright (C) 2017 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 #define LOG_TAG "VtsHalUsbV1_0TargetTest"
18 #include <android-base/logging.h>
19 
20 #include <android/hardware/usb/1.0/types.h>
21 #include <android/hardware/usb/1.1/IUsb.h>
22 #include <android/hardware/usb/1.1/IUsbCallback.h>
23 #include <android/hardware/usb/1.1/types.h>
24 
25 #include <VtsHalHidlTargetCallbackBase.h>
26 #include <gtest/gtest.h>
27 #include <hidl/GtestPrinter.h>
28 #include <hidl/ServiceManagement.h>
29 #include <log/log.h>
30 #include <stdlib.h>
31 #include <chrono>
32 #include <condition_variable>
33 #include <mutex>
34 
35 using ::android::hardware::usb::V1_1::IUsb;
36 using ::android::hardware::usb::V1_1::IUsbCallback;
37 using ::android::hardware::usb::V1_0::PortDataRole;
38 using ::android::hardware::usb::V1_0::PortMode;
39 using ::android::hardware::usb::V1_1::PortMode_1_1;
40 using ::android::hardware::usb::V1_0::PortPowerRole;
41 using ::android::hardware::usb::V1_0::PortRole;
42 using ::android::hardware::usb::V1_0::PortRoleType;
43 using ::android::hardware::usb::V1_0::PortStatus;
44 using ::android::hardware::usb::V1_1::PortStatus_1_1;
45 using ::android::hardware::usb::V1_0::Status;
46 using ::android::hidl::base::V1_0::IBase;
47 using ::android::hardware::hidl_array;
48 using ::android::hardware::hidl_memory;
49 using ::android::hardware::hidl_string;
50 using ::android::hardware::hidl_vec;
51 using ::android::hardware::Return;
52 using ::android::hardware::Void;
53 using ::android::sp;
54 
55 constexpr char kCallbackNameNotifyPortStatusChange_1_1[] = "notifyPortStatusChange_1_1";
56 
57 // Worst case wait time 20secs
58 #define WAIT_FOR_TIMEOUT std::chrono::milliseconds(20000)
59 
60 class UsbClientCallbackArgs {
61    public:
62     // The last conveyed status of the USB ports.
63     // Stores information of currentt_data_role, power_role for all the USB ports
64     PortStatus_1_1 usb_last_port_status;
65 
66     // Status of the last role switch operation.
67     Status usb_last_status;
68 
69     // Identifier for the usb callback object.
70     // Stores the cookie of the last invoked usb callback object.
71     int last_usb_cookie;
72 };
73 
74 // Callback class for the USB HIDL hal.
75 // Usb Hal will call this object upon role switch or port query.
76 class UsbCallback : public ::testing::VtsHalHidlTargetCallbackBase<UsbClientCallbackArgs>,
77                     public IUsbCallback {
78     int cookie;
79 
80    public:
UsbCallback(int cookie)81     UsbCallback(int cookie) : cookie(cookie){};
82 
83     virtual ~UsbCallback() = default;
84 
85     // V1_0 Callback method for the port status.
86     // This should not be called so not signalling the Test here assuming that
87     // the test thread will timeout
notifyPortStatusChange(const hidl_vec<PortStatus> &,Status)88     Return<void> notifyPortStatusChange(const hidl_vec<PortStatus>& /* currentPortStatus */,
89                                         Status /*retval*/) override {
90         return Void();
91     };
92 
93     // This callback methode should be used.
notifyPortStatusChange_1_1(const hidl_vec<PortStatus_1_1> & currentPortStatus,Status retval)94     Return<void> notifyPortStatusChange_1_1(const hidl_vec<PortStatus_1_1>& currentPortStatus,
95                                             Status retval) override {
96         UsbClientCallbackArgs arg;
97         if (retval == Status::SUCCESS) {
98             arg.usb_last_port_status.status.portName = currentPortStatus[0].status.portName.c_str();
99             arg.usb_last_port_status.status.supportedModes =
100                 currentPortStatus[0].status.supportedModes;
101             arg.usb_last_port_status.status.currentMode = currentPortStatus[0].status.currentMode;
102         }
103         arg.usb_last_status = retval;
104         arg.last_usb_cookie = cookie;
105 
106         NotifyFromCallback(kCallbackNameNotifyPortStatusChange_1_1, arg);
107         return Void();
108     }
109 
110     // Callback method for the status of role switch operation.
111     // RoleSwitch operation has not changed since V1_0 so leaving
112     // the callback blank here.
notifyRoleSwitchStatus(const hidl_string &,const PortRole &,Status)113     Return<void> notifyRoleSwitchStatus(const hidl_string& /*portName*/,
114                                         const PortRole& /*newRole*/, Status /*retval*/) override {
115         return Void();
116     };
117 };
118 
119 // The main test class for the USB hidl HAL
120 class UsbHidlTest : public ::testing::TestWithParam<std::string> {
121    public:
SetUp()122     virtual void SetUp() override {
123         ALOGI(__FUNCTION__);
124         usb = IUsb::getService(GetParam());
125         ASSERT_NE(usb, nullptr);
126 
127         usb_cb_2 = new UsbCallback(2);
128         ASSERT_NE(usb_cb_2, nullptr);
129         usb_cb_2->SetWaitTimeout(kCallbackNameNotifyPortStatusChange_1_1, WAIT_FOR_TIMEOUT);
130         Return<void> ret = usb->setCallback(usb_cb_2);
131         ASSERT_TRUE(ret.isOk());
132     }
133 
TearDown()134     virtual void TearDown() override { ALOGI("Teardown"); }
135 
136     // USB hidl hal Proxy
137     sp<IUsb> usb;
138 
139     // Callback objects for usb hidl
140     // Methods of these objects are called to notify port status updates.
141     sp<UsbCallback> usb_cb_1;
142     sp<UsbCallback> usb_cb_2;
143 };
144 
145 /*
146  * Test to see if setCallback on V1_1 callback object succeeds.
147  * Callback oject is created and registered.
148  * Check to see if the hidl transaction succeeded.
149  */
TEST_P(UsbHidlTest,setCallback)150 TEST_P(UsbHidlTest, setCallback) {
151     usb_cb_1 = new UsbCallback(1);
152     ASSERT_NE(usb_cb_1, nullptr);
153     Return<void> ret = usb->setCallback(usb_cb_1);
154     ASSERT_TRUE(ret.isOk());
155 }
156 
157 /*
158  * Check to see if querying type-c
159  * port status succeeds.
160  * HAL service should call notifyPortStatusChange_1_1
161  * instead of notifyPortStatusChange of V1_0 interface
162  */
TEST_P(UsbHidlTest,queryPortStatus)163 TEST_P(UsbHidlTest, queryPortStatus) {
164     Return<void> ret = usb->queryPortStatus();
165     ASSERT_TRUE(ret.isOk());
166     auto res = usb_cb_2->WaitForCallback(kCallbackNameNotifyPortStatusChange_1_1);
167     EXPECT_TRUE(res.no_timeout);
168     EXPECT_EQ(2, res.args->last_usb_cookie);
169     // if there are no type-c ports, skip below checks
170     if (!res.args->usb_last_port_status.status.portName.empty()) {
171         EXPECT_EQ(PortMode::NONE, res.args->usb_last_port_status.status.currentMode);
172         EXPECT_EQ(PortMode::NONE, res.args->usb_last_port_status.status.supportedModes);
173         EXPECT_EQ(Status::SUCCESS, res.args->usb_last_status);
174     }
175 }
176 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(UsbHidlTest);
177 INSTANTIATE_TEST_SUITE_P(
178         PerInstance, UsbHidlTest,
179         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IUsb::descriptor)),
180         android::hardware::PrintInstanceNameToString);
181