1e41f4b71Sopenharmony_ci# @ohos.usb (USB Manager) (No Longer Maintained) (System API)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **usb** module provides USB device management functions, including USB device list query, bulk data transfer, control transfer, and permission control.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci>  **NOTE**
6e41f4b71Sopenharmony_ci> 
7e41f4b71Sopenharmony_ci> 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.
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> The APIs provided by this module are no longer maintained since API version 9. You are advised to use [`@ohos.usbManager`](js-apis-usbManager.md).
10e41f4b71Sopenharmony_ci>
11e41f4b71Sopenharmony_ci> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.usb (USB Manager) (No Longer Maintained)](js-apis-usb-deprecated.md).
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci## Modules to Import
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci```js
16e41f4b71Sopenharmony_ciimport usb from "@ohos.usb";
17e41f4b71Sopenharmony_ciimport { BusinessError } from '@ohos.base';
18e41f4b71Sopenharmony_ci```
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci## usb.usbFunctionsFromString<sup>9+</sup>
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ciusbFunctionsFromString(funcs: string): number
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ciConverts the USB function list in the string format to a numeric mask in Device mode.
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci**System API**: This is a system API.
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci**Parameters**
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description                  |
33e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------------------- |
34e41f4b71Sopenharmony_ci| funcs  | string | Yes  | Function list in string format.|
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci**Return value**
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci| Type  | Description              |
39e41f4b71Sopenharmony_ci| ------ | ------------------ |
40e41f4b71Sopenharmony_ci| number | Function list in numeric mask format.|
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci**Example**
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci```js
45e41f4b71Sopenharmony_cilet funcs = "acm";
46e41f4b71Sopenharmony_cilet ret = usb.usbFunctionsFromString(funcs);
47e41f4b71Sopenharmony_ci```
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci## usb.usbFunctionsToString<sup>9+</sup>
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ciusbFunctionsToString(funcs: FunctionType): string
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ciConverts the USB function list in the numeric mask format to a string in Device mode.
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci**System API**: This is a system API.
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci**Parameters**
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci| Name| Type                          | Mandatory| Description             |
62e41f4b71Sopenharmony_ci| ------ | ------------------------------ | ---- | ----------------- |
63e41f4b71Sopenharmony_ci| funcs  | [FunctionType](#functiontype9) | Yes  | USB function list in numeric mask format.|
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci**Return value**
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci| Type  | Description                          |
68e41f4b71Sopenharmony_ci| ------ | ------------------------------ |
69e41f4b71Sopenharmony_ci| string | Function list in string format.|
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci**Example**
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci```js
74e41f4b71Sopenharmony_cilet funcs = usb.FunctionType.ACM | usb.FunctionType.ECM;
75e41f4b71Sopenharmony_cilet ret = usb.usbFunctionsToString(funcs);
76e41f4b71Sopenharmony_ci```
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci## usb.setCurrentFunctions<sup>9+</sup>
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_cisetCurrentFunctions(funcs: FunctionType): Promise\<boolean\>
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ciSets the current USB function list in Device mode.
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci**System API**: This is a system API.
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**Parameters**
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci| Name| Type                          | Mandatory| Description             |
91e41f4b71Sopenharmony_ci| ------ | ------------------------------ | ---- | ----------------- |
92e41f4b71Sopenharmony_ci| funcs  | [FunctionType](#functiontype9) | Yes  | USB function list in numeric mask format.|
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci**Return value**
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci| Type              | Description                                                        |
97e41f4b71Sopenharmony_ci| ------------------ | ------------------------------------------------------------ |
98e41f4b71Sopenharmony_ci| Promise\<boolean\> | Promise used to return the result. The value **true** indicates that the operation is successful, and the value **false** indicates the opposite.|
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci**Example**
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci```js
103e41f4b71Sopenharmony_cilet funcs : number = usb.FunctionType.HDC;
104e41f4b71Sopenharmony_ciusb.setCurrentFunctions(funcs).then(() => {
105e41f4b71Sopenharmony_ci    console.info('usb setCurrentFunctions successfully.');
106e41f4b71Sopenharmony_ci}).catch((err : BusinessError) => {
107e41f4b71Sopenharmony_ci    console.error('usb setCurrentFunctions failed: ' + err.code + ' message: ' + err.message);
108e41f4b71Sopenharmony_ci});
109e41f4b71Sopenharmony_ci```
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci## usb.getCurrentFunctions<sup>9+</sup>
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_cigetCurrentFunctions(): FunctionType
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ciObtains the numeric mask combination for the USB function list in Device mode.
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci**System API**: This is a system API.
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci**Return value**
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci| Type                          | Description                             |
124e41f4b71Sopenharmony_ci| ------------------------------ | --------------------------------- |
125e41f4b71Sopenharmony_ci| [FunctionType](#functiontype9) | Numeric mask combination for the USB function list.|
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci**Example**
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci```js
130e41f4b71Sopenharmony_cilet ret = usb.getCurrentFunctions();
131e41f4b71Sopenharmony_ci```
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci## usb.getPorts<sup>9+</sup>
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_cigetPorts(): Array\<USBPort\>
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ciObtains the list of all physical USB ports.
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci**System API**: This is a system API.
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci**Return value**
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci| Type                         | Description                 |
146e41f4b71Sopenharmony_ci| ----------------------------- | --------------------- |
147e41f4b71Sopenharmony_ci| [Array\<USBPort\>](#usbport9) | List of physical USB ports.|
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci**Example**
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci```js
152e41f4b71Sopenharmony_cilet ret = usb.getPorts();
153e41f4b71Sopenharmony_ci```
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci## usb.getSupportedModes<sup>9+</sup>
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_cigetSupportedModes(portId: number): PortModeType
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ciObtains the mask combination for the supported mode list of a given USB port.
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci**System API**: This is a system API.
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci**Parameters**
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description    |
168e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------- |
169e41f4b71Sopenharmony_ci| portId | number | Yes  | Port number.|
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci**Return value**
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci| Type                          | Description                      |
174e41f4b71Sopenharmony_ci| ------------------------------ | -------------------------- |
175e41f4b71Sopenharmony_ci| [PortModeType](#portmodetype9) | Mask combination for the supported mode list.|
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci**Example**
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci```js
180e41f4b71Sopenharmony_cilet ret = usb.getSupportedModes(0);
181e41f4b71Sopenharmony_ci```
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ci## usb.setPortRoles<sup>9+</sup>
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_cisetPortRoles(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise\<boolean\>
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ciSets the role types supported by a specified port, which can be **powerRole** (for charging) and **dataRole** (for data transfer).
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci**System API**: This is a system API.
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci**Parameters**
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ci| Name   | Type                            | Mandatory| Description            |
196e41f4b71Sopenharmony_ci| --------- | -------------------------------- | ---- | ---------------- |
197e41f4b71Sopenharmony_ci| portId    | number                           | Yes  | Port number.        |
198e41f4b71Sopenharmony_ci| powerRole | [PowerRoleType](#powerroletype9) | Yes  | Role for charging.    |
199e41f4b71Sopenharmony_ci| dataRole  | [DataRoleType](#dataroletype9)   | Yes  | Role for data transfer.|
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ci**Return value**
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ci| Type              | Description                                                        |
204e41f4b71Sopenharmony_ci| ------------------ | ------------------------------------------------------------ |
205e41f4b71Sopenharmony_ci| Promise\<boolean\> | Promise used to return the result. The value **true** indicates that the operation is successful, and the value **false** indicates the opposite.|
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci**Example**
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci```js
210e41f4b71Sopenharmony_cilet portId = 1;
211e41f4b71Sopenharmony_ciusb.setPortRoles(portId, usb.PowerRoleType.SOURCE, usb.DataRoleType.HOST).then(() => {
212e41f4b71Sopenharmony_ci    console.info('usb setPortRoles successfully.');
213e41f4b71Sopenharmony_ci}).catch((err : BusinessError) => {
214e41f4b71Sopenharmony_ci    console.error('usb setPortRoles failed: ' + err.code + ' message: ' + err.message);
215e41f4b71Sopenharmony_ci});
216e41f4b71Sopenharmony_ci```
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci## USBPort<sup>9+</sup>
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ciRepresents a USB port.
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_ci**System API**: This is a system API.
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ci| Name          | Type                        | Mandatory|Description                               |
227e41f4b71Sopenharmony_ci| -------------- | -------------------------------- | -------------- |----------------------------------- |
228e41f4b71Sopenharmony_ci| id             | number                           | Yes  |Unique identifier of a USB port.                  |
229e41f4b71Sopenharmony_ci| supportedModes | [PortModeType](#portmodetype9)   | Yes  |Numeric mask combination for the supported mode list.|
230e41f4b71Sopenharmony_ci| status         | [USBPortStatus](#usbportstatus9) | Yes  |USB port role.                      |
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci## USBPortStatus<sup>9+</sup>
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ciEnumerates USB port roles.
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ci**System API**: This is a system API.
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci| Name            | Type| Mandatory|Description                  |
241e41f4b71Sopenharmony_ci| ---------------- | -------- | ----------- |---------------------- |
242e41f4b71Sopenharmony_ci| currentMode      | number   | Yes  |Current USB mode.       |
243e41f4b71Sopenharmony_ci| currentPowerRole | number   | Yes  |Current power role.    |
244e41f4b71Sopenharmony_ci| currentDataRole  | number   | Yes  |Current data role.|
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci## FunctionType<sup>9+</sup>
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ciEnumerates USB device function types.
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci**System API**: This is a system API.
251e41f4b71Sopenharmony_ci
252e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_ci| Name        | Value  | Description      |
255e41f4b71Sopenharmony_ci| ------------ | ---- | ---------- |
256e41f4b71Sopenharmony_ci| NONE         | 0    | No function.|
257e41f4b71Sopenharmony_ci| ACM          | 1    | ACM function. |
258e41f4b71Sopenharmony_ci| ECM          | 2    | ECM function. |
259e41f4b71Sopenharmony_ci| HDC          | 4    | HDC function. |
260e41f4b71Sopenharmony_ci| MTP          | 8    | Not supported currently.|
261e41f4b71Sopenharmony_ci| PTP          | 16   | Not supported currently.|
262e41f4b71Sopenharmony_ci| RNDIS        | 32   | Not supported currently.|
263e41f4b71Sopenharmony_ci| MIDI         | 64   | Not supported currently.|
264e41f4b71Sopenharmony_ci| AUDIO_SOURCE | 128  | Not supported currently.|
265e41f4b71Sopenharmony_ci| NCM          | 256  | Not supported currently.|
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ci## PortModeType<sup>9+</sup>
268e41f4b71Sopenharmony_ci
269e41f4b71Sopenharmony_ciEnumerates USB port mode types.
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ci**System API**: This is a system API.
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ci| Name     | Value  | Description                                                |
276e41f4b71Sopenharmony_ci| --------- | ---- | ---------------------------------------------------- |
277e41f4b71Sopenharmony_ci| NONE      | 0    | None                                                |
278e41f4b71Sopenharmony_ci| UFP       | 1    | Upstream facing port, which functions as the sink of power supply.                            |
279e41f4b71Sopenharmony_ci| DFP       | 2    | Downstream facing port, which functions as the source of power supply.                            |
280e41f4b71Sopenharmony_ci| DRP       | 3    | Dynamic reconfiguration port (DRP), which can function as the DFP (host) or UFP (device). It is not supported currently.|
281e41f4b71Sopenharmony_ci| NUM_MODES | 4    | Not supported currently.                                        |
282e41f4b71Sopenharmony_ci
283e41f4b71Sopenharmony_ci## PowerRoleType<sup>9+</sup>
284e41f4b71Sopenharmony_ci
285e41f4b71Sopenharmony_ciEnumerates power role types.
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_ci**System API**: This is a system API.
288e41f4b71Sopenharmony_ci
289e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
290e41f4b71Sopenharmony_ci
291e41f4b71Sopenharmony_ci| Name  | Value  | Description      |
292e41f4b71Sopenharmony_ci| ------ | ---- | ---------- |
293e41f4b71Sopenharmony_ci| NONE   | 0    | None      |
294e41f4b71Sopenharmony_ci| SOURCE | 1    | External power supply.|
295e41f4b71Sopenharmony_ci| SINK   | 2    | Internal power supply.|
296e41f4b71Sopenharmony_ci
297e41f4b71Sopenharmony_ci## DataRoleType<sup>9+</sup>
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ciEnumerates data role types.
300e41f4b71Sopenharmony_ci
301e41f4b71Sopenharmony_ci**System API**: This is a system API.
302e41f4b71Sopenharmony_ci
303e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
304e41f4b71Sopenharmony_ci
305e41f4b71Sopenharmony_ci| Name  | Value  | Description        |
306e41f4b71Sopenharmony_ci| ------ | ---- | ------------ |
307e41f4b71Sopenharmony_ci| NONE   | 0    | None        |
308e41f4b71Sopenharmony_ci| HOST   | 1    | USB host.|
309e41f4b71Sopenharmony_ci| DEVICE | 2    | USB device.|
310