142103316Sopenharmony_ci# USB Manager `<a name="EN-US_TOPIC_0000001124094823"></a>`
242103316Sopenharmony_ci
342103316Sopenharmony_ci- [Introduction](#section11660541593)
442103316Sopenharmony_ci- [Directory Structure](#section19472752217)
542103316Sopenharmony_ci- [Available APIs](#section19472752218)
642103316Sopenharmony_ci- [Development Example](#section19472752219)
742103316Sopenharmony_ci- [Repositories Involved](#section63151229062)
842103316Sopenharmony_ci
942103316Sopenharmony_ci## Introduction`<a name="section11660541593"></a>`
1042103316Sopenharmony_ci
1142103316Sopenharmony_ciThe following figure shows the USB service architecture.
1242103316Sopenharmony_ci
1342103316Sopenharmony_ci**Figure 1**  USB service architecture`<a name="fig15658513184019"></a>`
1442103316Sopenharmony_ci![](figures/usb-manager-architecture.png "USB_service_architecture")
1542103316Sopenharmony_ci
1642103316Sopenharmony_ciThe architecture logically consists of three layers:
1742103316Sopenharmony_ci
1842103316Sopenharmony_ci1. USB API: provides USB APIs that implement various basic functions, for example, query of the USB device list, USB device plug notification, USB host and device mode switching, bulk transfer, control transfer, right management, and function switching in device mode.
1942103316Sopenharmony_ci2. USB Service: interacts with the HAL layer to receive, parse, and distribute data, manages foreground and background policies, and performs USB device management and right control.
2042103316Sopenharmony_ci3. USB HAL: provides driver capability APIs that can be directly called in user mode. The APIs are classified into the DDK initialization class, interface operation class, and request operation class by function. They can be used to perform DDK initialization, bind/release and open/close an interface, allocate/release a request, and implement isochronous or non-isochronous transfer. The USB HAL code is stored in [drivers\_peripheral](https://gitee.com/openharmony/drivers_peripheral/blob/master/README.md).
2142103316Sopenharmony_ci
2242103316Sopenharmony_ci## Directory Structure`<a name="section19472752217"></a>`
2342103316Sopenharmony_ci
2442103316Sopenharmony_ci```
2542103316Sopenharmony_cibase/usb/usb_manager
2642103316Sopenharmony_ci├── interfaces                  # APIs
2742103316Sopenharmony_ci│   ├── innerkits                  # Internal APIs
2842103316Sopenharmony_ci│   └── kits                       # External APIs
2942103316Sopenharmony_ci├── sa_profile                  # SA profile
3042103316Sopenharmony_ci└── services                    # Services
3142103316Sopenharmony_ci│   ├── native                     # Native APIs
3242103316Sopenharmony_ci│   └── zidl                       # zidl APIs
3342103316Sopenharmony_ci└── test                        # Test cases
3442103316Sopenharmony_ci└── utils                       # Utilities
3542103316Sopenharmony_ci```
3642103316Sopenharmony_ci
3742103316Sopenharmony_ci## Available APIs`<a name="section19472752218"></a>`
3842103316Sopenharmony_ci
3942103316Sopenharmony_ci### [External APIs](https://gitee.com/openharmony/usb_manager/blob/master/interfaces/kits/js/@ohos.usb.d.ts)
4042103316Sopenharmony_ci
4142103316Sopenharmony_ci### Internal APIs
4242103316Sopenharmony_ci
4342103316Sopenharmony_ci#### Host APIs
4442103316Sopenharmony_ci
4542103316Sopenharmony_ci| **API**                                                                                                        | **Description**                                                                                                                                                                                                                                                                                                                               |
4642103316Sopenharmony_ci| -------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
4742103316Sopenharmony_ci| int32_t OpenDevice(const UsbDevice &device, USBDevicePipe &pip);                                                     | Opens a USB device to set up a connection.                                                                                                                                                                                                                                                                                                          |
4842103316Sopenharmony_ci| bool Close(const USBDevicePipe &pip);                                                                                | Closes a USB device to release all system resources related to the device.                                                                                                                                                                                                                                                                          |
4942103316Sopenharmony_ci| int32_t GetDevices(std::vector &deviceList);                                                                         | Obtains the USB device list.                                                                                                                                                                                                                                                                                                                        |
5042103316Sopenharmony_ci| int32_t SetConfiguration(USBDevicePipe &pip, const USBConfig &config);                                               | Sets the current configuration of the USB device.                                                                                                                                                                                                                                                                                                   |
5142103316Sopenharmony_ci| int32_t ClaimInterface(USBDevicePipe &pip, const UsbInterface &interface, bool force);                               | Claims a USB interface exclusively. This must be done before data transfer.                                                                                                                                                                                                                                                                         |
5242103316Sopenharmony_ci| int32_t ReleaseInterface(USBDevicePipe &pip, const UsbInterface &interface);                                         | Releases a USB interface. This is usually done after data transfer.                                                                                                                                                                                                                                                                                 |
5342103316Sopenharmony_ci| int32_t SetInterface(USBDevicePipe &pipe, const UsbInterface &interface);                                            | Sets the alternate settings for the specified USB interface. This allows you to switch between two interfaces with the same ID but different alternate settings.                                                                                                                                                                                    |
5442103316Sopenharmony_ci| int32_t BulkTransfer(USBDevicePipe &pip, const USBEndpoint &endpoint, std::vector<uint8_t> &vdata, int32_t timeout); | Writes data on a specified endpoint during bulk transfer.                                                                                                                                                                                                                                                                                           |
5542103316Sopenharmony_ci| int32_t ControlTransfer(USBDevicePipe &pip, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &vdata);               | Performs control transfer for endpoint 0 of the device. The data transfer direction is determined by the request type. If the result of**requestType & USB_ENDPOINT_DIR_MASK** is **USB_DIR_OUT**, the endpoint is in the data writing direction; if the result is **USB_DIR_IN**, the endpoint is in the data reading direction. |
5642103316Sopenharmony_ci| int32_t RequestInitialize(UsbRequest &request);                                                                      | Initializes a request.                                                                                                                                                                                                                                                                                                                              |
5742103316Sopenharmony_ci| int32_t RequestQueue(UsbRequest &request);                                                                           | Sends or receives requests for isochronous transfer on a specified endpoint. The data transfer direction is determined by the endpoint direction.                                                                                                                                                                                                   |
5842103316Sopenharmony_ci| int32_t PipeRequestWait(USBDevicePipe &pip, int64_t timeout, UsbRequest &req);                                       | Waits for the operation result of the isochronous transfer request in`<b>`RequestQueue`</b>`.                                                                                                                                                                                                                                                   |
5942103316Sopenharmony_ci| int32_t RequestAbort(UsbRequest &request);                                                                           | Cancels the data transfer requests to be processed.                                                                                                                                                                                                                                                                                                 |
6042103316Sopenharmony_ci| int32_t RequestFree(UsbRequest &request);                                                                            | Requests for releasing data.                                                                                                                                                                                                                                                                                                                        |
6142103316Sopenharmony_ci| bool HasRight(std::string deviceName);                                                                               | Checks whether the application has permission to access the USB device.                                                                                                                                                                                                                                                                             |
6242103316Sopenharmony_ci| int32_t RequestRight(std::string deviceName);                                                                        | Requests for permission to access the USB device.                                                                                                                                                                                                                                                                                                   |
6342103316Sopenharmony_ci
6442103316Sopenharmony_ci#### Device APIs
6542103316Sopenharmony_ci
6642103316Sopenharmony_ci| **API**                                      | **Description**                                                                      |
6742103316Sopenharmony_ci| -------------------------------------------------- | ------------------------------------------------------------------------------------------ |
6842103316Sopenharmony_ci| int32_t GetCurrentFunctions(int32_t &funcs);      | Obtains the list of functions (represented by bit field) supported by the current device.  |
6942103316Sopenharmony_ci| int32_t SetCurrentFunctions(int32_t funcs);       | Sets the list of functions (represented by bit field) supported by the current device.     |
7042103316Sopenharmony_ci| int32_t UsbFunctionsFromString(std::string funcs); | Converts the string descriptor of a given USB function list to a numeric mask combination. |
7142103316Sopenharmony_ci| std::string UsbFunctionsToString(int32_t funcs);   | Converts the numeric mask combination of a given USB function list to a string descriptor. |
7242103316Sopenharmony_ci
7342103316Sopenharmony_ci#### Port APIs
7442103316Sopenharmony_ci
7542103316Sopenharmony_ci| **API**                                                             | **Description**                                                     |
7642103316Sopenharmony_ci| ------------------------------------------------------------------------- | ------------------------------------------------------------------------- |
7742103316Sopenharmony_ci| int32_t GetPorts(std::vector &usbPorts);                                  | Obtains the port list.                                                    |
7842103316Sopenharmony_ci| int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole); | Sets the port role.                                                       |
7942103316Sopenharmony_ci| int32_t GetSupportedModes(int32_t portId, int32_t &supportedModes);       | Obtains the mask combination for the supported mode list of a given port. |
8042103316Sopenharmony_ci
8142103316Sopenharmony_ci## Development Example`<a name="section19472752219"></a>`
8242103316Sopenharmony_ci
8342103316Sopenharmony_ciThe following provides some examples on how to use major APIs. For detailed service code, see [js_unittest](test/native/js_unittest).
8442103316Sopenharmony_ci
8542103316Sopenharmony_ci#### Host Development
8642103316Sopenharmony_ci
8742103316Sopenharmony_ciIn this example, the USB device serves as the host and connects to the device to implement data transfer.
8842103316Sopenharmony_ci
8942103316Sopenharmony_ci1. Obtain the USB device list.
9042103316Sopenharmony_ci
9142103316Sopenharmony_ci```JS
9242103316Sopenharmony_ci// Import the USB API package.
9342103316Sopenharmony_ciimport usb from '@ohos.usb';
9442103316Sopenharmony_ci// Obtain the USB device list.
9542103316Sopenharmony_civar deviceList = usb.getDevices();
9642103316Sopenharmony_ci```
9742103316Sopenharmony_ci
9842103316Sopenharmony_ci2. Obtain the device operation permissions.
9942103316Sopenharmony_ci
10042103316Sopenharmony_ci```JS
10142103316Sopenharmony_ci// device name
10242103316Sopenharmony_civar deviceName = deviceList[0].name;
10342103316Sopenharmony_ci// Request for permission to operate a specified device.
10442103316Sopenharmony_ciusb.requestRight(deviceName).then(hasRight => {
10542103316Sopenharmony_ci	console.info("usb device request right result: " + hasRight);
10642103316Sopenharmony_ci}).catch(error => {
10742103316Sopenharmony_ci	console.info("usb device request right failed : " + error);
10842103316Sopenharmony_ci});
10942103316Sopenharmony_ci```
11042103316Sopenharmony_ci
11142103316Sopenharmony_ci3. Open the USB device.
11242103316Sopenharmony_ci
11342103316Sopenharmony_ci```JS
11442103316Sopenharmony_ci// Open the USB device, and obtain the device pipe for data transfer.
11542103316Sopenharmony_civar pipe = usb.connectDevice(deviceList[0]);
11642103316Sopenharmony_ci// Claim the USB interface.
11742103316Sopenharmony_ciusb.claimInterface(pipe, interface, true); // Among the input arguments, interface refers to the one to be operated on the USB device.
11842103316Sopenharmony_ci```
11942103316Sopenharmony_ci
12042103316Sopenharmony_ci4. Perform data transfer.
12142103316Sopenharmony_ci
12242103316Sopenharmony_ci```JS
12342103316Sopenharmony_ci// Read data. Select the corresponding RX endpoint (endpoint.direction == 0x80) from deviceList for data transfer. The data data to be transferred is encapsulated in dataUint8Array.
12442103316Sopenharmony_ciusb.bulkTransfer(pipe, inEndpoint, dataUint8Array, 15000).then(dataLength => {
12542103316Sopenharmony_ciif (dataLength >= 0) {
12642103316Sopenharmony_ci	console.info("usb readData result Length : " + dataLength);
12742103316Sopenharmony_ci	var resultStr = this.ab2str(dataUint8Array); // Convert uint8 data into a string.
12842103316Sopenharmony_ci	console.info("usb readData buffer : " + resultStr);
12942103316Sopenharmony_ci} else {
13042103316Sopenharmony_ci	console.info("usb readData failed : " + dataLength);
13142103316Sopenharmony_ci}
13242103316Sopenharmony_ci}).catch(error => {
13342103316Sopenharmony_ciconsole.info("usb readData error : " + JSON.stringify(error));
13442103316Sopenharmony_ci});
13542103316Sopenharmony_ci// Send data. Select the corresponding TX endpoint (endpoint.direction == 0) from deviceList for data transfer. 
13642103316Sopenharmony_ciusb.bulkTransfer(this.pip, this.outEndpoint, dataUint8Array, 15000).then(dataLength => {
13742103316Sopenharmony_ci	if (dataLength >= 0) {
13842103316Sopenharmony_ci		console.info("usb writeData result write length : " + dataLength);
13942103316Sopenharmony_ci	} else {
14042103316Sopenharmony_ci		console.info("writeData failed");
14142103316Sopenharmony_ci	}
14242103316Sopenharmony_ci}).catch(error => {
14342103316Sopenharmony_ci	console.info("usb writeData error : " + JSON.stringify(error));
14442103316Sopenharmony_ci});
14542103316Sopenharmony_ci```
14642103316Sopenharmony_ci
14742103316Sopenharmony_ci5. Release the USB interface, and close the USB device.
14842103316Sopenharmony_ci
14942103316Sopenharmony_ci```JS
15042103316Sopenharmony_ciusb.releaseInterface(pipe, interface);
15142103316Sopenharmony_ciusb.closePipe(pipe);
15242103316Sopenharmony_ci```
15342103316Sopenharmony_ci
15442103316Sopenharmony_ci#### Device Development
15542103316Sopenharmony_ci
15642103316Sopenharmony_ciIn this example, the USB device is used as the device to set the ACM, ECM, and HDC functions.
15742103316Sopenharmony_ci
15842103316Sopenharmony_ci1. Set the USB functions.
15942103316Sopenharmony_ci
16042103316Sopenharmony_ci```JS
16142103316Sopenharmony_ciusb.setCurrentFunctions(funType).then(data => {
16242103316Sopenharmony_ci	console.info("usb setCurrentFunctions : " + data);
16342103316Sopenharmony_ci}).catch(error => {
16442103316Sopenharmony_ci	console.info("usb setCurrentFunctions error : " + error);
16542103316Sopenharmony_ci});
16642103316Sopenharmony_ci```
16742103316Sopenharmony_ci
16842103316Sopenharmony_ci## Repositories Involved`<a name="section63151229062"></a>`
16942103316Sopenharmony_ci
17042103316Sopenharmony_ci[Drive Subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/driver.md)
17142103316Sopenharmony_ci
17242103316Sopenharmony_ci[drivers\_peripheral](https://gitee.com/openharmony/drivers_peripheral/blob/master/README.md)
17342103316Sopenharmony_ci
17442103316Sopenharmony_ci[drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README.md)
17542103316Sopenharmony_ci
17642103316Sopenharmony_ci[drivers\_adapter](https://gitee.com/openharmony/drivers_adapter/blob/master/README.md)
17742103316Sopenharmony_ci
17842103316Sopenharmony_ci[drivers\_adapter\_khdf\_linux](https://gitee.com/openharmony/drivers_adapter_khdf_linux/blob/master/README.md)
179