Badhri Jagan Sridharan | 9b0a2c6 | 2017-05-30 20:26:13 -0700 | [diff] [blame] | 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> |
nelsonli | 822b82d | 2019-12-11 16:20:02 +0800 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | #include <hidl/GtestPrinter.h> |
| 28 | #include <hidl/ServiceManagement.h> |
Badhri Jagan Sridharan | 9b0a2c6 | 2017-05-30 20:26:13 -0700 | [diff] [blame] | 29 | #include <log/log.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <chrono> |
| 32 | #include <condition_variable> |
| 33 | #include <mutex> |
| 34 | |
Hsin-Yi Chen | b4193b8 | 2018-07-04 10:35:01 +0800 | [diff] [blame] | 35 | using ::android::hardware::usb::V1_1::IUsb; |
Badhri Jagan Sridharan | 9b0a2c6 | 2017-05-30 20:26:13 -0700 | [diff] [blame] | 36 | using ::android::hardware::usb::V1_1::IUsbCallback; |
Badhri Jagan Sridharan | 9b0a2c6 | 2017-05-30 20:26:13 -0700 | [diff] [blame] | 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: |
| 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) { |
| 98 | arg.usb_last_port_status.status.supportedModes = |
| 99 | currentPortStatus[0].status.supportedModes; |
| 100 | arg.usb_last_port_status.status.currentMode = currentPortStatus[0].status.currentMode; |
| 101 | } |
| 102 | arg.usb_last_status = retval; |
| 103 | arg.last_usb_cookie = cookie; |
| 104 | |
| 105 | NotifyFromCallback(kCallbackNameNotifyPortStatusChange_1_1, arg); |
| 106 | return Void(); |
| 107 | } |
| 108 | |
| 109 | // Callback method for the status of role switch operation. |
| 110 | // RoleSwitch operation has not changed since V1_0 so leaving |
| 111 | // the callback blank here. |
| 112 | Return<void> notifyRoleSwitchStatus(const hidl_string& /*portName*/, |
| 113 | const PortRole& /*newRole*/, Status /*retval*/) override { |
| 114 | return Void(); |
| 115 | }; |
| 116 | }; |
| 117 | |
| 118 | // The main test class for the USB hidl HAL |
nelsonli | 822b82d | 2019-12-11 16:20:02 +0800 | [diff] [blame] | 119 | class UsbHidlTest : public ::testing::TestWithParam<std::string> { |
Badhri Jagan Sridharan | 9b0a2c6 | 2017-05-30 20:26:13 -0700 | [diff] [blame] | 120 | public: |
| 121 | virtual void SetUp() override { |
| 122 | ALOGI(__FUNCTION__); |
nelsonli | 822b82d | 2019-12-11 16:20:02 +0800 | [diff] [blame] | 123 | usb = IUsb::getService(GetParam()); |
Badhri Jagan Sridharan | 9b0a2c6 | 2017-05-30 20:26:13 -0700 | [diff] [blame] | 124 | ASSERT_NE(usb, nullptr); |
| 125 | |
| 126 | usb_cb_2 = new UsbCallback(2); |
| 127 | ASSERT_NE(usb_cb_2, nullptr); |
| 128 | usb_cb_2->SetWaitTimeout(kCallbackNameNotifyPortStatusChange_1_1, WAIT_FOR_TIMEOUT); |
| 129 | Return<void> ret = usb->setCallback(usb_cb_2); |
| 130 | ASSERT_TRUE(ret.isOk()); |
| 131 | } |
| 132 | |
| 133 | virtual void TearDown() override { ALOGI("Teardown"); } |
| 134 | |
| 135 | // USB hidl hal Proxy |
| 136 | sp<IUsb> usb; |
| 137 | |
| 138 | // Callback objects for usb hidl |
| 139 | // Methods of these objects are called to notify port status updates. |
| 140 | sp<UsbCallback> usb_cb_1; |
| 141 | sp<UsbCallback> usb_cb_2; |
| 142 | }; |
| 143 | |
| 144 | /* |
| 145 | * Test to see if setCallback on V1_1 callback object succeeds. |
| 146 | * Callback oject is created and registered. |
| 147 | * Check to see if the hidl transaction succeeded. |
| 148 | */ |
nelsonli | 822b82d | 2019-12-11 16:20:02 +0800 | [diff] [blame] | 149 | TEST_P(UsbHidlTest, setCallback) { |
Badhri Jagan Sridharan | 9b0a2c6 | 2017-05-30 20:26:13 -0700 | [diff] [blame] | 150 | usb_cb_1 = new UsbCallback(1); |
| 151 | ASSERT_NE(usb_cb_1, nullptr); |
| 152 | Return<void> ret = usb->setCallback(usb_cb_1); |
| 153 | ASSERT_TRUE(ret.isOk()); |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Check to see if querying type-c |
| 158 | * port status succeeds. |
| 159 | * HAL service should call notifyPortStatusChange_1_1 |
| 160 | * instead of notifyPortStatusChange of V1_0 interface |
| 161 | */ |
nelsonli | 822b82d | 2019-12-11 16:20:02 +0800 | [diff] [blame] | 162 | TEST_P(UsbHidlTest, queryPortStatus) { |
Badhri Jagan Sridharan | 9b0a2c6 | 2017-05-30 20:26:13 -0700 | [diff] [blame] | 163 | Return<void> ret = usb->queryPortStatus(); |
| 164 | ASSERT_TRUE(ret.isOk()); |
| 165 | auto res = usb_cb_2->WaitForCallback(kCallbackNameNotifyPortStatusChange_1_1); |
| 166 | EXPECT_TRUE(res.no_timeout); |
| 167 | EXPECT_EQ(2, res.args->last_usb_cookie); |
| 168 | EXPECT_EQ(PortMode::NONE, res.args->usb_last_port_status.status.currentMode); |
| 169 | EXPECT_EQ(PortMode::NONE, res.args->usb_last_port_status.status.supportedModes); |
| 170 | EXPECT_EQ(Status::SUCCESS, res.args->usb_last_status); |
| 171 | } |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame] | 172 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(UsbHidlTest); |
nelsonli | 822b82d | 2019-12-11 16:20:02 +0800 | [diff] [blame] | 173 | INSTANTIATE_TEST_SUITE_P( |
| 174 | PerInstance, UsbHidlTest, |
| 175 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(IUsb::descriptor)), |
| 176 | android::hardware::PrintInstanceNameToString); |