* @brief 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.
*
* @param dev Indicates the USB device address.
* @param interfaceid Indicates the interface ID of the USB device.
* @param altIndex Indicates the alternate settings of the USB interface.
*
* @return Returns <b>0</b> if the operation is successful; returns a non-0 value if the operation fails.
* @brief Reads data on a specified endpoint during bulk transfer. The endpoint must be in the data reading direction. You can specify a timeout duration if needed.
*
* @param dev Indicates the USB device address.
* @param pipe Indicates the pipe of the USB device.
* @param timeout Indicates the timeout duration.
* @param data Indicates the read data.
*
* @return Returns <b>0</b> if the operation is successful; returns a non-0 value if the operation fails.
* @brief Performs control transfer for endpoint 0 of the device. The data transfer direction is determined by the request type. If the result of <b>requestType</b>&
* <b>USB_ENDPOINT_DIR_MASK</b> is USB_DIR_OUT, the endpoint is in the data writing direction; if the result is <b>USB_DIR_IN</b>, the endpoint is in the data reading direction.
*
* @param dev Indicates the USB device address.
* @param ctrl Indicates the control data packet structure.
* @param data Indicates the read data or written.
*
* @return Returns <b>0</b> if the operation is successful; returns a non-0 value if the operation fails.
* @brief Sends or receives requests for isochronous transfer on a specified endpoint. The data transfer direction is determined by the endpoint direction.
*
* @param dev Indicates the USB device address.
* @param pipe Indicates the pipe of the USB device.
* @param clientData Indicates the client data.
* @param buffer Indicates the transferred data.
*
* @return Returns <b>0</b> if the operation is successful; returns a non-0 value if the operation fails.
* @since 3.0
*/
staticint32_tRequestQueue(constUsbDev&dev,
constUsbPipe&pipe,
conststd::vector<uint8_t>&clientData,
conststd::vector<uint8_t>&buffer);
/* *
* @brief Waits for the operation result of the requests for isochronous transfer in <b>RequestQueue</b>.
*
* @param dev Indicates the USB device address.
* @param clientData Indicates the client data.
* @param buffer Indicates the transferred data.
* @param timeout Indicates the timeout duration.
*
* @return Returns <b>0</b> if the operation is successful; returns a non-0 value if the operation fails.
**pipe** indicates the pipe for data transfer after the USB device is opened. **endpoint** indicates the endpoint for data transfer on the USB device. **vdata** indicates the binary data block to be transferred or read. **timeout** indicates the timeout duration of data transfer.
USB devices are classified into two types: USB host and USB device. On OpenHarmony, you can use the port service to switch between the host mode and device mode. In host mode, you can obtain the list of connected USB devices, manage device access permissions, and perform bulk transfer or control transfer between the host and connected devices. In device mode, you can switch between functions including HDC (debugging), ACM (serial port), and ECM (Ethernet port).
**Figure 1** USB service architecture
![](figure/USB_service_architecture.png)
- USB FWK/API: a layer that provides JS APIs for the upper layer through NAPI.
- USB SERVICE: a layer implemented by using the C++ programming language and logically divided into the HOST, DEVICE, and PORT modules. HDI-based APIs provided by USB SERVICE are mainly used to implement management of USB device list, USB functions, USB ports, and USB device access permissions.
- USB HAL: a layer implemented by using the C programming language. Based on the Host SDK and Device SDK, USB HAL encapsulates basic USB device operations, provides C++ APIs for the upper layer, and receives information from the kernel through the Hardware Driver Foundation (HDF) framework.
## Available APIs<a name="section83365421647"></a>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p201331557185512"><aname="p201331557185512"></a><aname="p201331557185512"></a>Opens a USB device to set up a connection.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p161332570553"><aname="p161332570553"></a><aname="p161332570553"></a>Checks whether the user has the permission to access the USB device.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p131331557175510"><aname="p131331557175510"></a><aname="p131331557175510"></a>Requests the temporary permission for a given application to access the USB device.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1470315695811"><aname="p1470315695811"></a><aname="p1470315695811"></a>Obtains the USB device list.</p>
The <tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1186597589"><aname="p1186597589"></a><aname="p1186597589"></a>Claims a USB interface exclusively. This must be done before data transfer.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p15832129135813"><aname="p15832129135813"></a><aname="p15832129135813"></a>Releases a USB interface. This is usually done after data transfer.</p>
The <tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1186597589"><aname="p1186597589"></a><aname="p1186597589"></a>Performs a bulk transfer on a specified endpoint. The data transfer direction is determined by the endpoint direction.</p>
The <tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p15832129135813"><aname="p15832129135813"></a><aname="p15832129135813"></a>Performs control transaction for endpoint 0 of the device. The transmission direction is determined by the request type.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1186597589"><aname="p1186597589"></a><aname="p1186597589"></a>Sets the current configuration of the USB device.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p15832129135813"><aname="p15832129135813"></a><aname="p15832129135813"></a>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.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1186597589"><aname="p1186597589"></a><aname="p1186597589"></a>Obtains the raw USB descriptor.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p15832129135813"><aname="p15832129135813"></a><aname="p15832129135813"></a>Obtains the file descriptor.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1186597589"><aname="p1186597589"></a><aname="p1186597589"></a>Closes a USB device to release all system resources related to the device.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p15832129135813"><aname="p15832129135813"></a><aname="p15832129135813"></a>Obtains the isochronous transfer result.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1186597589"><aname="p1186597589"></a><aname="p1186597589"></a>Initializes the isochronous transfer request.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p15832129135813"><aname="p15832129135813"></a><aname="p15832129135813"></a>Frees the isochronous transfer request.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.3 "><pid="p752531095814"><aname="p752531095814"></a><aname="p752531095814"></a>Cancels the data transfer requests to be processed.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1929141611198"><aname="p1929141611198"></a><aname="p1929141611198"></a>Sends or receives isochronous data requests on a specified endpoint. The data transfer direction is determined by the endpoint direction.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1929141611198"><aname="p1929141611198"></a><aname="p1929141611198"></a>Reads bulk data in isochronous manner. This method is applicable to transfer of a huge amount of data.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1929141611198"><aname="p1929141611198"></a><aname="p1929141611198"></a>Obtains the data reading result. Use this method together with <b>BulkReadData</b>.
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1929141611198"><aname="p1929141611198"></a><aname="p1929141611198"></a>Writes bulk data in isochronous manner. This method is applicable to transfer of a huge amount of data.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1929141611198"><aname="p1929141611198"></a><aname="p1929141611198"></a>Obtains the data writing status based on <b>length</b>. Use this method together with <b>BulkWriteData</b>.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p201331557185512"><aname="p201331557185512"></a><aname="p201331557185512"></a>Obtains the numeric mask combination for the current USB function list in Device mode.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p161332570553"><aname="p161332570553"></a><aname="p161332570553"></a>Sets the current USB function list in Device mode.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p131331557175510"><aname="p131331557175510"></a><aname="p131331557175510"></a>Converts the string descriptor of a given USB function list to a numeric mask combination.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1470315695811"><aname="p1470315695811"></a><aname="p1470315695811"></a>Converts the numeric mask combination of a given USB function list to a string descriptor.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p201331557185512"><aname="p201331557185512"></a><aname="p201331557185512"></a>Obtains the mask combination for the supported mode list of a given port.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p161332570553"><aname="p161332570553"></a><aname="p161332570553"></a>Sets the role types supported by a specified port, which can be <b>powerRole</b> (for charging) or <b>dataRole</b> (for data transfer).</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p131331557175510"><aname="p131331557175510"></a><aname="p131331557175510"></a>Obtains the USB port descriptor list.</p>