openharmony 鸿蒙 capi-usb-ddk-api-h

2026-08-25 浏览 (1)

usb_ddk_api.h

Overview

Declares the USB DDK APIs used by the USB host to access USB devices.

File to include: <usb/usb_ddk_api.h>

Library: libusb_ndk.z.so

System capability: SystemCapability.Driver.USB.Extension

Since: 10

Related module: UsbDdk

Summary

Functions

NameDescription
int32_t OH_Usb_Init(void)Initializes the USB DDK.
void OH_Usb_Release(void)Releases the USB DDK.
int32_t OH_Usb_ReleaseResource(void)Releases the DDK.
int32_t OH_Usb_GetDeviceDescriptor(uint64_t deviceId, struct UsbDeviceDescriptor *desc)Obtains the device descriptor.
int32_t OH_Usb_GetConfigDescriptor(uint64_t deviceId, uint8_t configIndex, struct UsbDdkConfigDescriptor ** const config)Obtains the configuration descriptor. To avoid memory leakage, use OH_Usb_FreeConfigDescriptor to release a descriptor after use.
void OH_Usb_FreeConfigDescriptor(struct UsbDdkConfigDescriptor * const config)Releases a configuration descriptor after use to prevent memory leakage.
int32_t OH_Usb_ClaimInterface(uint64_t deviceId, uint8_t interfaceIndex, uint64_t *interfaceHandle)Claims a USB interface.
int32_t OH_Usb_ReleaseInterface(uint64_t interfaceHandle)Releases a USB interface.
int32_t OH_Usb_SelectInterfaceSetting(uint64_t interfaceHandle, uint8_t settingIndex)Activates the alternate setting of a USB interface.
int32_t OH_Usb_GetCurrentInterfaceSetting(uint64_t interfaceHandle, uint8_t *settingIndex)Obtains the activated alternate setting of a USB interface.
int32_t OH_Usb_SendControlReadRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup,uint32_t timeout, uint8_t *data, uint32_t *dataLen)Sends a control read transfer request. This API works in a synchronous manner.
int32_t OH_Usb_SendControlWriteRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup,uint32_t timeout, const uint8_t *data, uint32_t dataLen)Sends a control write transfer request. This API works in a synchronous manner.
int32_t OH_Usb_SendPipeRequest(const struct UsbRequestPipe *pipe, UsbDeviceMemMap *devMmap)Sends a pipe request. This API works in a synchronous manner. It applies to interrupt transfer and bulk transfer.
int32_t OH_Usb_SendPipeRequestWithAshmem(const struct UsbRequestPipe *pipe, DDK_Ashmem *ashmem)Sends a pipe request based on the shared memory. This API returns the result synchronously. It applies to interrupt transfer and bulk transfer.
int32_t OH_Usb_CreateDeviceMemMap(uint64_t deviceId, size_t size, UsbDeviceMemMap **devMmap)Creates a buffer. To avoid resource leakage, use OH_Usb_DestroyDeviceMemMap to destroy a buffer after use.
void OH_Usb_DestroyDeviceMemMap(UsbDeviceMemMap *devMmap)Destroys a buffer. To prevent resource leakage, destroy a buffer in time after use.
int32_t OH_Usb_GetDevices(struct Usb_DeviceArray *devices)Obtains the USB device ID list. Ensure that the pointer parameters passed in are valid. To avoid excessive memory usage, the size of the requested device ID array is recommended not to exceed 128. After using the struct, release the memory of its members; otherwise, resource leaks may occur. Besides, make sure that the obtained USB device ID has been filtered by vid in the driver configuration information.
int32_t OH_Usb_ControlTransfer(uint64_t deviceID, const struct UsbControlRequestSetup *setupPacket, uint8_t *data, uint32_t timeout)Performs USB control transfer. This API returns the result synchronously.
int32_t OH_Usb_GetNonRootHubs(struct Usb_NonRootHubArray *nonRootHub)Queries and returns the non-root hub list. Ensure that the input pointer is valid. It is recommended that the number of non-root hub IDs do not exceed 128 to prevent excessive memory usage. After using the struct, release the memory of its members; otherwise, resource leaks may occur.

Function Description

OH_Usb_Init()

int32_t OH_Usb_Init(void)

Description

