/**
 * @file hv_comm_SafeQueue.h
 * @brief Header file of SafeQueue.
 *
 * @verbatim
 * ==============================================================================
 *                        ##### How to use #####
 * ==============================================================================
 *
 *
 * @endverbatim
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2023-05-18
 */

#ifndef __SDK_COMMON_UTIL_HVQUEUE_INC_HV_SAFE_QUEUE_H__
#define __SDK_COMMON_UTIL_HVQUEUE_INC_HV_SAFE_QUEUE_H__
#include "hv_comm_DataType.h"
#include "hv_comm_Assert.h"

#define DECLARE_HS_SAFE_QUEUE(NAME, QUEUE_LENGTH, ELEM_SIZE) \
        typedef struct _##NAME##Elem\
        {\
            UCHAR8 ucBuffer[ELEM_SIZE];\
            USHORT16 bufferSize;\
        }__attribute__((aligned(4)))NAME##Elem;\
        typedef struct _##NAME\
        {\
            NAME##Elem stElem[QUEUE_LENGTH];\
            USHORT16 m_usCount;\
        }__attribute__((aligned(4)))NAME;\
        VOID NAME##SafeQueueInit(NAME* pstHsQueue);\
        NAME##Elem* NAME##SafeQueueEnqueue(NAME* pstHsQueue, VOID* pvdata, USHORT16 usSize);\
        
#define DEFINE_HS_SAFE_QUEUE(NAME, QUEUE_LENGTH, ELEM_SIZE)\
        VOID NAME##SafeQueueInit(NAME* pstHsQueue)\
        {\
            memset(pstHsQueue, 0, sizeof(NAME));\
            return;\
        }\
        NAME##Elem* NAME##SafeQueueEnqueue(NAME* pstHsQueue, VOID* pvdata, USHORT16 usSize)\
        {\
            NAME##Elem* pElem = NULL;\
            HV_ASSERT_VALID_PTR_RET_NO_LOG(pstHsQueue, NULL);\
            HV_ASSERT_VALID_PTR_RET_NO_LOG(pvdata, NULL);\
            HV_ASSERT_TRUE_RET_NO_LOG(usSize <= ELEM_SIZE, NULL);\
            HV_ASSERT_TRUE_RET_NO_LOG(pstHsQueue->m_usCount < QUEUE_LENGTH, NULL);\
            memcpy(pstHsQueue->stElem[pstHsQueue->m_usCount].ucBuffer, pvdata, usSize);\
            pstHsQueue->stElem[pstHsQueue->m_usCount].bufferSize = usSize;\
            pElem = &pstHsQueue->stElem[pstHsQueue->m_usCount];\
            pstHsQueue->m_usCount++;\
            pstHsQueue->m_usCount = pstHsQueue->m_usCount % QUEUE_LENGTH;\
            return pElem;\
        }

/*For Test Eg*/
/*DECLARE_HS_SAFE_QUEUE(SystemQueue, 50, sizeof(Event)); .h file*/
/* DEFINE_HS_SAFE_QUEUE(SystemQueue, 50, sizeof(Event)); .c file*/
/* SystemQueue g_SystemQueue; .c file*/

#endif/*endif ----  __SDK_COMMON_UTIL_HSQUEUE_INC_HS_SAFE_QUEUE_H__ */
