
#ifndef J1939_QUEUE_C_
#define J1939_QUEUE_C_
#include"j1939.h"
//#include "RTE_APP.h"
/*****************************************************************************
*函数名称:J1939_BufferSet
*函数功能:将目标内存设置成一个值
*参    数:uint8_T* J1939_PAGED dest目标内存指针
*         uint8_T value设置的值
*         uint16_T size内存数据大小
*返 回 值:无
*作    者:Admin
*修订信息:
******************************************************************************/
void J1939_BufferSet(void* J1939_PAGED dest, uint8_T value, uint16_T size)
{ 
	//uint8_T*J1939_PAGED data = (uint8_T*J1939_PAGED)dest;
	//uint16_T index;
	//目标为空无效
	if(dest == NULL)
	{
		return;
	}else
        {
	//设置成一个值
	//for (index = 0; index < size; ++index)
	//{
	//	data[index] = value;
	//}
            memset(dest,value,size);
        }
}
/*****************************************************************************
*函数名称:J1939_BufferCopy
*函数功能:数据拷贝,将源内存的数据拷贝到目标内存中.
*参    数:uint8_T* J1939_PAGED dest目标内存指针
*         const uint8_T* J1939_PAGED src源内存指针
*         uint16_T size内存数据大小
*返 回 值:无
*作    者:Admin
*修订信息:
******************************************************************************/
void J1939_BufferCopy(void* J1939_PAGED dest, const void* J1939_PAGED src, uint16_T size)
{  
	//uint8_T*J1939_PAGED  dest_data =  (uint8_T*J1939_PAGED)dest;
	//const uint8_T*J1939_PAGED  src_data =  (uint8_T*J1939_PAGED)src;
	//uint16_T index;
	//目标和源无效
	if(dest == NULL || src == NULL)
	{
		return;
	}else
        {
	
	//for (index = 0; index < size; ++index)
	//{
	//	dest_data[index] = src_data[index];
	//}
            memcpy(dest,src,size);
        }
}
/*****************************************************************************
*函数名称:J1939_PushCanFrameToQueue
*函数功能:将CAN数据帧压入队列
*参    数:J1939CanFrameQueue* J1939_PAGED frame_queue CAN数据帧队列指针
*         const J1939CanFrame* J1939_PAGED frame要入队列的CAN数据帧指针
*返 回 值:返回J1939_OK表示成功,其他失败
*作    者:Admin
*修订信息:
******************************************************************************/
J1939Result J1939_PushCanFrameToQueue(J1939CanFrameQueue* J1939_PAGED frame_queue, const J1939CanFrame* J1939_PAGED frame)
{
    
    //检查参数
    if((frame_queue == NULL)||(frame == NULL))
    {
        return J1939_PTR_NULL;   
    }
    
    //CAN数据放入1939缓存
    if(frame_queue->cnt < J1939_CAN_FRAME_QUEUE_NUM)
    {
        J1939_BufferCopy(&frame_queue->frame[frame_queue->push_index],frame,sizeof(J1939CanFrame));
        
        frame_queue->push_index++;
        if(frame_queue->push_index >= J1939_CAN_FRAME_QUEUE_NUM)
        {
            frame_queue->push_index = 0;   
        }
        
        frame_queue->cnt++;
        
        return J1939_OK;
    } 
    else
    {
       
       return J1939_QUEUE_FULL;   
    }
}
/*****************************************************************************
*函数名称:J1939_PopCanFrameFromQueue
*函数功能:将CAN数据帧弹出队列
*参    数:const J1939CanFrameQueue* J1939_PAGED frame_queue CAN数据帧队列指针
*         J1939CanFrame* J1939_PAGED frame 弹出的CAN数据帧指针
*返 回 值:返回J1939_OK表示成功,其他失败
*作    者:Admin
*修订信息:
******************************************************************************/
J1939Result J1939_PopCanFrameFromQueue(J1939CanFrameQueue* J1939_PAGED frame_queue, J1939CanFrame* J1939_PAGED frame)
{
   // OS_CPU_SR cpu_sr=0;
    //检查参数
    if((frame_queue == NULL)||(frame == NULL))
    {
        return J1939_PTR_NULL;   
    }
   
    //从缓存拿数据
    if(frame_queue->cnt > 0)
    {
        J1939_BufferCopy(frame, &frame_queue->frame[frame_queue->pop_index],sizeof(J1939CanFrame));
        
        frame_queue->pop_index++;
        if(frame_queue->pop_index >= J1939_CAN_FRAME_QUEUE_NUM)
        {
            frame_queue->pop_index = 0;   
        }
        
        frame_queue->cnt--;
          
        return J1939_OK;
    }
    else
    {      
        return J1939_QUEUE_EMPTY;  
    }
}
/*****************************************************************************
*函数名称:J1939_PushRxMsgToQueue
*函数功能:将接收到的J1939消息压入队列
*参    数:J1939RxMsgQueue* J1939_PAGED msg_queue J1939接收消息队列指针
*         const J1939RxMsg* J1939_PAGED msg要入队列的消息指针
*返 回 值:返回J1939_OK表示成功,其他失败
*作    者:Admin
*修订信息:
******************************************************************************/
J1939Result J1939_PushRxMsgToQueue(J1939RxMsgQueue* J1939_PAGED msg_queue, const J1939Msg* J1939_PAGED msg)
{
   // OS_CPU_SR cpu_sr=0;
    uint16_T  byte_cnt = 0;
    
    //检查参数
    if((msg_queue == NULL)||(msg == NULL))
    {
        return J1939_PTR_NULL;   
    }
    
    //多包数据放入缓存
    if(msg_queue->cnt < J1939_RX_MSG_QUEUE_MAX_NUM) 
    {
    	//检查长度
        if(msg->msg_len > msg_queue->msg[msg_queue->push_index].msg_max_len)
        {
            byte_cnt = msg_queue->msg[msg_queue->push_index].msg_max_len;
        }
        else
        {
            byte_cnt = msg->msg_len;
        }
            
        msg_queue->msg[msg_queue->push_index].msg_len = msg->msg_len;

        J1939_BufferCopy(msg_queue->msg[msg_queue->push_index].data, msg->data, byte_cnt);
        
        msg_queue->msg[msg_queue->push_index].id.value = msg->id.value;
        msg_queue->msg[msg_queue->push_index].pgn = msg->pgn;
        msg_queue->msg[msg_queue->push_index].dest_addr = msg->dest_addr;
        msg_queue->msg[msg_queue->push_index].source_addr = msg->source_addr;
             
        //更新位置
        msg_queue->push_index++;
        if(msg_queue->push_index >= J1939_RX_MSG_QUEUE_MAX_NUM)
        {
            msg_queue->push_index = 0;   
        }
        msg_queue->cnt++;
       
        return J1939_OK;
    } 
    else 
    {
        return J1939_QUEUE_FULL;   
    }
}

