Exemplo n.º 1
0
 public void ResetParser()
 {
     lock (_locker)
     {
         _parserState = FrameReceiveStates.FRS_SOF_HUNT;
         ResetReceivingDataFrameBuffer();
     }
 }
Exemplo n.º 2
0
        private byte[] ParseRawData(byte buffer, bool isOutcome, DateTime timeStamp, bool isFromFile)
        {
            byte[] ret = null;
            AddToReceivingDataFrameBuffer(buffer);
            lock (_locker)
            {
                switch (_parserState)
                {
                case FrameReceiveStates.FRS_SOF_HUNT:
                    _isTestInterface = false;
                    if (HeaderTypes.StartOfFrame == (HeaderTypes)buffer)
                    {
                        ResetReceivingDataFrameBuffer();
                        AddToReceivingDataFrameBuffer(buffer);
                        ReceivingDataFrame = new DataFrame(SessionId, DataFrameTypes.Data, isFromFile, isOutcome, timeStamp);
                        _parserState       = FrameReceiveStates.FRS_LENGTH;
                    }
                    // Acknowledge received from peer.
                    else if (HeaderTypes.Acknowledge == (HeaderTypes)buffer)
                    {
                        FrameBufferBlock.Acknowledge(true);
                        ReceivingDataFrame = new DataFrame(SessionId, DataFrameTypes.Ack, isFromFile, isOutcome, timeStamp);
                        ReceivingDataFrame.SetBuffer(ACK, 0, 1);
                        OnFrameReceived();
                    }
                    // Not Acknowledge received from peer.
                    else if (HeaderTypes.NotAcknowledged == (HeaderTypes)buffer)
                    {
                        ReceivingDataFrame = new DataFrame(SessionId, DataFrameTypes.NAck, isFromFile, isOutcome, timeStamp);
                        ReceivingDataFrame.SetBuffer(NAK, 0, 1);
                        OnFrameReceived();
                    }
                    // CAN frame received - peer dropped a data frame transmitted by us.
                    else if (HeaderTypes.Can == (HeaderTypes)buffer)
                    {
                        FrameBufferBlock.Acknowledge(false);
                        ReceivingDataFrame = new DataFrame(SessionId, DataFrameTypes.CAN, isFromFile, isOutcome, timeStamp);
                        ReceivingDataFrame.SetBuffer(CAN, 0, 1);
                        OnFrameReceived();
                    }
                    break;

                case FrameReceiveStates.FRS_LENGTH:
                    if (buffer == 0)
                    {
                        _parserState     = FrameReceiveStates.FRS_LENGHT2;
                        _isTestInterface = true;
                    }
                    else if (buffer < MIN_FRAME_SIZE || buffer > MAX_FRAME_SIZE)
                    {
                        _parserState = FrameReceiveStates.FRS_SOF_HUNT;
                    }
                    else
                    {
                        ReceivingDataFrameLength = (byte)(buffer + 1);     // Payload size is excluding len field.
                        _parserState             = FrameReceiveStates.FRS_TYPE;
                    }
                    break;

                case FrameReceiveStates.FRS_LENGHT2:
                    if (buffer < MIN_FRAME_SIZE || buffer > MAX_FRAME_SIZE)
                    {
                        _parserState = FrameReceiveStates.FRS_SOF_HUNT;
                    }
                    else
                    {
                        ReceivingDataFrameLength = (byte)(buffer + 1);     // Payload size is excluding len field.
                        _parserState             = FrameReceiveStates.FRS_COMMAND;
                    }
                    break;

                case FrameReceiveStates.FRS_TYPE:
                    if (buffer == (byte)FrameTypes.Request || buffer == (byte)FrameTypes.Response)
                    {
                        _parserState = FrameReceiveStates.FRS_COMMAND;
                    }
                    else
                    {
                        _parserState = FrameReceiveStates.FRS_SOF_HUNT;
                    }
                    break;

                case FrameReceiveStates.FRS_COMMAND:
                    if (_receivingDataFrameLengthCounter == ReceivingDataFrameLength)
                    {
                        _parserState = FrameReceiveStates.FRS_CHECKSUM;
                    }
                    else
                    {
                        _parserState = FrameReceiveStates.FRS_DATA;
                    }
                    break;

                case FrameReceiveStates.FRS_DATA:
                    if (_receivingDataFrameLengthCounter > ReceivingDataFrameLength)
                    {
                        _parserState = FrameReceiveStates.FRS_SOF_HUNT;
                    }
                    else if (_receivingDataFrameLengthCounter == ReceivingDataFrameLength)
                    {
                        _parserState = FrameReceiveStates.FRS_CHECKSUM;
                    }
                    break;

                case FrameReceiveStates.FRS_CHECKSUM:
                    // Frame received successfully -> Send acknowledge (ACK).
                    ReceivingDataFrame.SetBuffer(_receivingDataFrameBuffer, 0, _receivingDataFrameLengthCounter);
                    if (_isTestInterface)
                    {
                        if (!isFromFile)
                        {
                            WriteData(ACK);
                        }
                    }
                    else
                    {
                        if (!isFromFile && ReceivingDataFrame.IsChecksumValid(buffer))
                        {
                            WriteData(ACK);
                        }
                        // Frame receive failed -> Send NAK.
                        else if (!isFromFile)
                        {
                            WriteData(NAK);
                        }
                    }
                    OnFrameReceived();
                    _parserState = FrameReceiveStates.FRS_SOF_HUNT;
                    break;

                case FrameReceiveStates.FRS_RX_TIMEOUT:
                default:
                    _parserState = FrameReceiveStates.FRS_SOF_HUNT;
                    break;
                }
            }
            return(ret);
        }