• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# USB Interrupt Transfer
2
3<!--Kit: Basic Services Kit-->
4<!--Subsystem: USB-->
5<!--Owner: @hwymlgitcode-->
6<!--Designer: @w00373942-->
7<!--Tester: @dong-dongzhen-->
8<!--Adviser: @w_Machine_cc-->
9
10## When to Use
11
12The interrupt transfer is mainly used by the host to receive a data packet sent by a device. The endpoint mode of a device determines whether the interface supports interrupt reading or interrupt writing. This mode is applicable to the transfer of a small number of scattered and unpredictable data input by devices such as the mouse, keyboard, and joystick. In addition, the endpoints of these devices support only interrupt reading.
13
14## Preparing the Environment
15
16### Environment Requirements
17
18- Development tool and configuration:
19
20  DevEco Studio, as the driver development tool, allows you to develop, debug, and package drivers. [Download and install](https://developer.huawei.com/consumer/en/download/) DevEco Studio and verify basic operations to ensure that it can function properly. For details, see [Creating a Project](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-create-new-project) in [DevEco Studio User Guide](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-tools-overview).
21
22
23- SDK version configuration:
24
25  The ArkTs APIs for peripheral management can be used only when the SDK is of API version 16 or later.
26
27
28- HDC configuration:
29
30  HarmonyOS Device Connector (hdc) is a command-line tool for debugging. It can be used to interact with real devices or the Emulator on Windows, Linux, and macOS. For details about the configuration, see [hdc](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/hdc).
31
32### Environment Setup
33
34- Install [DevEco Studio](https://developer.huawei.com/consumer/en/download/) 4.1 or later on the PC.
35- Update the public SDK to API version 16 or later.<!--Del--> For details, see [Switching to Full SDK](../../../../faqs/full-sdk-switch-guide.md).<!--DelEnd-->
36- Install hdc on the PC. You can use it to interact with a real device or the Emulator on Windows, Linux, or macOS.
37- Use a USB cable to connect a device to the PC.
38
39## How to Develop
40
41### Available APIs
42
43| API                                                                                                             | Description                                                     |
44|------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------|
45| usbSubmitTransfer(transfer: UsbDataTransferParams): void                                                         | Submits isochronous, bulk, or interrupt transfer in an asynchronous manner.                                  |
46| usbCancelTransfer(transfer: UsbDataTransferParams): void                                                         | Cancels the submitted asynchronous transfer.                                            |
47
48For details about the APIs of device management and transfer modes, see [@ohos.usbManager (USB Manager)](../../../../reference/apis-basic-services-kit/js-apis-usbManager.md).
49
50### How to Develop
51
52Connect a host to a device and use the **usbSubmitTransfer** API to transfer data. The following steps describe how to implement an interrupt transfer:
53
54> **NOTE**
55>
56> The following sample code shows only a basic process. You should execute the code in a specific method. When calling this method, you must comply with device protocols to ensure proper data transfer and device compatibility.
57
581. Import modules.
59
60    ```ts
61    // Import the usbManager module.
62    import { usbManager } from '@kit.BasicServicesKit';
63    ```
64
652. Obtain the USB device list.
66
67    ```ts
68    // Obtain the list of USB devices connected to the host.
69    let usbDevices: Array<usbManager.USBDevice> = usbManager.getDevices();
70    console.info(`usbDevices: ${usbDevices}`);
71    if(usbDevices.length === 0) {
72      console.error('usbDevices is empty');
73      return;
74    }
75    ```
76
773. Obtain the device operation permissions.
78
79    ```ts
80    // Check whether the first USB device in the list has the access permission.
81    // Name the function based on the specific service.
82    async function transferDefault() {
83        let usbDevice: usbManager.USBDevice = usbDevices[0];
84        if(!usbManager.hasRight(usbDevice.name)) {
85          await usbManager.requestRight(usbDevice.name).then(result => {
86            if(!result) {
87              // If the USB device does not have the access permission and is not granted by the user, the device exits.
88              console.error('The user does not have permission to perform this operation');
89              return;
90            }
91          });
92        }
93    }
94    ```
95
964. Obtain the endpoint for reading data through interrupt transfer.
97
98   ```ts
99   let devicePipe: usbManager.USBDevicePipe = usbManager.connectDevice(usbDevice);
100   let usbConfigs: usbManager.USBConfiguration[] = usbDevice.configs;
101   let usbInterfaces: usbManager.USBInterface[] = [];
102   let usbInterface: usbManager.USBInterface | undefined = undefined
103   let usbEndpoints: usbManager.USBEndpoint[] = [];
104   let usbEndpoint: usbManager.USBEndpoint | undefined = undefined
105   for (let i = 0; i < usbConfigs.length; i++) {
106     usbInterfaces = usbConfigs[i].interfaces;
107     for (let j = 0; j < usbInterfaces.length; j++) {
108       usbEndpoints = usbInterfaces[j].endpoints;
109       usbEndpoint = usbEndpoints.find((value) => {
110         return value.direction === 128 && value.type === usbManager.UsbEndpointTransferType.TRANSFER_TYPE_INTERRUPT;
111       })
112       if (usbEndpoint !== undefined) {
113         usbInterface = usbInterfaces[j];
114         break;
115       }
116     }
117   }
118   if (usbEndpoint === undefined) {
119     console.error(`get usbEndpoint error`)
120     return;
121   }
122   ```
123
1245. Connect to the device and register the communication interface.
125
126    ```ts
127    // Register a communication interface. If the registration is successful, 0 is returned; otherwise, other error codes are returned.
128    let claimInterfaceResult: number = usbManager.claimInterface(devicePipe, usbInterface, true);
129    if (claimInterfaceResult !== 0) {
130      console.error(`claimInterface error = ${claimInterfaceResult}`)
131      return;
132    }
133    ```
134
1356. Perform data transfer.
136
137   ```ts
138   try {
139     // The communication interface is successfully registered and performs data transfer.
140     let transferParams: usbManager.UsbDataTransferParams = {
141       devPipe: devicePipe,
142       flags: usbManager.UsbTransferFlags.USB_TRANSFER_SHORT_NOT_OK,
143       endpoint: usbEndpoint.address,
144       type: usbManager.UsbEndpointTransferType.TRANSFER_TYPE_INTERRUPT,
145       timeout: 2000,
146       length: 10,
147       callback: () => {},
148       userData: new Uint8Array(10),
149       buffer: new Uint8Array(10),
150       isoPacketCount: 2,
151     };
152
153     transferParams.callback = (err: Error, callBackData: usbManager.SubmitTransferCallback) => {
154       console.info(`callBackData = ${callBackData}`);
155       console.info(`transfer success, result = ${transferParams.buffer}`);
156     }
157     usbManager.usbSubmitTransfer(transferParams);
158     console.info('USB transfer request submitted.');
159   } catch (error) {
160     console.error(`USB transfer failed: ${error}`);
161   }
162   ```
163
1647. Cancel the data transfer, releases the interface, and closes the USB device pipe.
165
166    ```ts
167    usbManager.usbCancelTransfer(transferParams);
168    usbManager.releaseInterface(devicePipe, usbInterface);
169    usbManager.closePipe(devicePipe);
170    ```
171
172### Verification
173
1741. Connect the host to a terminal device (such as a mouse or a keyboard) that supports interrupt transfer through a USB interface.
1752. Execute the preceding code.
1763. Search for the keyword **transfer success** in the log. If the keyword is found, the interrupt transfer interface is successfully called.
177