js-apis-usb.md 29.4 KB
Newer Older
S
shawn_he 已提交
1
# USB
Z
zengyawen 已提交
2

S
shawn_he 已提交
3
The USB module provides USB device management functions, including USB device list query, bulk data transfer, control transfer, and permission control.
S
shawn_he 已提交
4

S
shawn_he 已提交
5
>  **NOTE**
S
shawn_he 已提交
6
> 
S
shawn_he 已提交
7
> 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.
Z
zengyawen 已提交
8

S
shawn_he 已提交
9
## Modules to Import
Z
zengyawen 已提交
10

S
shawn_he 已提交
11
```js
Z
zengyawen 已提交
12 13 14
import usb from "@ohos.usb";
```

S
shawn_he 已提交
15
## usb.getDevices
Z
zengyawen 已提交
16

S
shawn_he 已提交
17
getDevices(): Array<Readonly<USBDevice>>
Z
zengyawen 已提交
18 19 20

Obtains the USB device list.

S
shawn_he 已提交
21 22
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
23 24 25 26 27
**Return value**

| Type                                                  | Description     |
| ---------------------------------------------------- | ------- |
| Array<Readonly<[USBDevice](#usbdevice)>> | Device information list.|
S
shawn_he 已提交
28

S
shawn_he 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
**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,
              },
            ],
          },
        ],
      },
    ],
  },
]
```
S
shawn_he 已提交
85 86 87

## usb.connectDevice

S
shawn_he 已提交
88
connectDevice(device: USBDevice): Readonly<USBDevicePipe>
Z
zengyawen 已提交
89 90 91

Connects to a USB device.

S
shawn_he 已提交
92 93 94 95
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

S
shawn_he 已提交
96
**Parameters**
97 98
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
S
shawn_he 已提交
99
  | device | [USBDevice](#usbdevice) | Yes| USB device information.|
S
shawn_he 已提交
100

S
shawn_he 已提交
101
**Return value**
102 103
  | Type| Description|
  | -------- | -------- |
S
shawn_he 已提交
104
  | Readonly<[USBDevicePipe](#usbdevicepipe)> | USB device pipe for data transfer.|
S
shawn_he 已提交
105

S
shawn_he 已提交
106
**Example**
S
shawn_he 已提交
107

S
shawn_he 已提交
108 109 110 111
```js
let devicepipe= usb.connectDevice(device);
console.log(`devicepipe = ${JSON.stringify(devicepipe)}`);
```
S
shawn_he 已提交
112 113 114

## usb.hasRight

S
shawn_he 已提交
115
hasRight(deviceName: string): boolean
S
shawn_he 已提交
116 117 118 119 120

Checks whether the application has the permission to access the device.

**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
121
**Parameters**
122 123
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
S
shawn_he 已提交
124
  | deviceName | string | Yes| Device name to set.|
S
shawn_he 已提交
125

S
shawn_he 已提交
126
**Return value**
127 128
  | Type| Description|
  | -------- | -------- |
S
shawn_he 已提交
129
  | boolean | Returns **true** if the application has the permission to access the device; returns **false** otherwise.|
S
shawn_he 已提交
130

S
shawn_he 已提交
131
**Example**
S
shawn_he 已提交
132

S
shawn_he 已提交
133 134 135 136 137
```js
let devicesName="1-1";
let bool = usb.hasRight(devicesName);
console.log(bool);
```
S
shawn_he 已提交
138 139 140

## usb.requestRight

S
shawn_he 已提交
141
requestRight(deviceName: string): Promise<boolean>
Z
zengyawen 已提交
142 143 144

Requests the temporary permission for the application to access the USB device.

S
shawn_he 已提交
145 146
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
147
**Parameters**
148 149
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
S
shawn_he 已提交
150
  | deviceName | string | Yes| Device name to set.|
S
shawn_he 已提交
151

S
shawn_he 已提交
152
**Return value**
153 154
  | Type| Description|
  | -------- | -------- |
S
shawn_he 已提交
155
  | Promise<boolean> | Returns **true** if the temporary device access permissions are granted; returns **false** otherwise.|
S
shawn_he 已提交
156

S
shawn_he 已提交
157
**Example**
S
shawn_he 已提交
158

S
shawn_he 已提交
159 160 161 162 163 164
```js
let devicesName="1-1";
usb.requestRight(devicesName).then((ret) => {
  console.log(`requestRight = ${JSON.stringify(ret)}`);
});
```
S
shawn_he 已提交
165 166 167

## usb.claimInterface

S
shawn_he 已提交
168
claimInterface(pipe: USBDevicePipe, iface: USBInterface, force?: boolean): number
Z
zengyawen 已提交
169 170 171

Claims a USB interface.

S
shawn_he 已提交
172 173 174 175
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

S
shawn_he 已提交
176
**Parameters**
177 178
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
S
shawn_he 已提交
179 180 181
  | 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.|
S
shawn_he 已提交
182

S
shawn_he 已提交
183
**Return value**
184 185
  | Type| Description|
  | -------- | -------- |
S
shawn_he 已提交
186
  | number | Returns **0** if the USB interface is successfully claimed; returns an error code otherwise.|
S
shawn_he 已提交
187

S
shawn_he 已提交
188
**Example**
S
shawn_he 已提交
189

S
shawn_he 已提交
190 191 192 193
```js
let ret = usb.claimInterface(devicepipe, interfaces);
console.log(`claimInterface = ${ret}`);
```
S
shawn_he 已提交
194 195 196

## usb.releaseInterface

S
shawn_he 已提交
197
releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number
Z
zengyawen 已提交
198 199 200

Releases a USB interface.

S
shawn_he 已提交
201 202 203 204
Before you do this, ensure that you have claimed the interface by calling [usb.claimInterface](#usbclaiminterface).

**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
205
**Parameters**
206 207
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
S
shawn_he 已提交
208 209
  | 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.|
S
shawn_he 已提交
210

S
shawn_he 已提交
211
**Return value**
212 213
  | Type| Description|
  | -------- | -------- |
S
shawn_he 已提交
214
  | number | Returns **0** if the USB interface is successfully released; returns an error code otherwise.|
S
shawn_he 已提交
215

S
shawn_he 已提交
216
**Example**
S
shawn_he 已提交
217

S
shawn_he 已提交
218 219 220 221
```js
let ret = usb.releaseInterface(devicepipe, interfaces);
console.log(`releaseInterface = ${ret}`);
```
S
shawn_he 已提交
222 223 224

## usb.setConfiguration

S
shawn_he 已提交
225
setConfiguration(pipe: USBDevicePipe, config: USBConfig): number
Z
zengyawen 已提交
226 227 228

Sets the device configuration.

S
shawn_he 已提交
229 230 231 232
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

S
shawn_he 已提交
233
**Parameters**
234 235
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
S
shawn_he 已提交
236 237
  | 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.|
S
shawn_he 已提交
238

S
shawn_he 已提交
239
**Return value**
240 241
  | Type| Description|
  | -------- | -------- |
S
shawn_he 已提交
242
  | number | Returns **0** if the USB configuration is successfully set; returns an error code otherwise.|
S
shawn_he 已提交
243

S
shawn_he 已提交
244
**Example**
S
shawn_he 已提交
245

S
shawn_he 已提交
246 247 248 249
```js
let ret = usb.setConfiguration(devicepipe, config);
console.log(`setConfiguration = ${ret}`);
```
S
shawn_he 已提交
250 251 252

## usb.setInterface

S
shawn_he 已提交
253
setInterface(pipe: USBDevicePipe, iface: USBInterface): number
Z
zengyawen 已提交
254 255 256

Sets a USB interface.

S
shawn_he 已提交
257
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.
S
shawn_he 已提交
258 259 260

**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
261
**Parameters**
S
shawn_he 已提交
262

S
shawn_he 已提交
263 264 265 266
| 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. |
S
shawn_he 已提交
267

S
shawn_he 已提交
268
**Return value**
269 270
  | Type| Description|
  | -------- | -------- |
S
shawn_he 已提交
271
  | number | Returns **0** if the USB interface is successfully set; returns an error code otherwise.|
S
shawn_he 已提交
272

S
shawn_he 已提交
273
**Example**
S
shawn_he 已提交
274

S
shawn_he 已提交
275 276 277 278
```js
let ret = usb.setInterface(devicepipe, interfaces);
console.log(`setInterface = ${ret}`);
```
S
shawn_he 已提交
279 280 281

## usb.getRawDescriptor

S
shawn_he 已提交
282
getRawDescriptor(pipe: USBDevicePipe): Uint8Array
Z
zengyawen 已提交
283 284 285

Obtains the raw USB descriptor.

S
shawn_he 已提交
286 287 288 289
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

S
shawn_he 已提交
290
**Parameters**
S
shawn_he 已提交
291

292 293
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
S
shawn_he 已提交
294
  | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.|
S
shawn_he 已提交
295

S
shawn_he 已提交
296 297 298 299
**Return value**
| Type| Description|
| -------- | -------- |
| Uint8Array | Returns the raw USB descriptor if the operation is successful; returns **undefined** otherwise.|
S
shawn_he 已提交
300

S
shawn_he 已提交
301
**Example**
S
shawn_he 已提交
302

S
shawn_he 已提交
303 304 305
```js
let ret = usb.getRawDescriptor(devicepipe);
```
S
shawn_he 已提交
306 307 308

## usb.getFileDescriptor

S
shawn_he 已提交
309
getFileDescriptor(pipe: USBDevicePipe): number
Z
zengyawen 已提交
310 311 312

Obtains the file descriptor.

S
shawn_he 已提交
313 314 315 316
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

S
shawn_he 已提交
317
**Parameters**
318 319
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
S
shawn_he 已提交
320
  | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.|
S
shawn_he 已提交
321

S
shawn_he 已提交
322
**Return value**
S
shawn_he 已提交
323

S
shawn_he 已提交
324 325 326
| Type    | Description                  |
| ------ | -------------------- |
| number | Returns the file descriptor of the USB device if the operation is successful; returns **-1** otherwise.|
S
shawn_he 已提交
327

S
shawn_he 已提交
328
**Example**
S
shawn_he 已提交
329

S
shawn_he 已提交
330 331 332
```js
let ret = usb.getFileDescriptor(devicepipe);
```
S
shawn_he 已提交
333 334 335

## usb.controlTransfer

S
shawn_he 已提交
336
controlTransfer(pipe: USBDevicePipe, contrlparam: USBControlParams, timeout?: number): Promise<number>
Z
zengyawen 已提交
337 338 339

Performs control transfer.

S
shawn_he 已提交
340 341 342 343
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

S
shawn_he 已提交
344
**Parameters**
345 346
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
S
shawn_he 已提交
347 348 349
  | 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. The default value is **0**, indicating no timeout.|
S
shawn_he 已提交
350

S
shawn_he 已提交
351
**Return value**
352 353
  | Type| Description|
  | -------- | -------- |
S
shawn_he 已提交
354
  | Promise<number> | Returns the size of the transmitted or received data block if the control transfer is successful; returns **-1** if an exception occurs.|
S
shawn_he 已提交
355

S
shawn_he 已提交
356
**Example**
S
shawn_he 已提交
357

S
shawn_he 已提交
358 359 360 361 362
```js
usb.controlTransfer(devicepipe, USBControlParams).then((ret) => {
 console.log(`controlTransfer = ${JSON.stringify(ret)}`);
})
```
S
shawn_he 已提交
363 364 365

## usb.bulkTransfer

S
shawn_he 已提交
366
bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, timeout?: number): Promise<number>
Z
zengyawen 已提交
367 368 369

Performs bulk transfer.

S
shawn_he 已提交
370
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.
S
shawn_he 已提交
371 372 373

**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
374
**Parameters**
375 376
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
S
shawn_he 已提交
377 378 379 380
  | 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. The default value is **0**, indicating no timeout.|
S
shawn_he 已提交
381

S
shawn_he 已提交
382
**Return value**
383 384
  | Type| Description|
  | -------- | -------- |
S
shawn_he 已提交
385
  | Promise<number> | Returns the size of the transmitted or received data block if the control transfer is successful; returns **-1** if an exception occurs.|
S
shawn_he 已提交
386

S
shawn_he 已提交
387
**Example**
S
shawn_he 已提交
388

S
shawn_he 已提交
389 390 391 392 393 394 395 396
```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)}`);
});
```
S
shawn_he 已提交
397 398 399

