1# USB Interrupt Transfer 2 3## When to Use 4 5The 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. 6 7## Preparing the Environment 8 9### Environment Requirements 10 11- Development tool and configuration: 12 13 DevEco Studio, as the driver development tool, allows you to develop, debug, and package drivers. [Download and install](https://developer.huawei.com/consumer/cn/download/) DevEco Studio and verify basic operations to ensure that it can function properly. For details, see [Creating and Running a Project](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V14/ide-create-new-project-V14) in [DevEco Studio User Guide](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V14/ide-tools-overview-V14). 14 15 16- SDK version configuration: 17 18 The ArkTs APIs for peripheral management can be used only when the SDK is of API version 16 or later. 19 20 21- HDC configuration: 22 23 HarmonyOS Device Connector (hdc) is a command-line tool for debugging. It can be used to interact with real devices or the Emulators on Windows, Linux, and macOS. For details about the configuration, see [hdc](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/hdc-V5). 24 25### Environment Setup 26 27- Install [DevEco Studio](https://developer.huawei.com/consumer/cn/download/deveco-studio) 4.1 or later on the PC. 28- Update the public SDK to API version 16 or later. For details, see [Switching to Full SDK](https://gitee.com/openharmony/docs/blob/master/en/application-dev/faqs/full-sdk-switch-guide.md). 29- Install hdc on the PC. You can use it to interact with a real device or the Emulator on Windows, Linux, or macOS. For details, see [hdc](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/hdc-V5). 30- Use a USB cable to connect an OpenHarmony device to the PC. 31 32## How to Develop 33 34### Available APIs 35 36| API | Description | 37|------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------| 38| usbSubmitTransfer(transfer: UsbDataTransferParams): void | Submits isochronous, bulk, or interrupt transfer in an asynchronous manner. | 39| usbCancelTransfer(transfer: UsbDataTransferParams): void | Cancels the submitted asynchronous transfer. | 40 41For details about the APIs of device management and transfer modes, see [@ohos.usbManager (USB Manager)](../../../../reference/apis-basic-services-kit/js-apis-usbManager.md). 42 43### How to Develop 44 45Connect a host to a device and use the **usbSubmitTransfer** API to transfer data. The following steps describe how to implement an interrupt transfer: 46 471. Import modules. 48 49 ```ts 50 // Import the usbManager module. 51 import { usbManager } from '@kit.BasicServicesKit'; 52 ``` 53 542. Obtain the USB device list. 55 56 ```ts 57 // Obtain the list of USB devices connected to the host. 58 let usbDevices: Array<usbManager.USBDevice> = usbManager.getDevices(); 59 console.info('usbDevices: ', JSON.stringify(usbDevices)); 60 if(usbDevices.length === 0) { 61 console.info('usbDevices is empty'); 62 return; 63 } 64 ``` 65 663. Obtain the device operation permissions. 67 68 ```ts 69 // Check whether the first USB device in the list has the access permission. 70 let usbDevice: usbManager.USBDevice = usbDevices[0]; 71 if(!usbManager.hasRight(usbDevice.name)) { 72 await usbManager.requestRight(usbDevice.name).then(result => { 73 if(!result) { 74 // If the USB device does not have the access permission and is not granted by the user, the device exits. 75 console.info('user is not granted the operation permission'); 76 return; 77 } 78 }); 79 } 80 ``` 81 824. Obtain the endpoint for reading data through interrupt transfer. 83 84 ```ts 85 let devicePipe: usbManager.USBDevicePipe = usbManager.connectDevice(usbDevice); 86 let usbConfigs: usbManager.USBConfiguration[] = usbDevice.configs; 87 let usbInterfaces: usbManager.USBInterface[] = []; 88 let usbInterface: usbManager.USBInterface | undefined = undefined 89 let usbEndpoints: usbManager.USBEndpoint[] = []; 90 let usbEndprint: usbManager.USBEndpoint | undefined = undefined 91 for (let i = 0; i < usbConfigs.length; i++) { 92 usbInterfaces = usbConfigs[i].interfaces; 93 for (let i = 0; i < usbInterfaces.length; i++) { 94 usbEndpoints = usbInterfaces[i].endpoints; 95 usbEndprint = usbEndpoints.find((value) => { 96 return value.direction === 128 && value.type === usbManager.UsbEndpointTransferType.TRANSFER_TYPE_INTERRUPT; 97 }) 98 if (usbEndprint !== undefined) { 99 usbInterface = usbInterfaces[i]; 100 break; 101 } 102 } 103 } 104 if (usbEndprint === undefined) { 105 console.error(`get usbEndprint error`) 106 return; 107 } 108 ``` 109 1105. Connect to the device and register the communication interface. 111 112 ```ts 113 // Register a communication interface. If the registration is successful, 0 is returned; otherwise, other error codes are returned. 114 let claimInterfaceResult: number = usbManager.claimInterface(devicePipe, usbInterface, true); 115 if (claimInterfaceResult !== 0) { 116 console.error(`claimInterface error = ${claimInterfaceResult}`) 117 return; 118 } 119 ``` 120 1216. Perform data transfer. 122 123 ```ts 124 try { 125 // The communication interface is successfully registered and performs data transfer. 126 let transferParams: usbManager.UsbDataTransferParams = { 127 devPipe: devicePipe, 128 flags: usbManager.UsbTransferFlags.USB_TRANSFER_SHORT_NOT_OK, 129 endpoint: usbEndprint.address, 130 type: usbManager.UsbEndpointTransferType.TRANSFER_TYPE_INTERRUPT, 131 timeout: 2000, 132 length: 10, 133 callback: () => {}, 134 userData: new Uint8Array(10), 135 buffer: new Uint8Array(10), 136 isoPacketCount: 2, 137 }; 138 139 transferParams.callback = (err: Error, callBackData: usbManager.SubmitTransferCallback) => { 140 console.info('callBackData = ' + JSON.stringify(callBackData)); 141 console.info('transfer success, result = ' + transferParams.buffer.toString()); 142 } 143 usbManager.usbSubmitTransfer(transferParams); 144 console.info('USB transfer request submitted.'); 145 } catch (error) { 146 console.error('USB transfer failed:', error); 147 } 148 ``` 149 1507. Cancels the data transfer, releases the interface, and closes the USB device pipe. 151 152 ```ts 153 usbManager.usbCancelTransfer(transferParams); 154 usbManager.releaseInterface(devicePipe, usbInterface); 155 usbManager.closePipe(devicePipe); 156 ``` 157 158### Verification 159 1601. Connect the host to a terminal device (such as a mouse or a keyboard) that supports interrupt transfer through a USB interface. 1612. Execute the preceding code. 1623. Search for the keyword **transfer success** in the log. If the keyword is found, the interrupt transfer interface is successfully called. 163