同步操作将从 OpenHarmony/usb_usb_manager 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<a name="EN-US_TOPIC_0000001124094823"></a>
<a name="section11660541593"></a>
The following figure shows the USB service architecture.
Figure 1 USB service architecture<a name="fig15658513184019"></a>
The architecture logically consists of three layers:
<a name="section19472752217"></a>
base/usb/usb_manager
├── interfaces # APIs
│ ├── innerkits # Internal APIs
│ └── kits # External APIs
├── sa_profile # SA profile
└── services # Services
│ ├── native # Native APIs
│ └── zidl # zidl APIs
└── test # Test cases
└── utils # Utilities
<a name="section19472752218"></a>
API | Description |
---|---|
int32_t OpenDevice(const UsbDevice &device, USBDevicePipe &pip); | Opens a USB device to set up a connection. |
bool Close(const USBDevicePipe &pip); | Closes a USB device to release all system resources related to the device. |
int32_t GetDevices(std::vector &deviceList); | Obtains the USB device list. |
int32_t SetConfiguration(USBDevicePipe &pip, const USBConfig &config); | Sets the current configuration of the USB device. |
int32_t ClaimInterface(USBDevicePipe &pip, const UsbInterface &interface, bool force); | Claims a USB interface exclusively. This must be done before data transfer. |
int32_t ReleaseInterface(USBDevicePipe &pip, const UsbInterface &interface); | Releases a USB interface. This is usually done after data transfer. |
int32_t SetInterface(USBDevicePipe &pipe, const UsbInterface &interface); | Sets the alternate settings for the specified USB interface. This allows you to switch between two interfaces with the same ID but different alternate settings. |
int32_t BulkTransfer(USBDevicePipe &pip, const USBEndpoint &endpoint, std::vector<uint8_t> &vdata, int32_t timeout); | Writes data on a specified endpoint during bulk transfer. |
int32_t ControlTransfer(USBDevicePipe &pip, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &vdata); | Performs control transfer for endpoint 0 of the device. The data transfer direction is determined by the request type. If the result ofrequestType & USB_ENDPOINT_DIR_MASK is USB_DIR_OUT, the endpoint is in the data writing direction; if the result is USB_DIR_IN, the endpoint is in the data reading direction. |
int32_t RequestInitialize(UsbRequest &request); | Initializes a request. |
int32_t RequestQueue(UsbRequest &request); | Sends or receives requests for isochronous transfer on a specified endpoint. The data transfer direction is determined by the endpoint direction. |
int32_t PipeRequestWait(USBDevicePipe &pip, int64_t timeout, UsbRequest &req); | Waits for the operation result of the isochronous transfer request in<b> RequestQueue</b> . |
int32_t RequestAbort(UsbRequest &request); | Cancels the data transfer requests to be processed. |
int32_t RequestFree(UsbRequest &request); | Requests for releasing data. |
bool HasRight(std::string deviceName); | Checks whether the application has permission to access the USB device. |
int32_t RequestRight(std::string deviceName); | Requests for permission to access the USB device. |
API | Description |
---|---|
int32_t GetCurrentFunctions(int32_t &funcs); | Obtains the list of functions (represented by bit field) supported by the current device. |
int32_t SetCurrentFunctions(int32_t funcs); | Sets the list of functions (represented by bit field) supported by the current device. |
int32_t UsbFunctionsFromString(std::string funcs); | Converts the string descriptor of a given USB function list to a numeric mask combination. |
std::string UsbFunctionsToString(int32_t funcs); | Converts the numeric mask combination of a given USB function list to a string descriptor. |
API | Description |
---|---|
int32_t GetPorts(std::vector &usbPorts); | Obtains the port list. |
int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole); | Sets the port role. |
int32_t GetSupportedModes(int32_t portId, int32_t &supportedModes); | Obtains the mask combination for the supported mode list of a given port. |
<a name="section19472752219"></a>
The following provides some examples on how to use major APIs. For detailed service code, see js_unittest.
In this example, the USB device serves as the host and connects to the device to implement data transfer.
// Import the USB API package.
import usb from '@ohos.usb';
// Obtain the USB device list.
var deviceList = usb.getDevices();
// device name
var deviceName = deviceList[0].name;
// Request for permission to operate a specified device.
usb.requestRight(deviceName).then(hasRight => {
console.info("usb device request right result: " + hasRight);
}).catch(error => {
console.info("usb device request right failed : " + error);
});
// Open the USB device, and obtain the device pipe for data transfer.
var pipe = usb.connectDevice(deviceList[0]);
// Claim the USB interface.
usb.claimInterface(pipe, interface, true); // Among the input arguments, interface refers to the one to be operated on the USB device.
// Read data. Select the corresponding RX endpoint (endpoint.direction == 0x80) from deviceList for data transfer. The data data to be transferred is encapsulated in dataUint8Array.
usb.bulkTransfer(pipe, inEndpoint, dataUint8Array, 15000).then(dataLength => {
if (dataLength >= 0) {
console.info("usb readData result Length : " + dataLength);
var resultStr = this.ab2str(dataUint8Array); // Convert uint8 data into a string.
console.info("usb readData buffer : " + resultStr);
} else {
console.info("usb readData failed : " + dataLength);
}
}).catch(error => {
console.info("usb readData error : " + JSON.stringify(error));
});
// Send data. Select the corresponding TX endpoint (endpoint.direction == 0) from deviceList for data transfer.
usb.bulkTransfer(this.pip, this.outEndpoint, dataUint8Array, 15000).then(dataLength => {
if (dataLength >= 0) {
console.info("usb writeData result write length : " + dataLength);
} else {
console.info("writeData failed");
}
}).catch(error => {
console.info("usb writeData error : " + JSON.stringify(error));
});
usb.releaseInterface(pipe, interface);
usb.closePipe(pipe);
In this example, the USB device is used as the device to set the ACM, ECM, and HDC functions.
usb.setCurrentFunctions(funType).then(data => {
console.info("usb setCurrentFunctions : " + data);
}).catch(error => {
console.info("usb setCurrentFunctions error : " + error);
});
<a name="section63151229062"></a>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。