Initializes the USB DDK.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails, or an internal error occurs.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_MEMORY_ERROR: The memory allocation fails.

OH_Usb_Release()

void OH_Usb_Release(void)

Description

Releases the USB DDK.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

OH_Usb_ReleaseResource()

int32_t OH_Usb_ReleaseResource(void)

Description

Releases the USB DDK.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 18

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails.

OH_Usb_GetDeviceDescriptor()

int32_t OH_Usb_GetDeviceDescriptor(uint64_t deviceId, struct UsbDeviceDescriptor *desc)

Description

Obtains the device descriptor.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Parameters

NameDescription
uint64_t deviceIdDevice ID.
struct UsbDeviceDescriptor *descDevice descriptor. For details, see UsbDeviceDescriptor.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails.
USB_DDK_INVALID_PARAMETER: The input desc is a null pointer.

OH_Usb_GetConfigDescriptor()

int32_t OH_Usb_GetConfigDescriptor(uint64_t deviceId, uint8_t configIndex, struct UsbDdkConfigDescriptor ** const config)

Description

Obtains the configuration descriptor. To avoid memory leakage, use OH_Usb_FreeConfigDescriptor to release a descriptor after use.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Parameters

NameDescription
uint64_t deviceIdDevice ID.
uint8_t configIndexConfiguration index, which corresponds to the bConfigurationValue field in the USB configuration descriptor.
struct UsbDdkConfigDescriptor ** const configConfiguration descriptor, which includes the standard configuration descriptor defined in the USB protocol and the associated interface descriptor and endpoint descriptor.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails.
USB_DDK_INVALID_PARAMETER: The input config is a null pointer.
USB_DDK_IO_FAILED: An I/O exception occurs.
USB_DDK_MEMORY_ERROR: The memory allocation fails.

OH_Usb_FreeConfigDescriptor()

void OH_Usb_FreeConfigDescriptor(struct UsbDdkConfigDescriptor * const config)

Description

Releases a configuration descriptor after use to prevent memory leakage.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Parameters

NameDescription
const struct UsbDdkConfigDescriptor * const configConfiguration descriptor, which is obtained by calling OH_Usb_GetConfigDescriptor.

OH_Usb_ClaimInterface()

int32_t OH_Usb_ClaimInterface(uint64_t deviceId, uint8_t interfaceIndex, uint64_t *interfaceHandle)

Description

Claims a USB interface.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Parameters

NameDescription
uint64_t deviceIdDevice ID.
uint8_t interfaceIndexInterface index, which corresponds to bInterfaceNumber in the USB protocol.
uint64_t *interfaceHandleInterface operation handle. After the interface is claimed successfully, a value will be assigned to this parameter.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails.
USB_DDK_INVALID_PARAMETER: The input interfaceHandle is a null pointer.
USB_DDK_MEMORY_ERROR: The memory to be allocated exceeds the limit.

OH_Usb_ReleaseInterface()

int32_t OH_Usb_ReleaseInterface(uint64_t interfaceHandle)

Description

Releases a USB interface.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Parameters

NameDescription
uint64_t interfaceHandleInterface operation handle.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails.
USB_DDK_INVALID_PARAMETER: One or more parameters are invalid.

OH_Usb_SelectInterfaceSetting()

int32_t OH_Usb_SelectInterfaceSetting(uint64_t interfaceHandle, uint8_t settingIndex)

Description

Activates the alternate setting of a USB interface.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Parameters

NameDescription
uint64_t interfaceHandleInterface operation handle.
uint8_t settingIndexAlternate setting index, which corresponds to the bAlternateSetting field of the interface descriptor in the USB protocol.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails.
USB_DDK_INVALID_PARAMETER: One or more parameters are invalid.

OH_Usb_GetCurrentInterfaceSetting()

int32_t OH_Usb_GetCurrentInterfaceSetting(uint64_t interfaceHandle, uint8_t *settingIndex)

Description

Obtains the activated alternate setting of a USB interface.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Parameters

NameDescription
uint64_t interfaceHandleInterface operation handle.
uint8_t *settingIndexAlternate setting index, which corresponds to the bAlternateSetting field of the interface descriptor in the USB protocol.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails.
USB_DDK_INVALID_PARAMETER: The input settingIndex is a null pointer.

