DriverUtils

    This module provides interfaces such as log printing, doubly linked list operations, and work queues.

    Since:

    1.0

    Version:

    1.0

    Summary

    Files

    Data Structures

    Data Structure Name

    Description

    Describes a doubly linked list.

    HdfWork

    Describes a work item and a delayed work item. This structure defines the work and delayed work items, and then calls the initialization function or HdfDelayedWorkInit to perform initialization. The function is to add a work item to a work queue immediately, and the HdfAddDelayedWork() function is to add a work item to a work queue after the configured delayed time.

    Describes a work queue.

    Macros

    Macro Name and Value

    Description

    HDF_WAIT_FOREVER    0xFFFFFFFF

    Indicates that the function keeps waiting to obtain a semaphore or mutex.

    HDF_ARRAY_SIZE (a)   (sizeof(a) / sizeof((a)[0]))

    Defines the array size.

    HDF_KILO_UNIT    1000

    Defines a time conversion unit, for example, the unit for converting from second to millisecond.

    CONTAINER_OF(ptr, type, member)   (type )((char )(ptr) - (char )&((type )0)->member)

    Obtains the address of a structure variable from its member address.

    (ptr, type, member)   CONTAINER_OF((ptr)->next, type, member)

    Obtains the first node of a doubly linked list.

    (ptr, type, member)   CONTAINER_OF((ptr)->prev, type, member)

    Obtains the last node of a doubly linked list.

    (pos, head, type, member)

    Traverses all nodes in a doubly linked list.

    DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, head, type, member)

    Traverses all nodes in a doubly linked list. This function is used to delete the nodes pointed to by pos during traversal.

    (HDF_TAG)   #HDF_TAG

     

    HDF_LOGV(fmt, arg…)   (“[HDF:V/“ LOG_TAG “]” fmt “\r\n”, ##arg)

    Prints logs at the verbose level.

    HDF_LOGD(fmt, arg…)   (“[HDF:D/“ LOG_TAG “]” fmt “\r\n”, ##arg)

    Prints logs at the debug level.

    HDF_LOGI(fmt, arg…)   (“[HDF:I/“ LOG_TAG “]” fmt “\r\n”, ##arg)

    Prints logs at the information level.

    HDF_LOGW(fmt, arg…)   (“[HDF:W/“ LOG_TAG “]” fmt “\r\n”, ##arg)

    Prints logs at the warning level.

    HDF_LOGE(fmt, arg…)   (“[HDF:E/“ LOG_TAG “]” fmt “\r\n”, ##arg)

    Prints logs at the error level.

    Typedefs

    Typedef Name

    Description

    HdfWorkFunc) (void )

    typedef void( 

    Describes a work execution function type.

    Enumerations

    Enumeration Name

    Description

    {   HDF_SUCCESS = 0, = -1, HDF_ERR_NOT_SUPPORT = -2, = -3,   HDF_ERR_INVALID_OBJECT = -4, = -6, HDF_ERR_TIMEOUT = -7, = -10,   HDF_ERR_QUEUE_FULL = -15, = -16, HDF_ERR_IO = -17, = -18,   HDF_BSP_ERR_OP = HDF_BSP_ERR_NUM(-1), = HDF_BSP_ERR_NUM(-2), HDF_PAL_ERR_DEV_CREATE = HDF_BSP_ERR_NUM(-3), = HDF_BSP_ERR_NUM(-4),   HDF_DEV_ERR_NO_MEMORY = HDF_DEV_ERR_NUM(-1), = HDF_DEV_ERR_NUM(-2), HDF_DEV_ERR_NO_DEVICE_SERVICE = HDF_DEV_ERR_NUM(-3), = HDF_DEV_ERR_NUM(-4),   HDF_DEV_ERR_PUBLISH_FAIL = HDF_DEV_ERR_NUM(-5), = HDF_DEV_ERR_NUM(-6), HDF_DEV_ERR_NODATA = HDF_DEV_ERR_NUM(-7), = HDF_DEV_ERR_NUM(-8),   HDF_DEV_ERR_OP = HDF_DEV_ERR_NUM(-10) }

    Enumerates HDF return value types.

    { = 1 << 0, HDF_WORK_BUSY_RUNNING = 1 << 1 }

    Enumerates statuses of a work item or a delayed work item.

    Functions

    Function Name

    Description

    (struct DListHead head)

    static void 

    Initializes a doubly linked list.

    (const struct DListHead head)

    static bool 

    Checks whether a doubly linked list is empty.

    (struct DListHead )

    static void 

    Removes a node from a doubly linked list.

    DListInsertHead (struct entry, struct head)

    static void 

    Inserts a node from the head of a doubly linked list.

    DListInsertTail (struct entry, struct head)

    static void 

    Inserts a node from the tail of a doubly linked list.

    DListMerge (struct list, struct DListHead head)

    static void 

    Merges two linked lists by adding the list specified by list to the head of the list specified by head and initializes the merged list.

    (HdfWorkQueue queue, char name)

    int32_t 

    Initializes a work queue.

    (HdfWork work, func, void arg)

    int32_t 

    Initializes a work item.

    HdfDelayedWorkInit ( work, HdfWorkFunc func, void arg)

    int32_t 

    Initializes a delayed work item.

    (HdfWork work)

    void 

    Destroys a work item.

    (HdfWorkQueue queue)

    void 

    Destroys a work queue.

    (HdfWork work)

    void 

    Destroys a delayed work item.

    (HdfWorkQueue queue, work)

    bool 

    Adds a work item to a work queue.

    HdfAddDelayedWork ( queue, HdfWork work, unsigned long ms)

    bool 

    Adds a delayed work item to a work queue.

    (HdfWork work)

    unsigned int 

    Obtains the status of a work item or delayed work item.

    (HdfWork work)

    bool 

    Cancels a work item. This function waits until the work item is complete.

    bool 

    Cancels a delayed work item.

    Details

    Macro Definition Documentation

    CONTAINER_OF

    Description:

    Obtains the address of a structure variable from its member address.

    Parameters:

    Name

    Description

    ptr Indicates the structure member address.
    type Indicates the structure type.
    member Indicates the structure member.

    DLIST_FIRST_ENTRY

    Description:

    Obtains the first node of a doubly linked list.

    Parameters:

    Name

    Description

    ptr Indicates the structure member address.
    type Indicates the structure type.
    member Indicates the structure member.

    DLIST_FOR_EACH_ENTRY

    1. #define DLIST_FOR_EACH_ENTRY( pos, head, type, member )
    1. Values: for ((pos) = [CONTAINER_OF]($api-api-SmartVision-Devices-DriverUtils.md#ga818b9cca761fe7bc18e4e417da772976)((head)->next, type, member); \
    2. &(pos)->member != (head); \
    3. (pos) = [CONTAINER_OF]($api-api-SmartVision-Devices-DriverUtils.md#ga818b9cca761fe7bc18e4e417da772976)((pos)->member.next, type, member))

    Description:

    Traverses all nodes in a doubly linked list.

    Parameters:

    Name

    Description

    pos Indicates the pointer to the structure variable.
    head Indicates the pointer to the linked list head.
    type Indicates the structure type.
    member Indicates the member type of the structure.

    DLIST_FOR_EACH_ENTRY_SAFE

    1. #define DLIST_FOR_EACH_ENTRY_SAFE( pos, tmp, head, type, member )
    1. Values: for ((pos) = [CONTAINER_OF]($api-api-SmartVision-Devices-DriverUtils.md#ga818b9cca761fe7bc18e4e417da772976)((head)->next, type, member), \
    2. (tmp) = [CONTAINER_OF]($api-api-SmartVision-Devices-DriverUtils.md#ga818b9cca761fe7bc18e4e417da772976)((pos)->member.next, type, member); \
    3. &(pos)->member != (head); \
    4. (pos) = (tmp), (tmp) = [CONTAINER_OF]($api-api-SmartVision-Devices-DriverUtils.md#ga818b9cca761fe7bc18e4e417da772976)((pos)->member.next, type, member))

    Description:

    Traverses all nodes in a doubly linked list. This function is used to delete the nodes pointed to by pos during traversal.

    Parameters:

    Name

    Description

    pos Indicates the pointer to the structure variable.
    tmp Indicates the pointer to the structure variable, pointing to the next node of pos.
    head Indicates the pointer to the linked list head.
    type Indicates the structure type.
    member Indicates the member type of the structure.
    1. #define DLIST_LAST_ENTRY( ptr, type, member ) [CONTAINER_OF]($api-api-SmartVision-Devices-DriverUtils.md#ga818b9cca761fe7bc18e4e417da772976)((ptr)->prev, type, member)

    Description:

    Obtains the last node of a doubly linked list.

    Parameters:

    Name

    Description

    ptr Indicates the structure member address.
    type Indicates the structure type.
    member Indicates the structure member.

    HDF_LOGD

    1. #define HDF_LOGD( fmt, arg... ) [printf]($api-api-SmartVision-Devices-IO.md#ga98631211a4a8aee62f572375d5b637be)("[HDF:D/" LOG_TAG "]" fmt "\r\n", ##arg)

    Description:

    Prints logs at the debug level.

    To use this function, you must define HDF_LOG_TAG, for example, #define HDF_LOG_TAG evt.

    HDF_LOGE

    1. #define HDF_LOGE( fmt, arg... ) [printf]($api-api-SmartVision-Devices-IO.md#ga98631211a4a8aee62f572375d5b637be)("[HDF:E/" LOG_TAG "]" fmt "\r\n", ##arg)

    Description:

    Prints logs at the error level.

    To use this function, you must define HDF_LOG_TAG, for example, #define HDF_LOG_TAG evt.

    HDF_LOGI

    1. #define HDF_LOGI( fmt, arg... ) [printf]($api-api-SmartVision-Devices-IO.md#ga98631211a4a8aee62f572375d5b637be)("[HDF:I/" LOG_TAG "]" fmt "\r\n", ##arg)

    Description:

    Prints logs at the information level.

    To use this function, you must define HDF_LOG_TAG, for example, #define HDF_LOG_TAG evt.

    HDF_LOGV

    1. #define HDF_LOGV( fmt, arg... ) [printf]($api-api-SmartVision-Devices-IO.md#ga98631211a4a8aee62f572375d5b637be)("[HDF:V/" LOG_TAG "]" fmt "\r\n", ##arg)

    Description:

    Prints logs at the verbose level.

    To use this function, you must define HDF_LOG_TAG, for example, #define HDF_LOG_TAG evt.

    HDF_LOGW

    Description:

    Prints logs at the warning level.

    To use this function, you must define HDF_LOG_TAG, for example, #define HDF_LOG_TAG evt.

    LOG_TAG_MARK_EXTEND

    1. #define LOG_TAG_MARK_EXTEND( HDF_TAG) #HDF_TAG

    Description:

    Add quotation mark

    Typedef Documentation

    HdfWorkFunc

    1. typedef void(* HdfWorkFunc) (void *)

    Description:

    Describes a work execution function type.

    The thread of the work queue executes this function after the work item is added to the work queue.

    Enumeration Type Documentation

    anonymous enum

    1. anonymous enum

    Description:

    Enumerates statuses of a work item or a delayed work item.

    HDF_STATUS

    1. enum [HDF_STATUS]($api-api-SmartVision-Devices-DriverUtils.md#ga7e01536ecbe9b17563dd3fe256202a67)

    Description:

    Enumerates HDF return value types.

    Enumerator

    Description

    HDF_SUCCESS 

    The operation is successful.

    HDF_FAILURE 

    Failed to invoke the OS underlying function.

    HDF_ERR_NOT_SUPPORT 

    Not supported.

    HDF_ERR_INVALID_PARAM 

    Invalid parameter.

    HDF_ERR_INVALID_OBJECT 

    Invalid object.

    HDF_ERR_MALLOC_FAIL 

    Memory allocation fails.

    HDF_ERR_TIMEOUT 

    Timeout occurs.

    HDF_ERR_THREAD_CREATE_FAIL 

    Failed to create a thread.

    HDF_ERR_QUEUE_FULL 

    The queue is full.

    HDF_ERR_DEVICE_BUSY 

    The device is busy.

    HDF_ERR_IO 

    I/O error.

    HDF_ERR_BAD_FD 

    Incorrect file descriptor.

    HDF_BSP_ERR_OP 

    Failed to operate a BSP module.

    HDF_ERR_BSP_PLT_API_ERR 

    The platform API of the BSP module is incorrect.

    HDF_PAL_ERR_DEV_CREATE 

    Failed to create a BSP module device.

    HDF_PAL_ERR_INNER 

    Internal error codes of the BSP module.

    HDF_DEV_ERR_NO_MEMORY 

    Failed to allocate memory to the device module.

    HDF_DEV_ERR_NO_DEVICE 

    The device module has no device.

    HDF_DEV_ERR_NO_DEVICE_SERVICE 

    The device module has no device service.

    HDF_DEV_ERR_DEV_INIT_FAIL 

    Failed to initialize a device module.

    HDF_DEV_ERR_PUBLISH_FAIL 

    The device module failed to release a service.

    HDF_DEV_ERR_ATTACHDEV_FAIL 

    Failed to attach a device to a device module.

    HDF_DEV_ERR_NODATA 

    Failed to read data from a device module.

    HDF_DEV_ERR_NORANGE 

    The device module data is out of range.

    HDF_DEV_ERR_OP 

    Failed to operate a device module.

    Function Documentation

    DListHeadInit()

    1. static void DListHeadInit (struct [DListHead]($api-api-SmartVision-Devices-DListHead.md) * head)

    Description:

    Initializes a doubly linked list.

    Parameters:

    Name

    Description

    head Indicates the pointer to the linked list DListHead. The parameter cannot be empty.
    1. static void DListInsertHead (struct [DListHead]($api-api-SmartVision-Devices-DListHead.md) * entry, struct [DListHead]($api-api-SmartVision-Devices-DListHead.md) * head )

    Description:

    Inserts a node from the head of a doubly linked list.

    Parameters:

    Name

    Description

    entry Indicates the pointer to the node to insert. For details, see DListHead. The parameter cannot be empty.
    head Indicates the pointer to the linked list . The parameter cannot be empty.

    DListInsertTail()

    Description:

    Inserts a node from the tail of a doubly linked list.

    Parameters:

    Name

    Description

    entry Indicates the pointer to the node to insert. For details, see DListHead. The parameter cannot be empty.
    head Indicates the pointer to the linked list . The parameter cannot be empty.

    DListIsEmpty()

    1. static bool DListIsEmpty (const struct [DListHead]($api-api-SmartVision-Devices-DListHead.md) * head)

    Description:

    Checks whether a doubly linked list is empty.

    Parameters:

    Name

    Description

    head Indicates the pointer to the linked list DListHead. The parameter cannot be empty.

    DListMerge()

    1. static void DListMerge (struct [DListHead]($api-api-SmartVision-Devices-DListHead.md) * list, struct [DListHead]($api-api-SmartVision-Devices-DListHead.md) * head )

    Description:

    Merges two linked lists by adding the list specified by list to the head of the list specified by head and initializes the merged list.

    Parameters:

    Name

    Description

    list Indicates the pointer to the linked list . The parameter cannot be empty.
    head Indicates the pointer to the linked list DListHead. The parameter cannot be empty.

    DListRemove()

    1. static void DListRemove (struct [DListHead]($api-api-SmartVision-Devices-DListHead.md) * entry)

    Removes a node from a doubly linked list.

    Parameters:

    Name

    Description

    entry Indicates the pointer to the node to remove. For details, see . The parameter cannot be empty.

    HdfAddDelayedWork()

    Description:

    Adds a delayed work item to a work queue.

    A delayed work item is added to a work queue after the configured delayed time (ms), and the thread of the work queue executes the work function.

    Parameters:

    Name

    Description

    queue Indicates the pointer to the work queue HdfWorkQueue.
    work Indicates the pointer to the delayed work item .

    Returns:

    Returns true if the operation is successful; returns false otherwise.

    HdfAddWork()

    1. bool HdfAddWork ([HdfWorkQueue]($api-api-SmartVision-Devices-HdfWorkQueue.md) * queue, [HdfWork]($api-api-SmartVision-Devices-HdfWork.md) * work )

    Description:

    Adds a work item to a work queue.

    After a work item is added to a work queue, the thread of the work queue executes the function of the work item.

    Parameters:

    Name

    Description

    queue Indicates the pointer to the work queue HdfWorkQueue.
    work Indicates the pointer to the work item .

    Returns:

    Returns true if the operation is successful; returns false otherwise.

    HdfCancelDelayedWorkSync()

    1. bool HdfCancelDelayedWorkSync ([HdfWork]($api-api-SmartVision-Devices-HdfWork.md) * work)

    Description:

    Cancels a delayed work item.

    Parameters:

    Name

    Description

    work Indicates the pointer to the delayed work item HdfWork.

    Returns:

    Returns true if the operation is successful; returns false otherwise.

    HdfCancelWorkSync()

    1. bool HdfCancelWorkSync ([HdfWork]($api-api-SmartVision-Devices-HdfWork.md) * work)

    Description:

    Cancels a work item. This function waits until the work item is complete.

    Parameters:

    Returns:

    Returns true if the operation is successful; returns false otherwise.

    HdfDelayedWorkDestroy()

    1. void HdfDelayedWorkDestroy ([HdfWork]($api-api-SmartVision-Devices-HdfWork.md) * work)

    Description:

    Destroys a delayed work item.

    Parameters:

    Name

    Description

    work Indicates the pointer to the delayed work item .

    HdfDelayedWorkInit()

    1. int32_t HdfDelayedWorkInit ([HdfWork]($api-api-SmartVision-Devices-HdfWork.md) * work, [HdfWorkFunc]($api-api-SmartVision-Devices-DriverUtils.md#ga30665d61b03fae4a2ebc778c3d775ce5) func, void * arg )

    Description:

    Initializes a delayed work item.

    This function uses func and arg to initialize a work item. The work item is added to a work queue after the configured delayed time. The thread of the work queue executes this function, and arg is passed to func.

    Parameters:

    Name

    Description

    work Indicates the pointer to the delayed work item HdfWork.
    func Indicates the work execution function.
    arg Indicates the pointer to the argument of the work execution function.

    Returns:

    Returns a value listed below:

    HDF_STATUS

    Description

    HDF_SUCCESS

    The operation is successful.

    HDF_ERR_INVALID_PARAM

    Invalid parameter.

    HDF_ERR_MALLOC_FAIL

    Memory allocation fails.

    HdfWorkBusy()

    1. unsigned int HdfWorkBusy ([HdfWork]($api-api-SmartVision-Devices-HdfWork.md) * work)

    Description:

    Obtains the status of a work item or delayed work item.

    Parameters:

    Name

    Description

    work Indicates the pointer to the work item or delayed work item HdfWork.

    Returns:

    Returns HDF_WORK_BUSY_PENDING if the work item is pending; returns HDF_WORK_BUSY_RUNNING if the work item is running.

    HdfWorkDestroy()

    1. void HdfWorkDestroy ([HdfWork]($api-api-SmartVision-Devices-HdfWork.md) * work)

    Description:

    Destroys a work item.

    Parameters:

    Name

    Description

    work Indicates the pointer to the work item .

    HdfWorkInit()

    1. int32_t HdfWorkInit ([HdfWork]($api-api-SmartVision-Devices-HdfWork.md) * work, [HdfWorkFunc]($api-api-SmartVision-Devices-DriverUtils.md#ga30665d61b03fae4a2ebc778c3d775ce5) func, void * arg )

    Description:

    Initializes a work item.

    This function uses func and arg to initialize a work item. After the work item is added to a work queue, the thread of the work queue executes this function, and arg is passed to func.

    Parameters:

    Name

    Description

    work Indicates the pointer to the work item HdfWork.
    func Indicates the work execution function.
    arg Indicates the pointer to the argument of the work execution function.

    Returns:

    Returns a value listed below:

    HDF_STATUS

    Description

    HDF_SUCCESS

    The operation is successful.

    HDF_ERR_INVALID_PARAM

    Invalid parameter.

    HDF_ERR_MALLOC_FAIL

    Memory allocation fails.

    1. void HdfWorkQueueDestroy ([HdfWorkQueue]($api-api-SmartVision-Devices-HdfWorkQueue.md) * queue)

    Description:

    Destroys a work queue.

    Parameters:

    Name

    Description

    queue Indicates the pointer to the work queue .

    HdfWorkQueueInit()

      Description:

      Initializes a work queue.

      When a work queue is initialized, a thread is created. The thread cyclically executes the work items in the work queue, that is, executes their functions.

      Parameters:

      Name

      Description

      queue Indicates the pointer to the work queue OsalWorkQueue.
      name Indicates the pointer to the work queue name.

      Returns:

      Returns a value listed below:

      HDF_STATUS

      Description

      HDF_SUCCESS

      The operation is successful.

      HDF_ERR_INVALID_PARAM

      Invalid parameter.

      HDF_ERR_MALLOC_FAIL

      Memory allocation fails.