blob: 0883de2349a7b40371877267ccbf6a59364c439d [file] [log] [blame]
Badhri Jagan Sridharan9b0a2c62017-05-30 20:26:13 -07001/*
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>
nelsonli822b82d2019-12-11 16:20:02 +080026#include <gtest/gtest.h>
27#include <hidl/GtestPrinter.h>
28#include <hidl/ServiceManagement.h>
Badhri Jagan Sridharan9b0a2c62017-05-30 20:26:13 -070029#include <log/log.h>
30#include <stdlib.h>
31#include <chrono>
32#include <condition_variable>
33#include <mutex>
34
Hsin-Yi Chenb4193b82018-07-04 10:35:01 +080035using ::android::hardware::usb::V1_1::IUsb;
Badhri Jagan Sridharan9b0a2c62017-05-30 20:26:13 -070036using ::android::hardware::usb::V1_1::IUsbCallback;
Badhri Jagan Sridharan9b0a2c62017-05-30 20:26:13 -070037using ::android::hardware::usb::V1_0::PortDataRole;
38using ::android::hardware::usb::V1_0::PortMode;
39using ::android::hardware::usb::V1_1::PortMode_1_1;
40using ::android::hardware::usb::V1_0::PortPowerRole;
41using ::android::hardware::usb::V1_0::PortRole;
42using ::android::hardware::usb::V1_0::PortRoleType;
43using ::android::hardware::usb::V1_0::PortStatus;
44using ::android::hardware::usb::V1_1::PortStatus_1_1;
45using ::android::hardware::usb::V1_0::Status;
46using ::android::hidl::base::V1_0::IBase;
47using ::android::hardware::hidl_array;
48using ::android::hardware::hidl_memory;
49using ::android::hardware::hidl_string;
50using ::android::hardware::hidl_vec;
51using ::android::hardware::Return;
52using ::android::hardware::Void;
53using ::android::sp;
54
55constexpr char kCallbackNameNotifyPortStatusChange_1_1[] = "notifyPortStatusChange_1_1";
56
57// Worst case wait time 20secs
58#define WAIT_FOR_TIMEOUT std::chrono::milliseconds(20000)
59
60class 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.
76class UsbCallback : public ::testing::VtsHalHidlTargetCallbackBase<UsbClientCallbackArgs>,
77 public IUsbCallback {
78 int cookie;
79
80 public:
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
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.
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) {
Haotien Hsu392d5302023-05-08 18:46:14 +080098 arg.usb_last_port_status.status.portName = currentPortStatus[0].status.portName.c_str();
Badhri Jagan Sridharan9b0a2c62017-05-30 20:26:13 -070099 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.
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
nelsonli822b82d2019-12-11 16:20:02 +0800120class UsbHidlTest : public ::testing::TestWithParam<std::string> {
Badhri Jagan Sridharan9b0a2c62017-05-30 20:26:13 -0700121 public:
122 virtual void SetUp() override {
123 ALOGI(__FUNCTION__);
nelsonli822b82d2019-12-11 16:20:02 +0800124 usb = IUsb::getService(GetParam());
Badhri Jagan Sridharan9b0a2c62017-05-30 20:26:13 -0700125 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
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 */
nelsonli822b82d2019-12-11 16:20:02 +0800150TEST_P(UsbHidlTest, setCallback) {
Badhri Jagan Sridharan9b0a2c62017-05-30 20:26:13 -0700151 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 */
nelsonli822b82d2019-12-11 16:20:02 +0800163TEST_P(UsbHidlTest, queryPortStatus) {
Badhri Jagan Sridharan9b0a2c62017-05-30 20:26:13 -0700164 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);
Haotien Hsu392d5302023-05-08 18:46:14 +0800169 // 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 }
Badhri Jagan Sridharan9b0a2c62017-05-30 20:26:13 -0700175}
Dan Shiba4d5322020-07-28 13:09:30 -0700176GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(UsbHidlTest);
nelsonli822b82d2019-12-11 16:20:02 +0800177INSTANTIATE_TEST_SUITE_P(
178 PerInstance, UsbHidlTest,
179 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IUsb::descriptor)),
180 android::hardware::PrintInstanceNameToString);