OH_Usb_SendControlReadRequest()

int32_t OH_Usb_SendControlReadRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup,uint32_t timeout, uint8_t *data, uint32_t *dataLen)

Description

Sends a control read transfer request. This API works in a synchronous manner.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Parameters

NameDescription
uint64_t interfaceHandleInterface operation handle.
const struct UsbControlRequestSetup *setupRequest parameters. For details, see UsbControlRequestSetup.
uint32_t timeoutTimeout duration, in ms.
uint8_t *dataData to transfer.
uint32_t *dataLenData length. The return value indicates the length of the actually read data.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_FAILED: The permission check fails.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails.
USB_DDK_INVALID_PARAMETER: The input setup, data, or dataLen is a null pointer, or the value of datalen is less than the length of the read data.
USB_DDK_MEMORY_ERROR: The attempt to copy the memory that stores the read data fails.
USB_DDK_IO_FAILED: An I/O exception occurs.
USB_DDK_TIMEOUT: The operation times out.

OH_Usb_SendControlWriteRequest()

int32_t OH_Usb_SendControlWriteRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup,uint32_t timeout, const uint8_t *data, uint32_t dataLen)

Description

Sends a control write transfer request. This API works in a synchronous manner.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Parameters

NameDescription
uint64_t interfaceHandleInterface operation handle.
const struct UsbControlRequestSetup *setupRequest parameters. For details, see UsbControlRequestSetup.
uint32_t timeoutTimeout duration, in ms.
const uint8_t *dataData to transfer.
uint32_t dataLenData length.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_FAILED: The permission check fails.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails.
USB_DDK_INVALID_PARAMETER: The input setup or data is a null pointer.
USB_DDK_MEMORY_ERROR: The attempt to copy the memory that stores the read data fails.
USB_DDK_IO_FAILED: An I/O exception occurs.
USB_DDK_TIMEOUT: The operation times out.

OH_Usb_SendPipeRequest()

int32_t OH_Usb_SendPipeRequest(const struct UsbRequestPipe *pipe, UsbDeviceMemMap *devMmap)

Description

Sends a pipe request. This API works in a synchronous manner. It applies to interrupt transfer and bulk transfer.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Parameters

NameDescription
const struct UsbRequestPipe *pipePipe used to transfer data.
UsbDeviceMemMap *devMmapData buffer, which can be obtained by calling OH_Usb_CreateDeviceMemMap.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails.
USB_DDK_INVALID_PARAMETER: The input pipe or devMmap is a null pointer, or the devMmap address is null.
USB_DDK_MEMORY_ERROR: The attempt to copy the memory that stores the read data fails.
USB_DDK_IO_FAILED: An I/O exception occurs.
USB_DDK_TIMEOUT: The operation times out.

OH_Usb_SendPipeRequestWithAshmem()

int32_t OH_Usb_SendPipeRequestWithAshmem(const struct UsbRequestPipe *pipe, DDK_Ashmem *ashmem)

Description

Sends a pipe request based on the shared memory. This API returns the result synchronously. It applies to interrupt transfer and bulk transfer.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 12

Parameters

NameDescription
const struct UsbRequestPipe *pipePipe used to transfer data.
DDK_Ashmem *ashmemShared memory, which can be obtained through OH_DDK_CreateAshmem.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails.
USB_DDK_INVALID_PARAMETER: The input pipe or ashmem is a null pointer, or the ashmem address is null.
USB_DDK_MEMORY_ERROR: The attempt to copy the memory that stores the read data fails.
USB_DDK_IO_FAILED: An I/O exception occurs.
USB_DDK_TIMEOUT: The operation times out.

OH_Usb_CreateDeviceMemMap()

int32_t OH_Usb_CreateDeviceMemMap(uint64_t deviceId, size_t size, UsbDeviceMemMap **devMmap)

Description

Creates a buffer. To avoid resource leakage, use OH_Usb_DestroyDeviceMemMap to destroy a buffer after use.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Parameters

NameDescription
uint64_t deviceIdDevice ID.
size_t sizeBuffer size.
UsbDeviceMemMap **devMmapData memory map, through which the created buffer is returned to the caller.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_PARAMETER: The input parameter devMmap or *devMmap is a null pointer.
USB_DDK_MEMORY_ERROR: The memory mapping fails, or the memory allocation of devMmap fails.

