• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# USB Control 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 control transfer is used to obtain and set the device status, and control the device attribute status. You can determine whether the control transfer can read or write data based on the endpoint types supported by the device.
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 Emulators 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| usbControlTransfer(pipe: USBDevicePipe, requestparam: USBDeviceRequestParams, timeout?: number): Promise&lt;number&gt; | Performs control transfer. |
46
47For details about the APIs of device management and transfer modes, see [@ohos.usbManager (USB Manager)](../../../../reference/apis-basic-services-kit/js-apis-usbManager.md).
48
49### Development Procedure
50
51Connect a host to a device and use the **usbControlTransfer** API to transfer data. The following steps describe how to implement a control transfer:
52
53> **NOTE**
54>
55> 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.
56
571. Import modules.
58
59   ```ts
60   // Import the usbManager module.
61   import { usbManager } from '@kit.BasicServicesKit';
62   import { BusinessError } from '@kit.BasicServicesKit';
63   ```
64
652. Obtain the USB device list.
66
67   ```ts
68   // Obtain the USB device list.
69   let deviceList : Array<usbManager.USBDevice> = usbManager.getDevices();
70   console.info(`usbDevices: ${deviceList}`);
71   if(deviceList.length === 0) {
72     console.error('deviceList is empty');
73     return;
74   }
75   /*
76   Example deviceList structure:
77   [
78     {
79       name: "1-1",
80       serial: "",
81       manufacturerName: "",
82       productName: "",
83       version: "",
84       vendorId: 7531,
85       productId: 2,
86       clazz: 9,
87       subClass: 0,
88       protocol: 1,
89       devAddress: 1,
90       busNum: 1,
91       configs: [
92         {
93           id: 1,
94           attributes: 224,
95           isRemoteWakeup: true,
96           isSelfPowered: true,
97           maxPower: 0,
98           name: "1-1",
99           interfaces: [
100             {
101               id: 0,
102               protocol: 0,
103               clazz: 9,
104               subClass: 0,
105               alternateSetting: 0,
106               name: "1-1",
107               endpoints: [
108                 {
109                   address: 129,
110                   attributes: 3,
111                   interval: 12,
112                   maxPacketSize: 4,
113                   direction: 128,
114                   number: 1,
115                   type: 3,
116                   interfaceId: 0,
117                 }
118               ]
119             }
120           ]
121         }
122       ]
123     }
124   ]
125   */
126   ```
127
1283. Obtain the device operation permissions.
129
130   ```ts
131   let deviceName : string = deviceList[0].name;
132   // Request the permissions to operate a specified device.
133   usbManager.requestRight(deviceName).then((hasRight : boolean) => {
134     console.info("usb device request right result: " + hasRight);
135   }).catch((error : BusinessError)=> {
136     console.error(`usb device request right failed : ${error}`);
137   });
138   ```
139
1404. Open the device.
141
142   ```ts
143   // Open the device, and obtain the USB device pipe for data transfer.
144   let pipe : usbManager.USBDevicePipe = usbManager.connectDevice(deviceList[0]);
145   let interface1 : usbManager.USBInterface = deviceList[0].configs[0].interfaces[0];
146
147   /*
148    Claim the corresponding interface from deviceList.
149   interface1 must be one present in the device configuration.
150   */
151   usbManager.claimInterface(pipe, interface1, true);
152   ```
153
1545. Perform data transfer.
155
156    ```ts
157    /*
158      Construct control transfer parameters.
159    */
160    let param: usbManager.USBDeviceRequestParams = {
161      bmRequestType: 0x80,    // 0x80 indicates a standard request for data transfer from the device to the host.
162      bRequest: 0x06,    // 0x06 indicates a request for the descriptor.
163      wValue:0x01 << 8 | 0,    // The value is of two bytes. The high byte indicates the descriptor type. Here, 0x01 indicates the device descriptor. The low byte indicates the descriptor index. The value is set to 0 because it is not involved for the device descriptor.
164      wIndex: 0,    // Descriptor index. The value can be 0.
165      wLength: 18,    // Descriptor length. The value 18 indicates the length of a device descriptor. A maximum of 1024 characters are supported.
166      data: new Uint8Array(18)
167    };
168
169    usbManager.usbControlTransfer(pipe, param).then((ret: number) => {
170    console.info(`usbControlTransfer = ${ret}`);
171    })
172    ```
173
1746. Release the USB interface, and close the USB device pipe.
175
176   ```ts
177   usbManager.releaseInterface(pipe, interface1);
178   usbManager.closePipe(pipe);
179   ```
180