Exemple #1
0
/****************************************************************
FunctionName    :   GAgent_Local_ExtractOnePacket
Description     :   extract one packet from local cycle buf, and 
                    put data into buf.Will change pos_start
buf             :   dest buf
return          :   >0 the local packet data length.
                    <0 don't have one whole packet data
****************************************************************/
int32 GAgent_Local_ExtractOnePacket(uint8 *buf)
{
    uint8 data;
    uint32 i = 0;

    while((pos_start & HAL_BUF_MASK) != (pos_current & HAL_BUF_MASK))
    {
        data = __halbuf_read(pos_start);

        if(LOCAL_HAL_REC_SYNCHEAD1 == gu8LocalHalStatus)
        {
            if(MCU_HDR_FF == data)
            {
                gu8LocalHalStatus = LOCAL_HAL_REC_SYNCHEAD2;

                guiLocalPacketStart = pos_start;
            }
        }
        else if(LOCAL_HAL_REC_SYNCHEAD2 == gu8LocalHalStatus)
        {
            if(MCU_HDR_FF == data)
            {
                gu8LocalHalStatus = LOCAL_HAL_REC_DATALEN1;
            }
            else
            {
                gu8LocalHalStatus = LOCAL_HAL_REC_SYNCHEAD1;
            }
        }
        else
        {
            if(halRecKeyWord)
            {
                /* 前面接收到0xff */
                halRecKeyWord = 0;
                if(0x55 == data)
                {
                    data = 0xff;
                    move_data_backward(hal_RxBuffer, pos_start + 1, pos_current, 1);
                    pos_current--;
                    pos_start--;

                }
                else if(MCU_HDR_FF == data)
                {
                    /* 新的一包数据,前面数据丢弃 */
                    gu8LocalHalStatus = LOCAL_HAL_REC_DATALEN1;
                    guiLocalPacketStart = pos_start - 1;
                    pos_start++;
                    continue;
                }
                else
                {
                    if(LOCAL_HAL_REC_DATALEN1 == gu8LocalHalStatus)
                    {
                        /* 说明前面接收到的0xff和包头0xffff是连在一起的,以最近的0xffff作为包头起始,
                         * 当前字节作为len字节进行解析
                         */
                        guiLocalPacketStart = pos_start - 2;
                    }
                    else
                    {
                        gu8LocalHalStatus = LOCAL_HAL_REC_SYNCHEAD1;
                        pos_start++;
                        continue;
                    }
                }

            }
            else
            {
                if(MCU_HDR_FF == data)
                {
                    halRecKeyWord = 1;
                    pos_start++;
                    continue;
                }
            }

            if(LOCAL_HAL_REC_DATALEN1 == gu8LocalHalStatus)
            {
                gu16LocalPacketDataLen = data;
                gu16LocalPacketDataLen = (gu16LocalPacketDataLen << 8) & 0xff00;
                gu8LocalHalStatus = LOCAL_HAL_REC_DATALEN2;
            }
            else if(LOCAL_HAL_REC_DATALEN2 == gu8LocalHalStatus)
            {
                gu16LocalPacketDataLen += data;
                gu8LocalHalStatus = LOCAL_HAL_REC_DATA;

                if(0 == gu16LocalPacketDataLen)
                {
                    /* invalid packet */
                    gu8LocalHalStatus = LOCAL_HAL_REC_SYNCHEAD1;
                }
            }
            else
            {
                /* Rec data */
                gu16LocalPacketDataLen--;
                if(0 == gu16LocalPacketDataLen)
                {
                    /* 接收到完整一包数据,拷贝到应用层缓冲区 */
                    pos_start++;
                    i = 0;
                    while(guiLocalPacketStart != pos_start)
                    {
                        buf[i] = __halbuf_read(guiLocalPacketStart++);
                        i++;
                    }

                    return i;
                }
            }
            
        }
        
        pos_start++;
    }

    return RET_FAILED;
}
/****************************************************************
FunctionName    :   Local_decode_data
Description     :   decode local data in local cycle buf.transfer 0xff55 to 0xff.
start           :   the posation in local cycle buf where will be decode  
length          :   decode data length

will change the global pointer "put"--pos_current
****************************************************************/
void hal_decode_data(uint32 start, uint32 length)
{
    static uint8 isSyncHead = 0;
    uint8 data;
    uint32 i;
    uint32 offRec;
    uint32 invalidLen;
    int32 ret;
    
    if(0 == length)
    {
        return ;
    }

    offRec = start;
    for(i = 0; i < length; i++)
    {
        data = __halbuf_read(offRec);
        if(isSyncHead)
        {
            isSyncHead = 0;
            /* check if is a new packet */
            if(0xFF == data)
            {
                /* check if pre byte is in one packet */
                ret = check_isInPacket(offRec - 1);
                if(0 == ret)
                {   /* new packet */
                    offRec++;
                    continue;
                }
                else
                {
                    ret = find_packet_backward(offRec);
                    invalidLen = get_data_len(ret, offRec + 1);
                    move_data_backward(hal_RxBuffer, offRec + 1, pos_current, invalidLen);
                    offRec -= invalidLen;
                    pos_current = pos_current - invalidLen;
                }
            }
            /* check if is 0xff 0x55 */
            else if(0x55 == data)
            {
                if((offRec & HAL_BUF_MASK) == ((pos_current - 1) & HAL_BUF_MASK))
                {
                    /* the last byte has received */
                    __halbuf_write(offRec, 0x00);
                }
                else
                {
                    move_data_backward(hal_RxBuffer, offRec + 1, pos_current, 1);
                }
                offRec -= 1;
                pos_current -= 1;
            }
            /* invalid packet */
            else
            {
                /* 找到当前字节处于哪一个packet */
                ret = find_packet_backward(offRec);
                invalidLen = get_data_len(ret, offRec + 1);
                move_data_backward(hal_RxBuffer, offRec + 1, pos_current, invalidLen);
                offRec -= invalidLen;
                pos_current = pos_current - invalidLen;
            }
        }
        else if(MCU_HDR_FF == data)
        {
            isSyncHead = 1;
        }
        offRec++;
    }
}