目录

USB Device Management (C/C++)

Overview

OH_UsbManager is a USB device management C interface module provided by HarmonyOS, supporting developers to perform operations such as device enumeration, permission management, device connection, and underlying communication on the USB Host side at the C/C++ layer. This interface corresponds to the @ohos.usbManager interface at the ArkTS layer, and is suitable for development scenarios that require native USB device management using C/C++.

System capability: SystemCapability.USB.USBManager

[h2]Core Capabilities

The OH_UsbManager C API mainly provides the following capabilities:

[h2]Basic Concepts

[h2]Applicable Scenarios

The OH_UsbManager C API is applicable to the following typical scenarios:

[h2]Working Principles

The USB service system consists of a three-layer architecture: USB API, USB Service, and USB HAL.

Figure 1 USB service operation mechanism

In Host mode, the current device acts as the host device responsible for data transmission and port management, and the connected USB device is the managed peripheral device. Applications enumerate devices, obtain permissions, and establish UsbPipe communication channels through the OH_UsbManager C interface, and perform underlying I/O operations through file descriptors.

Before You Start

[h2]Environment Requirements

Development Tools and Configuration

DevEco Studio is an essential tool for USB device driver development. Developers can use this tool for development, debugging, packaging, and other operations. Please download and install this tool, and refer to Creating a Project and Running It in the DevEco Studio User Guide to perform basic operation verification and ensure that DevEco Studio runs properly.

SDK Version Configuration

The C interfaces provided by OH_UsbManager require an SDK version of API 26.0.1 or later.

HDC Configuration

HDC (HarmonyOS Device Connector) is a command-line debugging tool provided for developers. Through this tool, you can interact with real devices or emulators on Windows/Linux/Mac systems. For details, see HDC Configuration.

Note:

"Configuring the environment variable hdc_server_port" and "global environment variables" are mandatory operations.

[h2]Setting Up the Environment

  1. Install DevEco Studio on the PC. The version must be 4.1 or later.
  2. Update the public-SDK to API 26.0.1 or later.
  3. Install the HDC tool on the PC. Through this tool, you can interact with a real device or an emulator on Windows/Linux/Mac systems.
  4. Use a USB cable to connect the device running HarmonyOS to the PC.

[h2]Verify the Environment Setup

Check whether DevEco Studio is connected to the HarmonyOS device.

[h2]Permission Configuration

When calling OH_UsbManager_RequestPermission to request device permission, the system displays a dialog box to request user authorization. This interface itself does not require declaring a specific permission in module.json5, but requires user confirmation in the dialog box.

[h2]Constraints

Available APIs

[h2]Interfaces

Name Description
OH_UsbManager_ErrorCode OH_UsbManager_GetUsbDeviceList(OH_UsbManager_UsbDevice **devices, uint32_t *deviceCount) Obtains the list of all connected USB devices. The function allocates the device array and internal string buffers. The caller must release them through OH_UsbManager_FreeUsbDeviceList and must not release each field separately.
void OH_UsbManager_FreeUsbDeviceList(OH_UsbManager_UsbDevice *devices, uint32_t deviceCount) Releases the device array returned by OH_UsbManager_GetUsbDeviceList. After the call, the pointer becomes invalid. Passing null or a count of 0 is a safe no-op.
OH_UsbManager_ErrorCode OH_UsbManager_HasPermission(const char *deviceName, bool *result) Checks whether the application has permission to access the specified device. The device name format is <bus number>-<device address>.
OH_UsbManager_ErrorCode OH_UsbManager_RequestPermission(const char *deviceName, OH_UsbManager_PermissionCallback callback, void *userContext) Asynchronously requests permission to access the specified USB device. A system dialog may be triggered to request user authorization. The function returns immediately, and the result is returned through the callback.
OH_UsbManager_ErrorCode OH_UsbManager_ConnectDevice(const OH_UsbManager_UsbDevice *device, OH_UsbManager_UsbPipe *pipe) Connects a USB device and opens a communication pipe. Only the busNum and devAddress fields in device are required; other fields are ignored. The returned pipe must be closed through OH_UsbManager_ClosePipe.
OH_UsbManager_ErrorCode OH_UsbManager_GetFileDescriptor(const OH_UsbManager_UsbPipe *pipe, int32_t *fd) Obtains the file descriptor of an opened USB device pipe. This fd can be used for ioctl-based underlying USB transfer.
OH_UsbManager_ErrorCode OH_UsbManager_ClosePipe(const OH_UsbManager_UsbPipe *pipe) Closes the USB device pipe and releases the underlying resources. The pipe must be obtained through OH_UsbManager_ConnectDevice.

For detailed interface descriptions, see OH_UsbManager and ohusb_manager.h.

[h2]Data Structure

Structs Description
OH_UsbManager_UsbDevice USB device information, including bus number, device address, name, vendor information, product ID, configuration list, etc.
OH_UsbManager_UsbConfig USB configuration information, including configuration ID, attributes, maximum power, interface list, etc. A UsbDevice can include multiple UsbConfigs.
OH_UsbManager_UsbInterface USB interface information, including interface ID, protocol, class, endpoint list, etc. A UsbConfig can include multiple UsbInterfaces.
OH_UsbManager_UsbEndpoint USB endpoint information, including endpoint address, attributes, transfer type, direction, etc. A UsbInterface can include multiple UsbEndpoints.
OH_UsbManager_UsbPipe USB device pipe, including bus number and device address, used to identify an opened device communication channel.