OH_Usb_DestroyDeviceMemMap()

void OH_Usb_DestroyDeviceMemMap(UsbDeviceMemMap *devMmap)

Description

Destroys a buffer. To prevent resource leakage, destroy a buffer in time after use.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 10

Parameters

NameDescription
UsbDeviceMemMap *devMmapDestroys the buffer created by calling OH_Usb_CreateDeviceMemMap.

OH_Usb_GetDevices()

int32_t OH_Usb_GetDevices(struct Usb_DeviceArray *devices)

Description

Obtains the USB device ID list. Ensure that the pointer parameters passed in are valid. To avoid excessive memory usage, the size of the requested device ID array is recommended not to exceed 128. After using the struct, release the memory of its members; otherwise, resource leaks may occur. Besides, make sure that the obtained USB device ID has been filtered by vid in the driver configuration information.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 18

Parameters

NameDescription
struct Usb_DeviceArray *devicesDevice memory address, which is used to store the obtained device ID list and quantity.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The operation is successful.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_OPERATION: The USB DDK service connection fails.
USB_DDK_INVALID_PARAMETER: The input devices is a null pointer.

OH_Usb_ControlTransfer()

int32_t OH_Usb_ControlTransfer(uint64_t deviceID, const struct UsbControlRequestSetup *setupPacket, uint8_t *data, uint32_t timeout)

Description

Performs USB control transfer. This API returns the result synchronously.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 26.0.0

Parameters

NameDescription
uint64_t deviceIDDevice ID.
const struct UsbControlRequestSetup *setupPacketSetup packet parameter that controls the transfer request, including the transfer direction and data length.
uint8_t *dataRequested buffer for storing input or output data. The buffer size must be the same as the value of the wLength field in the setup packet and cannot exceed 1,024 bytes. Otherwise, the data will be truncated.
uint32_t timeoutTimeout interval, in milliseconds, which is the maximum waiting time before a response is received. The value 0 means to wait infinitely.

Returns

TypeDescription
int32_tNumber of actually transferred bytes upon success (a non-negative number).
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_OPERATION: DDK initialization fails. Call OH_Usb_Init to initialize the DDK.
USB_DDK_INVALID_PARAMETER: The setupPacket or data parameter is invalid.
USB_DDK_TIMEOUT: Controls transfer timeout.
USB_DDK_IO_FAILED: Controls transfer request I/O exceptions.

OH_Usb_GetNonRootHubs()

int32_t OH_Usb_GetNonRootHubs(struct Usb_NonRootHubArray *nonRootHub)

Description

Queries and returns the non-root hub list. Ensure that the input pointer is valid. It is recommended that the number of non-root hub IDs do not exceed 128 to prevent excessive memory usage. After using the struct, release the memory of its members; otherwise, resource leaks may occur.

Required permissions: ohos.permission.ACCESS_DDK_USB

Since: 26.0.0

Parameters

NameDescription
struct Usb_NonRootHubArray *nonRootHubRequested non-root hub memory address, which is used to store the queried non-root hub ID list and quantity.

Returns

TypeDescription
int32_tUSB_DDK_SUCCESS: The query is successful.
USB_DDK_NO_PERM: The permission check fails.
USB_DDK_INVALID_OPERATION: DDK initialization fails. Call OH_Usb_Init to initialize the DDK.
USB_DDK_INVALID_PARAMETER: The input nonRootHub is a null pointer.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-scsiperipheralddk-scsiperipheral-request

openharmony 鸿蒙 capi-usbddk-usbconfigdescriptor

openharmony 鸿蒙 capi-hidddk

openharmony 鸿蒙 capi-usbddk

openharmony 鸿蒙 capi-hidddk-hid-absaxesarray

openharmony 鸿蒙 capi-scsiperipheralddk-scsiperipheral-response

openharmony 鸿蒙 capi-scsi-peripheral-api-h

openharmony 鸿蒙 capi-hidddk-hid-relaxesarray

openharmony 鸿蒙 js-apis-driver-deviceManager

openharmony 鸿蒙 capi-hidddk-hid-eventtypearray

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/3NitczOG