/**
 * @file hv_vos_Queue.c
 * @brief vos queue 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 "queue.h"

struct _HV_VOS_QUEUE_S{
    QueueHandle_t handler;
};

HV_VOS_QUEUE_S *Hv_Vos_QueueInit(Hv_Vos_QUEUE_INIT_PARAMS *param)
{
    
    HV_VOS_QUEUE_S *self = NULL;
    HV_ASSERT(param);
    self = HV_MALLOC_OBJECT(HV_VOS_QUEUE_S);
    self->handler = xQueueCreate(param->queueLen, param->itemSize);
    if(self->handler == NULL){
        HV_FREE_OBJECT(self);
        self = NULL; 
    }
    return self;
}

BOOL Hv_Vos_QueueSend(HV_VOS_QUEUE_S *self, const void *buffer)
{
   BaseType_t ret = 0;
   ret = xQueueSend(self->handler, buffer, portMAX_DELAY);   
   return ret == pdPASS;
}

BOOL Hv_Vos_QueueTimedSend(HV_VOS_QUEUE_S *self, const void *buffer, UINT32 waitMs)
{
   BaseType_t ret = 0;
   TickType_t ticks = pdMS_TO_TICKS(waitMs);
   ret = xQueueSend(self->handler, buffer, ticks);   
   return ret == pdPASS;
}

BOOL Hv_Vos_QueueSendFromISR(HV_VOS_QUEUE_S *self, const void *buffer)
{
   BaseType_t ret = 0;
   BaseType_t xHigherPriorityTaskWoken = pdHV_FALSE;
   ret = xQueueSendFromISR(self->handler, buffer, &xHigherPriorityTaskWoken); 
   portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
   return ret == pdPASS;
}

BOOL Hv_Vos_QueueSendFromISR_unYield(HV_VOS_QUEUE_S *self, const void *buffer)
{
   BaseType_t ret = 0;
   BaseType_t xHigherPriorityTaskWoken = pdHV_FALSE;
   ret = xQueueSendFromISR(self->handler, buffer, &xHigherPriorityTaskWoken); 
   return ret == pdPASS;
}

BOOL Hv_Vos_QueueRecv(HV_VOS_QUEUE_S *self, void *buffer)
{
    BaseType_t ret = 0;
    ret = xQueueReceive( self->handler, buffer, portMAX_DELAY); 
    return ret == pdPASS;
}

BOOL Hv_Vos_QueueTimedRecv(HV_VOS_QUEUE_S *self, void *buffer, UINT32 waitMs)
{
    BaseType_t ret = 0;
    TickType_t ticks = pdMS_TO_TICKS(waitMs);
    ret = xQueueReceive( self->handler, buffer, ticks); 
    return ret == pdPASS;
}


BOOL Hv_Vos_QueueReceiveFromISR(HV_VOS_QUEUE_S *self, void *buffer)
{
   BaseType_t ret = 0;
   BaseType_t xHigherPriorityTaskWoken = pdHV_FALSE;
   ret = xQueueReceiveFromISR(self->handler, buffer, &xHigherPriorityTaskWoken); 
   portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
   return ret == pdPASS;
}

void Hv_Vos_QueueCleanup(HV_VOS_QUEUE_S *self){   
    if (self == NULL)
    {
        vQueueDelete(NULL);     
    } 
    else 
    {
        vQueueDelete(self->handler);
    }
    HV_FREE_OBJECT(self);
}


ULONG32 Hv_Vos_QueueMessagesWaiting(const HV_VOS_QUEUE_S *self)
{   
    UBaseType_t ret = 0;
    ret = uxQueueMessagesWaiting(self->handler);
    return ret;
}
ULONG32 Hv_Vos_QueueMessagesWaitingFromISR(const HV_VOS_QUEUE_S *self)
{   
    UBaseType_t ret = 0;
    ret = uxQueueMessagesWaitingFromISR(self->handler);
    return ret;
}