1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "VtsHalUsbV1_0TargetTest"
#include <android-base/logging.h>
#include <android/hardware/usb/1.0/IUsb.h>
#include <android/hardware/usb/1.0/IUsbCallback.h>
#include <android/hardware/usb/1.0/types.h>
#include <gtest/gtest.h>
#include <hidl/GtestPrinter.h>
#include <hidl/ServiceManagement.h>
#include <log/log.h>
#include <stdlib.h>
#include <chrono>
#include <condition_variable>
#include <mutex>
#define TIMEOUT_PERIOD 10
using ::android::hardware::usb::V1_0::IUsbCallback;
using ::android::hardware::usb::V1_0::IUsb;
using ::android::hardware::usb::V1_0::PortDataRole;
using ::android::hardware::usb::V1_0::PortMode;
using ::android::hardware::usb::V1_0::PortPowerRole;
using ::android::hardware::usb::V1_0::PortRole;
using ::android::hardware::usb::V1_0::PortRoleType;
using ::android::hardware::usb::V1_0::PortStatus;
using ::android::hardware::usb::V1_0::Status;
using ::android::hidl::base::V1_0::IBase;
using ::android::hardware::hidl_array;
using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;
// The main test class for the USB hidl HAL
class UsbHidlTest : public testing::TestWithParam<std::string> {
public:
// Callback class for the USB HIDL hal.
// Usb Hal will call this object upon role switch or port query.
class UsbCallback : public IUsbCallback {
UsbHidlTest& parent_;
int cookie;
public:
UsbCallback(UsbHidlTest& parent, int cookie)
: parent_(parent), cookie(cookie){};
virtual ~UsbCallback() = default;
// Callback method for the port status.
Return<void> notifyPortStatusChange(
const hidl_vec<PortStatus>& currentPortStatus, Status retval) override {
if (retval == Status::SUCCESS) {
parent_.usb_last_port_status.portName =
currentPortStatus[0].portName.c_str();
parent_.usb_last_port_status.currentDataRole =
currentPortStatus[0].currentDataRole;
parent_.usb_last_port_status.currentPowerRole =
currentPortStatus[0].currentPowerRole;
parent_.usb_last_port_status.currentMode =
currentPortStatus[0].currentMode;
}
parent_.usb_last_cookie = cookie;
parent_.notify();
return Void();
};
// Callback method for the status of role switch operation.
Return<void> notifyRoleSwitchStatus(const hidl_string& /*portName*/,
const PortRole& newRole,
Status retval) override {
parent_.usb_last_status = retval;
parent_.usb_last_cookie = cookie;
parent_.usb_last_port_role = newRole;
parent_.usb_role_switch_done = true;
parent_.notify();
return Void();
};
};
virtual void SetUp() override {
ALOGI("Setup");
usb = IUsb::getService(GetParam());
ASSERT_NE(usb, nullptr);
usb_cb_2 = new UsbCallback(*this, 2);
ASSERT_NE(usb_cb_2, nullptr);
Return<void> ret = usb->setCallback(usb_cb_2);
ASSERT_TRUE(ret.isOk());
}
virtual void TearDown() override { ALOGI("Teardown"); }
// Used as a mechanism to inform the test about data/event callback.
inline void notify() {
std::unique_lock<std::mutex> lock(usb_mtx);
usb_count++;
usb_cv.notify_one();
}
// Test code calls this function to wait for data/event callback.
inline std::cv_status wait() {
std::unique_lock<std::mutex> lock(usb_mtx);
std::cv_status status = std::cv_status::no_timeout;
auto now = std::chrono::system_clock::now();
while (usb_count == 0) {
status =
usb_cv.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
if (status == std::cv_status::timeout) {
ALOGI("timeout");
return status;
}
}
usb_count--;
return status;
}
// USB hidl hal Proxy
sp<IUsb> usb;
// Callback objects for usb hidl
// Methods of these objects are called to notify port status updates.
sp<IUsbCallback> usb_cb_1, usb_cb_2;
// The last conveyed status of the USB ports.
// Stores information of currentt_data_role, power_role for all the USB ports
PortStatus usb_last_port_status;
// Status of the last role switch operation.
Status usb_last_status;
// Port role information of the last role switch operation.
PortRole usb_last_port_role;
// Flag to indicate the invocation of role switch callback.
bool usb_role_switch_done;
// Identifier for the usb callback object.
// Stores the cookie of the last invoked usb callback object.
int usb_last_cookie;
// synchronization primitives to coordinate between main test thread
// and the callback thread.
std::mutex usb_mtx;
std::condition_variable usb_cv;
int usb_count = 0;
};
/*
* Test to see if setCallback succeeds.
* Callback oject is created and registered.
* Check to see if the hidl transaction succeeded.
*/
TEST_P(UsbHidlTest, setCallback) {
usb_cb_1 = new UsbCallback(*this, 1);
ASSERT_NE(usb_cb_1, nullptr);
Return<void> ret = usb->setCallback(usb_cb_1);
ASSERT_TRUE(ret.isOk());
}
/*
* Check to see if querying type-c
* port status succeeds.
*/
TEST_P(UsbHidlTest, queryPortStatus) {
Return<void> ret = usb->queryPortStatus();
ASSERT_TRUE(ret.isOk());
EXPECT_EQ(std::cv_status::no_timeout, wait());
EXPECT_EQ(2, usb_last_cookie);
ALOGI("rightafter: %s", usb_last_port_status.portName.c_str());
}
/*
* Trying to switch a non-existent port should fail.
* This test case tried to switch the port with empty
* name which is expected to fail.
*/
TEST_P(UsbHidlTest, switchEmptyPort) {
struct PortRole role;
role.type = PortRoleType::DATA_ROLE;
Return<void> ret = usb->switchRole("", role);
ASSERT_TRUE(ret.isOk());
EXPECT_EQ(std::cv_status::no_timeout, wait());
EXPECT_EQ(Status::ERROR, usb_last_status);
EXPECT_EQ(2, usb_last_cookie);
}
/*
* Test switching the power role of usb port.
* Test case queries the usb ports present in device.
* If there is atleast one usb port, a power role switch
* to SOURCE is attempted for the port.
* The callback parametes are checked to see if the power role
* switch was successfull. Upon success, Status::SUCCESS
* is expected to be returned.
*/
TEST_P(UsbHidlTest, switchPowerRole) {
struct PortRole role;
role.type = PortRoleType::POWER_ROLE;
role.role = static_cast<uint32_t>(PortPowerRole::SOURCE);
Return<void> ret = usb->queryPortStatus();
ASSERT_TRUE(ret.isOk());
EXPECT_EQ(std::cv_status::no_timeout, wait());
EXPECT_EQ(2, usb_last_cookie);
if (!usb_last_port_status.portName.empty()) {
hidl_string portBeingSwitched = usb_last_port_status.portName;
ALOGI("switchPower role portname:%s", portBeingSwitched.c_str());
usb_role_switch_done = false;
Return<void> ret = usb->switchRole(portBeingSwitched.c_str(), role);
ASSERT_TRUE(ret.isOk());
std::cv_status waitStatus = wait();
while (waitStatus == std::cv_status::no_timeout &&
usb_role_switch_done == false)
waitStatus = wait();
EXPECT_EQ(std::cv_status::no_timeout, waitStatus);
EXPECT_EQ(2, usb_last_cookie);
EXPECT_EQ(static_cast<uint32_t>(PortRoleType::POWER_ROLE),
static_cast<uint32_t>(usb_last_port_role.type));
if (usb_last_status == Status::SUCCESS) {
EXPECT_EQ(static_cast<uint32_t>(PortPowerRole::SOURCE),
static_cast<uint32_t>(usb_last_port_role.role));
} else {
EXPECT_NE(static_cast<uint32_t>(PortPowerRole::SINK),
static_cast<uint32_t>(usb_last_port_role.role));
}
}
}
/*
* Test switching the data role of usb port.
* Test case queries the usb ports present in device.
* If there is atleast one usb port, a power role switch
* to HOST is attempted for the port.
* The callback parametes are checked to see if the power role
* switch was successfull. Upon success, Status::SUCCESS
* is expected to be returned.
*/
TEST_P(UsbHidlTest, switchDataRole) {
struct PortRole role;
role.type = PortRoleType::DATA_ROLE;
role.role = static_cast<uint32_t>(PortDataRole::HOST);
Return<void> ret = usb->queryPortStatus();
ASSERT_TRUE(ret.isOk());
EXPECT_EQ(std::cv_status::no_timeout, wait());
EXPECT_EQ(2, usb_last_cookie);
if (!usb_last_port_status.portName.empty()) {
hidl_string portBeingSwitched = usb_last_port_status.portName;
ALOGI("portname:%s", portBeingSwitched.c_str());
usb_role_switch_done = false;
Return<void> ret = usb->switchRole(portBeingSwitched.c_str(), role);
ASSERT_TRUE(ret.isOk());
std::cv_status waitStatus = wait();
while (waitStatus == std::cv_status::no_timeout &&
usb_role_switch_done == false)
waitStatus = wait();
EXPECT_EQ(std::cv_status::no_timeout, waitStatus);
EXPECT_EQ(2, usb_last_cookie);
EXPECT_EQ(static_cast<uint32_t>(PortRoleType::DATA_ROLE),
static_cast<uint32_t>(usb_last_port_role.type));
if (usb_last_status == Status::SUCCESS) {
EXPECT_EQ(static_cast<uint32_t>(PortDataRole::HOST),
static_cast<uint32_t>(usb_last_port_role.role));
} else {
EXPECT_NE(static_cast<uint32_t>(PortDataRole::DEVICE),
static_cast<uint32_t>(usb_last_port_role.role));
}
}
}
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(UsbHidlTest);
INSTANTIATE_TEST_SUITE_P(
PerInstance, UsbHidlTest,
testing::ValuesIn(android::hardware::getAllHalInstanceNames(IUsb::descriptor)),
android::hardware::PrintInstanceNameToString);
|