For detailed data structure descriptions, see OH_UsbManager and ohusb_manager.h.

[h2]Error Code

The OH_UsbManager C interface returns error codes through the OH_UsbManager_ErrorCode enum, as follows:

Error Code Value Description
OH_USBMANAGER_SUCCESS 0 Operation successful.
OH_USBMANAGER_ERROR_PERMISSION_DENIED 14400001 No access permission.
OH_USBMANAGER_ERROR_SERVICE_EXCEPTION 14400004 Service exception.
OH_USBMANAGER_ERROR_NO_DEVICE 14400008 The device does not exist (it may have been disconnected).
OH_USBMANAGER_ERROR_NO_MEMORY 14400009 Insufficient memory.
OH_USBMANAGER_ERROR_IO_ERROR 14400012 Transfer I/O error.
OH_USBMANAGER_ERROR_INVALID_PARAMETER 14400014 Invalid parameter. A null pointer is passed to a parameter that cannot be null.

For detailed error code descriptions, see ohusb_manager.h.

How to Develop

The complete development process of the USB device management C API includes: device discovery → permission authorization → device connection → underlying communication → resource close. The operations of each stage are described step by step below.

Add the Dynamic Link Library

Add the following lib to CMakeLists.txt.

libohusb_manager.so

Header File

#include "BasicServicesKit/ohusb_manager.h"

[h2]Device Discovery

Call OH_UsbManager_GetUsbDeviceList to obtain the list of all currently connected USB devices. This function allocates the device array and all internal string buffers, and the caller must not free the memory of each field separately. The returned device array contains complete descriptor hierarchy information (configuration, interface, endpoint), and the target device can be found by traversing the device list.

After use, you must call OH_UsbManager_FreeUsbDeviceList to release the device array.

OH_UsbManager_UsbDevice *devices = nullptr;
uint32_t deviceCount = 0;
OH_UsbManager_ErrorCode code = OH_UsbManager_GetUsbDeviceList(&devices, &deviceCount);
if (code != OH_USBMANAGER_SUCCESS) {
    ThrowUsbError(env, "OH_UsbManager_GetUsbDeviceList", code);
    return nullptr;
}
OH_UsbManager_UsbDevice *devices = nullptr;
uint32_t deviceCount = 0;
bool freed = false;
{
    std::lock_guard<std::mutex> lock(g_deviceListMutex);
    devices = g_heldDevices;
    deviceCount = g_heldDeviceCount;
    if (devices != nullptr) {
        OH_UsbManager_FreeUsbDeviceList(devices, deviceCount);
        g_heldDevices = nullptr;
        g_heldDeviceCount = 0;
        freed = true;
    }
}

[h2]Authorization

Check whether the current application already has device access permission through OH_UsbManager_HasPermission. The device name format is <bus number>-<device address>.

If no permission is granted, call OH_UsbManager_RequestPermission to request permission asynchronously. This function returns immediately, and the request result is notified through the OH_UsbManager_PermissionCallback callback. Device connection operations can be performed only after authorization succeeds.

std::string deviceName;
if (!GetDeviceNameArg(env, info, deviceName)) {
    return nullptr;
}
bool result = false;
OH_UsbManager_ErrorCode code = OH_UsbManager_HasPermission(deviceName.c_str(), &result);
if (code != OH_USBMANAGER_SUCCESS) {
    ThrowUsbError(env, "OH_UsbManager_HasPermission", code);
    return nullptr;
}
RequestPermissionContext *context = static_cast<RequestPermissionContext *>(data);
OH_UsbManager_ErrorCode code =
    OH_UsbManager_RequestPermission(context->deviceName.c_str(), OnPermissionCallback, context);
std::unique_lock<std::mutex> lock(context->mutex);
if (code != OH_USBMANAGER_SUCCESS) {
    context->apiCode = code;
    context->completed = true;
    return;
}

[h2]Connection

After the permission is obtained, call OH_UsbManager_ConnectDevice to connect the target USB device and open the communication pipe. When connecting, only the busNum and devAddress fields in the OH_UsbManager_UsbDevice structure are required, and other fields are ignored. The returned OH_UsbManager_UsbPipe contains the bus number and device address of the device, which are used for all subsequent device operations.

After a successful connection, you can call OH_UsbManager_GetFileDescriptor to obtain the file descriptor (fd) of the device pipe. This fd can be used for underlying USB transfer operations based on ioctl.

uint32_t busNum = 0;
uint32_t devAddress = 0;
napi_get_value_uint32(env, args[0], &busNum);
napi_get_value_uint32(env, args[1], &devAddress);

