Cirrious.MvvmCross.Plugins.Sphero.Messages.SpheroResponse.Validate C# (CSharp) Method

Validate() public method

public Validate ( ) : bool
return bool
        public bool Validate()
        {
            if (StartOfPacket1 != 0xFF)
                return false;
            if (StartOfPacket2 != 0xFF && StartOfPacket2 != 0xFE)
                return false;
            if (DataLength != (Payload.Count + 1))
                return false;

            var calculated = 0x00;
            calculated += MessageResponse;
            calculated += SequenceNumber;
            calculated += DataLength;
            foreach (var x in Payload)
            {
                calculated += x;
            }

            var inverted = (byte) (calculated ^ 0xFFFFFFFF);
            return inverted == CheckSum;
        }
    }

Usage Example

        public void Add(byte x)
        {
            if (IsErrored)
            {
                throw new SpheroPluginException("Response already errored. You cannot add more bytes to it.");
            }

            switch (_currentState)
            {
            case State.WaitingForSop1:
                if (x != 0xff)
                {
                    _currentState = State.ErrorHeader;
                    throw new SpheroPluginException("Unexpected packet SOP1 header byte {0}", x);
                }
                _response.StartOfPacket1 = x;
                _currentState++;
                break;

            case State.WaitingForSop2:
                if (x != 0xff && x != 0xfe)
                {
                    _currentState = State.ErrorHeader;
                    throw new SpheroPluginException("Unexpected packet SOP2 header byte {0}", x);
                }
                _response.StartOfPacket2 = x;
                _currentState++;
                break;

            case State.WaitingForMrsp:
                _response.MessageResponse = x;
                _currentState++;
                break;

            case State.WaitingForSeq:
                _response.SequenceNumber = x;
                _currentState++;
                break;

            case State.WaitingForDlen:
                _response.DataLength = x;
                _currentState++;
                break;

            case State.WaitingForPayloadAndCheckSum:
                if (_response.DataLength - _response.Payload.Count > 1)
                {
                    _response.Payload.Add(x);
                }
                else
                {
                    _response.CheckSum = x;
                    if (!_response.Validate())
                    {
                        _currentState = State.ErrorCheckSum;
                        throw new SpheroPluginException("CheckSum error in received packet");
                    }
                    _currentState++;
                }
                break;

            case State.Complete:
                _currentState = State.ErrorOverflow;
                throw new SpheroPluginException("You cannot add any more bytes to this complete response");

            case State.Error:
            case State.ErrorHeader:
            case State.ErrorCheckSum:
            case State.ErrorOverflow:
                // do nothing
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }