/**
 * @file hv_vos_EventGroup.c
 * @brief vos eventgroup source code
 * @details 
 * @author HiView SoC Software Team
 * @version 0.0.1
 * @date 20230301
 * @copyright Copyright(c),2023-3, Hiview Software. All rights reserved.
 * @par History: 
 * <table>
 * <tr><th>author                     <th>date           <th>description
 * <tr><td>HiView SoC Software Team   <td>20230301       <td>Create and first Version.
 * </table>
**/

#include "hv_vos_Comm.h"
#include "FreeRTOS.h"
#include "event_groups.h"


struct _HV_VOS_EVENT_GROUP_S{
    EventGroupHandle_t tHandle;
};

HV_VOS_EVENT_GROUP_S *Hv_Vos_EventGroupInit(void)
{
    HV_VOS_EVENT_GROUP_S *psSelf = NULL;

    psSelf = HV_MALLOC_OBJECT(HV_VOS_EVENT_GROUP_S);
    psSelf->tHandle = xEventGroupCreate();
    if(psSelf->tHandle == NULL)
    {
        HV_FREE_OBJECT(psSelf);
        psSelf = NULL; 
    }
    return psSelf;
}

UINT32 Hv_Vos_EventGroupWaitBits(HV_VOS_EVENT_GROUP_S *psSelf, UINT32 uiBitsToWait)
{
    EventBits_t xEventRet = xEventGroupWaitBits(psSelf->tHandle, 
                        uiBitsToWait,
                        pdHV_TRUE, 
                        pdHV_FALSE, 
                        (TickType_t)portMAX_DELAY);
    return (UINT32)xEventRet;
}

UINT32 Hv_Vos_EventGroupTimedWaitBits(HV_VOS_EVENT_GROUP_S *psSelf, UINT32 uiBitsToWait, UINT32 uiWaitMs)
{
    TickType_t ticks = pdMS_TO_TICKS(uiWaitMs);
    EventBits_t xEventRet = xEventGroupWaitBits(psSelf->tHandle, 
                        uiBitsToWait,
                        pdHV_TRUE, 
                        pdHV_FALSE, 
                        ticks);
    return (UINT32)xEventRet;
}

UINT32 Hv_Vos_EventGroupSetBits(HV_VOS_EVENT_GROUP_S *psSelf, UINT32 uiBitsToSet)
{
    EventBits_t xEventRet = xEventGroupSetBits(psSelf->tHandle, uiBitsToSet);
    return (UINT32)xEventRet;
}

BOOL Hv_Vos_EventGroupSetBitsFromISR(HV_VOS_EVENT_GROUP_S *psSelf, UINT32 uiBitsToSet)
{
    BaseType_t xHigherPriorityTaskWoken, xResult;
    xResult = xEventGroupSetBitsFromISR(psSelf->tHandle, uiBitsToSet, &xHigherPriorityTaskWoken);
    return xResult != pdFAIL ;
}