OH_UsbManager_UsbDevice device = {0};
device.busNum = static_cast<uint8_t>(busNum);
device.devAddress = static_cast<uint8_t>(devAddress);
OH_UsbManager_UsbPipe pipe = {0};
OH_UsbManager_ErrorCode code = OH_UsbManager_ConnectDevice(&device, &pipe);
if (code != OH_USBMANAGER_SUCCESS) {
    ThrowUsbError(env, "OH_UsbManager_ConnectDevice", code);
    return nullptr;
}
(void)info;
OH_UsbManager_UsbPipe pipe = {0};
{
    std::lock_guard<std::mutex> lock(g_pipeMutex);
    if (!g_pipeConnected) {
        napi_throw_error(env, nullptr, "pipe is not open, call connectDevice first");
        return nullptr;
    }
    pipe = g_pipe;
}
int32_t fd = -1;
OH_UsbManager_ErrorCode code = OH_UsbManager_GetFileDescriptor(&pipe, &fd);
if (code != OH_USBMANAGER_SUCCESS) {
    ThrowUsbError(env, "OH_UsbManager_GetFileDescriptor", code);
    return nullptr;
}

[h2]Communication

Through the file descriptor obtained by OH_UsbManager_GetFileDescriptor, developers can use standard file I/O or ioctl for underlying USB communication. Common communication methods include:

Note:

The specific ioctl commands and data formats for underlying communication depend on the USB device protocol and kernel driver implementation. Developers need to refer to the relevant USB protocol specifications.

[h2]Close

After use, release resources in the following order:

  1. Call OH_UsbManager_ClosePipe to close the device pipe and release underlying resources.
  2. Call OH_UsbManager_FreeUsbDeviceList to release the device array memory allocated by OH_UsbManager_GetUsbDeviceList.

Important:

The resource release order is to close the pipe first, and then release the device array. Ensure that the release operation is also performed on abnormal exit paths to avoid resource leaks.

(void)info;
OH_UsbManager_UsbPipe pipe = {0};
{
    std::lock_guard<std::mutex> lock(g_pipeMutex);
    if (!g_pipeConnected) {
        napi_throw_error(env, nullptr, "pipe is not open, call connectDevice first");
        return nullptr;
    }
    pipe = g_pipe;
}
OH_UsbManager_ErrorCode code = OH_UsbManager_ClosePipe(&pipe);
if (code != OH_USBMANAGER_SUCCESS) {
    ThrowUsbError(env, "OH_UsbManager_ClosePipe", code);
    return nullptr;
}

Description:

For the complete sample code, see USBManager C API Sample.

FAQs

Problem Symptom Possible Cause Solution Measure
When calling OH_UsbManager_GetUsbDeviceList to obtain the device list, the returned deviceCount is 0.

- The current device operates in USB Device mode instead of Host mode.

- No USB device is connected.

Ensure that the current device operates in USB Host mode and the connected device operates in Device mode. Some devices support both host and peripheral USB device modes. The device must be set to Host mode before peripheral devices can be enumerated.
When calling OH_UsbManager_ConnectDevice to connect a device, OH_USBMANAGER_ERROR_PERMISSION_DENIED (14400001) is returned.

- The application has not obtained device access permission.

- The user rejected the authorization request in the permission dialog.

1. Before connecting a device, call OH_UsbManager_HasPermission to check whether access permission is available.

2. If there is no permission, call OH_UsbManager_RequestPermission to request permission, and confirm the authorization result in the callback.

3. Call OH_UsbManager_ConnectDevice only after permission is granted.

After calling OH_UsbManager_RequestPermission, the callback function OH_UsbManager_PermissionCallback is not invoked.

- The deviceName parameter format is incorrect.

- The USB service is abnormal and cannot process the permission request.

- The callback function pointer is null.

1. Confirm that deviceName is obtained from the name field of OH_UsbManager_UsbDevice and the format is correct.

2. Check whether the callback function pointer is valid.

3. Use logs to confirm whether the USB service is running normally.

After calling OH_UsbManager_GetUsbDeviceList multiple times, the application memory keeps growing.

- After calling OH_UsbManager_GetUsbDeviceList, OH_UsbManager_FreeUsbDeviceList is not called to release the device array.

- After calling OH_UsbManager_ConnectDevice, OH_UsbManager_ClosePipe is not called to close the pipe.

- The deviceCount passed during release is inconsistent with the value at the time of acquisition.

1. After each call to OH_UsbManager_GetUsbDeviceList, you must call OH_UsbManager_FreeUsbDeviceList to release memory, and the deviceCount passed in should be consistent with the value at the time of acquisition.

2. After each call to OH_UsbManager_ConnectDevice, you must call OH_UsbManager_ClosePipe to close the pipe.

3. Ensure that the release operation is also performed on abnormal exit paths.

When calling OH_UsbManager_GetFileDescriptor, OH_USBMANAGER_ERROR_NO_DEVICE (14400008) is returned.

- The USB device was disconnected before the file descriptor was obtained.

- The passed OH_UsbManager_UsbPipe is invalid.

1. Confirm that the device is still connected. You can call OH_UsbManager_GetUsbDeviceList to confirm whether the device is in the list.

2. Confirm that OH_UsbManager_UsbPipe is successfully returned by OH_UsbManager_ConnectDevice.

3. After the device reconnects, the complete connection process must be executed again.