#include "bsp_queue.h"

#include "kit_data.h"
#include "kit_debug.h"


//首先获取队列地址,调用函数利用地址进行数据填充
KitResult bsp_queue_push_pointer(QueueItem* item, void **prt)
{
    uint32_t  tmp;
    KitResult res = kKitResult_ParamErr;
    
    KIT_ASSERT_PARAM((item != NULL) && (prt != NULL));
    
    if((item != NULL) && (prt != NULL))
    {
        tmp = (item->push_index + 1) % item->max_cnt; 
        if (tmp != item->pop_index)
        {        
            *prt = item->buf + (item->push_index * item->cell_len);
            item->push_index = tmp;
            res = kKitResult_Ok;
        }
        else
        {        
            res = kKitResult_OutRange;
        }
    }

    KIT_ASSERT_RES(0, res);
    return res;
}

KitResult bsp_queue_pop_pointer(QueueItem* item, void **prt)
{
    KitResult res = kKitResult_ParamErr;
    
    KIT_ASSERT_PARAM((item != NULL) && (prt != NULL));
    
    if((item != NULL) && (prt != NULL))
    {
        if (item->push_index != item->pop_index)
        {
            *prt = item->buf + (item->pop_index * item->cell_len);
            item->pop_index = (item->pop_index + 1)%item->max_cnt;
            res = kKitResult_Ok;
        }
        else
        {
            res = kKitResult_Null;
        }
    }
    
    return res;
}


/*****************************************************************************
*函数名称:PushEmsCanFrameToQueue
*函数功能:将CAN数据帧压入队列
*参    数:const CanMsg*  msg
*返 回 值:返回CAN_OK表示成功,其他失败
*修订信息:
******************************************************************************/
KitResult bsp_queue_push_instance(QueueItem* item, uint8_t *ins)
{
    void *prt;
    KitResult res = kKitResult_ParamErr;
    KIT_ASSERT_PARAM((item != NULL) && (ins != NULL));
    
    if((item != NULL) && (ins != NULL))
    {         
        res = bsp_queue_push_pointer(item, &prt);
        if(res == kKitResult_Ok)
        {
            kit_copy_buf(prt, ins, item->cell_len);
        }
    }

    return res;
}
/*****************************************************************************
 *函数名称:PopEmsCanFrameFromQueue
 *函数功能:将CAN数据帧弹出队列
 *参    数:CanMsg*  msg
 *返 回 值:返回CAN_OK表示成功,其他失败
 *修订信息:
 ******************************************************************************/
KitResult bsp_queue_pop_instance(QueueItem* item, uint8_t *ins)
{
    void *prt;
    KitResult res = kKitResult_ParamErr;
    KIT_ASSERT_PARAM((item != NULL) && (ins != NULL));
    
    if((item != NULL) && (ins != NULL))
    {  
        res = bsp_queue_pop_pointer(item, &prt);
        if(res == kKitResult_Ok)
        {
            kit_copy_buf(ins, prt, item->cell_len);
        }
    }
        
    return res;
}

bool bsp_queue_is_empty(QueueItem* item)
{
    bool res = false;
    
    KIT_ASSERT_PARAM(item != NULL);
    
    if(item != NULL)
    {
        res = (item->push_index == item->pop_index);
    }
    return res;
}

bool bsp_queue_is_full(QueueItem* item)
{
    bool res = false;
    
    KIT_ASSERT_PARAM(item != NULL);
    
    if(item != NULL)
    {
        res = (((item->push_index + 1) % item->max_cnt) == item->pop_index);
    }
    return res;
}