提交 2fe260c5 编写于 作者: S shawn_he

update doc

Signed-off-by: Nshawn_he <shawn.he@huawei.com>
上级 fb0d545d
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
## When to Use ## When to Use
In Host mode, you can obtain the list of connected devices, enable or disable the devices, manage device access permissions, and perform data transfer or control transfer. In Host mode, you can obtain the list of connected USB devices, enable or disable the devices, manage device access permissions, and perform data transfer or control transfer.
## APIs ## APIs
...@@ -16,6 +16,7 @@ The following table lists the USB APIs currently available. For details, see the ...@@ -16,6 +16,7 @@ The following table lists the USB APIs currently available. For details, see the
| ------------------------------------------------------------ | ------------------------------------------------------------ | | ------------------------------------------------------------ | ------------------------------------------------------------ |
| hasRight(deviceName: string): boolean | Checks whether the user, for example, the application or system, has the device access permissions. The value **true** is returned if the user has the device access permissions; the value **false** is returned otherwise. | | hasRight(deviceName: string): boolean | Checks whether the user, for example, the application or system, has the device access permissions. The value **true** is returned if the user has the device access permissions; the value **false** is returned otherwise. |
| requestRight(deviceName: string): Promise\<boolean> | Requests the temporary permission for a given application to access the USB device. | | requestRight(deviceName: string): Promise\<boolean> | Requests the temporary permission for a given application to access the USB device. |
| removeRight(deviceName: string): boolean | Removes the permission for a given application to access the USB device. |
| connectDevice(device: USBDevice): Readonly\<USBDevicePipe> | Connects to the USB device based on the device information returned by `getDevices()`. | | connectDevice(device: USBDevice): Readonly\<USBDevicePipe> | Connects to the USB device based on the device information returned by `getDevices()`. |
| getDevices(): Array<Readonly\<USBDevice>> | Obtains the USB device list. | | getDevices(): Array<Readonly\<USBDevice>> | Obtains the USB device list. |
| setConfiguration(pipe: USBDevicePipe, config: USBConfig): number | Sets the USB device configuration. | | setConfiguration(pipe: USBDevicePipe, config: USBConfig): number | Sets the USB device configuration. |
......
# USB
The USB module provides USB device management functions, including USB device list query, bulk data transfer, control transfer, and permission control.
> **NOTE**
>
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```js
import usb from "@ohos.usb";
```
## usb.getDevices
getDevices(): Array&lt;Readonly&lt;USBDevice&gt;&gt;
Obtains the USB device list.
**System capability**: SystemCapability.USB.USBManager
**Return value**
| Type | Description |
| ---------------------------------------------------- | ------- |
| Array&lt;Readonly&lt;[USBDevice](#usbdevice)&gt;&gt; | Device information list.|
**Example**
```js
let devicesList = usb.getDevices();
console.log(`devicesList = ${JSON.stringify(devicesList)}`);
// devicesList is a list of USB devices.
// A simple example of devicesList is provided as follows:
[
{
name: "1-1",
serial: "",
manufacturerName: "",
productName: "",
version: "",
vendorId: 7531,
productId: 2,
clazz: 9,
subclass: 0,
protocol: 1,
devAddress: 1,
busNum: 1,
configs: [
{
id: 1,
attributes: 224,
isRemoteWakeup: true,
isSelfPowered: true,
maxPower: 0,
name: "1-1",
interfaces: [
{
id: 0,
protocol: 0,
clazz: 9,
subclass: 0,
alternateSetting: 0,
name: "1-1",
endpoints: [
{
address: 129,
attributes: 3,
interval: 12,
maxPacketSize: 4,
direction: 128,
number: 1,
type: 3,
interfaceId: 0,
},
],
},
],
},
],
},
]
```
## usb.connectDevice
connectDevice(device: USBDevice): Readonly&lt;USBDevicePipe&gt;
Connects to a USB device.
Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list, and then call [usb.requestRight](#usbrequestright) to request the device access permission.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| device | [USBDevice](#usbdevice) | Yes| USB device information.|
**Return value**
| Type| Description|
| -------- | -------- |
| Readonly&lt;[USBDevicePipe](#usbdevicepipe)&gt; | USB device pipe for data transfer.|
**Example**
```js
let devicepipe= usb.connectDevice(device);
console.log(`devicepipe = ${JSON.stringify(devicepipe)}`);
```
## usb.hasRight
hasRight(deviceName: string): boolean
Checks whether the application has the permission to access the device.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| deviceName | string | Yes| Device name.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | The value **true** indicates that the application has the permission to access the device, and the value **false** indicates the opposite.|
**Example**
```js
let devicesName="1-1";
let bool = usb.hasRight(devicesName);
console.log(bool);
```
## usb.requestRight
requestRight(deviceName: string): Promise&lt;boolean&gt;
Requests the temporary permission for the application to access the USB device. This API uses a promise to return the result.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| deviceName | string | Yes| Device name.|
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;boolean&gt; | Promise used to return the result. The value **true** indicates that the temporary device access permissions are granted, and the value **false** indicates the opposite.|
**Example**
```js
let devicesName="1-1";
usb.requestRight(devicesName).then((ret) => {
console.log(`requestRight = ${JSON.stringify(ret)}`);
});
```
## usb.claimInterface
claimInterface(pipe: USBDevicePipe, iface: USBInterface, force?: boolean): number
Claims a USB interface.
Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list and USB interfaces, call [usb.requestRight](#usbrequestright) to request the device access permission, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.|
| iface | [USBInterface](#usbinterface) | Yes| USB interface, which is used to determine the index of the interface to claim.|
| force | boolean | No| Whether to forcibly claim the USB interface. The default value is **false**, indicating not to forcibly claim the USB interface.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | The value **0** indicates that the USB interface is successfully claimed, and an error code indicates the opposite.|
**Example**
```js
let ret = usb.claimInterface(devicepipe, interfaces);
console.log(`claimInterface = ${ret}`);
```
## usb.releaseInterface
releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number
Releases a USB interface.
Before you do this, ensure that you have claimed the interface by calling [usb.claimInterface](#usbclaiminterface).
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.|
| iface | [USBInterface](#usbinterface) | Yes| USB interface, which is used to determine the index of the interface to release.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | The value **0** indicates that the USB interface is successfully released, and an error code indicates the opposite.|
**Example**
```js
let ret = usb.releaseInterface(devicepipe, interfaces);
console.log(`releaseInterface = ${ret}`);
```
## usb.setConfiguration
setConfiguration(pipe: USBDevicePipe, config: USBConfig): number
Sets the device configuration.
Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list and device configuration, call [usb.requestRight](#usbrequestright) to request the device access permission, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.|
| config | [USBConfig](#usbconfig) | Yes| USB configuration to set.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | The value **0** indicates that the USB configuration is successfully set, and an error code indicates the opposite.|
**Example**
```js
let ret = usb.setConfiguration(devicepipe, config);
console.log(`setConfiguration = ${ret}`);
```
## usb.setInterface
setInterface(pipe: USBDevicePipe, iface: USBInterface): number
Sets a USB interface.
Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list and interfaces, call [usb.requestRight](#usbrequestright) to request the device access permission, call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter, and call [usb.claimInterface](#usbclaiminterface) to claim the USB interface.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------------------------------- | --- | ------------- |
| pipe | [USBDevicePipe](#usbdevicepipe) | Yes | Device pipe, which is used to determine the bus number and device address.|
| iface | [USBInterface](#usbinterface) | Yes | USB interface to set. |
**Return value**
| Type| Description|
| -------- | -------- |
| number | The value **0** indicates that the USB interface is successfully set, and an error code indicates the opposite.|
**Example**
```js
let ret = usb.setInterface(devicepipe, interfaces);
console.log(`setInterface = ${ret}`);
```
## usb.getRawDescriptor
getRawDescriptor(pipe: USBDevicePipe): Uint8Array
Obtains the raw USB descriptor.
Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list, call [usb.requestRight](#usbrequestright) to request the device access permission, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.|
**Return value**
| Type| Description|
| -------- | -------- |
| Uint8Array | The return value is the raw USB descriptor if the operation is successful, or **undefined** if the operation has failed.|
**Example**
```js
let ret = usb.getRawDescriptor(devicepipe);
```
## usb.getFileDescriptor
getFileDescriptor(pipe: USBDevicePipe): number
Obtains the file descriptor.
Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list, call [usb.requestRight](#usbrequestright) to request the device access permission, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.|
**Return value**
| Type | Description |
| ------ | -------------------- |
| number | The return value is the file descriptor of the USB device if the operation is successful, or **-1** if the operation has failed.|
**Example**
```js
let ret = usb.getFileDescriptor(devicepipe);
```
## usb.controlTransfer
controlTransfer(pipe: USBDevicePipe, contrlparam: USBControlParams, timeout?: number): Promise&lt;number&gt;
Performs control transfer.
Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list, call [usb.requestRight](#usbrequestright) to request the device access permission, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the USB device.|
| contrlparam | [USBControlParams](#usbcontrolparams) | Yes| Control transfer parameters.|
| timeout | number | No| Timeout duration in ms. This parameter is optional. The default value is **0**, indicating no timeout.|
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;number&gt; | Promise used to return the result, which is the size of the transmitted or received data block if the transfer is successful, or **-1** if an exception has occurred.|
**Example**
```js
usb.controlTransfer(devicepipe, USBControlParams).then((ret) => {
console.log(`controlTransfer = ${JSON.stringify(ret)}`);
})
```
## usb.bulkTransfer
bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, timeout?: number): Promise&lt;number&gt;
Performs bulk transfer.
Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list and endpoints, call [usb.requestRight](#usbrequestright) to request the device access permission, call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter, and call [usb.claimInterface](#usbclaiminterface) to claim the USB interface.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the USB device.|
| endpoint | [USBEndpoint](#usbendpoint) | Yes| USB endpoint, which is used to determine the USB port for data transfer.|
| buffer | Uint8Array | Yes| Buffer for writing or reading data.|
| timeout | number | No| Timeout duration in ms. This parameter is optional. The default value is **0**, indicating no timeout.|
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;number&gt; | Promise used to return the result, which is the size of the transmitted or received data block if the transfer is successful, or **-1** if an exception has occurred.|
**Example**
```js
// Call usb.getDevices to obtain a data set. Then, obtain a USB device and its access permission.
// Pass the obtained USB device as a parameter to usb.connectDevice. Then, call usb.connectDevice to connect the USB device.
// Call usb.claimInterface to claim the USB interface. After that, call usb.bulkTransfer to start bulk transfer.
usb.bulkTransfer(devicepipe, endpoint, buffer).then((ret) => {
console.log(`bulkTransfer = ${JSON.stringify(ret)}`);
});
```
## usb.closePipe
closePipe(pipe: USBDevicePipe): number
Closes a USB device pipe.
Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list, call [usb.requestRight](#usbrequestright) to request the device access permission, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | The value **0** indicates that the USB device pipe is closed successfully, and an error code indicates the opposite.|
**Example**
```js
let ret = usb.closePipe(devicepipe);
console.log(`closePipe = ${ret}`);
```
## usb.usbFunctionsFromString<sup>9+</sup>
usbFunctionsFromString(funcs: string): number
Converts the USB function list in the string format to a numeric mask in Device mode.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ---------------------- |
| funcs | string | Yes | Function list in string format.|
**Return value**
| Type | Description |
| ------ | ------------------ |
| number | Function list in numeric mask format.|
**Example**
```js
let funcs = "acm";
let ret = usb.usbFunctionsFromString(funcs);
```
## usb.usbFunctionsToString<sup>9+</sup>
usbFunctionsToString(funcs: FunctionType): string
Converts the USB function list in the numeric mask format to a string in Device mode.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------------------------------ | ---- | ----------------- |
| funcs | [FunctionType](#functiontype9) | Yes | USB function list in numeric mask format.|
**Return value**
| Type | Description |
| ------ | ------------------------------ |
| string | Function list in string format.|
**Example**
```js
let funcs = ACM | ECM;
let ret = usb.usbFunctionsToString(funcs);
```
## usb.setCurrentFunctions<sup>9+</sup>
setCurrentFunctions(funcs: FunctionType): Promise\<boolean\>
Sets the current USB function list in Device mode.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------------------------------ | ---- | ----------------- |
| funcs | [FunctionType](#functiontype9) | Yes | USB function list in numeric mask format.|
**Return value**
| Type | Description |
| ------------------ | ------------------------------------------------------------ |
| Promise\<boolean\> | Promise used to return the result. The value **true** indicates that the operation is successful, and the value **false** indicates the opposite.|
**Example**
```js
let funcs = HDC;
let ret = usb.setCurrentFunctions(funcs);
```
## usb.getCurrentFunctions<sup>9+</sup>
getCurrentFunctions(): FunctionType
Obtains the numeric mask combination for the USB function list in Device mode.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
**Return value**
| Type | Description |
| ------------------------------ | --------------------------------- |
| [FunctionType](#functiontype9) | Numeric mask combination for the USB function list.|
**Example**
```js
let ret = usb.getCurrentFunctions();
```
## usb.getPorts<sup>9+</sup>
getPorts(): Array\<USBPort\>
Obtains the list of all physical USB ports.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
**Return value**
| Type | Description |
| ----------------------------- | --------------------- |
| [Array\<USBPort\>](#usbport9) | List of physical USB ports.|
**Example**
```js
let ret = usb.getPorts();
```
## usb.getSupportedModes<sup>9+</sup>
getSupportedModes(portId: number): PortModeType
Obtains the mask combination for the supported mode list of a given USB port.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------- |
| portId | number | Yes | Port number.|
**Return value**
| Type | Description |
| ------------------------------ | -------------------------- |
| [PortModeType](#portmodetype9) | Mask combination for the supported mode list.|
**Example**
```js
let ret = usb.getSupportedModes(0);
```
## usb.setPortRoles<sup>9+</sup>
setPortRoles(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise\<boolean\>
Sets the role types supported by a specified port, which can be **powerRole** (for charging) and **dataRole** (for data transfer).
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name | Type | Mandatory| Description |
| --------- | -------------------------------- | ---- | ---------------- |
| portId | number | Yes | Port number. |
| powerRole | [PowerRoleType](#powerroletype9) | Yes | Role for charging. |
| dataRole | [DataRoleType](#dataroletype9) | Yes | Role for data transfer.|
**Return value**
| Type | Description |
| ------------------ | ------------------------------------------------------------ |
| Promise\<boolean\> | Promise used to return the result. The value **true** indicates that the operation is successful, and the value **false** indicates the opposite.|
**Example**
```js
let ret = usb.getSupportedModes(0);
```
## USBEndpoint
Represents the USB endpoint from which data is sent or received. You can obtain the USB endpoint through [USBInterface](#usbinterface).
**System capability**: SystemCapability.USB.USBManager
| Name | Type | Description |
| ------------- | ------------------------------------------- | ------------- |
| address | number | Endpoint address. |
| attributes | number | Endpoint attributes. |
| interval | number | Endpoint interval. |
| maxPacketSize | number | Maximum size of data packets on the endpoint. |
| direction | [USBRequestDirection](#usbrequestdirection) | Endpoint direction. |
| number | number | Endpoint number. |
| type | number | Endpoint type. |
| interfaceId | number | Unique ID of the interface to which the endpoint belongs.|
## USBInterface
Represents a USB interface. One [USBConfig](#usbconfig) can contain multiple **USBInterface** instances, each providing a specific function.
**System capability**: SystemCapability.USB.USBManager
| Name | Type | Description |
| ---------------- | ---------------------------------------- | --------------------- |
| id | number | Unique ID of the USB interface. |
| protocol | number | Interface protocol. |
| clazz | number | Device type. |
| subClass | number | Device subclass. |
| alternateSetting | number | Settings for alternating between descriptors of the same USB interface.|
| name | string | Interface name. |
| endpoints | Array&lt;[USBEndpoint](#usbendpoint)&gt; | Endpoints that belong to the USB interface. |
## USBConfig
Represents the USB configuration. One [USBDevice](#usbdevice) can contain multiple **USBConfig** instances.
**System capability**: SystemCapability.USB.USBManager
| Name | Type | Description |
| -------------- | ------------------------------------------------ | --------------- |
| id | number | Unique ID of the USB configuration. |
| attributes | number | Configuration attributes. |
| maxPower | number | Maximum power consumption, in mA. |
| name | string | Configuration name, which can be left empty. |
| isRemoteWakeup | boolean | Support for remote wakeup.|
| isSelfPowered | boolean | Support for independent power supplies.|
| interfaces | Array&nbsp;&lt;[USBInterface](#usbinterface)&gt; | Supported interface attributes. |
## USBDevice
Represents the USB device information.
**System capability**: SystemCapability.USB.USBManager
| Name | Type | Description |
| ---------------- | ------------------------------------ | ---------- |
| busNum | number | Bus address. |
| devAddress | number | Device address. |
| serial | string | Sequence number. |
| name | string | Device name. |
| manufacturerName | string | Device manufacturer. |
| productName | string | Product name. |
| version | string | Version number. |
| vendorId | number | Vendor ID. |
| productId | number | Product ID. |
| clazz | number | Device class. |
| subClass | number | Device subclass. |
| protocol | number | Device protocol code. |
| configs | Array&lt;[USBConfig](#usbconfig)&gt; | Device configuration descriptor information.|
## USBDevicePipe
Represents a USB device pipe, which is used to determine a USB device.
**System capability**: SystemCapability.USB.USBManager
| Name | Type | Description |
| ---------- | ------ | ----- |
| busNum | number | Bus address.|
| devAddress | number | Device address.|
## USBControlParams
Represents control transfer parameters.
**System capability**: SystemCapability.USB.USBManager
| Name | Type | Description |
| ------- | ----------------------------------------------- | ---------------- |
| request | number | Request type. |
| target | [USBRequestTargetType](#usbrequesttargettype) | Request target type. |
| reqType | [USBControlRequestType](#usbcontrolrequesttype) | Control request type. |
| value | number | Request parameter value. |
| index | number | Index of the request parameter value.|
| data | Uint8Array | Buffer for writing or reading data. |
## USBPort<sup>9+</sup>
Represents a USB port.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
| Name | Type | Description |
| -------------- | -------------------------------- | ----------------------------------- |
| id | number | Unique identifier of a USB port. |
| supportedModes | [PortModeType](#portmodetype9) | Numeric mask combination for the supported mode list.|
| status | [USBPortStatus](#usbportstatus9) | USB port role. |
## USBPortStatus<sup>9+</sup>
Enumerates USB port roles.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
| Name | Type| Description |
| ---------------- | -------- | ---------------------- |
| currentMode | number | Current USB mode. |
| currentPowerRole | number | Current power role. |
| currentDataRole | number | Current data role.|
## USBRequestTargetType
Enumerates request target types.
**System capability**: SystemCapability.USB.USBManager
| Name | Value | Description |
| ---------------------------- | ---- | ------ |
| USB_REQUEST_TARGET_DEVICE | 0 | Device.|
| USB_REQUEST_TARGET_INTERFACE | 1 | Interface.|
| USB_REQUEST_TARGET_ENDPOINT | 2 | Endpoint.|
| USB_REQUEST_TARGET_OTHER | 3 | Other.|
## USBControlRequestType
Enumerates control request types.
**System capability**: SystemCapability.USB.USBManager
| Name | Value | Description |
| ------------------------- | ---- | ------ |
| USB_REQUEST_TYPE_STANDARD | 0 | Standard.|
| USB_REQUEST_TYPE_CLASS | 1 | Class. |
| USB_REQUEST_TYPE_VENDOR | 2 | Vendor.|
## USBRequestDirection
Enumerates request directions.
**System capability**: SystemCapability.USB.USBManager
| Name | Value | Description |
| --------------------------- | ---- | ------------------------ |
| USB_REQUEST_DIR_TO_DEVICE | 0 | Request for writing data from the host to the device.|
| USB_REQUEST_DIR_FROM_DEVICE | 0x80 | Request for reading data from the device to the host.|
## FunctionType<sup>9+</sup>
Enumerates USB device function types.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
| Name | Value | Description |
| ------------ | ---- | ---------- |
| NONE | 0 | No function.|
| ACM | 1 | ACM function. |
| ECM | 2 | ECM function. |
| HDC | 4 | HDC function. |
| MTP | 8 | Not supported currently.|
| PTP | 16 | Not supported currently.|
| RNDIS | 32 | Not supported currently.|
| MIDI | 64 | Not supported currently.|
| AUDIO_SOURCE | 128 | Not supported currently.|
| NCM | 256 | Not supported currently.|
## PortModeType<sup>9+</sup>
Enumerates USB port mode types.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
| Name | Value | Description |
| --------- | ---- | ---------------------------------------------------- |
| NONE | 0 | None. |
| UFP | 1 | Upstream facing port, which functions as the sink of power supply. |
| DFP | 2 | Downstream facing port, which functions as the source of power supply. |
| DRP | 3 | Dynamic reconfiguration port (DRP), which can function as the DFP (host) or UFP (device). It is not supported currently.|
| NUM_MODES | 4 | Not supported currently. |
## PowerRoleType<sup>9+</sup>
Enumerates power role types.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
| Name | Value | Description |
| ------ | ---- | ---------- |
| NONE | 0 | None. |
| SOURCE | 1 | External power supply.|
| SINK | 2 | Internal power supply.|
## DataRoleType<sup>9+</sup>
Enumerates data role types.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
| Name | Value | Description |
| ------ | ---- | ------------ |
| NONE | 0 | None. |
| HOST | 1 | USB host.|
| DEVICE | 2 | USB device.|
# USB # USB
The USB module provides USB device management functions, including USB device list query, bulk data transfer, control transfer, and permission control. The USB module provides USB device management functions, including USB device list query, bulk data transfer, control transfer, and permission control on the host side as well as port management, and function switch and query on the device side.
> **NOTE** > **NOTE**
> >
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. > The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import ## Modules to Import
```js ```js
import usb from "@ohos.usb"; import usb from "@ohos.usbV9";
``` ```
## usb.getDevices ## usb.getDevices
getDevices(): Array&lt;Readonly&lt;USBDevice&gt;&gt; getDevices(): Array&lt;Readonly&lt;USBDevice&gt;&gt;
Obtains the USB device list. Obtains the list of USB devices connected to the host. If no device is connected, an empty list is returned.
**System capability**: SystemCapability.USB.USBManager **System capability**: SystemCapability.USB.USBManager
...@@ -24,7 +24,7 @@ Obtains the USB device list. ...@@ -24,7 +24,7 @@ Obtains the USB device list.
| Type | Description | | Type | Description |
| ---------------------------------------------------- | ------- | | ---------------------------------------------------- | ------- |
| Array&lt;Readonly&lt;[USBDevice](#usbdevice)&gt;&gt; | Device information list.| | Array&lt;Readonly&lt;[USBDevice](#usbdevice)&gt;&gt; | USB device list.|
**Example** **Example**
...@@ -89,7 +89,7 @@ connectDevice(device: USBDevice): Readonly&lt;USBDevicePipe&gt; ...@@ -89,7 +89,7 @@ connectDevice(device: USBDevice): Readonly&lt;USBDevicePipe&gt;
Connects to a USB device. Connects to a USB device.
Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list, and then call [usb.requestRight](#usbrequestright) to request the device access permission. Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list and device information, and then call [usb.requestRight](#usbrequestright) to request the device access permission.
**System capability**: SystemCapability.USB.USBManager **System capability**: SystemCapability.USB.USBManager
...@@ -105,10 +105,26 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi ...@@ -105,10 +105,26 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi
| -------- | -------- | | -------- | -------- |
| Readonly&lt;[USBDevicePipe](#usbdevicepipe)&gt; | USB device pipe for data transfer.| | Readonly&lt;[USBDevicePipe](#usbdevicepipe)&gt; | USB device pipe for data transfer.|
**Error codes**
For details about the error codes, see [USB Error Codes](../errorcodes/errorcode-usb.md).
| ID| Error Message|
| -------- | -------- |
| 14400001 |Permission denied. Need call requestRight to get permission. |
**Example** **Example**
```js ```js
let devicepipe= usb.connectDevice(device); let devicesList = usb.getDevices();
if (devicesList.length == 0) {
console.log(`device list is empty`);
return;
}
let device = devicesList[0];
usb.requestRight(device.name);
let devicepipe = usb.connectDevice(device);
console.log(`devicepipe = ${JSON.stringify(devicepipe)}`); console.log(`devicepipe = ${JSON.stringify(devicepipe)}`);
``` ```
...@@ -130,7 +146,7 @@ Checks whether the application has the permission to access the device. ...@@ -130,7 +146,7 @@ Checks whether the application has the permission to access the device.
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | The value **true** indicates that the application has the permission to access the device, and the value **false** indicates the opposite.| | boolean | Returns **true** if the application has the permission to access the device; returns **false** otherwise.|
**Example** **Example**
...@@ -144,7 +160,7 @@ console.log(bool); ...@@ -144,7 +160,7 @@ console.log(bool);
requestRight(deviceName: string): Promise&lt;boolean&gt; requestRight(deviceName: string): Promise&lt;boolean&gt;
Requests the temporary permission for the application to access the USB device. This API uses a promise to return the result. Requests the temporary permission for the application to access a USB device. This API uses a promise to return the result.
**System capability**: SystemCapability.USB.USBManager **System capability**: SystemCapability.USB.USBManager
...@@ -158,7 +174,7 @@ Requests the temporary permission for the application to access the USB device. ...@@ -158,7 +174,7 @@ Requests the temporary permission for the application to access the USB device.
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;boolean&gt; | Promise used to return the result. The value **true** indicates that the temporary device access permissions are granted, and the value **false** indicates the opposite.| | Promise&lt;boolean&gt; | Promise used to return the result. The value **true** indicates that the temporary device access permissions are granted; and the value **false** indicates the opposite.|
**Example** **Example**
...@@ -169,6 +185,70 @@ usb.requestRight(devicesName).then((ret) => { ...@@ -169,6 +185,70 @@ usb.requestRight(devicesName).then((ret) => {
}); });
``` ```
## usb.removeRight
removeRight(deviceName: string): boolean;
Removes the permission for the application to access a USB device.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| deviceName | string | Yes| Device name.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Permission removal result. The value **true** indicates that the access permission is removed successfully; and the value **false** indicates the opposite.|
**Example**
```js
let devicesName="1-1";
if (usb.removeRight(devicesName) {
console.log(`Succeed in removing right`);
}
```
## usb.addRight
addRight(bundleName: string, deviceName: string): boolean;
Adds the permission for the application to access a USB device.
[requestRight](#usbrequestright) triggers a dialog box to request for user authorization, whereas **addRight** adds the access permission directly without displaying a dialog box.
**System API**: This is a system API.
**System capability**: SystemCapability.USB.USBManager
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| deviceName | string | Yes| Device name.|
| bundleName | string | Yes| Bundle name of the application.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Permission addition result. The value **true** indicates that the access permission is added successfully; and the value **false** indicates the opposite.|
**Example**
```js
let devicesName = "1-1";
let bundleName = "com.example.hello";
if (usb.addRight(bundleName, devicesName) {
console.log(`Succeed in adding right`);
}
```
## usb.claimInterface ## usb.claimInterface
claimInterface(pipe: USBDevicePipe, iface: USBInterface, force?: boolean): number claimInterface(pipe: USBDevicePipe, iface: USBInterface, force?: boolean): number
...@@ -191,7 +271,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi ...@@ -191,7 +271,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | The value **0** indicates that the USB interface is successfully claimed, and an error code indicates the opposite.| | number | Returns **0** if the USB interface is successfully claimed; returns an error code otherwise.|
**Example** **Example**
...@@ -221,7 +301,7 @@ Before you do this, ensure that you have claimed the interface by calling [usb.c ...@@ -221,7 +301,7 @@ Before you do this, ensure that you have claimed the interface by calling [usb.c
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | The value **0** indicates that the USB interface is successfully released, and an error code indicates the opposite.| | number | Returns **0** if the USB interface is successfully released; returns an error code otherwise.|
**Example** **Example**
...@@ -251,7 +331,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi ...@@ -251,7 +331,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | The value **0** indicates that the USB configuration is successfully set, and an error code indicates the opposite.| | number | Returns **0** if the USB configuration is successfully set; returns an error code otherwise.|
**Example** **Example**
...@@ -281,7 +361,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi ...@@ -281,7 +361,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | The value **0** indicates that the USB interface is successfully set, and an error code indicates the opposite.| | number | Returns **0** if the USB interface is successfully set; returns an error code otherwise.|
**Example** **Example**
...@@ -310,7 +390,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi ...@@ -310,7 +390,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Uint8Array | The return value is the raw USB descriptor if the operation is successful, or **undefined** if the operation has failed.| | Uint8Array | Returns the raw USB descriptor if the operation is successful; returns **undefined** otherwise.|
**Example** **Example**
...@@ -338,7 +418,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi ...@@ -338,7 +418,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi
| Type | Description | | Type | Description |
| ------ | -------------------- | | ------ | -------------------- |
| number | The return value is the file descriptor of the USB device if the operation is successful, or **-1** if the operation has failed.| | number | Returns the file descriptor of the USB device if the operation is successful; returns **-1** otherwise.|
**Example** **Example**
...@@ -348,7 +428,7 @@ let ret = usb.getFileDescriptor(devicepipe); ...@@ -348,7 +428,7 @@ let ret = usb.getFileDescriptor(devicepipe);
## usb.controlTransfer ## usb.controlTransfer
controlTransfer(pipe: USBDevicePipe, contrlparam: USBControlParams, timeout?: number): Promise&lt;number&gt; controlTransfer(pipe: USBDevicePipe, controlparam: USBControlParams, timeout?: number): Promise&lt;number&gt;
Performs control transfer. Performs control transfer.
...@@ -361,7 +441,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi ...@@ -361,7 +441,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the USB device.| | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the USB device.|
| contrlparam | [USBControlParams](#usbcontrolparams) | Yes| Control transfer parameters.| | controlparam | [USBControlParams](#usbcontrolparams) | Yes| Control transfer parameters.|
| timeout | number | No| Timeout duration in ms. This parameter is optional. The default value is **0**, indicating no timeout.| | timeout | number | No| Timeout duration in ms. This parameter is optional. The default value is **0**, indicating no timeout.|
**Return value** **Return value**
...@@ -434,7 +514,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi ...@@ -434,7 +514,7 @@ Before you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB devi
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | The value **0** indicates that the USB device pipe is closed successfully, and an error code indicates the opposite.| | number | Returns **0** if the USB device pipe is closed successfully; returns an error code otherwise.|
**Example** **Example**
...@@ -443,7 +523,7 @@ let ret = usb.closePipe(devicepipe); ...@@ -443,7 +523,7 @@ let ret = usb.closePipe(devicepipe);
console.log(`closePipe = ${ret}`); console.log(`closePipe = ${ret}`);
``` ```
## usb.usbFunctionsFromString<sup>9+</sup> ## usb.usbFunctionsFromString
usbFunctionsFromString(funcs: string): number usbFunctionsFromString(funcs: string): number
...@@ -472,7 +552,7 @@ let funcs = "acm"; ...@@ -472,7 +552,7 @@ let funcs = "acm";
let ret = usb.usbFunctionsFromString(funcs); let ret = usb.usbFunctionsFromString(funcs);
``` ```
## usb.usbFunctionsToString<sup>9+</sup> ## usb.usbFunctionsToString
usbFunctionsToString(funcs: FunctionType): string usbFunctionsToString(funcs: FunctionType): string
...@@ -486,7 +566,7 @@ Converts the USB function list in the numeric mask format to a string in Device ...@@ -486,7 +566,7 @@ Converts the USB function list in the numeric mask format to a string in Device
| Name| Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| ------ | ------------------------------ | ---- | ----------------- | | ------ | ------------------------------ | ---- | ----------------- |
| funcs | [FunctionType](#functiontype9) | Yes | USB function list in numeric mask format.| | funcs | [FunctionType](#functiontype) | Yes | USB function list in numeric mask format.|
**Return value** **Return value**
...@@ -501,7 +581,7 @@ let funcs = ACM | ECM; ...@@ -501,7 +581,7 @@ let funcs = ACM | ECM;
let ret = usb.usbFunctionsToString(funcs); let ret = usb.usbFunctionsToString(funcs);
``` ```
## usb.setCurrentFunctions<sup>9+</sup> ## usb.setCurrentFunctions
setCurrentFunctions(funcs: FunctionType): Promise\<boolean\> setCurrentFunctions(funcs: FunctionType): Promise\<boolean\>
...@@ -515,7 +595,7 @@ Sets the current USB function list in Device mode. ...@@ -515,7 +595,7 @@ Sets the current USB function list in Device mode.
| Name| Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| ------ | ------------------------------ | ---- | ----------------- | | ------ | ------------------------------ | ---- | ----------------- |
| funcs | [FunctionType](#functiontype9) | Yes | USB function list in numeric mask format.| | funcs | [FunctionType](#functiontype) | Yes | USB function list in numeric mask format.|
**Return value** **Return value**
...@@ -530,7 +610,7 @@ let funcs = HDC; ...@@ -530,7 +610,7 @@ let funcs = HDC;
let ret = usb.setCurrentFunctions(funcs); let ret = usb.setCurrentFunctions(funcs);
``` ```
## usb.getCurrentFunctions<sup>9+</sup> ## usb.getCurrentFunctions
getCurrentFunctions(): FunctionType getCurrentFunctions(): FunctionType
...@@ -544,7 +624,7 @@ Obtains the numeric mask combination for the USB function list in Device mode. ...@@ -544,7 +624,7 @@ Obtains the numeric mask combination for the USB function list in Device mode.
| Type | Description | | Type | Description |
| ------------------------------ | --------------------------------- | | ------------------------------ | --------------------------------- |
| [FunctionType](#functiontype9) | Numeric mask combination for the USB function list.| | [FunctionType](#functiontype) | Numeric mask combination for the USB function list.|
**Example** **Example**
...@@ -552,7 +632,7 @@ Obtains the numeric mask combination for the USB function list in Device mode. ...@@ -552,7 +632,7 @@ Obtains the numeric mask combination for the USB function list in Device mode.
let ret = usb.getCurrentFunctions(); let ret = usb.getCurrentFunctions();
``` ```
## usb.getPorts<sup>9+</sup> ## usb.getPorts
getPorts(): Array\<USBPort\> getPorts(): Array\<USBPort\>
...@@ -566,7 +646,7 @@ Obtains the list of all physical USB ports. ...@@ -566,7 +646,7 @@ Obtains the list of all physical USB ports.
| Type | Description | | Type | Description |
| ----------------------------- | --------------------- | | ----------------------------- | --------------------- |
| [Array\<USBPort\>](#usbport9) | List of physical USB ports.| | [Array\<USBPort\>](#usbport) | List of physical USB ports.|
**Example** **Example**
...@@ -574,7 +654,7 @@ Obtains the list of all physical USB ports. ...@@ -574,7 +654,7 @@ Obtains the list of all physical USB ports.
let ret = usb.getPorts(); let ret = usb.getPorts();
``` ```
## usb.getSupportedModes<sup>9+</sup> ## usb.getSupportedModes
getSupportedModes(portId: number): PortModeType getSupportedModes(portId: number): PortModeType
...@@ -594,7 +674,7 @@ Obtains the mask combination for the supported mode list of a given USB port. ...@@ -594,7 +674,7 @@ Obtains the mask combination for the supported mode list of a given USB port.
| Type | Description | | Type | Description |
| ------------------------------ | -------------------------- | | ------------------------------ | -------------------------- |
| [PortModeType](#portmodetype9) | Mask combination for the supported mode list.| | [PortModeType](#portmodetype) | Mask combination for the supported mode list.|
**Example** **Example**
...@@ -602,7 +682,7 @@ Obtains the mask combination for the supported mode list of a given USB port. ...@@ -602,7 +682,7 @@ Obtains the mask combination for the supported mode list of a given USB port.
let ret = usb.getSupportedModes(0); let ret = usb.getSupportedModes(0);
``` ```
## usb.setPortRoles<sup>9+</sup> ## usb.setPortRoles
setPortRoles(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise\<boolean\> setPortRoles(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise\<boolean\>
...@@ -617,8 +697,8 @@ Sets the role types supported by a specified port, which can be **powerRole** (f ...@@ -617,8 +697,8 @@ Sets the role types supported by a specified port, which can be **powerRole** (f
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| --------- | -------------------------------- | ---- | ---------------- | | --------- | -------------------------------- | ---- | ---------------- |
| portId | number | Yes | Port number. | | portId | number | Yes | Port number. |
| powerRole | [PowerRoleType](#powerroletype9) | Yes | Role for charging. | | powerRole | [PowerRoleType](#powerroletype) | Yes | Role for charging. |
| dataRole | [DataRoleType](#dataroletype9) | Yes | Role for data transfer.| | dataRole | [DataRoleType](#dataroletype) | Yes | Role for data transfer.|
**Return value** **Return value**
...@@ -638,16 +718,16 @@ Represents the USB endpoint from which data is sent or received. You can obtain ...@@ -638,16 +718,16 @@ Represents the USB endpoint from which data is sent or received. You can obtain
**System capability**: SystemCapability.USB.USBManager **System capability**: SystemCapability.USB.USBManager
| Name | Type | Description | | Name | Type | Mandatory |Description |
| ------------- | ------------------------------------------- | ------------- | | ------------- | ------------------------------------------- | ------------- |------------- |
| address | number | Endpoint address. | | address | number | Yes|Endpoint address. |
| attributes | number | Endpoint attributes. | | attributes | number | Yes|Endpoint attributes. |
| interval | number | Endpoint interval. | | interval | number | Yes|Endpoint interval. |
| maxPacketSize | number | Maximum size of data packets on the endpoint. | | maxPacketSize | number | Yes|Maximum size of data packets on the endpoint. |
| direction | [USBRequestDirection](#usbrequestdirection) | Endpoint direction. | | direction | [USBRequestDirection](#usbrequestdirection) | Yes|Endpoint direction. |
| number | number | Endpoint number. | | number | number | Yes|Endpoint number. |
| type | number | Endpoint type. | | type | number | Yes|Endpoint type. |
| interfaceId | number | Unique ID of the interface to which the endpoint belongs.| | interfaceId | number | Yes|Unique ID of the interface to which the endpoint belongs.|
## USBInterface ## USBInterface
...@@ -655,15 +735,15 @@ Represents a USB interface. One [USBConfig](#usbconfig) can contain multiple **U ...@@ -655,15 +735,15 @@ Represents a USB interface. One [USBConfig](#usbconfig) can contain multiple **U
**System capability**: SystemCapability.USB.USBManager **System capability**: SystemCapability.USB.USBManager
| Name | Type | Description | | Name | Type | Mandatory |Description |
| ---------------- | ---------------------------------------- | --------------------- | | ---------------- | ---------------------------------------- | ------------- |--------------------- |
| id | number | Unique ID of the USB interface. | | id | number | Yes|Unique ID of the USB interface. |
| protocol | number | Interface protocol. | | protocol | number | Yes|Interface protocol. |
| clazz | number | Device type. | | clazz | number | Yes|Device type. |
| subClass | number | Device subclass. | | subClass | number | Yes|Device subclass. |
| alternateSetting | number | Settings for alternating between descriptors of the same USB interface.| | alternateSetting | number | Yes|Settings for alternating between descriptors of the same USB interface.|
| name | string | Interface name. | | name | string | Yes|Interface name. |
| endpoints | Array&lt;[USBEndpoint](#usbendpoint)&gt; | Endpoints that belong to the USB interface. | | endpoints | Array&lt;[USBEndpoint](#usbendpoint)&gt; | Yes|Endpoints that belong to the USB interface. |
## USBConfig ## USBConfig
...@@ -671,15 +751,15 @@ Represents the USB configuration. One [USBDevice](#usbdevice) can contain multip ...@@ -671,15 +751,15 @@ Represents the USB configuration. One [USBDevice](#usbdevice) can contain multip
**System capability**: SystemCapability.USB.USBManager **System capability**: SystemCapability.USB.USBManager
| Name | Type | Description | | Name | Type | Mandatory |Description |
| -------------- | ------------------------------------------------ | --------------- | | -------------- | ------------------------------------------------ | --------------- |--------------- |
| id | number | Unique ID of the USB configuration. | | id | number | Yes|Unique ID of the USB configuration. |
| attributes | number | Configuration attributes. | | attributes | number | Yes|Configuration attributes. |
| maxPower | number | Maximum power consumption, in mA. | | maxPower | number | Yes|Maximum power consumption, in mA. |
| name | string | Configuration name, which can be left empty. | | name | string | Yes|Configuration name, which can be left empty. |
| isRemoteWakeup | boolean | Support for remote wakeup.| | isRemoteWakeup | boolean | Yes|Support for remote wakeup.|
| isSelfPowered | boolean | Support for independent power supplies.| | isSelfPowered | boolean | Yes| Support for independent power supplies.|
| interfaces | Array&nbsp;&lt;[USBInterface](#usbinterface)&gt; | Supported interface attributes. | | interfaces | Array&nbsp;&lt;[USBInterface](#usbinterface)&gt; | Yes|Supported interface attributes. |
## USBDevice ## USBDevice
...@@ -687,21 +767,21 @@ Represents the USB device information. ...@@ -687,21 +767,21 @@ Represents the USB device information.
**System capability**: SystemCapability.USB.USBManager **System capability**: SystemCapability.USB.USBManager
| Name | Type | Description | | Name | Type | Mandatory |Description |
| ---------------- | ------------------------------------ | ---------- | | ---------------- | ------------------------------------ | ---------- |---------- |
| busNum | number | Bus address. | | busNum | number | Yes|Bus address. |
| devAddress | number | Device address. | | devAddress | number | Yes|Device address. |
| serial | string | Sequence number. | | serial | string | Yes|Sequence number. |
| name | string | Device name. | | name | string | Yes|Device name. |
| manufacturerName | string | Device manufacturer. | | manufacturerName | string | Yes| Device manufacturer. |
| productName | string | Product name. | | productName | string | Yes|Product name. |
| version | string | Version number. | | version | string | Yes|Version number. |
| vendorId | number | Vendor ID. | | vendorId | number | Yes|Vendor ID. |
| productId | number | Product ID. | | productId | number | Yes|Product ID. |
| clazz | number | Device class. | | clazz | number | Yes|Device class. |
| subClass | number | Device subclass. | | subClass | number | Yes|Device subclass. |
| protocol | number | Device protocol code. | | protocol | number | Yes|Device protocol code. |
| configs | Array&lt;[USBConfig](#usbconfig)&gt; | Device configuration descriptor information.| | configs | Array&lt;[USBConfig](#usbconfig)&gt; | Yes|Device configuration descriptor information.|
## USBDevicePipe ## USBDevicePipe
...@@ -709,10 +789,10 @@ Represents a USB device pipe, which is used to determine a USB device. ...@@ -709,10 +789,10 @@ Represents a USB device pipe, which is used to determine a USB device.
**System capability**: SystemCapability.USB.USBManager **System capability**: SystemCapability.USB.USBManager
| Name | Type | Description | | Name | Type | Mandatory |Description |
| ---------- | ------ | ----- | | ---------- | ------ | ----- |----- |
| busNum | number | Bus address.| | busNum | number |Yes| Bus address.|
| devAddress | number | Device address.| | devAddress | number |Yes| Device address.|
## USBControlParams ## USBControlParams
...@@ -720,16 +800,16 @@ Represents control transfer parameters. ...@@ -720,16 +800,16 @@ Represents control transfer parameters.
**System capability**: SystemCapability.USB.USBManager **System capability**: SystemCapability.USB.USBManager
| Name | Type | Description | | Name | Type | Mandatory |Description |
| ------- | ----------------------------------------------- | ---------------- | | ------- | ----------------------------------------------- | ---------------- |---------------- |
| request | number | Request type. | | request | number | Yes |Request type. |
| target | [USBRequestTargetType](#usbrequesttargettype) | Request target type. | | target | [USBRequestTargetType](#usbrequesttargettype) | Yes |Request target type. |
| reqType | [USBControlRequestType](#usbcontrolrequesttype) | Control request type. | | reqType | [USBControlRequestType](#usbcontrolrequesttype) | Yes |Control request type. |
| value | number | Request parameter value. | | value | number | Yes |Request parameter value. |
| index | number | Index of the request parameter value.| | index | number | Yes |Index of the request parameter value.|
| data | Uint8Array | Buffer for writing or reading data. | | data | Uint8Array | Yes |Buffer for writing or reading data. |
## USBPort<sup>9+</sup> ## USBPort
Represents a USB port. Represents a USB port.
...@@ -737,13 +817,13 @@ Represents a USB port. ...@@ -737,13 +817,13 @@ Represents a USB port.
**System capability**: SystemCapability.USB.USBManager **System capability**: SystemCapability.USB.USBManager
| Name | Type | Description | | Name | Type | Mandatory |Description |
| -------------- | -------------------------------- | ----------------------------------- | | -------------- | ------------------------------- | ------------------- |------------------------ |
| id | number | Unique identifier of a USB port. | | id | number | Yes |Unique identifier of a USB port. |
| supportedModes | [PortModeType](#portmodetype9) | Numeric mask combination for the supported mode list.| | supportedModes | [PortModeType](#portmodetype) | Yes |Numeric mask combination for the supported mode list.|
| status | [USBPortStatus](#usbportstatus9) | USB port role. | | status | [USBPortStatus](#usbportstatus) | Yes |USB port role. |
## USBPortStatus<sup>9+</sup> ## USBPortStatus
Enumerates USB port roles. Enumerates USB port roles.
...@@ -751,11 +831,11 @@ Enumerates USB port roles. ...@@ -751,11 +831,11 @@ Enumerates USB port roles.
**System capability**: SystemCapability.USB.USBManager **System capability**: SystemCapability.USB.USBManager
| Name | Type| Description | | Name | Type| Mandatory |Description |
| ---------------- | -------- | ---------------------- | | ---------------- | -------- | ---------------- |---------------------- |
| currentMode | number | Current USB mode. | | currentMode | number | Yes|Current USB mode. |
| currentPowerRole | number | Current power role. | | currentPowerRole | number | Yes |Current power role. |
| currentDataRole | number | Current data role.| | currentDataRole | number | Yes |Current data role.|
## USBRequestTargetType ## USBRequestTargetType
...@@ -765,10 +845,10 @@ Enumerates request target types. ...@@ -765,10 +845,10 @@ Enumerates request target types.
| Name | Value | Description | | Name | Value | Description |
| ---------------------------- | ---- | ------ | | ---------------------------- | ---- | ------ |
| USB_REQUEST_TARGET_DEVICE | 0 | Device.| | USB_REQUEST_TARGET_DEVICE | 0 | Device|
| USB_REQUEST_TARGET_INTERFACE | 1 | Interface.| | USB_REQUEST_TARGET_INTERFACE | 1 | Interface|
| USB_REQUEST_TARGET_ENDPOINT | 2 | Endpoint.| | USB_REQUEST_TARGET_ENDPOINT | 2 | Endpoint|
| USB_REQUEST_TARGET_OTHER | 3 | Other.| | USB_REQUEST_TARGET_OTHER | 3 | Other|
## USBControlRequestType ## USBControlRequestType
...@@ -778,9 +858,9 @@ Enumerates control request types. ...@@ -778,9 +858,9 @@ Enumerates control request types.
| Name | Value | Description | | Name | Value | Description |
| ------------------------- | ---- | ------ | | ------------------------- | ---- | ------ |
| USB_REQUEST_TYPE_STANDARD | 0 | Standard.| | USB_REQUEST_TYPE_STANDARD | 0 | Standard|
| USB_REQUEST_TYPE_CLASS | 1 | Class. | | USB_REQUEST_TYPE_CLASS | 1 | Class |
| USB_REQUEST_TYPE_VENDOR | 2 | Vendor.| | USB_REQUEST_TYPE_VENDOR | 2 | Vendor|
## USBRequestDirection ## USBRequestDirection
...@@ -793,7 +873,7 @@ Enumerates request directions. ...@@ -793,7 +873,7 @@ Enumerates request directions.
| USB_REQUEST_DIR_TO_DEVICE | 0 | Request for writing data from the host to the device.| | USB_REQUEST_DIR_TO_DEVICE | 0 | Request for writing data from the host to the device.|
| USB_REQUEST_DIR_FROM_DEVICE | 0x80 | Request for reading data from the device to the host.| | USB_REQUEST_DIR_FROM_DEVICE | 0x80 | Request for reading data from the device to the host.|
## FunctionType<sup>9+</sup> ## FunctionType
Enumerates USB device function types. Enumerates USB device function types.
...@@ -814,7 +894,7 @@ Enumerates USB device function types. ...@@ -814,7 +894,7 @@ Enumerates USB device function types.
| AUDIO_SOURCE | 128 | Not supported currently.| | AUDIO_SOURCE | 128 | Not supported currently.|
| NCM | 256 | Not supported currently.| | NCM | 256 | Not supported currently.|
## PortModeType<sup>9+</sup> ## PortModeType
Enumerates USB port mode types. Enumerates USB port mode types.
...@@ -830,7 +910,7 @@ Enumerates USB port mode types. ...@@ -830,7 +910,7 @@ Enumerates USB port mode types.
| DRP | 3 | Dynamic reconfiguration port (DRP), which can function as the DFP (host) or UFP (device). It is not supported currently.| | DRP | 3 | Dynamic reconfiguration port (DRP), which can function as the DFP (host) or UFP (device). It is not supported currently.|
| NUM_MODES | 4 | Not supported currently. | | NUM_MODES | 4 | Not supported currently. |
## PowerRoleType<sup>9+</sup> ## PowerRoleType
Enumerates power role types. Enumerates power role types.
...@@ -844,7 +924,7 @@ Enumerates power role types. ...@@ -844,7 +924,7 @@ Enumerates power role types.
| SOURCE | 1 | External power supply.| | SOURCE | 1 | External power supply.|
| SINK | 2 | Internal power supply.| | SINK | 2 | Internal power supply.|
## DataRoleType<sup>9+</sup> ## DataRoleType
Enumerates data role types. Enumerates data role types.
......
# USB Error Codes
## 14400001 USB Device Connection Denied
**Error Message**
Permission denied. Need call requestRight to get permission.
**Description**
This error code is reported if a certain API of the USB module is called but the required permission is not granted.
**Possible Causes**
The permission to access the USB device is not granted.
**Solution**
Call **requestRight** to request for the permission to access the USB device.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册