Ejemplo n.º 1
0
bool PacketBuffer::GetPacket(Packet &packet)
{
    // check packet
    if (PacketVaild()) 
    {
        Packet *pPacket = (Packet *)raw();
        //INFO("Has a Packet");
        // block unnecessary packet
        if (PreProcess())
        {
            // delete a packet
            del(pPacket->GetPacketLen());
            return false;
        }
        else
        {
            // copy packet
            //packet = *pPacket; // this may dangerous
            ::memcpy(&packet, pPacket, pPacket->GetPacketLen());

            del(pPacket->GetPacketLen());
            return true;
        }
    }
    return false;
}
Ejemplo n.º 2
0
bool PacketBuffer::PacketVaild()
{
    if (size() > 0)
    {
        //DEBUG("recv: %d", size());
        //DEBUG_HEX(raw(), size());
        Packet *pPacket = (Packet *)raw();
        // check length
        if (size() >= Packet::PACKET_MINLEN &&
            size() >= pPacket->GetPacketLen())
        {
            // check checksum and packet end
            WORD wCRC = __le16_to_cpu(*(WORD *)&(pPacket->GetData()[pPacket->GetDataLen()]));
            WORD wCalCRC = pPacket->CheckSUM(&pPacket->m_btAddr, pPacket->GetPacketLen()-2);
            if (wCRC == wCalCRC)
            {
                DEBUG("recv: %d", size());
                DEBUG_HEX(raw(), size());
                return true;
            }
            ERROR("serialport packet checksum wrong calc:%04x recv:%04X", wCalCRC, wCRC);
            DEBUG_HEX(raw(), size());
        }
        else
        {
            return false;
        }

        // throw exception
        throw(Packet::EXCEPTION_PACKET);
    }
    else
    {
        return false;
    }
}