1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "usbd_hub_device_test.h"
17
18 #include <iostream>
19 #include <vector>
20
21 #include "hdf_log.h"
22
23 #include "v2_0/iusb_host_interface.h"
24 #include "v2_0/usb_types.h"
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace std;
28
29 using namespace OHOS::HDI::Usb::V2_0;
30
31 UsbDev UsbdHubDeviceTest::dev_ = {0, 0};
32 UsbDev UsbdHubDeviceTest::hubDev_ = {0, 0};
33 sptr<UsbSubscriberTest> UsbdHubDeviceTest::subscriber_ = nullptr;
34 const std::string SERVICE_NAME = "usb_host_interface_service";
35 namespace {
36 sptr<OHOS::HDI::Usb::V2_0::IUsbHostInterface> g_usbInterface = nullptr;
37
SetUpTestCase(void)38 void UsbdHubDeviceTest::SetUpTestCase(void)
39 {
40 g_usbInterface = OHOS::HDI::Usb::V2_0::IUsbHostInterface::Get(SERVICE_NAME, true);
41 if (g_usbInterface == nullptr) {
42 HDF_LOGE("%{public}s:IUsbInterface::Get() failed.", __func__);
43 exit(0);
44 }
45
46 subscriber_ = new UsbSubscriberTest();
47 if (g_usbInterface->BindUsbdHostSubscriber(subscriber_) != HDF_SUCCESS) {
48 HDF_LOGE("%{public}s: bind usbd subscriber_ failed", __func__);
49 exit(0);
50 }
51 for (size_t i = 0; i < subscriber_->busInfos.size(); i++) {
52 struct UsbDev dev = {subscriber_->busInfos[i].busNum, subscriber_->busInfos[i].devAddr};
53 g_usbInterface->OpenDevice(dev);
54 std::vector<uint8_t> rawData;
55 auto ret = g_usbInterface->GetRawDescriptor(dev, rawData);
56 ASSERT_EQ(0, ret);
57 uint32_t deviceDescriptorSize = sizeof(UsbdDeviceDescriptor);
58 if (rawData.size() < deviceDescriptorSize) {
59 HDF_LOGE("%{public}s: rawData failed", __func__);
60 exit(0);
61 return ;
62 }
63
64 UsbdDeviceDescriptor deviceDescriptor = *(reinterpret_cast<const UsbdDeviceDescriptor *>(rawData.data()));
65 if (deviceDescriptor.bLength != deviceDescriptorSize) {
66 HDF_LOGE("UsbdDeviceDescriptor size error");
67 exit(0);
68 }
69 if (deviceDescriptor.bDeviceClass != 9) {
70 dev_ = dev;
71 } else {
72 hubDev_ = dev;
73 }
74 }
75
76 std::cout << "please connect hub device, press enter to continue" << std::endl;
77 int c;
78 while ((c = getchar()) != '\n' && c != EOF) {}
79 }
80
TearDownTestCase(void)81 void UsbdHubDeviceTest::TearDownTestCase(void)
82 {
83 g_usbInterface->UnbindUsbdHostSubscriber(subscriber_);
84 }
85
SetUp(void)86 void UsbdHubDeviceTest::SetUp(void) {}
87
TearDown(void)88 void UsbdHubDeviceTest::TearDown(void) {}
89
90 /**
91 * @tc.name: hub device
92 * @tc.desc: Test functions to hub device hot plug
93 * @tc.desc: int32_t;
94 * @tc.desc: 正向测试:参数正确
95 * @tc.type: FUNC
96 */
97 HWTEST_F(UsbdHubDeviceTest, UsbdHubDevicehotPlug001, TestSize.Level1)
98 {
99 int32_t ret = 1;
100 if (hubDev_.busNum != 0 && hubDev_.devAddr != 0) {
101 ret = 0;
102 }
103 HDF_LOGI("UsbdHubDeviceTest:: Line:%{public}d hub device busNum =%{public}d", __LINE__, hubDev_.busNum);
104 EXPECT_EQ(0, ret);
105 }
106
107 } // namespace
108