/*****************************************************************************
*函数名称:J1939_PopRxMsgFromQueue
*函数功能:将接收到的J1939消息弹出队列
*参    数: J1939RxMsgStack* J1939_PAGED msg_queue  J1939接收消息队列指针
*         J1939RxMsg* J1939_PAGED msg msg要出栈的消息指针
*返 回 值:返回J1939_OK表示成功,其他失败
*作    者:Admin
*修订信息:
******************************************************************************/
J1939Result J1939_PopRxMsgFromQueue( J1939RxMsgQueue* J1939_PAGED msg_queue, J1939Msg* J1939_PAGED msg)
{
   // OS_CPU_SR cpu_sr = 0;
    
    //检查参数
    if((msg_queue == NULL)||(msg == NULL))
    {
        return J1939_PTR_NULL;   
    }
    
    //从缓存拿数据
     if(msg_queue->cnt > 0) 
     {
        if(msg_queue->msg[msg_queue->pop_index].msg_len > msg->msg_max_len)
        {
            msg->msg_len = msg->msg_max_len;
        }
        else
        {
            msg->msg_len = msg_queue->msg[msg_queue->pop_index].msg_len;
        }

        J1939_BufferCopy(msg->data,msg_queue->msg[msg_queue->pop_index].data,msg->msg_len);
        
        msg->id.value = msg_queue->msg[msg_queue->pop_index].id.value;
        msg->pgn = msg_queue->msg[msg_queue->pop_index].pgn;
        msg->dest_addr = msg_queue->msg[msg_queue->pop_index].dest_addr;
        msg->source_addr = msg_queue->msg[msg_queue->pop_index].source_addr;

        //更新位置
        msg_queue->pop_index++;
        if(msg_queue->pop_index >= J1939_RX_MSG_QUEUE_MAX_NUM)
        {
            msg_queue->pop_index = 0;   
        }
        
        msg_queue->cnt--;      
        return J1939_OK;
    }
    else
    { 
         return J1939_QUEUE_EMPTY;  
    }
}

#endif