## usb.closePipe

S
shawn_he 已提交
400
closePipe(pipe: USBDevicePipe): number
Z
zengyawen 已提交
401 402 403

Closes a USB device pipe.

S
shawn_he 已提交
404 405 406 407
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

S
shawn_he 已提交
408
**Parameters**
409 410
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
S
shawn_he 已提交
411
  | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe.|
S
shawn_he 已提交
412

S
shawn_he 已提交
413
**Return value**
414 415
  | Type| Description|
  | -------- | -------- |
S
shawn_he 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  | number | Returns **0** if the USB device pipe is closed successfully; returns an error code otherwise.|

**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**
S
shawn_he 已提交
465

S
shawn_he 已提交
466 467 468
| Name| Type                          | Mandatory| Description             |
| ------ | ------------------------------ | ---- | ----------------- |
| funcs  | [FunctionType](#functiontype9) | Yes  | USB function list in numeric mask format.|
S
shawn_he 已提交
469

S
shawn_he 已提交
470
**Return value**
S
shawn_he 已提交
471

S
shawn_he 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
| 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);
```
S
shawn_he 已提交
613 614 615 616 617

## USBEndpoint

Represents the USB endpoint from which data is sent or received. You can obtain the USB endpoint through [USBInterface](#usbinterface).

S
shawn_he 已提交
618 619
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
620 621 622 623 624 625 626 627 628 629
| 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.|
S
shawn_he 已提交
630 631 632 633 634

## USBInterface

Represents a USB interface. One [USBConfig](#usbconfig) can contain multiple **USBInterface** instances, each providing a specific function.

S
shawn_he 已提交
635 636
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
637 638 639 640 641 642 643 644 645
| 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.          |
S
shawn_he 已提交
646 647 648 649 650

## USBConfig

Represents the USB configuration. One [USBDevice](#usbdevice) can contain multiple **USBConfig** instances.

S
shawn_he 已提交
651 652
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
653 654 655 656 657 658 659 660 661
| 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.     |
S
shawn_he 已提交
662 663 664

## USBDevice

S
shawn_he 已提交
665
Represents the USB device information.
S
shawn_he 已提交
666 667

**System capability**: SystemCapability.USB.USBManager
S
shawn_he 已提交
668

S
shawn_he 已提交
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
| 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.|
S
shawn_he 已提交
684 685

## USBDevicePipe
Z
zengyawen 已提交
686 687 688

Represents a USB device pipe, which is used to determine a USB device.

S
shawn_he 已提交
689 690
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
691 692 693 694
| Name        | Type  | Description   |
| ---------- | ------ | ----- |
| busNum     | number | Bus address.|
| devAddress | number | Device address.|
S
shawn_he 已提交
695 696

## USBControlParams
Z
zengyawen 已提交
697 698 699

Represents control transfer parameters.

S
shawn_he 已提交
700 701
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
702 703 704 705 706 707 708 709
| 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.    |
S
shawn_he 已提交
710

S
shawn_he 已提交
711
## USBPort<sup>9+</sup>
S
shawn_he 已提交
712

S
shawn_he 已提交
713 714 715
Represents a USB port.

**System API**: This is a system API.
S
shawn_he 已提交
716

S
shawn_he 已提交
717 718 719 720 721 722 723 724 725 726 727 728 729
**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.
S
shawn_he 已提交
730

S
shawn_he 已提交
731
**System capability**: SystemCapability.USB.USBManager
S
shawn_he 已提交
732

S
shawn_he 已提交
733 734 735 736 737
| Name            | Type| Description                  |
| ---------------- | -------- | ---------------------- |
| currentMode      | number   | Current USB mode.       |
| currentPowerRole | number   | Current power role.    |
| currentDataRole  | number   | Current data role.|
S
shawn_he 已提交
738

S
shawn_he 已提交
739 740 741 742 743 744 745 746 747 748 749 750
## 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.|
S
shawn_he 已提交
751 752

## USBControlRequestType
Z
zengyawen 已提交
753 754 755

Enumerates control request types.

S
shawn_he 已提交
756 757
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
758 759 760 761 762
| Name                     | Value  | Description  |
| ------------------------- | ---- | ------ |
| USB_REQUEST_TYPE_STANDARD | 0    | Standard.|
| USB_REQUEST_TYPE_CLASS    | 1    | Class.  |
| USB_REQUEST_TYPE_VENDOR   | 2    | Vendor.|
S
shawn_he 已提交
763 764

## USBRequestDirection
Z
zengyawen 已提交
765 766 767

Enumerates request directions.

S
shawn_he 已提交
768 769
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
770 771 772 773
| 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.|
S
shawn_he 已提交
774 775 776

## FunctionType<sup>9+</sup>

S
shawn_he 已提交
777
Enumerates USB device function types.
S
shawn_he 已提交
778

S
shawn_he 已提交
779
**System API**: This is a system API.
S
shawn_he 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

**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.

S
shawn_he 已提交
800
**System API**: This is a system API.
S
shawn_he 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815

**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.

S
shawn_he 已提交
816
**System API**: This is a system API.
S
shawn_he 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829

**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.

S
shawn_he 已提交
830
**System API**: This is a system API.
S
shawn_he 已提交
831 832 833 834 835 836 837 838

**System capability**: SystemCapability.USB.USBManager

| Name  | Value  | Description        |
| ------ | ---- | ------------ |
| NONE   | 0    | None.        |
| HOST   | 1    | USB host.|
| DEVICE | 2    | USB device.|