• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "usb_submit_transfer_interrupt_test.h"
17 
18 #include <csignal>
19 #include <iostream>
20 #include <vector>
21 
22 #include "if_system_ability_manager.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "iusb_srv.h"
26 #include "system_ability_definition.h"
27 #include "usb_common.h"
28 #include "usb_common_test.h"
29 #include "usb_device_pipe.h"
30 #include "usb_request.h"
31 #include "usb_srv_client.h"
32 #include "v1_2/usb_types.h"
33 #include "usb_errors.h"
34 #include "ashmem.h"
35 
36 using namespace testing::ext;
37 using namespace OHOS::USB;
38 using namespace OHOS;
39 using namespace std;
40 using namespace OHOS::USB::Common;
41 
42 namespace OHOS {
43 namespace USB {
44 namespace SubmitTransfer {
45 constexpr int32_t SLEEP_TIME = 3;
46 
SetUpTestCase(void)47 void UsbSubmitTransferInterruptTest::SetUpTestCase(void)
48 {
49     UsbCommonTest::GrantPermissionSysNative();
50     auto &srvClient = UsbSrvClient::GetInstance();
51     auto ret = srvClient.SetPortRole(1, 1, 1);
52     sleep(SLEEP_TIME);
53     USB_HILOGI(MODULE_USB_SERVICE, "UsbSubmitTransferInterruptTest:: [Device] SetPortRole:%{public}d", ret);
54     ret = UsbCommonTest::SwitchErrCode(ret);
55     ASSERT_TRUE(ret == 0);
56     if (ret != 0) {
57         exit(0);
58     }
59     std::cout << "please connect device, press enter to continue" << std::endl;
60     int c;
61     do {
62         c = getchar();
63     } while (c != '\n' && c != EOF);
64     USB_HILOGI(MODULE_USB_SERVICE, "Start UsbSubmitTransferInterruptTest");
65 }
66 
TearDownTestCase(void)67 void UsbSubmitTransferInterruptTest::TearDownTestCase(void)
68 {
69     USB_HILOGI(MODULE_USB_SERVICE, "End UsbSubmitTransferInterruptTest");
70 }
71 
SetUp(void)72 void UsbSubmitTransferInterruptTest::SetUp(void) {}
73 
TearDown(void)74 void UsbSubmitTransferInterruptTest::TearDown(void) {}
75 
76 /**
77  * @tc.name: UsbSubmitTransferInterruptWrite
78  * @tc.desc: Test the USB data write functionality of UsbSubmitTransfer OK.
79  * @tc.type: FUNC
80  */
81 HWTEST_F(UsbSubmitTransferInterruptTest, UsbSubmitTransferInterruptWrite, TestSize.Level1)
82 {
83     USB_HILOGI(MODULE_USB_SERVICE, "UsbSubmitTransferInterruptWrite enter.");
84     vector<UsbDevice> delist;
85     auto &UsbSrvClient = UsbSrvClient::GetInstance();
86     auto ret = UsbSrvClient.GetDevices(delist);
87     USB_HILOGI(MODULE_USB_SERVICE, "UsbSubmitTransferInterruptWrite %{public}d ret=%{public}d", __LINE__, ret);
88     EXPECT_TRUE(!(delist.empty())) << "delist NULL";
89     USB_HILOGI(MODULE_USB_SERVICE, "UsbSubmitTransferInterruptWrite %{public}d size=%{public}zu", __LINE__,
90                delist.size());
91     USBDevicePipe pipe;
92     UsbDevice device;
93     bool hasDevice = false;
94     for (int32_t i = 0; i < delist.size(); i++) {
95         if (delist[i].GetClass() != 9) {
96             device = delist[i];
97             hasDevice = true;
98         }
99     }
100     EXPECT_TRUE(hasDevice);
101     UsbSrvClient.RequestRight(device.GetName());
102     ret = UsbSrvClient.OpenDevice(device, pipe);
103     UsbInterface interface = device.GetConfigs().front().GetInterfaces().at(0);
104     USBEndpoint point = interface.GetEndpoints().front();
105     USB_HILOGI(MODULE_USB_SERVICE, "UsbSubmitTransferInterruptWrite %{public}d point=%{public}d", __LINE__,
106                point.GetInterfaceId());
107     ret = UsbSrvClient.ClaimInterface(pipe, interface, true);
108     sptr<Ashmem> ashmem = Ashmem::CreateAshmem("usb_shared_memory", TEN);
109     ASSERT_NE(ashmem, nullptr);
110     const uint8_t dataToWrite[TEN] = {0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
111     ashmem->MapReadAndWriteAshmem();
112     bool writeSuccess = ashmem->WriteToAshmem(dataToWrite, sizeof(dataToWrite), 0);
113     ASSERT_TRUE(writeSuccess);
114     HDI::Usb::V1_2::USBTransferInfo transferInfo;
115     transferInfo.endpoint = 0x01;    // 0x01写 0x81读,中断写不支持,只能读
116     transferInfo.flags = 0;
117     transferInfo.type = TYPE_INTERRUPT; // 开发板仅支持bulk
118     transferInfo.timeOut = 2000;
119     transferInfo.length = TEN;        // 期望长度
120     transferInfo.userData = 0;
121     transferInfo.numIsoPackets = 0;  // iso传输包数量 iso单包传输最大长度192
122     auto callback = [](const TransferCallbackInfo &info,
__anon046e99190102(const TransferCallbackInfo &info, const std::vector<HDI::Usb::V1_2::UsbIsoPacketDescriptor> &packets, uint64_t userData) 123                         const std::vector<HDI::Usb::V1_2::UsbIsoPacketDescriptor> &packets, uint64_t userData) {
124         USB_HILOGI(MODULE_USB_SERVICE, "UsbSubmitTransferInterruptWrite cb status:%{public}d,actualLength:%{public}d",
125             info.status, info.actualLength);
126     };
127     ret = UsbSrvClient.UsbSubmitTransfer(pipe, transferInfo, callback, ashmem);
128     USB_HILOGI(MODULE_USB_SERVICE, "%{public}d line. UsbSubmitTransferInterruptWrite ret:%{public}d", __LINE__, ret);
129     ASSERT_EQ(ret, USB_SUBMIT_TRANSFER_IO_ERROR);
130     bool close = UsbSrvClient.Close(pipe);
131     USB_HILOGI(MODULE_USB_SERVICE, "UsbSubmitTransferInterruptWrite %{public}d close=%{public}d", __LINE__, close);
132     EXPECT_TRUE(close);
133     USB_HILOGI(MODULE_USB_SERVICE, "UsbSubmitTransferInterruptWrite end.");
134 }
135 
136 /**
137  * @tc.name: UsbSubmitTransferInterruptReadInvalidEndpoint
138  * @tc.desc: Test the USB data write functionality of UsbSubmitTransfer with invalid endpoint type
139  * @tc.type: FUNC
140  */
141 HWTEST_F(UsbSubmitTransferInterruptTest, UsbSubmitTransferInterruptReadInvalidEndpoint, TestSize.Level1)
142 {
143     USB_HILOGI(MODULE_USB_SERVICE, "UsbSubmitTransferInterruptReadInvalidEndpoint begin.");
144 
145     vector<UsbDevice> delist;
146     auto &UsbSrvClient = UsbSrvClient::GetInstance();
147     auto ret = UsbSrvClient.GetDevices(delist);
148     EXPECT_TRUE(!(delist.empty())) << "Device list is empty";
149     UsbDevice device;
150     bool hasDevice = false;
151     for (int32_t i = 0; i < delist.size(); i++) {
152         if (delist[i].GetClass() != 9) {
153             device = delist[i];
154             hasDevice = true;
155         }
156     }
157     EXPECT_TRUE(hasDevice);
158     ret = UsbSrvClient.RequestRight(device.GetName());
159     USBDevicePipe pip;
160     ret = UsbSrvClient.OpenDevice(device, pip);
161     UsbInterface interface = device.GetConfigs().front().GetInterfaces().at(0);
162     USBEndpoint point = interface.GetEndpoints().front();
163     ret = UsbSrvClient.ClaimInterface(pip, interface, true);
164     USB_HILOGI(MODULE_USB_SERVICE, "%{public}d line. UsbSubmitTransferInterruptReadInvalidEndpoint ret:%{public}d",
165         __LINE__, ret);
166 
167     sptr<Ashmem> ashmem = Ashmem::CreateAshmem("usb_shared_memory", TEN);
168     ASSERT_NE(ashmem, nullptr);
169     const uint8_t dataToWrite[TEN] = {0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
170     ashmem->MapReadAndWriteAshmem();
171     bool writeSuccess = ashmem->WriteToAshmem(dataToWrite, sizeof(dataToWrite), 0);
172     ASSERT_TRUE(writeSuccess);
173     HDI::Usb::V1_2::USBTransferInfo transferInfo;
174     transferInfo.endpoint = 0xFF;    //无效参数
175     transferInfo.flags = 0;
176     transferInfo.type = TYPE_INTERRUPT; // 开发板仅支持bulk
177     transferInfo.timeOut = 2000;
178     transferInfo.length = TEN;        // 期望长度
179     transferInfo.userData = 0;
180     transferInfo.numIsoPackets = 0;  // iso传输包数量 iso单包传输最大长度192
181 
182     auto callback = [](const TransferCallbackInfo &info,
__anon046e99190202(const TransferCallbackInfo &info, const std::vector<HDI::Usb::V1_2::UsbIsoPacketDescriptor> &packets, uint64_t userData) 183                         const std::vector<HDI::Usb::V1_2::UsbIsoPacketDescriptor> &packets, uint64_t userData) {
184         USB_HILOGI(MODULE_USB_SERVICE,
185             "UsbSubmitTransferInterruptReadInvalidEndpoint cb status: %{public}d, actualLength: %{public}d",
186             info.status, info.actualLength);
187     };
188     ret = UsbSrvClient.UsbSubmitTransfer(pip, transferInfo, callback, ashmem);
189     USB_HILOGI(MODULE_USB_SERVICE, "%{public}d line. UsbSubmitTransferInterruptReadInvalidEndpoint ret=%{public}d",
190         __LINE__, ret);
191     ASSERT_EQ(ret, USB_SUBMIT_TRANSFER_IO_ERROR);
192     bool close = UsbSrvClient.Close(pip);
193     EXPECT_TRUE(close);
194     USB_HILOGI(MODULE_USB_SERVICE, "UsbSubmitTransferInterruptReadInvalidEndpoint end.");
195 }
196 
197 /**
198  * @tc.name:  UsbSubmitTransferInterruptWriteIoError
199  * @tc.desc: Test USB I/O error when interface is not claimed.
200  * @tc.type: FUNC
201  */
202 HWTEST_F(UsbSubmitTransferInterruptTest, UsbSubmitTransferInterruptWriteIoError, TestSize.Level1)
203 {
204     USB_HILOGI(MODULE_USB_SERVICE, "UsbSubmitTransferInterruptWriteIoError begin.");
205     vector<UsbDevice> delist;
206     auto &UsbSrvClient = UsbSrvClient::GetInstance();
207     auto ret = UsbSrvClient.GetDevices(delist);
208     EXPECT_TRUE(!(delist.empty())) << "Device list is empty";
209     UsbDevice device;
210     bool hasDevice = false;
211     for (int32_t i = 0; i < delist.size(); i++) {
212         if (delist[i].GetClass() != 9) {
213             device = delist[i];
214             hasDevice = true;
215         }
216     }
217     EXPECT_TRUE(hasDevice);
218     ret = UsbSrvClient.RequestRight(device.GetName());
219     USBDevicePipe pip;
220     ret = UsbSrvClient.OpenDevice(device, pip);
221     UsbInterface interface = device.GetConfigs().front().GetInterfaces().at(0);
222     USBEndpoint point = interface.GetEndpoints().front();
223     UsbSrvClient.ReleaseInterface(pip, interface);
224     sptr<Ashmem> ashmem = Ashmem::CreateAshmem("usb_shared_memory", TEN);
225     ASSERT_NE(ashmem, nullptr);
226     const uint8_t dataToWrite[TEN] = {0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
227     ashmem->MapReadAndWriteAshmem();
228     bool writeSuccess = ashmem->WriteToAshmem(dataToWrite, sizeof(dataToWrite), 0);
229     ASSERT_TRUE(writeSuccess);
230 
231     HDI::Usb::V1_2::USBTransferInfo transferInfo;
232     transferInfo.endpoint = 0x01;    // 写操作(对于开发板,0x81是读操作)
233     transferInfo.flags = 0;
234     transferInfo.type = TYPE_INTERRUPT; // 开发板仅支持bulk
235     transferInfo.timeOut = 2000;
236     transferInfo.length = TEN;        // 期望长度
237     transferInfo.userData = 0;
238     transferInfo.numIsoPackets = 0;  // iso传输包数量 iso单包传输最大长度192
239     auto callback = [](const TransferCallbackInfo &info,
__anon046e99190302(const TransferCallbackInfo &info, const std::vector<HDI::Usb::V1_2::UsbIsoPacketDescriptor> &packets, uint64_t userData) 240                         const std::vector<HDI::Usb::V1_2::UsbIsoPacketDescriptor> &packets, uint64_t userData) {
241         USB_HILOGI(MODULE_USB_SERVICE, "InterruptWriteIoError status:%{public}d,actualLength:%{public}d",
242             info.status, info.actualLength);
243     };
244 
245     ret = UsbSrvClient.UsbSubmitTransfer(pip, transferInfo, callback, ashmem);
246     USB_HILOGI(MODULE_USB_SERVICE, "%{public}d line. UsbSubmitTransferInterruptWriteIoError ret:%{public}d",
247         __LINE__, ret);
248     ASSERT_EQ(ret,  USB_SUBMIT_TRANSFER_IO_ERROR);
249     bool close = UsbSrvClient.Close(pip);
250     EXPECT_TRUE(close);
251     USB_HILOGI(MODULE_USB_SERVICE, "UsbSubmitTransferInterruptWriteIoError end.");
252 }
253 
254 } // namespace SubmitTransfer
255 } // namespace USB
256 } // namespace OHOS