subsys-usbservice-guide.md 1.2 KB
Newer Older
A
annie_wangli 已提交
1
# USB Usage Guidelines<a name="EN-US_TOPIC_0000001077367159"></a>
S
shawn_he 已提交
2

A
annie_wangli 已提交
3

A
annie_wangli 已提交
4
The following procedure uses bulk transfer as an example.
S
shawn_he 已提交
5 6 7 8 9

## Procedure<a name="section18816105182315"></a>

1.  Obtain a USB service instance.

W
wu-chengwen 已提交
10
```cpp
S
shawn_he 已提交
11 12 13 14 15
static OHOS::USB::UsbSrvClient &g_usbClient = OHOS::USB::UsbSrvClient::GetInstance();
```

2.  Obtain the USB device list.

W
wu-chengwen 已提交
16
```cpp
S
shawn_he 已提交
17 18 19 20 21 22
std::vector<OHOS::USB::UsbDevice> deviceList;
int32_t ret = g_usbClient.GetDevices(deviceList);
```

3.  Apply for device access permissions.

W
wu-chengwen 已提交
23
```cpp
S
shawn_he 已提交
24 25 26
int32_t ret = g_usbClient.RequestRight(device.GetName());
```

A
annie_wangli 已提交
27
4.  Open the USB device.
S
shawn_he 已提交
28

W
wu-chengwen 已提交
29
```cpp
S
shawn_he 已提交
30 31 32 33 34 35
USBDevicePipe pip;
int32_t et = g_usbClient.OpenDevice(device, pip);
```

5.  Configure the USB interface.

W
wu-chengwen 已提交
36
```cpp
S
shawn_he 已提交
37 38 39 40
ret = g_usbClient.ClaimInterface(pip, interface, true);
**interface** indicates an interface of the USB device in **deviceList**.
```

A
annie_wangli 已提交
41
6.  Transfer data.
S
shawn_he 已提交
42

W
wu-chengwen 已提交
43
```cpp
S
shawn_he 已提交
44 45
srvClient.BulkTransfer(pipe, endpoint, vdata, timeout);
```
46 47 48 49 50
Parameter description:
- **pipe**: pipe for data transfer of the USB device opened. 
- **endpoint**: endpoint for data transfer on the USB device. 
- **vdata**: binary data block to be transferred or read. 
- **timeout**: timeout duration of data transfer.
S
shawn_he 已提交
51 52 53

7.  Close the USB device.

W
wu-chengwen 已提交
54
```cpp
S
shawn_he 已提交
55 56
ret = g_usbClient.Close(pip);
```